-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWizardConfig.cs
More file actions
1098 lines (968 loc) · 35.9 KB
/
WizardConfig.cs
File metadata and controls
1098 lines (968 loc) · 35.9 KB
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using Tjs;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Drawing;
namespace Wizard
{
// 分辨率设置对象
class Resolution
{
public int _w;
public int _h;
public Resolution(int w, int h) { _w = w; _h = h; }
public override string ToString()
{
const float delta = 0.001f;
string ratioStr = string.Empty;
float ratio = (float)_w / _h;
if (Math.Abs(ratio - 4.0 / 3.0) < delta)
{
ratioStr = "(4:3)";
}
else if (Math.Abs(ratio - 16.0 / 10.0) < delta)
{
ratioStr = "(16:10)";
}
else if (Math.Abs(ratio - 16.0 / 9.0) < delta)
{
ratioStr = "(16:9)";
}
else if (Math.Abs(ratio - 5.0 / 4.0) < delta)
{
ratioStr = "(5:4)";
}
return string.Format("{0}x{1} {2}", _w, _h, ratioStr);
}
public static Resolution[] List
{
get
{
return new Resolution[] {
//new Resolution(640, 480),
new Resolution(800, 600),
new Resolution(1024, 768),
new Resolution(1152, 864),
new Resolution(1280, 720),
new Resolution(1280, 800),
//new Resolution(1280, 960),
//new Resolution(1280, 1024),
//new Resolution(1366, 768),
//new Resolution(1400, 1050),
//new Resolution(1440, 900),
//new Resolution(1680, 1050),
//new Resolution(1920, 1080),
};
}
}
}
// 模板的基本属性
class ProjectProperty
{
public string readme = string.Empty;
public string title
{
get
{
// 读取标题
string ret = null;
if (setting != null)
{
ret = setting.GetString("title");
}
return ret == null ? string.Empty : ret;
}
}
public int width
{
get
{
// 读取预设宽度
double ret = double.NaN;
if (setting != null)
{
TjsValue val = setting.GetValue("width");
if (val != null) ret = val.ToDouble();
}
return double.IsNaN(ret) ? 0 : (int)ret;
}
}
public int height
{
get
{
// 读取预设高度
double ret = double.NaN;
if (setting != null)
{
TjsValue val = setting.GetValue("height");
if (val != null) ret = val.ToDouble();
}
return double.IsNaN(ret) ? 0 : (int)ret;
}
}
public int thumbnailwidth
{
get
{
// 读取缩略图大小
double ret = double.NaN;
if (setting != null)
{
TjsValue val = setting.GetValue("savedata/thumbnailwidth");
if(val != null) ret = val.ToDouble();
}
return double.IsNaN(ret) ? 0 : (int)ret;
}
}
public TjsDict setting = null;
public void LoadSetting(string file)
{
setting = null;
if (File.Exists(file))
{
setting = TjsValue.Load(file) as TjsDict;
}
}
}
// 封装一些辅助的方法用于转换数据
class TjsHelper
{
public static double ScaleInteger(TjsDict dict, string name, double scale)
{
TjsValue val = dict.GetValue(name);
double num = (val != null) ? val.ToDouble() : double.NaN;
if (!double.IsNaN(num))
{
num = num * scale;
dict.SetNumber(name, Math.Floor(num));
return num;
}
string str = dict.GetString(name);
if(double.TryParse(str, out num))
{
num = num * scale;
dict.SetString(name, Math.Floor(num).ToString());
return num;
}
return double.NaN;
}
public static TjsArray ScalePosArray(TjsDict dict, string name, double scaleX, double scaleY)
{
TjsValue v = null;
if (dict.val.TryGetValue(name, out v))
{
// 检查是不是数组
TjsArray arr = v as TjsArray;
if (arr != null)
{
// 从中读取两个元素的坐标数组
List<TjsValue> arraynew = new List<TjsValue>();
foreach (TjsValue pos in arr.val)
{
Point p = Point.Empty;
if (TryGetPos(pos, out p))
{
// 按比例缩放
TjsArray posnew = CreatePos((int)(p.X * scaleX), (int)(p.Y * scaleY));
arraynew.Add(posnew);
}
else
{
Debug.Assert(false, "invalid struct in pos array");
}
}
dict.val[name] = new TjsArray(arraynew);
return arr;
}
}
return null;
}
public static TjsArray ScaleButton(TjsDict dict, string name, double scaleX, double scaleY)
{
TjsValue v = null;
if (dict.val.TryGetValue(name, out v))
{
// 按钮上多记录了一个是否显示: x, y, shown
TjsArray xys = v as TjsArray;
if (xys != null && xys.val.Count == 3)
{
TjsNumber x = xys.val[0] as TjsNumber;
TjsNumber y = xys.val[1] as TjsNumber;
TjsNumber s = xys.val[2] as TjsNumber;
if (x != null && y != null && s != null)
{
TjsArray xysnew = CreatePos((int)(x.val * scaleX), (int)(y.val * scaleY));
xysnew.val.Add(new TjsNumber(s.val));
dict.val[name] = xysnew;
return xysnew;
}
else
{
Debug.Assert(false, "invalid element in button struct");
}
}
else
{
Debug.Assert(false, "invalid button struct");
}
}
return null;
}
public static TjsArray CreatePos(int x, int y)
{
List<TjsValue> inner = new List<TjsValue>();
inner.Add(new TjsNumber(x));
inner.Add(new TjsNumber(y));
return new TjsArray(inner);
}
public static bool TryGetPos(TjsValue pos, out Point p)
{
TjsArray xy = pos as TjsArray;
if (xy != null && xy.val.Count == 2)
{
TjsNumber x = xy.val[0] as TjsNumber;
TjsNumber y = xy.val[1] as TjsNumber;
if (x != null && y != null)
{
p = new Point((int)x.val, (int)y.val);
return true;
}
else
{
Debug.Assert(false, "invalid element in pos struct");
}
}
else
{
Debug.Assert(false, "invalid pos struct");
}
p = Point.Empty;
return false;
}
}
// 项目向导配置对象
class WizardConfig
{
// 一些常量
public const string THEME_FOLDER = "\\skin";
public const string TEMPLATE_FOLDER = "\\project\\template";
public const string DATA_FOLDER = "\\data";
public const string PROJECT_FOLDER = "\\project";
public const string UI_LAYOUT = "macro\\ui*.tjs";
public const string UI_SETTING = "macro\\setting.tjs";
public const string UI_CONFIG = "Config.tjs";
public const string UI_README = "Readme.txt";
public const int DEFAULT_WIDTH = 1024;
public const int DEFAULT_HEIGHT = 768;
public const string NAME_DEFAULT_THEME = "默认主题";
public const string PROJECT_DEFAULT_THEME = "template";
// 忽略指定的图片文件
const string PIC_IGNORE1 = @"data\system";
const string PIC_IGNORE2 = @"system";
public static bool IgnorePicture(string relFile)
{
string file = relFile.ToLower();
return file.StartsWith(PIC_IGNORE1) || file.StartsWith(PIC_IGNORE2);
}
#region 数据成员
private string _baseFolder = string.Empty; // nvlmaker根目录
private string _themeName = string.Empty; // 主题目录名
public int _height; // 分辨率-高度
public int _width; // 分辨率-宽度
private string _projectName = string.Empty; // 项目名称
private string _projectFolder = string.Empty; // 项目目录,空则取名称作为目录
// 目前缩放就按默认做
private string _scaler = ResFile.SCALER_DEFAULT; // 缩放策略,目前只有这种:(
private string _quality = ResFile.QUALITY_DEFAULT; // 缩放质量,默认是高
// 是否处在项目编辑模式
private bool _modifyproject = false;
// 储存上次读取的属性,避免多次读取。各种Bug都是这货搞的。
private ProjectProperty _info = null;
#endregion
// nvlmaker根路径
public string BaseFolder
{
get
{
// 软件根目录绝对路径,不包括结尾的 “\”
return _baseFolder;
}
set
{
// 处理下,保证不为空指针或空白字串
_baseFolder = (value == null ? string.Empty : value.Trim());
}
}
// 基础模板路径
public string BaseTemplateFolder
{
get
{
return this.BaseFolder + TEMPLATE_FOLDER;
}
}
// 是否选择了默认主题
public bool IsDefaultTheme
{
get
{
return string.IsNullOrEmpty(this.ThemeName);
}
}
// 是否处在编辑项目模式
public bool IsModifyProject
{
get { return _modifyproject; }
set
{
if (_modifyproject != value)
{
this._modifyproject = value;
this._info = null;
this.ThemeName = string.Empty;
this.ProjectName = string.Empty;
}
}
}
// 主题名称
public string ThemeName
{
get
{
return _themeName;
}
set
{
// 处理下,保证不为空指针或空白字串
string themeName = (value == null ? string.Empty : value.Trim());
// 如果主题更换则清空预读的设置
if(themeName != _themeName)
{
this._themeName = themeName;
this._info = null;
}
}
}
// 主题路径
public string ThemeFolder
{
get
{
if (this.IsDefaultTheme)
{
return this.BaseTemplateFolder;
}
else
{
// 连接主题目录和根目录
return this.BaseFolder + THEME_FOLDER + "\\" + this.ThemeName;
}
}
}
// 主题配置文件
public string ThemeSetting
{
get
{
return Path.Combine(this.ThemeDataFolder, UI_SETTING);
}
}
// 主题的数据目录
public string ThemeDataFolder
{
get
{
if (this.IsDefaultTheme)
{
return this.ThemeFolder + DATA_FOLDER;
}
else
{
return this.ThemeFolder;
}
}
}
// 所选主题的属性
public ProjectProperty ThemeInfo
{
get { return ReadInfo(this.ThemeDataFolder, this.ThemeSetting); }
}
// 目标项目路径
public string ProjectFolder
{
get
{
if (_projectFolder.Length == 0)
{
return this.BaseFolder + PROJECT_FOLDER + "\\" + _projectName;
}
else
{
return this.BaseFolder + PROJECT_FOLDER + "\\" + _projectFolder;
}
}
set
{
// 0长度字串表示没有单独设置项目目录
_projectFolder = (value == null ? string.Empty : value.Trim());
}
}
// 目标项目数据路径
public string ProjectDataFolder
{
get
{
return this.ProjectFolder + DATA_FOLDER;
}
}
// 目标项目名称
public string ProjectName
{
get
{
return _projectName;
}
set
{
// 处理下,保证不为空指针或空白字串
string projectName = (value == null ? string.Empty : value.Trim());
if(_projectName != projectName)
{
_projectName = projectName;
// 需要更新info信息
if(this.IsModifyProject) _info = null;
}
}
}
// 项目配置文件
public string ProjectSetting
{
get
{
return Path.Combine(this.ProjectDataFolder, UI_SETTING);
}
}
// 所选项目的属性
public ProjectProperty ProjectInfo
{
get { return ReadInfo(this.ProjectDataFolder, this.ProjectSetting); }
}
// 检查这个配置是否已经完备,把出错信息写入output
public bool IsReady(TextWriter output)
{
try
{
string path = this.BaseFolder;
if (string.IsNullOrEmpty(_baseFolder) || !Directory.Exists(path))
{
if (output != null) output.WriteLine("错误:软件根目录不存在。");
return false;
}
if (_height <= 0 || _width <= 0)
{
if (output != null) output.WriteLine("错误:无效的分辨率设置。");
return false;
}
path = this.ProjectFolder;
if (string.IsNullOrEmpty(_projectName))
{
if (output != null) output.WriteLine("错误:无效的项目名称。");
return false;
}
else if (!this.IsModifyProject && Directory.Exists(path))
{
if (output != null) output.WriteLine("错误:项目文件夹已存在,请更换项目名或设置其他路径。");
return false;
}
if (this.IsModifyProject)
{
ProjectProperty info = ProjectInfo;
if (info == null || (info.width == this._width && info.height == this._height))
{
if (output != null) output.WriteLine("错误:项目分辨率未改动,不需要进行转换");
return false;
}
}
else
{
path = this.ThemeFolder;
if (!string.IsNullOrEmpty(path))
{
if (!Directory.Exists(path))
{
if (output != null) output.WriteLine("错误:主题目录不存在。");
return false;
}
path = this.ThemeSetting;
if (string.IsNullOrEmpty(path) || !File.Exists(path))
{
if (output != null) output.WriteLine("警告:主题缺少配置文件");
}
}
ProjectProperty info = ThemeInfo;
if (info == null || info.height <= 0 || info.width <= 0)
{
if (output != null) output.WriteLine("警告:主题分辨率错误。");
}
ProjectProperty baseInfo = ReadBaseTemplateInfo();
if (baseInfo != info && (baseInfo == null || baseInfo.height <= 0 || baseInfo.width <= 0))
{
if (output != null) output.WriteLine("警告:默认主题分辨率错误。");
}
}
// 生成配置报告
if(output != null)
{
output.WriteLine(this.ToString());
}
}
catch (System.Exception e)
{
if (output != null) output.WriteLine("无效的项目配置:" + e.Message);
return false;
}
return true;
}
// 根据配置的内容生成报告
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (this.IsModifyProject)
{
sb.AppendFormat("【修改项目配置清单】"); sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
ProjectProperty info = this.ProjectInfo;
sb.AppendFormat("原始分辨率:{0}x{1}", info.width, info.height);sb.Append(Environment.NewLine);
sb.AppendFormat("目标分辨率:{0}x{1}", this._width, this._height);
if (info.width != _width || info.height != this._height)
{
sb.AppendFormat(" (分辨率已修改)");
}
sb.Append(Environment.NewLine);
}
else
{
sb.AppendFormat("【创建项目配置清单】"); sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
string theme = (this.IsDefaultTheme) ? NAME_DEFAULT_THEME : this.ThemeName;
sb.AppendFormat("所选主题:{0}", theme); sb.Append(Environment.NewLine);
sb.AppendFormat("分辨率设定:{0}x{1}", this._width, this._height);
ProjectProperty info = this.ThemeInfo;
if (info.width != _width || info.height != this._height)
{
sb.AppendFormat(" (非原始分辨率)");
}
sb.Append(Environment.NewLine);
}
sb.Append(Environment.NewLine);
sb.AppendFormat("项目名称:{0}", this._projectName);sb.Append(Environment.NewLine);
sb.AppendFormat("项目位置:{0}", this.ProjectFolder); sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.AppendFormat("缩放策略:{0}", this._scaler); sb.Append(Environment.NewLine);
sb.AppendFormat("缩放质量:{0}", this._quality); sb.Append(Environment.NewLine);
sb.AppendFormat("NVLMaker目录:{0}", this.BaseFolder);sb.Append(Environment.NewLine);
return sb.ToString();
}
// 从Data目录下读取属性
private ProjectProperty ReadInfo(string dataFolder, string setting)
{
// 直接返回上一次读取的值
if (this._info != null)
{
return this._info;
}
ProjectProperty info = new ProjectProperty();
this._info = info;
// 读取readme文件作为显示内容
try
{
string readmefile = Path.Combine(dataFolder, UI_README);
if (File.Exists(readmefile))
{
using (StreamReader r = new StreamReader(readmefile))
{
info.readme = r.ReadToEnd();
}
}
else
{
string configfile = Path.Combine(dataFolder, UI_CONFIG);
if (File.Exists(configfile))
{
using (StreamReader r = new StreamReader(configfile))
{
info.readme = r.ReadToEnd();
}
}
}
}
catch (System.Exception e)
{
// 出错的不保留
this._info = null;
info.readme = e.Message;
}
// 读取设置文件
try
{
info.LoadSetting(setting);
}
catch (System.Exception e)
{
// 出错的不保留
this._info = null;
info.readme = e.Message;
}
return info;
}
// 读取基础模板的配置
public ProjectProperty ReadBaseTemplateInfo()
{
// 如果选的是默认的主题,则返回主题属性
if(this.IsDefaultTheme)
{
return this.ThemeInfo;
}
// 这里就不读readme了,也不做保存,每次调用都从文件读一次
string file = Path.Combine(this.BaseTemplateFolder + DATA_FOLDER, UI_SETTING);
ProjectProperty info = new ProjectProperty();
try
{
info.LoadSetting(file);
}
catch (System.Exception e)
{
info.readme = e.Message;
}
return info;
}
}
// 根据项目配置转换项目文件
class WizardConverter : ResConverter
{
#region 事件:Logging及Report消息通知
public class MessageEventArgs : EventArgs
{
public readonly string msg;
public MessageEventArgs(string msg)
{
this.msg = msg;
}
}
// 消息通知响应函数
public delegate void MessageHandler(WizardConverter sender, MessageEventArgs e);
// Logging事件
public event MessageHandler LoggingEvent;
protected void OnLogging(string msg)
{
if (LoggingEvent != null)
{
LoggingEvent(this, new MessageEventArgs(msg));
}
}
// 错误消息通知事件
public event MessageHandler ErrorEvent;
protected void OnError(string msg)
{
if (ErrorEvent != null)
{
ErrorEvent(this, new MessageEventArgs(msg));
}
}
#endregion
WizardConfig _config;
public WizardConverter(WizardConfig config)
{
_config = config;
}
// 根据配置创建目标项目
public void Start()
{
if (_config.IsModifyProject)
{
// 从配置中读取目标大小
int dw = _config._width, dh = _config._height;
// 从项目自身的文件夹转换
string project = _config.ProjectFolder;
// 读取项目原始配置
ProjectProperty projInfo = _config.ProjectInfo;
int sw = projInfo.width, sh = projInfo.height;
ConvertFiles(project, sw, sh, project, dw, dh);
// 修正所有坐标
AdjustSettings(sw, sh);
}
else
{
// 从配置中读取目标大小
int dw = _config._width, dh = _config._height;
// 先从基础模板目录拷贝文件到项目目录
string template = _config.BaseTemplateFolder;
string project = _config.ProjectFolder;
// 读取基础模板的配置
ProjectProperty baseInfo = _config.ReadBaseTemplateInfo();
int sw = baseInfo.width;
if (sw <= 0) sw = WizardConfig.DEFAULT_WIDTH;
int sh = baseInfo.height;
if (sh <= 0) sh = WizardConfig.DEFAULT_HEIGHT;
ConvertFiles(template, sw, sh, project, dw, dh);
// 修正所有坐标,写入项目名称
AdjustSettings(sw, sh);
// 如果选择了非默认主题,再从主题目录拷贝文件到项目资料文件夹
if (_config.ThemeFolder != template)
{
// 读取所选主题配置
ProjectProperty themeInfo = _config.ThemeInfo;
sw = themeInfo.width;
if (sw <= 0) sw = WizardConfig.DEFAULT_WIDTH;
sh = themeInfo.height;
if (sh <= 0) sh = WizardConfig.DEFAULT_HEIGHT;
// 主题的文件直接拷入数据目录
ConvertFiles(_config.ThemeFolder, sw, sh, _config.ProjectDataFolder, dw, dh);
// 修正所有坐标,写入项目名称
AdjustSettings(sw, sh);
}
}
}
// 拷贝并缩放文件
void ConvertFiles(string srcPath, int sw, int sh, string destPath, int dw, int dh)
{
// 源文件列表
List<string> srcFiles = new List<string>();
try
{
bool ignoreCopy = (srcPath.ToLower() == destPath.ToLower());
// 建立目录并获取文件列表
CreateDir(srcPath, destPath, srcFiles);
// 建立图片转换配置,用于记录需要转换的图片文件,其他文件则直接拷贝
ResConfig resource = new ResConfig();
resource.path = srcPath;
resource.name = WizardConfig.NAME_DEFAULT_THEME;
// 遍历所有文件
int cutLen = srcPath.Length;
foreach (string srcfile in srcFiles)
{
// 截掉模板目录以径获取相对路径
string relFile = srcfile.Substring(cutLen + 1);
// 取得扩展名
string ext = Path.GetExtension(relFile).ToLower();
if ( // 宽高如果和源文件相同那就不用转换了
(sw != dw || sh != dh) &&
// 忽略某些图片
!WizardConfig.IgnorePicture(relFile) &&
// 只转换这些扩展名对应的文件
(ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".bmp"))
{
// 是图片则添加到转换器中
resource.files.Add(new ResFile(relFile));
}
else if (!ignoreCopy)
{
// 直接拷贝
OnLogging(string.Format("拷贝{0}", relFile));
File.Copy(srcfile, Path.Combine(destPath, relFile), true);
}
}
OnLogging("图片转换中……");
if (resource.files.Count > 0)
{
// 调用资源转换器方法,并开始转换
Start(resource, destPath, sw, sh, dw, dh);
}
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
// 修正目标项目文件夹中的配置
void AdjustSettings(int sw, int sh)
{
string dataPath = _config.ProjectDataFolder;
int dh = _config._height;
int dw = _config._width;
string title = _config.ProjectName;
try
{
ModifySetting(dataPath, title, sw, sh, dh, dw);
}
catch (System.Exception e)
{
OnError("修改setting.tjs失败:" + e.Message);
}
try
{
ModifyConfig(dataPath, title, sw, sh, dh, dw);
}
catch (System.Exception e)
{
OnError("修改Config.tjs失败:" + e.Message);
}
// 检查是否需要转换
if (sw != dw || sh != dh)
{
try
{
ModifyLayout(dataPath, sw, sh, dh, dw);
}
catch (System.Exception e)
{
OnError("修改界面布局文件失败:" + e.Message);
}
}
}
// 修改字典文件
static void ModifyDict(TjsDict dict, int sw, int sh, int dw, int dh)
{
double scaleX = (double)dw / sw;
double scaleY = (double)dh / sh;
TjsHelper.ScaleInteger(dict, "left", scaleX);
TjsHelper.ScaleInteger(dict, "x", scaleX);
TjsHelper.ScaleInteger(dict, "marginr", scaleX);
TjsHelper.ScaleInteger(dict, "marginl", scaleX);
TjsHelper.ScaleInteger(dict, "top", scaleY);
TjsHelper.ScaleInteger(dict, "y", scaleY);
TjsHelper.ScaleInteger(dict, "margint", scaleY);
TjsHelper.ScaleInteger(dict, "marginb", scaleY);
// 修改locate数组
TjsHelper.ScalePosArray(dict, "locate", scaleX, scaleY);
foreach (KeyValuePair<string, TjsValue> kv in dict.val)
{
TjsDict inner = kv.Value as TjsDict;
if(inner != null)
{
ModifyDict(inner, sw, sh, dw, dh);
}
}
}
// 修改UI布局文件
static void ModifyLayout(string dataPath, int sw, int sh, int dh, int dw)
{
// 更新layout
string[] layouts = Directory.GetFiles(dataPath, WizardConfig.UI_LAYOUT);
foreach (string layout in layouts)
{
TjsDict setting = TjsValue.Load(layout) as TjsDict;
if (setting != null)
{
ModifyDict(setting, sw, sh, dw, dh);
// 对这个文件里的按钮作特殊处理
if(layout.ToLower().EndsWith("uislpos.tjs"))
{
double scaleX = (double)dw / sw;
double scaleY = (double)dh / sh;
TjsHelper.ScaleButton(setting, "back", scaleX, scaleY);
TjsHelper.ScaleButton(setting, "up", scaleX, scaleY);
TjsHelper.ScaleButton(setting, "down", scaleX, scaleY);
}
}
setting.Save(layout, Encoding.Unicode);
}
}
// 修改config.tjs
static void ModifyConfig(string dataPath, string title, int sw, int sh, int dh, int dw)
{
// 更新config
string configFile = Path.Combine(dataPath, WizardConfig.UI_CONFIG);
if (File.Exists(configFile))
{
Regex regTitle = new Regex(@"\s*;\s*System.title\s*=");
Regex regW = new Regex(@"\s*;\s*scWidth\s*=");
Regex regH = new Regex(@"\s*;\s*scHeight\s*=");
Regex regThumb = new Regex(@"\s*;\s*thumbnailWidth\s*=");
StringBuilder buf = new StringBuilder();
using (StreamReader r = new StreamReader(configFile))
{
while (!r.EndOfStream)
{
string line = r.ReadLine();