summaryrefslogtreecommitdiff
path: root/core/io
diff options
context:
space:
mode:
Diffstat (limited to 'core/io')
-rw-r--r--core/io/config_file.cpp8
-rw-r--r--core/io/dir_access.cpp17
-rw-r--r--core/io/http_client.cpp7
-rw-r--r--core/io/image_loader.cpp4
-rw-r--r--core/io/ip.cpp12
-rw-r--r--core/io/json.cpp11
-rw-r--r--core/io/marshalls.cpp32
-rw-r--r--core/io/marshalls.h2
-rw-r--r--core/io/multiplayer_api.cpp64
-rw-r--r--core/io/multiplayer_api.h4
-rw-r--r--core/io/multiplayer_peer.cpp24
-rw-r--r--core/io/multiplayer_peer.h1
-rw-r--r--core/io/packed_data_container.cpp14
-rw-r--r--core/io/resource.cpp94
-rw-r--r--core/io/resource.h14
-rw-r--r--core/io/resource_format_binary.cpp244
-rw-r--r--core/io/resource_format_binary.h15
-rw-r--r--core/io/resource_importer.cpp42
-rw-r--r--core/io/resource_importer.h15
-rw-r--r--core/io/resource_loader.cpp138
-rw-r--r--core/io/resource_loader.h10
-rw-r--r--core/io/resource_saver.cpp19
-rw-r--r--core/io/resource_saver.h5
-rw-r--r--core/io/resource_uid.cpp262
-rw-r--r--core/io/resource_uid.h89
25 files changed, 812 insertions, 335 deletions
diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp
index 10f68f3cef..aeaf25f321 100644
--- a/core/io/config_file.cpp
+++ b/core/io/config_file.cpp
@@ -40,8 +40,8 @@ PackedStringArray ConfigFile::_get_sections() const {
PackedStringArray arr;
arr.resize(s.size());
int idx = 0;
- for (const List<String>::Element *E = s.front(); E; E = E->next()) {
- arr.set(idx++, E->get());
+ for (const String &E : s) {
+ arr.set(idx++, E);
}
return arr;
@@ -53,8 +53,8 @@ PackedStringArray ConfigFile::_get_section_keys(const String &p_section) const {
PackedStringArray arr;
arr.resize(s.size());
int idx = 0;
- for (const List<String>::Element *E = s.front(); E; E = E->next()) {
- arr.set(idx++, E->get());
+ for (const String &E : s) {
+ arr.set(idx++, E);
}
return arr;
diff --git a/core/io/dir_access.cpp b/core/io/dir_access.cpp
index dfba00067f..8234adea06 100644
--- a/core/io/dir_access.cpp
+++ b/core/io/dir_access.cpp
@@ -93,8 +93,8 @@ static Error _erase_recursive(DirAccess *da) {
da->list_dir_end();
- for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
- Error err = da->change_dir(E->get());
+ for (const String &E : dirs) {
+ Error err = da->change_dir(E);
if (err == OK) {
err = _erase_recursive(da);
if (err) {
@@ -105,7 +105,7 @@ static Error _erase_recursive(DirAccess *da) {
if (err) {
return err;
}
- err = da->remove(da->get_current_dir().plus_file(E->get()));
+ err = da->remove(da->get_current_dir().plus_file(E));
if (err) {
return err;
}
@@ -114,8 +114,8 @@ static Error _erase_recursive(DirAccess *da) {
}
}
- for (List<String>::Element *E = files.front(); E; E = E->next()) {
- Error err = da->remove(da->get_current_dir().plus_file(E->get()));
+ for (const String &E : files) {
+ Error err = da->remove(da->get_current_dir().plus_file(E));
if (err) {
return err;
}
@@ -362,16 +362,15 @@ Error DirAccess::_copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flag
list_dir_end();
- for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
- String rel_path = E->get();
+ for (const String &rel_path : dirs) {
String target_dir = p_to + rel_path;
if (!p_target_da->dir_exists(target_dir)) {
Error err = p_target_da->make_dir(target_dir);
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create directory '" + target_dir + "'.");
}
- Error err = change_dir(E->get());
- ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot change current directory to '" + E->get() + "'.");
+ Error err = change_dir(rel_path);
+ ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot change current directory to '" + rel_path + "'.");
err = _copy_dir(p_target_da, p_to + rel_path + "/", p_chmod_flags, p_copy_links);
if (err) {
diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp
index 8000dd4290..5c1352c1b6 100644
--- a/core/io/http_client.cpp
+++ b/core/io/http_client.cpp
@@ -93,8 +93,7 @@ Dictionary HTTPClient::_get_response_headers_as_dictionary() {
List<String> rh;
get_response_headers(&rh);
Dictionary ret;
- for (const List<String>::Element *E = rh.front(); E; E = E->next()) {
- const String &s = E->get();
+ for (const String &s : rh) {
int sp = s.find(":");
if (sp == -1) {
continue;
@@ -113,8 +112,8 @@ PackedStringArray HTTPClient::_get_response_headers() {
PackedStringArray ret;
ret.resize(rh.size());
int idx = 0;
- for (const List<String>::Element *E = rh.front(); E; E = E->next()) {
- ret.set(idx++, E->get());
+ for (const String &E : rh) {
+ ret.set(idx++, E);
}
return ret;
diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp
index b45e9d26b1..b9fc416f65 100644
--- a/core/io/image_loader.cpp
+++ b/core/io/image_loader.cpp
@@ -35,8 +35,8 @@
bool ImageFormatLoader::recognize(const String &p_extension) const {
List<String> extensions;
get_recognized_extensions(&extensions);
- for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
- if (E->get().nocasecmp_to(p_extension) == 0) {
+ for (const String &E : extensions) {
+ if (E.nocasecmp_to(p_extension) == 0) {
return true;
}
}
diff --git a/core/io/ip.cpp b/core/io/ip.cpp
index 001b1c4757..cd1b6d1994 100644
--- a/core/io/ip.cpp
+++ b/core/io/ip.cpp
@@ -252,8 +252,8 @@ Array IP::_get_local_addresses() const {
Array addresses;
List<IPAddress> ip_addresses;
get_local_addresses(&ip_addresses);
- for (List<IPAddress>::Element *E = ip_addresses.front(); E; E = E->next()) {
- addresses.push_back(E->get());
+ for (const IPAddress &E : ip_addresses) {
+ addresses.push_back(E);
}
return addresses;
@@ -271,8 +271,8 @@ Array IP::_get_local_interfaces() const {
rc["index"] = c.index;
Array ips;
- for (const List<IPAddress>::Element *F = c.ip_addresses.front(); F; F = F->next()) {
- ips.push_front(F->get());
+ for (const IPAddress &F : c.ip_addresses) {
+ ips.push_front(F);
}
rc["addresses"] = ips;
@@ -286,8 +286,8 @@ void IP::get_local_addresses(List<IPAddress> *r_addresses) const {
Map<String, Interface_Info> interfaces;
get_local_interfaces(&interfaces);
for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
- for (const List<IPAddress>::Element *F = E->get().ip_addresses.front(); F; F = F->next()) {
- r_addresses->push_front(F->get());
+ for (const IPAddress &F : E->get().ip_addresses) {
+ r_addresses->push_front(F);
}
}
}
diff --git a/core/io/json.cpp b/core/io/json.cpp
index b3a2498212..5823afbdcd 100644
--- a/core/io/json.cpp
+++ b/core/io/json.cpp
@@ -121,14 +121,17 @@ String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_
keys.sort();
}
- for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
- if (E != keys.front()) {
+ bool first_key = true;
+ for (const Variant &E : keys) {
+ if (first_key) {
+ first_key = false;
+ } else {
s += ",";
s += end_statement;
}
- s += _make_indent(p_indent, p_cur_indent + 1) + _stringify(String(E->get()), p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
+ s += _make_indent(p_indent, p_cur_indent + 1) + _stringify(String(E), p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
s += colon;
- s += _stringify(d[E->get()], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
+ s += _stringify(d[E], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
}
s += end_statement + _make_indent(p_indent, p_cur_indent) + "}";
diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp
index f342db2dad..4f85eced93 100644
--- a/core/io/marshalls.cpp
+++ b/core/io/marshalls.cpp
@@ -1031,7 +1031,8 @@ static void _encode_string(const String &p_string, uint8_t *&buf, int &r_len) {
}
}
-Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects) {
+Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects, int p_depth) {
+ ERR_FAIL_COND_V_MSG(p_depth > Variant::MAX_RECURSION_DEPTH, ERR_OUT_OF_MEMORY, "Potential inifite recursion detected. Bailing.");
uint8_t *buf = r_buffer;
r_len = 0;
@@ -1358,8 +1359,8 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
obj->get_property_list(&props);
int pc = 0;
- for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
+ for (const PropertyInfo &E : props) {
+ if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
continue;
}
pc++;
@@ -1372,18 +1373,16 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
r_len += 4;
- for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
+ for (const PropertyInfo &E : props) {
+ if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
continue;
}
- _encode_string(E->get().name, buf, r_len);
+ _encode_string(E.name, buf, r_len);
int len;
- Error err = encode_variant(obj->get(E->get().name), buf, len, p_full_objects);
- if (err) {
- return err;
- }
+ Error err = encode_variant(obj->get(E.name), buf, len, p_full_objects, p_depth + 1);
+ ERR_FAIL_COND_V(err, err);
ERR_FAIL_COND_V(len % 4, ERR_BUG);
r_len += len;
if (buf) {
@@ -1418,7 +1417,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
List<Variant> keys;
d.get_key_list(&keys);
- for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
+ for (const Variant &E : keys) {
/*
CharString utf8 = E->->utf8();
@@ -1433,15 +1432,17 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
r_len++; //pad
*/
int len;
- encode_variant(E->get(), buf, len, p_full_objects);
+ Error err = encode_variant(E, buf, len, p_full_objects, p_depth + 1);
+ ERR_FAIL_COND_V(err, err);
ERR_FAIL_COND_V(len % 4, ERR_BUG);
r_len += len;
if (buf) {
buf += len;
}
- Variant *v = d.getptr(E->get());
+ Variant *v = d.getptr(E);
ERR_FAIL_COND_V(!v, ERR_BUG);
- encode_variant(*v, buf, len, p_full_objects);
+ err = encode_variant(*v, buf, len, p_full_objects, p_depth + 1);
+ ERR_FAIL_COND_V(err, err);
ERR_FAIL_COND_V(len % 4, ERR_BUG);
r_len += len;
if (buf) {
@@ -1462,7 +1463,8 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
for (int i = 0; i < v.size(); i++) {
int len;
- encode_variant(v.get(i), buf, len, p_full_objects);
+ Error err = encode_variant(v.get(i), buf, len, p_full_objects, p_depth + 1);
+ ERR_FAIL_COND_V(err, err);
ERR_FAIL_COND_V(len % 4, ERR_BUG);
r_len += len;
if (buf) {
diff --git a/core/io/marshalls.h b/core/io/marshalls.h
index 3ebed914a3..05804d5a46 100644
--- a/core/io/marshalls.h
+++ b/core/io/marshalls.h
@@ -213,6 +213,6 @@ public:
};
Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false);
-Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false);
+Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0);
#endif // MARSHALLS_H
diff --git a/core/io/multiplayer_api.cpp b/core/io/multiplayer_api.cpp
index 0745757d48..d4f09b2135 100644
--- a/core/io/multiplayer_api.cpp
+++ b/core/io/multiplayer_api.cpp
@@ -63,7 +63,7 @@ const MultiplayerAPI::RPCConfig _get_rpc_config(const Node *p_node, const String
const Vector<MultiplayerAPI::RPCConfig> node_config = p_node->get_node_rpc_methods();
for (int i = 0; i < node_config.size(); i++) {
if (node_config[i].name == p_method) {
- r_id = ((uint16_t)i) & (1 << 15);
+ r_id = ((uint16_t)i) | (1 << 15);
return node_config[i];
}
}
@@ -89,57 +89,22 @@ const MultiplayerAPI::RPCConfig _get_rpc_config_by_id(Node *p_node, uint16_t p_i
config = p_node->get_script_instance()->get_rpc_methods();
}
if (id < config.size()) {
- return config[p_id];
+ return config[id];
}
return MultiplayerAPI::RPCConfig();
}
-_FORCE_INLINE_ bool _should_call_local(MultiplayerAPI::RPCMode mode, bool is_master, bool &r_skip_rpc) {
- switch (mode) {
- case MultiplayerAPI::RPC_MODE_DISABLED: {
- // Do nothing.
- } break;
- case MultiplayerAPI::RPC_MODE_REMOTE: {
- // Do nothing. Remote cannot produce a local call.
- } break;
- case MultiplayerAPI::RPC_MODE_MASTERSYNC: {
- if (is_master) {
- r_skip_rpc = true; // I am the master, so skip remote call.
- }
- [[fallthrough]];
- }
- case MultiplayerAPI::RPC_MODE_REMOTESYNC:
- case MultiplayerAPI::RPC_MODE_PUPPETSYNC: {
- // Call it, sync always results in a local call.
- return true;
- } break;
- case MultiplayerAPI::RPC_MODE_MASTER: {
- if (is_master) {
- r_skip_rpc = true; // I am the master, so skip remote call.
- }
- return is_master;
- } break;
- case MultiplayerAPI::RPC_MODE_PUPPET: {
- return !is_master;
- } break;
- }
- return false;
-}
-
_FORCE_INLINE_ bool _can_call_mode(Node *p_node, MultiplayerAPI::RPCMode mode, int p_remote_id) {
switch (mode) {
case MultiplayerAPI::RPC_MODE_DISABLED: {
return false;
} break;
- case MultiplayerAPI::RPC_MODE_REMOTE:
- case MultiplayerAPI::RPC_MODE_REMOTESYNC: {
+ case MultiplayerAPI::RPC_MODE_REMOTE: {
return true;
} break;
- case MultiplayerAPI::RPC_MODE_MASTERSYNC:
case MultiplayerAPI::RPC_MODE_MASTER: {
return p_node->is_network_master();
} break;
- case MultiplayerAPI::RPC_MODE_PUPPETSYNC:
case MultiplayerAPI::RPC_MODE_PUPPET: {
return !p_node->is_network_master() && p_remote_id == p_node->get_network_master();
} break;
@@ -590,12 +555,12 @@ bool MultiplayerAPI::_send_confirm_path(Node *p_node, NodePath p_path, PathSentC
ofs += encode_cstring(path.get_data(), &packet.write[ofs]);
- for (List<int>::Element *E = peers_to_add.front(); E; E = E->next()) {
- network_peer->set_target_peer(E->get()); // To all of you.
+ for (int &E : peers_to_add) {
+ network_peer->set_target_peer(E); // To all of you.
network_peer->set_transfer_mode(MultiplayerPeer::TRANSFER_MODE_RELIABLE);
network_peer->put_packet(packet.ptr(), packet.size());
- psc->confirmed_peers.insert(E->get(), false); // Insert into confirmed, but as false since it was not confirmed.
+ psc->confirmed_peers.insert(E, false); // Insert into confirmed, but as false since it was not confirmed.
}
}
@@ -952,8 +917,8 @@ void MultiplayerAPI::_del_peer(int p_id) {
// Some refactoring is needed to make this faster and do paths GC.
List<NodePath> keys;
path_send_cache.get_key_list(&keys);
- for (List<NodePath>::Element *E = keys.front(); E; E = E->next()) {
- PathSentCache *psc = path_send_cache.getptr(E->get());
+ for (const NodePath &E : keys) {
+ PathSentCache *psc = path_send_cache.getptr(E);
psc->confirmed_peers.erase(p_id);
}
emit_signal(SNAME("network_peer_disconnected"), p_id);
@@ -977,23 +942,21 @@ void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const
ERR_FAIL_COND_MSG(network_peer->get_connection_status() != MultiplayerPeer::CONNECTION_CONNECTED, "Trying to call an RPC via a network peer which is not connected.");
int node_id = network_peer->get_unique_id();
- bool skip_rpc = node_id == p_peer_id;
bool call_local_native = false;
bool call_local_script = false;
- bool is_master = p_node->is_network_master();
uint16_t rpc_id = UINT16_MAX;
const RPCConfig config = _get_rpc_config(p_node, p_method, rpc_id);
ERR_FAIL_COND_MSG(config.name == StringName(),
vformat("Unable to get the RPC configuration for the function \"%s\" at path: \"%s\". This happens when the method is not marked for RPCs.", p_method, p_node->get_path()));
if (p_peer_id == 0 || p_peer_id == node_id || (p_peer_id < 0 && p_peer_id != -node_id)) {
if (rpc_id & (1 << 15)) {
- call_local_native = _should_call_local(config.rpc_mode, is_master, skip_rpc);
+ call_local_native = config.sync;
} else {
- call_local_script = _should_call_local(config.rpc_mode, is_master, skip_rpc);
+ call_local_script = config.sync;
}
}
- if (!skip_rpc) {
+ if (p_peer_id != node_id) {
#ifdef DEBUG_ENABLED
_profile_node_data("out_rpc", p_node->get_instance_id());
#endif
@@ -1030,7 +993,7 @@ void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const
}
}
- ERR_FAIL_COND_MSG(skip_rpc && !(call_local_native || call_local_script), "RPC '" + p_method + "' on yourself is not allowed by selected mode.");
+ ERR_FAIL_COND_MSG(p_peer_id == node_id && !config.sync, "RPC '" + p_method + "' on yourself is not allowed by selected mode.");
}
Error MultiplayerAPI::send_bytes(Vector<uint8_t> p_data, int p_to, MultiplayerPeer::TransferMode p_mode) {
@@ -1136,9 +1099,6 @@ void MultiplayerAPI::_bind_methods() {
BIND_ENUM_CONSTANT(RPC_MODE_REMOTE);
BIND_ENUM_CONSTANT(RPC_MODE_MASTER);
BIND_ENUM_CONSTANT(RPC_MODE_PUPPET);
- BIND_ENUM_CONSTANT(RPC_MODE_REMOTESYNC);
- BIND_ENUM_CONSTANT(RPC_MODE_MASTERSYNC);
- BIND_ENUM_CONSTANT(RPC_MODE_PUPPETSYNC);
}
MultiplayerAPI::MultiplayerAPI() {
diff --git a/core/io/multiplayer_api.h b/core/io/multiplayer_api.h
index e9f96383c9..cc994a9852 100644
--- a/core/io/multiplayer_api.h
+++ b/core/io/multiplayer_api.h
@@ -43,14 +43,12 @@ public:
RPC_MODE_REMOTE, // Using rpc() on it will call method in all remote peers
RPC_MODE_MASTER, // Using rpc() on it will call method on wherever the master is, be it local or remote
RPC_MODE_PUPPET, // Using rpc() on it will call method for all puppets
- RPC_MODE_REMOTESYNC, // Using rpc() on it will call method in all remote peers and locally
- RPC_MODE_MASTERSYNC, // Using rpc() on it will call method in the master peer and locally
- RPC_MODE_PUPPETSYNC, // Using rpc() on it will call method in all puppets peers and locally
};
struct RPCConfig {
StringName name;
RPCMode rpc_mode = RPC_MODE_DISABLED;
+ bool sync = false;
MultiplayerPeer::TransferMode transfer_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
int channel = 0;
diff --git a/core/io/multiplayer_peer.cpp b/core/io/multiplayer_peer.cpp
index 8126b4cea3..8b3b1eef8e 100644
--- a/core/io/multiplayer_peer.cpp
+++ b/core/io/multiplayer_peer.cpp
@@ -30,6 +30,29 @@
#include "multiplayer_peer.h"
+#include "core/os/os.h"
+
+uint32_t MultiplayerPeer::generate_unique_id() const {
+ uint32_t hash = 0;
+
+ while (hash == 0 || hash == 1) {
+ hash = hash_djb2_one_32(
+ (uint32_t)OS::get_singleton()->get_ticks_usec());
+ hash = hash_djb2_one_32(
+ (uint32_t)OS::get_singleton()->get_unix_time(), hash);
+ hash = hash_djb2_one_32(
+ (uint32_t)OS::get_singleton()->get_user_data_dir().hash64(), hash);
+ hash = hash_djb2_one_32(
+ (uint32_t)((uint64_t)this), hash); // Rely on ASLR heap
+ hash = hash_djb2_one_32(
+ (uint32_t)((uint64_t)&hash), hash); // Rely on ASLR stack
+
+ hash = hash & 0x7FFFFFFF; // Make it compatible with unsigned, since negative ID is used for exclusion
+ }
+
+ return hash;
+}
+
void MultiplayerPeer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_transfer_mode", "mode"), &MultiplayerPeer::set_transfer_mode);
ClassDB::bind_method(D_METHOD("get_transfer_mode"), &MultiplayerPeer::get_transfer_mode);
@@ -41,6 +64,7 @@ void MultiplayerPeer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_connection_status"), &MultiplayerPeer::get_connection_status);
ClassDB::bind_method(D_METHOD("get_unique_id"), &MultiplayerPeer::get_unique_id);
+ ClassDB::bind_method(D_METHOD("generate_unique_id"), &MultiplayerPeer::generate_unique_id);
ClassDB::bind_method(D_METHOD("set_refuse_new_connections", "enable"), &MultiplayerPeer::set_refuse_new_connections);
ClassDB::bind_method(D_METHOD("is_refusing_new_connections"), &MultiplayerPeer::is_refusing_new_connections);
diff --git a/core/io/multiplayer_peer.h b/core/io/multiplayer_peer.h
index 432f47280f..91a3ad7954 100644
--- a/core/io/multiplayer_peer.h
+++ b/core/io/multiplayer_peer.h
@@ -72,6 +72,7 @@ public:
virtual bool is_refusing_new_connections() const = 0;
virtual ConnectionStatus get_connection_status() const = 0;
+ uint32_t generate_unique_id() const;
MultiplayerPeer() {}
};
diff --git a/core/io/packed_data_container.cpp b/core/io/packed_data_container.cpp
index cf6a0b6027..4a76f0191d 100644
--- a/core/io/packed_data_container.cpp
+++ b/core/io/packed_data_container.cpp
@@ -268,21 +268,21 @@ uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpd
d.get_key_list(&keys);
List<DictKey> sortk;
- for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
+ for (const Variant &key : keys) {
DictKey dk;
- dk.hash = E->get().hash();
- dk.key = E->get();
+ dk.hash = key.hash();
+ dk.key = key;
sortk.push_back(dk);
}
sortk.sort();
int idx = 0;
- for (List<DictKey>::Element *E = sortk.front(); E; E = E->next()) {
- encode_uint32(E->get().hash, &tmpdata.write[pos + 8 + idx * 12 + 0]);
- uint32_t ofs = _pack(E->get().key, tmpdata, string_cache);
+ for (const DictKey &E : sortk) {
+ encode_uint32(E.hash, &tmpdata.write[pos + 8 + idx * 12 + 0]);
+ uint32_t ofs = _pack(E.key, tmpdata, string_cache);
encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 4]);
- ofs = _pack(d[E->get().key], tmpdata, string_cache);
+ ofs = _pack(d[E.key], tmpdata, string_cache);
encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 8]);
idx++;
}
diff --git a/core/io/resource.cpp b/core/io/resource.cpp
index efa622d976..727611a573 100644
--- a/core/io/resource.cpp
+++ b/core/io/resource.cpp
@@ -33,6 +33,7 @@
#include "core/core_string_names.h"
#include "core/io/file_access.h"
#include "core/io/resource_loader.h"
+#include "core/math/math_funcs.h"
#include "core/object/script_language.h"
#include "core/os/os.h"
#include "scene/main/node.h" //only so casting works
@@ -94,12 +95,43 @@ String Resource::get_path() const {
return path_cache;
}
-void Resource::set_subindex(int p_sub_index) {
- subindex = p_sub_index;
+String Resource::generate_scene_unique_id() {
+ // Generate a unique enough hash, but still user-readable.
+ // If it's not unique it does not matter because the saver will try again.
+ OS::Date date = OS::get_singleton()->get_date();
+ OS::Time time = OS::get_singleton()->get_time();
+ uint32_t hash = hash_djb2_one_32(OS::get_singleton()->get_ticks_usec());
+ hash = hash_djb2_one_32(date.year, hash);
+ hash = hash_djb2_one_32(date.month, hash);
+ hash = hash_djb2_one_32(date.day, hash);
+ hash = hash_djb2_one_32(time.hour, hash);
+ hash = hash_djb2_one_32(time.minute, hash);
+ hash = hash_djb2_one_32(time.second, hash);
+ hash = hash_djb2_one_32(Math::rand(), hash);
+
+ static constexpr uint32_t characters = 5;
+ static constexpr uint32_t char_count = ('z' - 'a');
+ static constexpr uint32_t base = char_count + ('9' - '0');
+ String id;
+ for (uint32_t i = 0; i < characters; i++) {
+ uint32_t c = hash % base;
+ if (c < char_count) {
+ id += String::chr('a' + c);
+ } else {
+ id += String::chr('0' + (c - char_count));
+ }
+ hash /= base;
+ }
+
+ return id;
+}
+
+void Resource::set_scene_unique_id(const String &p_id) {
+ scene_unique_id = p_id;
}
-int Resource::get_subindex() const {
- return subindex;
+String Resource::get_scene_unique_id() const {
+ return scene_unique_id;
}
void Resource::set_name(const String &p_name) {
@@ -133,15 +165,15 @@ Error Resource::copy_from(const Ref<Resource> &p_resource) {
List<PropertyInfo> pi;
p_resource->get_property_list(&pi);
- for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
+ for (const PropertyInfo &E : pi) {
+ if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
continue;
}
- if (E->get().name == "resource_path") {
+ if (E.name == "resource_path") {
continue; //do not change path
}
- set(E->get().name, p_resource->get(E->get().name));
+ set(E.name, p_resource->get(E.name));
}
return OK;
}
@@ -169,11 +201,11 @@ Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Res
r->local_scene = p_for_scene;
- for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
+ for (const PropertyInfo &E : plist) {
+ if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
continue;
}
- Variant p = get(E->get().name);
+ Variant p = get(E.name);
if (p.get_type() == Variant::OBJECT) {
RES sr = p;
if (sr.is_valid()) {
@@ -189,7 +221,7 @@ Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Res
}
}
- r->set(E->get().name, p);
+ r->set(E.name, p);
}
return r;
@@ -201,11 +233,11 @@ void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, R
local_scene = p_for_scene;
- for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
+ for (const PropertyInfo &E : plist) {
+ if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
continue;
}
- Variant p = get(E->get().name);
+ Variant p = get(E.name);
if (p.get_type() == Variant::OBJECT) {
RES sr = p;
if (sr.is_valid()) {
@@ -227,21 +259,21 @@ Ref<Resource> Resource::duplicate(bool p_subresources) const {
Ref<Resource> r = (Resource *)ClassDB::instantiate(get_class());
ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
- for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
+ for (const PropertyInfo &E : plist) {
+ if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
continue;
}
- Variant p = get(E->get().name);
+ Variant p = get(E.name);
if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
- r->set(E->get().name, p.duplicate(p_subresources));
- } else if (p.get_type() == Variant::OBJECT && (p_subresources || (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
+ r->set(E.name, p.duplicate(p_subresources));
+ } else if (p.get_type() == Variant::OBJECT && (p_subresources || (E.usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
RES sr = p;
if (sr.is_valid()) {
- r->set(E->get().name, sr->duplicate(p_subresources));
+ r->set(E.name, sr->duplicate(p_subresources));
}
} else {
- r->set(E->get().name, p);
+ r->set(E.name, p);
}
}
@@ -285,9 +317,9 @@ uint32_t Resource::hash_edited_version() const {
List<PropertyInfo> plist;
get_property_list(&plist);
- for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
- if (E->get().usage & PROPERTY_USAGE_STORAGE && E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
- RES res = get(E->get().name);
+ for (const PropertyInfo &E : plist) {
+ if (E.usage & PROPERTY_USAGE_STORAGE && E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
+ RES res = get(E.name);
if (res.is_valid()) {
hash = hash_djb2_one_32(res->hash_edited_version(), hash);
}
@@ -350,8 +382,8 @@ bool Resource::is_translation_remapped() const {
#ifdef TOOLS_ENABLED
//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
-void Resource::set_id_for_path(const String &p_path, int p_id) {
- if (p_id == -1) {
+void Resource::set_id_for_path(const String &p_path, const String &p_id) {
+ if (p_id == "") {
ResourceCache::path_cache_lock.write_lock();
ResourceCache::resource_path_cache[p_path].erase(get_path());
ResourceCache::path_cache_lock.write_unlock();
@@ -362,15 +394,15 @@ void Resource::set_id_for_path(const String &p_path, int p_id) {
}
}
-int Resource::get_id_for_path(const String &p_path) const {
+String Resource::get_id_for_path(const String &p_path) const {
ResourceCache::path_cache_lock.read_lock();
if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
- int result = ResourceCache::resource_path_cache[p_path][get_path()];
+ String result = ResourceCache::resource_path_cache[p_path][get_path()];
ResourceCache::path_cache_lock.read_unlock();
return result;
} else {
ResourceCache::path_cache_lock.read_unlock();
- return -1;
+ return "";
}
}
#endif
@@ -414,7 +446,7 @@ Resource::~Resource() {
HashMap<String, Resource *> ResourceCache::resources;
#ifdef TOOLS_ENABLED
-HashMap<String, HashMap<String, int>> ResourceCache::resource_path_cache;
+HashMap<String, HashMap<String, String>> ResourceCache::resource_path_cache;
#endif
RWLock ResourceCache::lock;
diff --git a/core/io/resource.h b/core/io/resource.h
index 028fed1c6e..e864b371ad 100644
--- a/core/io/resource.h
+++ b/core/io/resource.h
@@ -31,6 +31,7 @@
#ifndef RESOURCE_H
#define RESOURCE_H
+#include "core/io/resource_uid.h"
#include "core/object/class_db.h"
#include "core/object/ref_counted.h"
#include "core/templates/safe_refcount.h"
@@ -59,7 +60,7 @@ private:
String name;
String path_cache;
- int subindex = 0;
+ String scene_unique_id;
virtual bool _use_builtin_script() const { return true; }
@@ -105,8 +106,9 @@ public:
virtual void set_path(const String &p_path, bool p_take_over = false);
String get_path() const;
- void set_subindex(int p_sub_index);
- int get_subindex() const;
+ static String generate_scene_unique_id();
+ void set_scene_unique_id(const String &p_id);
+ String get_scene_unique_id() const;
virtual Ref<Resource> duplicate(bool p_subresources = false) const;
Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache);
@@ -140,8 +142,8 @@ public:
#ifdef TOOLS_ENABLED
//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
- void set_id_for_path(const String &p_path, int p_id);
- int get_id_for_path(const String &p_path) const;
+ void set_id_for_path(const String &p_path, const String &p_id);
+ String get_id_for_path(const String &p_path) const;
#endif
Resource();
@@ -156,7 +158,7 @@ class ResourceCache {
static RWLock lock;
static HashMap<String, Resource *> resources;
#ifdef TOOLS_ENABLED
- static HashMap<String, HashMap<String, int>> resource_path_cache; // each tscn has a set of resource paths and IDs
+ static HashMap<String, HashMap<String, String>> resource_path_cache; // Each tscn has a set of resource paths and IDs.
static RWLock path_cache_lock;
#endif // TOOLS_ENABLED
friend void unregister_core_types();
diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp
index 0e9815245f..a3ebc32cc5 100644
--- a/core/io/resource_format_binary.cpp
+++ b/core/io/resource_format_binary.cpp
@@ -84,9 +84,10 @@ enum {
OBJECT_EXTERNAL_RESOURCE = 1,
OBJECT_INTERNAL_RESOURCE = 2,
OBJECT_EXTERNAL_RESOURCE_INDEX = 3,
- //version 2: added 64 bits support for float and int
- //version 3: changed nodepath encoding
- FORMAT_VERSION = 3,
+ // Version 2: added 64 bits support for float and int.
+ // Version 3: changed nodepath encoding.
+ // Version 4: new string ID for ext/subresources, breaks forward compat.
+ FORMAT_VERSION = 4,
FORMAT_VERSION_CAN_RENAME_DEPS = 1,
FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3,
};
@@ -311,7 +312,14 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) {
} break;
case OBJECT_INTERNAL_RESOURCE: {
uint32_t index = f->get_32();
- String path = res_path + "::" + itos(index);
+ String path;
+
+ if (using_named_scene_ids) { // New format.
+ ERR_FAIL_INDEX_V((int)index, internal_resources.size(), ERR_PARSE_ERROR);
+ path = internal_resources[index].path;
+ } else {
+ path += res_path + "::" + itos(index);
+ }
//always use internal cache for loading internal resources
if (!internal_index_cache.has(path)) {
@@ -320,7 +328,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) {
} else {
r_v = internal_index_cache[path];
}
-
} break;
case OBJECT_EXTERNAL_RESOURCE: {
//old file format, still around for compatibility
@@ -378,7 +385,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) {
ERR_FAIL_V(ERR_FILE_CORRUPT);
} break;
}
-
} break;
case VARIANT_CALLABLE: {
r_v = Callable();
@@ -659,15 +665,17 @@ Error ResourceLoaderBinary::load() {
//maybe it is loaded already
String path;
- int subindex = 0;
+ String id;
if (!main) {
path = internal_resources[i].path;
if (path.begins_with("local://")) {
path = path.replace_first("local://", "");
- subindex = path.to_int();
+ id = path;
path = res_path + "::" + path;
+
+ internal_resources.write[i].path = path; // Update path.
}
if (cache_mode == ResourceFormatLoader::CACHE_MODE_REUSE) {
@@ -722,7 +730,7 @@ Error ResourceLoaderBinary::load() {
if (path != String() && cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
r->set_path(path, cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE); //if got here because the resource with same path has different type, replace it
}
- r->set_subindex(subindex);
+ r->set_scene_unique_id(id);
}
if (!main) {
@@ -808,13 +816,18 @@ String ResourceLoaderBinary::get_unicode_string() {
}
void ResourceLoaderBinary::get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types) {
- open(p_f);
+ open(p_f, false, true);
if (error) {
return;
}
for (int i = 0; i < external_resources.size(); i++) {
- String dep = external_resources[i].path;
+ String dep;
+ if (external_resources[i].uid != ResourceUID::INVALID_ID) {
+ dep = ResourceUID::get_singleton()->id_to_text(external_resources[i].uid);
+ } else {
+ dep = external_resources[i].path;
+ }
if (p_add_types && external_resources[i].type != String()) {
dep += "::" + external_resources[i].type;
@@ -824,7 +837,7 @@ void ResourceLoaderBinary::get_dependencies(FileAccess *p_f, List<String> *p_dep
}
}
-void ResourceLoaderBinary::open(FileAccess *p_f) {
+void ResourceLoaderBinary::open(FileAccess *p_f, bool p_no_resources, bool p_keep_uuid_paths) {
error = OK;
f = p_f;
@@ -879,10 +892,28 @@ void ResourceLoaderBinary::open(FileAccess *p_f) {
print_bl("type: " + type);
importmd_ofs = f->get_64();
- for (int i = 0; i < 14; i++) {
+ uint32_t flags = f->get_32();
+ if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS) {
+ using_named_scene_ids = true;
+ }
+ if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS) {
+ using_uids = true;
+ }
+
+ if (using_uids) {
+ uid = f->get_64();
+ } else {
+ uid = ResourceUID::INVALID_ID;
+ }
+
+ for (int i = 0; i < 5; i++) {
f->get_32(); //skip a few reserved fields
}
+ if (p_no_resources) {
+ return;
+ }
+
uint32_t string_table_size = f->get_32();
string_map.resize(string_table_size);
for (uint32_t i = 0; i < string_table_size; i++) {
@@ -896,8 +927,18 @@ void ResourceLoaderBinary::open(FileAccess *p_f) {
for (uint32_t i = 0; i < ext_resources_size; i++) {
ExtResource er;
er.type = get_unicode_string();
-
er.path = get_unicode_string();
+ if (using_uids) {
+ er.uid = f->get_64();
+ if (!p_keep_uuid_paths && er.uid != ResourceUID::INVALID_ID) {
+ if (ResourceUID::get_singleton()->has_id(er.uid)) {
+ // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
+ er.path = ResourceUID::get_singleton()->get_id_path(er.uid);
+ } else {
+ WARN_PRINT(String(res_path + ": In external resource #" + itos(i) + ", invalid UUID: " + ResourceUID::get_singleton()->id_to_text(er.uid) + " - using text path instead: " + er.path).utf8().get_data());
+ }
+ }
+ }
external_resources.push_back(er);
}
@@ -1013,8 +1054,8 @@ void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String
extensions.sort();
- for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
- String ext = E->get().to_lower();
+ for (const String &E : extensions) {
+ String ext = E.to_lower();
p_extensions->push_back(ext);
}
}
@@ -1024,8 +1065,8 @@ void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_exten
ClassDB::get_resource_base_extensions(&extensions);
extensions.sort();
- for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
- String ext = E->get().to_lower();
+ for (const String &E : extensions) {
+ String ext = E.to_lower();
p_extensions->push_back(ext);
}
}
@@ -1161,8 +1202,15 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons
uint64_t importmd_ofs = f->get_64();
fw->store_64(0); //metadata offset
- for (int i = 0; i < 14; i++) {
- fw->store_32(0);
+ uint32_t flags = f->get_32();
+ bool using_uids = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS);
+ uint64_t uid_data = f->get_64();
+
+ fw->store_32(flags);
+ fw->store_64(uid_data);
+
+ for (int i = 0; i < 5; i++) {
+ f->store_32(0); // reserved
f->get_32();
}
@@ -1183,6 +1231,16 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons
String type = get_ustring(f);
String path = get_ustring(f);
+ if (using_uids) {
+ ResourceUID::ID uid = f->get_64();
+ if (uid != ResourceUID::INVALID_ID) {
+ if (ResourceUID::get_singleton()->has_id(uid)) {
+ // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
+ path = ResourceUID::get_singleton()->get_id_path(uid);
+ }
+ }
+ }
+
bool relative = false;
if (!path.begins_with("res://")) {
path = local_path.plus_file(path).simplify_path();
@@ -1194,6 +1252,8 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons
path = np;
}
+ String full_path = path;
+
if (relative) {
//restore relative
path = local_path.path_to_file(path);
@@ -1201,6 +1261,11 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons
save_ustring(fw, type);
save_ustring(fw, path);
+
+ if (using_uids) {
+ ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path);
+ f->store_64(uid);
+ }
}
int64_t size_diff = (int64_t)fw->get_position() - (int64_t)f->get_position();
@@ -1256,6 +1321,28 @@ String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const
return ClassDB::get_compatibility_remapped_class(r);
}
+ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_path) const {
+ String ext = p_path.get_extension().to_lower();
+ if (!ClassDB::is_resource_extension(ext)) {
+ return ResourceUID::INVALID_ID;
+ }
+
+ FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
+ if (!f) {
+ return ResourceUID::INVALID_ID; //could not read
+ }
+
+ ResourceLoaderBinary loader;
+ loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path);
+ loader.res_path = loader.local_path;
+ //loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
+ loader.open(f, true);
+ if (loader.error != OK) {
+ return ResourceUID::INVALID_ID; //could not read
+ }
+ return loader.uid;
+}
+
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
@@ -1269,11 +1356,7 @@ void ResourceFormatSaverBinaryInstance::_pad_buffer(FileAccess *f, int p_bytes)
}
}
-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) {
+void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Variant &p_property, Map<RES, int> &resource_map, Map<RES, int> &external_resources, Map<StringName, int> &string_map, const PropertyInfo &p_hint) {
switch (p_property.get_type()) {
case Variant::NIL: {
f->store_32(VARIANT_NIL);
@@ -1492,13 +1575,13 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX);
f->store_32(external_resources[res]);
} else {
- if (!resource_set.has(res)) {
+ if (!resource_map.has(res)) {
f->store_32(OBJECT_EMPTY);
ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference.");
}
f->store_32(OBJECT_INTERNAL_RESOURCE);
- f->store_32(res->get_subindex());
+ f->store_32(resource_map[res]);
//internal resource
}
@@ -1520,14 +1603,14 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
List<Variant> keys;
d.get_key_list(&keys);
- for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
+ for (const Variant &E : keys) {
/*
- if (!_check_type(dict[E->get()]))
+ if (!_check_type(dict[E]))
continue;
*/
- write_variant(f, E->get(), resource_set, external_resources, string_map);
- write_variant(f, d[E->get()], resource_set, external_resources, string_map);
+ write_variant(f, E, resource_map, external_resources, string_map);
+ write_variant(f, d[E], resource_map, external_resources, string_map);
}
} break;
@@ -1536,7 +1619,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
Array a = p_property;
f->store_32(uint32_t(a.size()));
for (int i = 0; i < a.size(); i++) {
- write_variant(f, a[i], resource_set, external_resources, string_map);
+ write_variant(f, a[i], resource_map, external_resources, string_map);
}
} break;
@@ -1677,15 +1760,15 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
res->get_property_list(&property_list);
- for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
- if (E->get().usage & PROPERTY_USAGE_STORAGE) {
- Variant value = res->get(E->get().name);
- if (E->get().usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
+ for (const PropertyInfo &E : property_list) {
+ if (E.usage & PROPERTY_USAGE_STORAGE) {
+ Variant value = res->get(E.name);
+ if (E.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
RES sres = value;
if (sres.is_valid()) {
NonPersistentKey npk;
npk.base = res;
- npk.property = E->get().name;
+ npk.property = E.name;
non_persistent_map[npk] = sres;
resource_set.insert(sres);
saved_resources.push_back(sres);
@@ -1715,9 +1798,9 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
Dictionary d = p_variant;
List<Variant> keys;
d.get_key_list(&keys);
- for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
- _find_resources(E->get());
- Variant v = d[E->get()];
+ for (const Variant &E : keys) {
+ _find_resources(E);
+ Variant v = d[E];
_find_resources(v);
}
} break;
@@ -1816,46 +1899,49 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p
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(FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS);
+ ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true);
+ f->store_64(uid);
+ for (int i = 0; i < 5; i++) {
f->store_32(0); // reserved
}
List<ResourceData> resources;
{
- for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
+ for (const RES &E : saved_resources) {
ResourceData &rd = resources.push_back(ResourceData())->get();
- rd.type = E->get()->get_class();
+ rd.type = E->get_class();
List<PropertyInfo> property_list;
- E->get()->get_property_list(&property_list);
+ E->get_property_list(&property_list);
- for (List<PropertyInfo>::Element *F = property_list.front(); F; F = F->next()) {
- if (skip_editor && F->get().name.begins_with("__editor")) {
+ for (const PropertyInfo &F : property_list) {
+ if (skip_editor && F.name.begins_with("__editor")) {
continue;
}
- if ((F->get().usage & PROPERTY_USAGE_STORAGE)) {
+ if ((F.usage & PROPERTY_USAGE_STORAGE)) {
Property p;
- p.name_idx = get_string_index(F->get().name);
+ p.name_idx = get_string_index(F.name);
- if (F->get().usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
+ if (F.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
NonPersistentKey npk;
- npk.base = E->get();
- npk.property = F->get().name;
+ npk.base = E;
+ npk.property = F.name;
if (non_persistent_map.has(npk)) {
p.value = non_persistent_map[npk];
}
} else {
- p.value = E->get()->get(F->get().name);
+ p.value = E->get(F.name);
}
- Variant default_value = ClassDB::class_get_default_property_value(E->get()->get_class(), F->get().name);
+ Variant default_value = ClassDB::class_get_default_property_value(E->get_class(), F.name);
if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, p.value, default_value))) {
continue;
}
- p.pi = F->get();
+ p.pi = F;
rd.properties.push_back(p);
}
@@ -1882,41 +1968,47 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p
String path = save_order[i]->get_path();
path = relative_paths ? local_path.path_to_file(path) : path;
save_unicode_string(f, path);
+ ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false);
+ f->store_64(ruid);
}
// save internal resource table
f->store_32(saved_resources.size()); //amount of internal resources
Vector<uint64_t> ofs_pos;
- Set<int> used_indices;
+ Set<String> used_unique_ids;
- for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
- RES r = E->get();
+ for (RES &r : saved_resources) {
if (r->get_path() == "" || r->get_path().find("::") != -1) {
- if (r->get_subindex() != 0) {
- if (used_indices.has(r->get_subindex())) {
- r->set_subindex(0); //repeated
+ if (r->get_scene_unique_id() != "") {
+ if (used_unique_ids.has(r->get_scene_unique_id())) {
+ r->set_scene_unique_id("");
} else {
- used_indices.insert(r->get_subindex());
+ used_unique_ids.insert(r->get_scene_unique_id());
}
}
}
}
- for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) {
- RES r = E->get();
+ Map<RES, int> resource_map;
+ int res_index = 0;
+ for (RES &r : saved_resources) {
if (r->get_path() == "" || r->get_path().find("::") != -1) {
- if (r->get_subindex() == 0) {
- int new_subindex = 1;
- if (used_indices.size()) {
- new_subindex = used_indices.back()->get() + 1;
+ if (r->get_scene_unique_id() == "") {
+ String new_id;
+
+ while (true) {
+ new_id = r->get_class() + "_" + Resource::generate_scene_unique_id();
+ if (!used_unique_ids.has(new_id)) {
+ break;
+ }
}
- r->set_subindex(new_subindex);
- used_indices.insert(new_subindex);
+ r->set_scene_unique_id(new_id);
+ used_unique_ids.insert(new_id);
}
- save_unicode_string(f, "local://" + itos(r->get_subindex()));
+ save_unicode_string(f, "local://" + r->get_scene_unique_id());
if (takeover_paths) {
- r->set_path(p_path + "::" + itos(r->get_subindex()), true);
+ r->set_path(p_path + "::" + r->get_scene_unique_id(), true);
}
#ifdef TOOLS_ENABLED
r->set_edited(false);
@@ -1926,22 +2018,20 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p
}
ofs_pos.push_back(f->get_position());
f->store_64(0); //offset in 64 bits
+ resource_map[r] = res_index++;
}
Vector<uint64_t> ofs_table;
//now actually save the resources
- for (List<ResourceData>::Element *E = resources.front(); E; E = E->next()) {
- ResourceData &rd = E->get();
-
+ for (const ResourceData &rd : resources) {
ofs_table.push_back(f->get_position());
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();
+ for (const Property &p : rd.properties) {
f->store_32(p.name_idx);
- _write_variant(p.value, F->get().pi);
+ write_variant(f, p.value, resource_map, external_resources, string_map, p.pi);
}
}
diff --git a/core/io/resource_format_binary.h b/core/io/resource_format_binary.h
index abc7403935..ac964d2053 100644
--- a/core/io/resource_format_binary.h
+++ b/core/io/resource_format_binary.h
@@ -47,6 +47,8 @@ class ResourceLoaderBinary {
uint64_t importmd_ofs = 0;
+ ResourceUID::ID uid = ResourceUID::INVALID_ID;
+
Vector<char> str_buf;
List<RES> resource_cache;
@@ -57,9 +59,12 @@ class ResourceLoaderBinary {
struct ExtResource {
String path;
String type;
+ ResourceUID::ID uid = ResourceUID::INVALID_ID;
RES cache;
};
+ bool using_named_scene_ids = false;
+ bool using_uids = false;
bool use_sub_threads = false;
float *progress = nullptr;
Vector<ExtResource> external_resources;
@@ -93,7 +98,7 @@ public:
void set_translation_remapped(bool p_remapped);
void set_remaps(const Map<String, String> &p_remaps) { remaps = p_remaps; }
- void open(FileAccess *p_f);
+ void open(FileAccess *p_f, bool p_no_resources = false, bool p_keep_uuid_paths = false);
String recognize(FileAccess *p_f);
void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
@@ -108,6 +113,7 @@ public:
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
+ virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
};
@@ -150,14 +156,17 @@ class ResourceFormatSaverBinaryInstance {
};
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);
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:
+ enum {
+ FORMAT_FLAG_NAMED_SCENE_IDS = 1,
+ FORMAT_FLAG_UIDS = 2,
+ };
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());
+ static void write_variant(FileAccess *f, const Variant &p_property, Map<RES, int> &resource_map, Map<RES, int> &external_resources, Map<StringName, int> &string_map, const PropertyInfo &p_hint = PropertyInfo());
};
class ResourceFormatSaverBinary : public ResourceFormatSaver {
diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp
index b503655edd..1e166015b0 100644
--- a/core/io/resource_importer.cpp
+++ b/core/io/resource_importer.cpp
@@ -93,6 +93,8 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy
r_path_and_type.type = ClassDB::get_compatibility_remapped_class(value);
} else if (assign == "importer") {
r_path_and_type.importer = value;
+ } else if (assign == "uid") {
+ r_path_and_type.uid = ResourceUID::get_singleton()->text_to_id(value);
} else if (assign == "group_file") {
r_path_and_type.group_file = value;
} else if (assign == "metadata") {
@@ -146,10 +148,10 @@ void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extension
for (int i = 0; i < importers.size(); i++) {
List<String> local_exts;
importers[i]->get_recognized_extensions(&local_exts);
- for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
- if (!found.has(F->get())) {
- p_extensions->push_back(F->get());
- found.insert(F->get());
+ for (const String &F : local_exts) {
+ if (!found.has(F)) {
+ p_extensions->push_back(F);
+ found.insert(F);
}
}
}
@@ -175,10 +177,10 @@ void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_
List<String> local_exts;
importers[i]->get_recognized_extensions(&local_exts);
- for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
- if (!found.has(F->get())) {
- p_extensions->push_back(F->get());
- found.insert(F->get());
+ for (const String &F : local_exts) {
+ if (!found.has(F)) {
+ p_extensions->push_back(F);
+ found.insert(F);
}
}
}
@@ -336,6 +338,17 @@ String ResourceFormatImporter::get_resource_type(const String &p_path) const {
return pat.type;
}
+ResourceUID::ID ResourceFormatImporter::get_resource_uid(const String &p_path) const {
+ PathAndType pat;
+ Error err = _get_path_and_type(p_path, pat);
+
+ if (err != OK) {
+ return ResourceUID::INVALID_ID;
+ }
+
+ return pat.uid;
+}
+
Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
PathAndType pat;
Error err = _get_path_and_type(p_path, pat);
@@ -372,8 +385,8 @@ void ResourceFormatImporter::get_importers_for_extension(const String &p_extensi
for (int i = 0; i < importers.size(); i++) {
List<String> local_exts;
importers[i]->get_recognized_extensions(&local_exts);
- for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
- if (p_extension.to_lower() == F->get()) {
+ for (const String &F : local_exts) {
+ if (p_extension.to_lower() == F) {
r_importers->push_back(importers[i]);
}
}
@@ -393,8 +406,8 @@ Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const St
for (int i = 0; i < importers.size(); i++) {
List<String> local_exts;
importers[i]->get_recognized_extensions(&local_exts);
- for (List<String>::Element *F = local_exts.front(); F; F = F->next()) {
- if (p_extension.to_lower() == F->get() && importers[i]->get_priority() > priority) {
+ for (const String &F : local_exts) {
+ if (p_extension.to_lower() == F && importers[i]->get_priority() > priority) {
importer = importers[i];
priority = importers[i]->get_priority();
}
@@ -445,3 +458,8 @@ ResourceFormatImporter *ResourceFormatImporter::singleton = nullptr;
ResourceFormatImporter::ResourceFormatImporter() {
singleton = this;
}
+
+void ResourceImporter::_bind_methods() {
+ BIND_ENUM_CONSTANT(IMPORT_ORDER_DEFAULT);
+ BIND_ENUM_CONSTANT(IMPORT_ORDER_SCENE);
+}
diff --git a/core/io/resource_importer.h b/core/io/resource_importer.h
index 2ceeb176e5..a1cacbd306 100644
--- a/core/io/resource_importer.h
+++ b/core/io/resource_importer.h
@@ -42,6 +42,7 @@ class ResourceFormatImporter : public ResourceFormatLoader {
String importer;
String group_file;
Variant metadata;
+ uint64_t uid = ResourceUID::INVALID_ID;
};
Error _get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool *r_valid = nullptr) const;
@@ -63,6 +64,8 @@ public:
virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
+ virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
+
virtual Variant get_resource_metadata(const String &p_path) const;
virtual bool is_import_valid(const String &p_path) const;
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
@@ -96,6 +99,9 @@ public:
class ResourceImporter : public RefCounted {
GDCLASS(ResourceImporter, RefCounted);
+protected:
+ static void _bind_methods();
+
public:
virtual String get_importer_name() const = 0;
virtual String get_visible_name() const = 0;
@@ -103,7 +109,7 @@ public:
virtual String get_save_extension() const = 0;
virtual String get_resource_type() const = 0;
virtual float get_priority() const { return 1.0; }
- virtual int get_import_order() const { return 0; }
+ virtual int get_import_order() const { return IMPORT_ORDER_DEFAULT; }
virtual int get_format_version() const { return 0; }
struct ImportOption {
@@ -117,6 +123,11 @@ public:
ImportOption() {}
};
+ enum ImportOrder {
+ IMPORT_ORDER_DEFAULT = 0,
+ IMPORT_ORDER_SCENE = 100,
+ };
+
virtual bool has_advanced_options() const { return false; }
virtual void show_advanced_options(const String &p_path) {}
@@ -137,4 +148,6 @@ public:
virtual String get_import_settings_string() const { return String(); }
};
+VARIANT_ENUM_CAST(ResourceImporter::ImportOrder);
+
#endif // RESOURCE_IMPORTER_H
diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp
index c5dfe1f2b0..d02d827443 100644
--- a/core/io/resource_loader.cpp
+++ b/core/io/resource_loader.cpp
@@ -58,8 +58,8 @@ bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_
get_recognized_extensions_for_type(p_for_type, &extensions);
}
- for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
- if (E->get().nocasecmp_to(extension) == 0) {
+ for (const String &E : extensions) {
+ if (E.nocasecmp_to(extension) == 0) {
return true;
}
}
@@ -84,6 +84,14 @@ String ResourceFormatLoader::get_resource_type(const String &p_path) const {
return "";
}
+ResourceUID::ID ResourceFormatLoader::get_resource_uid(const String &p_path) const {
+ if (get_script_instance() && get_script_instance()->has_method("_get_resource_uid")) {
+ return get_script_instance()->call("_get_resource_uid", p_path);
+ }
+
+ return ResourceUID::INVALID_ID;
+}
+
void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
if (p_type == "" || handles_type(p_type)) {
get_recognized_extensions(p_extensions);
@@ -270,13 +278,18 @@ void ResourceLoader::_thread_load_function(void *p_userdata) {
thread_load_mutex->unlock();
}
-Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, ResourceFormatLoader::CacheMode p_cache_mode, const String &p_source_resource) {
- String local_path;
- if (p_path.is_rel_path()) {
- local_path = "res://" + p_path;
+static String _validate_local_path(const String &p_path) {
+ ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(p_path);
+ if (uid != ResourceUID::INVALID_ID) {
+ return ResourceUID::get_singleton()->get_id_path(uid);
+ } else if (p_path.is_rel_path()) {
+ return "res://" + p_path;
} else {
- local_path = ProjectSettings::get_singleton()->localize_path(p_path);
+ return ProjectSettings::get_singleton()->localize_path(p_path);
}
+}
+Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, ResourceFormatLoader::CacheMode p_cache_mode, const String &p_source_resource) {
+ String local_path = _validate_local_path(p_path);
thread_load_mutex->lock();
@@ -399,12 +412,7 @@ float ResourceLoader::_dependency_get_progress(const String &p_path) {
}
ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const String &p_path, float *r_progress) {
- String local_path;
- if (p_path.is_rel_path()) {
- local_path = "res://" + p_path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(p_path);
- }
+ String local_path = _validate_local_path(p_path);
thread_load_mutex->lock();
if (!thread_load_tasks.has(local_path)) {
@@ -424,12 +432,7 @@ ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const
}
RES ResourceLoader::load_threaded_get(const String &p_path, Error *r_error) {
- String local_path;
- if (p_path.is_rel_path()) {
- local_path = "res://" + p_path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(p_path);
- }
+ String local_path = _validate_local_path(p_path);
thread_load_mutex->lock();
if (!thread_load_tasks.has(local_path)) {
@@ -510,12 +513,7 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, Resour
*r_error = ERR_CANT_OPEN;
}
- String local_path;
- if (p_path.is_rel_path()) {
- local_path = "res://" + p_path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(p_path);
- }
+ String local_path = _validate_local_path(p_path);
if (p_cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
thread_load_mutex->lock();
@@ -612,12 +610,7 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, Resour
}
bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
- String local_path;
- if (p_path.is_rel_path()) {
- local_path = "res://" + p_path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(p_path);
- }
+ String local_path = _validate_local_path(p_path);
if (ResourceCache::has(local_path)) {
return true; // If cached, it probably exists
@@ -677,14 +670,7 @@ void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_f
}
int ResourceLoader::get_import_order(const String &p_path) {
- String path = _path_remap(p_path);
-
- String local_path;
- if (path.is_rel_path()) {
- local_path = "res://" + path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(path);
- }
+ String local_path = _path_remap(_validate_local_path(p_path));
for (int i = 0; i < loader_count; i++) {
if (!loader[i]->recognize_path(local_path)) {
@@ -702,14 +688,7 @@ int ResourceLoader::get_import_order(const String &p_path) {
}
String ResourceLoader::get_import_group_file(const String &p_path) {
- String path = _path_remap(p_path);
-
- String local_path;
- if (path.is_rel_path()) {
- local_path = "res://" + path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(path);
- }
+ String local_path = _path_remap(_validate_local_path(p_path));
for (int i = 0; i < loader_count; i++) {
if (!loader[i]->recognize_path(local_path)) {
@@ -727,14 +706,7 @@ String ResourceLoader::get_import_group_file(const String &p_path) {
}
bool ResourceLoader::is_import_valid(const String &p_path) {
- String path = _path_remap(p_path);
-
- String local_path;
- if (path.is_rel_path()) {
- local_path = "res://" + path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(path);
- }
+ String local_path = _path_remap(_validate_local_path(p_path));
for (int i = 0; i < loader_count; i++) {
if (!loader[i]->recognize_path(local_path)) {
@@ -752,14 +724,7 @@ bool ResourceLoader::is_import_valid(const String &p_path) {
}
bool ResourceLoader::is_imported(const String &p_path) {
- String path = _path_remap(p_path);
-
- String local_path;
- if (path.is_rel_path()) {
- local_path = "res://" + path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(path);
- }
+ String local_path = _path_remap(_validate_local_path(p_path));
for (int i = 0; i < loader_count; i++) {
if (!loader[i]->recognize_path(local_path)) {
@@ -777,14 +742,7 @@ bool ResourceLoader::is_imported(const String &p_path) {
}
void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
- String path = _path_remap(p_path);
-
- String local_path;
- if (path.is_rel_path()) {
- local_path = "res://" + path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(path);
- }
+ String local_path = _path_remap(_validate_local_path(p_path));
for (int i = 0; i < loader_count; i++) {
if (!loader[i]->recognize_path(local_path)) {
@@ -800,14 +758,7 @@ void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_depe
}
Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
- String path = _path_remap(p_path);
-
- String local_path;
- if (path.is_rel_path()) {
- local_path = "res://" + path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(path);
- }
+ String local_path = _path_remap(_validate_local_path(p_path));
for (int i = 0; i < loader_count; i++) {
if (!loader[i]->recognize_path(local_path)) {
@@ -825,12 +776,7 @@ Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String
}
String ResourceLoader::get_resource_type(const String &p_path) {
- String local_path;
- if (p_path.is_rel_path()) {
- local_path = "res://" + p_path;
- } else {
- local_path = ProjectSettings::get_singleton()->localize_path(p_path);
- }
+ String local_path = _validate_local_path(p_path);
for (int i = 0; i < loader_count; i++) {
String result = loader[i]->get_resource_type(local_path);
@@ -842,6 +788,19 @@ String ResourceLoader::get_resource_type(const String &p_path) {
return "";
}
+ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {
+ String local_path = _validate_local_path(p_path);
+
+ for (int i = 0; i < loader_count; i++) {
+ ResourceUID::ID id = loader[i]->get_resource_uid(local_path);
+ if (id != ResourceUID::INVALID_ID) {
+ return id;
+ }
+ }
+
+ return ResourceUID::INVALID_ID;
+}
+
String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_remapped) {
String new_path = p_path;
@@ -978,15 +937,15 @@ void ResourceLoader::load_translation_remaps() {
Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps");
List<Variant> keys;
remaps.get_key_list(&keys);
- for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
- Array langs = remaps[E->get()];
+ for (const Variant &E : keys) {
+ Array langs = remaps[E];
Vector<String> lang_remaps;
lang_remaps.resize(langs.size());
for (int i = 0; i < langs.size(); i++) {
lang_remaps.write[i] = langs[i];
}
- translation_remaps[String(E->get())] = lang_remaps;
+ translation_remaps[String(E)] = lang_remaps;
}
}
@@ -1071,8 +1030,7 @@ void ResourceLoader::add_custom_loaders() {
List<StringName> global_classes;
ScriptServer::get_global_class_list(&global_classes);
- for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
- StringName class_name = E->get();
+ for (const StringName &class_name : global_classes) {
StringName base_class = ScriptServer::get_global_class_native_base(class_name);
if (base_class == custom_loader_base_class) {
diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h
index c656b9a69c..e525e80a9d 100644
--- a/core/io/resource_loader.h
+++ b/core/io/resource_loader.h
@@ -40,9 +40,9 @@ class ResourceFormatLoader : public RefCounted {
public:
enum CacheMode {
- CACHE_MODE_IGNORE, //resource and subresources do not use path cache, no path is set into resource.
- CACHE_MODE_REUSE, //resource and subresources use patch cache, reuse existing loaded resources instead of loading from disk when available
- CACHE_MODE_REPLACE, //resource and and subresource use path cache, but replace existing loaded resources when available with information from disk
+ CACHE_MODE_IGNORE, // Resource and subresources do not use path cache, no path is set into resource.
+ CACHE_MODE_REUSE, // Resource and subresources use patch cache, reuse existing loaded resources instead of loading from disk when available.
+ CACHE_MODE_REPLACE, // Resource and subresource use path cache, but replace existing loaded resources when available with information from disk.
};
protected:
@@ -56,6 +56,7 @@ public:
virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
+ virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
virtual bool is_import_valid(const String &p_path) const { return true; }
@@ -107,7 +108,7 @@ private:
friend class ResourceFormatImporter;
friend class ResourceInteractiveLoader;
- //internal load function
+ // Internal load function.
static RES _load(const String &p_path, const String &p_original_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error, bool p_use_sub_threads, float *r_progress);
static ResourceLoadedCallback _loaded_callback;
@@ -157,6 +158,7 @@ public:
static void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front = false);
static void remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader);
static String get_resource_type(const String &p_path);
+ static ResourceUID::ID get_resource_uid(const String &p_path);
static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
static bool is_import_valid(const String &p_path);
diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp
index 80cb85fba3..564de5ee69 100644
--- a/core/io/resource_saver.cpp
+++ b/core/io/resource_saver.cpp
@@ -39,6 +39,7 @@ Ref<ResourceFormatSaver> ResourceSaver::saver[MAX_SAVERS];
int ResourceSaver::saver_count = 0;
bool ResourceSaver::timestamp_on_save = false;
ResourceSavedCallback ResourceSaver::save_callback = nullptr;
+ResourceSaverGetResourceIDForPath ResourceSaver::save_get_id_for_path = nullptr;
Error ResourceFormatSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
if (get_script_instance() && get_script_instance()->has_method("_save")) {
@@ -94,8 +95,8 @@ Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t
bool recognized = false;
saver[i]->get_recognized_extensions(p_resource, &extensions);
- for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
- if (E->get().nocasecmp_to(extension) == 0) {
+ for (const String &E : extensions) {
+ if (E.nocasecmp_to(extension) == 0) {
recognized = true;
}
}
@@ -236,8 +237,7 @@ void ResourceSaver::add_custom_savers() {
List<StringName> global_classes;
ScriptServer::get_global_class_list(&global_classes);
- for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
- StringName class_name = E->get();
+ for (const StringName &class_name : global_classes) {
StringName base_class = ScriptServer::get_global_class_native_base(class_name);
if (base_class == custom_saver_base_class) {
@@ -259,3 +259,14 @@ void ResourceSaver::remove_custom_savers() {
remove_resource_format_saver(custom_savers[i]);
}
}
+
+ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
+ if (save_get_id_for_path) {
+ return save_get_id_for_path(p_path, p_generate);
+ }
+ return ResourceUID::INVALID_ID;
+}
+
+void ResourceSaver::set_get_resource_id_for_path(ResourceSaverGetResourceIDForPath p_callback) {
+ save_get_id_for_path = p_callback;
+}
diff --git a/core/io/resource_saver.h b/core/io/resource_saver.h
index 07154aac4d..2fc8d32126 100644
--- a/core/io/resource_saver.h
+++ b/core/io/resource_saver.h
@@ -48,6 +48,7 @@ public:
};
typedef void (*ResourceSavedCallback)(Ref<Resource> p_resource, const String &p_path);
+typedef ResourceUID::ID (*ResourceSaverGetResourceIDForPath)(const String &p_path, bool p_generate);
class ResourceSaver {
enum {
@@ -58,6 +59,7 @@ class ResourceSaver {
static int saver_count;
static bool timestamp_on_save;
static ResourceSavedCallback save_callback;
+ static ResourceSaverGetResourceIDForPath save_get_id_for_path;
static Ref<ResourceFormatSaver> _find_custom_resource_format_saver(String path);
@@ -80,7 +82,10 @@ public:
static void set_timestamp_on_save(bool p_timestamp) { timestamp_on_save = p_timestamp; }
static bool get_timestamp_on_save() { return timestamp_on_save; }
+ static ResourceUID::ID get_resource_id_for_path(const String &p_path, bool p_generate = false);
+
static void set_save_callback(ResourceSavedCallback p_callback);
+ static void set_get_resource_id_for_path(ResourceSaverGetResourceIDForPath p_callback);
static bool add_custom_resource_format_saver(String script_path);
static void remove_custom_resource_format_saver(String script_path);
diff --git a/core/io/resource_uid.cpp b/core/io/resource_uid.cpp
new file mode 100644
index 0000000000..97d683f415
--- /dev/null
+++ b/core/io/resource_uid.cpp
@@ -0,0 +1,262 @@
+/*************************************************************************/
+/* resource_uid.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#include "resource_uid.h"
+#include "core/crypto/crypto.h"
+#include "core/io/dir_access.h"
+#include "core/io/file_access.h"
+
+static constexpr uint32_t char_count = ('z' - 'a');
+static constexpr uint32_t base = char_count + ('9' - '0');
+
+const char *ResourceUID::CACHE_FILE = "res://.godot/uid_cache.bin";
+
+String ResourceUID::id_to_text(ID p_id) const {
+ if (p_id < 0) {
+ return "uid://<invalid>";
+ }
+ String txt;
+
+ while (p_id) {
+ uint32_t c = p_id % base;
+ if (c < char_count) {
+ txt = String::chr('a' + c) + txt;
+ } else {
+ txt = String::chr('0' + (c - char_count)) + txt;
+ }
+ p_id /= base;
+ }
+
+ return "uid://" + txt;
+}
+
+ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const {
+ if (!p_text.begins_with("uid://") || p_text == "uid://<invalid>") {
+ return INVALID_ID;
+ }
+
+ uint32_t l = p_text.length();
+ uint64_t uid = 0;
+ for (uint32_t i = 6; i < l; i++) {
+ uid *= base;
+ uint32_t c = p_text[i];
+ if (c >= 'a' && c <= 'z') {
+ uid += c - 'a';
+ } else if (c >= '0' && c <= '9') {
+ uid += c - '0' + char_count;
+ } else {
+ return INVALID_ID;
+ }
+ }
+ return ID(uid & 0x7FFFFFFFFFFFFFFF);
+}
+
+ResourceUID::ID ResourceUID::create_id() const {
+ mutex.lock();
+ if (crypto.is_null()) {
+ crypto = Ref<Crypto>(Crypto::create());
+ }
+ mutex.unlock();
+ while (true) {
+ PackedByteArray bytes = crypto->generate_random_bytes(8);
+ ERR_FAIL_COND_V(bytes.size() != 8, INVALID_ID);
+ const uint64_t *ptr64 = (const uint64_t *)bytes.ptr();
+ ID id = int64_t((*ptr64) & 0x7FFFFFFFFFFFFFFF);
+ mutex.lock();
+ bool exists = unique_ids.has(id);
+ mutex.unlock();
+ if (!exists) {
+ return id;
+ }
+ }
+}
+
+bool ResourceUID::has_id(ID p_id) const {
+ MutexLock l(mutex);
+ return unique_ids.has(p_id);
+}
+void ResourceUID::add_id(ID p_id, const String &p_path) {
+ MutexLock l(mutex);
+ ERR_FAIL_COND(unique_ids.has(p_id));
+ Cache c;
+ c.cs = p_path.utf8();
+ unique_ids[p_id] = c;
+ changed = true;
+}
+
+void ResourceUID::set_id(ID p_id, const String &p_path) {
+ MutexLock l(mutex);
+ ERR_FAIL_COND(!unique_ids.has(p_id));
+ CharString cs = p_path.utf8();
+ if (strcmp(cs.ptr(), unique_ids[p_id].cs.ptr()) != 0) {
+ unique_ids[p_id].cs = cs;
+ unique_ids[p_id].saved_to_cache = false; //changed
+ changed = true;
+ }
+}
+
+String ResourceUID::get_id_path(ID p_id) const {
+ MutexLock l(mutex);
+ ERR_FAIL_COND_V(!unique_ids.has(p_id), String());
+ const CharString &cs = unique_ids[p_id].cs;
+ String s(cs.ptr());
+ return s;
+}
+void ResourceUID::remove_id(ID p_id) {
+ MutexLock l(mutex);
+ ERR_FAIL_COND(!unique_ids.has(p_id));
+ unique_ids.erase(p_id);
+}
+
+Error ResourceUID::save_to_cache() {
+ if (!FileAccess::exists(CACHE_FILE)) {
+ DirAccessRef d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
+ d->make_dir_recursive(String(CACHE_FILE).get_base_dir()); //ensure base dir exists
+ }
+
+ FileAccessRef f = FileAccess::open(CACHE_FILE, FileAccess::WRITE);
+ if (!f) {
+ return ERR_CANT_OPEN;
+ }
+
+ MutexLock l(mutex);
+ f->store_32(unique_ids.size());
+
+ cache_entries = 0;
+
+ for (OrderedHashMap<ID, Cache>::Element E = unique_ids.front(); E; E = E.next()) {
+ f->store_64(E.key());
+ uint32_t s = E.get().cs.length();
+ f->store_32(s);
+ f->store_buffer((const uint8_t *)E.get().cs.ptr(), s);
+ E.get().saved_to_cache = true;
+ cache_entries++;
+ }
+
+ changed = false;
+ return OK;
+}
+
+Error ResourceUID::load_from_cache() {
+ FileAccessRef f = FileAccess::open(CACHE_FILE, FileAccess::READ);
+ if (!f) {
+ return ERR_CANT_OPEN;
+ }
+
+ MutexLock l(mutex);
+ unique_ids.clear();
+
+ uint32_t entry_count = f->get_32();
+ for (uint32_t i = 0; i < entry_count; i++) {
+ int64_t id = f->get_64();
+ int32_t len = f->get_32();
+ Cache c;
+ c.cs.resize(len + 1);
+ ERR_FAIL_COND_V(c.cs.size() != len + 1, ERR_FILE_CORRUPT); // out of memory
+ c.cs[len] = 0;
+ int32_t rl = f->get_buffer((uint8_t *)c.cs.ptrw(), len);
+ ERR_FAIL_COND_V(rl != len, ERR_FILE_CORRUPT);
+
+ c.saved_to_cache = true;
+ unique_ids[id] = c;
+ }
+
+ cache_entries = entry_count;
+ changed = false;
+ return OK;
+}
+
+Error ResourceUID::update_cache() {
+ if (!changed) {
+ return OK;
+ }
+
+ if (cache_entries == 0) {
+ return save_to_cache();
+ }
+ MutexLock l(mutex);
+
+ FileAccess *f = nullptr;
+ for (OrderedHashMap<ID, Cache>::Element E = unique_ids.front(); E; E = E.next()) {
+ if (!E.get().saved_to_cache) {
+ if (f == nullptr) {
+ f = FileAccess::open(CACHE_FILE, FileAccess::READ_WRITE); //append
+ if (!f) {
+ return ERR_CANT_OPEN;
+ }
+ f->seek_end();
+ }
+ f->store_64(E.key());
+ uint32_t s = E.get().cs.length();
+ f->store_32(s);
+ f->store_buffer((const uint8_t *)E.get().cs.ptr(), s);
+ E.get().saved_to_cache = true;
+ cache_entries++;
+ }
+ }
+
+ if (f != nullptr) {
+ f->seek(0);
+ f->store_32(cache_entries); //update amount of entries
+ f->close();
+ memdelete(f);
+ }
+
+ changed = false;
+
+ return OK;
+}
+
+void ResourceUID::clear() {
+ cache_entries = 0;
+ unique_ids.clear();
+ changed = false;
+}
+void ResourceUID::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("id_to_text", "id"), &ResourceUID::id_to_text);
+ ClassDB::bind_method(D_METHOD("text_to_id", "text_id"), &ResourceUID::text_to_id);
+
+ ClassDB::bind_method(D_METHOD("create_id"), &ResourceUID::create_id);
+
+ ClassDB::bind_method(D_METHOD("has_id", "id"), &ResourceUID::has_id);
+ ClassDB::bind_method(D_METHOD("add_id", "id", "path"), &ResourceUID::add_id);
+ ClassDB::bind_method(D_METHOD("set_id", "id", "path"), &ResourceUID::set_id);
+ ClassDB::bind_method(D_METHOD("get_id_path", "id"), &ResourceUID::get_id_path);
+ ClassDB::bind_method(D_METHOD("remove_id", "id"), &ResourceUID::remove_id);
+
+ BIND_CONSTANT(INVALID_ID)
+}
+ResourceUID *ResourceUID::singleton = nullptr;
+ResourceUID::ResourceUID() {
+ ERR_FAIL_COND(singleton != nullptr);
+ singleton = this;
+}
+ResourceUID::~ResourceUID() {
+}
diff --git a/core/io/resource_uid.h b/core/io/resource_uid.h
new file mode 100644
index 0000000000..b12138425a
--- /dev/null
+++ b/core/io/resource_uid.h
@@ -0,0 +1,89 @@
+/*************************************************************************/
+/* resource_uid.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#ifndef RESOURCE_UUID_H
+#define RESOURCE_UUID_H
+
+#include "core/object/ref_counted.h"
+#include "core/string/string_name.h"
+#include "core/templates/ordered_hash_map.h"
+
+class Crypto;
+class ResourceUID : public Object {
+ GDCLASS(ResourceUID, Object)
+public:
+ typedef int64_t ID;
+ enum {
+ INVALID_ID = -1
+ };
+
+ static const char *CACHE_FILE;
+
+private:
+ mutable Ref<Crypto> crypto;
+ Mutex mutex;
+ struct Cache {
+ CharString cs;
+ bool saved_to_cache = false;
+ };
+
+ OrderedHashMap<ID, Cache> unique_ids; //unique IDs and utf8 paths (less memory used)
+ static ResourceUID *singleton;
+
+ uint32_t cache_entries = 0;
+ bool changed = false;
+
+protected:
+ static void _bind_methods();
+
+public:
+ String id_to_text(ID p_id) const;
+ ID text_to_id(const String &p_text) const;
+
+ ID create_id() const;
+ bool has_id(ID p_id) const;
+ void add_id(ID p_id, const String &p_path);
+ void set_id(ID p_id, const String &p_path);
+ String get_id_path(ID p_id) const;
+ void remove_id(ID p_id);
+
+ Error load_from_cache();
+ Error save_to_cache();
+ Error update_cache();
+
+ void clear();
+
+ static ResourceUID *get_singleton() { return singleton; }
+
+ ResourceUID();
+ ~ResourceUID();
+};
+
+#endif // RESOURCEUUID_H