Reading / writing user id based on os username

This commit is contained in:
Patedam committed 2026-09-19 21:48:02 -04:00
1 parent f32a3f73a9
commit 7075556bf2
1 file changed
+53 -13
+53 -13
View File
@@ -8,6 +8,11 @@
#include <Core/Thread/ThreadContext.h> #include <Core/Thread/ThreadContext.h>
#include <Engine/Engine.h> #include <Engine/Engine.h>
namespace
{
UserID local_user_id = 0U;
}
void initialize_user_system() void initialize_user_system()
{ {
// Project needs a user.ini file with all users // 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+")); IOStream* stream = IOFromFile(temp.Arena, user_system_path.FullPath, ConstString("w+"));
Archive ar = { .arena = temp.Arena, .loading = false, .stream = stream }; Archive ar = { .arena = temp.Arena, .loading = false, .stream = stream };
uint8 id = 1; UserID id = 1;
serialize(ar, username, crc32(username), id); serialize(ar, username, crc32(username), id);
local_user_id = id;
IOClose(stream); IOClose(stream);
} }
else else
{ {
ByteBuffer user_file = LoadFile(temp.Arena, user_system_path.FullPath); NonNullPtr<IOStream> stream = IOFromFile(temp.Arena, user_system_path.FullPath, ConstString("r+"));
size_t file_size = IOSize(stream);
ParsedArchive parsed_archive = tokenize_archive(temp.Arena, user_file); if (file_size == 0)
Archive ar = { .arena = temp.Arena, .loading = true, .base = parsed_archive };
if (ArchivePropertyNode* prop = find_property(&parsed_archive, crc32(username)))
{ {
// User exists lets read Archive ar = { .arena = temp.Arena, .loading = false, .stream = stream };
uint8 user_id = 0; UserID id = 1;
read_prop(ar, prop->value, user_id); serialize(ar, username, crc32(username), id);
Assert(user_id != 0); 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<uint8>(biggest_id, id + 1);
}
serialize(ar, username, crc32(username), biggest_id);
local_user_id = biggest_id;
}
}
IOClose(stream);
} }
scratch_end(temp); scratch_end(temp);
@@ -58,5 +98,5 @@ void shutdown_user_system() {}
uint8 get_local_user_id() uint8 get_local_user_id()
{ {
return 0u; return local_user_id;
} }