From 8cfd6b4a068e1957b41acaf0ed72b7e81ae3e8ff Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 11 Jul 2022 02:10:10 +0700 Subject: [PATCH] Fix persistent data saving/loading --- sources/Users/User.uc | 52 +++++++++++++++++++++++++++++++--------- sources/Users/UserAPI.uc | 5 ++-- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/sources/Users/User.uc b/sources/Users/User.uc index 2d1ccf2..db9ba36 100644 --- a/sources/Users/User.uc +++ b/sources/Users/User.uc @@ -69,6 +69,34 @@ public final function int GetKey() return key; } +/** + * Reads user's persistent data saved inside group `groupName`, saving it into + * a collection using mutable data types. + * Only should be used if `_.users.PersistentStorageExists()` returns `true`. + * + * @param groupName Name of the group these settings belong to. + * This exists to help reduce name collisions between different mods. + * Acedia stores all its settings under "Acedia" group. We suggest that you + * pick at least one name to use for your own mods. + * It should be unique enough to not get picked by others - "weapons" is + * a bad name, while "CoolModMastah79" is actually a good pick. + * @return Task object for reading specified persistent data from the database. + * For more info see `Database.ReadData()` method. + * Guaranteed to not be `none` iff + * `_.users.PersistentStorageExists() == true`. + */ +public final function DBReadTask ReadGroupOfPersistentData(BaseText groupName) +{ + local DBReadTask task; + if (groupName == none) return none; + if (!SetupDatabaseVariables()) return none; + + persistentSettingsPointer.Push(groupName); + task = persistentDatabase.ReadData(persistentSettingsPointer, true); + _.memory.Free(persistentSettingsPointer.Pop()); + return task; +} + /** * Reads user's persistent data saved under name `dataName`, saving it into * a collection using mutable data types. @@ -127,13 +155,13 @@ public final function DBWriteTask WritePersistentData( BaseText dataName, AcediaObject data) { - local DBWriteTask task; - local AssociativeArray emptyObject; + local DBWriteTask task; + local HashTable emptyObject; if (groupName == none) return none; if (dataName == none) return none; if (!SetupDatabaseVariables()) return none; - emptyObject = _.collections.EmptyAssociativeArray(); + emptyObject = _.collections.EmptyHashTable(); persistentSettingsPointer.Push(groupName); persistentDatabase.IncrementData(persistentSettingsPointer, emptyObject); persistentSettingsPointer.Push(dataName); @@ -150,9 +178,10 @@ public final function DBWriteTask WritePersistentData( // and `false` otherwise. private function bool SetupDatabaseVariables() { - local Text userDataLink; - local Text userTextID; - local AssociativeArray skeletonObject; + local Text userDataLink; + local Text userTextID; + local HashTable emptyObject, skeletonObject; + if ( persistentDatabase != none && persistentSettingsPointer != none && persistentDatabase.IsAllocated()) { @@ -171,17 +200,18 @@ private function bool SetupDatabaseVariables() } persistentSettingsPointer = _.db.GetPointer(userDataLink); userTextID = id.GetSteamID64String(); - skeletonObject = _.collections.EmptyAssociativeArray(); - skeletonObject.SetItem( P("statistics"), - _.collections.EmptyAssociativeArray(), true); - skeletonObject.SetItem( P("settings"), - _.collections.EmptyAssociativeArray(), true); + skeletonObject = _.collections.EmptyHashTable(); + skeletonObject.SetItem(P("statistics"), _.collections.EmptyHashTable()); + skeletonObject.SetItem(P("settings"), _.collections.EmptyHashTable()); + emptyObject = _.collections.EmptyHashTable(); + persistentDatabase.IncrementData(persistentSettingsPointer, emptyObject); persistentSettingsPointer.Push(userTextID); persistentDatabase.IncrementData(persistentSettingsPointer, skeletonObject); persistentSettingsPointer.Push(P("settings")); _.memory.Free(userTextID); _.memory.Free(userDataLink); _.memory.Free(skeletonObject); + _.memory.Free(emptyObject); return true; } diff --git a/sources/Users/UserAPI.uc b/sources/Users/UserAPI.uc index 5deb01e..d6d7ec6 100644 --- a/sources/Users/UserAPI.uc +++ b/sources/Users/UserAPI.uc @@ -17,9 +17,10 @@ * You should have received a copy of the GNU General Public License * along with Acedia. If not, see . */ -class UserAPI extends AcediaObject; +class UserAPI extends AcediaObject + config(AcediaSystem); -var private string userDataDBLink; +var private config string userDataDBLink; /** * Returns reference to the database of user records that Acedia was