NLDClient-yudde/ProjectNLD/Assets/Editor/Tools/AutoConfig/ConfigWindow.cs

500 lines
14 KiB
C#
Raw Normal View History

2023-10-23 16:48:43 +08:00
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using Cysharp.Threading.Tasks;
using System;
2023-10-24 12:15:31 +08:00
using AutoConfig;
2023-10-23 16:48:43 +08:00
using Luban.Editor;
2023-10-24 12:15:31 +08:00
using UnityEngine.Networking;
2023-08-22 19:08:45 +08:00
public class ConfigInfo
{
public string Name;
public string DocID;
public bool Check;
2023-10-23 16:48:43 +08:00
}
public class DownloadConfigData
2023-08-22 19:08:45 +08:00
{
public string excelName;
public bool isDone
{
get
{
return downloader.isFinished;
}
}
public WPExcelDownloader downloader;
}
2023-08-22 19:08:45 +08:00
public class ConfigWindow : EditorWindow
{
2023-08-22 19:08:45 +08:00
2023-10-23 16:48:43 +08:00
private List<ConfigInfo> m_Configs;
private bool ExportClient;
private bool ExportServer;
private bool NeedDownload;
private bool _isGenMixKey;
2023-10-23 16:48:43 +08:00
private bool _isDownloading;
2023-10-23 16:48:43 +08:00
private Vector2 scrollPosition = Vector2.zero;
private Vector2 _scrollPositionDownload = Vector2.zero;
2023-10-23 16:48:43 +08:00
private string cookie;
const string DATA_TABLE_ASSEMBLY_REFERENCE_PATH = "Assets/Code/Scripts/Gameplay/DataTable/FrameWork.asmref";
const string DATA_TABLE_OUTSIDE_ASSEMBLY_REFERENCE_PATH = "Assets/Code/Scripts/Gameplay/FrameWork.asmref";
2023-10-24 12:15:31 +08:00
2024-11-11 15:36:59 +08:00
public const string DATA_STRING_LOCALIZE_CN = "Assets/Config/Data/Localize/CN";
public const string DATA_STRING_LOCALIZE_EN = "Assets/Config/Data/Localize/EN";
2023-10-24 12:15:31 +08:00
/// <summary>
/// 0 未知
/// 1 checking
/// 2 ok
/// 3 failed
/// </summary>
private int _authState = 0;
2023-10-23 16:48:43 +08:00
public static void ShowWindow()
{
2023-08-22 19:08:45 +08:00
2023-10-24 12:15:31 +08:00
var window = GetWindow<ConfigWindow>();
window.titleContent = new GUIContent("自动化配置工具");
window.maximized = false;
window.minSize = new Vector2(300, 300);
int width = 600;
int height = 400;
var x = (Screen.currentResolution.width - width) / 2;
var y = (Screen.currentResolution.height - height) / 2;
Rect centerRect = new Rect(x, y, width, height);
window.position = centerRect;
window._authState = 0;
window.InitData();
window.Show();
window.Focus();
2023-10-23 16:48:43 +08:00
}
2023-08-22 19:08:45 +08:00
public void InitData()
2023-10-23 16:48:43 +08:00
{
var dir = new DirectoryInfo(ConfigTools.CONFIG_DIR);
var combiePath = dir.FullName + "Datas/AConfigList.xlsx";
2023-10-24 12:15:31 +08:00
2023-08-22 19:08:45 +08:00
// var configPath = ConfigTools.CONFIG_DIR + "/config_list.txt";
// var lines = File.ReadAllLines(configPath);
m_Configs = new List<ConfigInfo>();
if (File.Exists(combiePath))
{
var excelDataList = ReadExcelList.ReadList(combiePath);
for (var i = 0; i < excelDataList.Count; i++)
{
var lineData = excelDataList[i];
var docName = lineData.excelName;
var docId = lineData.docId;
ConfigInfo ci = new ConfigInfo();
ci.Name = docName;
ci.DocID = docId;
ci.Check = true;
m_Configs.Add(ci);
}
m_Configs.Sort((a,
b) => a.Name.CompareTo(b.Name));
2023-08-22 19:08:45 +08:00
}
2023-10-24 12:15:31 +08:00
else
{
2023-10-23 16:48:43 +08:00
Debug.LogError($"配置文件不存在path={combiePath}");
}
2023-08-22 19:08:45 +08:00
ExportClient = true;
ExportServer = false;
_isGenMixKey = false;
2023-08-22 19:08:45 +08:00
2023-10-23 16:48:43 +08:00
NeedDownload = true;
2023-10-24 12:15:31 +08:00
cookie = EditorPrefs.GetString("ConfCookie", "");
2023-10-23 16:48:43 +08:00
}
2023-08-22 19:08:45 +08:00
void OnGUI()
2023-10-23 16:48:43 +08:00
{
2023-10-24 15:33:07 +08:00
if (m_Configs == null)
{
InitData();
}
_OnGUIContent();
2023-10-24 12:15:31 +08:00
}
private async void _StartChecking()
{
if (string.IsNullOrEmpty(cookie)) return;
_authState = 1;
var downLoader = new WPExcelDownloader("AConfigList.xlsx",
"e3_AXoAQAavAFIi9riMuy9Q5CMxcHei1", cookie);//e3_AQIA8QbmAKEFwoGRFPhT9uizRsOaA
2023-10-24 12:15:31 +08:00
Debug.Log("开始检查授权");
var checkResult = await downLoader.OnlyCheck();
if (checkResult)
{
Debug.Log("授权成功");
_authState = 2;
}
else
{
Debug.LogError("授权失败");
_authState = 3;
}
}
private void _ShowSIDConfig()
{
cookie = EditorGUILayout.TextField("Cookie", cookie);
GUILayout.BeginHorizontal();
if (GUILayout.Button("重新检查授权"))
{
_StartChecking();
2023-10-23 16:48:43 +08:00
}
2023-10-24 12:15:31 +08:00
if (GUILayout.Button("打开授权网页"))
2023-10-23 16:48:43 +08:00
{
2023-10-24 12:15:31 +08:00
_OpenAuthWindow();
2023-10-23 16:48:43 +08:00
}
2023-10-24 12:15:31 +08:00
GUILayout.EndHorizontal();
}
private void _OpenAuthWindow()
{
var url = "https://doc.weixin.qq.com/sheet/e3_AXoAQAavAFI2YSsTvvuQ9aS06iieP";
2023-10-24 12:15:31 +08:00
Application.OpenURL(url);
2023-10-23 16:48:43 +08:00
}
private async void _RefreshList()
{
await ConfigTools.DownloadConfig(cookie);
InitData();
}
private void _ShowAuthState()
2023-10-23 16:48:43 +08:00
{
2023-10-24 12:15:31 +08:00
switch (_authState)
{
case 0:
// 未知
if (string.IsNullOrEmpty(cookie))
{
// 未配置
EditorGUILayout.HelpBox("请先配置sid和cookie", MessageType.Error);
_ShowSIDConfig();
}
else
{
// 已配置
_ShowSIDConfig();
_StartChecking();
}
break;
case 1:
EditorGUILayout.HelpBox("检查授权中", MessageType.Warning);
_ShowSIDConfig();
break;
case 2:
EditorGUILayout.HelpBox("授权成功", MessageType.Info);
2023-10-24 12:15:31 +08:00
_ShowSIDConfig();
break;
case 3:
EditorGUILayout.HelpBox("授权失败", MessageType.Error);
_ShowSIDConfig();
break;
}
}
2023-10-23 16:48:43 +08:00
private void _ShowRefreshConfigList()
{
2024-06-03 12:31:13 +08:00
GUILayout.BeginHorizontal();
2023-08-22 19:08:45 +08:00
if (GUILayout.Button("刷新Config List"))
{
EditorPrefs.SetString("ConfCookie", cookie);
_RefreshList();
2023-10-23 16:48:43 +08:00
}
2024-06-03 12:31:13 +08:00
if (GUILayout.Button("打开excel所在路径"))
{
var openPath = ConfigTools.CONFIG_DIR + "Datas/";
EditorUtility.RevealInFinder(openPath);
}
GUILayout.EndHorizontal();
}
2023-10-23 16:48:43 +08:00
private void _ShowExcelList()
{
2023-10-23 16:48:43 +08:00
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
for (int i = 0; i < m_Configs.Count; i++)
{
var excelName = m_Configs[i].Name;
m_Configs[i].Check = EditorGUILayout.ToggleLeft(excelName, m_Configs[i].Check);
2023-08-22 19:08:45 +08:00
}
EditorGUILayout.EndScrollView();
2024-06-03 12:31:13 +08:00
2023-08-22 19:08:45 +08:00
GUILayout.BeginHorizontal();
if (GUILayout.Button("全选"))
{
for (int i = 0; i < m_Configs.Count; i++)
{
m_Configs[i].Check = true;
}
2023-10-23 16:48:43 +08:00
}
2023-08-22 19:08:45 +08:00
if (GUILayout.Button("全不选"))
{
for (int i = 0; i < m_Configs.Count; i++)
{
m_Configs[i].Check = false;
}
2023-10-23 16:48:43 +08:00
}
GUILayout.EndHorizontal();
2024-06-03 12:31:13 +08:00
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
ExportClient = EditorGUILayout.ToggleLeft("导出客户端", ExportClient);
ExportServer = EditorGUILayout.ToggleLeft("导出服务端", ExportServer);
EditorGUILayout.EndHorizontal();
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
_isGenMixKey = EditorGUILayout.ToggleLeft("导出时生成MixKey", _isGenMixKey);
2024-06-03 12:31:13 +08:00
GUILayout.Space(10);
NeedDownload = EditorGUILayout.ToggleLeft("下载选中配置", NeedDownload);
GUILayout.EndHorizontal();
}
2023-10-23 16:48:43 +08:00
private void _ShowGenBtn()
{
2023-10-23 16:48:43 +08:00
GUILayout.BeginHorizontal();
string buttonName = "开始";
2023-08-22 19:08:45 +08:00
if (NeedDownload)
2023-10-23 16:48:43 +08:00
{
buttonName += " 【下载配置】";
}
2023-08-22 19:08:45 +08:00
if (ExportClient)
{
buttonName += " 【导出到 Client】";
}
if (ExportServer)
{
buttonName += " 【导出到 Server】";
}
if (GUILayout.Button(buttonName))
{
2023-10-24 12:15:31 +08:00
if (string.IsNullOrEmpty(cookie))
{
2023-08-22 19:08:45 +08:00
EditorUtility.DisplayDialog("配置工具", "请先配置sid和cookie", "OK");
return;
}
2023-08-22 19:08:45 +08:00
PlayerPrefs.SetString("ConfCookie", cookie);
_AsyncAction();
}
GUILayout.EndHorizontal();
2023-10-23 16:48:43 +08:00
}
2023-10-24 12:15:31 +08:00
private void _OnGUIContent()
{
GUILayout.Space(10);
_ShowAuthState();
_ShowRefreshConfigList();
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
GUILayout.Label("配置列表:", EditorStyles.boldLabel);
GUILayout.Label("共计:" + m_Configs.Count);
EditorGUILayout.EndHorizontal();
if (_isDownloading)
{
EditorGUILayout.HelpBox("正在下载中,请稍后", MessageType.Warning);
_ShowDonwloadExcels();
}
else
{
_ShowExcelList();
GUILayout.Space(10);
_ShowGenBtn();
}
2024-04-23 14:43:56 +08:00
GUILayout.Space(10);
if (GUILayout.Button("生成MixKey配置"))
{
_GenMixKeyConfig();
//GenMixKeyTool.ReplaceMixKey();
2024-04-23 14:43:56 +08:00
}
}
private void _ShowDonwloadExcels()
{
var configList = DownloadConfig.downloadConfigs;
_scrollPositionDownload = GUILayout.BeginScrollView(_scrollPositionDownload);
for (int i = 0; i < configList.Count; i++)
{
var config = configList[i];
GUILayout.BeginHorizontal();
GUILayout.Label(config.excelName, GUILayout.Width(200));
if (config.isDone)
{
if (config.downloader.isCanceled)
{
GUILayout.Label("取消下载", GUILayout.Width(100));
}
else if (!config.downloader.isDownloaded)
{
GUILayout.Label("下载失败", GUILayout.Width(100));
}
else
{
GUILayout.Label("下载完成", GUILayout.Width(100));
}
}
else
{
GUILayout.Label("下载中", GUILayout.Width(100));
if (GUILayout.Button("取消下载"))
{
config.downloader.Cancel();
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
if (GUILayout.Button("全部取消", GUILayout.Height(40)))
{
for (int i = 0; i < configList.Count; i++)
{
var config = configList[i];
if (config.isDone)
{
continue;
}
config.downloader.Cancel();
}
}
}
2023-10-19 15:00:19 +08:00
private void MoveDataTableAssemblyReference(bool isOut)
{
if (isOut)
AssetDatabase.MoveAsset(DATA_TABLE_ASSEMBLY_REFERENCE_PATH, DATA_TABLE_OUTSIDE_ASSEMBLY_REFERENCE_PATH);
else
AssetDatabase.MoveAsset(DATA_TABLE_OUTSIDE_ASSEMBLY_REFERENCE_PATH, DATA_TABLE_ASSEMBLY_REFERENCE_PATH);
2023-10-23 16:48:43 +08:00
}
2023-10-24 12:15:31 +08:00
2023-08-22 19:08:45 +08:00
private async void _AsyncAction()
{
2023-10-24 12:15:31 +08:00
bool needDownload = false;
for (int i = 0; i < m_Configs.Count; i++)
2023-08-22 19:08:45 +08:00
{
2023-10-24 12:15:31 +08:00
if (m_Configs[i].Check)
needDownload = true;
}
2023-08-22 19:08:45 +08:00
2023-10-24 12:15:31 +08:00
if (needDownload && NeedDownload)
{
_isDownloading = true;
await DownloadConfig.AutoProcessConfig(m_Configs, cookie);
_isDownloading = false;
2023-10-24 12:15:31 +08:00
}
else
{
Debug.Log("当前没有需要下载的配置,已跳过");
}
2023-08-22 19:08:45 +08:00
2023-10-24 12:15:31 +08:00
await UniTask.Delay(100);
2023-10-19 15:00:19 +08:00
2023-10-24 12:15:31 +08:00
if (ExportClient)
2023-08-22 19:08:45 +08:00
{
2023-10-24 12:15:31 +08:00
var lubanAsset = AssetDatabase.LoadAssetAtPath<LubanExportConfig>("Assets/Editor/Tools/Luban_Client.asset");
2024-11-11 15:36:59 +08:00
var lubanLocalizeCNAsset = AssetDatabase.LoadAssetAtPath<LubanExportConfig>("Assets/Editor/Tools/Luban_Client_CN.asset");
var lubanLocalizeENAsset = AssetDatabase.LoadAssetAtPath<LubanExportConfig>("Assets/Editor/Tools/Luban_Client_EN.asset");
2024-06-20 18:47:22 +08:00
var genSuccess = lubanAsset.Gen();
2024-11-11 15:36:59 +08:00
var genCNSuccess = lubanLocalizeCNAsset.Gen();
var genENSuccess = lubanLocalizeENAsset.Gen();
_ClearLocalizeDir();
if (genSuccess && _isGenMixKey)
2024-06-20 18:47:22 +08:00
{
_GenMixKeyConfig();
}
2023-08-22 19:08:45 +08:00
}
2023-10-24 12:15:31 +08:00
if (ExportServer)
2024-04-26 13:41:44 +08:00
{
var lubanAsset = AssetDatabase.LoadAssetAtPath<LubanExportConfig>("Assets/Editor/Tools/Luban_Server.asset");
lubanAsset.Gen();
}
2023-10-24 12:15:31 +08:00
2023-08-22 19:08:45 +08:00
}
2024-04-23 14:43:56 +08:00
private void _GenMixKeyConfig()
{
//GenMixKeyTool.GenMixKeyFile();
MixKeyGenJson.IsGenJsonProcess = true;
GenMixKeyTool.GenMixKeyJson();
2024-04-23 14:43:56 +08:00
}
2024-11-11 15:36:59 +08:00
/// <summary>
/// 清理本地化生成的无用json文件
/// </summary>
private void _ClearLocalizeDir()
{
if (Directory.Exists(DATA_STRING_LOCALIZE_CN))
{
foreach (var file in Directory.GetFileSystemEntries(DATA_STRING_LOCALIZE_CN))
{
if (!file.Contains("stringcfg"))
{
File.Delete(file);
}
}
}
if (Directory.Exists(DATA_STRING_LOCALIZE_EN))
{
foreach (var file in Directory.GetFileSystemEntries(DATA_STRING_LOCALIZE_EN))
{
if (!file.Contains("stringcfg"))
{
File.Delete(file);
}
}
}
AssetDatabase.Refresh();
}
2023-10-23 16:48:43 +08:00
}