Skip to content

Commit 70f37d6

Browse files
committed
更新第二版
1 parent f68f6d0 commit 70f37d6

File tree

576 files changed

+1247
-710
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

576 files changed

+1247
-710
lines changed
6 KB
Binary file not shown.

ExcelToByteFile/Config/GlobalConfig.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ private GlobalConfig() { }
2727
/// </summary>
2828
public string codeFileOutputDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
2929

30+
public string structOutputDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
31+
3032
/// <summary>
3133
/// 是否自动补全
3234
/// </summary>
@@ -57,6 +59,14 @@ private GlobalConfig() { }
5759
/// </summary>
5860
public bool onlyOneSheet = true;
5961

62+
public bool firstColIsPrimary = true;
63+
64+
public bool customExportSheetPrefix = false;
65+
66+
public string customSheetPrefix = "s_";
67+
68+
public bool generateStructInfoCs = true;
69+
6070
/// <summary>
6171
/// 存储在本地的配置文件名称
6272
/// </summary>
@@ -104,6 +114,16 @@ public void ReadConfig()
104114
onlyOneSheet = str == "True";
105115
str = sr.ReadLine();
106116
idColName = str;
117+
str = sr.ReadLine();
118+
firstColIsPrimary = str == "True";
119+
str = sr.ReadLine();
120+
customExportSheetPrefix = str == "True";
121+
str = sr.ReadLine();
122+
customSheetPrefix = str;
123+
str = sr.ReadLine();
124+
generateStructInfoCs = str == "True";
125+
str = sr.ReadLine();
126+
structOutputDir = str;
107127

108128
sr.Dispose();
109129
sr.Close();
@@ -147,6 +167,11 @@ public void SaveConfig()
147167
sw.WriteLine(commentInFirstRow.ToString());
148168
sw.WriteLine(onlyOneSheet.ToString());
149169
sw.WriteLine(idColName);
170+
sw.WriteLine(firstColIsPrimary.ToString());
171+
sw.WriteLine(customExportSheetPrefix.ToString());
172+
sw.WriteLine(customSheetPrefix);
173+
sw.WriteLine(generateStructInfoCs.ToString());
174+
sw.WriteLine(structOutputDir);
150175

151176
sw.Flush();
152177
sw.Dispose();

ExcelToByteFile/Data/ExcelData.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public bool Load()
8383
for (int i = 0; i < len; i++)
8484
{
8585
ISheet sheet = _workbook.GetSheetAt(i);
86+
if (GlobalConfig.Ins.customExportSheetPrefix)
87+
{
88+
if (!sheet.SheetName.StartsWith(GlobalConfig.Ins.customSheetPrefix))
89+
continue;
90+
}
8691
SheetData sheetData = new SheetData(_workbook, sheet, _evaluator, ExcelName);
8792
sheetData.Load();
8893
sheetDataList.Add(sheetData);

ExcelToByteFile/Data/FileInfoData.cs

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class FileInfoData
3232

3333
public int IdColIndex { get; }
3434

35+
public string PrimaryColCsType { get; }
36+
3537
/// <summary>
3638
/// sheet里每列变量的偏移(相对行首)
3739
/// </summary>
@@ -62,7 +64,8 @@ public FileInfoData(SheetData data)
6264
Tokens = GetTypeToken(data.heads);
6365
VariableNames = GetVariable(data.heads);
6466
Comments = GetComment(data.heads);
65-
IdColIndex = VariableNames.FindIndex((x) => x == GlobalConfig.Ins.idColName);
67+
IdColIndex = GlobalConfig.Ins.firstColIsPrimary ? 0 : VariableNames.FindIndex((x) => x == GlobalConfig.Ins.idColName);
68+
PrimaryColCsType = GetCsType(IdColIndex);
6669
}
6770

6871
/// <summary>
@@ -135,7 +138,7 @@ private List<int> GetTypeToken(List<HeadData> data)
135138
{
136139
int baseToken = GetTypeToken(head.MainType);
137140
int dimen = int.Parse(head.SubType[0]);
138-
int valToken = GetTypeToken(head.SubType[1]);
141+
int valToken = string.Empty == head.SubType[1] ? 0 : GetTypeToken(head.SubType[1]);
139142
ls.Add(baseToken + dimen * 100 + valToken);
140143
}
141144
else if (head.MainType == TypeDefine.dictType)
@@ -193,15 +196,17 @@ private int GetTypeToken(string type)
193196
}
194197
}
195198

196-
public string GetTypeByToken(int token)
199+
public string GetCommentTypeByToken(int token)
197200
{
198201
if (token >= (int)TypeToken.Vector)
199202
{
200203
int dimen = (token - 20000) / 100;
201204
int valToken = (token - 20000) % 100;
202-
return "Vector" + (dimen).ToString() + ((TypeToken)valToken).ToString() ;
205+
string type = string.Empty;
206+
if (valToken != 0) type = ((TypeToken)valToken).ToString();
207+
return "Vector" + (dimen).ToString() + type;
203208
}
204-
if (token >= (int)TypeToken.Dictionary)
209+
else if (token >= (int)TypeToken.Dictionary)
205210
{
206211
int keyToken = (token - 10000) / 100;
207212
int valToken = (token - 10000) % 100;
@@ -218,5 +223,76 @@ public string GetTypeByToken(int token)
218223
return ((TypeToken)token).ToString();
219224
}
220225
}
226+
227+
public string GetCsType(int index)
228+
{
229+
int type = Tokens[index];
230+
if (type >= (int)TypeToken.Vector)
231+
{
232+
int dimen = (type - 20000) / 100;
233+
int valToken = (type - 20000) % 100;
234+
string s = "Vector" + (dimen).ToString();
235+
if (valToken != 0) s += "Int";
236+
return s;
237+
}
238+
else if (type >= (int)TypeToken.Dictionary)
239+
{
240+
int keyToken = (type - 10000) / 100;
241+
int valToken = (type - 10000) % 100;
242+
return "Dictionary<" + ((TypeToken)keyToken).ToString().ToLower() + " ,"
243+
+ ((TypeToken)valToken).ToString().ToLower() + ">";
244+
}
245+
else if (type >= (int)TypeToken.List)
246+
{
247+
int subToken = type - 100;
248+
return "List<" + ((TypeToken)subToken).ToString().ToLower() + ">";
249+
}
250+
else
251+
{
252+
return ((TypeToken)type).ToString().ToLower();
253+
}
254+
}
255+
256+
public string GetPropertyStr(int index, string varName)
257+
{
258+
string csType = GetCsType(index);
259+
int type = Tokens[index];
260+
int off = (index << 16) + ColOffset[index];
261+
if (type >= (int)TypeToken.Vector)
262+
{
263+
int dimen = (type - 20000) / 100;
264+
int valToken = (type - 20000) % 100;
265+
return "public " + csType + " " + varName +
266+
" { get { return ExcelDataAccess.Get" + csType + "<" +
267+
PrimaryColCsType +
268+
">(ExcelName." + FileName + ", primaryColVal, " + off.ToString() + "); } }";
269+
}
270+
else if (type >= (int)TypeToken.Dictionary)
271+
{
272+
int keyToken = (type - 10000) / 100;
273+
int valToken = (type - 10000) % 100;
274+
string keyType = ((TypeToken)keyToken).ToString().ToLower();
275+
string valType = ((TypeToken)valToken).ToString().ToLower();
276+
return "public Dictionary<" + keyType + ", " + valType + "> " + varName +
277+
" { get { return ExcelDataAccess.GetDict<" +
278+
keyType + ", " + valType + ", " + PrimaryColCsType +
279+
">(ExcelName." + FileName + ", primaryColVal, " + off.ToString() + "); } }";
280+
}
281+
else if (type >= (int)TypeToken.List)
282+
{
283+
int subToken = type - 100;
284+
return "public " + csType + " " + varName +
285+
" { get { return ExcelDataAccess.GetList<" +
286+
((TypeToken)subToken).ToString().ToLower() + ", " + PrimaryColCsType +
287+
">(ExcelName." + FileName + ", primaryColVal, " + off.ToString() + "); } }";
288+
}
289+
else
290+
{
291+
return "public " + csType + " " + varName +
292+
" { get { return ExcelDataAccess.Get<" +
293+
csType + ", " + PrimaryColCsType +
294+
">(ExcelName." + FileName + ", primaryColVal, " + off.ToString() + "); } }";
295+
}
296+
}
221297
}
222298
}

ExcelToByteFile/Data/SheetData.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ public class SheetData
5959
public SheetData(IWorkbook workbook, ISheet sheet, XSSFFormulaEvaluator evaluator, string excelName)
6060
{
6161
ExcelName = excelName;
62-
SheetName = sheet.SheetName;
62+
if (GlobalConfig.Ins.customExportSheetPrefix)
63+
{
64+
int start = GlobalConfig.Ins.customSheetPrefix.Length;
65+
SheetName = sheet.SheetName.Substring(start);
66+
}
67+
else SheetName = sheet.SheetName;
6368
_workbook = workbook;
6469
_sheet = sheet;
6570
_evaluator = evaluator;
@@ -98,6 +103,15 @@ public void Load()
98103
commentRow = _sheet.GetRow(curRow++); //备注: 可能为空
99104
}
100105
int endCol = typeRow.LastCellNum;
106+
107+
if (GlobalConfig.Ins.firstColIsPrimary)
108+
{
109+
ICell typeCell = typeRow.GetCell(typeRow.FirstCellNum);
110+
string idType = ExcelTool.GetCellValue(typeCell, _evaluator).Trim().Replace(" ", "").ToLower();
111+
if (!DataTypeHelper.IsBaseType(idType))
112+
Log.LogError($"{ExcelName}_{SheetName} 主列类型不能为{idType}");
113+
}
114+
101115
// 获取数据类型信息
102116
for (int index = typeRow.FirstCellNum; index < endCol; index++)
103117
{
@@ -106,7 +120,7 @@ public void Load()
106120
ICell commentCell = commentRow?.GetCell(index);
107121

108122
// 检测重复的列
109-
string type = ExcelTool.GetCellValue(typeCell, _evaluator).Trim().ToLower();
123+
string type = ExcelTool.GetCellValue(typeCell, _evaluator).Trim().Replace(" ","").ToLower();
110124
string name = ExcelTool.GetCellValue(nameCell, _evaluator).Trim();
111125
string comment = ExcelTool.GetCellValue(commentCell, _evaluator);
112126
bool isNotesCol = (type.Contains(ConstDefine.noteChar))
@@ -137,7 +151,7 @@ public void Load()
137151
}
138152

139153
// 如果没有ID列
140-
if (!IsNameExist(GlobalConfig.Ins.idColName))
154+
if (!GlobalConfig.Ins.firstColIsPrimary && !IsNameExist(GlobalConfig.Ins.idColName))
141155
{
142156
Log.LogError($"{ExcelName}_{SheetName}表格必须设立一个 'id' 列.");
143157
}

ExcelToByteFile/Export/ExportMgr.cs

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public static void Export(List<string> fileList)
5656
ExportManifest(fileDatas);
5757
// 导出cs定义文件
5858
ExportExcelDefineCSCode(GlobalConfig.Ins.codeFileOutputDir, fileDatas);
59+
// 导出数据结构信息文件
60+
ExportStructInfoCsCode(GlobalConfig.Ins.structOutputDir, fileDatas);
5961
}
6062

6163
public static void ExportOneSheet(string path, SheetData sheet)
@@ -120,34 +122,35 @@ private static void ExportManifest(List<FileInfoData> fileDatas)
120122

121123
private static void ExportExcelDefineCSCode(string path, List<FileInfoData> data)
122124
{
123-
Log.LogConsole("正在生成 ExcelDefine.cs ...");
125+
Log.LogConsole("正在生成 ExcelVariableDefine.cs ...");
124126
using (StreamWriter sw = new StreamWriter(
125-
path + Path.DirectorySeparatorChar + "ExcelDefine" + ".cs",
127+
path + Path.DirectorySeparatorChar + "ExcelVariableDefine" + ".cs",
126128
false,
127129
new UTF8Encoding(false)))
128130
{
129131
StringBuilder sb1 = new StringBuilder(); // 类变量
130132
StringBuilder sb2 = new StringBuilder(); // 文件枚举
131-
sb1.AppendLine(@"public sealed class ExcelVariableDef");
132-
sb1.AppendLine(@"{");
133+
//sb1.AppendLine(@"public sealed class ExcelVariableDef");
134+
//sb1.AppendLine(@"{");
133135
sb2.AppendLine(@"public enum ExcelName");
134136
sb2.AppendLine(@"{");
135137
for (int i = 0; i < data.Count; i++)
136138
{
137139
FileInfoData info = data[i];
138-
sb1.Append(@" public sealed class ");
140+
string fileName = "EVD_" + info.FileName;
141+
sb1.Append(@"public static class EVD_");
139142
sb1.AppendLine(info.FileName);
140-
sb1.AppendLine(@" {");
141-
sb2.Append(@" ///<summary>");
143+
sb1.AppendLine(@"{");
144+
sb2.Append(@" ///<summary>");
142145
sb2.Append($"主列: " + info.VariableNames[info.IdColIndex] +
143-
$" [{info.GetTypeByToken(info.Tokens[info.IdColIndex])}]");
146+
$" [{info.GetCommentTypeByToken(info.Tokens[info.IdColIndex])}]");
144147
sb2.AppendLine(@"</summary>");
145-
sb2.AppendLine(@" " + info.FileName + @" = " + i.ToString() + @",");
148+
sb2.AppendLine(@" " + info.FileName + @" = " + i.ToString() + @",");
146149
for (int j = 0; j < info.VariableNames.Count; j++)
147150
{
148151
string varName = info.VariableNames[j];
149152
string raw = varName;
150-
if (varName == info.FileName)
153+
if (varName == fileName)
151154
{
152155
bool b = true;
153156
int a = 0;
@@ -161,23 +164,77 @@ private static void ExportExcelDefineCSCode(string path, List<FileInfoData> data
161164
else a++;
162165
}
163166
}
164-
sb1.Append(@" /// <summary>");
165-
string type = info.GetTypeByToken(info.Tokens[j]);
167+
sb1.Append(@" /// <summary>");
168+
string type = info.GetCommentTypeByToken(info.Tokens[j]);
166169
sb1.Append($"[{type}] " + info.Comments[j]);
167170
sb1.AppendLine(@"</summary>");
168-
sb1.Append(@" public const int ");
171+
sb1.Append(@" public const int ");
169172
int val = (j << 16) + info.ColOffset[j];
170173
sb1.AppendLine(varName + @" = " + val.ToString() + ";");
171174
}
172-
sb1.AppendLine(@" }");
175+
sb1.AppendLine(@"}");
173176
}
174-
sb1.AppendLine(@"}");
177+
//sb1.AppendLine(@"}");
175178
sb2.Append(@"}");
176179
sw.Write(sb1.ToString());
177180
sw.Write(sb2.ToString());
178181
}
179182
}
180183

184+
private static void ExportStructInfoCsCode(string path, List<FileInfoData> data)
185+
{
186+
Log.LogConsole("正在生成 ExcelDataStruct.cs ...");
187+
using (StreamWriter sw = new StreamWriter(
188+
path + Path.DirectorySeparatorChar + "ExcelDataStruct" + ".cs",
189+
false,
190+
new UTF8Encoding(false)))
191+
{
192+
StringBuilder sb = new StringBuilder();
193+
sb.AppendLine(@"using System.Collections.Generic;");
194+
sb.AppendLine(@"using UnityEngine;");
195+
sb.AppendLine();
196+
197+
for (int i = 0; i < data.Count; i++)
198+
{
199+
FileInfoData info = data[i];
200+
string fileName = "EDS_" + info.FileName;
201+
string idType = info.GetCsType(info.IdColIndex);
202+
sb.AppendLine(@"public struct " + fileName);
203+
sb.AppendLine(@"{");
204+
sb.AppendLine(@" " + idType + @" primaryColVal;");
205+
206+
sb.AppendLine(@" public " + fileName + @"(" + idType
207+
+ @" val) { this.primaryColVal = val; }");
208+
for (int j = 0; j < info.VariableNames.Count; j++)
209+
{
210+
string varName = info.VariableNames[j];
211+
string raw = varName;
212+
string csType = info.GetCsType(j);
213+
if (varName == fileName)
214+
{
215+
bool b = true;
216+
int a = 0;
217+
while (b)
218+
{
219+
varName = raw + "_c_" + a.ToString();
220+
if (!info.VariableNames.Contains(varName))
221+
{
222+
b = false;
223+
}
224+
else a++;
225+
}
226+
}
227+
sb.Append(@" /// <summary>");
228+
sb.Append(info.Comments[j]);
229+
sb.AppendLine(@"</summary>");
230+
sb.AppendLine(" " + info.GetPropertyStr(j, varName));
231+
}
232+
sb.AppendLine(@"}");
233+
}
234+
sw.Write(sb.ToString());
235+
}
236+
}
237+
181238
private static void WriteCell(ByteWriteBuffer buffer, HeadData head, string value)
182239
{
183240
switch (head.MainType)

0 commit comments

Comments
 (0)