From 7075556bf2b14601245a0895508fdd772836a51e Mon Sep 17 00:00:00 2001 From: Patedam Date: Sat, 19 Sep 2026 21:48:02 -0400 Subject: [PATCH] Reading / writing user id based on os username --- Juliet/src/Engine/user.cpp | 66 ++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/Juliet/src/Engine/user.cpp b/Juliet/src/Engine/user.cpp index 0f0f4ab..37f1ae3 100644 --- a/Juliet/src/Engine/user.cpp +++ b/Juliet/src/Engine/user.cpp @@ -8,6 +8,11 @@ #include #include +namespace +{ + UserID local_user_id = 0U; +} + void initialize_user_system() { // Project needs a user.ini file with all users @@ -29,26 +34,61 @@ void initialize_user_system() IOStream* stream = IOFromFile(temp.Arena, user_system_path.FullPath, ConstString("w+")); Archive ar = { .arena = temp.Arena, .loading = false, .stream = stream }; - uint8 id = 1; + UserID id = 1; serialize(ar, username, crc32(username), id); + local_user_id = id; IOClose(stream); } else { - ByteBuffer user_file = LoadFile(temp.Arena, user_system_path.FullPath); - - ParsedArchive parsed_archive = tokenize_archive(temp.Arena, user_file); - - Archive ar = { .arena = temp.Arena, .loading = true, .base = parsed_archive }; - - if (ArchivePropertyNode* prop = find_property(&parsed_archive, crc32(username))) + NonNullPtr stream = IOFromFile(temp.Arena, user_system_path.FullPath, ConstString("r+")); + size_t file_size = IOSize(stream); + if (file_size == 0) { - // User exists lets read - uint8 user_id = 0; - read_prop(ar, prop->value, user_id); - Assert(user_id != 0); + Archive ar = { .arena = temp.Arena, .loading = false, .stream = stream }; + UserID id = 1; + serialize(ar, username, crc32(username), id); + local_user_id = id; } + else + { + Byte* content = (Byte*)ArenaPushSize(temp.Arena, file_size, 8, true JULIET_DEBUG_PARAM("File Content")); + IORead(stream, content, file_size); + + ByteBuffer user_file = { content, file_size }; + + ParsedArchive parsed_archive = tokenize_archive(temp.Arena, user_file); + + if (ArchivePropertyNode* prop = find_property(&parsed_archive, crc32(username))) + { + Archive ar = { .arena = temp.Arena, .loading = true, .base = parsed_archive }; + + // User exists lets read + read_prop(ar, prop->value, local_user_id); + Assert(local_user_id != 0); + } + else + { + // User do not exist lets create it + Archive ar = { .arena = temp.Arena, .loading = false, .stream = stream }; + + // Read all users and take biggest id + 1 + uint8 biggest_id = 0; + auto* nodes = parsed_archive.nodes; + for (index_t idx = 0; idx < parsed_archive.property_count; ++idx) + { + uint8 id = 0; + read_prop(ar, nodes[idx].value, id); + biggest_id = Max(biggest_id, id + 1); + } + + serialize(ar, username, crc32(username), biggest_id); + local_user_id = biggest_id; + } + } + + IOClose(stream); } scratch_end(temp); @@ -58,5 +98,5 @@ void shutdown_user_system() {} uint8 get_local_user_id() { - return 0u; + return local_user_id; }