表格重构
This commit is contained in:
197
Assets/Scripts/Configs~/Editor/CfgEditorUtil.cs
Normal file
197
Assets/Scripts/Configs~/Editor/CfgEditorUtil.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public static class CfgEditorUtil
|
||||
{
|
||||
[MenuItem("构建/配置表/生成脚本")]
|
||||
public static void CreateScriptableObject()
|
||||
{
|
||||
EditorUtils.GetOrCreateAsset<ConfigAssets>(ConfigAssets.SavePath);
|
||||
GenConfigScripts();
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
|
||||
[MenuItem("构建/配置表/导表")]
|
||||
public static void BuildExcel()
|
||||
{
|
||||
ExcelToJsonWindow.GenConfig(false);
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
|
||||
[MenuItem("构建/配置表/导多语言")]
|
||||
public static void BuildLanguage()
|
||||
{
|
||||
ExcelToJsonWindow.GenLanguage();
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
|
||||
#region 生成脚本
|
||||
|
||||
private static string GenPath = "Scripts/Configs/Gen";
|
||||
private static string TempPath = "Scripts/Configs/Editor/ConfigWarpTemplate";
|
||||
|
||||
public static void GenConfigScripts()
|
||||
{
|
||||
if (!Directory.Exists($"{Application.dataPath}/{GenPath}"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var types = Reflection.GetAllNonAbstractDerivedTypes<ConfigBase>();
|
||||
Dictionary<Type, string> tableNameAttributes = new Dictionary<Type, string>();
|
||||
foreach (var type in types)
|
||||
{
|
||||
tableNameAttributes[type] = type.Name;
|
||||
}
|
||||
|
||||
// var canGen = CanGen(tableNameAttributes);
|
||||
//
|
||||
// if (!canGen) return;
|
||||
GenParse(tableNameAttributes);
|
||||
GenWarp(tableNameAttributes);
|
||||
// GenBinder(tableNameAttributes);
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
|
||||
private static bool CanGen(Dictionary<Type, string> tableNameAttributes)
|
||||
{
|
||||
// return true;
|
||||
string filePath = Path.Combine(Application.dataPath, $"{GenPath}/Warps");
|
||||
if (!Directory.Exists(filePath)) return true;
|
||||
var files = Directory.GetFiles(filePath);
|
||||
List<string> allFileName = new List<string>();
|
||||
foreach (var file in files)
|
||||
{
|
||||
if (Path.GetExtension(file).ToLower() == ".meta") continue;
|
||||
var fileName = Path.GetFileNameWithoutExtension(file);
|
||||
allFileName.Add(fileName.Replace("Warp", ""));
|
||||
}
|
||||
|
||||
if (allFileName.Count != tableNameAttributes.Count) return true;
|
||||
|
||||
foreach (var type in tableNameAttributes.Keys)
|
||||
{
|
||||
if (!allFileName.Contains(type.Name)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private static void GenWarp(Dictionary<Type, string> tableNameAttributes)
|
||||
{
|
||||
// 为何使用生成式,不使用static静态泛型 ? 生成式扩展更强且不会破坏原类的集成结构,父类也不用是泛型类
|
||||
//否则比如 BaseConfig<T> 类型来使用。集成结构会受很大限制,反而没有生成式来的灵活
|
||||
|
||||
string filePath = Path.Combine(Application.dataPath, TempPath);
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
// 读取文本内容
|
||||
string fileContent = File.ReadAllText(filePath);
|
||||
var rootPath = $"{Application.dataPath}/{GenPath}/Warps";
|
||||
if (!Directory.Exists(rootPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Directory.Exists(rootPath))
|
||||
{
|
||||
Directory.CreateDirectory(rootPath);
|
||||
}
|
||||
|
||||
foreach (var type in tableNameAttributes.Keys)
|
||||
{
|
||||
var content = fileContent.Replace("##NAME##", type.Name);
|
||||
File.WriteAllText($"{rootPath}/{type.Name}Warp.cs", content);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("生成代码模板不存在,请检查");
|
||||
}
|
||||
}
|
||||
|
||||
private static void GenParse(Dictionary<Type, string> tableNameAttributes)
|
||||
{
|
||||
if (!Directory.Exists($"{Application.dataPath}/{GenPath}"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CodeWriter codeWriter = new CodeWriter();
|
||||
codeWriter.Writeln("/**本脚本为自动生成,每次生成会覆盖!请勿手动修改**/");
|
||||
codeWriter.Writeln();
|
||||
codeWriter.Writeln("using System;");
|
||||
codeWriter.Writeln("using System.Collections.Generic;");
|
||||
codeWriter.Writeln("using System.Reflection;");
|
||||
codeWriter.Writeln("using Newtonsoft.Json.Linq;");
|
||||
codeWriter.Writeln("using UnityEngine;");
|
||||
|
||||
codeWriter.Writeln();
|
||||
codeWriter.Writeln("namespace NBF");
|
||||
|
||||
codeWriter.StartBlock();
|
||||
|
||||
codeWriter.Writeln("public partial class ConfigAssets");
|
||||
codeWriter.StartBlock();
|
||||
|
||||
foreach (var type in tableNameAttributes.Keys)
|
||||
{
|
||||
// codeWriter.Writeln($"[HideInInspector] public List<{type.Name}> {type.Name}Arr;");
|
||||
codeWriter.Writeln($"public List<{type.Name}> {type.Name}Arr;");
|
||||
}
|
||||
|
||||
codeWriter.Writeln();
|
||||
|
||||
codeWriter.Writeln("public void Parse(JToken[] arr, Type type)");
|
||||
codeWriter.StartBlock();
|
||||
codeWriter.Writeln("var tableNameAttribute = type.GetCustomAttribute<TableNameAttribute>();");
|
||||
codeWriter.Writeln("if (tableNameAttribute == null) return;");
|
||||
|
||||
foreach (var type in tableNameAttributes.Keys)
|
||||
{
|
||||
codeWriter.Writeln($"if (type == typeof({type.Name}))");
|
||||
codeWriter.StartBlock();
|
||||
codeWriter.Writeln($"{type.Name}Arr = ParseLine<{type.Name}>(arr, tableNameAttribute);");
|
||||
codeWriter.EndBlock();
|
||||
codeWriter.Writeln();
|
||||
}
|
||||
|
||||
codeWriter.EndBlock();
|
||||
|
||||
codeWriter.Writeln();
|
||||
codeWriter.Writeln("public void AssociationContexts()");
|
||||
codeWriter.StartBlock();
|
||||
|
||||
foreach (var type in tableNameAttributes.Keys)
|
||||
{
|
||||
codeWriter.Writeln($"new ConfigContext<{type.Name}>().Association({type.Name}Arr);");
|
||||
}
|
||||
|
||||
codeWriter.EndBlock();
|
||||
|
||||
|
||||
codeWriter.EndBlock();
|
||||
codeWriter.EndBlock();
|
||||
|
||||
codeWriter.Save($"{Application.dataPath}/{GenPath}/ConfigAssets.Gen.cs");
|
||||
}
|
||||
|
||||
|
||||
private static void WriterCreateParse(CodeWriter codeWriter, Type type)
|
||||
{
|
||||
codeWriter.Writeln($"if (type == typeof({type.Name}))");
|
||||
codeWriter.StartBlock();
|
||||
codeWriter.Writeln($"return CreateParseTableTask<{type.Name}>();");
|
||||
codeWriter.EndBlock();
|
||||
codeWriter.Writeln();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Configs~/Editor/CfgEditorUtil.cs.meta
Normal file
3
Assets/Scripts/Configs~/Editor/CfgEditorUtil.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5bc0ac74b225496da64195b28ce14cb2
|
||||
timeCreated: 1742998279
|
||||
105
Assets/Scripts/Configs~/Editor/CodeWriter.cs
Normal file
105
Assets/Scripts/Configs~/Editor/CodeWriter.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public interface ICodeWriterConfig
|
||||
{
|
||||
string BlockStart { get; set; }
|
||||
string BlockEnd { get; set; }
|
||||
bool BlockFromNewLine { get; set; }
|
||||
bool UsingTabs { get; set; }
|
||||
string EndOfLine { get; set; }
|
||||
}
|
||||
|
||||
public class DefCSharpCodeWriterConfig : ICodeWriterConfig
|
||||
{
|
||||
public string BlockStart { get; set; } = "{";
|
||||
public string BlockEnd { get; set; } = "}";
|
||||
public bool BlockFromNewLine { get; set; }
|
||||
public bool UsingTabs { get; set; } = true;
|
||||
public string EndOfLine { get; set; }
|
||||
}
|
||||
|
||||
public class CodeWriter
|
||||
{
|
||||
private ICodeWriterConfig _config;
|
||||
|
||||
private StringBuilder _stringBuilder = new StringBuilder();
|
||||
|
||||
private int _nowTabCount;
|
||||
|
||||
public CodeWriter()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public CodeWriter(ICodeWriterConfig config)
|
||||
{
|
||||
Init(config);
|
||||
}
|
||||
|
||||
public void Write(string content)
|
||||
{
|
||||
_stringBuilder.Append(content);
|
||||
}
|
||||
|
||||
public void Writeln()
|
||||
{
|
||||
_stringBuilder.Append(Environment.NewLine);
|
||||
}
|
||||
|
||||
public void Writeln(string str)
|
||||
{
|
||||
_stringBuilder.Append(GetLinePrefix());
|
||||
_stringBuilder.Append(str);
|
||||
_stringBuilder.Append(Environment.NewLine);
|
||||
}
|
||||
|
||||
public void StartBlock()
|
||||
{
|
||||
Writeln(_config.BlockStart);
|
||||
_nowTabCount++;
|
||||
}
|
||||
|
||||
public void EndBlock()
|
||||
{
|
||||
_nowTabCount--;
|
||||
Writeln(_config.BlockEnd);
|
||||
}
|
||||
|
||||
public void Save(string path)
|
||||
{
|
||||
var dirPath = Path.GetDirectoryName(path);
|
||||
if (dirPath != null && !Directory.Exists(dirPath))
|
||||
{
|
||||
Directory.CreateDirectory(dirPath);
|
||||
}
|
||||
|
||||
var content = _stringBuilder.ToString();
|
||||
File.WriteAllText(path, content);
|
||||
}
|
||||
|
||||
#region 内部方法
|
||||
|
||||
private void Init(ICodeWriterConfig config = null)
|
||||
{
|
||||
_config = config ?? new DefCSharpCodeWriterConfig();
|
||||
}
|
||||
|
||||
private string GetLinePrefix()
|
||||
{
|
||||
string ret = string.Empty;
|
||||
if (!_config.UsingTabs) return ret;
|
||||
for (var i = 0; i < _nowTabCount; i++)
|
||||
{
|
||||
ret += "\t";
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Configs~/Editor/CodeWriter.cs.meta
Normal file
2
Assets/Scripts/Configs~/Editor/CodeWriter.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf1ac73cd67e4bdc9de1a53a13f5b841
|
||||
65
Assets/Scripts/Configs~/Editor/ConfigWarpTemplate
Normal file
65
Assets/Scripts/Configs~/Editor/ConfigWarpTemplate
Normal file
@@ -0,0 +1,65 @@
|
||||
/**本脚本为自动生成,每次生成会覆盖!请勿手动修改**/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
[Serializable]
|
||||
public partial class ##NAME##
|
||||
{
|
||||
private static ConfigContext<##NAME##> _context;
|
||||
|
||||
private static ConfigContext<##NAME##> Context => _context ??= Configs.Table<##NAME##>();
|
||||
|
||||
public static ##NAME## Get(int key)
|
||||
{
|
||||
return Context.Get(key);
|
||||
}
|
||||
|
||||
public static ##NAME## Get(Predicate<##NAME##> match)
|
||||
{
|
||||
return Context.Get(match);
|
||||
}
|
||||
|
||||
public static ##NAME## Fist()
|
||||
{
|
||||
return Context.Fist();
|
||||
}
|
||||
|
||||
public static ##NAME## Last()
|
||||
{
|
||||
return Context.Last();
|
||||
}
|
||||
|
||||
public static ##NAME## Fist(Predicate<##NAME##> match)
|
||||
{
|
||||
return Context.Fist(match);
|
||||
}
|
||||
|
||||
public static ##NAME## Last(Predicate<##NAME##> match)
|
||||
{
|
||||
return Context.Last(match);
|
||||
}
|
||||
|
||||
public static int Count()
|
||||
{
|
||||
return Context.Count();
|
||||
}
|
||||
|
||||
public static int Count(Func<##NAME##, bool> predicate)
|
||||
{
|
||||
return Context.Count(predicate);
|
||||
}
|
||||
|
||||
|
||||
public static List<##NAME##> GetList()
|
||||
{
|
||||
return Context.GetList();
|
||||
}
|
||||
|
||||
public static List<##NAME##> GetList(Predicate<##NAME##> match)
|
||||
{
|
||||
return Context.GetList(match);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Assets/Scripts/Configs~/Editor/ConfigWarpTemplate.meta
Normal file
7
Assets/Scripts/Configs~/Editor/ConfigWarpTemplate.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b77eee728204f26bc602016c4a20c86
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user