34 changed files with 195 additions and 320 deletions
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 5cd35fec940c4cb89f79b6c66901008e |
||||
timeCreated: 1735992114 |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 0fa6f94e2e704e0faeac90ef8e185f2f |
||||
timeCreated: 1735992174 |
@ -1,31 +0,0 @@
|
||||
using System.Collections.Generic; |
||||
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public abstract class CodeScope : ICodeScope |
||||
{ |
||||
public bool Semicolon { get; set; } |
||||
|
||||
public virtual void Gen(ICodeWriter writer) |
||||
{ |
||||
GenFirstLine(writer); |
||||
|
||||
new OpenBraceCode().Gen(writer); |
||||
|
||||
writer.IndentCount++; |
||||
|
||||
foreach (var code in Codes) |
||||
{ |
||||
code.Gen(writer); |
||||
} |
||||
|
||||
writer.IndentCount--; |
||||
|
||||
new CloseBraceCode(Semicolon).Gen(writer); |
||||
} |
||||
|
||||
protected abstract void GenFirstLine(ICodeWriter writer); |
||||
|
||||
public List<ICode> Codes { get; } = new(); |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 737f0286aef24e158bf957af85f06c15 |
||||
timeCreated: 1735992259 |
@ -1,7 +0,0 @@
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public interface ICode |
||||
{ |
||||
void Gen(ICodeWriter writer); |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 5b930fc6d1d247949af8177d796c4265 |
||||
timeCreated: 1735992194 |
@ -1,9 +0,0 @@
|
||||
using System.Collections.Generic; |
||||
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public interface ICodeScope : ICode |
||||
{ |
||||
List<ICode> Codes { get; } |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 249dc2e076114187958bc580f2455fec |
||||
timeCreated: 1735992236 |
@ -1,8 +0,0 @@
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public interface ICodeWriter |
||||
{ |
||||
int IndentCount { get; set; } |
||||
void WriteLine(string code = null); |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 96536dd301a640eba7ff5c8d8a597663 |
||||
timeCreated: 1735992216 |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 2362665fe861427fb3d22bfc213e7543 |
||||
timeCreated: 1735992283 |
@ -1,43 +0,0 @@
|
||||
using System; |
||||
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public sealed class ClassScope : CodeScope |
||||
{ |
||||
private readonly bool _isPartial; |
||||
private readonly bool _isStatic; |
||||
private readonly bool _isSealed; |
||||
private readonly string _parentClassName; |
||||
private readonly string _className; |
||||
|
||||
public bool IsStatic => _isStatic; |
||||
|
||||
public ClassScope(bool isPartial, bool isStatic, bool isSealed, string className, string parentClassName = "") |
||||
{ |
||||
_isPartial = isPartial; |
||||
_isStatic = isStatic; |
||||
_isSealed = isSealed; |
||||
_parentClassName = parentClassName; |
||||
_className = className; |
||||
} |
||||
|
||||
protected override void GenFirstLine(ICodeWriter writer) |
||||
{ |
||||
writer.WriteLine( |
||||
$"public {(_isSealed ? "sealed " : string.Empty)}{(_isStatic ? "static " : string.Empty)}{(_isPartial ? "partial " : string.Empty)}class {_className}{(string.IsNullOrEmpty(_parentClassName) ? string.Empty : " : " + _parentClassName)}"); |
||||
} |
||||
} |
||||
|
||||
public static partial class CodeScopeExtensions |
||||
{ |
||||
public static ICodeScope Class(this ICodeScope self, string className, bool isPartial, bool isStatic, |
||||
bool isSealed, |
||||
string parentClassName = "", Action<ClassScope> classScopeSetting = null) |
||||
{ |
||||
var classScope = new ClassScope(isPartial, isStatic, isSealed, className, parentClassName); |
||||
classScopeSetting?.Invoke(classScope); |
||||
self.Codes.Add(classScope); |
||||
return self; |
||||
} |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 87bdee7f495f44fdab681a299935d76e |
||||
timeCreated: 1735993249 |
@ -1,17 +0,0 @@
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public sealed class CloseBraceCode : ICode |
||||
{ |
||||
private readonly bool _semicolon; |
||||
|
||||
public CloseBraceCode(bool semicolon) |
||||
{ |
||||
_semicolon = semicolon; |
||||
} |
||||
|
||||
public void Gen(ICodeWriter writer) |
||||
{ |
||||
writer.WriteLine("}" + (_semicolon ? ";" : string.Empty)); |
||||
} |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 21a1d549d9fc4c5893e8b5454e43e5af |
||||
timeCreated: 1735992305 |
@ -1,26 +0,0 @@
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public sealed class CustomCode : ICode |
||||
{ |
||||
private readonly string _content; |
||||
|
||||
public CustomCode(string content) |
||||
{ |
||||
_content = content; |
||||
} |
||||
|
||||
public void Gen(ICodeWriter writer) |
||||
{ |
||||
writer.WriteLine(_content); |
||||
} |
||||
} |
||||
|
||||
public static partial class CodeScopeExtensions |
||||
{ |
||||
public static ICodeScope Custom(this ICodeScope self, string content) |
||||
{ |
||||
self.Codes.Add(new CustomCode(content)); |
||||
return self; |
||||
} |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: d6858c0550a54e8d9c5cf7065bf582a1 |
||||
timeCreated: 1735992474 |
@ -1,31 +0,0 @@
|
||||
using System; |
||||
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public sealed class NameSpaceScope : CodeScope |
||||
{ |
||||
private readonly string _nameSpace; |
||||
|
||||
public NameSpaceScope(string nameSpace) |
||||
{ |
||||
_nameSpace = nameSpace; |
||||
} |
||||
|
||||
protected override void GenFirstLine(ICodeWriter writer) |
||||
{ |
||||
writer.WriteLine($"namespace {_nameSpace}"); |
||||
} |
||||
} |
||||
|
||||
public static partial class CodeScopeExtensions |
||||
{ |
||||
public static ICodeScope NameSpace(this ICodeScope self, string nameSpace, |
||||
Action<NameSpaceScope> nameSpaceSetting = null) |
||||
{ |
||||
var nameSpaceCode = new NameSpaceScope(nameSpace); |
||||
nameSpaceSetting?.Invoke(nameSpaceCode); |
||||
self.Codes.Add(nameSpaceCode); |
||||
return self; |
||||
} |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 5fe126821e724abaaaf317cceac9d41b |
||||
timeCreated: 1735992680 |
@ -1,10 +0,0 @@
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public sealed class OpenBraceCode : ICode |
||||
{ |
||||
public void Gen(ICodeWriter writer) |
||||
{ |
||||
writer.WriteLine("{"); |
||||
} |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 3702874220a7472bb4f3f83980744f72 |
||||
timeCreated: 1735992289 |
@ -1,28 +0,0 @@
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public sealed class UsingCode : ICode |
||||
{ |
||||
private readonly string _nameSpace; |
||||
|
||||
public UsingCode(string nameSpace) |
||||
{ |
||||
_nameSpace = nameSpace; |
||||
} |
||||
|
||||
public void Gen(ICodeWriter writer) |
||||
{ |
||||
writer.WriteLine($"using {_nameSpace};"); |
||||
} |
||||
} |
||||
|
||||
|
||||
public static partial class CodeScopeExtensions |
||||
{ |
||||
public static ICodeScope Using(this ICodeScope self, string nameSpace) |
||||
{ |
||||
var code = new UsingCode(nameSpace); |
||||
self.Codes.Add(code); |
||||
return self; |
||||
} |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 299e6452df21426183f1622b56cfdd85 |
||||
timeCreated: 1735992567 |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 541182aef19445418568c1693eda34de |
||||
timeCreated: 1735992180 |
@ -1,17 +0,0 @@
|
||||
using System.Collections.Generic; |
||||
|
||||
namespace Tools.Editor.CodeGenKit |
||||
{ |
||||
public sealed class RootCode : ICodeScope |
||||
{ |
||||
public void Gen(ICodeWriter writer) |
||||
{ |
||||
foreach (var code in Codes) |
||||
{ |
||||
code.Gen(writer); |
||||
} |
||||
} |
||||
|
||||
public List<ICode> Codes { get; } = new(); |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 26b647313d594f05b7ca562f2b34ce56 |
||||
timeCreated: 1735992153 |
@ -0,0 +1,55 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Sirenix.Utilities; |
||||
|
||||
namespace Tools.ExcelResolver.Editor |
||||
{ |
||||
internal class ClassCodeData : IDisposable |
||||
{ |
||||
public TableType tableType; |
||||
public string className; |
||||
public FieldData[] fields; |
||||
public int[] keyIndex; |
||||
|
||||
public ClassCodeData(TableType tableType, string className) |
||||
{ |
||||
this.tableType = tableType; |
||||
this.className = $"{char.ToUpper(className[0])}{className.Substring(1)}"; |
||||
} |
||||
|
||||
public void Dispose() |
||||
{ |
||||
className = null; |
||||
fields.ForEach(field => field.Dispose()); |
||||
fields = null; |
||||
keyIndex = null; |
||||
} |
||||
} |
||||
|
||||
internal class FieldData : IDisposable |
||||
{ |
||||
public string name; |
||||
public Type type; |
||||
public string info; |
||||
public string description; |
||||
public string path; |
||||
|
||||
public void Dispose() |
||||
{ |
||||
name = null; |
||||
type = null; |
||||
info = null; |
||||
description = null; |
||||
path = null; |
||||
} |
||||
} |
||||
|
||||
internal enum TableType |
||||
{ |
||||
SingleKeyTable, // 单主键表 |
||||
UnionMultiKeyTable, // 多主键表(联合索引) |
||||
MultiKeyTable, // 多主键表(独立索引) |
||||
NotKetTable, // 无主键表 |
||||
ColumnTable, // 纵表 |
||||
} |
||||
} |
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2 |
||||
guid: aa78605805cf49e0aa0df50ccac8bb9d |
||||
timeCreated: 1736054922 |
@ -1,33 +1,62 @@
|
||||
using System; |
||||
using Tools.Editor.CodeGenKit; |
||||
using System.CodeDom; |
||||
|
||||
/* 使用CodeCompileUnit需要将Edit->Project Settings->Player->Other Settings->Api Compatibility Level改为.NET 4.x(或.NET Framework) */ |
||||
|
||||
|
||||
namespace Tools.ExcelResolver.Editor |
||||
{ |
||||
internal class FieldData |
||||
{ |
||||
public string name; |
||||
public Type type; |
||||
public string description; |
||||
|
||||
} |
||||
|
||||
public sealed partial class ExcelResolverEditorWindow |
||||
{ |
||||
private int[] keyIndex; |
||||
|
||||
private void WriteTypeCode() |
||||
private void WriteDataCode(ClassCodeData classCodeData) |
||||
{ |
||||
var code = new RootCode(); |
||||
code.Custom("// 代码使用工具生成,请勿随意修改"); |
||||
code.Using("System"); |
||||
code.Using("System.Collections.Generic"); |
||||
code.Using("System.Linq"); |
||||
|
||||
code.NameSpace(excelResolverConfig.GenerateDataClassNameSpace, cope => |
||||
string path = $"{excelResolverConfig.CodePathRoot}/{classCodeData.className}.cs"; |
||||
CodeCompileUnit compileUnit = new CodeCompileUnit(); |
||||
CodeNamespace codeNamespace = new CodeNamespace(excelResolverConfig.GenerateDataClassNameSpace); |
||||
compileUnit.Namespaces.Add(codeNamespace); |
||||
|
||||
string[] classImports = new string[] |
||||
{ |
||||
"System", |
||||
"System.Collections", |
||||
"System.Collections.Generic", |
||||
"UnityEngine", |
||||
}; |
||||
foreach (var import in classImports) |
||||
{ |
||||
cope.Custom("[Serializable]"); |
||||
// cope.Class($"") |
||||
}); |
||||
codeNamespace.Imports.Add(new CodeNamespaceImport(import)); |
||||
} |
||||
|
||||
CodeTypeDeclaration classType = new CodeTypeDeclaration(classCodeData.className) |
||||
{ |
||||
IsClass = true, |
||||
TypeAttributes = System.Reflection.TypeAttributes.Public, |
||||
CustomAttributes = new CodeAttributeDeclarationCollection() |
||||
{ |
||||
new CodeAttributeDeclaration("Serializable") |
||||
} |
||||
}; |
||||
|
||||
// switch (classCodeData.tableType) |
||||
// { |
||||
// case TableType.SingleKeyTable: |
||||
// classType.BaseTypes.Add(new CodeTypeReference("Dictionary<int, " + classCodeData.className + ">")); |
||||
// break; |
||||
// case TableType.UnionMultiKeyTable: |
||||
// classType.BaseTypes.Add(new CodeTypeReference("Dictionary<string, " + classCodeData.className + ">")); |
||||
// break; |
||||
// case TableType.MultiKeyTable: |
||||
// classType.BaseTypes.Add(new CodeTypeReference("Dictionary<int, " + classCodeData.className + ">")); |
||||
// break; |
||||
// case TableType.NotKetTable: |
||||
// classType.BaseTypes.Add(new CodeTypeReference("List<" + classCodeData.className + ">")); |
||||
// break; |
||||
// case TableType.ColumnTable: |
||||
// classType.BaseTypes.Add(new CodeTypeReference("Dictionary<int, " + classCodeData.className + ">")); |
||||
// break; |
||||
// default: |
||||
// throw new ArgumentOutOfRangeException(); |
||||
// } |
||||
} |
||||
} |
||||
} |
@ -1,7 +1,54 @@
|
||||
namespace Tools.ExcelResolver.Editor |
||||
using System.CodeDom; |
||||
using System.CodeDom.Compiler; |
||||
using System.IO; |
||||
using Microsoft.CSharp; |
||||
|
||||
namespace Tools.ExcelResolver.Editor |
||||
{ |
||||
public sealed partial class ExcelResolverEditorWindow |
||||
{ |
||||
|
||||
private void WriteSOCode(ClassCodeData classCodeData) |
||||
{ |
||||
string outputPath = $"{excelResolverConfig.CodePathRoot}/{classCodeData.className}.cs"; |
||||
CodeCompileUnit compileUnit = new CodeCompileUnit(); |
||||
CodeNamespace codeNamespace = new CodeNamespace(excelResolverConfig.GenerateDataClassNameSpace); |
||||
compileUnit.Namespaces.Add(codeNamespace); |
||||
|
||||
string[] classImports = new string[] |
||||
{ |
||||
"System", |
||||
"System.Collections", |
||||
"System.Collections.Generic", |
||||
"UnityEngine", |
||||
}; |
||||
foreach (var import in classImports) |
||||
{ |
||||
codeNamespace.Imports.Add(new CodeNamespaceImport(import)); |
||||
} |
||||
|
||||
CodeTypeDeclaration classType = new CodeTypeDeclaration(classCodeData.className) |
||||
{ |
||||
IsClass = true, |
||||
TypeAttributes = System.Reflection.TypeAttributes.Public, |
||||
BaseTypes = |
||||
{ |
||||
new CodeTypeReference("ScriptableObject") |
||||
} |
||||
}; |
||||
codeNamespace.Types.Add(classType); |
||||
|
||||
CodeGeneratorOptions options = new CodeGeneratorOptions |
||||
{ |
||||
BracingStyle = "C", |
||||
BlankLinesBetweenMembers = false, |
||||
VerbatimOrder = true, |
||||
}; |
||||
|
||||
using (StreamWriter writer = new StreamWriter(outputPath)) |
||||
{ |
||||
CSharpCodeProvider provider = new CSharpCodeProvider(); |
||||
provider.GenerateCodeFromCompileUnit(compileUnit, writer, options); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 0d117c752ed842e2a1350b2a36b0cc00 |
||||
timeCreated: 1736058450 |
Loading…
Reference in new issue