Skip to content

Commit

Permalink
Add array serializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1maker committed Oct 29, 2024
1 parent 7fcaaf6 commit 9497432
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
15 changes: 15 additions & 0 deletions Release/Tests/serialize_array.ngt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
array<string> items;
void main(){
items.insert_last("Hello");
items.insert_last("Test");
string data = serialize_array(items);
file f;
f.open("Test.txt", "w");
f.write(data);
f.close();
array<string>@ items_des = array<string>();
deserialize_array(data, items_des);
for (uint i = 0; i < items_des.length(); ++i){
alert("Value", items_des[i]);
}
}
Binary file modified Release/Tests/test.txt
Binary file not shown.
15 changes: 8 additions & 7 deletions SRC/ngt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,9 @@ CScriptDictionary* deserialize(const string& data) {
}
return dict;
}
string serialize_array(CScriptArray* data) {
if (data == nullptr) return "";

void serialize_array(asIScriptGeneric* gen) {
CScriptArray* data = (CScriptArray*)gen->GetArgAddress(0);
asIScriptContext* context = asGetActiveContext();
asIScriptEngine* engine = context->GetEngine();
std::string serialized;
Expand Down Expand Up @@ -1383,18 +1384,18 @@ string serialize_array(CScriptArray* data) {
break;
}
}
return serialized;
gen->SetReturnObject(&serialized);
}

CScriptArray* deserialize_array(const string& data) {
void deserialize_array(asIScriptGeneric* gen) {
std::string data = *(std::string*)gen->GetArgAddress(0);
CScriptArray* array = (CScriptArray*)gen->GetArgAddress(1);
asIScriptContext* context = asGetActiveContext();
asIScriptEngine* engine = context->GetEngine();
int32_t array_tid;
cmp_buffer buf = { (string*)&data, 0 };
cmp_ctx_t sctx = { 0, &buf, cmp_read_bytes, cmp_skip_bytes, cmp_write_bytes };
cmp_read_s32(&sctx, &array_tid);
asITypeInfo* arrayType = engine->GetTypeInfoById(array_tid);
CScriptArray* array = CScriptArray::Create(arrayType);
uint64_t size;
cmp_read_u64(&sctx, &size);
array->Reserve(size);
Expand Down Expand Up @@ -1469,7 +1470,7 @@ CScriptArray* deserialize_array(const string& data) {
array->InsertAt(i, &val);
}
}
return array;
gen->SetReturnDWord(1);
}


Expand Down
4 changes: 2 additions & 2 deletions SRC/ngt.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ string read_environment_variable(const string&);
bool write_environment_variable(const string& path, const string& value);
string serialize(CScriptDictionary*);
CScriptDictionary* deserialize(const string&);
string serialize_array(CScriptArray*);
CScriptArray* deserialize_array(const string&);
void serialize_array(asIScriptGeneric*);
void deserialize_array(asIScriptGeneric* gen);
bool urlopen(const string& url);
std::string c_str_to_string(const char* ptr, size_t length = 0);
std::wstring wc_str_to_wstring(const wchar_t* ptr, size_t length = 0);
Expand Down
3 changes: 3 additions & 0 deletions SRC/ngtreg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ void RegisterFunctions(asIScriptEngine* engine)

engine->RegisterGlobalFunction("string serialize(dictionary@ handle = null)", asFUNCTION(serialize), asCALL_CDECL);
engine->RegisterGlobalFunction("dictionary@ deserialize(const string &in the_serialized_dictionary)", asFUNCTION(deserialize), asCALL_CDECL);
engine->RegisterGlobalFunction("string serialize_array(?&in array)", asFUNCTION(serialize_array), asCALL_GENERIC);
engine->RegisterGlobalFunction("bool deserialize_array(const string &in the_serialized_array, ?&out array)", asFUNCTION(deserialize_array), asCALL_GENERIC);

engine->RegisterGlobalFunction("bool urlopen(const string &in url)", asFUNCTION(urlopen), asCALL_CDECL);
engine->RegisterGlobalFunction("uint64 malloc(size_t alloc_size)", asFUNCTION(malloc), asCALL_CDECL);
engine->RegisterGlobalFunction("uint64 calloc(uint64, uint64)", asFUNCTION(calloc), asCALL_CDECL);
Expand Down

0 comments on commit 9497432

Please sign in to comment.