forked from AnotherSymbiote/WitcherScriptMerger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModFile.cs
More file actions
126 lines (104 loc) · 3.85 KB
/
Copy pathModFile.cs
File metadata and controls
126 lines (104 loc) · 3.85 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using WitcherScriptMerger.Inventory;
namespace WitcherScriptMerger.FileIndex
{
public class ModFile
{
#region Members
[XmlElement]
public string RelativePath { get; set; }
[XmlElement("IncludedMod")]
public List<FileHash> Mods { get; private set; }
[XmlElement]
public string BundleName { get; set; }
[XmlIgnore]
public ModFileCategory Category
{
get
{
if (BundleName != null)
{
if (IsTextFile(RelativePath))
return Categories.BundleText;
else
return Categories.BundleNotMergeable;
}
else if (IsScript(RelativePath))
return Categories.Script;
else if (IsXml(RelativePath))
return Categories.Xml;
else
return Categories.FlatNotMergeable;
}
}
[XmlIgnore]
public bool IsBundleContent => (BundleName != null);
[XmlIgnore]
public bool HasConflict => (Mods.Count > 1);
#endregion
public ModFile(string relPath, string bundlePath = null)
{
RelativePath = relPath;
Mods = new List<FileHash>();
if (bundlePath != null)
BundleName = Path.GetFileName(bundlePath);
}
public ModFile()
{
Mods = new List<FileHash>();
}
public bool ContainsMod(string modName)
{
return Mods.Any(mod => mod.Name.EqualsIgnoreCase(modName));
}
public string GetVanillaFile()
{
if (Category == Categories.Script)
return Path.Combine(Paths.ScriptsDirectory, RelativePath);
else if (Category == Categories.Xml)
return Path.Combine(Paths.GameDirectory, RelativePath);
else
throw new Exception($"Can't get vanilla file for category '{Category.DisplayName}'.");
}
public string GetModFile(string modName)
{
if (Category == Categories.Script)
return Path.Combine(Paths.ModsDirectory, modName, Paths.ModScriptBase, RelativePath);
else if (Category == Categories.Xml)
return Path.Combine(Paths.ModsDirectory, modName, RelativePath);
else if (Category.IsBundled)
return Path.Combine(Paths.ModsDirectory, modName, Paths.BundleBase, BundleName);
else
throw new NotImplementedException();
}
public static string GetModNameFromPath(string modFilePath)
{
if (!modFilePath.StartsWithIgnoreCase(Paths.ModsDirectory)) // Merged bundle content has internal path, not derived from mod folder
return Paths.MergedBundleContent;
var nameStart = Paths.ModsDirectory.Length + 1;
var name = modFilePath.Substring(nameStart);
// Path.DirectorySeparatorChar, not a hardcoded '\\': modFilePath is built via
// Path.Combine (directly or through Paths.GetRelativePath's substring logic
// over an OS-walked path), which uses '/' on Linux - confirmed by direct
// crash repro under WSL2 (WitcherScriptMerger.Headless, the Linux-capable
// host, running a real merge): the old hardcoded '\\' made IndexOf return -1
// on every Linux path, throwing ArgumentOutOfRangeException from the
// Substring call below on literally every flat-file merge attempt. Flagged in
// code review, see CLAUDE.md.
return name.Substring(0, name.IndexOf(Path.DirectorySeparatorChar));
}
public static bool IsScript(string path) => path.EndsWithIgnoreCase(".ws");
public static bool IsXml(string path) => path.EndsWithIgnoreCase(".xml");
public static bool IsFlatFile(string path) => (IsScript(path) || IsXml(path));
public static bool IsBundle(string path) => path.EndsWithIgnoreCase(".bundle");
public static bool IsTextFile(string path) => (path.EndsWithIgnoreCase(".ws") || path.EndsWithIgnoreCase(".xml") || path.EndsWithIgnoreCase(".txt") || path.EndsWithIgnoreCase(".csv"));
public override string ToString()
{
return $"({Mods.Count} mod{Mods.Count.GetPluralS()}) {RelativePath}";
}
}
}