[PostProcessBuild(0)] // First to be called after build. public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) { bool buildSuccessful = true; string buildFolder = Directory.GetParent(pathToBuiltProject).FullName; if (target == BuildTarget.StandaloneWindows || target == BuildTarget.StandaloneWindows64 || target == BuildTarget.StandaloneLinux64 || target == BuildTarget.StandaloneOSX) { try { CopyConfigFolder(buildFolder); } catch (Exception e) { buildSuccessful = false; Logger.LogException(e); } } else { Logger.LogInfo("Target is not standalone, skipping config copy.", "BuildPostProcessor"); } if (buildSuccessful) { Logger.LogInfo("Successfully postprocessed build in " + buildFolder + ".", "BuildPostProcessor"); } else { Logger.LogInfo("There was an error postprocessing the build, check the console.", "BuildPostProcessor"); } }
/// <summary> /// Download a file. /// Make sure the file exists before calling this. /// </summary> /// <param name="path">Path to the file.</param> public static void DownloadFile(string path) { Logger.LogInfo("Downloading file " + path + ".", "WebGLUtils"); byte[] array = File.ReadAllBytes(path); DownloadFile(array, array.Length, Path.GetFileName(path)); File.Delete(path); }
/// <summary> /// Sets a new config for the given type. /// </summary> /// <param name="newConfig">New config to save.</param> /// <typeparam name="T">The type of config to save.</typeparam> /// <returns>True if it saved successfully.</returns> public bool SetConfig <T>(T newConfig) where T : ConfigData { if (!IsReady) { Logger.LogError("Configuration manager has not initialized, will not update config.", this); return(false); } for (int i = 0; i < ConfigFiles.Count; ++i) { if (ConfigFiles[i] is ConfigFile <T> ) { ConfigFile <T> file = (ConfigFile <T>)ConfigFiles[i]; file.ConfigData = newConfig; file.Save(); ConfigurationUpdated?.Invoke(newConfig); return(true); } } Logger.LogError("No file found for that config type, will not update config.", this); return(false); }
/// <summary> /// SAves the game. /// </summary> public void SaveGame() { string savegameName = CurrentTimeInSaveFormat; string path = SavesPath + savegameName; Logger.LogInfo("Saving game to " + path, this); SaveGame(path); }
/// <summary> /// Operations to be performed before build. /// </summary> /// <param name="report">Report to add things to.</param> public void OnPreprocessBuild(BuildReport report) { bool buildSuccessful = GenerateVersion(); Logger.LogInfo(buildSuccessful ? "Successfully preprocessed build." : "There was an error preprocessing the build, check the console.", "BuildPreProcessor"); }
/// <summary> /// Decompress a zip file into the given path. /// </summary> /// <param name="zipPath">Path to the zip file.</param> /// <param name="targetPath">Path to decompress to.</param> /// <returns>True if it was successful.</returns> public static bool DecompressDirectory(string zipPath, string targetPath) { if (File.Exists(zipPath) || Path.GetExtension(zipPath) != "zip") { ZipFile.ExtractToDirectory(zipPath, targetPath); return(true); } Logger.LogError("File " + zipPath + " doesn't exist or it's not a zip file.", "CoreUtils"); return(false); }
/// <summary> /// Save and download the savegame. /// </summary> private static void SaveAndDownloadSaveGame(SavegameManager service) { service.SaveGame(); if (Utils.CompressDirectory(SavegameManager.LastSavePath, out string compressedPath)) { DownloadFile(compressedPath); File.Delete(compressedPath); return; } Logger.LogInfo("Error compressing savegame.", "WebGLUtils"); }
/// <summary> /// Compresses a directory into Zip. /// </summary> /// <param name="path">Directory to compress.</param> /// <param name="compressedPath">Compressed file.</param> /// <returns>True if it was successful.</returns> public static bool CompressDirectory(string path, out string compressedPath) { if (Directory.Exists(path)) { compressedPath = path + ".zip"; ZipFile.CreateFromDirectory(path, compressedPath); return(true); } Logger.LogError("Directory " + path + " doesn't exist!", "CoreUtils"); compressedPath = ""; return(false); }
/// <summary> /// Generate a new version for the game. /// </summary> /// <returns></returns> private static bool GenerateVersion() { Version version = AssetDatabase.LoadAssetAtPath <Version>(VersionPath); if (version == null) { Logger.LogError("No version found at " + VersionPath + ". Don't you want a version shipped with your game?", "BuildPreProcessor"); return(false); } int newMinorVersion = version.MinorVersion + 1; string now = CurrentDate; string newVersion = new StringBuilder(version.GameVersion.ToString()).Append(".") .Append(version.MayorVersion) .Append(".") .Append(newMinorVersion) .Append(Version .StabilityToString(version .Stability)) .Append(".") .Append(now) .ToString(); bool useNewVersion = EditorUtility.DisplayDialog("Game version", "Shifting from " + version.FullVersion + " to " + newVersion + ". NOTE: To change mayor versions, do it manually on the version asset.", "Shift version", "Leave previous minor version"); if (!useNewVersion) { return(true); } version.MinorVersion = newMinorVersion; version.Date = now; EditorUtility.SetDirty(version); AssetDatabase.SaveAssets(); Logger.LogInfo("New version asset generated.", "BuildPreProcessor"); return(true); }
/// <summary> /// Loads the latest game if any. /// </summary> public void LoadGame() { if (!SaveExists) { Logger.LogError("There is no game to load in " + SavesPath + ".", this); return; } IEnumerable <string> directories = Directory.EnumerateDirectories(SavesPath).OrderByDescending(x => x); string directory = directories.First(); Logger.LogInfo("Loading game from " + directory, this); LoadGame(directory); }
/// <summary> /// Saves the game. /// </summary> /// <param name="path">Save game path.</param> private void SaveGame(string path) { Utils.DeleteDirectory(path); Directory.CreateDirectory(path); for (int i = 0; i < Library.SavableObjects.Count; i++) { File.WriteAllText(path + "/" + i + ".savo", Library.SavableObjects[i].Save()); } CleanUpOldSaves(); #if UNITY_WEBGL && !UNITY_EDITOR Logger.LogInfo("WebGL file sync in progress.", this); WebGlUtils.SyncFiles(); #endif }
/// <summary> /// Checks if the singleton is a DontDestroyOnLoad and sets it up if it isn't. /// </summary> private static void CheckDontDestroyOnLoad() { if (instance == null) { return; } if (instance.transform.parent != null) { return; } if (instance.gameObject.IsDontDestroyOnLoad()) { return; } Logger.LogInfo(instance.name + " is not DontDestroyOnLoad so setting it up.", PrefabName); DontDestroyOnLoad(instance); }
/// <summary> /// Gets the configuration file with the given type. /// </summary> /// <typeparam name="T">The type of the configuration file we are looking for.</typeparam> /// <returns>The matching configuration file.</returns> public T GetConfig <T>() where T : ConfigData { if (!IsReady) { Logger.LogError("Configuration manager has not initialized, will return null.", this); return(null); } for (int i = 0; i < ConfigFiles.Count; ++i) { if (ConfigFiles[i] is ConfigFile <T> ) { return(((ConfigFile <T>)ConfigFiles[i]).ConfigData); } } Logger.LogError("Config file not found! Returning null.", this); return(null); }
/// <summary> /// Called when the savegame is received. /// </summary> /// <param name="file">The savegame data.</param> private static void SaveGameReceived(byte[] file) { JsCallbackReceiver.Instance.FileReceived -= SaveGameReceived; SavegameManager manager = null; SavegameManager.OnServiceReady += service => manager = service; string temporalSavePath = Application.persistentDataPath + "/TempSave.zip"; File.WriteAllBytes(temporalSavePath, file); Utils.DecompressDirectory(temporalSavePath, SavegameManager.SavesPath + SavegameManager.CurrentTimeInSaveFormat); while (manager == null) { Logger.LogInfo("Waiting for saves manager to be ready.", "WebGLUtils"); } manager.LoadGame(); File.Delete(temporalSavePath); }
/// <summary> /// Copies the configuration folder to the build if it exists. /// </summary> /// <param name="buildPath">The path of the build.</param> private static void CopyConfigFolder(string buildPath) { if (Directory.Exists(buildPath + "/Configuration")) { Utils.DeleteDirectory(buildPath + "/Configuration"); } if (Directory.Exists("Configuration")) { Logger.LogInfo("Configuration folder found, copying it to build.", "BuildPostProcessor"); } else { Logger.LogInfo("No configuration folder found, skipping config copy.", "BuildPostProcessor"); return; } Utils.CopyFilesRecursively(new DirectoryInfo("Configuration"), new DirectoryInfo(buildPath + "/Configuration")); }
/// <summary> /// Routine to update the screen resolution file. /// </summary> /// <param name="width">New width.</param> /// <param name="height">New height.</param> /// <param name="fullScreenMode">New mode.</param> private IEnumerator UpdateScreenResolutionFileRoutine(int width, int height, FullScreenMode fullScreenMode) { fileUpdating = true; while (configManager == null) { Logger.LogInfo("Waiting for config manager initialization.", this); yield return(waitForASecond); } ScreenConfiguration configData = new ScreenConfiguration { Width = width, Height = height, FullScreen = fullScreenMode }; configManager.SetConfig(configData); fileUpdating = false; }
/// <summary> /// Routine to update the screen resolution from the config file. /// </summary> private IEnumerator UpdateScreenResolutionRoutine() { #if UNITY_EDITOR || UNITY_WEBGL Logger.LogWarning("Running on a platform unaffected by the resolution changer, not updating the resolution.", this); yield break; #endif #pragma warning disable 162 // ReSharper disable HeuristicUnreachableCode #pragma warning disable 162 while (configManager == null) { Logger.LogInfo("Waiting for config manager initialization.", this); yield return(waitForASecond); } while (fileUpdating) { yield return(waitForASecond); } ScreenConfiguration config = configManager.GetConfig <ScreenConfiguration>(); UnityEngine.Screen.SetResolution(config.Width, config.Height, config.FullScreen); Logger.LogInfo("Updated screen resolution to " + config.Width + "x" + config.Height + " on " + config.FullScreen + " mode.", this); #pragma warning restore 162 ResolutionUpdated?.Invoke(UnityEngine.Screen.width, UnityEngine.Screen.height); }
/// <summary> /// Load the configuration file. /// </summary> public override void Load() { Logger.LogInfo("Trying to read configuration file from " + FullPath + ".", LoggingName); if (!File.Exists(FullPath)) { Logger.LogWarning("File " + FullPath + " not found, using default settings and generating file.", LoggingName); Save(); return; } string data = File.ReadAllText(FullPath); if (Encrypted) { data = data.FromBase64(); } ConfigData = JsonUtility.FromJson <T>(data); Logger.LogInfo("Loaded configuration " + FullPath + ".", LoggingName); }
/// <summary> /// Load the config files. /// </summary> private void OnEnable() { Logger.LogInfo("Initializing configuration manager...", this); List <Type> checkedTypes = new List <Type>(); for (int i = 0; i < ConfigFiles.Count; ++i) { if (checkedTypes.Contains(ConfigFiles[i].GetType())) { Logger.LogError("You have two configuration files of the same type added to the configuration list. " + "Only one file of each type should be created. Manager won't initialize.", this); return; } checkedTypes.Add(ConfigFiles[i].GetType()); ConfigFiles[i].Load(); } Ready(); }
/// <summary> /// Saved the data edited in the scriptable to the configuration file. /// </summary> public override void Save() { Logger.LogInfo("Trying to save configuration file to " + FullPath + ".", LoggingName); if (!Directory.Exists(Path)) { Directory.CreateDirectory(Path); } string data = JsonUtility.ToJson(ConfigData, true); if (Encrypted) { data = data.ToBase64(); } File.WriteAllText(FullPath, data); Logger.LogInfo("Saved configuration file to " + FullPath + ".", LoggingName); #if UNITY_WEBGL && !UNITY_EDITOR WebGlUtils.SyncFiles(); #endif }
/// <summary> /// Loads the scene dictionary. /// </summary> private void LoadDictionary() { sceneDictionary.Clear(); List <StringBoolPair> scenesToRemove = new List <StringBoolPair>(); for (int i = 0; i < TargetObject.SceneDictionary.Count; ++i) { SceneAsset scene = AssetDatabase.LoadAssetAtPath <SceneAsset>(TargetObject.SceneDictionary[i].Key); if (scene == null) { Logger.LogError("Library contained a path to a scene that doesn't exist, deleting it.", this); scenesToRemove.Add(TargetObject.SceneDictionary[i]); continue; } sceneDictionary.Add(new SceneAssetBoolPair(scene, TargetObject.SceneDictionary[i].Value)); } for (int i = 0; i < scenesToRemove.Count; ++i) { TargetObject.SceneDictionary.Remove(scenesToRemove[i]); } }
/// <summary> /// Allows the player to upload a file. /// </summary> /// <param name="fileType">Type of sile to upload.</param> private static void GetFileFromUserAsync(FileType fileType) { if (JsCallbackReceiver.Instance == null) { return; // Make sure the callback receiver is ready. } Logger.LogInfo("Calling Js to get file from browser.", "WebGLUtils"); GetFileFromBrowser("[Singleton] JsCallbackReceiver", "ReceiveFile", FileTypeToString(fileType), new LocalizableString { Key = "webgl_choose_file" }.LocalizedValue, new LocalizableString { Key = "webgl_cancel" }.LocalizedValue, new LocalizableString { Key = "webgl_no_file_selected" }.LocalizedValue, new LocalizableString { Key = "webgl_cancelled" }.LocalizedValue); }