-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPatchDepsEditing.cs
More file actions
118 lines (100 loc) · 4.21 KB
/
PatchDepsEditing.cs
File metadata and controls
118 lines (100 loc) · 4.21 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using Mono.Cecil;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CorePatcher
{
internal static class PatchDepsEditing
{
private static string depsPath = Path.Combine(Environment.CurrentDirectory, "tModLoader.deps.json");
private static string depsPatchedPath = Path.Combine(Environment.CurrentDirectory, "tModLoader.patched.deps.json");
private static string content;
private static List<AssemblyDefinition> depsToInject = new List<AssemblyDefinition>();
internal static void AddDependency(AssemblyDefinition info)
{
if (!depsToInject.Contains(info))
{
depsToInject.Add(info);
}
else
{
throw new Exception("Assembly already in queue to be injected!");
}
}
static PatchDepsEditing()
{
if (!File.Exists(depsPath))
{
throw new FileNotFoundException("Failed to find tModLoader.deps.json! Verify your game file integrity to (re)acquire this file.");
}
content = File.ReadAllText(depsPath);
}
internal static bool PatchTargetRuntime()
{
if (!File.Exists(depsPath))
{
throw new FileNotFoundException("Failed to find tModLoader.deps.json! Verify your game file integrity to (re)acquire this file.");
}
JObject jsonObject = JObject.Parse(content);
JArray array = new JArray();
try
{
// jsonObject["targets"][".NETCoreApp,Version=v6.0"]["tModLoader/1.4.4.9"]["runtime"].AddAfterSelf(new JProperty("tModLoader.patched.dll", ""));
var target = jsonObject["targets"];
var netcoreapp = target[".NETCoreApp,Version=v8.0"];
var tmodloader1449 = netcoreapp["tModLoader/1.4.4.9"];
var runtime = (JObject)tmodloader1449["runtime"];
var libraries = jsonObject["libraries"];
runtime.Property("tModLoader.dll").Remove();
runtime["tModLoader.patched.dll"] = new JValue("");
foreach (var assemblyDefinition in depsToInject)
{
InjectIntoDependencies(tmodloader1449, assemblyDefinition);
InjectIntoNetCoreApp(netcoreapp, assemblyDefinition);
InjectIntoLibraries(libraries, assemblyDefinition);
}
string modifiedJsonObject = jsonObject.ToString();
File.WriteAllText(depsPatchedPath, modifiedJsonObject);
}
catch (Exception e)
{
ILog log = LogManager.GetLogger("DepsPatching");
log.Error(e);
return false;
}
return true;
}
private static void InjectIntoDependencies(JToken token, AssemblyDefinition asmInfo)
{
Version version = asmInfo.Name.Version;
string name = asmInfo.Name.Name;
JObject depsArray = (JObject)token["dependencies"];
depsArray[name] = new JValue(version.ToString());
}
private static void InjectIntoNetCoreApp(JToken token, AssemblyDefinition asmInfo)
{
Version version = asmInfo.Name.Version;
string name = asmInfo.Name.Name;
string nameVersion = name + "/" + version;
token[nameVersion] = new JObject();
token[nameVersion]["runtime"] = new JObject();
token[nameVersion]["runtime"][name + ".dll"] = new JObject();
}
private static void InjectIntoLibraries(JToken token, AssemblyDefinition asmInfo)
{
Version version = asmInfo.Name.Version;
string name = asmInfo.Name.Name;
string nameVersion = name + "/" + version;
var tokenInfo = token[nameVersion] = new JObject();
tokenInfo["type"] = new JValue("project");
tokenInfo["serviceable"] = new JValue(false);
tokenInfo["sha512"] = new JValue("");
}
}
}