-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathModLocalComp.cs
More file actions
2605 lines (2304 loc) · 103 KB
/
ModLocalComp.cs
File metadata and controls
2605 lines (2304 loc) · 103 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.IO;
using System.IO.Compression;
using System.Text;
using System.Text.RegularExpressions;
using fNbt;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PCL.Core.App;
using PCL.Core.Utils;
using static PCL.ModComp;
using static PCL.ModLoader;
namespace PCL;
public static class ModLocalComp
{
private const int LocalModCacheVersion = 7;
public class LocalCompFile
{
/// <summary>
/// 是否可能为前置 Mod。
/// </summary>
public bool IsPresetMod()
{
return !Dependencies.Any() && Name is not null &&
(Name.ToLower().Contains("core") || Name.ToLower().Contains("lib"));
}
/// <summary>
/// 根据完整文件路径的文件扩展名判断是否为 Mod 文件。
/// </summary>
public static bool IsModFile(string Path)
{
if (Path is null || !Path.Contains("."))
return false;
Path = Path.ToLower();
if (Path.EndsWithF(".jar", true) || Path.EndsWithF(".zip", true) || Path.EndsWithF(".litemod", true) ||
Path.EndsWithF(".jar.disabled", true) || Path.EndsWithF(".zip.disabled", true) ||
Path.EndsWithF(".litemod.disabled", true) || Path.EndsWithF(".jar.old", true) ||
Path.EndsWithF(".zip.old", true) || Path.EndsWithF(".litemod.old", true))
return true;
return false;
}
/// <summary>
/// 检查是否为指定类型的组件文件。
/// </summary>
public static bool IsCompFile(string Path, CompType CompType)
{
if (Path is null || !Path.Contains("."))
return false;
Path = Path.ToLower();
switch (CompType)
{
case CompType.Mod:
{
return IsModFile(Path);
}
case CompType.ResourcePack:
case CompType.Shader:
{
return Path.EndsWithF(".zip", true);
}
case CompType.DataPack:
{
return Path.EndsWithF(".zip", true) || Path.EndsWithF(".zip.disabled", true);
}
case CompType.Schematic:
{
return Path.EndsWithF(".litematic", true) || Path.EndsWithF(".nbt", true) ||
Path.EndsWithF(".schematic", true) || Path.EndsWithF(".schem", true);
}
default:
{
return false;
}
}
}
/// <summary>
/// 获取图标路径。
/// </summary>
public string GetLogo()
{
if (Comp is not null && Comp.LogoUrl is not null)
return Comp.LogoUrl;
if (Logo is not null)
return Logo;
// 为文件夹设置特定图标
if (IsFolder) return "pack://application:,,,/images/Icons/Folder.png";
return ModBase.PathImage + "Icons/NoIcon.png";
}
#region Litematic 文件处理
/// <summary>
/// 读取 Litematic 文件的 NBT 数据。
/// </summary>
private void LoadLitematicNbtData()
{
try
{
ModBase.Log($"开始读取 Litematic NBT 数据:{Path}", ModBase.LogLevel.Debug);
using (var fs = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var scheNbt = new NbtFile();
scheNbt.LoadFromStream(fs, NbtCompression.AutoDetect);
// 读取版本信息
var versionTag = (NbtInt)scheNbt.RootTag.Get("Version");
if (versionTag is not null) _litematicVersion = versionTag.Value;
// 读取 Metadata 节点
var metadataTag = scheNbt.RootTag.Get<NbtCompound>("Metadata");
if (metadataTag is not null)
{
ModBase.Log("找到 Litematic Metadata 节点", ModBase.LogLevel.Debug);
// 读取名称
var nameTag = metadataTag.Get<NbtString>("Name");
if (nameTag is not null && !string.IsNullOrWhiteSpace(nameTag.Value) &&
nameTag.Value != "Unnamed") _litematicOriginalName = nameTag.Value;
// 读取描述信息
var descriptionTag = metadataTag.Get<NbtString>("Description");
if (descriptionTag is not null && !string.IsNullOrWhiteSpace(descriptionTag.Value))
_Description = descriptionTag.Value;
// 读取作者信息
var authorTag = metadataTag.Get<NbtString>("Author");
if (authorTag is not null && !string.IsNullOrWhiteSpace(authorTag.Value))
_Authors = authorTag.Value;
// 读取时间信息
var timeCreatedTag = metadataTag.Get<NbtLong>("TimeCreated");
if (timeCreatedTag is not null) _litematicTimeCreated = timeCreatedTag.Value;
var timeModifiedTag = metadataTag.Get<NbtLong>("TimeModified");
if (timeModifiedTag is not null) _litematicTimeModified = timeModifiedTag.Value;
// 读取包围盒大小
var enclosingSizeTag = metadataTag.Get<NbtCompound>("EnclosingSize");
if (enclosingSizeTag is not null)
{
var xTag = enclosingSizeTag.Get<NbtInt>("x");
var yTag = enclosingSizeTag.Get<NbtInt>("y");
var zTag = enclosingSizeTag.Get<NbtInt>("z");
if (xTag is not null && yTag is not null && zTag is not null)
_litematicEnclosingSize = $"{xTag.Value} × {yTag.Value} × {zTag.Value}";
}
// 读取区域数量
var regionCountTag = metadataTag.Get<NbtInt>("RegionCount");
if (regionCountTag is not null) _litematicRegionCount = regionCountTag.Value;
// 读取总方块数
var totalBlocksTag = metadataTag.Get<NbtInt>("TotalBlocks");
if (totalBlocksTag is not null) _litematicTotalBlocks = totalBlocksTag.Value;
// 读取总体积
var totalVolumeTag = metadataTag.Get<NbtInt>("TotalVolume");
if (totalVolumeTag is not null) _litematicTotalVolume = totalVolumeTag.Value;
}
else
{
ModBase.Log("未找到 Litematic Metadata 节点", ModBase.LogLevel.Debug);
}
}
ModBase.Log("Litematic NBT 数据读取完成", ModBase.LogLevel.Debug);
}
catch (Exception ex)
{
ModBase.Log(ex, "读取 Litematic NBT 数据时出错(" + Path + ")");
}
}
#endregion
#region Schem 文件处理
/// <summary>
/// 读取 .schem 文件的 NBT 数据(Sponge Schematic 格式)。
/// </summary>
private void LoadSchemNbtData()
{
try
{
ModBase.Log($"开始读取 Schem NBT 数据:{Path}", ModBase.LogLevel.Debug);
// 使用自动检测压缩格式
using (var fs = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var scheNbt = new NbtFile();
scheNbt.LoadFromStream(fs, NbtCompression.AutoDetect);
// 读取Sponge版本信息
var versionTag = scheNbt.RootTag.Get<NbtInt>("Version");
if (versionTag is not null) _spongeVersion = versionTag.Value;
// 读取数据版本信息
var dataVersionTag = scheNbt.RootTag.Get<NbtInt>("DataVersion");
if (dataVersionTag is not null) _structureDataVersion = dataVersionTag.Value;
// 读取尺寸信息
var widthTag = scheNbt.RootTag.Get<NbtShort>("Width");
var heightTag = scheNbt.RootTag.Get<NbtShort>("Height");
var lengthTag = scheNbt.RootTag.Get<NbtShort>("Length");
if (widthTag is not null && heightTag is not null && lengthTag is not null)
{
_litematicEnclosingSize = $"{widthTag.Value} × {heightTag.Value} × {lengthTag.Value}";
_litematicTotalVolume = (short)(widthTag.Value * heightTag.Value) * lengthTag.Value;
// 对于Sponge格式,方块数量等于总体积(因为包含空气方块)
_litematicTotalBlocks = _litematicTotalVolume;
}
// 读取调色板信息来计算区域数量
var paletteTag = scheNbt.RootTag.Get<NbtCompound>("Palette");
if (paletteTag is not null) _litematicRegionCount = 1; // Sponge Schematic 通常只有一个区域
// 读取元数据
var metadataTag = scheNbt.RootTag.Get<NbtCompound>("Metadata");
if (metadataTag is not null)
{
// 读取名称
var nameTag = metadataTag.Get<NbtString>("Name");
if (nameTag is not null && !string.IsNullOrWhiteSpace(nameTag.Value))
_schemOriginalName = nameTag.Value;
// 读取作者信息
var authorTag = metadataTag.Get<NbtString>("Author");
if (authorTag is not null && !string.IsNullOrWhiteSpace(authorTag.Value))
{
_structureAuthor = authorTag.Value;
if (_Authors is null)
_Authors = _structureAuthor;
}
}
}
ModBase.Log("Schem NBT 数据读取完成", ModBase.LogLevel.Debug);
}
catch (Exception ex)
{
ModBase.Log(ex, "读取 Schem NBT 数据时出错(" + Path + ")");
}
}
#endregion
#region Schematic 文件处理
/// <summary>
/// 读取 .schematic 文件的 NBT 数据(MCEdit/WorldEdit 格式)。
/// </summary>
private void LoadSchematicNbtData()
{
try
{
ModBase.Log($"开始读取 Schematic NBT 数据:{Path}", ModBase.LogLevel.Debug);
using (var fs = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var scheNbt = new NbtFile();
scheNbt.LoadFromStream(fs, NbtCompression.AutoDetect);
// 读取尺寸信息
var widthTag = scheNbt.RootTag.Get<NbtShort>("Width");
var heightTag = scheNbt.RootTag.Get<NbtShort>("Height");
var lengthTag = scheNbt.RootTag.Get<NbtShort>("Length");
if (widthTag is not null && heightTag is not null && lengthTag is not null)
{
_litematicEnclosingSize = $"{widthTag.Value} × {heightTag.Value} × {lengthTag.Value}";
_litematicTotalVolume = (short)(widthTag.Value * heightTag.Value) * lengthTag.Value;
}
// 读取材料列表
var materialsTag = scheNbt.RootTag.Get<NbtString>("Materials");
if (materialsTag is not null)
ModBase.Log($"Schematic 材料类型:{materialsTag.Value}", ModBase.LogLevel.Debug);
ModBase.Log("Schematic NBT 数据读取完成", ModBase.LogLevel.Debug);
}
}
catch (Exception ex)
{
ModBase.Log(ex, "读取 Schematic NBT 数据时出错(" + Path + ")");
}
}
#endregion
#region NBT 结构文件处理
/// <summary>
/// 读取 .nbt 文件的 NBT 数据(Minecraft 结构文件格式)。
/// </summary>
private void LoadStructureNbtData()
{
try
{
ModBase.Log($"开始读取 NBT 结构文件数据:{Path}", ModBase.LogLevel.Debug);
using (var fs = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var scheNbt = new NbtFile();
scheNbt.LoadFromStream(fs, NbtCompression.AutoDetect);
// 读取作者信息
var authorTag = scheNbt.RootTag.Get<NbtString>("author");
if (authorTag is not null && !string.IsNullOrWhiteSpace(authorTag.Value))
{
_structureAuthor = authorTag.Value;
if (_Authors is null)
_Authors = _structureAuthor;
}
// 读取尺寸信息
var sizeTag = scheNbt.RootTag.Get<NbtList>("size");
if (sizeTag is not null)
{
var sizeElements = sizeTag.ToArray();
if (sizeElements.Length >= 3)
{
var sizeArray = sizeElements.Take(3).Select(e => e.IntValue).ToArray();
_litematicEnclosingSize = $"{sizeArray[0]} × {sizeArray[1]} × {sizeArray[2]}";
_litematicTotalVolume = sizeArray[0] * sizeArray[1] * sizeArray[2];
}
}
// 读取方块数量信息
var blocksTag = scheNbt.RootTag.Get<NbtList>("blocks");
if (blocksTag is not null)
_litematicTotalBlocks = blocksTag.Where(x => x.TagType == NbtTagType.Compound).Count();
// 读取调色板信息来计算区域数量
var paletteTag = scheNbt.RootTag.Get<NbtList>("palette");
if (paletteTag is not null) _litematicRegionCount = 1; // 原版结构文件通常只有一个区域
}
}
catch (Exception ex)
{
ModBase.Log(ex, "读取 NBT 结构文件数据时出错(" + Path + ")");
}
}
#endregion
#region 基础
/// <summary>
/// 资源的文件的地址。
/// </summary>
public readonly string Path;
/// <summary>
/// 是否为文件夹项。
/// </summary>
public bool IsFolder => Path.EndsWithF(@"\__FOLDER__", true);
/// <summary>
/// 获取实际的文件夹路径(去除 __FOLDER__ 标记)。
/// </summary>
public string ActualPath
{
get
{
if (IsFolder) return Path.Replace(@"\__FOLDER__", "");
return Path;
}
}
public LocalCompFile(string Path)
{
this.Path = Path ?? "";
}
/// <summary>
/// NBT数据是否已加载(用于延迟加载优化)。
/// </summary>
private bool _nbtDataLoaded;
/// <summary>
/// Mod 资源的完整路径,去除最后的 .disabled 和 .old。
/// </summary>
public string RawPath => ModBase.GetPathFromFullPath(Path) + RawFileName;
/// <summary>
/// 资源的完整文件名。
/// </summary>
public string FileName
{
get
{
if (IsFolder && !string.IsNullOrEmpty(Name)) return Name;
return ModBase.GetFileNameFromPath(Path);
}
}
/// <summary>
/// Mod 资源的完整文件名,去除最后的 .disabled 和 .old。
/// </summary>
public string RawFileName => FileName.Replace(".disabled", "").Replace(".old", "");
/// <summary>
/// 资源的状态。对于 Mod 有 Disabled
/// </summary>
public LocalFileStatus State
{
get
{
Load();
if (!IsFileAvailable) return LocalFileStatus.Unavailable;
if (Path.EndsWithF(".disabled", true) || Path.EndsWithF(".old", true)) return LocalFileStatus.Disabled;
return LocalFileStatus.Fine;
}
}
public enum LocalFileStatus
{
Fine = 0,
Disabled = 1,
Unavailable = 2
}
#endregion
#region 信息项
/// <summary>
/// Mod 的名称。若不可用则为 ModID 或无扩展的文件名。
/// </summary>
public string Name
{
get
{
if (_Name is null)
Load();
if (_Name is null)
_Name = _ModId;
if (_Name is null)
{
if (IsFolder)
_Name = ModBase.GetFolderNameFromPath(ActualPath);
else
_Name = ModBase.GetFileNameWithoutExtentionFromPath(Path);
}
return _Name;
}
set
{
if (_Name is null && value is not null && !value.Contains("modname") && value.ToLower() != "name" &&
value.Count() > 1 && (ModBase.Val(value).ToString() ?? "") != (value ?? "")) _Name = value;
}
}
private string _Name;
/// <summary>
/// Mod 的描述信息。
/// </summary>
public string Description
{
get
{
if (_Description is null)
Load();
if (_Description is null && FileUnavailableReason is not null)
_Description = FileUnavailableReason.Message;
// If _Description Is Nothing Then _Description = Path
return _Description;
}
set
{
if (_Description is null && value is not null && value.Count() > 2)
{
_Description = value.Trim('\n');
// 优化显示:若以 [a-zA-Z0-9] 结尾,加上小数点句号
if (_Description.ToLower().LastIndexOfAny("qwertyuiopasdfghjklzxcvbnm0123456789".ToCharArray()) ==
_Description.Count() - 1)
_Description += ".";
}
}
}
private string _Description;
/// <summary>
/// 文件类型标签。
/// </summary>
public List<string> Tags
{
get
{
if (_tags is null)
{
_tags = new List<string>();
if (IsFolder)
{
_tags.Add("文件夹");
}
else
{
var extension = System.IO.Path.GetExtension(RawPath).ToLower();
switch (extension ?? "")
{
case ".litematic":
{
_tags.Add("原理图");
break;
}
case ".schem":
case ".schematic":
{
_tags.Add("Schematic结构");
break;
}
case ".nbt":
{
_tags.Add("原版结构");
break;
}
}
}
}
return _tags;
}
}
private List<string> _tags;
/// <summary>
/// Mod 的版本,不保证符合版本格式规范。
/// </summary>
public string Version
{
get
{
if (_Version is null)
Load();
return _Version;
}
set
{
if (_Version is not null && _Version.RegexCheck(@"[0-9.\-]+"))
return;
if (value?.ContainsF("version", true) == true)
value = "version"; // 需要修改的标识
_Version = value;
}
}
public string _Version;
/// <summary>
/// 用于依赖检查的 ModID。
/// </summary>
public string ModId
{
get
{
if (_ModId is null)
Load();
return _ModId;
}
set
{
if (value is null)
return;
value = value.RegexSeek(RegexPatterns.ModIdMatch);
if (value is null || value.Count() <= 1 || (ModBase.Val(value).ToString() ?? "") == (value ?? ""))
return;
if (value.ContainsF("name", true) || value.ContainsF("modid", true))
return;
if (!PossibleModId.Contains(value))
PossibleModId.Add(value);
if (_ModId is null)
_ModId = value;
}
}
private string _ModId;
/// <summary>
/// 其他可能的 ModID。
/// </summary>
public List<string> PossibleModId = new();
/// <summary>
/// Mod 的主页。
/// </summary>
public string Url
{
get
{
if (_Url is null)
Load();
return _Url;
}
set
{
if (_Url is null && value is not null && value.StartsWithF("http")) _Url = value;
}
}
private string _Url;
/// <summary>
/// Mod 的作者列表。
/// </summary>
public string Authors
{
get
{
if (_Authors is null)
Load();
return _Authors;
}
set
{
if (_Authors is null && !string.IsNullOrWhiteSpace(value)) _Authors = value;
}
}
private string _Authors;
/// <summary>
/// Litematic 文件的创建时间戳。
/// </summary>
public long? LitematicTimeCreated
{
get
{
LoadNbtDataIfNeeded();
return _litematicTimeCreated;
}
}
private long? _litematicTimeCreated;
/// <summary>
/// Litematic 文件的修改时间戳。
/// </summary>
public long? LitematicTimeModified
{
get
{
LoadNbtDataIfNeeded();
return _litematicTimeModified;
}
}
private long? _litematicTimeModified;
/// <summary>
/// Schem 读取到的原始名称。
/// </summary>
public string SchemOriginalName
{
get
{
LoadNbtDataIfNeeded();
return _schemOriginalName;
}
}
private string _schemOriginalName;
/// <summary>
/// Litematic 读取到的原始名称。
/// </summary>
public string LitematicOriginalName
{
get
{
LoadNbtDataIfNeeded();
return _litematicOriginalName;
}
}
private string _litematicOriginalName;
/// <summary>
/// Litematic 文件的版本。
/// </summary>
public int? LitematicVersion
{
get
{
LoadNbtDataIfNeeded();
return _litematicVersion;
}
}
private int? _litematicVersion;
/// <summary>
/// Litematic 文件的包围盒大小。
/// </summary>
public string LitematicEnclosingSize
{
get
{
LoadNbtDataIfNeeded();
return _litematicEnclosingSize;
}
}
private string _litematicEnclosingSize;
/// <summary>
/// Litematic 文件的区域数量。
/// </summary>
public int? LitematicRegionCount
{
get
{
LoadNbtDataIfNeeded();
return _litematicRegionCount;
}
}
private int? _litematicRegionCount;
/// <summary>
/// Litematic 文件的总方块数。
/// </summary>
public int? LitematicTotalBlocks
{
get
{
LoadNbtDataIfNeeded();
return _litematicTotalBlocks;
}
}
private int? _litematicTotalBlocks;
/// <summary>
/// Litematic 文件的总体积。
/// </summary>
public int? LitematicTotalVolume
{
get
{
LoadNbtDataIfNeeded();
return _litematicTotalVolume;
}
}
private int? _litematicTotalVolume;
/// <summary>
/// 原版结构文件的游戏版本。
/// </summary>
public string StructureGameVersion
{
get
{
LoadNbtDataIfNeeded();
return _structureGameVersion;
}
}
private string _structureGameVersion;
/// <summary>
/// 原版结构文件的数据版本。
/// </summary>
public int? StructureDataVersion
{
get
{
LoadNbtDataIfNeeded();
return _structureDataVersion;
}
}
private int? _structureDataVersion;
/// <summary>
/// 原版结构文件的作者。
/// </summary>
public string StructureAuthor
{
get
{
LoadNbtDataIfNeeded();
return _structureAuthor;
}
}
private string _structureAuthor;
/// <summary>
/// Sponge Schematic 文件的版本。
/// </summary>
public int? SpongeVersion
{
get
{
LoadNbtDataIfNeeded();
return _spongeVersion;
}
}
private int? _spongeVersion;
/// <summary>
/// Mod 图标路径。
/// </summary>
public string Logo { get; set; }
/// <summary>
/// 依赖项,其中包括了 Minecraft 的版本要求。格式为 ModID - VersionRequirement,若无版本要求则为 Nothing。
/// </summary>
public Dictionary<string, string> Dependencies
{
get
{
Load();
return _Dependencies;
}
}
private Dictionary<string, string> _Dependencies = new();
private void AddDependency(string ModID, string VersionRequirement = null)
{
// 确保信息正确
if (ModID is null || ModID.Count() < 2)
return;
ModID = ModID.ToLower();
if (ModID == "name" || (ModBase.Val(ModID).ToString() ?? "") == (ModID ?? ""))
return; // 跳过 name 与纯数字 id
if (VersionRequirement is null ||
(!VersionRequirement.Contains(".") && !VersionRequirement.Contains("-")) ||
VersionRequirement.Contains("$"))
VersionRequirement = null;
else if (!VersionRequirement.StartsWithF("[") && !VersionRequirement.StartsWithF("(") &&
!VersionRequirement.EndsWithF("]") && !VersionRequirement.EndsWithF(")"))
VersionRequirement = "[" + VersionRequirement + ",)";
// 向依赖项中添加
if (_Dependencies.ContainsKey(ModID))
{
if (_Dependencies[ModID] is null)
_Dependencies[ModID] = VersionRequirement;
}
else
{
_Dependencies.Add(ModID, VersionRequirement);
}
}
#endregion
#region 加载步骤标记
// 1. 进行文件可用性检查
// 成功:继续第二步。
// 失败:标记 FileUnavailableReason, 并停止后续加载。
/// <summary>
/// 是否已进行 Mod 文件的基础加载。(这包括第一步和第二步)
/// </summary>
private bool IsLoaded;
/// <summary>
/// Mod 文件是否可被正常读取。
/// </summary>
public bool IsFileAvailable
{
get
{
Load();
return FileUnavailableReason is null;
}
}
/// <summary>
/// Mod 文件出错的原因。若无错误,则为 Nothing。
/// </summary>
public Exception FileUnavailableReason
{
get
{
Load();
return _FileUnavailableReason;
}
}
private Exception _FileUnavailableReason;
// 2. 进行 .class 以外的信息获取
// 成功:标记 IsInfoWithoutClassAvailable。
// 失败:什么也不干。如果需要补充信息的话,检测到 IsInfoWithoutClassAvailable 为 False,会自动继续加载。
/// <summary>
/// 是否已在不获取 .class 文件的前提下完成了所需信息的加载。
/// </summary>
private bool IsInfoWithoutClassAvailable = false;
// 3. 尝试从 .class 文件中获取信息
// 成功:标记 IsInfoWithClassAvailable。
// 失败:什么也不干。
/// <summary>
/// 是否已进行 .class 文件的信息获取。
/// </summary>
private bool IsInfoWithClassLoaded;
/// <summary>
/// 是否已在 .class 文件中完成了所需信息的加载。
/// </summary>
private bool IsInfoWithClassAvailable;
#endregion
#region 加载
/// <summary>
/// 初始化所有数据。
/// </summary>
private void Init()
{
_Name = null;
_Description = null;
_Version = null;
_ModId = null;
PossibleModId = new List<string>();
_Dependencies = new Dictionary<string, string>();
IsLoaded = false;
_FileUnavailableReason = null;
IsInfoWithClassLoaded = false;
IsInfoWithClassAvailable = false;
}
/// <summary>
/// 加载基本信息(不解析NBT数据)。
/// </summary>
public void LoadBasicInfo()
{
try
{
// 可用性检查
if (IsFolder)
{
// 文件夹项不需要进一步处理
IsLoaded = true;
return;
}
if (!File.Exists(Path))
{
_FileUnavailableReason = new FileNotFoundException("未找到资源文件(" + Path + ")");
IsLoaded = true;
return;
}
// 对于原理图文件,只设置基本状态,不解析NBT数据
if (Path.EndsWithF(".litematic", true) || Path.EndsWithF(".nbt", true) ||
Path.EndsWithF(".schem", true) || Path.EndsWithF(".schematic", true))
{
_Name = ModBase.GetFileNameWithoutExtentionFromPath(Path);
IsLoaded = true;
return;
}
// 对于其他文件类型,正常加载
Load();
}
catch (Exception ex)
{
ModBase.Log(ex, $"加载基本信息失败:{Path}");
}
}
/// <summary>
/// 延迟加载NBT数据。
/// </summary>
public void LoadNbtDataIfNeeded()
{
try
{
// 如果已经加载过NBT数据,则跳过
if (_nbtDataLoaded)
return;
// 根据文件类型加载NBT数据
if (Path.EndsWithF(".litematic", true))
LoadLitematicNbtData();
else if (Path.EndsWithF(".nbt", true))
LoadStructureNbtData();
else if (Path.EndsWithF(".schem", true))
LoadSchemNbtData();
else if (Path.EndsWithF(".schematic", true)) LoadSchematicNbtData();