-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
344 lines (297 loc) · 13.1 KB
/
Config.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Xml.Linq;
using System.IO;
using InnerSpaceAPI;
using System.Xml;
namespace Eve_TradingAgent
{
public class Config
{
public Config()
{
_random = new Random(DateTime.Now.Millisecond);
_dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
AppDomain.CurrentDomain.RelativeSearchPath,
"TradeAgent",
"Configs");
if (!Directory.Exists(_dir))
{
Directory.CreateDirectory(_dir);
}
}
#region general(static) configs
// All these files should have upper and lower bounds; deal with them when saving /loading
public static bool WriteLogToFile = true;
public static bool EchoLoginInnerSpace = false;
public static int MaxiumLogNumberToDisplay = 500;
public static int MaxiumDaysToKeepLog = 30;
/// <summary>
/// load general configs from file when it exists, otherwise use default value.
/// General configs are common configs that should be shared between characters or processes.
/// </summary>
public void LoadGeneralConfig()
{
string filename = Path.Combine(_dir,
_getGeneralConfigFilename("General_Config", ".xml"));
try
{
_generalConfigFile = XDocument.Load(filename);
//load
_loadBooleanItem(_generalConfigFile, "WriteLogToFile", ref WriteLogToFile);
_loadBooleanItem(_generalConfigFile, "EchoLoginInnerSpace", ref EchoLoginInnerSpace);
_loadValueItem(_generalConfigFile, "MaxiumLogNumberToDisplay", 10, -1, ref MaxiumLogNumberToDisplay);
_loadValueItem(_generalConfigFile, "MaxiumDaysToKeepLog", 1, -1, ref MaxiumDaysToKeepLog);
}
catch (Exception e)
{
if ((e is XmlException) || (e is FileNotFoundException))
{
// Do nothing here, just use default value;
}
else
{
throw;
}
}
}
public void SaveGeneralConfig()
{
string filename = Path.Combine(_dir,
_getGeneralConfigFilename("General_Config", ".xml"));
//try create
XDocument Doc = new XDocument();
XElement Root = new XElement(
new XElement("Configs",
new XElement("WriteLogToFile", WriteLogToFile),
new XElement("EchoLoginInnerSpace", EchoLoginInnerSpace),
new XElement("MaxiumLogNumberToDisplay", MaxiumLogNumberToDisplay),
new XElement("MaxiumDaysToKeepLog", MaxiumDaysToKeepLog))
);
Doc.Add(Root);
// TODO error handling;
//if fail throw(not really necessary)
// This will automatically replace the old values, no need to consider about it.
Doc.Save(filename);
}
#endregion
#region character specific configs
// cooldown of evaluate and maybe modify a SINGLE order.
public int RandomizedOrderProcessIntervalInMilliSec
{
get
{
return makeRandom(_orderProcessIntervalInMilliSec);
}
}
public int RandomizedOrderModifyIntervalInMilliSec
{
get
{
return makeRandom(_orderModifyIntervalInMilliSec);
}
}
public int RandomizedTurnsBetweenReloadOrderList
{
get
{
return makeRandom(_turnsBetweenReloadOrderList);
}
}
public int _orderProcessIntervalInMilliSec = 10000;
public int _orderModifyIntervalInMilliSec = 500000;
public int _turnsBetweenReloadOrderList = 8;
// This value is fixed
public int IntervalOfProcessingSameOrder = 60000;
public int ValidGapSizeRatio = 10;
public byte[] ListViewState;
public bool ShowOrdersOutOfReach = true;
public bool ShowOrdersCoolingDown = true;
public bool ShowOrdersNeedNoUpdate = true;
public bool ShowOrdersMarkedSkip = true;
public bool ShowLogWindow = false;
// TODO save window size and position of mainwindow/logwindow
public List<long> OrderIDsToSkip = new List<long>();
/// <summary>
/// load character configs from file when it exists, otherwise use default value.
/// Character configs are specified configs for each character or process.
/// </summary>
/// <param name="characterName"></param>
public void LoadCharacterConfig(string characterName)
{
if (!string.IsNullOrEmpty(characterName))
{
_characterName = characterName;
string filename = Path.Combine(_dir,
_getCharacterConfigFilename("_Config", ".xml"));
try
{
_characterConfigFile = XDocument.Load(filename);
//load
_loadValueItem(_characterConfigFile, "OrderProcessIntervalInMilliSec", 6000, 300000, ref _orderProcessIntervalInMilliSec);
_loadValueItem(_characterConfigFile, "OrderModifyIntervalInMilliSec", 390000, 600000, ref _orderModifyIntervalInMilliSec);
_loadValueItem(_characterConfigFile, "TurnsBetweenReloadOrderList", 5, 100, ref _turnsBetweenReloadOrderList);
_loadValueItem(_characterConfigFile, "ValidGapSizeRatio", 2, 50, ref ValidGapSizeRatio);
_loadByteArrayItem(_characterConfigFile, "ListViewState", ref ListViewState);
_loadBooleanItem(_characterConfigFile, "ShowOrdersOutOfReach", ref ShowOrdersOutOfReach);
_loadBooleanItem(_characterConfigFile, "ShowOrdersCoolingDown", ref ShowOrdersCoolingDown);
_loadBooleanItem(_characterConfigFile, "ShowOrdersNeedNoUpdate", ref ShowOrdersNeedNoUpdate);
_loadBooleanItem(_characterConfigFile, "ShowOrdersMarkedSkip", ref ShowOrdersMarkedSkip);
_loadBooleanItem(_characterConfigFile, "ShowLogWindow", ref ShowLogWindow);
_loadListItem(_characterConfigFile, "OrderIDsToSkip", "ID", ref OrderIDsToSkip);
}
catch (Exception e)
{
if ((e is XmlException) || (e is FileNotFoundException))
{
// Do nothing here, just use the default value.
}
else
{
throw;
}
}
}
}
/// <summary>
///
/// </summary>
public void SaveCharacterConfig()
{
Debug.Assert(!string.IsNullOrEmpty(_characterName));
string filename = Path.Combine(_dir,
_getCharacterConfigFilename("_Config", ".xml"));
//try create
XDocument Doc = new XDocument();
XElement Root = new XElement(
new XElement("Configs",
new XElement("OrderProcessIntervalInMilliSec", _orderProcessIntervalInMilliSec),
new XElement("OrderModifyIntervalInMilliSec", _orderModifyIntervalInMilliSec),
new XElement("TurnsBetweenReloadOrderList", _turnsBetweenReloadOrderList),
new XElement("ValidGapSizeRatio", ValidGapSizeRatio),
new XElement("ListViewState", System.Convert.ToBase64String(ListViewState)),
new XElement("ShowOrdersOutOfReach", ShowOrdersOutOfReach),
new XElement("ShowOrdersCoolingDown", ShowOrdersCoolingDown),
new XElement("ShowOrdersNeedNoUpdate", ShowOrdersNeedNoUpdate),
new XElement("ShowOrdersMarkedSkip", ShowOrdersMarkedSkip),
new XElement("ShowLogWindow", ShowLogWindow),
new XElement("OrderIDsToSkip", from id in OrderIDsToSkip
select new XElement("ID", id)))
);
Doc.Add(Root);
// TODO error handling;
//if fail throw(not really necessary)
// This will automatically replace the old values, no need to consider about it.
Doc.Save(filename);
}
#endregion
#region private fields and methods
private Random _random;
private string _dir = null;
private string _characterName = null;
// Configs in this file are static
private XDocument _generalConfigFile;
// Configs in this file are character(process) specific and accessed through Config instance.
private XDocument _characterConfigFile;
private void _loadValueItem(XDocument document, string itemName, int lowerBound, int upperBound, ref int result)
{
Debug.Assert((lowerBound == -1) || (lowerBound > 0));
Debug.Assert((upperBound == -1) || (upperBound > 0));
Debug.Assert((upperBound == -1) || (lowerBound == -1) || (upperBound >= lowerBound));
XElement Configs = document.Element("Configs");
if (Configs.Elements(itemName).Any() &&
!string.IsNullOrEmpty(Configs.Elements(itemName).First().Value))
{
int ParsedValue;
if (int.TryParse(Configs.Element(itemName).Value, out ParsedValue))
{
if (((lowerBound == -1) ||
(ParsedValue >= lowerBound))
&&
((upperBound == -1) ||
(ParsedValue <= upperBound)))
{
result = ParsedValue;
}
}
}
}
private void _loadListItem(XDocument document, string listName, string listItemName, ref List<long> result)
{
result = new List<long>();
XElement Configs = document.Element("Configs");
if (Configs.Elements(listName).Any() &&
!string.IsNullOrEmpty(Configs.Elements(listName).First().Value))
{
XElement ItemList = Configs.Elements(listName).First();
if (ItemList.Elements(listItemName).Any() &&
!string.IsNullOrEmpty(ItemList.Elements(listItemName).First().Value))
{
var List = from itm in ItemList.Elements(listItemName)
select itm;
foreach (var item in List)
{
long ParsedValue;
if (long.TryParse(item.Value, out ParsedValue))
{
if (ParsedValue > 0)
{
result.Add(ParsedValue);
}
else
{
Log.WriteLog("Ignored unexpected order id from config.");
}
}
}
}
}
}
private void _loadBooleanItem(XDocument document, string itemName, ref bool result)
{
XElement Configs = document.Element("Configs");
if (Configs.Elements(itemName).Any() &&
!string.IsNullOrEmpty(Configs.Elements(itemName).First().Value))
{
bool ParsedValue;
if (Boolean.TryParse(Configs.Element(itemName).Value, out ParsedValue))
{
result = ParsedValue;
}
}
}
private void _loadByteArrayItem(XDocument document, string itemName, ref byte[] result)
{
XElement Configs = document.Element("Configs");
if (Configs.Elements(itemName).Any() &&
!string.IsNullOrEmpty(Configs.Elements(itemName).First().Value))
{
result = System.Convert.FromBase64String(Configs.Element(itemName).Value);
}
}
private string _getGeneralConfigFilename(string suffix, string extension)
{
return suffix + extension;
}
private string _getCharacterConfigFilename(string suffix, string extension)
{
Debug.Assert(!string.IsNullOrEmpty(_characterName));
return _characterName + suffix + extension;
}
/// <summary>
/// Make a time interval random to make it hard to detect
/// </summary>
/// <param name="timeInterval"></param>
/// <returns></returns>
private int makeRandom(int timeInterval)
{
Debug.Assert(timeInterval > 5000);
return _random.Next(timeInterval / 5 * 4, timeInterval / 5 * 6);
}
#endregion
}
}