BGC Tools
Public Member Functions | Protected Attributes | Properties | Private Attributes
BGC.Users.ProfileData Class Reference
Inheritance diagram for BGC.Users.ProfileData:
Inheritance graph
[legend]
Collaboration diagram for BGC.Users.ProfileData:
Collaboration graph
[legend]

Public Member Functions

 ProfileData (string userName)
 
virtual void Clear ()
 Clear all values and keys More...
 
void SetInt (string key, int value)
 Set value at indicated key More...
 
void SetBool (string key, bool value)
 Set value at indicated key More...
 
void SetString (string key, string value)
 Set value at indicated key More...
 
void SetFloat (string key, float value)
 Set value at indicated key More...
 
void SetDouble (string key, double value)
 Set value at indicated key More...
 
void SetJsonValue (string key, JsonValue value)
 Set value at indicated key More...
 
void SetJsonArray (string key, JsonArray value)
 Set value at indicated key More...
 
int GetInt (string key, int defaultReturn=0)
 Get value associated with indicated key More...
 
bool GetBool (string key, bool defaultReturn=false)
 Get value associated with indicated key More...
 
float GetFloat (string key, float defaultReturn=0f)
 Get value associated with indicated key More...
 
double GetDouble (string key, double defaultReturn=0.0)
 Get value associated with indicated key More...
 
string GetString (string key, string defaultReturn="")
 Get value associated with indicated key More...
 
JsonValue GetJsonValue (string key, JsonValue defaultReturn=default(JsonValue))
 Get value associated with indicated key More...
 
JsonArray GetJsonArray (string key, JsonArray defaultReturn=default(JsonArray))
 Get value associated with indicated key More...
 
bool HasKey (string key)
 Get if any value is associated with indicated key More...
 
void RemoveKey (string key)
 Remove any value is associated with indicated key More...
 
void Serialize ()
 Save contents to file More...
 
bool Deserialize ()
 Load user contents from file More...
 
virtual void DeletePlayerData ()
 

Protected Attributes

string PlayerFilePath
 Path of the user datafile More...
 

Properties

string UserName [get]
 
abstract bool IsDefault [get]
 Is this an instance of default data? More...
 

Private Attributes

JsonObject userData = new JsonObject()
 
const int userDataSerializationVersion = 1
 

Detailed Description

Definition at line 9 of file ProfileData.cs.

Constructor & Destructor Documentation

◆ ProfileData()

BGC.Users.ProfileData.ProfileData ( string  userName)
inline

Definition at line 17 of file ProfileData.cs.

18  {
19  UserName = userName;
20  }

Member Function Documentation

◆ Clear()

virtual void BGC.Users.ProfileData.Clear ( )
inlinevirtual

Clear all values and keys

Definition at line 31 of file ProfileData.cs.

References LightJson.JsonObject.Clear().

32  {
33  userData.Clear();
34  }
JsonObject Clear()
Clears the contents of this collection.
Definition: JsonObject.cs:95
Here is the call graph for this function:

◆ DeletePlayerData()

virtual void BGC.Users.ProfileData.DeletePlayerData ( )
inlinevirtual

Reimplemented in BGC.Users.DefaultData.

Definition at line 312 of file ProfileData.cs.

Referenced by BGC.Users.PlayerData.DeleteUserData().

313  {
314  if (File.Exists(PlayerFilePath))
315  {
316  File.Delete(PlayerFilePath);
317  }
318 
319  Clear();
320  }
virtual void Clear()
Clear all values and keys
Definition: ProfileData.cs:31
string PlayerFilePath
Path of the user datafile
Definition: ProfileData.cs:26
Here is the caller graph for this function:

◆ Deserialize()

bool BGC.Users.ProfileData.Deserialize ( )
inline

Load user contents from file

Definition at line 298 of file ProfileData.cs.

References LightJson.JsonObject.ContainsKey(), and BGC.IO.FileReader.ReadJsonFile().

Referenced by BGC.Users.PlayerData.DeleteUserData(), BGC.Users.PlayerData.GetMigrationData(), and BGC.Users.PlayerData.LogIn().

299  {
300  return FileReader.ReadJsonFile(
301  path: PlayerFilePath,
302  //If it is parsable, mark it as successfully loaded
303  successCallback: (JsonObject readData) =>
304  {
305  if (readData.ContainsKey("UserDicts"))
306  {
307  userData = readData["UserDicts"];
308  }
309  });
310  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
static bool ReadJsonFile(string path, Action< JsonObject > successCallback, Action failCallback=null, Action fileNotFoundCallback=null, Action< string, Exception > overrideExceptionHandling=null)
Offers two error callbacks for when a file is not found and for when a file failed to parse correctly...
Definition: FileReader.cs:24
string PlayerFilePath
Path of the user datafile
Definition: ProfileData.cs:26
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetBool()

bool BGC.Users.ProfileData.GetBool ( string  key,
bool  defaultReturn = false 
)
inline

Get value associated with indicated key

Parameters
defaultReturnThe value to return if the key is not present in dictionary

Definition at line 193 of file ProfileData.cs.

References LightJson.JsonObject.ContainsKey().

Referenced by BGC.Users.PlayerData.GetMigrationData().

194  {
195  if (userData.ContainsKey(key))
196  {
197  if (userData[key].IsBoolean)
198  {
199  return userData[key].AsBoolean;
200  }
201  else if (userData[key].IsInteger)
202  {
203  return userData[key].AsInteger != 0;
204  }
205  }
206 
207  return defaultReturn;
208  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDouble()

double BGC.Users.ProfileData.GetDouble ( string  key,
double  defaultReturn = 0.0 
)
inline

Get value associated with indicated key

Parameters
defaultReturnThe value to return if the key is not present in dictionary

Definition at line 224 of file ProfileData.cs.

References LightJson.JsonObject.ContainsKey().

225  {
226  if (userData.ContainsKey(key) && userData[key].IsNumber)
227  {
228  return userData[key].AsNumber;
229  }
230 
231  return defaultReturn;
232  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
Here is the call graph for this function:

◆ GetFloat()

float BGC.Users.ProfileData.GetFloat ( string  key,
float  defaultReturn = 0f 
)
inline

Get value associated with indicated key

Parameters
defaultReturnThe value to return if the key is not present in dictionary

Definition at line 212 of file ProfileData.cs.

References LightJson.JsonObject.ContainsKey().

213  {
214  if (userData.ContainsKey(key) && userData[key].IsNumber)
215  {
216  return (float)userData[key].AsNumber;
217  }
218 
219  return defaultReturn;
220  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
Here is the call graph for this function:

◆ GetInt()

int BGC.Users.ProfileData.GetInt ( string  key,
int  defaultReturn = 0 
)
inline

Get value associated with indicated key

Parameters
defaultReturnThe value to return if the key is not present in dictionary

Definition at line 181 of file ProfileData.cs.

References LightJson.JsonObject.ContainsKey().

182  {
183  if (userData.ContainsKey(key) && userData[key].IsInteger)
184  {
185  return userData[key].AsInteger;
186  }
187 
188  return defaultReturn;
189  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
Here is the call graph for this function:

◆ GetJsonArray()

JsonArray BGC.Users.ProfileData.GetJsonArray ( string  key,
JsonArray  defaultReturn = default(JsonArray) 
)
inline

Get value associated with indicated key

Parameters
defaultReturnThe value to return if the key is not present in dictionary

Definition at line 260 of file ProfileData.cs.

References LightJson.JsonObject.ContainsKey(), and LightJson.JsonObject.Remove().

261  {
262  if (userData.ContainsKey(key))
263  {
264  return userData[key];
265  }
266 
267  return defaultReturn;
268  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
Here is the call graph for this function:

◆ GetJsonValue()

JsonValue BGC.Users.ProfileData.GetJsonValue ( string  key,
JsonValue  defaultReturn = default(JsonValue) 
)
inline

Get value associated with indicated key

Parameters
defaultReturnThe value to return if the key is not present in dictionary

Definition at line 248 of file ProfileData.cs.

References LightJson.JsonObject.ContainsKey().

249  {
250  if (userData.ContainsKey(key))
251  {
252  return userData[key];
253  }
254 
255  return defaultReturn;
256  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
Here is the call graph for this function:

◆ GetString()

string BGC.Users.ProfileData.GetString ( string  key,
string  defaultReturn = "" 
)
inline

Get value associated with indicated key

Parameters
defaultReturnThe value to return if the key is not present in dictionary

Definition at line 236 of file ProfileData.cs.

References LightJson.JsonObject.ContainsKey().

Referenced by BGC.Users.PlayerData.GetMigrationData().

237  {
238  if (userData.ContainsKey(key) && userData[key].IsString)
239  {
240  return userData[key].AsString;
241  }
242 
243  return defaultReturn;
244  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HasKey()

bool BGC.Users.ProfileData.HasKey ( string  key)

Get if any value is associated with indicated key

◆ RemoveKey()

void BGC.Users.ProfileData.RemoveKey ( string  key)

Remove any value is associated with indicated key

◆ Serialize()

void BGC.Users.ProfileData.Serialize ( )
inline

Save contents to file

Definition at line 277 of file ProfileData.cs.

References BGC.IO.FileWriter.WriteJson().

Referenced by BGC.Users.PlayerData.AddUser().

278  {
279  //Don't serialize out blank usernames
280  if (UserName.CompareTo("") == 0)
281  {
282  Debug.LogError("Tried to serialize out a profile with a blank username.");
283  return;
284  }
285 
287  path: PlayerFilePath,
288  createJson: () => new JsonObject
289  {
290  { "Version", userDataSerializationVersion },
291  { "UserName", UserName },
292  { "UserDicts", userData }
293  },
294  pretty: true);
295  }
static bool WriteJson(string path, Func< JsonObject > createJson, bool pretty=false, Action failCallback=null, Action< string, Exception > overrideExceptionHandling=null)
Definition: FileWriter.cs:11
string PlayerFilePath
Path of the user datafile
Definition: ProfileData.cs:26
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
const int userDataSerializationVersion
Definition: ProfileData.cs:15
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetBool()

void BGC.Users.ProfileData.SetBool ( string  key,
bool  value 
)
inline

Set value at indicated key

Definition at line 59 of file ProfileData.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

60  {
61  if (userData.ContainsKey(key) == false)
62  {
63  //Data did not exist - add it
64  userData.Add(key, value);
65  }
66  else
67  {
68  //Check existing data for type match
69  if (userData[key].IsBoolean == false)
70  {
71  if (userData[key].IsInteger)
72  {
73  Debug.LogWarning($"PlayerData \"{key}\" Datatype changed from {userData[key].Type.ToString()} to Bool");
74  }
75  else
76  {
77  Debug.LogError($"PlayerData \"{key}\" Datatype changed from {userData[key].Type.ToString()} to Bool");
78  }
79  }
80 
81  //Set data
82  userData[key] = value;
83  }
84  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
JsonObject Add(string key)
Adds a key with a null value to this collection.
Here is the call graph for this function:

◆ SetDouble()

void BGC.Users.ProfileData.SetDouble ( string  key,
double  value 
)
inline

Set value at indicated key

Definition at line 129 of file ProfileData.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

130  {
131  if (userData.ContainsKey(key) == false)
132  {
133  //Data did not exist - add it
134  userData.Add(key, value);
135  }
136  else
137  {
138  //Check existing data for type match
139  if (userData[key].IsNumber == false)
140  {
141  Debug.LogError($"PlayerData \"{key}\" Datatype changed from {userData[key].Type.ToString()} to Number");
142  }
143 
144  //Set data
145  userData[key] = value;
146  }
147  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
JsonObject Add(string key)
Adds a key with a null value to this collection.
Here is the call graph for this function:

◆ SetFloat()

void BGC.Users.ProfileData.SetFloat ( string  key,
float  value 
)
inline

Set value at indicated key

Definition at line 108 of file ProfileData.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

109  {
110  if (userData.ContainsKey(key) == false)
111  {
112  //Data did not exist - add it
113  userData.Add(key, value);
114  }
115  else
116  {
117  //Check existing data for type match
118  if (userData[key].IsNumber == false)
119  {
120  Debug.LogError($"PlayerData \"{key}\" Datatype changed from {userData[key].Type.ToString()} to Number");
121  }
122 
123  //Set data
124  userData[key] = value;
125  }
126  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
JsonObject Add(string key)
Adds a key with a null value to this collection.
Here is the call graph for this function:

◆ SetInt()

void BGC.Users.ProfileData.SetInt ( string  key,
int  value 
)
inline

Set value at indicated key

Definition at line 37 of file ProfileData.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

38  {
39  if (userData.ContainsKey(key) == false)
40  {
41  //Data did not exist - add it
42  userData.Add(key, value);
43  }
44  else
45  {
46  //Check existing data for type match
47  if (userData[key].IsInteger == false)
48  {
49  Debug.LogError($"PlayerData \"{key}\" Datatype changed from {userData[key].Type.ToString()} to Int");
50  }
51 
52  //Set data
53  userData[key] = value;
54  }
55  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
JsonObject Add(string key)
Adds a key with a null value to this collection.
Here is the call graph for this function:

◆ SetJsonArray()

void BGC.Users.ProfileData.SetJsonArray ( string  key,
JsonArray  value 
)
inline

Set value at indicated key

Definition at line 165 of file ProfileData.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

166  {
167  if (userData.ContainsKey(key) == false)
168  {
169  //Data did not exist - add it
170  userData.Add(key, value);
171  }
172  else
173  {
174  //Set data
175  userData[key] = value;
176  }
177  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
JsonObject Add(string key)
Adds a key with a null value to this collection.
Here is the call graph for this function:

◆ SetJsonValue()

void BGC.Users.ProfileData.SetJsonValue ( string  key,
JsonValue  value 
)
inline

Set value at indicated key

Definition at line 150 of file ProfileData.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

151  {
152  if (userData.ContainsKey(key) == false)
153  {
154  //Data did not exist - add it
155  userData.Add(key, value);
156  }
157  else
158  {
159  //Set data
160  userData[key] = value;
161  }
162  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
JsonObject Add(string key)
Adds a key with a null value to this collection.
Here is the call graph for this function:

◆ SetString()

void BGC.Users.ProfileData.SetString ( string  key,
string  value 
)
inline

Set value at indicated key

Definition at line 87 of file ProfileData.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

88  {
89  if (userData.ContainsKey(key) == false)
90  {
91  //Data did not exist - add it
92  userData.Add(key, value);
93  }
94  else
95  {
96  //Check existing data for type match
97  if (userData[key].IsString == false)
98  {
99  Debug.LogError($"PlayerData \"{key}\" Datatype changed from {userData[key].Type.ToString()} to String");
100  }
101 
102  //Set data
103  userData[key] = value;
104  }
105  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
JsonObject Add(string key)
Adds a key with a null value to this collection.
Here is the call graph for this function:

Field Documentation

◆ PlayerFilePath

string BGC.Users.ProfileData.PlayerFilePath
protected
Initial value:
=> DataManagement.PathForDataFile(
dataDirectory: PlayerData.UserDataDir,
fileName: FileExtensions.AddJsonExtension(UserName))

Path of the user datafile

Definition at line 26 of file ProfileData.cs.

◆ userData

JsonObject BGC.Users.ProfileData.userData = new JsonObject()
private

Definition at line 13 of file ProfileData.cs.

◆ userDataSerializationVersion

const int BGC.Users.ProfileData.userDataSerializationVersion = 1
private

Definition at line 15 of file ProfileData.cs.

Property Documentation

◆ IsDefault

abstract bool BGC.Users.ProfileData.IsDefault
get

Is this an instance of default data?

Definition at line 23 of file ProfileData.cs.

◆ UserName

string BGC.Users.ProfileData.UserName
get

Definition at line 11 of file ProfileData.cs.

Referenced by BGC.Users.PlayerData.GetMigrationData().


The documentation for this class was generated from the following file: