216 lines
6.9 KiB
C#
216 lines
6.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Runtime.InteropServices;
|
|
using LE_LevelEditor.Example;
|
|
using LE_LevelEditor.UI;
|
|
using LapinerTools.uMyGUI;
|
|
using UnityEngine;
|
|
|
|
namespace LE_LevelEditor.Extensions
|
|
{
|
|
public static class LE_ExtensionInterface
|
|
{
|
|
public delegate void LoadDelegate(object p_sender, Action<byte[][]> p_onLoadedCallback, bool p_isReload);
|
|
|
|
public delegate void SaveDelegate(object p_sender, byte[] p_levelData, byte[] p_levelMeta, int p_removedDuplicatesCount);
|
|
|
|
public class FileSelectionExtensionLoader
|
|
{
|
|
public interface ILevelDatabase
|
|
{
|
|
IEnumerator GetLevelListAsync(Action<LevelFile[]> p_onLoaded);
|
|
|
|
void SaveFile(string p_levelName, byte[] p_levelData, byte[] p_levelMeta, int p_removedDuplicatesCount, LevelFile[] p_levelFiles, Action<string> p_onSuccess, Action p_onFail);
|
|
|
|
IEnumerator Delete(LevelFile p_levelFile, Action<bool> p_onResult);
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential, Size = 1)]
|
|
public struct LevelFile
|
|
{
|
|
public string Name { get; set; }
|
|
|
|
public string PathData { get; set; }
|
|
|
|
public string PathIcon { get; set; }
|
|
|
|
public static string[] GetLevelNames(LevelFile[] p_levelFiles)
|
|
{
|
|
string[] array = new string[p_levelFiles.Length];
|
|
for (int i = 0; i < p_levelFiles.Length; i++)
|
|
{
|
|
array[i] = p_levelFiles[i].Name;
|
|
}
|
|
return array;
|
|
}
|
|
|
|
public static string[] GetLevelIconPaths(LevelFile[] p_levelFiles)
|
|
{
|
|
string[] array = new string[p_levelFiles.Length];
|
|
for (int i = 0; i < p_levelFiles.Length; i++)
|
|
{
|
|
array[i] = p_levelFiles[i].PathIcon;
|
|
}
|
|
return array;
|
|
}
|
|
}
|
|
|
|
private string m_reloadLevelShortName;
|
|
|
|
private string m_reloadLevelName;
|
|
|
|
public string ReloadLevelName
|
|
{
|
|
get
|
|
{
|
|
return m_reloadLevelName;
|
|
}
|
|
set
|
|
{
|
|
m_reloadLevelName = value;
|
|
}
|
|
}
|
|
|
|
public ILevelDatabase LevelDB { get; set; }
|
|
|
|
public FileSelectionExtensionLoader()
|
|
{
|
|
LevelDB = new LE_LevelDatabase_Default();
|
|
Load.SetDelegate(10, SelectAndLoadFile);
|
|
Save.SetDelegate(10, SelectAndSaveFile);
|
|
}
|
|
|
|
private void SelectAndLoadFile(object p_sender, Action<byte[][]> p_onLoadedCallback, bool p_isReload)
|
|
{
|
|
if (p_isReload)
|
|
{
|
|
if (!string.IsNullOrEmpty(m_reloadLevelName))
|
|
{
|
|
LoadFile(p_sender, m_reloadLevelShortName, m_reloadLevelName, p_onLoadedCallback);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("FileSelectionExtensionLoader: SelectAndLoadFile: no level was loaded yet, but 'p_isReload' was 'true'!");
|
|
}
|
|
return;
|
|
}
|
|
((MonoBehaviour)p_sender).StartCoroutine(LevelDB.GetLevelListAsync(delegate(LevelFile[] p_levelFiles)
|
|
{
|
|
if (uMyGUI_PopupManager.Instance != null)
|
|
{
|
|
string[] levelNames = LevelFile.GetLevelNames(p_levelFiles);
|
|
string[] levelIconPaths = LevelFile.GetLevelIconPaths(p_levelFiles);
|
|
((LE_PopupFileSelection)uMyGUI_PopupManager.Instance.ShowPopup("fileselection")).SetFiles(levelNames, levelIconPaths, delegate(int p_selectedLevelIndex)
|
|
{
|
|
LoadFile(p_sender, p_levelFiles[p_selectedLevelIndex].Name, p_levelFiles[p_selectedLevelIndex].PathData, p_onLoadedCallback);
|
|
}, null).SetText(Utilities.GetTranslation("EDITOR/LOAD"), Utilities.GetTranslation("EDITOR/LOAD_WINDOW_MSG")).ShowButton("close");
|
|
}
|
|
}));
|
|
}
|
|
|
|
private void SelectAndSaveFile(object p_sender, byte[] p_levelData, byte[] p_levelMeta, int p_removedDuplicatesCount)
|
|
{
|
|
((MonoBehaviour)p_sender).StartCoroutine(LevelDB.GetLevelListAsync(delegate(LevelFile[] p_levelFiles)
|
|
{
|
|
if (uMyGUI_PopupManager.Instance != null)
|
|
{
|
|
string[] levelNames = LevelFile.GetLevelNames(p_levelFiles);
|
|
string[] levelIconPaths = LevelFile.GetLevelIconPaths(p_levelFiles);
|
|
LE_PopupFileSelection filePopup = (LE_PopupFileSelection)uMyGUI_PopupManager.Instance.ShowPopup("fileselection");
|
|
filePopup.SetFiles(levelNames, levelIconPaths, delegate(int p_selectedLevelIndex)
|
|
{
|
|
filePopup.SaveInput.text = levelNames[p_selectedLevelIndex];
|
|
}, delegate(int p_deletedLevelIndex)
|
|
{
|
|
filePopup.Hide();
|
|
((MonoBehaviour)p_sender).StartCoroutine(LevelDB.Delete(p_levelFiles[p_deletedLevelIndex], delegate
|
|
{
|
|
SelectAndSaveFile(p_sender, p_levelData, p_levelMeta, p_removedDuplicatesCount);
|
|
}));
|
|
}, false).SetText(Utilities.GetTranslation("EDITOR/SAVE"), Utilities.GetTranslation("EDITOR/SAVE_WINDOW_MSG")).ShowButton("close")
|
|
.ShowButton("save", delegate
|
|
{
|
|
if (!string.IsNullOrEmpty(filePopup.SaveInput.text))
|
|
{
|
|
LevelDB.SaveFile(filePopup.SaveInput.text, p_levelData, p_levelMeta, p_removedDuplicatesCount, p_levelFiles, delegate(string p_savedFilePath)
|
|
{
|
|
OnSaveSuccess(p_savedFilePath, filePopup.SaveInput.text);
|
|
}, delegate
|
|
{
|
|
SelectAndSaveFile(p_sender, p_levelData, p_levelMeta, p_removedDuplicatesCount);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
((uMyGUI_PopupText)uMyGUI_PopupManager.Instance.ShowPopup("text")).SetText(Utilities.GetTranslation("EDITOR/SAVE"), Utilities.GetTranslation("EDITOR/FILE_NAME_EMPTY")).ShowButton("ok", delegate
|
|
{
|
|
SelectAndSaveFile(p_sender, p_levelData, p_levelMeta, p_removedDuplicatesCount);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}));
|
|
}
|
|
|
|
public void OnSaveSuccess(string p_savedFilePath, string fileName)
|
|
{
|
|
m_reloadLevelShortName = fileName;
|
|
m_reloadLevelName = p_savedFilePath;
|
|
FisheryEditor.Instance.CURRENT_LEVEL_FILE_NAME = fileName;
|
|
FisheryEditor.Instance.CURRENT_LEVEL_FILE_PATH = m_reloadLevelName;
|
|
}
|
|
|
|
private void LoadFile(object p_sender, string p_fileName, string p_filePath, Action<byte[][]> p_onLoadedCallback)
|
|
{
|
|
m_reloadLevelShortName = p_fileName;
|
|
m_reloadLevelName = p_filePath;
|
|
FisheryEditor.Instance.CURRENT_LEVEL_FILE_NAME = m_reloadLevelShortName;
|
|
FisheryEditor.Instance.CURRENT_LEVEL_FILE_PATH = m_reloadLevelName;
|
|
if (uMyGUI_PopupManager.Instance != null)
|
|
{
|
|
uMyGUI_PopupManager.Instance.ShowPopup("loading");
|
|
}
|
|
((MonoBehaviour)p_sender).StartCoroutine(ExampleGame_LoadSave.LoadRoutineByFilePath(p_filePath, p_onLoadedCallback));
|
|
}
|
|
}
|
|
|
|
private static LE_ExtensionDelegate<LoadDelegate> m_load = null;
|
|
|
|
private static LE_ExtensionDelegate<SaveDelegate> m_save = null;
|
|
|
|
private static FileSelectionExtensionLoader s_fileSelectionExtensionLoader = new FileSelectionExtensionLoader();
|
|
|
|
public static LE_ExtensionDelegate<LoadDelegate> Load
|
|
{
|
|
get
|
|
{
|
|
if (m_load == null)
|
|
{
|
|
m_load = new LE_ExtensionDelegate<LoadDelegate>();
|
|
}
|
|
return m_load;
|
|
}
|
|
}
|
|
|
|
public static LE_ExtensionDelegate<SaveDelegate> Save
|
|
{
|
|
get
|
|
{
|
|
if (m_save == null)
|
|
{
|
|
m_save = new LE_ExtensionDelegate<SaveDelegate>();
|
|
}
|
|
return m_save;
|
|
}
|
|
}
|
|
|
|
public static FileSelectionExtensionLoader FileSelectionInstance
|
|
{
|
|
get
|
|
{
|
|
return s_fileSelectionExtensionLoader;
|
|
}
|
|
}
|
|
}
|
|
}
|