From 54fad068f41ba7b0d2f16699a3f774be2a0d84e9 Mon Sep 17 00:00:00 2001
From: client_Wu Xijin <364452445@qq.com>
Date: 星期四, 14 二月 2019 12:02:50 +0800
Subject: [PATCH] 3335 配置表读取重构。

---
 Assets/Editor/Tool/CreateLuaClassFile.cs |  186 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 184 insertions(+), 2 deletions(-)

diff --git a/Assets/Editor/Tool/CreateLuaClassFile.cs b/Assets/Editor/Tool/CreateLuaClassFile.cs
index fc36d05..d0ae8ef 100644
--- a/Assets/Editor/Tool/CreateLuaClassFile.cs
+++ b/Assets/Editor/Tool/CreateLuaClassFile.cs
@@ -8,7 +8,7 @@
 using System.Text;
 using System.Text.RegularExpressions;
 
-public class CreateLuaClassFile
+public class CreateLuaWindowClassFile
 {
     static string templatePath = "Assets/Editor/ScriptTemplate/LuaWindowTemplate.txt";
 
@@ -68,4 +68,186 @@
         return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
     }
 
-}
\ No newline at end of file
+}
+
+
+public class CreateLuaBehaviourClassFile
+{
+    static string templatePath = "Assets/Editor/ScriptTemplate/LuaBehaviourTemplate.txt";
+
+    [MenuItem("Assets/Create/Lua/LuaBehaviour", false, 4)]
+    public static void CreateLuaClass()
+    {
+        var path = GetSelectedPathOrFallback() + "/NewBehaviour.lua";
+        AssetDatabase.DeleteAsset(path);
+        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<LuaBehaviourTemplate>(), path, null, templatePath);
+    }
+
+    public static string GetSelectedPathOrFallback()
+    {
+        string path = "Assets";
+        foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
+        {
+            path = AssetDatabase.GetAssetPath(obj);
+            if (!string.IsNullOrEmpty(path) && File.Exists(path))
+            {
+                path = Path.GetDirectoryName(path);
+                break;
+            }
+        }
+        return path;
+    }
+
+}
+
+class LuaBehaviourTemplate : EndNameEditAction
+{
+
+    public override void Action(int instanceId, string pathName, string resourceFile)
+    {
+        UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);
+        ProjectWindowUtil.ShowCreatedAsset(o);
+    }
+
+    internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
+    {
+        string fullPath = Path.GetFullPath(pathName);
+
+        StreamReader streamReader = new StreamReader(resourceFile);
+        string text = streamReader.ReadToEnd();
+        streamReader.Close();
+        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
+        text = Regex.Replace(text, "#ClassName#", fileNameWithoutExtension);
+        text = Regex.Replace(text, "#DateTime#", System.DateTime.Now.ToLongDateString());
+
+        bool encoderShouldEmitUTF8Identifier = true;
+        bool throwOnInvalidBytes = false;
+        UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
+        bool append = false;
+        StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
+        streamWriter.Write(text);
+        streamWriter.Close();
+        AssetDatabase.ImportAsset(pathName);
+        return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
+    }
+
+}
+
+
+
+public class CreateLuaConfigClassFile
+{
+    static string templatePath = "Assets/Editor/ScriptTemplate/LuaConfigTemplate.txt";
+    public static string readContent = string.Empty;
+
+    [MenuItem("Assets/鐢熸垚Lua閰嶇疆瑙f瀽绫诲瀷")]
+    public static void CreateLuaClass()
+    {
+        if (Selection.objects != null)
+        {
+            foreach (var item in Selection.objects)
+            {
+                var name = item.name;
+                MakeReadContent(Application.dataPath.Replace("Assets", "") + AssetDatabase.GetAssetPath(item));
+                var path = string.Format("Assets/ResourcesOut/Lua/config/{0}Config.lua", name);
+                AssetDatabase.DeleteAsset(path);
+                ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
+                    ScriptableObject.CreateInstance<LuaConfigTemplate>(), path, null, templatePath);
+            }
+        }
+    }
+
+
+    public static void MakeReadContent(string path)
+    {
+        var fileInfo = new FileInfo(path);
+        var lines = File.ReadAllLines(fileInfo.FullName);
+        if (lines.Length > 2)
+        {
+            var typeLine = lines[0];
+            var fieldLine = lines[1];
+            var types = typeLine.Split('\t');
+            var fields = fieldLine.Split('\t');
+            var min = Mathf.Min(types.Length, fields.Length);
+            var fieldFulls = new List<string>();
+            var readFulls = new List<string>();
+
+            int index = 0;
+            for (int j = 0; j < min; j++)
+            {
+                var type = types[j];
+                var field = fields[j];
+                var readString = GetRead(type, field, index + 1);
+                if (!string.IsNullOrEmpty(readString))
+                {
+                    index++;
+                    readFulls.Add(readString);
+                }
+            }
+
+            readContent = string.Join("\r\n\t\t", readFulls.ToArray());
+        }
+    }
+
+
+    static string GetRead(string _type, string _field, int _index)
+    {
+        _field = _field.Replace(" ", "");
+        if (_type.Contains("int[]") || _type.Contains("float[]"))
+        {
+            return string.Format("subTable.{0} = stringArray_to_numberArray(Split(contents[{1}], '|'))", _field, _index);
+        }
+        else if (_type.Contains("string[]"))
+        {
+            return string.Format("subTable.{0} = Split(contents[{1}], '|')", _field, _index);
+        }
+        else if (_type.Contains("int") || _type.Contains("float"))
+        {
+            return string.Format("subTable.{0} = tonumber(contents[{1}])", _field, _index);
+        }
+        else if (_type.Contains("string"))
+        {
+            return string.Format("subTable.{0} = contents[{1}]", _field, _index);
+        }
+        else
+        {
+            return string.Empty;
+        }
+    }
+
+}
+
+class LuaConfigTemplate : EndNameEditAction
+{
+
+    public override void Action(int instanceId, string pathName, string resourceFile)
+    {
+        UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);
+        ProjectWindowUtil.ShowCreatedAsset(o);
+    }
+
+    internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
+    {
+        string fullPath = Path.GetFullPath(pathName);
+
+        StreamReader streamReader = new StreamReader(resourceFile);
+        string text = streamReader.ReadToEnd();
+        streamReader.Close();
+        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
+        text = Regex.Replace(text, "#ClassName#", fileNameWithoutExtension.Substring(0, fileNameWithoutExtension.Length - 6));
+        text = Regex.Replace(text, "#DateTime#", System.DateTime.Now.ToLongDateString());
+        text = Regex.Replace(text, "#Read#", CreateLuaConfigClassFile.readContent);
+
+        bool encoderShouldEmitUTF8Identifier = true;
+        bool throwOnInvalidBytes = false;
+        UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
+        bool append = false;
+        StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
+        streamWriter.Write(text);
+        streamWriter.Close();
+        AssetDatabase.ImportAsset(pathName);
+        return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
+    }
+
+}
+

--
Gitblit v1.8.0