Saving Data using a Deserializer
-
I made a really fast deserializer and validator that supports
int,float,vector,array,string, andstructvars by using recursion, allowing for 2D and 3D arrays of all supported data types. (4D arrays may cause a recursive error though)I'm also making a save data system using it, which is also in the project.
(Pending) Download ID:
D2K73MNDX5UPDATE: After looking at @pianofire's code and redesigning the save data system, the loading and saving is near-instant:
(roughly the code in the project)How to use the Save Data System:
store = [] // values to save and load int multiArray[10][2] int integerNum = 100 // keys for saving and loading int MultiArrayKey int IntegerKey if !LoadStore() then // Initialize values MultiArrayKey = NewValue("2DArray1", multiArray) IntegerKey = NewValue("integer", integerNum) // Save new values savestore() else // Find keys MultiArrayKey = FindKey("2DArray1") IntegerKey = FindKey("integer") // Deserialize values from stored strings multiArray = GetValue(MultiArrayKey) integerNum = GetValue(IntegerKey) endifDeserializer Functions:
TryDeserializeVar
// --- Deserializer FUNCTIONS--- (some at least) function TryDeserializeVar(aString) isAnyType = true aString = str(aString) type = "void" null v result = TryDeserializeString(aString) if (result.isType) then v = result.result else result = TryDeserializeNumber(aString) if (result.isType) then v = result.result else result = TryDeserializeVector(aString) if (result.isType) then v = result.result else result = TryDeserializeArray(aString) if (result.isType) then v = result.result else isAnyType = false endif endif endif endif resultStruct = [.isType = isAnyType, .result = v, .type = type] return resultStructArray Deserialization
function TryDeserializeArray(aString) aString = str(aString) elements = TryGetArrayElements(aString) array anArray[0] if elements.isType then for i = 0 to len(elements.result) result = TryDeserializeVar(elements.result[i]) if (result.isType then anArray[len(anArray)] = result.result else break endif repeat endif resultStruct = [.isType = elements.isType, .result = anArray] return resultStructString Deserialization
function TryDeserializeString(aString) aString = str(aString) quote = chr(34) foundQuote = false result = "" isType = false endQuote = 0 startQuote = -1 for i = 0 to len(aString) loop if aString[i] == quote then endQuote = i endif repeat for i = 0 to len(aString) loop if aString[i] == quote and !foundQuote then foundQuote = true i += 1 if i >= len(aString) then break endif endif if foundQuote and i == endQuote then isType = true break endif if aString[i] != quote and !foundQuote then isType = false break endif if foundQuote then result += aString[i] endif repeat resultStruct = [.isType = isType, .result = result] return resultStructmisc
function var(aString) return tryDeserializeVar.result struct null int null endstructthe deserializer can be used separately, like if you're using @gothon's filesystem you can get values using this.
-
Update: added support for structs with a really easy way to create custom converters for custom structs. Some come with the deserializer, like
controls(i), i'm still adding in the rest of the system structs.One thing to note is that the deserializer treats structs alot like arrays, so a 3D array with structs may cause a recursive error like 4D arrays.