ongoing refactor of serialization and various cleanup
This commit is contained in:
1 parent
1a045c3578
commit
a462575af4
43 files changed
+952
-483
No files matched your search
@@ -2,16 +2,126 @@
|
||||
|
||||
#include <Juliet.h>
|
||||
|
||||
#include <Core/Common/String.h>
|
||||
#include <Core/HAL/IO/IOStream.h>
|
||||
|
||||
// .jasset
|
||||
// AssetFile ::= { CommentLine | EmptyLine | PropertyDeclaration } ;
|
||||
// CommentLine ::= ( "#" | "//" ) { Character } LineEnding ;
|
||||
// EmptyLine ::= { Whitespace } LineEnding ;
|
||||
// PropertyDeclaration ::= KeyHeader LineEnding ValueBlock ;
|
||||
// KeyHeader ::= ";" { Whitespace } Identifier ;
|
||||
// ValueBlock ::= { ValueLine LineEnding } ;
|
||||
// ValueLine ::= { Whitespace } ValueString { Whitespace } ;
|
||||
// LineEnding ::= "\r\n" | "\n" ;
|
||||
// Identifier ::= [a-zA-Z_][a-zA-Z0-9_]* ;
|
||||
|
||||
struct Vector4;
|
||||
struct IOStream;
|
||||
struct Arena;
|
||||
|
||||
struct archive
|
||||
struct ArchivePropertyNode
|
||||
{
|
||||
Arena* arena;
|
||||
void* base_ptr;
|
||||
index_t offset;
|
||||
bool loading;
|
||||
String key;
|
||||
String value;
|
||||
uint32 key_crc;
|
||||
bool consumed;
|
||||
};
|
||||
|
||||
JULIET_API void serialize(archive& ar, void* data, size_t size);
|
||||
struct ParsedArchive
|
||||
{
|
||||
ArchivePropertyNode* nodes = nullptr;
|
||||
uint32 property_count = 0;
|
||||
};
|
||||
|
||||
struct Archive
|
||||
{
|
||||
Arena* arena;
|
||||
bool loading;
|
||||
ParsedArchive base = {};
|
||||
IOStream* stream = nullptr;
|
||||
uint32 base_version = 0;
|
||||
uint16 class_version = 0;
|
||||
|
||||
// to remove
|
||||
void* base_ptr = nullptr;
|
||||
index_t offset = 0;
|
||||
};
|
||||
|
||||
JULIET_API void serialize(Archive& ar, void* data, size_t size);
|
||||
|
||||
#define serialize_elem(ar, val) serialize((ar), &(val), sizeof(val))
|
||||
|
||||
JULIET_API ParsedArchive tokenize_archive(NonNullPtr<Arena> arena, ByteBuffer file_buffer);
|
||||
JULIET_API ArchivePropertyNode* find_property(NonNullPtr<ParsedArchive> archive, uint32 property_crc);
|
||||
JULIET_API void write_property_header(Archive& archive, String property_name);
|
||||
|
||||
JULIET_API bool read_prop(Archive& ar, String value_raw, String& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, String value);
|
||||
JULIET_API bool read(const char* buffer, float& value, const char** end = nullptr);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, float value);
|
||||
JULIET_API bool read(const char* buffer, int8& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, int8 value);
|
||||
JULIET_API bool read(const char* buffer, int16& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, int16 value);
|
||||
JULIET_API bool read(const char* buffer, int32& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, int32 value);
|
||||
JULIET_API bool read(const char* buffer, int64& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, int64 value);
|
||||
JULIET_API bool read(const char* buffer, uint8& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, uint8 value);
|
||||
JULIET_API bool read(const char* buffer, uint16& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, uint16 value);
|
||||
JULIET_API bool read(const char* buffer, uint32& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, uint32 value);
|
||||
JULIET_API bool read(const char* buffer, uint64& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, uint64 value);
|
||||
JULIET_API bool read(const char* buffer, bool& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, bool value);
|
||||
JULIET_API bool read(const char* buffer, Vector4& value);
|
||||
JULIET_API void write(NonNullPtr<IOStream> stream, Vector4 value);
|
||||
|
||||
// For primitives not needing archive nor allocation
|
||||
template <typename Type>
|
||||
bool read_prop(Archive& /*ar*/, String value_raw, Type& value)
|
||||
{
|
||||
char buffer[64];
|
||||
size_t cpy_size = Min(value_raw.Size, sizeof(buffer) - 1);
|
||||
MemCopy(buffer, value_raw.Str, cpy_size);
|
||||
buffer[cpy_size] = '\0';
|
||||
|
||||
return read(buffer, value);
|
||||
}
|
||||
|
||||
#define SERIALIZE(ar, name, var) serialize((ar), ConstString(#name), #name##_crc32, (var))
|
||||
|
||||
template <typename Type>
|
||||
bool serialize(Archive& ar, String property_name, uint32 property_crc, Type& value)
|
||||
{
|
||||
Assert(IsValid(property_name));
|
||||
|
||||
bool result = false;
|
||||
if (ar.loading)
|
||||
{
|
||||
if (auto* prop = find_property(&ar.base, property_crc))
|
||||
{
|
||||
if (read_prop(ar, prop->value, value))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Save
|
||||
write_property_header(ar, property_name);
|
||||
write(ar.stream, value);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#if JULIET_DEBUG
|
||||
JULIET_API void audit_unconsumed_properties(NonNullPtr<ParsedArchive> archive, String context_name);
|
||||
#endif
|
||||
Reference in new issue
Block a user