/// <summary>Saves the LocalUser instance.</summary> public static void Save(System.Action callback = null) { UserDataStorage.WriteJSONFile(LocalUser.FILENAME, LocalUser._instance, (path, success) => { if (callback != null) { callback.Invoke(); } }); }
/// <summary>Function used to read a user data file.</summary> public static void TryWriteJSONFile <T>(string filePathRelative, T jsonObject, WriteFileCallback callback) { Debug.Assert(UserDataStorage.isInitialized); Debug.Assert(!string.IsNullOrEmpty(filePathRelative)); byte[] fileData = null; if (UserDataStorage.TryGenerateJSONFile(jsonObject, out fileData)) { UserDataStorage.WriteFile(filePathRelative, fileData, callback); } else if (callback != null) { callback.Invoke(false); } }
/// <summary>Saves the LocalUser instance.</summary> public static void Save(System.Action callback = null) { Debug.Assert(UserDataStorage.isInitialized, "[mod.io] UserDataStorage is not yet intialized. Please call" + " UserDataStorage.InitializeForUser() before attempting to" + " save the LocalUser to disk."); UserDataStorage.TryWriteJSONFile(LocalUser.FILENAME, LocalUser._instance, (success) => { if (callback != null) { callback.Invoke(); } }); }
// ---------[ Data I/O ]--------- /// <summary>Loads the LocalUser instance.</summary> public static void Load(System.Action callback = null) { LocalUser.isLoaded = false; UserDataStorage.ReadJSONFile <LocalUser>(LocalUser.FILENAME, (path, success, fileData) => { LocalUser.AssertListsNotNull(ref fileData); LocalUser._instance = fileData; LocalUser.isLoaded = success; if (callback != null) { callback.Invoke(); } }); }
/// <summary>Function used to read a user data file.</summary> public static void ReadJSONFile <T>(string relativePath, ReadJSONFileCallback <T> callback) { UserDataStorage.ReadFile(relativePath, (p, success, fileData) => { T jsonObject; if (success) { success = IOUtilities.TryParseUTF8JSONData(fileData, out jsonObject); } else { jsonObject = default(T); } callback.Invoke(relativePath, success, jsonObject); }); }
/// <summary>Function used to read a user data file.</summary> public static void WriteJSONFile <T>(string relativePath, T jsonObject, WriteFileCallback callback) { byte[] data = IOUtilities.GenerateUTF8JSONData <T>(jsonObject); if (data != null) { UserDataStorage.WriteFile(relativePath, data, callback); } else { Debug.LogWarning("[mod.io] Failed create JSON representation of object before writing file." + "\nFile: " + relativePath + "\n\n"); if (callback != null) { callback.Invoke(relativePath, false); } } }
// ---------[ INITIALIZATION ]--------- /// <summary>Loads the platform I/O behaviour.</summary> static UserDataStorage() { // Select the platform appropriate functions #if UNITY_EDITOR && !DISABLE_EDITOR_CODEPATH UserDataStorage.PLATFORM = UserDataStorage.GetPlatformFunctions_Editor(); #elif MODIO_FACEPUNCH_SUPPORT UserDataStorage.PLATFORM = UserDataStorage.GetPlatformFunctions_Facepunch(); #elif MODIO_STEAMWORKSNET_SUPPORT UserDataStorage.PLATFORM = UserDataStorage.GetPlatformFunctions_SteamworksNET(); #else UserDataStorage.PLATFORM = UserDataStorage.GetPlatformFunctions_Standalone(); #endif Debug.Assert(UserDataStorage.PLATFORM.InitializeWithInt != null); Debug.Assert(UserDataStorage.PLATFORM.InitializeWithString != null); Debug.Assert(UserDataStorage.PLATFORM.ReadFile != null); Debug.Assert(UserDataStorage.PLATFORM.WriteFile != null); Debug.Assert(UserDataStorage.PLATFORM.DeleteFile != null); Debug.Assert(UserDataStorage.PLATFORM.ClearAllData != null); }
// ---------[ IO FUNCTIONS ]--------- /// <summary>Function used to read a user data file.</summary> public static void TryReadJSONFile <T>(string filePathRelative, ReadJsonFileCallback <T> callback) { Debug.Assert(UserDataStorage.isInitialized); Debug.Assert(!string.IsNullOrEmpty(filePathRelative)); Debug.Assert(callback != null); UserDataStorage.ReadFile(filePathRelative, (success, fileData) => { T jsonObject; if (success) { success = UserDataStorage.TryParseJSONFile(fileData, out jsonObject); } else { jsonObject = default(T); } callback(success, jsonObject); }); }
// ---------[ Data I/O ]--------- /// <summary>Loads the LocalUser instance.</summary> public static void Load(System.Action callback = null) { Debug.Assert(UserDataStorage.isInitialized, "[mod.io] UserDataStorage is not yet intialized. Please call" + " UserDataStorage.InitializeForUser() before attempting to" + " load the LocalUser from disk."); LocalUser.isLoaded = false; UserDataStorage.TryReadJSONFile <LocalUser>(LocalUser.FILENAME, (success, fileData) => { LocalUser.AssertListsNotNull(ref fileData); LocalUser._instance = fileData; LocalUser.isLoaded = success; if (callback != null) { callback.Invoke(); } }); }
// ---------[ 2019 ]--------- /// <summary>Moves the data from the UserAuthenticationData and ModManager caches to UserAccountManagement.</summary> private static void Update_2_0_to_2_1_UserData() { Debug.Log("[mod.io] Attempting 2.0->2.1 UserData update."); // check if the file already exists byte[] fileData = null; UserDataStorage.InitializeForUser(null, () => {}); UserDataStorage.ReadFile(LocalUser.FILENAME, (success, data) => fileData = data); if (fileData != null && fileData.Length > 0) { Debug.Log("[mod.io] Aborting UserData update. FileExists: \'" + LocalUser.FILENAME + "\' [" + ValueFormatting.ByteCount(fileData.Length, null) + "]"); } // update GenericJSONObject dataWrapper; LocalUser userData = new LocalUser(); string filePath = null; // - copy enabled/subbed - filePath = ModManager.PERSISTENTDATA_FILEPATH; if (IOUtilities.TryReadJsonObjectFile(filePath, out dataWrapper)) { int[] modIds = null; if (DataUpdater.TryGetArrayField(dataWrapper, "subscribedModIds", out modIds)) { userData.subscribedModIds = new List <int>(modIds); } if (DataUpdater.TryGetArrayField(dataWrapper, "enabledModIds", out modIds)) { userData.enabledModIds = new List <int>(modIds); } } // - copy queued subs/unsubs - filePath = IOUtilities.CombinePath(CacheClient.cacheDirectory, ModIO.UI.ModBrowser.MANIFEST_FILENAME); if (IOUtilities.TryReadJsonObjectFile(filePath, out dataWrapper)) { List <int> modIds = null; if (DataUpdater.TryGetArrayField(dataWrapper, "queuedSubscribes", out modIds)) { userData.queuedSubscribes = new List <int>(modIds); } if (DataUpdater.TryGetArrayField(dataWrapper, "queuedUnsubscribes", out modIds)) { userData.queuedUnsubscribes = new List <int>(modIds); } } // - copy UAD - filePath = UserAuthenticationData.FILE_LOCATION; if (IOUtilities.TryReadJsonObjectFile(filePath, out dataWrapper)) { // user profile int userId = UserProfile.NULL_ID; if (dataWrapper.data.ContainsKey("userId")) { userId = (int)dataWrapper.data["userId"]; } userData.profile = null; if (userId != UserProfile.NULL_ID) { userData.profile = CacheClient.LoadUserProfile(userId); } // token data if (dataWrapper.data.ContainsKey("token")) { userData.oAuthToken = (string)dataWrapper.data["token"]; } if (dataWrapper.data.ContainsKey("wasTokenRejected")) { userData.wasTokenRejected = (bool)dataWrapper.data["wasTokenRejected"]; } // NOTE(@jackson): External Authentication is no longer saved to disk and is thus ignored. IOUtilities.DeleteFile(filePath); } // - set and save - LocalUser.instance = userData; LocalUser.isLoaded = true; LocalUser.Save(); Debug.Log("[mod.io] UserData updated completed."); }
/// <summary>Initializes the data storage system for a given user. (Standalone Application)</summary> public static void InitializeForUser_Standalone(int platformUserIdentifier, InitializationCallback callback) { UserDataStorage.InitializeForUser_Standalone(platformUserIdentifier.ToString("x8"), callback); }