-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModInfo.cs
More file actions
95 lines (81 loc) · 2.84 KB
/
Copy pathModInfo.cs
File metadata and controls
95 lines (81 loc) · 2.84 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
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DimensionsModManager;
/// <summary>
/// A mod on disk: a folder under mods/ containing mod.json and a files/
/// subfolder that mirrors the game folder layout.
/// </summary>
public class ModInfo
{
[JsonPropertyName("name")]
public string Name { get; set; } = "";
[JsonPropertyName("author")]
public string Author { get; set; } = "";
[JsonPropertyName("version")]
public string Version { get; set; } = "1.0";
/// <summary>"x360", "ps3" or "any".</summary>
[JsonPropertyName("platform")]
public string Platform { get; set; } = "any";
[JsonPropertyName("description")]
public string Description { get; set; } = "";
[JsonIgnore]
public string FolderPath { get; set; } = "";
[JsonIgnore]
public string FilesPath => Path.Combine(FolderPath, "files");
/// <summary>
/// datfiles\ARCHIVE\internal\path.ext - files injected into the game's
/// ARCHIVE.DAT/.HDR pair instead of being copied loose.
/// </summary>
[JsonIgnore]
public string DatFilesPath => Path.Combine(FolderPath, "datfiles");
[JsonIgnore]
public bool Enabled { get; set; }
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true,
WriteIndented = true,
};
public static ModInfo? Load(string modFolder)
{
string manifestPath = Path.Combine(modFolder, "mod.json");
ModInfo mod;
if (File.Exists(manifestPath))
{
try
{
mod = JsonSerializer.Deserialize<ModInfo>(
File.ReadAllText(manifestPath), JsonOptions)
?? new ModInfo();
}
catch (JsonException)
{
mod = new ModInfo { Description = "(invalid mod.json)" };
}
}
else
{
mod = new ModInfo();
}
if (string.IsNullOrWhiteSpace(mod.Name))
{
mod.Name = Path.GetFileName(modFolder);
}
mod.FolderPath = modFolder;
// A mod must actually ship files (loose and/or DAT-injected).
bool hasLoose = Directory.Exists(mod.FilesPath) &&
Directory.EnumerateFiles(mod.FilesPath, "*", SearchOption.AllDirectories).Any();
bool hasDat = Directory.Exists(mod.DatFilesPath) &&
Directory.EnumerateFiles(mod.DatFilesPath, "*", SearchOption.AllDirectories).Any();
if (!hasLoose && !hasDat)
{
return null;
}
return mod;
}
public bool MatchesPlatform(string platform)
{
return Platform.Equals("any", StringComparison.OrdinalIgnoreCase) ||
Platform.Equals(platform, StringComparison.OrdinalIgnoreCase);
}
public override string ToString() => $"{Name} v{Version}";
}