[ResourceUID] Use CryptoCore::RandomGenerator for IDs.
This commit is contained in:
parent
ee7b67e135
commit
f72bd67068
@ -31,7 +31,7 @@
|
||||
#include "resource_uid.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "core/crypto/crypto.h"
|
||||
#include "core/crypto/crypto_core.h"
|
||||
#include "core/io/dir_access.h"
|
||||
#include "core/io/file_access.h"
|
||||
|
||||
@ -82,20 +82,14 @@ ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const {
|
||||
return ID(uid & 0x7FFFFFFFFFFFFFFF);
|
||||
}
|
||||
|
||||
ResourceUID::ID ResourceUID::create_id() const {
|
||||
mutex.lock();
|
||||
if (crypto.is_null()) {
|
||||
crypto = Ref<Crypto>(Crypto::create());
|
||||
}
|
||||
mutex.unlock();
|
||||
ResourceUID::ID ResourceUID::create_id() {
|
||||
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();
|
||||
ID id = INVALID_ID;
|
||||
MutexLock lock(mutex);
|
||||
Error err = ((CryptoCore::RandomGenerator *)crypto)->get_random_bytes((uint8_t *)&id, sizeof(id));
|
||||
ERR_FAIL_COND_V(err != OK, INVALID_ID);
|
||||
id &= 0x7FFFFFFFFFFFFFFF;
|
||||
bool exists = unique_ids.has(id);
|
||||
mutex.unlock();
|
||||
if (!exists) {
|
||||
return id;
|
||||
}
|
||||
@ -261,6 +255,9 @@ ResourceUID *ResourceUID::singleton = nullptr;
|
||||
ResourceUID::ResourceUID() {
|
||||
ERR_FAIL_COND(singleton != nullptr);
|
||||
singleton = this;
|
||||
crypto = memnew(CryptoCore::RandomGenerator);
|
||||
((CryptoCore::RandomGenerator *)crypto)->init();
|
||||
}
|
||||
ResourceUID::~ResourceUID() {
|
||||
memdelete((CryptoCore::RandomGenerator *)crypto);
|
||||
}
|
||||
|
@ -35,7 +35,6 @@
|
||||
#include "core/string/string_name.h"
|
||||
#include "core/templates/ordered_hash_map.h"
|
||||
|
||||
class Crypto;
|
||||
class ResourceUID : public Object {
|
||||
GDCLASS(ResourceUID, Object)
|
||||
public:
|
||||
@ -47,7 +46,7 @@ public:
|
||||
static String get_cache_file();
|
||||
|
||||
private:
|
||||
mutable Ref<Crypto> crypto;
|
||||
void *crypto; // CryptoCore::RandomGenerator (avoid including crypto_core.h)
|
||||
Mutex mutex;
|
||||
struct Cache {
|
||||
CharString cs;
|
||||
@ -67,7 +66,7 @@ public:
|
||||
String id_to_text(ID p_id) const;
|
||||
ID text_to_id(const String &p_text) const;
|
||||
|
||||
ID create_id() const;
|
||||
ID create_id();
|
||||
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);
|
||||
|
@ -14,7 +14,7 @@
|
||||
<description>
|
||||
</description>
|
||||
</method>
|
||||
<method name="create_id" qualifiers="const">
|
||||
<method name="create_id">
|
||||
<return type="int" />
|
||||
<description>
|
||||
</description>
|
||||
|
Loading…
Reference in New Issue
Block a user