Skip to content

Commit 137b85b

Browse files
committed
fix: 修复7bit读取行数使用接口不一致导致Unity中加载配置表失败
1 parent 7199f5f commit 137b85b

9 files changed

Lines changed: 250 additions & 19 deletions

File tree

89 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

sandbox/ConsoleApp/Generated/DRDataTableSample.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,36 @@ public sealed partial class DRDataTableSample : DataRowBase
250250
/// <summary>数组</summary>
251251
public int[] ArrayValue { get; private set; }
252252

253+
/// <summary>
254+
/// 二维数组
255+
/// <para>使用标准JSON格式串</para>
256+
/// <para>备注1字符串</para>
257+
/// </summary>
258+
public int[][] Array2DValue { get; private set; }
259+
260+
/// <summary>一维字符串数组</summary>
261+
public string[] ArrayStringValue { get; private set; }
262+
263+
/// <summary>三维数组</summary>
264+
public int[][][] Array3DValue { get; private set; }
265+
266+
/// <summary>枚举与整形的字典</summary>
267+
public Dictionary<ColorT, int> MapEnumToInt { get; private set; }
268+
269+
/// <summary>枚举数组</summary>
270+
public ColorT[] EnumArray { get; private set; }
271+
272+
/// <summary>JSON类</summary>
273+
/// <remarks>
274+
/// 批注示例文本,请注意查阅!
275+
/// <para>&lt;color&gt;Red&lt;/color&gt;</para>
276+
/// <para>&lt;b&gt;abc&lt;/b&gt;</para>
277+
/// </remarks>
278+
public SampleParent CustomJSON { get; private set; }
279+
280+
/// <summary>自定义类</summary>
281+
public CustomSample CustomFieldType { get; private set; }
282+
253283
public override bool Deserialize(BinaryReader reader)
254284
{
255285
Id = reader.Read7BitEncodedInt32();
@@ -277,6 +307,105 @@ public override bool Deserialize(BinaryReader reader)
277307
ArrayValue[x1] = key1;
278308
}
279309
}
310+
{
311+
var __Array2DValue_Count1 = reader.Read7BitEncodedInt32();
312+
Array2DValue = new int[__Array2DValue_Count1][];
313+
for (int x1 = 0; x1 < __Array2DValue_Count1; x1++)
314+
{
315+
int[] key1;
316+
{
317+
var __key1_Count3 = reader.Read7BitEncodedInt32();
318+
key1 = new int[__key1_Count3];
319+
for (int x3 = 0; x3 < __key1_Count3; x3++)
320+
{
321+
int key3;
322+
key3 = reader.Read7BitEncodedInt32();
323+
key1[x3] = key3;
324+
}
325+
}
326+
Array2DValue[x1] = key1;
327+
}
328+
}
329+
{
330+
var __ArrayStringValue_Count1 = reader.Read7BitEncodedInt32();
331+
ArrayStringValue = new string[__ArrayStringValue_Count1];
332+
for (int x1 = 0; x1 < __ArrayStringValue_Count1; x1++)
333+
{
334+
string key1;
335+
key1 = reader.ReadString();
336+
ArrayStringValue[x1] = key1;
337+
}
338+
}
339+
{
340+
var __Array3DValue_Count1 = reader.Read7BitEncodedInt32();
341+
Array3DValue = new int[__Array3DValue_Count1][][];
342+
for (int x1 = 0; x1 < __Array3DValue_Count1; x1++)
343+
{
344+
int[][] key1;
345+
{
346+
var __key1_Count3 = reader.Read7BitEncodedInt32();
347+
key1 = new int[__key1_Count3][];
348+
for (int x3 = 0; x3 < __key1_Count3; x3++)
349+
{
350+
int[] key3;
351+
{
352+
var __key3_Count5 = reader.Read7BitEncodedInt32();
353+
key3 = new int[__key3_Count5];
354+
for (int x5 = 0; x5 < __key3_Count5; x5++)
355+
{
356+
int key5;
357+
key5 = reader.Read7BitEncodedInt32();
358+
key3[x5] = key5;
359+
}
360+
}
361+
key1[x3] = key3;
362+
}
363+
}
364+
Array3DValue[x1] = key1;
365+
}
366+
}
367+
{
368+
MapEnumToInt = new Dictionary<ColorT, int>();
369+
var __MapEnumToInt_Count1 = reader.Read7BitEncodedInt32();
370+
for (int x1 = 0; x1 < __MapEnumToInt_Count1; x1++)
371+
{
372+
ColorT key1;
373+
{
374+
ColorT __enumVal = default;
375+
var __enumStr = reader.ReadString();
376+
if (!string.IsNullOrEmpty(__enumStr) && !Enum.TryParse(__enumStr, out __enumVal))
377+
{
378+
throw new ArgumentException();
379+
}
380+
key1 = __enumVal;
381+
}
382+
int value1;
383+
value1 = reader.Read7BitEncodedInt32();
384+
MapEnumToInt.Add(key1, value1);
385+
}
386+
}
387+
{
388+
var __EnumArray_Count1 = reader.Read7BitEncodedInt32();
389+
EnumArray = new ColorT[__EnumArray_Count1];
390+
for (int x1 = 0; x1 < __EnumArray_Count1; x1++)
391+
{
392+
ColorT key1;
393+
{
394+
ColorT __enumVal = default;
395+
var __enumStr = reader.ReadString();
396+
if (!string.IsNullOrEmpty(__enumStr) && !Enum.TryParse(__enumStr, out __enumVal))
397+
{
398+
throw new ArgumentException();
399+
}
400+
key1 = __enumVal;
401+
}
402+
EnumArray[x1] = key1;
403+
}
404+
}
405+
{
406+
CustomJSON = reader.ReadJson<SampleParent>();
407+
}
408+
CustomFieldType = new CustomSample(reader.ReadString());
280409
return true;
281410
}
282411
}

sandbox/ConsoleApp/Generated/DRDataTableSplitSample.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ public sealed partial class DRDataTableSplitSample : DataRowBase
250250
/// <summary>数组</summary>
251251
public int[] ArrayValue { get; private set; }
252252

253+
/// <summary>
254+
/// 二维数组
255+
/// <para>使用标准JSON格式串</para>
256+
/// <para>备注1字符串</para>
257+
/// </summary>
258+
public int[][] Array2DValue { get; private set; }
259+
260+
/// <summary>三维数组</summary>
261+
public int[][][] Array3DValue { get; private set; }
262+
263+
/// <summary>枚举与整形的字典</summary>
264+
public Dictionary<ColorT, int> MapEnumToInt { get; private set; }
265+
266+
/// <summary>枚举数组</summary>
267+
public ColorT[] EnumArray { get; private set; }
268+
269+
/// <summary>自定义类</summary>
270+
public SampleParent CustomJSON { get; private set; }
271+
253272
public override bool Deserialize(BinaryReader reader)
254273
{
255274
Id = reader.Read7BitEncodedInt32();
@@ -277,6 +296,94 @@ public override bool Deserialize(BinaryReader reader)
277296
ArrayValue[x1] = key1;
278297
}
279298
}
299+
{
300+
var __Array2DValue_Count1 = reader.Read7BitEncodedInt32();
301+
Array2DValue = new int[__Array2DValue_Count1][];
302+
for (int x1 = 0; x1 < __Array2DValue_Count1; x1++)
303+
{
304+
int[] key1;
305+
{
306+
var __key1_Count3 = reader.Read7BitEncodedInt32();
307+
key1 = new int[__key1_Count3];
308+
for (int x3 = 0; x3 < __key1_Count3; x3++)
309+
{
310+
int key3;
311+
key3 = reader.Read7BitEncodedInt32();
312+
key1[x3] = key3;
313+
}
314+
}
315+
Array2DValue[x1] = key1;
316+
}
317+
}
318+
{
319+
var __Array3DValue_Count1 = reader.Read7BitEncodedInt32();
320+
Array3DValue = new int[__Array3DValue_Count1][][];
321+
for (int x1 = 0; x1 < __Array3DValue_Count1; x1++)
322+
{
323+
int[][] key1;
324+
{
325+
var __key1_Count3 = reader.Read7BitEncodedInt32();
326+
key1 = new int[__key1_Count3][];
327+
for (int x3 = 0; x3 < __key1_Count3; x3++)
328+
{
329+
int[] key3;
330+
{
331+
var __key3_Count5 = reader.Read7BitEncodedInt32();
332+
key3 = new int[__key3_Count5];
333+
for (int x5 = 0; x5 < __key3_Count5; x5++)
334+
{
335+
int key5;
336+
key5 = reader.Read7BitEncodedInt32();
337+
key3[x5] = key5;
338+
}
339+
}
340+
key1[x3] = key3;
341+
}
342+
}
343+
Array3DValue[x1] = key1;
344+
}
345+
}
346+
{
347+
MapEnumToInt = new Dictionary<ColorT, int>();
348+
var __MapEnumToInt_Count1 = reader.Read7BitEncodedInt32();
349+
for (int x1 = 0; x1 < __MapEnumToInt_Count1; x1++)
350+
{
351+
ColorT key1;
352+
{
353+
ColorT __enumVal = default;
354+
var __enumStr = reader.ReadString();
355+
if (!string.IsNullOrEmpty(__enumStr) && !Enum.TryParse(__enumStr, out __enumVal))
356+
{
357+
throw new ArgumentException();
358+
}
359+
key1 = __enumVal;
360+
}
361+
int value1;
362+
value1 = reader.Read7BitEncodedInt32();
363+
MapEnumToInt.Add(key1, value1);
364+
}
365+
}
366+
{
367+
var __EnumArray_Count1 = reader.Read7BitEncodedInt32();
368+
EnumArray = new ColorT[__EnumArray_Count1];
369+
for (int x1 = 0; x1 < __EnumArray_Count1; x1++)
370+
{
371+
ColorT key1;
372+
{
373+
ColorT __enumVal = default;
374+
var __enumStr = reader.ReadString();
375+
if (!string.IsNullOrEmpty(__enumStr) && !Enum.TryParse(__enumStr, out __enumVal))
376+
{
377+
throw new ArgumentException();
378+
}
379+
key1 = __enumVal;
380+
}
381+
EnumArray[x1] = key1;
382+
}
383+
}
384+
{
385+
CustomJSON = reader.ReadJson<SampleParent>();
386+
}
280387
return true;
281388
}
282389
}

sandbox/ConsoleApp/Generated/DRMatrixSample.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public override bool ParseDataRow(int index, BinaryReader reader)
5252
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5353
public bool GetDataRowOrDefault(short key1, long key2)
5454
{
55-
bool? result = Get(key1, key2);
56-
return result.HasValue ? result.Value : DefaultValue;
55+
var result = Get(key1, key2);
56+
return result != null ? result : DefaultValue;
5757
}
5858

5959
#endregion
@@ -84,7 +84,7 @@ public static DTMatrixSample Table
8484
/// <summary>
8585
/// 检查数据矩阵是否已加载 (静态方法)
8686
/// </summary>
87-
public static bool IsLoaded => DataTableManager.HasDataTable<DTMatrixSample>();
87+
public static bool IsLoaded => DataTableManager.IsLoaded<DTMatrixSample>();
8888

8989
/// <summary>
9090
/// 根据Key1和Key2获取数据值 (静态方法)
@@ -105,8 +105,8 @@ public static bool GetRowOrDefault(short key1, long key2)
105105
var table = DataTableManager.GetDataTableInternal<DTMatrixSample>();
106106
if (table != null)
107107
{
108-
bool? result = table.Get(key1, key2);
109-
return result.HasValue ? result.Value : table.DefaultValue;
108+
var result = table.Get(key1, key2);
109+
return result != null ? result : table.DefaultValue;
110110
}
111111
return default(bool);
112112
}

src/DataTables.Generator/Program.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ public async Task ExportAll(
4242
string skipCellMarker = "#",
4343
string diagnosticsJsonOutput = "")
4444
{
45-
var oldEncoding = Console.OutputEncoding;
46-
Console.OutputEncoding = System.Text.Encoding.UTF8;
47-
4845
var sw = Stopwatch.StartNew();
4946
Console.WriteLine("Start DataTables CodeGeneration");
5047

@@ -74,13 +71,11 @@ public async Task ExportAll(
7471
catch (Exception e)
7572
{
7673
Console.ForegroundColor = ConsoleColor.Red;
77-
Console.WriteLine(e);
74+
Console.Error.WriteLine(e);
7875
Console.ResetColor();
7976
}
8077

8178
Console.WriteLine("Complete DataTables Generation, elapsed: " + sw.Elapsed);
82-
83-
Console.OutputEncoding = oldEncoding;
8479
}
8580

8681
/// <summary>
@@ -143,7 +138,7 @@ public async Task ExportOne(
143138
catch (Exception e)
144139
{
145140
Console.ForegroundColor = ConsoleColor.Red;
146-
Console.WriteLine(e);
141+
Console.Error.WriteLine(e);
147142
Console.ResetColor();
148143
}
149144

src/DataTables.GeneratorCore/DataTableProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ internal void GenerateDataFile(string filePath, string outputDir, bool forceOver
597597
binaryWriter.Write(DATA_TABLE_VERSION);
598598

599599
// 写入行数占位
600-
binaryWriter.Write7BitEncodedInt(0);
600+
binaryWriter.Write7BitEncodedInt32(0);
601601

602602
// 写入数据集
603603
int dataRowCount = WriteDataRows(sheet, binaryWriter);
@@ -607,7 +607,7 @@ internal void GenerateDataFile(string filePath, string outputDir, bool forceOver
607607
var signatureBytes = Encoding.UTF8.GetBytes(DATA_TABLE_SIGNATURE);
608608
long countPosition = GetCompactIntSize(signatureBytes.Length) + signatureBytes.Length + sizeof(int);
609609
fileStream.Seek(countPosition, SeekOrigin.Begin);
610-
binaryWriter.Write7BitEncodedInt(dataRowCount);
610+
binaryWriter.Write7BitEncodedInt32(dataRowCount);
611611

612612
logger.Debug(" > Generate {0}.bytes to: {1}. - {2}ms", m_Context.DataRowClassName, outputFileName, Environment.TickCount - startTickCount);
613613
}

tests/DataTables.Tests/ColumnTableTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task Generate_And_Load_From_ColumnTable()
5656
r3.CreateCell(4, CellType.String).SetCellValue("2");
5757
r3.CreateCell(5, CellType.String).SetCellValue("3");
5858

59-
using var fsx = File.Create(xlsxPath);
59+
await using var fsx = File.Create(xlsxPath);
6060
wb.Write(fsx);
6161
}
6262

@@ -76,18 +76,18 @@ await generator.GenerateFile(
7676

7777
// Assert data file exists
7878
// 类名 ItemConfig → DataTable 名称前缀 DT,输出文件 DataTables.Tests.Generated.DTItemConfig.bytes
79-
var dataFile = Directory.GetFiles(dataOut, "*.bytes", SearchOption.AllDirectories)
79+
string? dataFile = Directory.GetFiles(dataOut, "*.bytes", SearchOption.AllDirectories)
8080
.FirstOrDefault(p => Path.GetFileName(p).Contains("DTItemConfig"));
8181
dataFile.Should().NotBeNull();
8282

8383
// 运行时加载验证:通过 DataTable 基础设施读取并计数(无需生成代码类)
84-
using var fs = File.OpenRead(dataFile!);
84+
await using var fs = File.OpenRead(dataFile!);
8585
using var br = new BinaryReader(fs);
8686
// 读取签名
87-
var signature = br.ReadString();
87+
string signature = br.ReadString();
8888
signature.Should().Be("DTABLE");
8989
// 读取版本
90-
var version = br.ReadInt32();
90+
int version = br.ReadInt32();
9191
version.Should().BeGreaterThan(0);
9292
// 读取行数 7-bit 编码
9393
int rowCount = DataTables.BinaryExtension.Read7BitEncodedInt32(br);

0 commit comments

Comments
 (0)