-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.cs
691 lines (630 loc) · 27 KB
/
Common.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using NetDimension.NanUI.JavaScript;
using NetDimension.NanUI;
using NetDimension.NanUI.HostWindow;
using NetDimension.NanUI.EmbeddedFileResource;
using Microsoft.Win32;
namespace CSUR.Apps
{
public class Common
{
public static bool yn;
public static void TestShow()
{
MessageBox.Show("Test");
}
public static string getRoadimporter_CO()
{
string _userName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string destDir = _userName + "\\Colossal Order\\Cities_Skylines\\RoadImporter";
return destDir;
}
public static string getUsername()
{
string _userName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return _userName;
}
public static string getRoadimporter_Mods()
{
string _userName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string destDir = _userName + "\\Colossal Order\\Cities_Skylines\\Addons\\Mods\\Roadimporter";
return destDir;
}
public static bool Roadimpoter_Check()
{
Console.WriteLine(getRoadimporter_Mods() + "\\RoadImporter.dll");
if(File.Exists(getRoadimporter_Mods() + "\\RoadImporter.dll") == true)
{
return true;
}
else
{
return false;
}
}
public delegate void CopyDirCallback(bool _error, bool _finish);
public static void CopyDir(string _source, string _dest, CopyDirCallback _callback)
{
Thread _thread = new Thread(new ThreadStart(delegate ()
{
try
{
if (!CopyDir(_source, _dest))
{
_callback(true, true);
}
else
{
_callback(false, true);
}
}
catch (Exception)
{
_callback(true, true);
};
}));
_thread.Start();
}
private static bool CopyDir(string _source, string _dest)
{
bool retValue = false;
try
{
_source = _source.EndsWith(@"\") ? _source : _source + @"\";
_dest = _dest.EndsWith(@"\") ? _dest : _dest + @"\";
if (Directory.Exists(_source))
{
if (Directory.Exists(_dest) == false)
{
Directory.CreateDirectory(_dest);
}
foreach (string _file in Directory.GetFiles(_source))
{
FileInfo _fileInfo = new FileInfo(_file);
_fileInfo.CopyTo(_dest + _fileInfo.Name, true);
}
foreach (string _dir in Directory.GetDirectories(_source))
{
DirectoryInfo _dirInfo = new DirectoryInfo(_dir);
if (CopyDir(_dir, _dest + _dirInfo.Name) == false)
{
retValue = false;
}
}
retValue = true;
}
}
catch (Exception) { };
return retValue;
}
///判断CSUR代码仓库是否存在
public static bool CheckCSURMaster()
{
Console.WriteLine(Directory.GetCurrentDirectory());
if (Directory.Exists(Directory.GetCurrentDirectory() + "\\CSUR-master") == true)
{
return true;
}
else
{
return false;
}
}
//判断贴图包是否安装到CSL目录下
public static bool CheckTexturesPack()
{
Console.WriteLine(getRoadimporter_CO() + "\\textures");
if(Directory.Exists(getRoadimporter_CO() + "\\textures") == true)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 判断Blender是否安装
/// </summary>
/// <returns></returns>
public static bool checkBlender()
{
Process[] _blenders_opend = Process.GetProcessesByName("Blender");
if(_blenders_opend.Length > 0)
{
return true;
}
else
{
Process _c_b = new Process();
_c_b.StartInfo.FileName = "cmd.exe";
_c_b.StartInfo.UseShellExecute = false;
_c_b.StartInfo.RedirectStandardInput = true;
_c_b.StartInfo.RedirectStandardOutput = true;
_c_b.StartInfo.RedirectStandardError = true;
_c_b.StartInfo.CreateNoWindow = true;
_c_b.Start();
_c_b.StandardInput.WriteLine("Blender" + "&&exit");
_c_b.StandardInput.AutoFlush = true;
_c_b.WaitForExit(2000);
_c_b.Close();
Process[] _blenders = Process.GetProcessesByName("Blender");
if (_blenders.Length > 0)
{
Process _c_b_close = new Process();
_c_b_close.StartInfo.FileName = "cmd.exe";
_c_b_close.StartInfo.UseShellExecute = false;
_c_b_close.StartInfo.RedirectStandardInput = true;
_c_b_close.StartInfo.RedirectStandardOutput = true;
_c_b_close.StartInfo.RedirectStandardError = true;
_c_b_close.StartInfo.CreateNoWindow = true;
_c_b_close.Start();
_c_b_close.StandardInput.WriteLine("taskkill /f /t /im Blender.exe");
_c_b_close.StandardInput.AutoFlush = true;
_c_b_close.WaitForExit(2000);
_c_b_close.Close();
return true;
}
else
{
return false;
}
}
}
public static string GetEnvironmentVariable()
{
RegistryKey regLocalMachine = Registry.CurrentUser;
RegistryKey regEnvironment = regLocalMachine.OpenSubKey("Environment", true);
string sPath = regEnvironment.GetValue("path").ToString();//读取path的值
Console.WriteLine(sPath);
bool _is_blender = sPath.Contains("Blender");
if(_is_blender == true)
{
return "Blender OK";
}
else
{
return "Blender NO";
}
}
public static string GetCRMPath()
{
return Directory.GetCurrentDirectory() + "\\CSUR-master";
}
}
public static class Common_Var
{
/// <summary>
/// 常用变量
/// </summary>
public static bool Blender;
public static string CRM_path = "";
public static string CEM_path = "";
public static string CSL_path = "";
public static string CRM_ver = "";
public static string CEM_ver = "";
public static bool CRM;
public static bool Ri_mods;
public static string Roadimporter_CO = "";
public static string Roadimporter_Mods = "";
public static string w_dir = Directory.GetCurrentDirectory();
public static string config = Directory.GetCurrentDirectory() + "\\config.txt";
}
public static class CSUR_Generator_Var
{
public static string Task = "./make.bat";
public static string RoadeCode = "";
public static string Compress = "";
public static string Express = "";
public static string Filp = "";
public static string Bar = "";
public static bool CanStart = false;
public static bool _f_contain= false;
}
public static class Generator_Program
{
public static string _Generat_result;
public static string _Generat_Code;
//复制文件程序
public static bool CopyDir(string _source, string _dest)
{
bool retValue = false;
try
{
_source = _source.EndsWith(@"\") ? _source : _source + @"\";
_dest = _dest.EndsWith(@"\") ? _dest : _dest + @"\";
if (Directory.Exists(_source))
{
if (Directory.Exists(_dest) == false)
{
Directory.CreateDirectory(_dest);
}
foreach (string _file in Directory.GetFiles(_source))
{
FileInfo _fileInfo = new FileInfo(_file);
_fileInfo.CopyTo(_dest + _fileInfo.Name, true);
}
foreach (string _dir in Directory.GetDirectories(_source))
{
DirectoryInfo _dirInfo = new DirectoryInfo(_dir);
if (CopyDir(_dir, _dest + _dirInfo.Name) == false)
{
retValue = false;
}
}
retValue = true;
}
}
catch (Exception) { };
return retValue;
}
//生成器程序
public static void Generator_S()
{
Process proc = null;
if (Common.GetCRMPath() != "" && Common.GetCRMPath() != null)
{
string Path = Common.GetCRMPath();
}
else
{
_Generat_result = "CSUR Master error";
}
CSUR_Generator_Var.Bar = "15";
//检查是否有文件残留,如果没有残留则直接生成
if (Directory.Exists(Common.getRoadimporter_CO() + "\\import") == true)//检查CSL C盘根目录import目录是否存在,如果存在则清理残留
{
DirectoryInfo _del_imp_CO = new DirectoryInfo(Common.getRoadimporter_CO() + "\\import");//检查CSL C盘根目录import的文件残留
_del_imp_CO.Delete(true);
Directory.CreateDirectory(Common.getRoadimporter_CO() + "\\import");//删除完毕后重建确保程序正确运行
FileInfo _del_imp_CO_txt = new FileInfo(Common.getRoadimporter_CO() + "\\imports.txt");//imports.txt由生成器自动生成
_del_imp_CO_txt.Delete();
if (Directory.Exists(Common.GetCRMPath() + "\\output") == true)//检查CSUR代码仓库残留,有则删除
{
DirectoryInfo _del_CRM_imp = new DirectoryInfo(Common.GetCRMPath() + "\\output");
_del_CRM_imp.Delete(true);
///output为自动生成,可以不用程序创建
///import删除完毕后需要重建文件夹
}
}
else
{
Directory.CreateDirectory(Common.getRoadimporter_CO() + "\\import");//如果文件夹不存在则创建
}
CSUR_Generator_Var.Bar = "45";
///生成环境二次确认完毕
///开始生成
string GeneratorPath = Common.GetCRMPath();//生成器运行目录
proc = new Process();
proc.StartInfo.WorkingDirectory = GeneratorPath;
proc.StartInfo.FileName = Common.GetCRMPath() + "\\make.bat";
proc.StartInfo.Arguments = string.Format(_Generat_Code);//this is argument
//proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.CreateNoWindow = false;
proc.Start();
CSUR_Generator_Var.Bar = "65";
proc.WaitForExit();
///通过判断imports.txt有没有生成来进行一级判断
///再通过imports.txt的内容能否和RoadCode对应
///如果可以,则代表生成成功
///反之代表生成失败或者错误
CSUR_Generator_Var.Bar = "75";
Console.WriteLine(Directory.Exists(GeneratorPath + "\\output"));
if (Directory.Exists(GeneratorPath + "\\output") == true)
{
Console.WriteLine(File.Exists(GeneratorPath + "\\output\\imports.txt"));
if (File.Exists(GeneratorPath + "\\output\\imports.txt") == true)
{
if (Directory.Exists(GeneratorPath + "\\import") == true)
{
DirectoryInfo _old_import = new DirectoryInfo(GeneratorPath + "\\import");
_old_import.Delete(true);
Directory.Move(GeneratorPath + "\\output", GeneratorPath + "\\import");
//将生成的文件移动至CSL C盘根目录Roadimporter文件夹
CopyDir(GeneratorPath + "\\import", Common.getRoadimporter_CO() + "\\import");
//将imports.txt复制到上一级目录(Roadimporter)
if(Generator_Bo() == true)
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "1 True";//全部执行完毕返回true,程序关闭
}
else
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "false \n output failed";
}
}
else
{
Directory.Move(GeneratorPath + "\\output", GeneratorPath + "\\import");//将生成的文件移动至CSL C盘根目录Roadimporter文件夹
CopyDir(GeneratorPath + "\\import", Common.getRoadimporter_CO() + "\\import");
//将imports.txt复制到上一级目录(Roadimporter)
if (Generator_Bo() == true)
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "1 True";//全部执行完毕返回true,程序关闭
}
else
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "false \n output failed";
}
CSUR_Generator_Var.Bar = "100";
_Generat_result = "1 True";//全部执行完毕返回true,程序关闭
}
}
else
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "false \n output failed";
}
}
else
{
Directory.CreateDirectory(GeneratorPath + "\\output");
if (Directory.Exists(GeneratorPath + "\\import") == true)
{
DirectoryInfo _old_import = new DirectoryInfo(GeneratorPath + "\\import");
_old_import.Delete(true);
Directory.Move(GeneratorPath + "\\output", GeneratorPath + "\\import");
//将生成的文件移动至CSL C盘根目录Roadimporter文件夹
CopyDir(GeneratorPath + "\\import", Common.getRoadimporter_CO() + "\\import");
//将imports.txt复制到上一级目录(Roadimporter)
if (Generator_Bo() == true)
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "1 True";//全部执行完毕返回true,程序关闭
}
else
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "false \n output failed";
}
CSUR_Generator_Var.Bar = "100";
_Generat_result = "1 True";//全部执行完毕返回true,程序关闭
}
else
{
Directory.Move(GeneratorPath + "\\output", GeneratorPath + "\\import");//将生成的文件移动至CSL C盘根目录Roadimporter文件夹
CopyDir(GeneratorPath + "\\import", Common.getRoadimporter_CO() + "\\import");
//将imports.txt复制到上一级目录(Roadimporter)
if (Generator_Bo() == true)
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "1 True";//全部执行完毕返回true,程序关闭
}
else
{
CSUR_Generator_Var.Bar = "100";
_Generat_result = "false \n output failed";
}
CSUR_Generator_Var.Bar = "100";
_Generat_result = "1 True";//全部执行完毕返回true,程序关闭
}
}
}
public static bool Generator_Bo()
{
if(File.Exists(Common.getRoadimporter_CO() + "\\import\\imports.txt") == true)
{
FileInfo importtxt = new FileInfo(Common.getRoadimporter_CO() + "\\import\\imports.txt");
importtxt.MoveTo(Common.getRoadimporter_CO() + "\\imports.txt");
return true;
}
else
{
return false;
}
}
}
public static class Installation
{
public static string _instal_callback_mod = "";
public static string _instal_callback_blender = "";
public static string _instal_callback_texture = "";
public static bool _install_status_mod;
public static bool _install_status_blender;
public static bool _install_status_texture;
public static void _install_all()
{
if(Directory.Exists(Common.getRoadimporter_Mods()) == true)//先判断C盘的MOD文件夹是否存在
{
if (Directory.Exists(Common.GetCRMPath() + "\\Bin") == true)//其次判断代码仓库的bin文件夹是否存在
{
if (File.Exists(Common.getRoadimporter_Mods() + "\\Roadimporter.dll") == true)
{
_instal_callback_mod = "Mods installed";
_install_status_mod = true;
}
else if (File.Exists(Common.GetCRMPath() + "\\Bin\\Roadimporter.dll") == true)//最后判断MOD的文件是否存在
{
_install_dll();//如果存在进行安装
_instal_callback_mod = "Mods installed";
_install_status_mod = true;
}
else
{
_instal_callback_mod = "Mods install failed \n mod not found \n path:" + Common.GetCRMPath() + "\\Bin\\Roadimporter.dll";//如果不存在返回错误详情
_install_status_mod = false;
}
}
else
{
_instal_callback_mod = "Mods install failed \n Directory not found \n Path:" + Common.GetCRMPath() + "\\Bin";//如果不存在返回错误详情
_install_status_mod = false;
}
}
else
{
Directory.CreateDirectory(Common.getRoadimporter_Mods());
if (Directory.Exists(Common.GetCRMPath() + "\\Bin") == true)
{
if (File.Exists(Common.GetCRMPath() + "\\Bin\\Roadimporter.dll") == true)
{
_install_dll();
_instal_callback_mod = "Mods installed";
_install_status_mod = true;
}
}
else
{
_instal_callback_mod = "Mods install failed \n mod not found";//如果不存在返回错误详情
_install_status_mod = false;
}
}
if (Common.checkBlender() == true)
{
RegistryKey regLocalMachine = Registry.CurrentUser;
RegistryKey regEnvironment = regLocalMachine.OpenSubKey("Environment", true);
if (regEnvironment.GetValue("path").ToString().Contains("Blender") == true)
{
_instal_callback_blender = "Blender OK";
_install_status_blender = true;
}
else
{
_install_blender();
}
}
else
{
_instal_callback_blender = "Blender not install first.";
}
if (Directory.Exists(Common.getRoadimporter_CO() + "\\RoadElements-master\\textures") == true)
{
_install_status_texture = true;
_instal_callback_texture = "Texture OK";
}
else if (Directory.Exists(Common.getRoadimporter_CO() + "\\RoadElements-master\\textures") == false)
{
_install_textpack();
}
}
private static void _install_dll()
{
FileInfo _dll = new FileInfo(Common.GetCRMPath() + "\\Bin\\Roadimporter.dll");
_dll.CopyTo(Common.getRoadimporter_Mods() + "\\Roadimporter.dll");
}
private static void _install_blender()
{
Process[] _blenders = Process.GetProcessesByName("Blender");
if (_blenders.Length > 0)
{
string _blender_path = _blenders[0].MainModule.FileName;
RegistryKey regLocalMachine = Registry.CurrentUser;
RegistryKey regEnvironment = regLocalMachine.OpenSubKey("Environment", true);
regEnvironment.SetValue("path", _blender_path);//写入path的值
if (regEnvironment.GetValue("path").ToString().Contains("Blender") == true)
{
_instal_callback_blender = "Blender OK";
_install_status_blender = true;
}
}
else
{
_instal_callback_blender = "Blender not open";
_install_status_blender = false;
}
}
private static void _install_textpack()
{
if(Directory.Exists(Common.GetCRMPath() + "\\RoadElements-master\\textures") == true)
{
if(Directory.Exists(Common.getRoadimporter_CO()) == true)
{
Generator_Program.CopyDir(Common.GetCRMPath() + "\\RoadElements-master\\textures", Common.getRoadimporter_CO()+"\\textures");
if(Directory.Exists(Common.getRoadimporter_CO() + "\\textures") == true)
{
_install_status_texture = true;
_instal_callback_texture = "Texture OK";
}
else
{
_install_status_texture = false;
_instal_callback_texture = "Texture install failed";
}
}
else
{
Directory.CreateDirectory(Common.getRoadimporter_CO());
Generator_Program.CopyDir(Common.GetCRMPath() + "\\RoadElements-master\\textures", Common.getRoadimporter_CO() + "\\textures");
if (Directory.Exists(Common.getRoadimporter_CO() + "\\textures") == true)
{
_install_status_texture = true;
_instal_callback_texture = "Texture OK";
}
else
{
_install_status_texture = false;
_instal_callback_texture = "Texture install failed";
}
}
}
else
{
_install_status_texture = false;
_instal_callback_texture = "Textures souce not found";
}
}
}
public static class GetModules
{
public static bool cnm;
public static string name = "";
public static string type = "";
public static void GetInstalledModules()
{
DirectoryInfo cnmsb = new DirectoryInfo(Common.getUsername() + "\\Colossal Order\\Cities_Skylines\\Addons\\Assets");
if(File.Exists(Directory.GetCurrentDirectory() + "\\Modulesins.txt") == false)
{
foreach (FileInfo cnmfiles in cnmsb.GetFiles())
{
if (cnmfiles.Name.Contains("CSUR") == true)
{
using (StreamWriter sw = File.AppendText(Directory.GetCurrentDirectory() + "\\Modulesins.txt"))
{
sw.WriteLine(cnmfiles.Name);
}
cnm = true;
}
}
}
else
{
File.Delete(Directory.GetCurrentDirectory() + "\\Modulesins.txt");
foreach (FileInfo cnmfiles in cnmsb.GetFiles())
{
if (cnmfiles.Name.Contains("CSUR") == true)
{
using (StreamWriter sw = File.AppendText(Directory.GetCurrentDirectory() + "\\Modulesins.txt"))
{
sw.WriteLine(cnmfiles.Name);
}
cnm = true;
}
}
}
}
}
public static class Selector
{
public static string addname;
public static string addname_type;
public static string addname_mode;
public static string savesname;
public static string allnames;
public static string alltypes;
}
}