-
Notifications
You must be signed in to change notification settings - Fork 25
/
ModsValidation.cs
174 lines (158 loc) · 6.73 KB
/
ModsValidation.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MelonLoader;
using System.Security.Cryptography;
using MelonLoader.TinyJSON;
using MelonLoader.Lemons.Cryptography;
using Il2Cpp;
namespace SkyCoop
{
public class ModsValidation
{
public static ModValidationData LastRequested = null;
public static List<string> ServerSideOnlyFiles = new List<string>();
public static List<string> WhitelistFiles = new List<string>();
public static string SHA256CheckSum(string filePath)
{
if (!File.Exists(filePath))
return "null";
byte[] byteHash;
using (SHA256 SHA256 = SHA256Managed.Create())
{
using (FileStream fileStream = File.OpenRead(filePath))
{
byteHash = SHA256.ComputeHash(fileStream);
}
}
string finalHash = string.Empty;
foreach (byte b in byteHash)
finalHash += b.ToString("x2");
return finalHash;
}
public class ModHashPair
{
public string m_Name = "";
public string m_Hash = "";
public ModHashPair(string n, string h)
{
m_Name = n;
m_Hash = h;
}
}
public class ModValidationData
{
public long m_Hash = 0;
public List<ModHashPair> m_Files = new List<ModHashPair>();
public string m_FullString = "";
public string m_FullStringBase64 = "";
public List<string> m_WhiteList = new List<string>();
}
public static bool ServerSideOnly(string Name)
{
return ServerSideOnlyFiles.Contains(Name) || WhitelistFiles.Contains(Name);
}
public static ModValidationData GetModsHash(bool Force = false, List<string> WhitelistedHashes = default)
{
ModValidationData Valid = new ModValidationData();
if(WhitelistedHashes == null)
{
WhitelistedHashes = new List<string>();
}
if (!Force && LastRequested != null)
{
return LastRequested;
}
if (MyMod.DedicatedServerAppMode)
{
if (File.Exists(@"Mods\serversideonly.json"))
{
MelonLogger.Msg(ConsoleColor.Yellow, "[ModsValidation][Info] Found Server Side Files List!");
string FilterJson = System.IO.File.ReadAllText("Mods\\serversideonly.json");
ServerSideOnlyFiles = JSON.Load(FilterJson).Make<List<string>>();
}
if (File.Exists("modswhitelist.json"))
{
MelonLogger.Msg(ConsoleColor.Yellow, "[ModsValidation][Info] Found Mods White List!");
string FilterJson = System.IO.File.ReadAllText("modswhitelist.json");
WhitelistFiles = JSON.Load(FilterJson).Make<List<string>>();
}
}
foreach (MelonMod Mod in MelonHandler.Mods)
{
string Hash = MelonHandler.GetMelonHash(Mod);
string FileName = Mod.Assembly.GetName().Name + ".dll";
if (!ServerSideOnly(FileName) || WhitelistedHashes.Contains(Hash))
{
Valid.m_Files.Add(new ModHashPair(@"Mods\" + FileName, Hash));
}else{
MelonLogger.Msg(ConsoleColor.Yellow, "[ModsValidation][Info] Ignore " + FileName);
}
if (WhitelistFiles.Contains(FileName))
{
Valid.m_WhiteList.Add(Hash);
}
}
DirectoryInfo d = new DirectoryInfo("Mods");
FileInfo[] Files = d.GetFiles("*.modcomponent");
foreach (FileInfo file in Files)
{
string Hash = SHA256CheckSum("Mods\\" + file.Name);
string FileName = file.Name;
if (!ServerSideOnly(FileName) && !WhitelistedHashes.Contains(Hash))
{
Valid.m_Files.Add(new ModHashPair(@"Mods\" + FileName, Hash));
}else{
MelonLogger.Msg(ConsoleColor.Yellow, "[ModsValidation][Info] Ignore " + FileName);
}
if (WhitelistFiles.Contains(FileName))
{
Valid.m_WhiteList.Add(Hash);
}
}
foreach (MelonPlugin Plugin in MelonHandler.Plugins)
{
string Hash = MelonHandler.GetMelonHash(Plugin);
string FileName = Plugin.Assembly.GetName().Name + ".dll";
if (!ServerSideOnly(FileName) && !WhitelistedHashes.Contains(Hash))
{
Valid.m_Files.Add(new ModHashPair(@"Plugins\" + FileName, Hash));
}else{
MelonLogger.Msg(ConsoleColor.Yellow, "[ModsValidation][Info] Ignore " + FileName);
}
if (WhitelistFiles.Contains(FileName))
{
Valid.m_WhiteList.Add(Hash);
}
}
string MainHash = "";
string FullString = "";
Valid.m_Files.Sort(delegate (ModHashPair x, ModHashPair y) {
return x.m_Name.CompareTo(y.m_Name);
});
foreach (ModHashPair Mod in Valid.m_Files)
{
if (string.IsNullOrEmpty(MainHash))
{
MainHash = Mod.m_Hash;
FullString = Mod.m_Name;
}else{
MainHash = MainHash + Mod.m_Hash;
FullString = FullString + "\n" + Mod.m_Name;
}
MelonLogger.Msg(ConsoleColor.Green,"[ModsValidation][Info] " +Mod.m_Name+" Hash: "+Mod.m_Hash);
}
Valid.m_Hash = Shared.GetDeterministicId(MainHash);
Valid.m_FullString = FullString;
Valid.m_FullStringBase64 = Shared.CompressString(FullString);
MelonLogger.Msg(ConsoleColor.Blue,"[ModsValidation][Info] Main Hash: " + Valid.m_Hash);
MelonLogger.Msg(ConsoleColor.Magenta, "[ModsValidation][Info] Stock: " + Encoding.UTF8.GetBytes(Valid.m_FullString).Length);
MelonLogger.Msg(ConsoleColor.Magenta, "[ModsValidation][Info] Compressed: " + Shared.CompressString(Valid.m_FullStringBase64).Length);
LastRequested = Valid;
return Valid;
}
}
}