-
Notifications
You must be signed in to change notification settings - Fork 3
/
GMLayRenamerForm.cs
673 lines (617 loc) · 29.9 KB
/
GMLayRenamerForm.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
namespace KMZRebuilder
{
public partial class GMLayRenamerForm : Form
{
private bool noGML = false;
public bool noGPI = true;
public bool isCRC = false;
private string filename = "";
private string xml = "";
private XmlDocument xd = null;
public List<Category> categories = new List<Category>();
private List<Symbol> symbols = new List<Symbol>();
public GMLayRenamerForm(string filename)
{
InitializeComponent();
this.button2.Visible = false;
this.button3.Visible = false;
this.subtext.Visible = true;
this.filename = filename;
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
this.xml = sr.ReadToEnd();
sr.Close();
fs.Close();
this.xml = KMFile.RemoveXMLNamespaces(this.xml);
this.xd = new XmlDocument();
this.xd.LoadXml(xml);
int no = 0;
foreach (XmlNode xn in xd.SelectNodes("GPI/Group/CategoryList/Category"))
{
string id = xn.SelectSingleNode("ID").ChildNodes[0].Value;
string CustomSymbol = xn.SelectSingleNode("CustomSymbol").ChildNodes[0].Value;
string Name = xn.SelectSingleNode("Name/LString[@lang='RU']").ChildNodes[0].Value;
categories.Add(new Category(id, CustomSymbol, Name));
layers.Items.Add(String.Format("{0}: {1}", no++, Name), CustomSymbol);
};
string path = System.IO.Path.GetDirectoryName(filename) + @"\";
foreach (XmlNode xn in xd.SelectNodes("GPI/Group/SymbolList/Symbol"))
{
string id = xn.SelectSingleNode("ID").ChildNodes[0].Value;
string file = path + xn.SelectSingleNode("File").ChildNodes[0].Value;
symbols.Add(new Symbol(id, file));
Image im = Image.FromFile(file);
images.Images.Add(id, (Image)(new Bitmap(im)));
im.Dispose();
};
}
public GMLayRenamerForm()
{
noGML = true;
InitializeComponent();
layers.ContextMenuStrip = null;
}
public static GMLayRenamerForm FromGPIGPX(Dictionary<string,string> files, string dir)
{
GMLayRenamerForm res = new GMLayRenamerForm();
res.layers.ContextMenuStrip = res.contextMenuStrip1;
res.loadImagesToolStripMenuItem.Visible = false;
res.saveImagesToolStripMenuItem.Visible = false;
res.layers.FullRowSelect = true;
res.button2.Visible = false;
res.button3.Visible = false;
res.subtext.Visible = true;
int no = 0;
foreach (KeyValuePair<string, string> kvp in files)
{
res.categories.Add(new Category(kvp.Key, kvp.Key, kvp.Value));
res.layers.Items.Add(String.Format("{0}: {1}", no++, kvp.Value), kvp.Key);
res.symbols.Add(new Symbol(kvp.Key, kvp.Value));
Image im = Image.FromFile(dir + kvp.Key + ".bmp");
res.images.Images.Add(kvp.Key, (Image)(new Bitmap(im)));
im.Dispose();
};
res.noGPI = false;
return res;
}
public static GMLayRenamerForm ForCRCCheckSum(string[] files, Dictionary<string, string> d4crc_names, Dictionary<string, string> d4crc_subnames)
{
GMLayRenamerForm res = new GMLayRenamerForm();
res.layers.Columns[0].Width = 450;
if (res.layers.Columns.Count == 1)
res.layers.Columns.Add(new ColumnHeader());
res.layers.Columns[1].Width = 100;
res.layers.FullRowSelect = true;
res.Text = "GPI Layers Names Editor by Images CRC";
res.label1.Text = "You can enter names for images styles here (gpi_name_*):";
res.button2.Text = "Save to ...";
res.button3.Text = "Load from ...";
res.subtext.Visible = true;
res.layers.ContextMenuStrip = res.contextMenuStrip2;
CRC32 crc = new CRC32();
foreach (string file in files)
{
string sn = Path.GetFileNameWithoutExtension(file);
string cc = crc.CRC32Num(file).ToString();
bool skip = false;
foreach (Category cat in res.categories)
if (cat.ID == cc) skip = true;
if (skip)
continue;
bool chd = false;
string nm = "";
if ((d4crc_names != null) && (d4crc_names.ContainsKey(cc)))
{
nm = d4crc_names[cc];
chd = false;
};
if ((d4crc_subnames != null) && (d4crc_subnames.ContainsKey(cc)))
{
nm = d4crc_subnames[cc];
chd = true;
};
res.categories.Add(new Category(cc, sn, nm));
ListViewItem lvi = new ListViewItem(new string[] { nm, chd ? "subname" : "name" }, sn);
lvi.SubItems[0].BackColor = chd ? Color.LightPink : res.layers.BackColor;
res.layers.Items.Add(lvi);
res.symbols.Add(new Symbol(sn, sn));
Image im = Image.FromFile(file);
res.images.Images.Add(sn, (Image)(new Bitmap(im)));
im.Dispose();
};
res.isCRC = true;
return res;
}
private void GMLayRenamer_FormClosing(object sender, FormClosingEventArgs e)
{
if (noGML) return;
images.Dispose();
if (this.DialogResult == DialogResult.OK)
{
XmlNodeList nl = xd.SelectNodes("GPI/Group/CategoryList/Category");
for (int i = 0; i < nl.Count;i++)
{
XmlNode xn = nl[i];
xn.SelectSingleNode("Name/LString[@lang='RU']").ChildNodes[0].Value = categories[i].name;
xn.SelectSingleNode("Name/LString[@lang='EN']").ChildNodes[0].Value = KMZRebuilederForm.GetTranslit(categories[i].name);
};
XmlNode n = xd.SelectSingleNode("GPI");
FileStream fs = new FileStream(this.filename, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sw.WriteLine("<GPI xmlns=\"http://www.garmin.com\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.garmin.com GPI_XML.xsd\">");
sw.WriteLine(n.InnerXml);
sw.WriteLine("</GPI>");
sw.Close();
fs.Close();
};
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
changeBitmapToolStripMenuItem.Enabled = renameToolStripMenuItem.Enabled = layers.SelectedIndices.Count > 0;
}
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isCRC)
{
if (layers.SelectedIndices.Count == 0) return;
string nme = categories[layers.SelectedIndices[0]].name;
if (KMZRebuilederForm.InputBox("Layer name", "Change layer name:", ref nme, (Bitmap)images.Images[layers.Items[layers.SelectedIndices[0]].ImageKey]) != DialogResult.OK)
return;
categories[layers.SelectedIndices[0]].name = nme;
layers.Items[layers.SelectedIndices[0]].Text = nme;
};
if (noGML && noGPI) return;
if (layers.SelectedIndices.Count == 0) return;
string name = categories[layers.SelectedIndices[0]].name;
if (KMZRebuilederForm.InputBox("Layer name", "Change layer name:", ref name, (Bitmap)images.Images[layers.Items[layers.SelectedIndices[0]].ImageKey]) != DialogResult.OK)
return;
if (!noGPI)
{
name = name.Trim(Path.GetInvalidFileNameChars()).Trim();
for (int i = 0; i < categories.Count; i++)
{
if (i == layers.SelectedIndices[0]) continue;
if (categories[i].name == name)
{
MessageBox.Show("Categoty with name `" + name + "` already exists!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
};
};
};
categories[layers.SelectedIndices[0]].name = name;
layers.Items[layers.SelectedIndices[0]].Text = String.Format("{0}: {1}", layers.SelectedIndices[0], name);
}
private void layers_DoubleClick(object sender, EventArgs e)
{
renameToolStripMenuItem_Click(sender, e);
}
private void changeBitmapToolStripMenuItem_Click(object sender, EventArgs e)
{
if (layers.SelectedIndices.Count == 0) return;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Bitmap files (*.bmp)|*.bmp";
ofd.DefaultExt = ".bmp";
if (ofd.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(ofd.FileName);
if ((bmp.Height != 22) || (bmp.Width != 22) || (bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed))
MessageBox.Show("Error", "Bitmap must be 22x22 pixels 8bpp format", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
string img = layers.Items[layers.SelectedIndices[0]].ImageKey;
images.Images.RemoveByKey(img);
images.Images.Add(img, (Image)new Bitmap(bmp));
Symbol sym = null;
foreach (Symbol sm in symbols)
if (categories[layers.SelectedIndices[0]].CustomSymbol == sm.ID)
sym = sm;
File.Copy(ofd.FileName, sym.file, true);
};
bmp.Dispose();
};
ofd.Dispose();
}
private void saveListToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text files (*.txt)|*.txt";
sfd.DefaultExt = ".txt";
sfd.FileName = "layernames.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(1251));
for (int i = 0; i < categories.Count; i++)
sw.WriteLine(categories[i].name);
sw.Close();
fs.Close();
MessageBox.Show(String.Format("{0} layers names saved", categories.Count), "Save layers names", MessageBoxButtons.OK, MessageBoxIcon.Information);
};
sfd.Dispose();
}
private void loadNamesToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text files (*.txt)|*.txt";
ofd.DefaultExt = ".txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(1251));
List<string> ltmp = new List<string>();
while (!sr.EndOfStream) ltmp.Add(sr.ReadLine());
sr.Close();
fs.Close();
if (ltmp.Count != categories.Count)
{
MessageBox.Show("In text file count of lines must be " + layers.Items.Count.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
for (int i = 0; i < categories.Count; i++)
{
categories[i].name = ltmp[i];
layers.Items[i].Text = String.Format("{0}: {1}", i, ltmp[i]);
};
MessageBox.Show(categories.Count.ToString() + " layer names updated!","Load layers names",MessageBoxButtons.OK,MessageBoxIcon.Information);
};
};
ofd.Dispose();
}
private void saveImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Zip archives (*.zip)|*.zip";
sfd.DefaultExt = ".zip";
sfd.FileName = "layersprofile.zip";
if (sfd.ShowDialog() == DialogResult.OK)
{
FileStream fsOut = File.Create(sfd.FileName);
ZipOutputStream zipStream = new ZipOutputStream(fsOut);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
//names
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms, System.Text.Encoding.GetEncoding(1251));
for (int i = 0; i < categories.Count; i++)
sw.WriteLine(categories[i].name);
sw.Flush();
ms.Flush();
ms.Position = 0;
ZipEntry newEntry = new ZipEntry("names.txt");
newEntry.DateTime = DateTime.Now;
newEntry.Size = ms.Length;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
StreamUtils.Copy(ms, zipStream, buffer);
zipStream.CloseEntry();
sw.Close();
ms.Close();
// images
try
{
for (int i = 0; i < symbols.Count; i++)
ZipFile(symbols[i].file, zipStream);
}
catch { };
zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
zipStream.Close();
MessageBox.Show(String.Format("{0} layers names and {1} images saved", categories.Count, symbols.Count), "Save layers names and images", MessageBoxButtons.OK, MessageBoxIcon.Information);
};
sfd.Dispose();
}
private void ZipFile(string filename, ZipOutputStream zipStream)
{
FileInfo fi = new FileInfo(filename);
string entryName = fi.Name;
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime;
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(filename))
StreamUtils.Copy(streamReader, zipStream, buffer);
zipStream.CloseEntry();
}
private void loadImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Zip archives (*.zip)|*.zip";
ofd.DefaultExt = ".zip";
if (ofd.ShowDialog() == DialogResult.OK)
{
ExtractZipFile(ofd.FileName);
};
ofd.Dispose();
}
public void ExtractZipFile(string archiveFilenameIn)
{
ZipFile zf = null;
string txtT = "";
string txtI = "";
try
{
int imgsupd = 0;
FileStream fs = File.OpenRead(archiveFilenameIn);
zf = new ZipFile(fs);
foreach (ZipEntry zipEntry in zf)
{
if (!zipEntry.IsFile)
continue; // Ignore directories
String entryFileName = zipEntry.Name;
byte[] buffer = new byte[4096]; // 4K is optimum
Stream zipStream = zf.GetInputStream(zipEntry);
int file_id = 0;
if (entryFileName.ToLower() == "names.txt")
{
DialogResult dr = MessageBox.Show("Do you want to load names from selected file?", "Layer names", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (dr == DialogResult.Cancel) break;
if (dr == DialogResult.Yes)
{
MemoryStream ms = new MemoryStream();
StreamUtils.Copy(zipStream, ms, buffer);
ms.Position = 0;
StreamReader sr = new StreamReader(ms, System.Text.Encoding.GetEncoding(1251));
List<string> ltmp = new List<string>();
while (!sr.EndOfStream) ltmp.Add(sr.ReadLine());
sr.Close();
ms.Close();
if (ltmp.Count != categories.Count)
{
txtT += "No layer names updated!";
MessageBox.Show("In text file count of lines must be " + layers.Items.Count.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
for (int i = 0; i < categories.Count; i++)
{
categories[i].name = ltmp[i];
layers.Items[i].Text = String.Format("{0}: {1}", i, ltmp[i]);
};
txtT += categories.Count.ToString() + " layer names updated!";
};
};
}
else
{
if (!int.TryParse(System.IO.Path.GetFileNameWithoutExtension(zipEntry.Name), out file_id)) file_id = -1;
if ((file_id < 0) || (file_id > (symbols.Count - 1))) continue;
if (imgsupd == 0)
{
DialogResult dr = MessageBox.Show("Do you want to update images from selected file?", "Layer images", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (dr == DialogResult.Cancel) break;
if (dr == DialogResult.No) break;
};
Symbol sym = symbols[file_id];
using (FileStream streamWriter = File.Create(sym.file))
StreamUtils.Copy(zipStream, streamWriter, buffer);
images.Images.RemoveByKey(sym.ID);
Bitmap bmp = new Bitmap(sym.file);
images.Images.Add(sym.ID, (Image)new Bitmap(bmp));
bmp.Dispose();
imgsupd++;
};
};
if (imgsupd > 0)
{
txtI = imgsupd.ToString() + " layer images updated!";
};
}
finally
{
if (zf != null)
{
zf.IsStreamOwner = true; // Makes close also shut the underlying stream
zf.Close(); // Ensure we release resources
}
};
string txt = txtT + ((txtT == "" ? "" : "\r\n")) + txtI;
if (txt != "")
MessageBox.Show(txt, "Update layers", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void layers_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 113) layers_DoubleClick(sender, e);
if (e.KeyValue == 13) layers_DoubleClick(sender, e);
// if ((layers.CheckBoxes == false) && (e.KeyValue == 32)) layers_DoubleClick(sender, e);
if (e.KeyValue == 32) switchsub();
if (e.KeyValue == 37) switchsub();
if (e.KeyValue == 39) switchsub();
}
public bool IsSubName(string imageIndex)
{
if (layers.Items.Count == 0) return false;
if (String.IsNullOrEmpty(imageIndex)) return false;
for (int ii = 0; ii < layers.Items.Count; ii++)
if (layers.Items[ii].ImageKey == imageIndex)
if(layers.Items[ii].SubItems.Count > 1)
if(layers.Items[ii].SubItems[1].Text == "subname")
return true;
return false;
}
private void button3_Click(object sender, EventArgs e)
{
if (layers.Items.Count == 0) return;
if (isCRC)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Load Names From File";
ofd.DefaultExt = ".txt";
ofd.Filter = "Text Files (*.txt)|*.txt";
if (ofd.ShowDialog() == DialogResult.OK)
{
Regex rxgpinn = new Regex(@"gpi_name_(?<crc>[^=]+)\s*=(?<name>[^\r\n]+)", RegexOptions.IgnoreCase);
Regex rxgpisn = new Regex(@"gpi_subname_(?<crc>[^=]+)\s*=(?<name>[^\r\n]+)", RegexOptions.IgnoreCase);
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, true);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if(line.StartsWith("#")) continue;
MatchCollection mc;
if ((mc = rxgpisn.Matches(line)).Count > 0)
{
string cc = mc[0].Groups["crc"].Value;
string nn = mc[0].Groups["name"].Value;
for (int i = 0; i < this.categories.Count; i++)
if (this.categories[i].ID == cc)
{
this.categories[i].name = nn;
this.layers.Items[i].SubItems[0].Text = nn;
this.layers.Items[i].SubItems[1].Text = "subname";
this.layers.Items[i].SubItems[0].BackColor = Color.LightPink;
};
};
if ((mc = rxgpinn.Matches(line)).Count > 0)
{
string cc = mc[0].Groups["crc"].Value;
string nn = mc[0].Groups["name"].Value;
for (int i = 0; i < this.categories.Count; i++)
if (this.categories[i].ID == cc)
{
this.categories[i].name = nn;
this.layers.Items[i].SubItems[0].Text = nn;
this.layers.Items[i].SubItems[1].Text = "name";
this.layers.Items[i].SubItems[0].BackColor = this.layers.BackColor;
};
};
};
sr.Close();
fs.Close();
}
ofd.Dispose();
return;
};
for (int i = 0; i < layers.Items.Count; i++)
layers.Items[i].Checked = false;
}
private void button2_Click(object sender, EventArgs e)
{
if (layers.Items.Count == 0) return;
if (isCRC)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Save Names To File";
sfd.DefaultExt = ".txt";
sfd.Filter = "Text Files (*.txt)|*.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
List<string> gSave = new List<string>();
List<string> gLoad = new List<string>();
foreach (Category cat in categories)
if (!String.IsNullOrEmpty(cat.name))
{
if (this.IsSubName(cat.CustomSymbol))
gSave.Add("gpi_subname_" + cat.ID + "=" + cat.name);
else
gSave.Add("gpi_name_" + cat.ID + "=" + cat.name);
};
FileStream fs;
if(File.Exists(sfd.FileName))
{
fs = new FileStream(sfd.FileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, true);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (String.IsNullOrEmpty(line)) continue;
gLoad.Add(line);
};
sr.Close();
fs.Close();
};
Regex rxgpiun = new Regex(@"gpi_(?:sub)?name_(?<crc>\d+)=", RegexOptions.IgnoreCase);
foreach (string gs in gSave)
{
Match mgs = null;
if (!(mgs = rxgpiun.Match(gs)).Success) continue;
bool ex = false;
for (int ii = 0; ii < gLoad.Count; ii++)
{
string glt = gLoad[ii];
Match mgl = null;
if (((mgl = rxgpiun.Match(glt)).Success) && (mgs.Groups["crc"].Value == mgl.Groups["crc"].Value))
{
ex = true;
gLoad[ii] = gs;
ii = gLoad.Count;
};
}
if (!ex)
gLoad.Add(gs);
};
fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
foreach (string gl in gLoad) sw.WriteLine(gl);
sw.Close();
fs.Close();
}
sfd.Dispose();
return;
};
for (int i = 0; i < layers.Items.Count; i++)
layers.Items[i].Checked = true;
}
private void layers_SelectedIndexChanged(object sender, EventArgs e)
{
selttl.Text = layers.CheckedIndices.Count.ToString() + " / " + layers.Items.Count.ToString();
}
private void GMLayRenamerForm_Shown(object sender, EventArgs e)
{
layers_SelectedIndexChanged(this, e);
}
private void layers_ItemChecked(object sender, ItemCheckedEventArgs e)
{
layers_SelectedIndexChanged(this, e);
}
private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
{
switchToSubnameToolStripMenuItem.Enabled = (layers.SelectedItems.Count > 0) && (layers.SelectedItems[0].SubItems.Count > 1);
switchToSubnameToolStripMenuItem.Text = "Switch to " + (layers.SelectedItems[0].SubItems[1].Text == "name" ? "SubName" : "Name");
}
private void switchToSubnameToolStripMenuItem_Click(object sender, EventArgs e)
{
switchsub();
}
private void switchsub()
{
if ((layers.SelectedItems.Count == 0) || (layers.SelectedItems[0].SubItems.Count < 2)) return;
layers.SelectedItems[0].SubItems[1].Text = (layers.SelectedItems[0].SubItems[1].Text == "name" ? "subname" : "name");
layers.SelectedItems[0].SubItems[0].BackColor = (layers.SelectedItems[0].SubItems[1].Text == "name" ? layers.BackColor : Color.LightPink);
}
}
public class Category
{
public string ID;
public string CustomSymbol;
public string name;
public Category(string ID, string CustomSymbol, string name)
{
this.ID = ID;
this.CustomSymbol = CustomSymbol;
this.name = name;
}
}
public class Symbol
{
public string ID;
public string file;
public Symbol(string ID, string file)
{
this.ID = ID;
this.file = file;
}
}
}