Skip to content

Commit 653f5a9

Browse files
committed
Merge pull request #100976 from AThousandShips/uid_fixes
[Core] Fix UID encoding
2 parents 881d4bd + 25ecf5e commit 653f5a9

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

core/io/resource_uid.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ String ResourceUID::get_cache_file() {
4747

4848
static constexpr uint8_t uuid_characters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', '0', '1', '2', '3', '4', '5', '6', '7', '8' };
4949
static constexpr uint32_t uuid_characters_element_count = (sizeof(uuid_characters) / sizeof(*uuid_characters));
50-
static constexpr uint8_t max_uuid_number_length = 19; // Max 0x7FFFFFFFFFFFFFFF size is 19 digits.
50+
static constexpr uint8_t max_uuid_number_length = 13; // Max 0x7FFFFFFFFFFFFFFF (uid://d4n4ub6itg400) size is 13 characters.
5151

5252
String ResourceUID::id_to_text(ID p_id) const {
5353
if (p_id < 0) {
@@ -56,12 +56,12 @@ String ResourceUID::id_to_text(ID p_id) const {
5656

5757
char32_t tmp[max_uuid_number_length];
5858
uint32_t tmp_size = 0;
59-
while (p_id) {
59+
do {
6060
uint32_t c = p_id % uuid_characters_element_count;
6161
tmp[tmp_size] = uuid_characters[c];
6262
p_id /= uuid_characters_element_count;
6363
++tmp_size;
64-
}
64+
} while (p_id);
6565

6666
// tmp_size + uid:// (6) + 1 for null.
6767
String txt;

tests/core/io/test_resource_uid.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**************************************************************************/
2+
/* test_resource_uid.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef TEST_RESOURCE_UID_H
32+
#define TEST_RESOURCE_UID_H
33+
34+
#include "core/io/resource_uid.h"
35+
36+
#include "thirdparty/doctest/doctest.h"
37+
38+
#include "tests/test_macros.h"
39+
40+
namespace TestResourceUID {
41+
42+
TEST_CASE("[ResourceUID] Must encode/decode maximum/minimum UID correctly") {
43+
CHECK_MESSAGE(ResourceUID::get_singleton()->id_to_text(0x7fffffffffffffff) == "uid://d4n4ub6itg400", "Maximum UID must encode correctly.");
44+
CHECK_MESSAGE(ResourceUID::get_singleton()->text_to_id("uid://d4n4ub6itg400") == 0x7fffffffffffffff, "Maximum UID must decode correctly.");
45+
46+
CHECK_MESSAGE(ResourceUID::get_singleton()->id_to_text(0) == "uid://a", "Minimum UID must encode correctly.");
47+
CHECK_MESSAGE(ResourceUID::get_singleton()->text_to_id("uid://a") == 0, "Minimum UID must decode correctly.");
48+
}
49+
50+
} // namespace TestResourceUID
51+
52+
#endif // TEST_RESOURCE_UID_H

tests/test_main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "tests/core/io/test_packet_peer.h"
5454
#include "tests/core/io/test_pck_packer.h"
5555
#include "tests/core/io/test_resource.h"
56+
#include "tests/core/io/test_resource_uid.h"
5657
#include "tests/core/io/test_stream_peer.h"
5758
#include "tests/core/io/test_stream_peer_buffer.h"
5859
#include "tests/core/io/test_tcp_server.h"

0 commit comments

Comments
 (0)