Browse Source

Fix persistent data saving/loading

pull/8/head
Anton Tarasenko 2 years ago
parent
commit
8cfd6b4a06
  1. 46
      sources/Users/User.uc
  2. 5
      sources/Users/UserAPI.uc

46
sources/Users/User.uc

@ -69,6 +69,34 @@ public final function int GetKey()
return key; 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 * Reads user's persistent data saved under name `dataName`, saving it into
* a collection using mutable data types. * a collection using mutable data types.
@ -128,12 +156,12 @@ public final function DBWriteTask WritePersistentData(
AcediaObject data) AcediaObject data)
{ {
local DBWriteTask task; local DBWriteTask task;
local AssociativeArray emptyObject; local HashTable emptyObject;
if (groupName == none) return none; if (groupName == none) return none;
if (dataName == none) return none; if (dataName == none) return none;
if (!SetupDatabaseVariables()) return none; if (!SetupDatabaseVariables()) return none;
emptyObject = _.collections.EmptyAssociativeArray(); emptyObject = _.collections.EmptyHashTable();
persistentSettingsPointer.Push(groupName); persistentSettingsPointer.Push(groupName);
persistentDatabase.IncrementData(persistentSettingsPointer, emptyObject); persistentDatabase.IncrementData(persistentSettingsPointer, emptyObject);
persistentSettingsPointer.Push(dataName); persistentSettingsPointer.Push(dataName);
@ -152,7 +180,8 @@ private function bool SetupDatabaseVariables()
{ {
local Text userDataLink; local Text userDataLink;
local Text userTextID; local Text userTextID;
local AssociativeArray skeletonObject; local HashTable emptyObject, skeletonObject;
if ( persistentDatabase != none && persistentSettingsPointer != none if ( persistentDatabase != none && persistentSettingsPointer != none
&& persistentDatabase.IsAllocated()) && persistentDatabase.IsAllocated())
{ {
@ -171,17 +200,18 @@ private function bool SetupDatabaseVariables()
} }
persistentSettingsPointer = _.db.GetPointer(userDataLink); persistentSettingsPointer = _.db.GetPointer(userDataLink);
userTextID = id.GetSteamID64String(); userTextID = id.GetSteamID64String();
skeletonObject = _.collections.EmptyAssociativeArray(); skeletonObject = _.collections.EmptyHashTable();
skeletonObject.SetItem( P("statistics"), skeletonObject.SetItem(P("statistics"), _.collections.EmptyHashTable());
_.collections.EmptyAssociativeArray(), true); skeletonObject.SetItem(P("settings"), _.collections.EmptyHashTable());
skeletonObject.SetItem( P("settings"), emptyObject = _.collections.EmptyHashTable();
_.collections.EmptyAssociativeArray(), true); persistentDatabase.IncrementData(persistentSettingsPointer, emptyObject);
persistentSettingsPointer.Push(userTextID); persistentSettingsPointer.Push(userTextID);
persistentDatabase.IncrementData(persistentSettingsPointer, skeletonObject); persistentDatabase.IncrementData(persistentSettingsPointer, skeletonObject);
persistentSettingsPointer.Push(P("settings")); persistentSettingsPointer.Push(P("settings"));
_.memory.Free(userTextID); _.memory.Free(userTextID);
_.memory.Free(userDataLink); _.memory.Free(userDataLink);
_.memory.Free(skeletonObject); _.memory.Free(skeletonObject);
_.memory.Free(emptyObject);
return true; return true;
} }

5
sources/Users/UserAPI.uc

@ -17,9 +17,10 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>. * along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/ */
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 * Returns reference to the database of user records that Acedia was

Loading…
Cancel
Save