Skip to content

Commit

Permalink
-Added new scene conversion to binary on export (disabled by default,…
Browse files Browse the repository at this point in the history
… please test)

-This method works by directly converting text to binary, so the scene does not need to be loaded and saved
  • Loading branch information
reduz committed Dec 15, 2017
1 parent 01c04d6 commit 2514338
Show file tree
Hide file tree
Showing 7 changed files with 670 additions and 250 deletions.
68 changes: 45 additions & 23 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ StringName ResourceInteractiveLoaderBinary::_get_string() {

uint32_t id = f->get_32();
if (id & 0x80000000) {
int len = id & 0x7FFFFFFF;
uint len = id & 0x7FFFFFFF;
if (len > str_buf.size()) {
str_buf.resize(len);
}
Expand Down Expand Up @@ -734,6 +734,7 @@ Error ResourceInteractiveLoaderBinary::poll() {
for (int i = 0; i < pc; i++) {

StringName name = _get_string();

if (name == StringName()) {
error = ERR_FILE_CORRUPT;
ERR_FAIL_V(ERR_FILE_CORRUPT);
Expand Down Expand Up @@ -902,7 +903,9 @@ void ResourceInteractiveLoaderBinary::open(FileAccess *p_f) {

ExtResource er;
er.type = get_unicode_string();

er.path = get_unicode_string();

external_resources.push_back(er);
}

Expand Down Expand Up @@ -1271,7 +1274,7 @@ String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

void ResourceFormatSaverBinaryInstance::_pad_buffer(int p_bytes) {
void ResourceFormatSaverBinaryInstance::_pad_buffer(FileAccess *f, int p_bytes) {

int extra = 4 - (p_bytes % 4);
if (extra < 4) {
Expand All @@ -1280,7 +1283,12 @@ void ResourceFormatSaverBinaryInstance::_pad_buffer(int p_bytes) {
}
}

void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, const PropertyInfo &p_hint) {
void ResourceFormatSaverBinaryInstance::_write_variant(const Variant &p_property, const PropertyInfo &p_hint) {

write_variant(f, p_property, resource_set, external_resources, string_map, p_hint);
}

void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Variant &p_property, Set<RES> &resource_set, Map<RES, int> &external_resources, Map<StringName, int> &string_map, const PropertyInfo &p_hint) {

switch (p_property.get_type()) {

Expand Down Expand Up @@ -1327,7 +1335,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property,

f->store_32(VARIANT_STRING);
String val = p_property;
save_unicode_string(val);
save_unicode_string(f, val);

} break;
case Variant::VECTOR2: {
Expand Down Expand Up @@ -1453,10 +1461,20 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property,
if (np.is_absolute())
snc |= 0x8000;
f->store_16(snc);
for (int i = 0; i < np.get_name_count(); i++)
f->store_32(get_string_index(np.get_name(i)));
for (int i = 0; i < np.get_subname_count(); i++)
f->store_32(get_string_index(np.get_subname(i)));
for (int i = 0; i < np.get_name_count(); i++) {
if (string_map.has(np.get_name(i))) {
f->store_32(string_map[np.get_name(i)]);
} else {
save_unicode_string(f, np.get_name(i), true);
}
}
for (int i = 0; i < np.get_subname_count(); i++) {
if (string_map.has(np.get_subname(i))) {
f->store_32(string_map[np.get_subname(i)]);
} else {
save_unicode_string(f, np.get_subname(i), true);
}
}

} break;
case Variant::_RID: {
Expand Down Expand Up @@ -1508,8 +1526,8 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property,
continue;
*/

write_variant(E->get());
write_variant(d[E->get()]);
write_variant(f, E->get(), resource_set, external_resources, string_map);
write_variant(f, d[E->get()], resource_set, external_resources, string_map);
}

} break;
Expand All @@ -1520,7 +1538,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property,
f->store_32(uint32_t(a.size()));
for (int i = 0; i < a.size(); i++) {

write_variant(a[i]);
write_variant(f, a[i], resource_set, external_resources, string_map);
}

} break;
Expand All @@ -1532,7 +1550,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property,
f->store_32(len);
PoolVector<uint8_t>::Read r = arr.read();
f->store_buffer(r.ptr(), len);
_pad_buffer(len);
_pad_buffer(f, len);

} break;
case Variant::POOL_INT_ARRAY: {
Expand Down Expand Up @@ -1566,7 +1584,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property,
f->store_32(len);
PoolVector<String>::Read r = arr.read();
for (int i = 0; i < len; i++) {
save_unicode_string(r[i]);
save_unicode_string(f, r[i]);
}

} break;
Expand Down Expand Up @@ -1693,10 +1711,14 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
}
}

void ResourceFormatSaverBinaryInstance::save_unicode_string(const String &p_string) {
void ResourceFormatSaverBinaryInstance::save_unicode_string(FileAccess *f, const String &p_string, bool p_bit_on_len) {

CharString utf8 = p_string.utf8();
f->store_32(utf8.length() + 1);
if (p_bit_on_len) {
f->store_32(utf8.length() + 1 | 0x80000000);
} else {
f->store_32(utf8.length() + 1);
}
f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
}

Expand Down Expand Up @@ -1763,7 +1785,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p
return ERR_CANT_CREATE;
}

save_unicode_string(p_resource->get_class());
save_unicode_string(f, p_resource->get_class());
f->store_64(0); //offset to import metadata
for (int i = 0; i < 14; i++)
f->store_32(0); // reserved
Expand Down Expand Up @@ -1800,7 +1822,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p

f->store_32(strings.size()); //string table size
for (int i = 0; i < strings.size(); i++) {
save_unicode_string(strings[i]);
save_unicode_string(f, strings[i]);
}

// save external resource table
Expand All @@ -1814,10 +1836,10 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p

for (int i = 0; i < save_order.size(); i++) {

save_unicode_string(save_order[i]->get_save_class());
save_unicode_string(f, save_order[i]->get_save_class());
String path = save_order[i]->get_path();
path = relative_paths ? local_path.path_to_file(path) : path;
save_unicode_string(path);
save_unicode_string(f, path);
}
// save internal resource table
f->store_32(saved_resources.size()); //amount of internal resources
Expand Down Expand Up @@ -1853,15 +1875,15 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p
used_indices.insert(new_subindex);
}

save_unicode_string("local://" + itos(r->get_subindex()));
save_unicode_string(f, "local://" + itos(r->get_subindex()));
if (takeover_paths) {
r->set_path(p_path + "::" + itos(r->get_subindex()), true);
}
#ifdef TOOLS_ENABLED
r->set_edited(false);
#endif
} else {
save_unicode_string(r->get_path()); //actual external
save_unicode_string(f, r->get_path()); //actual external
}
ofs_pos.push_back(f->get_position());
f->store_64(0); //offset in 64 bits
Expand All @@ -1875,14 +1897,14 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p
ResourceData &rd = E->get();

ofs_table.push_back(f->get_position());
save_unicode_string(rd.type);
save_unicode_string(f, rd.type);
f->store_32(rd.properties.size());

for (List<Property>::Element *F = rd.properties.front(); F; F = F->next()) {

Property &p = F->get();
f->store_32(p.name_idx);
write_variant(p.value, F->get().pi);
_write_variant(p.value, F->get().pi);
}
}

Expand Down
7 changes: 4 additions & 3 deletions core/io/resource_format_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ class ResourceFormatSaverBinaryInstance {
List<Property> properties;
};

void _pad_buffer(int p_bytes);
void write_variant(const Variant &p_property, const PropertyInfo &p_hint = PropertyInfo());
static void _pad_buffer(FileAccess *f, int p_bytes);
void _write_variant(const Variant &p_property, const PropertyInfo &p_hint = PropertyInfo());
void _find_resources(const Variant &p_variant, bool p_main = false);
void save_unicode_string(const String &p_string);
static void save_unicode_string(FileAccess *f, const String &p_string, bool p_bit_on_len = false);
int get_string_index(const String &p_string);

public:
Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
static void write_variant(FileAccess *f, const Variant &p_property, Set<RES> &resource_set, Map<RES, int> &external_resources, Map<StringName, int> &string_map, const PropertyInfo &p_hint = PropertyInfo());
};

class ResourceFormatSaverBinary : public ResourceFormatSaver {
Expand Down
31 changes: 29 additions & 2 deletions editor/editor_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
#include "io/zip_io.h"
#include "os/file_access.h"
#include "project_settings.h"
#include "scene/resources/scene_format_text.h"
#include "script_language.h"
#include "version.h"

#include "thirdparty/misc/md5.h"
#include "version.h"

static int _get_pad(int p_alignment, int p_n) {

Expand Down Expand Up @@ -1399,3 +1399,30 @@ EditorExportPlatformPC::EditorExportPlatformPC() {

chmod_flags = -1;
}

///////////////////////

void EditorExportTextSceneToBinaryPlugin::_export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {

String extension = p_path.get_extension().to_lower();
if (extension != "tres" && extension != "tscn") {
return;
}

print_line("exporting " + p_path);

bool convert = GLOBAL_GET("editor/convert_text_resources_to_binary_on_export");
if (!convert)
return;
String tmp_path = EditorSettings::get_singleton()->get_cache_dir().plus_file("file.res");
Error err = ResourceFormatLoaderText::convert_file_to_binary(p_path, tmp_path);
ERR_FAIL_COND(err != OK);
Vector<uint8_t> data = FileAccess::get_file_as_array(tmp_path);
ERR_FAIL_COND(data.size() == 0);
add_file(p_path + ".converted.res", data, true);
}

EditorExportTextSceneToBinaryPlugin::EditorExportTextSceneToBinaryPlugin() {

GLOBAL_DEF("editor/convert_text_resources_to_binary_on_export", false);
}
9 changes: 9 additions & 0 deletions editor/editor_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,13 @@ class EditorExportPlatformPC : public EditorExportPlatform {
EditorExportPlatformPC();
};

class EditorExportTextSceneToBinaryPlugin : public EditorExportPlugin {

GDCLASS(EditorExportTextSceneToBinaryPlugin, EditorExportPlugin)

public:
virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features);
EditorExportTextSceneToBinaryPlugin();
};

#endif // EDITOR_IMPORT_EXPORT_H
5 changes: 5 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5718,6 +5718,11 @@ EditorNode::EditorNode() {
editor_plugins_force_over = memnew(EditorPluginList);
editor_plugins_force_input_forwarding = memnew(EditorPluginList);

Ref<EditorExportTextSceneToBinaryPlugin> export_text_to_binary_plugin;
export_text_to_binary_plugin.instance();

EditorExport::get_singleton()->add_export_plugin(export_text_to_binary_plugin);

_edit_current();
current = NULL;

Expand Down
Loading

0 comments on commit 2514338

Please sign in to comment.