public void Save() { string[] keys = new string[cachedStoredData.Keys.Count]; cachedStoredData.Keys.CopyTo(keys, 0); byte[][] values = new byte[cachedStoredData.Values.Count][]; cachedStoredData.Values.CopyTo(values, 0); UserPersistanceStorage storage = new UserPersistanceStorage(keys, values); if (!HasStoredData()) { Directory.CreateDirectory(GetAppPath()); } string path = GetSavePath(loadedUserId); FileStream stream = new FileStream(path, FileMode.Create); formatter.Serialize(stream, storage); stream.Close(); }
public void LoadUser(string userId) { if (userId == null || string.Empty.CompareTo(userId) == 0) { Debug.LogError("Trying to load Empty User, fallbacking to defaultUser"); userId = DEFAULT_USER; } string path = GetSavePath(userId); if (!File.Exists(path)) { Debug.Log("No Save file found in " + path); ClearData(userId, false); return; } FileStream stream = new FileStream(path, FileMode.Open); UserPersistanceStorage storage = formatter.Deserialize(stream) as UserPersistanceStorage; stream.Close(); if (storage == null) { Debug.LogError("Invalid format stored in path " + path); ClearData(userId, false); return; } cachedStoredData.Clear(); for (int i = 0; i < storage.keys.Length; i++) { cachedStoredData.Add(storage.keys[i], storage.values[i]); } loadedUserId = userId; hasLoaded = true; OnUserPersistanceReloaded?.Invoke(); }