using System; using System.IO; using LE_LevelEditor.Core; using LE_LevelEditor.Example; using LE_LevelEditor.UI; using LapinerTools.uMyGUI; using UnityEngine; namespace LE_LevelEditor.Extensions { public static class LE_FileSelectionHelpers { public const string POPUP_NAME = "fileselection"; public const string POPUP_LOADING = "loading"; public const string POPUP_TEXT = "text"; public static void SelectLevel(MonoBehaviour p_worker, string p_popupTitle, string p_popupText, Action p_onSelectedCallback) { LE_ExtensionInterface.FileSelectionExtensionLoader fileSelectionInstance = LE_ExtensionInterface.FileSelectionInstance; p_worker.StartCoroutine(fileSelectionInstance.LevelDB.GetLevelListAsync(delegate(LE_ExtensionInterface.FileSelectionExtensionLoader.LevelFile[] p_levelFiles) { if (uMyGUI_PopupManager.Instance != null) { string[] levelNames = LE_ExtensionInterface.FileSelectionExtensionLoader.LevelFile.GetLevelNames(p_levelFiles); string[] levelIconPaths = LE_ExtensionInterface.FileSelectionExtensionLoader.LevelFile.GetLevelIconPaths(p_levelFiles); ((LE_PopupFileSelection)uMyGUI_PopupManager.Instance.ShowPopup("fileselection")).SetFiles(levelNames, levelIconPaths, delegate(int p_selectedIndex) { p_onSelectedCallback(p_selectedIndex, p_levelFiles); }, null, true).SetText(p_popupTitle, p_popupText).ShowButton("close"); } })); } public static Texture2D DownscaleTexture(Texture2D p_tex, int p_maxHeightBeforeDownscale, int p_maxHeightAfterDownscale) { Texture2D texture2D = p_tex; if (p_tex.height > p_maxHeightBeforeDownscale) { Texture2D texture2D2 = new Texture2D(p_tex.width, p_tex.height, p_tex.format, true, true); texture2D2.SetPixels(p_tex.GetPixels()); texture2D2.Apply(true); int i; for (i = 1; (int)((float)p_tex.height / Mathf.Pow(2f, i)) > p_maxHeightAfterDownscale; i++) { } texture2D = new Texture2D((int)((float)p_tex.width / Mathf.Pow(2f, i)), (int)((float)p_tex.height / Mathf.Pow(2f, i)), texture2D2.format, false, true); texture2D.SetPixels(texture2D2.GetPixels(i)); texture2D.Apply(); UnityEngine.Object.Destroy(texture2D2); } return texture2D; } public static void SaveLevel(string p_levelDataFilePath, string p_levelIconFileName, byte[] p_levelData, byte[] p_levelMeta, int p_removedDuplicatesCount) { Action action = delegate(string p_infoText) { if (uMyGUI_PopupManager.Instance != null) { if (p_removedDuplicatesCount > 0) { string text = p_infoText; p_infoText = text + "\n'" + p_removedDuplicatesCount + "' duplicate object(s) removed before saving\n(duplicate = same: object, position, rotation, scale)."; } ((uMyGUI_PopupText)uMyGUI_PopupManager.Instance.ShowPopup("text")).SetText("Level Saved", p_infoText).ShowButton("ok"); } }; LE_SaveLoad.LevelMetaData levelMetaData = LE_SaveLoad.LoadLevelMetaFromByteArray(p_levelMeta, true); if (levelMetaData.Icon != null) { Texture2D texture2D = DownscaleTexture(levelMetaData.Icon, 255, 128); string p_filePath = Path.Combine(FisheryEditor_LoadSave.GetSaveFolderPath(), p_levelIconFileName); SaveToFile(p_filePath, texture2D.EncodeToPNG()); UnityEngine.Object.Destroy(levelMetaData.Icon); UnityEngine.Object.Destroy(texture2D); } action(ExampleGame_LoadSave.SaveByFilePath(p_levelDataFilePath, p_levelData, p_levelMeta)); } public static void SaveToFile(string p_filePath, byte[] p_fileContents) { using (FileStream fileStream = File.Open(p_filePath, FileMode.Create)) { fileStream.Write(p_fileContents, 0, p_fileContents.Length); } } } }