From 3cfcfa0c5d501255004ba3d027e5b3cd5895573d Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 23 Oct 2020 12:54:18 +0200 Subject: [PATCH] Make sure scriptstring arrays are being reallocated when they are reusable so if it is being referenced again the scriptstring indices are the ones of the zone instead of the asset that originally loaded them --- .../Templates/Loading/ArrayPointer.stg | 10 ++++++++ src/ZoneLoading/Loading/AssetLoader.cpp | 24 +++++++++++++++++-- src/ZoneLoading/Loading/AssetLoader.h | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/ZoneCodeGenerator/Generating/Templates/Loading/ArrayPointer.stg b/src/ZoneCodeGenerator/Generating/Templates/Loading/ArrayPointer.stg index b77ecf33..f4b7bc7c 100644 --- a/src/ZoneCodeGenerator/Generating/Templates/Loading/ArrayPointer.stg +++ b/src/ZoneCodeGenerator/Generating/Templates/Loading/ArrayPointer.stg @@ -40,7 +40,12 @@ $TypeVarName(structure.Type)$->$member.Member.Name$$PrintArrayIndices(reference) $\n$ varScriptString = $TypeVarName(structure.Type)$->$member.Member.Name$$PrintArrayIndices(reference)$;$\n$ +$if(!member.IsReusable)$ LoadScriptStringArray(true, $PrintEvaluation(reference.ArrayPointerCountEvaluation)$); +$else$ +LoadScriptStringArrayRealloc(true, $PrintEvaluation(reference.ArrayPointerCountEvaluation)$);$\n$ +$TypeVarName(structure.Type)$->$member.Member.Name$$PrintArrayIndices(reference)$ = varScriptString; +$endif$ %> @@ -70,6 +75,11 @@ if($TypeVarName(structure.Type)$->$member.Member.Name$$PrintArrayIndices(referen else { $TypeVarName(structure.Type)$->$member.Member.Name$$PrintArrayIndices(reference)$ = m_stream->ConvertOffsetToPointer($TypeVarName(structure.Type)$->$member.Member.Name$$PrintArrayIndices(reference)$); + $if(member.IsScriptString)$ + varScriptString = $TypeVarName(structure.Type)$->$member.Member.Name$$PrintArrayIndices(reference)$; + LoadScriptStringArrayRealloc(false, $PrintEvaluation(reference.ArrayPointerCountEvaluation)$); + $TypeVarName(structure.Type)$->$member.Member.Name$$PrintArrayIndices(reference)$ = varScriptString; + $endif$ }$\\$ $endif$ >> diff --git a/src/ZoneLoading/Loading/AssetLoader.cpp b/src/ZoneLoading/Loading/AssetLoader.cpp index 4c19608c..a608b0ee 100644 --- a/src/ZoneLoading/Loading/AssetLoader.cpp +++ b/src/ZoneLoading/Loading/AssetLoader.cpp @@ -52,10 +52,30 @@ void AssetLoader::LoadScriptStringArray(const bool atStreamStart, const size_t c if (atStreamStart) m_stream->Load(varScriptString, count); + auto* ptr = varScriptString; for (size_t index = 0; index < count; index++) { - *varScriptString = UseScriptString(*varScriptString); - varScriptString++; + *ptr = UseScriptString(*ptr); + ptr++; + } +} + +void AssetLoader::LoadScriptStringArrayRealloc(const bool atStreamStart, const size_t count) +{ + assert(varScriptString != nullptr); + + if (atStreamStart) + m_stream->Load(varScriptString, count); + + auto* scriptStringsNew = static_cast(m_zone->GetMemory()->Alloc(sizeof scr_string_t * count)); + memcpy_s(scriptStringsNew, sizeof scr_string_t * count, varScriptString, sizeof scr_string_t * count); + varScriptString = scriptStringsNew; + + auto* ptr = varScriptString; + for (size_t index = 0; index < count; index++) + { + *ptr = UseScriptString(*ptr); + ptr++; } } diff --git a/src/ZoneLoading/Loading/AssetLoader.h b/src/ZoneLoading/Loading/AssetLoader.h index df9b70bf..e2d32188 100644 --- a/src/ZoneLoading/Loading/AssetLoader.h +++ b/src/ZoneLoading/Loading/AssetLoader.h @@ -22,6 +22,7 @@ protected: scr_string_t UseScriptString(scr_string_t scrString); void LoadScriptStringArray(bool atStreamStart, size_t count); + void LoadScriptStringArrayRealloc(bool atStreamStart, size_t count); XAssetInfoGeneric* LinkAsset(std::string name, void* asset);