forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResEdit.cs
405 lines (332 loc) · 14.5 KB
/
ResEdit.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
using MissionPlanner.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace resedit
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<string> list = new List<string>();
list.Add("");
CultureInfo[] temp = System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo cul in temp)
{
list.Add(cul.DisplayName + " " + cul.Name);
}
list.Sort();
comboBox1.DataSource = list;
}
void ProcessAssembly(Assembly thisAssembly)
{
Console.WriteLine("Load Assembly " + thisAssembly.FullName);
string[] test = thisAssembly.GetManifestResourceNames();
foreach (string file in test)
{
if (!file.EndsWith(".resources"))
continue;
Stream rgbxml = thisAssembly.GetManifestResourceStream(
file);
try
{
ResourceReader res = new ResourceReader(rgbxml);
IDictionaryEnumerator dict = res.GetEnumerator();
while (dict.MoveNext())
{
if (dict.Value == null)
continue;
Console.WriteLine(" {0}: '{1}' (Type {2})",
dict.Key, dict.Value, dict.Value.GetType().Name);
if (file.Contains("MissionPlanner.Strings.resources") ||
dict.Key.ToString().EndsWith(".ToolTip") || dict.Key.ToString().EndsWith(".Text") ||
dict.Key.ToString().EndsWith("HeaderText") || dict.Key.ToString().EndsWith("ToolTipText"))
{
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[colFile.Index].Value =
System.IO.Path.GetFileName(file);
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[colInternal.Index].Value =
dict.Key.ToString();
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[colEnglish.Index].Value =
dict.Value.ToString();
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[colOtherLang.Index].Value =
dict.Value.ToString();
}
}
}
catch
{
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Assembly thisAssembly = Assembly.GetExecutingAssembly();
ProcessAssembly(thisAssembly);
foreach (var item in MissionPlanner.Plugin.PluginLoader.Plugins)
{
// silent fail
try
{
ProcessAssembly(item.Assembly);
}
catch
{
}
}
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.SelectedPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
fbd.ShowDialog();
if (fbd.SelectedPath != "")
{
dataGridView1.Rows.Clear();
string[] files = System.IO.Directory.GetFiles(fbd.SelectedPath, "*.resx",
System.IO.SearchOption.AllDirectories);
string ci = "";
CultureInfo[] temp = System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo cul in temp)
{
if ((cul.DisplayName + " " + cul.Name) == comboBox1.Text)
{
Console.WriteLine(cul.Name);
ci = cul.Name;
}
}
foreach (string file in files)
{
// load only file of the slected lang
if (!file.ToLower().Contains(ci.ToString().ToLower() + ".resx"))
continue;
// dont load and tralations if no lang selected
if (file.ToLower().Contains("translation") && comboBox1.Text == "")
continue;
// must be a resx
if (!file.ToLower().EndsWith(".resx"))
continue;
ResXResourceReader reader = new ResXResourceReader(file);
Console.WriteLine(reader);
reader.BasePath = fbd.SelectedPath + System.IO.Path.DirectorySeparatorChar + "Resources";
try
{
foreach (DictionaryEntry entry in reader)
{
if (entry.Key.ToString().EndsWith(".ToolTip") || entry.Key.ToString().EndsWith(".Text") ||
entry.Key.ToString().EndsWith("HeaderText") ||
entry.Key.ToString().EndsWith("ToolTipText"))
{
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[colFile.Index].Value =
System.IO.Path.GetFileName(file);
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[colInternal.Index].Value =
entry.Key.ToString();
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[colEnglish.Index].Value =
entry.Value.ToString();
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[colOtherLang.Index].Value =
entry.Value.ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string ci = "";
CultureInfo[] temp = System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo cul in temp)
{
if ((cul.DisplayName + " " + cul.Name) == comboBox1.Text)
{
Console.WriteLine(cul.Name);
ci = cul.Name;
}
}
string fname = "";
ResXResourceWriter writer = null;
System.IO.Directory.CreateDirectory("translation");
StreamWriter sw = new StreamWriter("translation/output.html");
sw.Write("<html><body><table>");
foreach (DataGridViewRow row in dataGridView1.Rows)
{
try
{
if (row.Cells[colFile.Index].Value.ToString() != fname)
{
if (writer != null)
writer.Close();
var filename = row.Cells[colFile.Index].Value.ToString();
var strings = filename.Split('.');
var dir = "translation";
for (int a = 0; a < strings.Length - 2; a++)
{
dir += Path.DirectorySeparatorChar + strings[a];
Directory.CreateDirectory(dir);
}
writer =
new ResXResourceWriter(dir + Path.DirectorySeparatorChar + strings[strings.Length - 2] + "." + ci + ".resx");
}
if (row.Cells[colEnglish.Index].Value.ToString() != row.Cells[colOtherLang.Index].Value.ToString())
writer.AddResource(row.Cells[colInternal.Index].Value.ToString(),
row.Cells[colOtherLang.Index].Value.ToString());
fname = row.Cells[colFile.Index].Value.ToString();
}
catch
{
}
try
{
sw.Write("<tr><td>" + row.Cells[colFile.Index].Value.ToString() + "</td><td>" +
row.Cells[colInternal.Index].Value.ToString() + "</td><td>" +
row.Cells[colOtherLang.Index].Value.ToString() + "</td></tr>");
}
catch (Exception ex)
{
try
{
CustomMessageBox.Show("Failed to save " + row.Cells[colOtherLang.Index].Value.ToString() + " " +
ex.ToString());
}
catch
{
}
}
}
if (writer != null)
writer.Close();
sw.Write("</table></html>");
sw.Close();
CustomMessageBox.Show("Saved");
}
private void button3_Click(object sender, EventArgs e)
{
if (!File.Exists("translation/output.html"))
{
CustomMessageBox.Show("No existing translation has been done");
return;
}
StreamReader sr1 = new StreamReader("translation/output.html");
string file = sr1.ReadToEnd();
Regex regex = new Regex("<tr><td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td></tr>",
RegexOptions.Multiline | RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(file);
int a = 0;
foreach (Match mat in matches)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (mat.Groups.Count == 4)
{
if (row.Cells[0].Value.ToString() == mat.Groups[1].Value.ToString() &&
row.Cells[1].Value.ToString() == mat.Groups[2].Value.ToString())
{
row.Cells[3].Value = mat.Groups[3].Value.ToString();
a++;
}
}
}
}
sr1.Close();
CustomMessageBox.Show("Modified " + a + " entries");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string ci = "";
CultureInfo[] temp = System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (CultureInfo cul in temp)
{
if ((cul.DisplayName + " " + cul.Name) == comboBox1.Text)
{
Console.WriteLine(cul.Name);
ci = cul.Name;
}
}
Assembly thisAssembly = null;
try
{
string fn = Settings.GetRunningDirectory() + ci +
Path.DirectorySeparatorChar + "MissionPlanner.resources.dll";
if (File.Exists(fn))
thisAssembly = Assembly.LoadFile(fn);
else
return;
}
catch
{
return;
}
string[] test = thisAssembly.GetManifestResourceNames();
Encoding unicode = Encoding.Unicode;
foreach (string file in test)
{
Stream rgbxml = thisAssembly.GetManifestResourceStream(
file);
try
{
ResourceReader res = new ResourceReader(rgbxml);
IDictionaryEnumerator dict = res.GetEnumerator();
while (dict.MoveNext())
{
try
{
Console.WriteLine(" {0}: '{1}' (Type {2})",
dict.Key, dict.Value, dict.Value.GetType().Name);
if (dict.Value is Size)
continue;
string thing = (string)dict.Value;
// dataGridView1.Rows[0].Cells[colOtherLang.Index].Value = dict.Value.ToString();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string t2 = file.Replace(ci + ".", "");
if (row.Cells[0].Value.ToString() == t2 &&
row.Cells[1].Value.ToString() == dict.Key.ToString())
{
row.Cells[3].Value = thing;
}
}
}
catch
{
}
}
}
catch
{
}
}
CustomMessageBox.Show("Loaded Existing");
}
private void BUT_clipboard_Click(object sender, EventArgs e)
{
string sb = "";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string lastitem = "";
foreach (DataGridViewCell item in row.Cells)
{
lastitem = item.Value.ToString();
sb += '"' + item.Value.ToString() + '"' + ",";
}
sb += "\n";
}
Clipboard.SetText(sb);
}
/*
*/
}
}