using System;  
 | 
using System.CodeDom;  
 | 
using System.Collections.Generic;  
 | 
using System.IO;  
 | 
using System.Security.Cryptography;  
 | 
using System.Text;  
 | 
using System.Windows.Forms;  
 | 
using UnityEditor;  
 | 
using UnityEngine;  
 | 
  
 | 
public class TableTool : EditorWindow  
 | 
{  
 | 
  
 | 
    public static string configOutPutPath = UnityEngine.Application.dataPath + "/StreamingAssets/Config";  
 | 
  
 | 
    [UnityEditor.MenuItem("策划工具/导出策划表到游戏工程")]  
 | 
    static void Init()  
 | 
    {  
 | 
        window = GetWindow(typeof(TableTool), true, "策划导表工具") 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()  
 | 
    {  
 | 
        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("导出表"))  
 | 
        {  
 | 
            ReadAllTxt();  
 | 
            GenAllClass();  
 | 
            MessageBox.Show("导出表成功!");  
 | 
            AssetDatabase.Refresh();  
 | 
        }  
 | 
        GUILayout.EndHorizontal();  
 | 
  
 | 
        GUILayout.EndVertical();  
 | 
  
 | 
        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  
 | 
  
 | 
        }  
 | 
    }  
 | 
  
 | 
    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 ReadAllTxtToBytes(string _outPath)  
 | 
    {  
 | 
        configOutPutPath = _outPath;  
 | 
        var rootPath = UnityEngine.Application.dataPath + "/ResourcesOut/Refdata/Config";  
 | 
        var configFiles = FileExtersion.GetFileInfos(rootPath, new string[] { "*.txt", "*.TXT" });  
 | 
        foreach (var file in configFiles)  
 | 
        {  
 | 
            var fileInfo = new System.IO.FileInfo(file.FullName);  
 | 
            ReadTxtToBytes(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;  
 | 
            }  
 | 
            ReadTxtToBytes(_tableNameLst[i].fileInfo);  
 | 
        }  
 | 
    }  
 | 
  
 | 
    /// <summary>  
 | 
    ///  读txt转二进制文件  
 | 
    /// </summary>  
 | 
    /// <param name="fileInfo"></param>  
 | 
    private static void ReadTxtToBytes(FileInfo fileInfo)  
 | 
    {  
 | 
        string fileName = fileInfo.Name.Split('.')[0];  
 | 
  
 | 
        if (!Directory.Exists(configOutPutPath))  
 | 
        {  
 | 
            Directory.CreateDirectory(configOutPutPath);  
 | 
        }  
 | 
  
 | 
        string filePath = configOutPutPath + "/" + fileName + ".bytes";  
 | 
        if (File.Exists(filePath))  
 | 
        {  
 | 
            File.Delete(filePath);  
 | 
        }  
 | 
        FileStream fileStream = fileInfo.OpenRead();  
 | 
        StreamReader streamStream = new StreamReader(fileStream, Encoding.UTF8);  
 | 
  
 | 
        TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();  
 | 
        des.Key = Convert.FromBase64String(ConfigManager.CustomKey);  
 | 
        des.IV = Convert.FromBase64String(ConfigManager.CustomIV);  
 | 
        des.Mode = System.Security.Cryptography.CipherMode.CBC;  
 | 
        des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;  
 | 
  
 | 
        FileStream fs = new FileStream(filePath, FileMode.Create);  
 | 
        CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(), CryptoStreamMode.Write);  
 | 
        BinaryWriter bw = new BinaryWriter(cs);  
 | 
        try  
 | 
        {  
 | 
            bw.Write(streamStream.ReadToEnd());  
 | 
            bw.Flush();  
 | 
            bw.Close();  
 | 
            streamStream.Dispose();  
 | 
            streamStream.Close();  
 | 
        }  
 | 
        catch (IOException e)  
 | 
        {  
 | 
            DesignDebug.Log(e.Message);  
 | 
        }  
 | 
    }  
 | 
  
 | 
    /// <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.Default);  
 | 
            CreateConfigData.CreateConfigClass(_tableNameLst[i].fileInfo);  
 | 
            sr.Dispose();  
 | 
            sr.Close();  
 | 
        }  
 | 
    }  
 | 
  
 | 
    /// <summary>  
 | 
    /// 路径缓存  
 | 
    /// </summary>  
 | 
    /// <param name="setPath"></param>  
 | 
    private static void PathCache(string 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;  
 | 
    }  
 | 
  
 | 
}  
 |