Пример #1
0
        /// <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);
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <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);
            }
Пример #4
0
        /// <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);
            }
        }
Пример #5
0
        /// <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();
            }
        }
Пример #6
0
        /// <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();
            }
        }
Пример #7
0
        // --- 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);
        }
Пример #8
0
        /// <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);
        }
Пример #9
0
        /// <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);
            }
        }
Пример #10
0
        /// <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);
            }
        }
Пример #11
0
        /// <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);
        }
Пример #12
0
        /// <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);
        }
Пример #13
0
        /// <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);
        }
Пример #14
0
        // ---------[ 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
        }
Пример #15
0
        // ---------[ 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.");
        }
Пример #16
0
 /// <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"));
 }
Пример #17
0
 /// <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"));
 }
Пример #18
0
 /// <summary>Deletes a user's profile from the cache.</summary>
 public static bool DeleteUserProfile(int userId)
 {
     return(IOUtilities.DeleteFile(CacheClient.GenerateUserProfileFilePath(userId)));
 }
Пример #19
0
 /// <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)));
 }
Пример #20
0
 /// <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"));
 }
Пример #21
0
 // ------[ 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"));
 }
Пример #22
0
 /// <summary>Deletes a mod team's data from the cache.</summary>
 public static bool DeleteModTeam(int modId)
 {
     return(IOUtilities.DeleteFile(CacheClient.GenerateModTeamFilePath(modId)));
 }
Пример #23
0
 // ---------[ 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);
 }
Пример #26
0
 /// <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));
 }
Пример #27
0
 /// <summary>Delete's a user's avatars from the cache.</summary>
 public static bool DeleteUserAvatar(int userId)
 {
     return(IOUtilities.DeleteDirectory(CacheClient.GenerateUserAvatarDirectoryPath(userId)));
 }
Пример #28
0
 /// <summary>Generates the directory path for the cached mod media.</summary>
 public static string GenerateModMediaDirectoryPath(int modId)
 {
     return(IOUtilities.CombinePath(GenerateModDirectoryPath(modId),
                                    "mod_media"));
 }
Пример #29
0
 /// <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"));
 }
Пример #30
0
 /// <summary>Retrieves the game's profile from the cache.</summary>
 public static GameProfile LoadGameProfile()
 {
     return(IOUtilities.ReadJsonObjectFile <GameProfile>(gameProfileFilePath));
 }