/// <summary>Iterates through all of the mod profiles from the given offset.</summary> public static IEnumerable <ModProfile> IterateAllModProfilesFromOffset(int offset) { string profileDirectory = IOUtilities.CombinePath(CacheClient.cacheDirectory, "mods"); if (Directory.Exists(profileDirectory)) { string[] modDirectories; try { modDirectories = Directory.GetDirectories(profileDirectory); } catch (Exception e) { string warningInfo = ("[mod.io] Failed to read mod profile directory." + "\nDirectory: " + profileDirectory + "\n\n"); Debug.LogWarning(warningInfo + Utility.GenerateExceptionDebugString(e)); modDirectories = new string[0]; } int offsetDirCount = modDirectories.Length - offset; if (offsetDirCount > 0) { string[] offsetModDirectories = new string[offsetDirCount]; Array.Copy(modDirectories, offset, offsetModDirectories, 0, offsetDirCount); foreach (string modDirectory in offsetModDirectories) { string profilePath = IOUtilities.CombinePath(modDirectory, "profile.data"); ModProfile profile = IOUtilities.ReadJsonObjectFile <ModProfile>(profilePath); if (profile != null) { yield return(profile); } } } } }
/// <summary>Write a user file. (Unity Editor)</summary> public static void WriteFile_Editor(string filePath, byte[] data, WriteFileCallback callback) { Debug.Assert(!string.IsNullOrEmpty(filePath)); Debug.Assert(data != null); bool fileExisted = System.IO.File.Exists(filePath); bool success = false; success = IOUtilities.WriteBinaryFile(filePath, data); if (success && !fileExisted) { UnityEditor.AssetDatabase.Refresh(); } if (callback != null) { callback.Invoke(success); } }
private string ReplaceKeywords(string input) { if (input != null) { string[] cacheDirParts = input.Split(System.IO.Path.AltDirectorySeparatorChar, System.IO.Path.DirectorySeparatorChar); for (int i = 0; i < cacheDirParts.Length; ++i) { if (cacheDirParts[i].ToUpper().Equals("$PERSISTENT_DATA_PATH$")) { cacheDirParts[i] = Application.persistentDataPath; } cacheDirParts[i] = cacheDirParts[i].Replace("$GAME_ID$", gameId.ToString()); } input = IOUtilities.CombinePath(cacheDirParts); } return(input); }
/// <summary>Retrieves a YouTube thumbnail from the cache.</summary> public static Texture2D LoadModYouTubeThumbnail(int modId, string youTubeId) { Debug.Assert(!String.IsNullOrEmpty(youTubeId)); string thumbnailFilePath = CacheClient.GenerateModYouTubeThumbnailFilePath(modId, youTubeId); byte[] imageData; if (LocalDataStorage.ReadFile(thumbnailFilePath, out imageData) && imageData != null) { return(IOUtilities.ParseImageData(imageData)); } else { return(null); } }
/// <summary>Initializes the data storage system for a given user. (Steamworks.NET)</summary> public static void InitializeForUser_SteamworksNET(string platformUserIdentifier, InitializationCallback callback) { string userDir = UserDataStorage.STEAMWORKSNET_USER_DIRECTORY; if (!string.IsNullOrEmpty(platformUserIdentifier)) { string folderName = IOUtilities.MakeValidFileName(platformUserIdentifier); userDir = IOUtilities.CombinePath(STEAMWORKSNET_USER_DIRECTORY, folderName); } UserDataStorage.activeUserDirectory = userDir; UserDataStorage.isInitialized = true; Debug.Log("[mod.io] Steam User Data Directory set: " + UserDataStorage.activeUserDirectory); if (callback != null) { callback.Invoke(); } }
/// <summary>Initializes the data storage system for a given user. (Standalone Application)</summary> public static void InitializeForUser_Standalone(string platformUserIdentifier, InitializationCallback callback) { string userDir = UserDataStorage.STANDALONE_USERS_FOLDER; if (!string.IsNullOrEmpty(platformUserIdentifier)) { string folderName = IOUtilities.MakeValidFileName(platformUserIdentifier); userDir = IOUtilities.CombinePath(STANDALONE_USERS_FOLDER, folderName); } UserDataStorage.activeUserDirectory = userDir; UserDataStorage.isInitialized = true; Debug.Log("[mod.io] User Data Directory set: " + UserDataStorage.activeUserDirectory); if (callback != null) { callback.Invoke(); } }
// --- File I/O --- /// <summary>Reads a file.</summary> public void ReadFile(string relativePath, ReadFileCallback callback) { Debug.Assert(!string.IsNullOrEmpty(relativePath)); Debug.Assert(callback != null); string path = IOUtilities.CombinePath(this.userDir, relativePath); byte[] data = null; if (Steamworks.SteamRemoteStorage.FileExists(path)) { int fileSize = Steamworks.SteamRemoteStorage.GetFileSize(path); if (fileSize > 0) { data = new byte[fileSize]; Steamworks.SteamRemoteStorage.FileRead(path, data, fileSize); } } callback.Invoke(relativePath, (data != null), data); }
/// <summary>Sets/saves the settings for the runtime instance.</summary> public static PluginSettings SaveToAsset(string path, PluginSettings.Data data) { string assetPath = IOUtilities.CombinePath("Assets", "Resources", path + ".asset"); // creates the containing folder string assetFolder = Path.GetDirectoryName(assetPath); System.IO.Directory.CreateDirectory(assetFolder); // create asset PluginSettings settings = ScriptableObject.CreateInstance <PluginSettings>(); settings.m_data = data; // save UnityEditor.AssetDatabase.CreateAsset(settings, assetPath); UnityEditor.AssetDatabase.SaveAssets(); UnityEditor.AssetDatabase.Refresh(); return(settings); }
/// <summary>Retrieves a mod gallery image from the cache.</summary> public static Texture2D LoadModGalleryImage(int modId, string imageFileName, ModGalleryImageSize size) { Debug.Assert(!String.IsNullOrEmpty(imageFileName)); string imageFilePath = CacheClient.GenerateModGalleryImageFilePath(modId, imageFileName, size); byte[] imageData; if (LocalDataStorage.ReadFile(imageFilePath, out imageData) && imageData != null) { return(IOUtilities.ParseImageData(imageData)); } else { return(null); } }
/// <summary>Delete a user file. (Unity Editor)</summary> public static void DeleteFile_Editor(string filePath, WriteFileCallback callback) { Debug.Assert(!string.IsNullOrEmpty(filePath)); bool fileExisted = System.IO.File.Exists(filePath); bool success = true; if (fileExisted) { success = IOUtilities.DeleteFile(filePath); if (success) { UnityEditor.AssetDatabase.Refresh(); } } if (callback != null) { callback.Invoke(success); } }
/// <summary>Loads the image data from a file into a new Texture.</summary> public static bool TryReadImageFile(string filePath, out Texture2D texture) { Debug.Assert(!String.IsNullOrEmpty(filePath)); if (File.Exists(filePath)) { byte[] imageData; bool readSuccessful = IOUtilities.TryLoadBinaryFile(filePath, out imageData); if (readSuccessful && imageData != null && imageData.Length > 0) { texture = new Texture2D(0, 0); texture.LoadImage(imageData); return(true); } } texture = null; return(false); }
/// <summary>Stores a mod logo in the cache with the given fileName.</summary> public static bool SaveModLogo(int modId, string fileName, LogoSize size, Texture2D logoTexture) { Debug.Assert(!String.IsNullOrEmpty(fileName)); Debug.Assert(logoTexture != null); string logoFilePath = CacheClient.GenerateModLogoFilePath(modId, size); bool isSuccessful = IOUtilities.WritePNGFile(logoFilePath, logoTexture); // - Update the versioning info - var versionInfo = CacheClient.GetModLogoVersionFileNames(modId); if (versionInfo == null) { versionInfo = new Dictionary <LogoSize, string>(); } versionInfo[size] = fileName; isSuccessful = (IOUtilities.WriteJsonObjectFile(GenerateModLogoVersionInfoFilePath(modId), versionInfo) && isSuccessful); return(isSuccessful); }
/// <summary>Replaces variables in the directory values.</summary> public static string ReplaceDirectoryVariables(string directory, int gameId) { // remove any trailing DSCs from Application paths string app_persistentDataPath = Application.persistentDataPath; if (IOUtilities.PathEndsWithDirectorySeparator(app_persistentDataPath)) { app_persistentDataPath = app_persistentDataPath.Remove(app_persistentDataPath.Length - 1); } string app_dataPath = Application.dataPath; if (IOUtilities.PathEndsWithDirectorySeparator(app_dataPath)) { app_dataPath = app_dataPath.Remove(app_dataPath.Length - 1); } string app_temporaryCachePath = Application.temporaryCachePath; if (IOUtilities.PathEndsWithDirectorySeparator(app_temporaryCachePath)) { app_temporaryCachePath = app_temporaryCachePath.Remove(app_temporaryCachePath.Length - 1); } // straight replaces directory = (directory .Replace("$PERSISTENT_DATA_PATH$", app_persistentDataPath) .Replace("$DATA_PATH$", app_dataPath) .Replace("$TEMPORARY_CACHE_PATH$", app_temporaryCachePath) .Replace("$BUILD_GUID$", Application.buildGUID) .Replace("$COMPANY_NAME$", Application.companyName) .Replace("$PRODUCT_NAME$", Application.productName) .Replace("$APPLICATION_IDENTIFIER", Application.identifier) .Replace("$GAME_ID$", gameId.ToString()) .Replace("$CURRENT_DIRECTORY$", System.IO.Directory.GetCurrentDirectory()) ); return(directory); }
// ---------[ Initialization ]--------- /// <summary>Loads the platform I/O behaviour.</summary> static DataStorage() { // Selects the platform appropriate functions #if UNITY_EDITOR DataStorage.PLATFORM_IO = new SystemIOWrapper_Editor(); #else DataStorage.PLATFORM_IO = new SystemIOWrapper(); #endif #if DEBUG // NOTE(@jackson): Due to hardcoded directory names the following configuration of // directories causes errors during the mod installation process. const string modCacheDir = "mods"; string cacheDirNoSep = DataStorage.PLATFORM_IO.CacheDirectory; if (IOUtilities.PathEndsWithDirectorySeparator(cacheDirNoSep)) { cacheDirNoSep = cacheDirNoSep.Substring(0, cacheDirNoSep.Length - 1); } string installDirNoSep = DataStorage.PLATFORM_IO.InstallationDirectory; if (IOUtilities.PathEndsWithDirectorySeparator(installDirNoSep)) { installDirNoSep = installDirNoSep.Substring(0, installDirNoSep.Length - 1); } if (System.IO.Path.GetDirectoryName(installDirNoSep) == cacheDirNoSep && installDirNoSep.Substring(cacheDirNoSep.Length + 1) == modCacheDir) { Debug.LogError("[mod.io] The installation directory cannot be a directory named" + " 'mods' and a child of the cache directory as this will cause" + " issues during the installation process." + "\nPlease change the values in your PluginSettings."); } #endif }
// ---------[ 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>Generates the file path for a mod logo.</summary> public static string GenerateModLogoFilePath(int modId, LogoSize size) { return(IOUtilities.CombinePath(GenerateModLogoCollectionDirectoryPath(modId), size.ToString() + ".png")); }
/// <summary>Generates the file path for a mod logo's cached version information.</summary> public static string GenerateModLogoVersionInfoFilePath(int modId) { return(IOUtilities.CombinePath(CacheClient.GenerateModLogoCollectionDirectoryPath(modId), "versionInfo.data")); }
/// <summary>Deletes a user's profile from the cache.</summary> public static bool DeleteUserProfile(int userId) { return(IOUtilities.DeleteFile(CacheClient.GenerateUserProfileFilePath(userId))); }
/// <summary>Retrieves the file paths for the mod logos in the cache.</summary> public static Dictionary <LogoSize, string> GetModLogoVersionFileNames(int modId) { return(IOUtilities.ReadJsonObjectFile <Dictionary <LogoSize, string> >(CacheClient.GenerateModLogoVersionInfoFilePath(modId))); }
/// <summary>Generates the file path for a user's profile.</summary> public static string GenerateUserAvatarDirectoryPath(int userId) { return(IOUtilities.CombinePath(CacheClient.cacheDirectory, "users", userId + "_avatar")); }
// ------[ PROFILES ]------ /// <summary>Generates the file path for a mod's profile data.</summary> public static string GenerateModProfileFilePath(int modId) { return(IOUtilities.CombinePath(CacheClient.GenerateModDirectoryPath(modId), "profile.data")); }
/// <summary>Deletes a mod team's data from the cache.</summary> public static bool DeleteModTeam(int modId) { return(IOUtilities.DeleteFile(CacheClient.GenerateModTeamFilePath(modId))); }
// ---------[ MODS ]--------- /// <summary>Generates the path for a mod cache directory.</summary> public static string GenerateModDirectoryPath(int modId) { return(IOUtilities.CombinePath(CacheClient.cacheDirectory, "mods", modId.ToString())); }
// ---------[ SAVE/LOAD ]--------- /// <summary>Writes the UserAuthenticationData to disk.</summary> private static void SaveInstance() { IOUtilities.WriteJsonObjectFile(FILE_LOCATION, UserAuthenticationData.m_instance); }
/// <summary>Clears the instance and deletes the data on disk.</summary> public static void Clear() { UserAuthenticationData.m_instance = UserAuthenticationData.NONE; IOUtilities.DeleteFile(UserAuthenticationData.FILE_LOCATION); }
/// <summary>Stores a mod's profile in the cache.</summary> public static bool SaveModProfile(ModProfile profile) { Debug.Assert(profile != null); return(IOUtilities.WriteJsonObjectFile(GenerateModProfileFilePath(profile.id), profile)); }
/// <summary>Delete's a user's avatars from the cache.</summary> public static bool DeleteUserAvatar(int userId) { return(IOUtilities.DeleteDirectory(CacheClient.GenerateUserAvatarDirectoryPath(userId))); }
/// <summary>Generates the directory path for the cached mod media.</summary> public static string GenerateModMediaDirectoryPath(int modId) { return(IOUtilities.CombinePath(GenerateModDirectoryPath(modId), "mod_media")); }
/// <summary>Generates the file path for a user's profile.</summary> public static string GenerateUserAvatarFilePath(int userId, UserAvatarSize size) { return(IOUtilities.CombinePath(CacheClient.GenerateUserAvatarDirectoryPath(userId), size.ToString() + ".png")); }
/// <summary>Retrieves the game's profile from the cache.</summary> public static GameProfile LoadGameProfile() { return(IOUtilities.ReadJsonObjectFile <GameProfile>(gameProfileFilePath)); }