diff --git a/Release/Tests/serialize_array.ngt b/Release/Tests/serialize_array.ngt new file mode 100644 index 0000000..6ebc53a --- /dev/null +++ b/Release/Tests/serialize_array.ngt @@ -0,0 +1,15 @@ +array 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@ items_des = array(); +deserialize_array(data, items_des); +for (uint i = 0; i < items_des.length(); ++i){ +alert("Value", items_des[i]); +} +} \ No newline at end of file diff --git a/Release/Tests/test.txt b/Release/Tests/test.txt index 99d14c7..593dc99 100644 Binary files a/Release/Tests/test.txt and b/Release/Tests/test.txt differ diff --git a/SRC/ngt.cpp b/SRC/ngt.cpp index 76d51e0..574b0fc 100644 --- a/SRC/ngt.cpp +++ b/SRC/ngt.cpp @@ -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; @@ -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); @@ -1469,7 +1470,7 @@ CScriptArray* deserialize_array(const string& data) { array->InsertAt(i, &val); } } - return array; + gen->SetReturnDWord(1); } diff --git a/SRC/ngt.h b/SRC/ngt.h index 49b98bb..427909e 100644 --- a/SRC/ngt.h +++ b/SRC/ngt.h @@ -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); diff --git a/SRC/ngtreg.cpp b/SRC/ngtreg.cpp index cf2dd8a..a80455d 100644 --- a/SRC/ngtreg.cpp +++ b/SRC/ngtreg.cpp @@ -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);