-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExcelConfigManager.cs
153 lines (124 loc) · 5.14 KB
/
ExcelConfigManager.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System.IO;
using ProtoBuf;
using System.Collections.Generic;
using xresloader.Protobuf;
namespace xresloader {
public static class ExcelConfigManager {
const string CLASS_NAME = "ExcelConfigManager";
public delegate Stream FileReadStreamHandle(string file_path);
public delegate string FileGetFullPathHandle(string file_path);
static private DynamicFactory factory = new DynamicFactory();
static private Dictionary<string, ExcelConfigSet> allConfigures = new Dictionary<string, ExcelConfigSet>();
static private FileReadStreamHandle readFileStream = null;
static private FileGetFullPathHandle getFileFullPath = null;
static private string packageName = "";
static private string lastError = "";
static public DynamicFactory Factory {
get { return factory; }
}
static public FileReadStreamHandle FileReadStream
{
get { return readFileStream; }
set { readFileStream = value; }
}
static public FileGetFullPathHandle FileGetFullPath
{
get { return getFileFullPath; }
set { getFileFullPath = value; }
}
static public string PackageName
{
get { return packageName; }
set { packageName = value; }
}
static public string LastError
{
get { return lastError; }
set { lastError = value; }
}
static public Stream GetFileStream(string path) {
if (null != getFileFullPath) {
path = getFileFullPath(path);
}
if (null != readFileStream) {
return readFileStream(path);
}
return File.OpenRead(path);
}
// Use this for initialization
static public void Init(string[] pb_files) {
for (int i = 0; i < pb_files.Length; ++i) {
if (null != pb_files[i]) {
factory.Register(GetFileStream(pb_files[i]));
}
}
}
/// <summary>
/// 添加配置
/// </summary>
/// <param name="config_name">配置名称</param>
/// <param name="protocol_name">protobuf协议名称</param>
/// <param name="file_name">文件名</param>
/// <returns></returns>
static public ExcelConfigSet AddConfig(string config_name, string protocol_name = "", string file_name = "") {
if (null == protocol_name || protocol_name.Length == 0) {
if (null != packageName && packageName.Length > 0)
{
protocol_name = string.Format("{0}.{1}", packageName, config_name);
}
else
{
protocol_name = config_name;
}
}
if (null != Get(config_name)) {
lastError = string.Format("configure name {0} already registered, can not register again", config_name);
return new ExcelConfigSet(factory, file_name, protocol_name);
}
ExcelConfigSet ret = new ExcelConfigSet(factory, file_name, protocol_name);
allConfigures[config_name] = ret;
return ret;
}
/// <summary>
/// 重新加载全部配置
/// </summary>
static public void ReloadAll() {
foreach (var cfg in allConfigures) {
cfg.Value.Reload();
}
}
/// <summary>
/// 获取某个配置集
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
static public ExcelConfigSet Get(string name) {
ExcelConfigSet ret;
if (allConfigures.TryGetValue(name, out ret)) {
return ret;
}
return null;
}
static private void RegisterAllConfigure() {
//Check TableConfigure
//AddConfig("const_parameter_cfg")
// .AddKVIndex((DynamicMessage item) =>{
// return new ExcelConfigSet.Key(item.GetFieldValue("id"));
// }).AddFilter((DynamicMessage item) => {
// return (uint)item.GetFieldValue("id") > 0;
//});
//AddConfig("const_parameter_cfg").AddKVIndexAuto("id").AddFilter(item => (uint)item.GetFieldValue("id") > 0);
//DynamicMessage msg = Get("errorcode_cfg").GetKVAuto(0);
//msg.GetFieldValue("level");
//AddConfig("errorcode_cfg").AddKVIndex((DynamicMessage item) => {
// return new ExcelConfigSet.Key(item.GetFieldValue("id"));
//});
// ============= test code here =============
//ReloadAll();
//ExcelConfigSet errcode_set = Get("errorcode_cfg");
//_Log.Debug("error code [{0}] = {1}", 0, errcode_set.GetKV(new ExcelConfigSet.Key(0)).GetFieldValue("text"));
//_Log.Debug("error code [{0}] = {1}", 1, errcode_set.GetKV(new ExcelConfigSet.Key(1)).GetFieldValue("text"));
//Get("errorcode_cfg").GetKV(new ExcelConfigSet.Key(0));
}
}
}