miduo_client/Assets/Scripts/Editor/ExcelTool/DataConfig/DataConfigWindow.cs

292 lines
11 KiB
C#
Raw Normal View History

2020-05-09 13:31:21 +08:00
using GameCore;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEditor;
using UnityEngine;
namespace GameEditor.Core.DataConfig
{
public class DataConfigWindow : EditorWindow
{
[MenuItem("Data Config/Window")]
private static void ShowConfigWin()
{
var win = GetWindow<DataConfigWindow>();
win.titleContent = new GUIContent("Data Config");
win.Show();
}
//true表示正在使用,false没有使用
private static bool IsFileInUse(string fileName)
{
bool inUse = true;
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
inUse = false;
}
catch { }
finally
{
if (fs != null) fs.Close();
}
return inUse;
}
private static void excelALLConfig(bool isIncrement)
{
if (!string.IsNullOrEmpty(DataConfigSetting.execlDir) && Directory.Exists(DataConfigSetting.execlDir))
{
string[] files = Directory.GetFiles(DataConfigSetting.execlDir, "*.*", SearchOption.AllDirectories);
DataConfigSetting setting = new DataConfigSetting();
object lockObj = new object();
string FilePath = null;
int count = 0;
int total = 0;
System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();
Dictionary<string, string> record = new Dictionary<string, string>();
Dictionary<string, string> newRecord = new Dictionary<string, string>();
for (int i = 0; i < DataConfigMD5Record.Instance.Keys.Length; i++)
{
record[DataConfigMD5Record.Instance.Keys[i]] = DataConfigMD5Record.Instance.Values[i];
}
time.Start();
EditorUtility.DisplayProgressBar("Loading", "正在导表:", 0f);
EditorApplication.update = () =>
{
if (count < total)
{
EditorUtility.DisplayProgressBar(string.Format("Loading({0}/{1})", count, total), "正在导表:" + FilePath, (float)count / total);
}
else
{
EditorApplication.update = null;
int i = 0;
DataConfigMD5Record.Instance.Keys = new string[newRecord.Count];
DataConfigMD5Record.Instance.Values = new string[newRecord.Count];
foreach (var kvp in newRecord)
{
DataConfigMD5Record.Instance.Keys[i] = kvp.Key;
DataConfigMD5Record.Instance.Values[i] = kvp.Value;
i++;
}
foreach (var kvp in record)
{
string luaName = Path.GetFileNameWithoutExtension(kvp.Key);
string luaPath = string.Format("{0}/{1}.lua", setting.clientOutputDir, luaName);
if (File.Exists(luaPath))
{
Debug.LogError("有表被移除正在清理Lua文件" + luaName);
File.Delete(luaPath);
}
}
EditorUtility.SetDirty(DataConfigMD5Record.Instance);
AssetDatabase.SaveAssets();
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh();
time.Stop();
Debug.LogError("耗时:" + time.ElapsedMilliseconds);
EditorUtil.ShowSureDialog("配置表数据导入完毕!");
}
};
for (int i = 0; i < files.Length; i++)
{
var item = files[i];
ThreadPool.QueueUserWorkItem(q =>
{
if (IsFileInUse(item))
{
Debug.LogError("该表正在编辑,无法导出!:" + item);
lock (lockObj)
{
if (record.ContainsKey(item))
{
newRecord[item] = record[item];
record.Remove(item);
}
count++;
}
return;
}
string crc = FileToCRC32.GetFileCRC32(item);
bool isChanged = !record.ContainsKey(item) || record[item] != crc;
if (!isIncrement || (isIncrement && isChanged))
{
lock (lockObj)
{
FilePath = item;
}
exportSheet(setting, item, isIncrement);
}
lock (lockObj)
{
newRecord[item] = crc;
if (record.ContainsKey(item))
{
record.Remove(item);
}
count++;
}
});
total++;
}
}
}
[MenuItem("Data Config/一键导表")]
private static void ExcelALLConfig()
{
excelALLConfig(false);
}
[MenuItem("Data Config/变化导表")]
private static void ExcelChangeConfig()
{
excelALLConfig(true);
}
private static void exportSheet(DataConfigSetting setting, string path, bool isIncrement)
{
string fileExt = Path.GetExtension(path).ToLower();
if (fileExt == ".xlsx" || fileExt == ".xls")
{
DataSheet sheet = DataHelper.GetSheet(path);
bool isServer = true;
foreach (DataField df in sheet.fields)
{
if (df.exportType == DataFieldExportType.Client || df.exportType == DataFieldExportType.All)
{
isServer = false;
break;
}
}
if (!isServer)
{
try
{
if (DataToOptimizeLuaExporter.CheckIsExport(sheet))
{
if (isIncrement)
{
Debug.Log("该表有改动!正在导表:" + sheet.name);
}
new DataToOptimizeLuaExporter(sheet, setting).Export();
}
else
{
Debug.LogError("前端没有数据导出");
}
}
catch (System.Exception e)
{
Debug.Log(e);
}
}
else
{
Debug.Log("该表为纯后端表,无需导出!" + sheet.name);
}
}
}
public enum TabType
{
Setting = 0,
Excel = 1,
// Check = 2,
}
public string[] TabNames = new string[]
{
"Setting",
"Excel",
// "Check",
};
public DataConfigExcelTab excelTab = null;
public DataConfigSettingTab settingTab = null;
private TabType tabType = TabType.Setting;
const float k_ToolbarPadding = 15;
public List<DataExcelSetting> excels = null;
public DataConfigSetting setting = null;
public static DataConfigWindow window = null;
void Awake()
{
window = this;
setting = DataConfigSetting.InitSetting();
settingTab = new DataConfigSettingTab();
excelTab = new DataConfigExcelTab(this);
}
void OnDestroy()
{
window = null;
}
void OnGUI()
{
if(excels == null)
{
excels = new List<DataExcelSetting>();
if (!string.IsNullOrEmpty(DataConfigSetting.execlDir) && Directory.Exists(DataConfigSetting.execlDir))
{
string[] files = Directory.GetFiles(DataConfigSetting.execlDir, "*.*", SearchOption.AllDirectories);
EditorUtility.DisplayProgressBar("Loading", "loading excel", 0f);
for(int i =0;i<files.Length;i++)
{
EditorUtility.DisplayProgressBar("Loading", "loading excel:"+files[i], ((float)i)/files.Length);
string fileExt = Path.GetExtension(files[i]).ToLower();
if (fileExt == ".xlsx" || fileExt == ".xls")
{
DataExcelSetting eSetting = new DataExcelSetting(files[i]);
excels.Add(eSetting);
}
}
EditorUtility.ClearProgressBar();
}
excels.Sort((x, y) => { return x.excelPath.CompareTo(y.excelPath); });
excelTab.InitExcelTreeView();
}
GUILayout.BeginHorizontal();
{
GUILayout.Space(k_ToolbarPadding);
float toolbarWidth = position.width - k_ToolbarPadding * 4;
tabType = (TabType)GUILayout.Toolbar((int)tabType, TabNames, "LargeButton", GUILayout.Width(toolbarWidth));
}
GUILayout.EndHorizontal();
Rect contentRect = new Rect(2, 40, position.width-4, position.height - 60);
if (tabType == TabType.Setting)
{
settingTab.OnGUI(contentRect);
} else if (tabType == TabType.Excel)
{
excelTab.OnGUI(contentRect);
}
}
public void OnSettingChanged()
{
DataConfigSetting.SaveSetting(setting);
excels = null;
}
}
}