-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModChecker.cs
More file actions
77 lines (65 loc) · 2.67 KB
/
ModChecker.cs
File metadata and controls
77 lines (65 loc) · 2.67 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
using System.Collections.Generic;
using System.Linq;
using BepInEx.Bootstrap;
namespace ModSync
{
internal static class ModChecker
{
/// <summary>
/// Returns a serialized list of all loaded plugins as "GUID|Version" entries.
/// ModSync itself is excluded so clients aren't flagged for version differences in the sync tool.
/// </summary>
internal static string[] GetLocalModList()
{
return Chainloader.PluginInfos
.Where(kvp => kvp.Key != MyPluginInfo.PLUGIN_GUID)
.Select(kvp => $"{kvp.Key}|{kvp.Value.Metadata.Version}")
.OrderBy(s => s)
.ToArray();
}
/// <summary>
/// Compares a remote mod list against the local one and returns a MismatchReport.
/// </summary>
internal static MismatchReport Compare(string[] localMods, string[] remoteMods)
{
var localSet = new HashSet<string>(localMods);
var remoteSet = new HashSet<string>(remoteMods);
var missingFromLocal = remoteSet.Except(localSet).ToArray(); // host has, we don't
var missingFromRemote = localSet.Except(remoteSet).ToArray(); // we have, host doesn't
return new MismatchReport(missingFromLocal, missingFromRemote);
}
}
internal class MismatchReport
{
internal string[] MissingFromLocal { get; } // present on host, absent locally
internal string[] MissingFromRemote { get; } // present locally, absent on host
internal bool HasMismatches => MissingFromLocal.Length > 0 || MissingFromRemote.Length > 0;
internal MismatchReport(string[] missingFromLocal, string[] missingFromRemote)
{
MissingFromLocal = missingFromLocal;
MissingFromRemote = missingFromRemote;
}
internal string FormatForDisplay()
{
var lines = new List<string>();
if (MissingFromLocal.Length > 0)
{
lines.Add("[ModSync] Mods you're missing:");
foreach (var entry in MissingFromLocal)
lines.Add($" - {FormatEntry(entry)}");
}
if (MissingFromRemote.Length > 0)
{
lines.Add("[ModSync] Mods the host doesn't have:");
foreach (var entry in MissingFromRemote)
lines.Add($" + {FormatEntry(entry)}");
}
return string.Join("\n", lines);
}
private static string FormatEntry(string entry)
{
var parts = entry.Split('|');
return parts.Length == 2 ? $"{parts[0]} (v{parts[1]})" : entry;
}
}
}