Files
UltimateFishing/Assets/Scripts/Assembly-CSharp/FisheryEditor_LoadSave.cs
2026-02-21 16:45:37 +08:00

107 lines
3.0 KiB
C#

using System;
using System.Collections;
using System.IO;
using LE_LevelEditor.Extensions;
using LapinerTools.uMyGUI;
using MyUtility;
using UnityEngine;
public static class FisheryEditor_LoadSave
{
public const string SAVE_FOLDER = "EDITOR_LEVELS";
public const string LEVEL_FILE_NAME = "level.txt";
private const string POPUP_LOADING = "loading";
private const string POPUP_TEXT = "text";
public static string GetSaveFolderPath()
{
string text = Application.persistentDataPath + "/EDITOR_LEVELS";
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
return text;
}
public static void Init()
{
LE_ExtensionInterface.Load.SetDelegate(1, delegate(object p_sender, Action<byte[][]> p_onLoadedCallback, bool p_isReload)
{
if (uMyGUI_PopupManager.Instance != null)
{
uMyGUI_PopupManager.Instance.ShowPopup("loading");
}
((MonoBehaviour)p_sender).StartCoroutine(LoadRoutine("level.txt", p_onLoadedCallback));
});
LE_ExtensionInterface.Save.SetDelegate(1, delegate(object p_sender, byte[] p_levelData, byte[] p_levelMeta, int p_removedDuplicatesCount)
{
string text = Save("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 Save(string p_fileName, 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";
string text2 = GetSaveFolderPath() + "/" + p_fileName;
UtilityPlatformIO.SaveToFile(text2, p_fileContentAsString);
return "Level saved to '" + text2 + "'.\n" + text;
}
public static IEnumerator LoadRoutine(string p_fileName, Action<byte[][]> p_onLoaded)
{
WWW www = new WWW(UtilityPlatformIO.FixFilePath(GetSaveFolderPath() + "/" + p_fileName));
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;
}
if (!string.IsNullOrEmpty(savedLevel))
{
string[] array = savedLevel.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(GetSaveFolderPath() + "/" + p_fileName);
}
}