using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Text;
|
using System.Windows.Forms;
|
using UnityEditor;
|
using UnityEngine;
|
|
public class TableTool : EditorWindow
|
{
|
|
public static string configOutPutPath;
|
static bool isIL = false; // 导出表代码是否IL使用
|
|
[UnityEditor.MenuItem("策划工具/导出策划表到游戏工程")]
|
static void Init()
|
{
|
configOutPutPath = UnityEngine.Application.dataPath + "/StreamingAssets/Config";
|
window = GetWindow(typeof(TableTool), false, "策划导表工具") as TableTool;
|
window.position = new Rect(UnityEngine.Screen.width / 2, UnityEngine.Screen.height / 2, 300, 700);
|
window.Show();
|
//PathCache();
|
}
|
|
public class FileToggleInfo
|
{
|
public FileToggleInfo(FileInfo file, bool sel)
|
{
|
fileInfo = file;
|
isSelect = sel;
|
}
|
|
public FileInfo fileInfo = null;
|
public bool isSelect = false;
|
}
|
|
private static TableTool window = null;
|
private Vector2 scrollPosition;
|
private static string _tablePath = "";
|
private static List<FileToggleInfo> _tableNameLst = new List<FileToggleInfo>(); //所有表名字列表
|
private void OnGUI()
|
{
|
_tablePath = UnityEngine.Application.dataPath + "/ResourcesOut/Refdata/Config";
|
GUILayout.BeginVertical();
|
GUILayout.TextArea("策划配表路径:" + _tablePath);
|
|
GUILayout.Box(new GUIContent("路径下的所有表:"));
|
|
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
|
ShowTableNames();
|
GUILayout.EndScrollView();
|
|
GUILayout.FlexibleSpace();
|
|
GUILayout.BeginHorizontal();
|
if (GUILayout.Button("刷新"))
|
{
|
GetDicFiles();
|
}
|
if (GUILayout.Button("全选"))
|
{
|
SelectAll();
|
}
|
GUILayout.EndHorizontal();
|
|
GUILayout.BeginHorizontal();
|
if (GUILayout.Button("导出表"))
|
{
|
isIL = false;
|
ReadAllTxt();
|
GenAllClass();
|
MessageBox.Show("导出表成功!");
|
AssetDatabase.Refresh();
|
}
|
GUILayout.EndHorizontal();
|
|
// GUILayout.BeginHorizontal();
|
// if (GUILayout.Button("导出IL表"))
|
// {
|
// isIL = true;
|
// ReadAllTxt();
|
// GenAllClass();
|
// MessageBox.Show("导出IL表成功!");
|
// AssetDatabase.Refresh();
|
// isIL = false;
|
// }
|
// GUILayout.EndHorizontal();
|
|
|
// if (GUILayout.Button("选择路径"))
|
// {
|
//#if UNITY_EDITOR
|
// FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
|
|
// if (folderBrowserDialog.ShowDialog() == DialogResult.OK || folderBrowserDialog.ShowDialog() == DialogResult.Yes)
|
// {
|
// _tablePath = folderBrowserDialog.SelectedPath + "\\";
|
// PathCache(_tablePath);
|
// }
|
|
// folderBrowserDialog.Dispose();
|
//#endif
|
|
// }
|
GUIUtility.ExitGUI();
|
}
|
|
private void ShowTableNames()
|
{
|
if (_tableNameLst == null)
|
{
|
return;
|
}
|
for (int i = 0; i < _tableNameLst.Count; i++)
|
{
|
string tableName = _tableNameLst[i].fileInfo.Name;
|
|
_tableNameLst[i].isSelect = GUILayout.Toggle(_tableNameLst[i].isSelect, tableName);
|
|
}
|
}
|
|
private void GetDicFiles()
|
{
|
if (string.IsNullOrEmpty(_tablePath))
|
{
|
MessageBox.Show("请选择策划表路径!");
|
return;
|
}
|
_tableNameLst.Clear();
|
DirectoryInfo folder = new DirectoryInfo(@_tablePath);
|
if (folder != null)
|
{
|
FileInfo[] fileInfoArr = folder.GetFiles("*.txt");
|
if (fileInfoArr == null || fileInfoArr.Length <= 0)
|
{
|
return;
|
}
|
for (int i = 0, length = fileInfoArr.Length; i < length; i++)
|
{
|
_tableNameLst.Add(new FileToggleInfo(fileInfoArr[i], false));
|
}
|
}
|
}
|
|
/// <summary>
|
/// 选中所有表
|
/// </summary>
|
private void SelectAll()
|
{
|
if (_tableNameLst == null)
|
{
|
MessageBox.Show("当前没有表可选!");
|
return;
|
}
|
for (int i = 0; i < _tableNameLst.Count; i++)
|
{
|
if (_tableNameLst[i] == null)
|
{
|
continue;
|
}
|
if (_tableNameLst[i].fileInfo == null)
|
{
|
continue;
|
}
|
_tableNameLst[i].isSelect = true;
|
}
|
}
|
|
[UnityEditor.MenuItem("Assets/生成配置解析类型")]
|
public static void GenerateConfigClass()
|
{
|
_tableNameLst.Clear();
|
|
if (UnityEditor.Selection.objects != null)
|
{
|
foreach (var o in UnityEditor.Selection.objects)
|
{
|
var objectName = AssetDatabase.GetAssetPath(o.GetInstanceID());
|
if (objectName.EndsWith(".txt") || objectName.EndsWith(".TXT"))
|
{
|
_tableNameLst.Add(new FileToggleInfo(new FileInfo(objectName), true));
|
}
|
}
|
|
GenAllClass();
|
AssetDatabase.Refresh();
|
}
|
|
_tableNameLst.Clear();
|
|
MessageBox.Show("导出成功!");
|
}
|
|
public static void CopyConfigsToOutPutPath(string _outPath)
|
{
|
configOutPutPath = _outPath;
|
var root1 = UnityEngine.Application.dataPath + "/ResourcesOut/Refdata/Config";
|
var configFiles = new List<FileInfo>();
|
configFiles.AddRange(FileExtersion.GetFileInfos(root1, new string[] { "*.txt", "*.TXT" }));
|
var root2 = UnityEngine.Application.dataPath + "/Resources/Config";
|
if (Directory.Exists(root2))
|
{
|
var fileInfos = FileExtersion.GetFileInfos(root2, new string[] { "*.txt", "*.TXT" });
|
if (fileInfos != null)
|
{
|
configFiles.AddRange(fileInfos);
|
}
|
}
|
|
foreach (var file in configFiles)
|
{
|
var fileInfo = new FileInfo(file.FullName);
|
CopyTxt(fileInfo);
|
}
|
}
|
|
/// <summary>
|
/// 导出所有表
|
/// </summary>
|
private void ReadAllTxt()
|
{
|
if (string.IsNullOrEmpty(_tablePath))
|
{
|
MessageBox.Show("请选择策划表路径!");
|
return;
|
}
|
if (_tableNameLst == null)
|
{
|
MessageBox.Show("当前路径下没有可读取的策划表!");
|
return;
|
}
|
for (int i = 0; i < _tableNameLst.Count; i++)
|
{
|
if (!_tableNameLst[i].isSelect)
|
{
|
continue;
|
}
|
CopyTxt(_tableNameLst[i].fileInfo);
|
}
|
}
|
|
private static void CopyTxt(FileInfo fileInfo)
|
{
|
string fileName = fileInfo.Name.Split('.')[0];
|
|
if (!Directory.Exists(configOutPutPath))
|
{
|
Directory.CreateDirectory(configOutPutPath);
|
}
|
|
string filePath = configOutPutPath + "/" + fileName + ".txt";
|
if (File.Exists(filePath))
|
{
|
File.Delete(filePath);
|
}
|
|
File.Copy(fileInfo.FullName, filePath);
|
}
|
|
/// <summary>
|
/// 生成所有表模型
|
/// </summary>
|
private static void GenAllClass()
|
{
|
if (_tableNameLst == null)
|
{
|
return;
|
}
|
|
for (int i = 0; i < _tableNameLst.Count; i++)
|
{
|
if (!_tableNameLst[i].isSelect)
|
{
|
continue;
|
}
|
FileStream fileStream = _tableNameLst[i].fileInfo.OpenRead();
|
StreamReader sr = new StreamReader(fileStream, Encoding.UTF8);
|
CreateConfigClassFile.CreateConfigClass(_tableNameLst[i].fileInfo, isIL);
|
sr.Dispose();
|
sr.Close();
|
}
|
}
|
|
/// <summary>
|
/// 路径缓存
|
/// </summary>
|
/// <param name="setPath"></param>
|
//private static void PathCache(string setPath = "")
|
//{
|
// DebugEx.Log("路径缓存:" + setPath);
|
// string configPath = UnityEngine.Application.dataPath;
|
// configPath = configPath + "/ResourcesOut/Refdata/Config/PathCache/TablePath.txt";
|
// FileStream configPathTxt = File.Open(configPath, FileMode.Open);
|
// if (string.IsNullOrEmpty(setPath))
|
// {
|
// StreamReader sr = new StreamReader(configPathTxt);
|
// string path = sr.ReadToEnd();
|
// _tablePath = path;
|
// sr.Dispose();
|
// sr.Close();
|
// }
|
// else
|
// {
|
// StreamWriter sw = new StreamWriter(configPathTxt);
|
// sw.Write(setPath);
|
// sw.Dispose();
|
// sw.Close();
|
// }
|
//}
|
}
|
|
public class Param
|
{
|
public int Paramindex;
|
public string AttType;
|
public Type SysType;
|
public List<string> ParmValue = null;
|
public string SampleValue;
|
public string parmName;
|
public string SrcParmName;
|
public string parmComment;
|
|
public Param(string attType, string attName, string attcomment, string samValue, int index)
|
{
|
ParmValue = new List<string>();
|
Paramindex = index;
|
SetParmName(attName);
|
parmComment = attcomment;
|
SampleValue = samValue;
|
|
StringBuilder valueSb = new StringBuilder();
|
valueSb.Append("modelDetails[");
|
valueSb.Append(index.ToString());
|
valueSb.Append("].Trim()");
|
string value = valueSb.ToString();
|
attType = attType.Trim();
|
|
switch (attType)
|
{
|
case "int":
|
{
|
SysType = typeof(System.Int32);
|
SetSigleValue("int", value);
|
}
|
break;
|
case "Int64":
|
{
|
SysType = typeof(System.Int64);
|
SetSigleValue("Int64", value);
|
}
|
break;
|
case "string":
|
{
|
SysType = typeof(System.String);
|
ParmValue.Add(value + ";");
|
}
|
break;
|
|
case "bool":
|
{
|
SysType = typeof(System.Boolean);
|
ParmValue.Add(value + ";");
|
}
|
break;
|
case "float":
|
{
|
SysType = typeof(System.Double);
|
SetSigleValue("float", value);
|
}
|
break;
|
case "byte":
|
{
|
SysType = typeof(System.Byte);
|
SetSigleValue("byte", value);
|
}
|
break;
|
case "double":
|
{
|
SysType = typeof(System.Double);
|
SetSigleValue("double", value);
|
}
|
break;
|
case "int[]":
|
SysType = Type.GetType("System.Int32[]");
|
SetValue("int", value, SampleValue);
|
break;
|
case "string[]":
|
SysType = Type.GetType("System.String[]");
|
SetValue("string", value, SampleValue);
|
break;
|
case "double[]":
|
SysType = Type.GetType("System.Double[]");
|
SetValue("double", value, SampleValue);
|
break;
|
default: SysType = null; break;
|
}
|
}
|
|
private void SetValue(string paramType, string value, string sampleValue)
|
{
|
string[] valueSpArray = sampleValue.Split('|');
|
if (valueSpArray == null)
|
{
|
return;
|
}
|
|
StringBuilder arrayName = new StringBuilder();
|
arrayName.Append(parmName);
|
arrayName.Append("SplitArray");
|
|
StringBuilder firstValue = new StringBuilder();
|
firstValue.Append("string[] ");
|
firstValue.Append(arrayName.ToString());
|
firstValue.Append(" = ");
|
firstValue.Append(value);
|
firstValue.Append(".Split('|');");
|
ParmValue.Add(firstValue.ToString());
|
|
StringBuilder secondValue = new StringBuilder();
|
secondValue.Append(parmName);
|
secondValue.Append(" = new ");
|
secondValue.Append(paramType);
|
secondValue.Append("[" + valueSpArray.Length + "];");
|
ParmValue.Add(secondValue.ToString());
|
|
ParmValue.Add(string.Format("for (int i = 0; i < {0}.Length; i++) {", parmName));
|
ParmValue.Add(string.Format(" {0}[i] = IsNumeric({1}SplitArray[i]) ? double.Parse({2}SplitArray[i]) : (double)0;", parmName, parmName, parmName));
|
ParmValue.Add("}");
|
}
|
|
private void SetSigleValue(string paramType, string value)
|
{
|
StringBuilder strBld = new StringBuilder();
|
strBld.Append("IsNumeric(");
|
strBld.Append(value);
|
strBld.Append(string.Format(")? {0}.Parse(", paramType));
|
strBld.Append(value);
|
strBld.Append(string.Format("):({0})0;", paramType));
|
ParmValue.Add(strBld.ToString());
|
}
|
|
public void SetParmName(string fieldName)
|
{
|
string fName = fieldName.Replace(" ", "");
|
fName = fName.Split('[')[0];
|
SrcParmName = fName;
|
fName = "_" + fName.ToLower();
|
parmName = fName;
|
}
|
|
}
|