-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
241 lines (203 loc) · 11 KB
/
Program.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using Microsoft.Win32;
namespace PolyConverter
{
public static class Program
{
public static int Main(string[] args)
{
bool hasArgs = args != null && args.Length > 0;
string gamePath = null;
if (!hasArgs) Console.WriteLine("[#] Booting up PolyConverter");
if (File.Exists(ManualGamePath))
{
try
{
gamePath = $"{File.ReadAllText(ManualGamePath).Trim()}";
}
catch (Exception e) // Could happen if it can't read files, I suppose
{
Console.WriteLine($"[Fatal Error] Failed to grab Poly Bridge 2 location from {ManualGamePath}: {e.Message}");
if (!hasArgs)
{
Console.WriteLine("\n[#] The program will now close.");
Console.ReadLine();
}
return ExitCodeGamePathError;
}
Console.WriteLine($"[#] Grabbed Poly Bridge 2 install location from {ManualGamePath}");
}
else
{
var errors = new List<Exception>(2);
try { gamePath = GetPolyBridge2SteamPath(); }
catch (Exception e) { errors.Add(e); }
if (gamePath == null)
{
try { gamePath = GetPolyBridge2EpicGamesPath(); }
catch (Exception e) { errors.Add(e); }
}
if (gamePath == null)
{
Console.WriteLine($"[Error] Failed to locate Poly Bridge 2 installation folder.");
Console.WriteLine($" You can manually set the location by creating a file here called \"{ManualGamePath}\" " +
"and writing the location of your game folder in that file, then restarting this program.");
foreach (var e in errors) Console.WriteLine($"\n[#] Error message: {e.Message}");
if (!hasArgs)
{
Console.WriteLine("\n[#] The program will now close.");
Console.ReadLine();
}
return ExitCodeGamePathError;
}
if (!hasArgs) Console.WriteLine($"[#] Automatically detected Poly Bridge 2 installation");
}
try
{
PolyBridge2Assembly = Assembly.LoadFrom($"{gamePath}\\Poly Bridge 2_Data\\Managed\\Assembly-CSharp.dll");
UnityAssembly = Assembly.LoadFrom($"{gamePath}\\Poly Bridge 2_Data\\Managed\\UnityEngine.CoreModule.dll");
SirenixAssembly = Assembly.LoadFrom($"{gamePath}\\Poly Bridge 2_Data\\Managed\\Sirenix.Serialization.dll");
SirenixConfigAssembly = Assembly.LoadFrom($"{gamePath}\\Poly Bridge 2_Data\\Managed\\Sirenix.Serialization.Config.dll");
object testObject = FormatterServices.GetUninitializedObject(VehicleProxy);
VehicleProxy.GetField("m_Pos").SetValue(testObject, Activator.CreateInstance(Vector2));
testObject = FormatterServices.GetUninitializedObject(DeserializationContext);
testObject = FormatterServices.GetUninitializedObject(DataFormat);
}
catch (Exception e)
{
Console.WriteLine($"[Fatal Error] Failed to load Poly Bridge 2 libraries at \"{gamePath}\":\n {e}");
if (!hasArgs)
{
Console.WriteLine("\n[#] The program will now close.");
Console.ReadLine();
}
return ExitCodeGamePathError;
}
Console.WriteLine();
if (hasArgs)
{
string filePath = string.Join(' ', args).Trim();
if (PolyConverter.LayoutJsonRegex.IsMatch(filePath))
{
string newPath = PolyConverter.LayoutJsonRegex.Replace(filePath, PolyConverter.LayoutExtension);
string backupPath = PolyConverter.LayoutJsonRegex.Replace(filePath, PolyConverter.LayoutBackupExtension);
string result = new PolyConverter().JsonToLayout(filePath, newPath, backupPath);
Console.WriteLine(result);
if (result.Contains("Invalid json")) return ExitCodeJsonError;
if (result.Contains("Error") && result.Contains("file")) return ExitCodeFileError;
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else if (PolyConverter.LayoutRegex.IsMatch(filePath))
{
string newPath = PolyConverter.LayoutRegex.Replace(filePath, PolyConverter.LayoutJsonExtension);
string result = new PolyConverter().LayoutToJson(filePath, newPath);
Console.WriteLine(result);
if (result.Contains("Error") && result.Contains("file")) return ExitCodeFileError;
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else if (PolyConverter.SlotJsonRegex.IsMatch(filePath))
{
string newPath = PolyConverter.SlotJsonRegex.Replace(filePath, PolyConverter.SlotExtension);
string backupPath = PolyConverter.SlotJsonRegex.Replace(filePath, PolyConverter.SlotBackupExtension);
string result = new PolyConverter().JsonToSlot(filePath, newPath, backupPath);
Console.WriteLine(result);
if (result.Contains("Invalid json")) return ExitCodeJsonError;
if (result.Contains("Error") && result.Contains("file")) return ExitCodeFileError;
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else if (PolyConverter.SlotRegex.IsMatch(filePath))
{
string newPath = PolyConverter.SlotRegex.Replace(filePath, PolyConverter.SlotJsonExtension);
string result = new PolyConverter().SlotToJson(filePath, newPath);
Console.WriteLine(result);
if (result.Contains("Error") && result.Contains("file")) return ExitCodeFileError;
if (result.Contains("Error")) return ExitCodeConversionError;
return ExitCodeSuccessful;
}
else
{
Console.WriteLine($"[Error] The only supported file extensions are: " +
$"{PolyConverter.LayoutExtension}, {PolyConverter.LayoutJsonExtension}, " +
$"{PolyConverter.SlotExtension}, {PolyConverter.SlotJsonExtension}");
return ExitCodeFileError;
}
}
else
{
while (true)
{
Console.WriteLine("\n");
new PolyConverter().ConvertAll();
Console.WriteLine("\n[#] Press Enter to run the program again.");
Console.ReadLine();
}
}
}
static string GetPolyBridge2SteamPath()
{
var paths = new List<string>(10);
paths.Add((string)Registry.LocalMachine?.OpenSubKey("SOFTWARE\\WOW6432Node\\Valve\\Steam")?.GetValue("InstallPath"));
if (paths[0] == null || !Directory.Exists(paths[0])) return null;
string config = File.ReadAllText($"{paths[0]}\\config\\config.vdf");
foreach (Match match in Regex.Matches(config, "\"BaseInstallFolder_[0-9]\"\\s+\"([^\"]+)\""))
{
paths.Add(match.Groups[1].Value.Replace("\\\\", "\\"));
}
foreach (string path in paths)
{
string assembliesPath = $"{path}\\steamapps\\common\\Poly Bridge 2";
if (Directory.Exists(assembliesPath)) return assembliesPath;
}
return null;
}
static string GetPolyBridge2EpicGamesPath()
{
var launcher = (string)Registry.LocalMachine?.OpenSubKey("SOFTWARE\\WOW6432Node\\Epic Games\\EpicGamesLauncher")?.GetValue("AppDataPath");
if (launcher == null || !Directory.Exists(launcher)) return null;
foreach (var file in Directory.GetFiles(launcher).Where(f => f.EndsWith(".item")))
{
string content = File.ReadAllText(file);
if (content.Contains("\"DisplayName\": \"Poly Bridge 2\""))
{
var match = Regex.Match(content, "\"InstallLocation\": \"(.+)\"");
return match.Groups[1].Value.Replace("\\\\", "\\");
}
}
return null;
}
const int ExitCodeSuccessful = 0;
const int ExitCodeJsonError = 1;
const int ExitCodeConversionError = 2;
const int ExitCodeFileError = 3;
const int ExitCodeGamePathError = 4;
const string ManualGamePath = "gamepath.txt";
public static Assembly PolyBridge2Assembly { get; private set; }
public static Assembly UnityAssembly { get; private set; }
public static Assembly SirenixAssembly { get; private set; }
public static Assembly SirenixConfigAssembly { get; private set; }
public static Type SandboxLayoutData => PolyBridge2Assembly.GetType("SandboxLayoutData");
public static Type BridgeSaveData => PolyBridge2Assembly.GetType("BridgeSaveData");
public static Type BridgeSaveSlotData => PolyBridge2Assembly.GetType("BridgeSaveSlotData");
public static Type ByteSerializer => PolyBridge2Assembly.GetType("ByteSerializer");
public static Type VehicleProxy => PolyBridge2Assembly.GetType("VehicleProxy");
public static Type BudgetProxy => PolyBridge2Assembly.GetType("BudgetProxy");
public static Type SandboxSettingsProxy => PolyBridge2Assembly.GetType("SandboxSettingsProxy");
public static Type WorkshopProxy => PolyBridge2Assembly.GetType("WorkshopProxy");
public static Type Vector2 => UnityAssembly.GetType("UnityEngine.Vector2");
public static Type Vector3 => UnityAssembly.GetType("UnityEngine.Vector3");
public static Type Quaternion => UnityAssembly.GetType("UnityEngine.Quaternion");
public static Type Color => UnityAssembly.GetType("UnityEngine.Color");
public static Type SerializationUtility => SirenixAssembly.GetType("Sirenix.Serialization.SerializationUtility");
public static Type DeserializationContext => SirenixAssembly.GetType("Sirenix.Serialization.DeserializationContext");
public static Type DataFormat => SirenixConfigAssembly.GetType("Sirenix.Serialization.DataFormat");
}
}