Browse Source

Fix persistent data saving/loading

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

52
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;
}

5
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 <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

Loading…
Cancel
Save