-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.cs
418 lines (392 loc) · 18.4 KB
/
Options.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;
namespace Schematix
{
static class Options
{
public const String TIME_FORMAT = "yyyy.MM.dd HH:mm:ss";
// IP
public const int DEFAULT_PING_PERIOD = 3000;
public const int DEFAULT_PING_TIMEOUT_GREEN = 100; // < Green
public const int DEFAULT_PING_TIMEOUT_YELLOW = 100; // < Yellow
public const int DEFAULT_PING_TIMEOUT_RED = 100; // < Red, ... - Out
public const bool DEFAULT_PING_ONN = false;
public const int DEFAULT_PING_ARRAY = 5;
public static String RECORD_EXT_OBJECT = ".xObj";
public static String RECORD_EXT_LINK = ".xLnk";
public static String RECORD_EXT_BOX = ".xBox";
public static String RECORD_EXT_MAP = ".xMap";
public static String RECORD_EXTFILLTER_MAP = "Schematix map file|*" + RECORD_EXT_MAP;
// Object Prototype
public static readonly Color DEFAULT_OBJECT_IMAGE_COLOR = Color.Black; // for preview
public static readonly Color DEFAULT_OBJECT_APLHA_COLOR = Color.White;
// Link Prototype
public static readonly Color DEFAULT_LINK_LINE_COLOR = Color.Maroon;
// Box Prototype
public static readonly Font DEFAULT_BOX_FONT = new Font("Curier", 14);
public static readonly Color DEFAULT_BOX_TEXT_COLOR = Color.Black;
// Exemplars
public const int DEFAULT_FRAME_PADDING = 3;
// Main options
public const String iniFile = "Schematix.ini";
public const int MAX_PING_PERIOD = 24 * 3600000;
public const int MAX_PING_COUNT = 10;
public const int DEFAULT_MAP_WIDTH = 320;
public const int DEFAULT_MAP_HEIGHT = 240;
public const bool DEFAULT_MAP_AUTOSIZE = true;
// Grid
public const int MAX_GRID_STEP = 1000;
public const int DEFAULT_GRID_STEP = 32;
public const int MIN_GRID_STEP = 2;
public const int MAX_GRID_THICK = 5;
public const GridStyles DEFAULT_GRID_STYLE = GridStyles.None;
public static readonly Color DEFAULT_GRID_COLOR = Color.Silver;
// Back
public const BackgroundStyles DEFAULT_BACK_STYLE = BackgroundStyles.Color;
public const AlignTypes DEFAULT_BACK_ALIGN = AlignTypes.TopLeft;
public static readonly Color DEFAULT_BACK_COLOR = Color.SteelBlue;
public static readonly Color DEFAULT_BACK_ACOLOR = Color.White;
// Language
static public LanguageRecord LangCur = new LanguageRecord();
static public List<LanguageRecord> Langs = new List<LanguageRecord>() { LangCur };
static public String
LangPath = "",
LangName = LangCur.Name;
// Behaiour
static public int
OnStart = 0,
OnClose = 0,
PingPeriod = 200,
PingCount = 1;
static public bool
PingOnn = false;
static public xIP[] PingIPsIDs = new xIP[10];
static public Ping[] PingSenders = new Ping[10];
static private PingOptions PingOptions = new PingOptions(64, true);
// Path roots
static public String
RootMaps = "Maps",
RootObjects = "Objects",
RootLinks = "Links",
RootBoxes = "Boxes";
static public xGrid Grid = new xGrid();
static public xBackground Back = new xBackground();
// Display window
static public MainForm mainForm = null;
static public bool
MainFormXY = false,
MainFormWH = false;
static public int
MainFormX, MainFormW, PortW,
MainFormY, MainFormH, PortH;
// Catalog
static public List<xPrototype> PObjects = new List<xPrototype>();
static public List<xPrototype> PLinks = new List<xPrototype>();
static public List<xPrototype> PBoxes = new List<xPrototype>();
static public List<xMap> Maps = new List<xMap>();
static public List<String> MapFiles = new List<String>();
// Ping list
static public List<xIP> IPs = new List<xIP>();
static public int LastSendIPIdx = 0;
static public void AddIP(xIP IP)//
{
if (!IPs.Contains(IP))
IPs.Add(IP);
}
static public void RemoveIP(xIP IP)//
{
int idx = IPs.IndexOf(IP);
if (0 <= idx)
if (idx < LastSendIPIdx)
LastSendIPIdx--;
IPs.Remove(IP);
}
static public int SelectLanguage(String name)//Ok
{
int i = Langs.Count() - 1;
for (; 0 < i; i--)
if (Langs[i].Name == name)
break;
LangCur = Langs[i];
return i;
}
static public String LoadLanguages(String Path = "")//Ok
{
String errorStr = "";
if (Path == "")
Path = Directory.GetCurrentDirectory();
try
{
String[] fileNames = Directory.GetFiles(Path, "*.lng");
foreach (String fileName in fileNames)
{
try
{
using (StreamReader file = File.OpenText(fileName))
{
// Read language name
String langName = file.ReadLine();
// Try to find
int i = Langs.Count() - 1;
for (; 0 <= i; i--)
if (Langs[i].Name == langName)
break;
// Add new record
if (i < 0)
{
i = Langs.Count();
Langs.Add(new LanguageRecord(langName, i));
}
// Load rest of content
String eStr = Langs[i].LoadFromStream(file);
errorStr += (errorStr == "" ? "" : "\r\n") + eStr;
}
}
catch (Exception e)
{
errorStr += (errorStr == "" ? "" : "\r\n") + e.Message;
}
}// for each file
}
catch (Exception e)
{
return e.Message;
}
return errorStr;
}
static public int StrToInt(String value, int def = 0)//Ok
{
try {
return Convert.ToInt32(value); }
catch {
return def; }
}
static public int SetCounter(int value, int maxValue, int minValue = 0, int defaultValue = 0)//Ok
{
if (value < minValue || maxValue < value)
value = defaultValue;
if (value < minValue)
return minValue;
if (maxValue < value)
return maxValue;
return value;
}
static public int SetCounter(String str, int maxValue, int minValue = 0, int defaultValue = 0) => SetCounter(StrToInt(str, defaultValue), maxValue, minValue, defaultValue);//Ok
static public String Load(String fileName = iniFile)//
{
try
{
using (StreamReader file = File.OpenText(fileName))
{
while (!file.EndOfStream)
{
String line = file.ReadLine();
//Decompress
String[] rec = line.Split('\t');
if (rec.Count() < 2)
continue; //not an option:value pair - skip
String option = rec[0],
value = rec[1],
value2 = ((rec.Count() < 3) ? "" : rec[2]);
//Set option
switch (option)
{
//# Main
case "Language": LangName = value; break;
case "LanguagesPath": LangPath = value; break;
// Root folders
case "RootMaps": RootMaps = value; break;
case "RootObjects": RootObjects = value; break;
case "RootLinks": RootLinks = value; break;
case "RootBoxes": RootBoxes = value; break;
// Behavior
case "OnStart": OnStart = SetCounter(value, 2); break;
case "OnClose": OnClose = SetCounter(value, 2); break;
case "PingOnn": PingOnn = (value.ToUpper() == "YES"); break;
case "PingPeriod": PingPeriod = SetCounter(value, MAX_PING_PERIOD, 100, 100); break;
case "PingCount": PingCount = SetCounter(value, 10, 1); break;
// MainForm
case "MainForm:XY": MainFormX = SetCounter(value, 99999, 0);
MainFormY = SetCounter(value2, 99999, 0);
MainFormXY = true; break;
case "MainForm:WH": MainFormW = SetCounter(value, 99999, 500);
MainFormH = SetCounter(value2, 99999, 300);
MainFormWH = true; break;
//# Map
// Grid
case "GridStoreOwn": Grid.StoreOwn = (value.ToUpper() == "YES"); break;
case "GridStyle": Grid.Style = (GridStyles)SetCounter(value, 4); break;
case "GridColor": Grid.Pen.Color = Color.FromArgb(StrToInt(value)); break;
case "GridStepX": Grid.StepX = (Int16)SetCounter(value, MAX_GRID_STEP, MIN_GRID_STEP); break;
case "GridStepY": Grid.StepY = (Int16)SetCounter(value, MAX_GRID_STEP, MIN_GRID_STEP); break;
case "GridThick": Grid.Pen.Width = (Int16)SetCounter(value, MAX_GRID_THICK, 1); break;
case "GridAlign": Grid.Snap = (value.ToUpper() == "YES"); break;
// Background
case "BackgroundStoreOwn": Back.StoreOwn = (value.ToUpper() == "YES"); break;
case "BackgroundStyle": Back.Style = (BackgroundStyles)SetCounter(value, 5); break;
case "BackgroundColor": Back.Color = Color.FromArgb(StrToInt(value)); break;
case "BackgroundImagePath":
Back.Path = value;
if (File.Exists(value))
Back.Image = new Bitmap(value);
break;
case "BackgroundImageAlign": Back.Align = (AlignTypes)SetCounter(value, 8); break;
case "BackgroundImageFloat": Back.Float = (value.ToUpper() == "YES"); break;
case "BackgroundImageBuildIn": Back.BuildIn = (value.ToUpper() == "YES"); break;
//# Map list
case "MapFile": MapFiles.Add(value); break;
default:
break;
}//case parameter
}//File reading
}//File open
}
catch (Exception e)
{
return e.Message;
}
return "";
}
static public String Save(String fileName = iniFile)//!
{
try
{
using (StreamWriter file = File.CreateText(fileName))
{
//# Main
file.WriteLine("Language\t" + LangCur.Name);
file.WriteLine("LanguagesPath\t" + LangPath);
// Root folders
file.WriteLine("RootMaps\t" + RootMaps);
file.WriteLine("RootObjects\t" + RootObjects);
file.WriteLine("RootLinks\t" + RootLinks);
file.WriteLine("RootBoxes\t" + RootBoxes);
// Behavior
file.WriteLine("OnStart\t" + OnStart);
file.WriteLine("OnClose\t" + OnClose);
file.WriteLine("PingOnn\t" + (PingOnn ? "yes" : "no"));
file.WriteLine("PingPeriod\t" + PingPeriod);
file.WriteLine("PingCount\t" + PingCount);
// MainForm
file.WriteLine("MainForm:XY\t" + mainForm.Left + "\t" + mainForm.Top);
file.WriteLine("MainForm:WH\t" + mainForm.Width + "\t" + mainForm.Height);
//# Map
// Grid
file.WriteLine("GridStoreOwn\t" + (Grid.StoreOwn ? "yes" : "no"));
file.WriteLine("GridStyle\t" + (int)Grid.Style);
file.WriteLine("GridColor\t" + Grid.Pen.Color.ToArgb());
file.WriteLine("GridStepX\t" + Grid.StepX);
file.WriteLine("GridStepY\t" + Grid.StepY);
file.WriteLine("GridThick\t" + (int)Grid.Pen.Width);
file.WriteLine("GridAlign\t" + (Grid.Snap ? "yes" : "no"));
// Background
file.WriteLine("BackgroundStoreOwn\t" + (Back.StoreOwn ? "yes" : "no"));
file.WriteLine("BackgroundStyle\t" + (int)Back.Style);
file.WriteLine("BackgroundColor\t" + Back.Color.ToArgb());
file.WriteLine("BackgroundImagePath\t" + Back.Path);
file.WriteLine("BackgroundImageAlign\t" + (int)Back.Align);
file.WriteLine("BackgroundImageFloat\t" + (Back.Float ? "yes" : "no"));
file.WriteLine("BackgroundImageBuildIn\t" + (Back.BuildIn ? "yes" : "no"));
//# Map list
foreach(var Map in Maps)
if (Map.FileName != "")
file.WriteLine("MapFile\t" + Map.FileName);
}
}
catch (Exception e)
{
return e.Message;
}
return "";
}
static public void Init()//Ok
{
for (int i = PingSenders.Length - 1; 0 <= i; i--)
{
PingIPsIDs[i] = null;
PingSenders[i] = new Ping();
PingSenders[i].PingCompleted += PingCallback;
}
}
static public void timerPing_Tick(object sender, EventArgs e)//!
{
if (IPs.Count < 1)
return;
// Init loop
if (IPs.Count <= LastSendIPIdx)
LastSendIPIdx = 0;
int count = 0;
xIP startIP = IPs[LastSendIPIdx];
int SendersIdx = PingSenders.Length - 1;
do
{
// Find free sender
for (; 0 <= SendersIdx; SendersIdx--)
if (PingIPsIDs[SendersIdx] == null)
break;
// No free sender
if (SendersIdx < 0)
break;
// Send
xIP IP = IPs[LastSendIPIdx];
if (IP.AddPing(DateTime.Now))
{
PingIPsIDs[SendersIdx] = IP;
PingSenders[SendersIdx].SendPingAsync(
IP.Address,
IP.TimeOutGreen + IP.TimeOutYellow + IP.TimeOutRed,
Encoding.ASCII.GetBytes(IP.ID.ToString()),
PingOptions);
}
// Step
LastSendIPIdx++;
count++;
// Finish?
if (IPs.Count <= LastSendIPIdx)
LastSendIPIdx = 0;
if (startIP == IPs[LastSendIPIdx])
break;
} while (count < PingCount);
}
private static void PingCallback(object sender, PingCompletedEventArgs e)//!
{
// Find sender index
int SendersIdx = PingSenders.Length - 1;
for (; 0 <= SendersIdx; SendersIdx--)
if (PingSenders[SendersIdx] == sender)
break;
// Error
if (SendersIdx < 0)
return;
// Get IP record
xIP IP = PingIPsIDs[SendersIdx];
if (IPs.Contains(IP))
if (IP.Pings[0] != null)
{
// Update record
if (e.Cancelled)
IP.Pings[0].State = PingStates.Cancelled;
else
{
if (e.Error != null)
IP.Pings[0].Error = e.Error.Message;
IP.Pings[0].State = PingStates.Replayed;
IP.Pings[0].Replayer = e.Reply.Address.ToString();
IP.Pings[0].Status = e.Reply.Status;
IP.Pings[0].TripTime = (int)e.Reply.RoundtripTime;
// Update showed info
Share.lvIPs_Renew(IP.Obj_lvItem, IP);
Share.lvIPs_Renew(IP.Map_lvItem, IP);
Share.lvPings_Renew(IP.Pings_lv?.Items[0], IP, IP.Pings[0]);
}
}
PingIPsIDs[SendersIdx] = null;
}
}
}