Skip to content

Commit

Permalink
Ensure to correctly compress release builds, closes #184 (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain authored Jul 7, 2024
1 parent a4dcc34 commit add509f
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 91 deletions.
11 changes: 7 additions & 4 deletions TestScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ extends CenterContainer
@export var entity:PandoraEntity


@onready var label = $Label


func _ready() -> void:
print(item.get_entity_id(), " - ", item.get_entity_name())
label.text += item.get_entity_id() + " - "+ item.get_entity_name() + '\n'
var copper_ore = Pandora.get_entity(Ores.COPPER_ORE) as Item
print(copper_ore.get_entity_name())
label.text += copper_ore.get_entity_name() + '\n'

var copper_instance = copper_ore.instantiate()

print(copper_instance.get_string("Description"))
label.text += copper_instance.get_string("Description") + '\n'

print(copper_ore.get_rarity().get_rarity_color())
label.text += str(copper_ore.get_rarity().get_rarity_color()) + '\n'
3 changes: 3 additions & 0 deletions TestScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ entity = SubResource("Resource_xfcj2")
[node name="TextureRect" type="TextureRect" parent="."]
layout_mode = 2
texture = ExtResource("1_r3xd3")

[node name="Label" type="Label" parent="."]
layout_mode = 2
6 changes: 3 additions & 3 deletions addons/pandora/api.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ signal import_calculation_failed(reason: String)
signal import_progress

var _context_manager: PandoraContextManager
var _storage: PandoraJsonDataStorage
var _storage: PandoraDataStorage
var _id_generator: PandoraIDGenerator
var _entity_backend: PandoraEntityBackend

Expand Down Expand Up @@ -164,7 +164,7 @@ func save_data() -> void:


func calculate_import_data(path: String) -> int:
var imported_data = _storage._load_from_file(path)
var imported_data = _storage.load_from_file(path)
var total_items = 0
if not imported_data.has("_entity_data"):
import_calculation_failed.emit("Provided file is invalid or is corrupted.")
Expand Down Expand Up @@ -192,7 +192,7 @@ func calculate_import_data(path: String) -> int:


func import_data(path: String) -> int:
var imported_data = _storage._load_from_file(path)
var imported_data = _storage.load_from_file(path)
if not imported_data.has("_entity_data"):
import_failed.emit("Provided file is invalid or is corrupted.")
return 0
Expand Down
34 changes: 18 additions & 16 deletions addons/pandora/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extends EditorPlugin
const PandoraEditor := preload("res://addons/pandora/ui/editor/pandora_editor.tscn")
const PandoraIcon := preload("res://addons/pandora/icons/pandora-icon.svg")
const PandoraEntityInspector = preload("res://addons/pandora/ui/editor/inspector/entity_instance_inspector.gd")
const Compression = preload("res://addons/pandora/util/compression.gd")

var editor_view
var entity_inspector
Expand All @@ -19,12 +20,12 @@ func _enter_tree() -> void:
add_autoload_singleton("Pandora", "res://addons/pandora/api.gd")
add_export_plugin(_exporter)
PandoraSettings.initialize()

if Engine.is_editor_hint():
editor_view = PandoraEditor.instantiate()
editor_view.hide()
get_editor_interface().get_editor_main_screen().add_child(editor_view)

# connect signals for error handling
get_editor_interface().get_resource_filesystem().resources_reimported.connect(func(res): if editor_view.has_method("reattempt_load_on_error"): editor_view.reattempt_load_on_error())
get_editor_interface().get_resource_filesystem().sources_changed.connect(func(res): if editor_view.has_method("reattempt_load_on_error"): editor_view.reattempt_load_on_error())
Expand All @@ -33,10 +34,10 @@ func _enter_tree() -> void:

entity_inspector = PandoraEntityInspector.new()
add_inspector_plugin(entity_inspector)

_make_visible(false)


func _apply_changes() -> void:
if Engine.is_editor_hint() and is_instance_valid(editor_view):
if editor_view.has_method("apply_changes"):
Expand All @@ -48,9 +49,9 @@ func _exit_tree() -> void:
remove_control_from_bottom_panel(editor_view)
editor_view.queue_free()
remove_inspector_plugin(entity_inspector)

Engine.remove_meta("PandoraEditorPlugin")

remove_export_plugin(_exporter)
remove_autoload_singleton("Pandora")

Expand All @@ -66,7 +67,7 @@ func _has_main_screen() -> bool:

func _get_plugin_name() -> String:
return "Pandora"


func _get_plugin_icon() -> Texture2D:
return PandoraIcon
Expand All @@ -75,14 +76,15 @@ class PandoraExportPlugin extends EditorExportPlugin:
# Override the _export_begin method to add the data.pandora file during export
func _export_begin(features: PackedStringArray, is_debug: bool, path: String, flags: int):
var pandora_path = "res://data.pandora"
var file: FileAccess
if is_debug:
file = FileAccess.open(pandora_path, FileAccess.READ)
else:
file = FileAccess.open_compressed(pandora_path, FileAccess.READ)

if file:
add_file(pandora_path, file.get_buffer(file.get_length()), false)
var file = FileAccess.open(pandora_path, FileAccess.READ)
if not file:
printerr("Unable to export Pandora data: ", FileAccess.get_open_error())
return
var data:PackedByteArray = file.get_buffer(file.get_length())
if not is_debug:
var text = file.get_as_text()
data = Compression.compress(text)
add_file(pandora_path, data, false)

func _get_name() -> String:
return "PandoraExporter"
4 changes: 4 additions & 0 deletions addons/pandora/storage/data_storage.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func store_all_data(data: Dictionary, context_id: String) -> Dictionary:

func get_all_data(context_id: String) -> Dictionary:
return {}


func load_from_file(file_path: String) -> Dictionary:
return {}
47 changes: 26 additions & 21 deletions addons/pandora/storage/json/json_data_storage.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ func _init(data_dir: String):
func store_all_data(data: Dictionary, context_id: String) -> Dictionary:
var file_path = _get_file_path(context_id)
var file: FileAccess
if OS.is_debug_build():
file = FileAccess.open(file_path, FileAccess.WRITE)
file.store_string(JSON.stringify(data, "\t"))
else:
if _should_use_compression():
file = FileAccess.open_compressed(file_path, FileAccess.WRITE)
file.store_string(JSON.stringify(data))
else:
file = FileAccess.open(file_path, FileAccess.WRITE)
file.store_string(JSON.stringify(data, "\t"))
file.close()
return data


func get_all_data(context_id: String) -> Dictionary:
var file_path = _get_file_path(context_id)
var file: FileAccess
if OS.is_debug_build():
file = FileAccess.open(file_path, FileAccess.READ)
else:
if _should_use_compression():
file = FileAccess.open_compressed(file_path, FileAccess.READ)
else:
file = FileAccess.open(file_path, FileAccess.READ)
var json: JSON = JSON.new()
if file != null:
var text = file.get_as_text()
Expand All @@ -68,6 +68,22 @@ func get_decompressed_data(file_path: String) -> Dictionary:
return {}


func load_from_file(file_path: String) -> Dictionary:
var file: FileAccess
if _should_use_compression():
file = FileAccess.open_compressed(file_path, FileAccess.READ)
else:
file = FileAccess.open(file_path, FileAccess.READ)
if FileAccess.file_exists(file_path) and file != null:
var content = file.get_as_text()
file.close()
var json = JSON.new()
json.parse(content)
return json.get_data()
else:
return {}


func _get_directory_path(context_id: String) -> String:
var directory_path = ""
if data_directory.ends_with("//"):
Expand All @@ -87,17 +103,6 @@ func _get_file_path(context_id: String) -> String:
return "%s/data.pandora" % [_get_directory_path(context_id)]


func _load_from_file(file_path: String) -> Dictionary:
var file: FileAccess
if OS.is_debug_build():
file = FileAccess.open(file_path, FileAccess.READ)
else:
file = FileAccess.open_compressed(file_path, FileAccess.READ)
if FileAccess.file_exists(file_path) and file != null:
var content = file.get_as_text()
file.close()
var json = JSON.new()
json.parse(content)
return json.get_data()
else:
return {}
func _should_use_compression() -> bool:
# Ensure within the Godot Engine editor Pandora remains uncompressed
return not Engine.is_editor_hint() and not OS.is_debug_build()
68 changes: 68 additions & 0 deletions addons/pandora/util/compression.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const BLOCK_SIZE = 4096
const MAGIC = "GCPF"


## magic
## char[4] "GCPF"
##
## header
## uint32_t compression_mode (Compression::MODE_ZSTD by default)
## uint32_t block_size (4096 by default)
## uint32_t uncompressed_size
##
## block compressed sizes, number of blocks = (uncompressed_size / block_size) + 1
## uint32_t block_sizes[]
##
## followed by compressed block data, same as calling `compress` for each source `block_size`
static func compress(text: String, compression_mode:FileAccess.CompressionMode = FileAccess.COMPRESSION_FASTLZ) -> PackedByteArray:
var data = _encode_string(text)
var uncompressed_size = data.size()

var num_blocks = int(ceil(float(uncompressed_size) / BLOCK_SIZE))

var buffer = PackedByteArray()

buffer.append_array(_encode_string(MAGIC))

buffer.append_array(_encode_uint32(compression_mode))
buffer.append_array(_encode_uint32(BLOCK_SIZE))
buffer.append_array(_encode_uint32(uncompressed_size))

var block_sizes = PackedByteArray()
var compressed_blocks = []

for i in range(num_blocks):
var start = i * BLOCK_SIZE
var end = min((i + 1) * BLOCK_SIZE, uncompressed_size)
var block_data = PackedByteArray()
var block_index = start
while block_index < end:
block_data.append(data[block_index])
block_index += 1

var compressed_block = block_data.compress(compression_mode)
block_sizes.append_array(_encode_uint32(compressed_block.size()))
compressed_blocks.append(compressed_block)

buffer.append_array(block_sizes)

for block in compressed_blocks:
buffer.append_array(block)

return buffer


static func _encode_uint32(value: int) -> PackedByteArray:
var arr = PackedByteArray()
arr.append(value & 0xFF)
arr.append((value >> 8) & 0xFF)
arr.append((value >> 16) & 0xFF)
arr.append((value >> 24) & 0xFF)
return arr


static func _encode_string(value: String) -> PackedByteArray:
var arr = PackedByteArray()
for char in value:
arr.append_array(char.to_ascii_buffer())
return arr
4 changes: 2 additions & 2 deletions examples/inventory/inventory.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class_name Inventory extends Node

signal item_added(item:Item, index:int)
signal item_removed(item:Item, index:int)


# id -> ItemInstance
var _slots = {}
Expand All @@ -24,7 +24,7 @@ func add_item(item:Item, index:int) -> void:
return
existing_item.set_current_stacksize(new_stacksize)
item_added.emit(existing_item, index)


func remove_item(index:int) -> Item:
if _slots.has(index):
Expand Down
1 change: 0 additions & 1 deletion examples/inventory/inventory_example.gd

This file was deleted.

40 changes: 0 additions & 40 deletions examples/inventory/inventory_example.tscn

This file was deleted.

18 changes: 14 additions & 4 deletions pandora/categories.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
class_name PandoraCategories


const ROOT = "m0LT2ut_BO"
const ITEMS = "1"
const RARITY = "16"
const QUESTS = "27"
const DIALOGUES = "28"
const SPELLS = "29"
const NPCS = "2"


class RootCategories:
const ITEMS = "dhuQ_I1owC"
const NPCS = "8-oXvCMZmH"
class ItemsCategories:
const ARMORY = "3"
const TOOLS = "4"
const ORES = "13"


class ArmoryCategories:
const SHIELDS = "8"


0 comments on commit add509f

Please sign in to comment.