using System; using System.Collections; using System.IO; using LE_LevelEditor.Extensions; using LapinerTools.uMyGUI; using MyUtility; using UnityEngine; namespace LE_LevelEditor.Example { public static class ExampleGame_LoadSave { private const string LEVEL_FILE_NAME = "level.txt"; public static void Init() { LE_ExtensionInterface.Load.SetDelegate(1, delegate(object p_sender, Action p_onLoadedCallback, bool p_isReload) { if (uMyGUI_PopupManager.Instance != null) { uMyGUI_PopupManager.Instance.ShowPopup("loading"); } ((MonoBehaviour)p_sender).StartCoroutine(LoadRoutineByFileName("level.txt", p_onLoadedCallback)); }); LE_ExtensionInterface.Save.SetDelegate(1, delegate(object p_sender, byte[] p_levelData, byte[] p_levelMeta, int p_removedDuplicatesCount) { string text = SaveByFileName("level.txt", p_levelData, p_levelMeta); if (uMyGUI_PopupManager.Instance != null) { if (p_removedDuplicatesCount > 0) { string text2 = text; text = text2 + "\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", text).ShowButton("ok"); } }); } public static string SaveByFileName(string p_relativeFileName, byte[] p_data, byte[] p_meta) { return SaveByFilePath(Path.Combine(Application.persistentDataPath, p_relativeFileName), p_data, p_meta); } public static string SaveByFilePath(string p_filePath, byte[] p_data, byte[] p_meta) { string p_fileContentAsString = Convert.ToBase64String(p_data) + "#" + Convert.ToBase64String(p_meta); string text = "Level size: '" + ((float)(p_data.Length + p_meta.Length) / 1048576f).ToString("0.00") + "' MB\n"; UtilityPlatformIO.SaveToFile(p_filePath, p_fileContentAsString); return "Level saved to '" + p_filePath + "'.\n" + text; } public static IEnumerator LoadRoutineByFileName(string p_relativeFileName, Action p_onLoaded) { return LoadRoutineByFilePath(Path.Combine(Application.persistentDataPath, p_relativeFileName), p_onLoaded); } public static IEnumerator LoadRoutineByFilePath(string p_filePath, Action p_onLoaded) { WWW www = new WWW(UtilityPlatformIO.FixFilePath(p_filePath)); yield return www; string savedLevel; if (string.IsNullOrEmpty(www.error)) { savedLevel = www.text; } else { Debug.LogError("ExampleGame_LoadSave: LoadRoutine: could load file '" + www.url + "'! Error:\n" + www.error); savedLevel = null; } LoadFromStr(savedLevel, p_onLoaded); } public static void LoadFromStr(string p_savedLevelStr, Action p_onLoaded) { if (!string.IsNullOrEmpty(p_savedLevelStr)) { string[] array = p_savedLevelStr.Split('#'); byte[][] obj; try { obj = new byte[2][] { Convert.FromBase64String(array[0]), Convert.FromBase64String(array[1]) }; } catch (Exception ex) { Debug.LogError("ExampleGame_LoadSave: LoadRoutine: unknown format! Error: " + ex.Message); obj = null; } p_onLoaded(obj); } else { p_onLoaded(null); } } public static bool IsLevelFileFound(string p_fileName) { return File.Exists(Application.persistentDataPath + "/" + p_fileName); } } }