Skip to content

Commit db1d45c

Browse files
committed
feat: encapsulate messager container
1 parent dc932cb commit db1d45c

File tree

8 files changed

+167
-28
lines changed

8 files changed

+167
-28
lines changed

cmd/protoc-gen-csharp-tableau-loader/hub.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,28 @@ func generateHub(gen *protogen.Plugin) {
1414
helper.GenerateCommonHeader(gen, g, version)
1515
g.P(staticHubContent1)
1616
for _, messager := range messagers {
17-
g.P(" Register<Tableau.", messager, ">();")
17+
g.P(helper.Indent(2), "public ", messager, "? ", messager, ";")
1818
}
1919
g.P(staticHubContent2)
20+
for _, messager := range messagers {
21+
g.P(helper.Indent(4), `if (messagerMap.ContainsKey("`, messager, `"))`)
22+
g.P(helper.Indent(4), "{")
23+
g.P(helper.Indent(5), messager, " = (", messager, `)messagerMap["`, messager, `"];`)
24+
g.P(helper.Indent(4), "}")
25+
}
26+
g.P(staticHubContent3)
27+
for _, messager := range messagers {
28+
g.P()
29+
g.P(helper.Indent(2), "public ", messager, "? Get", messager, "()")
30+
g.P(helper.Indent(2), "{")
31+
g.P(helper.Indent(3), "return MessagerContainer.", messager, ";")
32+
g.P(helper.Indent(2), "}")
33+
}
34+
g.P(staticHubContent4)
35+
for _, messager := range messagers {
36+
g.P(" Register<Tableau.", messager, ">();")
37+
}
38+
g.P(staticHubContent5)
2039
}
2140

2241
const staticHubContent1 = `using System;
@@ -54,13 +73,13 @@ namespace Tableau
5473
5574
public ref Stats GetStats() { return ref LoadStats; }
5675
57-
public abstract bool Load(string dir, Format fmt, LoadOptions? options = null);
76+
public abstract bool Load(string dir, Format fmt, in LoadOptions? options = null);
5877
5978
protected virtual bool ProcessAfterLoad() { return true; }
6079
6180
public virtual bool ProcessAfterLoadAll(in Hub hub) { return true; }
6281
63-
internal bool LoadMessageByPath<T>(out T msg, string dir, Format fmt, LoadOptions? options = null) where T : Google.Protobuf.IMessage<T>, new()
82+
internal bool LoadMessageByPath<T>(out T msg, string dir, Format fmt, in LoadOptions? options = null) where T : Google.Protobuf.IMessage<T>, new()
6483
{
6584
msg = new T();
6685
string name = msg.Descriptor.Name;
@@ -111,8 +130,19 @@ namespace Tableau
111130
112131
internal class MessagerContainer
113132
{
114-
public Dictionary<string, Messager> MessagerMap = new Dictionary<string, Messager>();
115-
public DateTime LastLoadedTime;
133+
public Dictionary<string, Messager> MessagerMap;
134+
public DateTime LastLoadedTime;`
135+
136+
var staticHubContent2 = `
137+
public MessagerContainer(in Dictionary<string, Messager>? messagerMap = null)
138+
{
139+
MessagerMap = messagerMap ?? new Dictionary<string, Messager>();
140+
LastLoadedTime = DateTime.Now;
141+
if (messagerMap != null)
142+
{`
143+
144+
var staticHubContent3 = ` }
145+
}
116146
}
117147
118148
public class HubOptions
@@ -130,7 +160,7 @@ namespace Tableau
130160
Options = options ?? new HubOptions();
131161
}
132162
133-
public bool Load(string dir, Format fmt, LoadOptions? options = null)
163+
public bool Load(string dir, Format fmt, in LoadOptions? options = null)
134164
{
135165
var messagerMap = NewMessagerMap();
136166
foreach (var messager in messagerMap.Values)
@@ -141,7 +171,7 @@ namespace Tableau
141171
}
142172
}
143173
var tmpHub = new Hub();
144-
tmpHub.SetMessagerMap(MessagerContainer.MessagerMap);
174+
tmpHub.SetMessagerMap(messagerMap);
145175
foreach (var messager in messagerMap.Values)
146176
{
147177
if (!messager.ProcessAfterLoadAll(tmpHub))
@@ -160,8 +190,7 @@ namespace Tableau
160190
161191
public void SetMessagerMap(in Dictionary<string, Messager> map)
162192
{
163-
MessagerContainer.MessagerMap = map;
164-
MessagerContainer.LastLoadedTime = DateTime.Now;
193+
MessagerContainer = new MessagerContainer(map);
165194
}
166195
167196
public T? Get<T>() where T : Messager, IMessagerName, new()
@@ -172,8 +201,9 @@ namespace Tableau
172201
return (T)messager;
173202
}
174203
return default;
175-
}
204+
}`
176205

206+
const staticHubContent4 = `
177207
private Messager GetMessager(string name)
178208
{
179209
return GetMessagerMap()[name];
@@ -211,6 +241,6 @@ namespace Tableau
211241
public static void Init()
212242
{`
213243

214-
const staticHubContent2 = ` }
244+
const staticHubContent5 = ` }
215245
}
216246
}`

cmd/protoc-gen-csharp-tableau-loader/messager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, message *protog
7070
g.P()
7171
g.P(helper.Indent(2), "public static string Name() => Protoconf.", messagerName, ".Descriptor.Name;")
7272
g.P()
73-
g.P(helper.Indent(2), "public override bool Load(string dir, Format fmt, LoadOptions? options = null)")
73+
g.P(helper.Indent(2), "public override bool Load(string dir, Format fmt, in LoadOptions? options = null)")
7474
g.P(helper.Indent(2), "{")
7575
g.P(helper.Indent(3), "var start = DateTime.Now;")
7676
g.P(helper.Indent(3), "bool loaded = LoadMessageByPath<Protoconf.", messagerName, ">(out var msg, dir, fmt, options);")

test/csharp-tableau-loader/Program.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,16 @@ static void Main(string[] args)
5959
Console.WriteLine($"ItemConf: {itemConf.Data()}");
6060
Console.WriteLine($"ItemConf Load duration: {itemConf.GetStats().Duration.TotalMilliseconds} ms");
6161
}
62+
63+
itemConf = hub.GetItemConf();
64+
if (itemConf is null)
65+
{
66+
Console.WriteLine("ItemConf is null");
67+
}
68+
else
69+
{
70+
Console.WriteLine($"ItemConf: {itemConf.Data()}");
71+
Console.WriteLine($"ItemConf Load duration: {itemConf.GetStats().Duration.TotalMilliseconds} ms");
72+
}
6273
}
6374
}

test/csharp-tableau-loader/tableau/HeroConf.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Hero_OrderedMap : SortedDictionary<string, Hero_OrderedMapValue> {
2727

2828
public static string Name() => Protoconf.HeroConf.Descriptor.Name;
2929

30-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
30+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
3131
{
3232
var start = DateTime.Now;
3333
bool loaded = LoadMessageByPath<Protoconf.HeroConf>(out var msg, dir, fmt, options);
@@ -93,7 +93,7 @@ public class HeroBaseConf : Messager, IMessagerName
9393

9494
public static string Name() => Protoconf.HeroBaseConf.Descriptor.Name;
9595

96-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
96+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
9797
{
9898
var start = DateTime.Now;
9999
bool loaded = LoadMessageByPath<Protoconf.HeroBaseConf>(out var msg, dir, fmt, options);

test/csharp-tableau-loader/tableau/Hub.cs

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ public class Stats
3737

3838
public ref Stats GetStats() { return ref LoadStats; }
3939

40-
public abstract bool Load(string dir, Format fmt, LoadOptions? options = null);
40+
public abstract bool Load(string dir, Format fmt, in LoadOptions? options = null);
4141

4242
protected virtual bool ProcessAfterLoad() { return true; }
4343

4444
public virtual bool ProcessAfterLoadAll(in Hub hub) { return true; }
4545

46-
internal bool LoadMessageByPath<T>(out T msg, string dir, Format fmt, LoadOptions? options = null) where T : Google.Protobuf.IMessage<T>, new()
46+
internal bool LoadMessageByPath<T>(out T msg, string dir, Format fmt, in LoadOptions? options = null) where T : Google.Protobuf.IMessage<T>, new()
4747
{
4848
msg = new T();
4949
string name = msg.Descriptor.Name;
@@ -94,8 +94,62 @@ internal string Format2Ext(Format fmt)
9494

9595
internal class MessagerContainer
9696
{
97-
public Dictionary<string, Messager> MessagerMap = new Dictionary<string, Messager>();
97+
public Dictionary<string, Messager> MessagerMap;
9898
public DateTime LastLoadedTime;
99+
public HeroConf? HeroConf;
100+
public HeroBaseConf? HeroBaseConf;
101+
public ItemConf? ItemConf;
102+
public PatchReplaceConf? PatchReplaceConf;
103+
public PatchMergeConf? PatchMergeConf;
104+
public RecursivePatchConf? RecursivePatchConf;
105+
public ActivityConf? ActivityConf;
106+
public ChapterConf? ChapterConf;
107+
public ThemeConf? ThemeConf;
108+
109+
public MessagerContainer(in Dictionary<string, Messager>? messagerMap = null)
110+
{
111+
MessagerMap = messagerMap ?? new Dictionary<string, Messager>();
112+
LastLoadedTime = DateTime.Now;
113+
if (messagerMap != null)
114+
{
115+
if (messagerMap.ContainsKey("HeroConf"))
116+
{
117+
HeroConf = (HeroConf)messagerMap["HeroConf"];
118+
}
119+
if (messagerMap.ContainsKey("HeroBaseConf"))
120+
{
121+
HeroBaseConf = (HeroBaseConf)messagerMap["HeroBaseConf"];
122+
}
123+
if (messagerMap.ContainsKey("ItemConf"))
124+
{
125+
ItemConf = (ItemConf)messagerMap["ItemConf"];
126+
}
127+
if (messagerMap.ContainsKey("PatchReplaceConf"))
128+
{
129+
PatchReplaceConf = (PatchReplaceConf)messagerMap["PatchReplaceConf"];
130+
}
131+
if (messagerMap.ContainsKey("PatchMergeConf"))
132+
{
133+
PatchMergeConf = (PatchMergeConf)messagerMap["PatchMergeConf"];
134+
}
135+
if (messagerMap.ContainsKey("RecursivePatchConf"))
136+
{
137+
RecursivePatchConf = (RecursivePatchConf)messagerMap["RecursivePatchConf"];
138+
}
139+
if (messagerMap.ContainsKey("ActivityConf"))
140+
{
141+
ActivityConf = (ActivityConf)messagerMap["ActivityConf"];
142+
}
143+
if (messagerMap.ContainsKey("ChapterConf"))
144+
{
145+
ChapterConf = (ChapterConf)messagerMap["ChapterConf"];
146+
}
147+
if (messagerMap.ContainsKey("ThemeConf"))
148+
{
149+
ThemeConf = (ThemeConf)messagerMap["ThemeConf"];
150+
}
151+
}
152+
}
99153
}
100154

101155
public class HubOptions
@@ -113,7 +167,7 @@ public Hub(HubOptions? options = null)
113167
Options = options ?? new HubOptions();
114168
}
115169

116-
public bool Load(string dir, Format fmt, LoadOptions? options = null)
170+
public bool Load(string dir, Format fmt, in LoadOptions? options = null)
117171
{
118172
var messagerMap = NewMessagerMap();
119173
foreach (var messager in messagerMap.Values)
@@ -124,7 +178,7 @@ public bool Load(string dir, Format fmt, LoadOptions? options = null)
124178
}
125179
}
126180
var tmpHub = new Hub();
127-
tmpHub.SetMessagerMap(MessagerContainer.MessagerMap);
181+
tmpHub.SetMessagerMap(messagerMap);
128182
foreach (var messager in messagerMap.Values)
129183
{
130184
if (!messager.ProcessAfterLoadAll(tmpHub))
@@ -143,8 +197,7 @@ public ref Dictionary<string, Messager> GetMessagerMap()
143197

144198
public void SetMessagerMap(in Dictionary<string, Messager> map)
145199
{
146-
MessagerContainer.MessagerMap = map;
147-
MessagerContainer.LastLoadedTime = DateTime.Now;
200+
MessagerContainer = new MessagerContainer(map);
148201
}
149202

150203
public T? Get<T>() where T : Messager, IMessagerName, new()
@@ -157,6 +210,51 @@ public void SetMessagerMap(in Dictionary<string, Messager> map)
157210
return default;
158211
}
159212

213+
public HeroConf? GetHeroConf()
214+
{
215+
return MessagerContainer.HeroConf;
216+
}
217+
218+
public HeroBaseConf? GetHeroBaseConf()
219+
{
220+
return MessagerContainer.HeroBaseConf;
221+
}
222+
223+
public ItemConf? GetItemConf()
224+
{
225+
return MessagerContainer.ItemConf;
226+
}
227+
228+
public PatchReplaceConf? GetPatchReplaceConf()
229+
{
230+
return MessagerContainer.PatchReplaceConf;
231+
}
232+
233+
public PatchMergeConf? GetPatchMergeConf()
234+
{
235+
return MessagerContainer.PatchMergeConf;
236+
}
237+
238+
public RecursivePatchConf? GetRecursivePatchConf()
239+
{
240+
return MessagerContainer.RecursivePatchConf;
241+
}
242+
243+
public ActivityConf? GetActivityConf()
244+
{
245+
return MessagerContainer.ActivityConf;
246+
}
247+
248+
public ChapterConf? GetChapterConf()
249+
{
250+
return MessagerContainer.ChapterConf;
251+
}
252+
253+
public ThemeConf? GetThemeConf()
254+
{
255+
return MessagerContainer.ThemeConf;
256+
}
257+
160258
private Messager GetMessager(string name)
161259
{
162260
return GetMessagerMap()[name];

test/csharp-tableau-loader/tableau/ItemConf.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class Index_UseEffectTypeMap : Dictionary<Protoconf.UseEffect.Types.Type,
100100

101101
public static string Name() => Protoconf.ItemConf.Descriptor.Name;
102102

103-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
103+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
104104
{
105105
var start = DateTime.Now;
106106
bool loaded = LoadMessageByPath<Protoconf.ItemConf>(out var msg, dir, fmt, options);

test/csharp-tableau-loader/tableau/PatchConf.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class PatchReplaceConf : Messager, IMessagerName
1616

1717
public static string Name() => Protoconf.PatchReplaceConf.Descriptor.Name;
1818

19-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
19+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
2020
{
2121
var start = DateTime.Now;
2222
bool loaded = LoadMessageByPath<Protoconf.PatchReplaceConf>(out var msg, dir, fmt, options);
@@ -35,7 +35,7 @@ public class PatchMergeConf : Messager, IMessagerName
3535

3636
public static string Name() => Protoconf.PatchMergeConf.Descriptor.Name;
3737

38-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
38+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
3939
{
4040
var start = DateTime.Now;
4141
bool loaded = LoadMessageByPath<Protoconf.PatchMergeConf>(out var msg, dir, fmt, options);
@@ -63,7 +63,7 @@ public class RecursivePatchConf : Messager, IMessagerName
6363

6464
public static string Name() => Protoconf.RecursivePatchConf.Descriptor.Name;
6565

66-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
66+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
6767
{
6868
var start = DateTime.Now;
6969
bool loaded = LoadMessageByPath<Protoconf.RecursivePatchConf>(out var msg, dir, fmt, options);

test/csharp-tableau-loader/tableau/TestConf.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class Index_AwardMap : Dictionary<uint, List<Protoconf.Item>> { }
6060

6161
public static string Name() => Protoconf.ActivityConf.Descriptor.Name;
6262

63-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
63+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
6464
{
6565
var start = DateTime.Now;
6666
bool loaded = LoadMessageByPath<Protoconf.ActivityConf>(out var msg, dir, fmt, options);
@@ -264,7 +264,7 @@ public class ChapterConf : Messager, IMessagerName
264264

265265
public static string Name() => Protoconf.ChapterConf.Descriptor.Name;
266266

267-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
267+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
268268
{
269269
var start = DateTime.Now;
270270
bool loaded = LoadMessageByPath<Protoconf.ChapterConf>(out var msg, dir, fmt, options);
@@ -292,7 +292,7 @@ public class ThemeConf : Messager, IMessagerName
292292

293293
public static string Name() => Protoconf.ThemeConf.Descriptor.Name;
294294

295-
public override bool Load(string dir, Format fmt, LoadOptions? options = null)
295+
public override bool Load(string dir, Format fmt, in LoadOptions? options = null)
296296
{
297297
var start = DateTime.Now;
298298
bool loaded = LoadMessageByPath<Protoconf.ThemeConf>(out var msg, dir, fmt, options);

0 commit comments

Comments
 (0)