-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConfigSetManager.cs.mako
100 lines (83 loc) · 3.14 KB
/
ConfigSetManager.cs.mako
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
## -*- coding: utf-8 -*-
<%!
import time
%><%namespace name="pb_loader" module="pb_loader" />
// Copyright ${time.strftime("%Y")} xresloader. All rights reserved.
// Generated by xres-code-generator, please don't edit it
//
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Org.Xresloader.Pb;
using Google.Protobuf;
${pb_loader.CsNamespaceBegin(global_package)}
public class ConfigSetManager {
protected Action<string> _logHandler;
public Action<string> LogHandler {
get => _logHandler;
set {
_logHandler = value;
if (_logHandler == null) _logHandler = DefaultLogHandler;
}
}
protected Func<string, byte[]> _loader;
public Func<string, byte[]> Loader {
get => _loader;
set {
_loader = value;
if (_loader == null) _loader = DefaultLoader;
}
}
public byte[] DefaultLoader(string name) {
try {
return File.ReadAllBytes(name);
} catch (Exception e) {
LogHandler?.Invoke($"ConfigSetManager DefaultLoader ReadFile Failed, Exception[{e.ToString()}] Stack:{e.StackTrace}");
return null;
}
}
public void DefaultLogHandler(string log) {
}
protected ConfigSetManager() {
_loader = DefaultLoader;
_logHandler = DefaultLogHandler;
//Reload();
}
private static ConfigSetManager _instance;
public static ConfigSetManager Instance {
get { return _instance ?? (_instance = new ConfigSetManager()); }
}
public T Parse<T>(byte[] bytes, MessageParser parser) where T : class, IMessage {
try {
return (T)parser.ParseFrom(bytes);
} catch (Exception e) {
LogHandler?.Invoke($"ConfigSetManager Deserialize<{typeof(T).Name}> Failed, bytes sz[{bytes.Length}], Exception[{e.ToString()}] Stack:{e.StackTrace}");
return null;
}
}
public T ParseByName<T>(string name, MessageParser parser) where T : class, IMessage {
byte[] bytes = Loader?.Invoke(name) ?? null;
if (bytes == null) {
LogHandler?.Invoke($"ConfigSetManager Load[{name}] failed!");
return null;
}
return Parse<T>(bytes, parser);
}
public void Reload() {
Clear();
% for pb_msg in pb_set.generate_message:
% for loader in pb_msg.loaders:
${loader.get_cs_class_name()}.Instance.Reload();
% endfor
% endfor
}
public void Clear() {
% for pb_msg in pb_set.generate_message:
% for loader in pb_msg.loaders:
${loader.get_cs_class_name()}.Instance.Clear();
% endfor
% endfor
}
}
${pb_loader.CsNamespaceEnd(global_package)} // ${global_package}