Skip to content

Commit c3a650a

Browse files
committed
Remove unused code to convert text resource format to binary
This is now handled in `ResourceSaver::save` when saving with a binary extension.
1 parent b9e0223 commit c3a650a

File tree

2 files changed

+0
-330
lines changed

2 files changed

+0
-330
lines changed

scene/resources/resource_format_text.cpp

Lines changed: 0 additions & 322 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,14 @@
3333
#include "core/config/project_settings.h"
3434
#include "core/io/dir_access.h"
3535
#include "core/io/missing_resource.h"
36-
#include "core/io/resource_format_binary.h"
3736
#include "core/object/script_language.h"
38-
#include "core/version.h"
3937

4038
// Version 2: changed names for Basis, AABB, Vectors, etc.
4139
// Version 3: new string ID for ext/subresources, breaks forward compat.
4240
// Version 4: PackedByteArray is now stored as base64 encoded.
4341
#define FORMAT_VERSION_COMPAT 3
4442
#define FORMAT_VERSION 4
4543

46-
#define BINARY_FORMAT_VERSION 4
47-
48-
#include "core/io/dir_access.h"
49-
#include "core/version.h"
50-
5144
#define _printerr() ERR_PRINT(String(res_path + ":" + itos(lines) + " - Parse Error: " + error_text).utf8().get_data());
5245

5346
///
@@ -1126,298 +1119,6 @@ void ResourceLoaderText::open(Ref<FileAccess> p_f, bool p_skip_first_tag) {
11261119
rp.userdata = this;
11271120
}
11281121

1129-
static void bs_save_unicode_string(Ref<FileAccess> p_f, const String &p_string, bool p_bit_on_len = false) {
1130-
CharString utf8 = p_string.utf8();
1131-
if (p_bit_on_len) {
1132-
p_f->store_32((utf8.length() + 1) | 0x80000000);
1133-
} else {
1134-
p_f->store_32(utf8.length() + 1);
1135-
}
1136-
p_f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1);
1137-
}
1138-
1139-
Error ResourceLoaderText::save_as_binary(const String &p_path) {
1140-
if (error) {
1141-
return error;
1142-
}
1143-
1144-
Ref<FileAccess> wf = FileAccess::open(p_path, FileAccess::WRITE);
1145-
if (wf.is_null()) {
1146-
return ERR_CANT_OPEN;
1147-
}
1148-
1149-
//save header compressed
1150-
static const uint8_t header[4] = { 'R', 'S', 'R', 'C' };
1151-
wf->store_buffer(header, 4);
1152-
1153-
wf->store_32(0); //endianness, little endian
1154-
wf->store_32(0); //64 bits file, false for now
1155-
wf->store_32(VERSION_MAJOR);
1156-
wf->store_32(VERSION_MINOR);
1157-
static const int save_format_version = BINARY_FORMAT_VERSION;
1158-
wf->store_32(save_format_version);
1159-
1160-
bs_save_unicode_string(wf, is_scene ? "PackedScene" : resource_type);
1161-
wf->store_64(0); //offset to import metadata, this is no longer used
1162-
1163-
wf->store_32(ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS | ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS);
1164-
1165-
wf->store_64(res_uid);
1166-
1167-
for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
1168-
wf->store_32(0); // reserved
1169-
}
1170-
1171-
wf->store_32(0); //string table size, will not be in use
1172-
uint64_t ext_res_count_pos = wf->get_position();
1173-
1174-
wf->store_32(0); //zero ext resources, still parsing them
1175-
1176-
//go with external resources
1177-
1178-
DummyReadData dummy_read;
1179-
VariantParser::ResourceParser rp_new;
1180-
rp_new.ext_func = _parse_ext_resource_dummys;
1181-
rp_new.sub_func = _parse_sub_resource_dummys;
1182-
rp_new.userdata = &dummy_read;
1183-
1184-
while (next_tag.name == "ext_resource") {
1185-
if (!next_tag.fields.has("path")) {
1186-
error = ERR_FILE_CORRUPT;
1187-
error_text = "Missing 'path' in external resource tag";
1188-
_printerr();
1189-
return error;
1190-
}
1191-
1192-
if (!next_tag.fields.has("type")) {
1193-
error = ERR_FILE_CORRUPT;
1194-
error_text = "Missing 'type' in external resource tag";
1195-
_printerr();
1196-
return error;
1197-
}
1198-
1199-
if (!next_tag.fields.has("id")) {
1200-
error = ERR_FILE_CORRUPT;
1201-
error_text = "Missing 'id' in external resource tag";
1202-
_printerr();
1203-
return error;
1204-
}
1205-
1206-
String path = next_tag.fields["path"];
1207-
String type = next_tag.fields["type"];
1208-
String id = next_tag.fields["id"];
1209-
ResourceUID::ID uid = ResourceUID::INVALID_ID;
1210-
if (next_tag.fields.has("uid")) {
1211-
String uidt = next_tag.fields["uid"];
1212-
uid = ResourceUID::get_singleton()->text_to_id(uidt);
1213-
}
1214-
1215-
bs_save_unicode_string(wf, type);
1216-
bs_save_unicode_string(wf, path);
1217-
wf->store_64(uid);
1218-
1219-
int lindex = dummy_read.external_resources.size();
1220-
Ref<DummyResource> dr;
1221-
dr.instantiate();
1222-
dr->set_path("res://dummy" + itos(lindex)); //anything is good to detect it for saving as external
1223-
dummy_read.external_resources[dr] = lindex;
1224-
dummy_read.rev_external_resources[id] = dr;
1225-
1226-
error = VariantParser::parse_tag(&stream, lines, error_text, next_tag, &rp_new);
1227-
1228-
if (error) {
1229-
_printerr();
1230-
return error;
1231-
}
1232-
}
1233-
1234-
// save external resource table
1235-
wf->seek(ext_res_count_pos);
1236-
wf->store_32(dummy_read.external_resources.size());
1237-
wf->seek_end();
1238-
1239-
//now, save resources to a separate file, for now
1240-
1241-
uint64_t sub_res_count_pos = wf->get_position();
1242-
wf->store_32(0); //zero sub resources, still parsing them
1243-
1244-
String temp_file = p_path + ".temp";
1245-
Vector<uint64_t> local_offsets;
1246-
Vector<uint64_t> local_pointers_pos;
1247-
{
1248-
Ref<FileAccess> wf2 = FileAccess::open(temp_file, FileAccess::WRITE);
1249-
if (wf2.is_null()) {
1250-
return ERR_CANT_OPEN;
1251-
}
1252-
1253-
while (next_tag.name == "sub_resource" || next_tag.name == "resource") {
1254-
String type;
1255-
String id;
1256-
bool main_res;
1257-
1258-
if (next_tag.name == "sub_resource") {
1259-
if (!next_tag.fields.has("type")) {
1260-
error = ERR_FILE_CORRUPT;
1261-
error_text = "Missing 'type' in external resource tag";
1262-
_printerr();
1263-
return error;
1264-
}
1265-
1266-
if (!next_tag.fields.has("id")) {
1267-
error = ERR_FILE_CORRUPT;
1268-
error_text = "Missing 'id' in external resource tag";
1269-
_printerr();
1270-
return error;
1271-
}
1272-
1273-
type = next_tag.fields["type"];
1274-
id = next_tag.fields["id"];
1275-
main_res = false;
1276-
1277-
if (!dummy_read.resource_map.has(id)) {
1278-
Ref<DummyResource> dr;
1279-
dr.instantiate();
1280-
dr->set_scene_unique_id(id);
1281-
dummy_read.resource_map[id] = dr;
1282-
uint32_t im_size = dummy_read.resource_index_map.size();
1283-
dummy_read.resource_index_map.insert(dr, im_size);
1284-
}
1285-
1286-
} else {
1287-
type = res_type;
1288-
String uid_text = ResourceUID::get_singleton()->id_to_text(res_uid);
1289-
id = type + "_" + uid_text.replace("uid://", "").replace("<invalid>", "0");
1290-
main_res = true;
1291-
}
1292-
1293-
local_offsets.push_back(wf2->get_position());
1294-
1295-
bs_save_unicode_string(wf, "local://" + id);
1296-
local_pointers_pos.push_back(wf->get_position());
1297-
wf->store_64(0); //temp local offset
1298-
1299-
bs_save_unicode_string(wf2, type);
1300-
uint64_t propcount_ofs = wf2->get_position();
1301-
wf2->store_32(0);
1302-
1303-
int prop_count = 0;
1304-
1305-
while (true) {
1306-
String assign;
1307-
Variant value;
1308-
1309-
error = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp_new);
1310-
1311-
if (error) {
1312-
if (main_res && error == ERR_FILE_EOF) {
1313-
next_tag.name = ""; //exit
1314-
break;
1315-
}
1316-
1317-
_printerr();
1318-
return error;
1319-
}
1320-
1321-
if (!assign.is_empty()) {
1322-
HashMap<StringName, int> empty_string_map; //unused
1323-
bs_save_unicode_string(wf2, assign, true);
1324-
ResourceFormatSaverBinaryInstance::write_variant(wf2, value, dummy_read.resource_index_map, dummy_read.external_resources, empty_string_map);
1325-
prop_count++;
1326-
1327-
} else if (!next_tag.name.is_empty()) {
1328-
error = OK;
1329-
break;
1330-
} else {
1331-
error = ERR_FILE_CORRUPT;
1332-
error_text = "Premature end of file while parsing [sub_resource]";
1333-
_printerr();
1334-
return error;
1335-
}
1336-
}
1337-
1338-
wf2->seek(propcount_ofs);
1339-
wf2->store_32(prop_count);
1340-
wf2->seek_end();
1341-
}
1342-
1343-
if (next_tag.name == "node") {
1344-
// This is a node, must save one more!
1345-
1346-
if (!is_scene) {
1347-
error_text += "found the 'node' tag on a resource file!";
1348-
_printerr();
1349-
error = ERR_FILE_CORRUPT;
1350-
return error;
1351-
}
1352-
1353-
Ref<PackedScene> packed_scene = _parse_node_tag(rp_new);
1354-
1355-
if (!packed_scene.is_valid()) {
1356-
return error;
1357-
}
1358-
1359-
error = OK;
1360-
//get it here
1361-
List<PropertyInfo> props;
1362-
packed_scene->get_property_list(&props);
1363-
1364-
String id = "PackedScene_" + ResourceUID::get_singleton()->id_to_text(res_uid).replace("uid://", "").replace("<invalid>", "0");
1365-
bs_save_unicode_string(wf, "local://" + id);
1366-
local_pointers_pos.push_back(wf->get_position());
1367-
wf->store_64(0); //temp local offset
1368-
1369-
local_offsets.push_back(wf2->get_position());
1370-
bs_save_unicode_string(wf2, "PackedScene");
1371-
uint64_t propcount_ofs = wf2->get_position();
1372-
wf2->store_32(0);
1373-
1374-
int prop_count = 0;
1375-
1376-
for (const PropertyInfo &E : props) {
1377-
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
1378-
continue;
1379-
}
1380-
1381-
String name = E.name;
1382-
Variant value = packed_scene->get(name);
1383-
1384-
HashMap<StringName, int> empty_string_map; //unused
1385-
bs_save_unicode_string(wf2, name, true);
1386-
ResourceFormatSaverBinaryInstance::write_variant(wf2, value, dummy_read.resource_index_map, dummy_read.external_resources, empty_string_map);
1387-
prop_count++;
1388-
}
1389-
1390-
wf2->seek(propcount_ofs);
1391-
wf2->store_32(prop_count);
1392-
wf2->seek_end();
1393-
}
1394-
}
1395-
1396-
uint64_t offset_from = wf->get_position();
1397-
wf->seek(sub_res_count_pos); //plus one because the saved one
1398-
wf->store_32(local_offsets.size());
1399-
1400-
for (int i = 0; i < local_offsets.size(); i++) {
1401-
wf->seek(local_pointers_pos[i]);
1402-
wf->store_64(local_offsets[i] + offset_from);
1403-
}
1404-
1405-
wf->seek_end();
1406-
1407-
Vector<uint8_t> data = FileAccess::get_file_as_bytes(temp_file);
1408-
wf->store_buffer(data.ptr(), data.size());
1409-
{
1410-
Ref<DirAccess> dar = DirAccess::open(temp_file.get_base_dir());
1411-
ERR_FAIL_COND_V(dar.is_null(), FAILED);
1412-
1413-
dar->remove(temp_file);
1414-
}
1415-
1416-
wf->store_buffer((const uint8_t *)"RSRC", 4); //magic at end
1417-
1418-
return OK;
1419-
}
1420-
14211122
Error ResourceLoaderText::get_classes_used(HashSet<StringName> *r_classes) {
14221123
if (error) {
14231124
return error;
@@ -1834,29 +1535,6 @@ Error ResourceFormatLoaderText::rename_dependencies(const String &p_path, const
18341535

18351536
ResourceFormatLoaderText *ResourceFormatLoaderText::singleton = nullptr;
18361537

1837-
Error ResourceFormatLoaderText::convert_file_to_binary(const String &p_src_path, const String &p_dst_path) {
1838-
Error err;
1839-
Ref<FileAccess> f = FileAccess::open(p_src_path, FileAccess::READ, &err);
1840-
1841-
ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_OPEN, "Cannot open file '" + p_src_path + "'.");
1842-
1843-
ResourceLoaderText loader;
1844-
const String &path = p_src_path;
1845-
loader.local_path = ProjectSettings::get_singleton()->localize_path(path);
1846-
loader.res_path = loader.local_path;
1847-
loader.open(f);
1848-
return loader.save_as_binary(p_dst_path);
1849-
}
1850-
1851-
/*****************************************************************************************************/
1852-
/*****************************************************************************************************/
1853-
/*****************************************************************************************************/
1854-
/*****************************************************************************************************/
1855-
/*****************************************************************************************************/
1856-
/*****************************************************************************************************/
1857-
/*****************************************************************************************************/
1858-
/*****************************************************************************************************/
1859-
/*****************************************************************************************************/
18601538
/*****************************************************************************************************/
18611539

18621540
String ResourceFormatSaverTextInstance::_write_resources(void *ud, const Ref<Resource> &p_resource) {

scene/resources/resource_format_text.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ class ResourceLoaderText {
8787
Error _parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
8888
Error _parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
8989

90-
// for converter
91-
class DummyResource : public Resource {
92-
public:
93-
};
94-
9590
struct DummyReadData {
9691
bool no_placeholders = false;
9792
HashMap<Ref<Resource>, int> external_resources;
@@ -133,7 +128,6 @@ class ResourceLoaderText {
133128
Error rename_dependencies(Ref<FileAccess> p_f, const String &p_path, const HashMap<String, String> &p_map);
134129
Error get_classes_used(HashSet<StringName> *r_classes);
135130

136-
Error save_as_binary(const String &p_path);
137131
ResourceLoaderText();
138132
};
139133

@@ -152,8 +146,6 @@ class ResourceFormatLoaderText : public ResourceFormatLoader {
152146
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;
153147
virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) override;
154148

155-
static Error convert_file_to_binary(const String &p_src_path, const String &p_dst_path);
156-
157149
ResourceFormatLoaderText() { singleton = this; }
158150
};
159151

0 commit comments

Comments
 (0)