-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaunchCommandBuilder.cs
More file actions
271 lines (242 loc) · 9.52 KB
/
LaunchCommandBuilder.cs
File metadata and controls
271 lines (242 loc) · 9.52 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text;
namespace JustLauncher;
public static class LaunchCommandBuilder
{
public static List<string> BuildArguments(Installation installation, Account account, VersionInfo versionInfo, LauncherSettings settings, string? authlibInjectorPath = null)
{
var args = new List<string>();
if (account.AccountType == "ElyBy" && !string.IsNullOrEmpty(authlibInjectorPath))
{
args.Add($"-javaagent:{authlibInjectorPath}=ely.by");
}
string mcDir = PlatformManager.GetMinecraftDirectory();
string currentOs = PlatformManager.GetCurrentOsName();
var placeholders = new Dictionary<string, string>
{
{ "${auth_player_name}", account.Username },
{ "${version_name}", installation.Version },
{ "${game_directory}", installation.GameDirectory },
{ "${assets_root}", Path.Combine(mcDir, "assets") },
{ "${assets_index_name}", versionInfo.AssetIndex.Id },
{ "${auth_uuid}", account.Id.Replace("-", "") },
{ "${auth_access_token}", account.AccessToken ?? "0" },
{ "${user_type}", account.AccountType == "Microsoft" ? "msa" : "legacy" },
{ "${version_type}", versionInfo.Type },
{ "${user_properties}", "{}" },
{ "${launcher_name}", "JustLauncher" },
{ "${launcher_version}", AppVersion.Version },
{ "${nativedir}", Path.Combine(mcDir, "versions", installation.Version, "natives") },
{ "${clientid}", "0" },
{ "${auth_xuid}", account.Xuid ?? "0" },
{ "${resolution_width}", "854" },
{ "${resolution_height}", "480" },
{ "${quickPlayPath}", "" },
{ "${quickPlaySingleplayer}", "" },
{ "${quickPlayMultiplayer}", "" },
{ "${quickPlayRealms}", "" }
};
var classpath = new List<string>();
foreach (var lib in versionInfo.Libraries)
{
if (!lib.IsAllowed(currentOs)) continue;
string libPath = lib.GetPath();
classpath.Add(Path.Combine(mcDir, "libraries", libPath));
if (lib.Downloads?.Classifiers != null && lib.Natives != null && lib.Natives.TryGetValue(currentOs, out string? classifier))
{
if (lib.Downloads.Classifiers.TryGetValue(classifier, out var nativeArtifact))
{
classpath.Add(Path.Combine(mcDir, "libraries", nativeArtifact.Path));
}
}
}
string jarVersion = installation.Version;
if (!string.IsNullOrEmpty(installation.BaseVersion)) jarVersion = installation.BaseVersion;
else if (!string.IsNullOrEmpty(versionInfo.InheritsFrom)) jarVersion = versionInfo.InheritsFrom;
classpath.Add(Path.Combine(mcDir, "versions", jarVersion, jarVersion + ".jar"));
placeholders["${classpath}"] = string.Join(Path.PathSeparator, classpath);
// Determine memory allocation
double ramGb = installation.MemoryAllocationGb > 0
? installation.MemoryAllocationGb
: settings.MemoryAllocationGb;
if (ramGb <= 0) ramGb = 2.0; // Fail-safe default
string minRam = $"-Xms{(int)ramGb}G";
string maxRam = $"-Xmx{(int)ramGb}G";
// Add memory arguments first (or early enough)
args.Add(maxRam);
args.Add(minRam);
if (versionInfo.Arguments?.Jvm != null && versionInfo.Arguments.Jvm.Count > 0)
{
ProcessArguments(versionInfo.Arguments.Jvm, args, placeholders, currentOs);
}
else
{
args.Add("-Djava.library.path=" + placeholders["${nativedir}"]);
args.Add("-cp");
args.Add(placeholders["${classpath}"]);
}
if (!string.IsNullOrEmpty(installation.JavaArgs))
{
args.AddRange(SplitArguments(installation.JavaArgs));
}
args.Add(versionInfo.MainClass);
if (versionInfo.Arguments?.Game != null && versionInfo.Arguments.Game.Count > 0)
{
ProcessArguments(versionInfo.Arguments.Game, args, placeholders, currentOs);
}
else if (!string.IsNullOrEmpty(versionInfo.MinecraftArguments))
{
string gameArgs = versionInfo.MinecraftArguments;
foreach (var kv in placeholders)
{
gameArgs = gameArgs.Replace(kv.Key, kv.Value);
}
args.AddRange(SplitArguments(gameArgs));
}
else
{
args.Add("--username");
args.Add(account.Username);
args.Add("--version");
args.Add(installation.Version);
args.Add("--gameDir");
args.Add(placeholders["${game_directory}"]);
args.Add("--assetsDir");
args.Add(placeholders["${assets_root}"]);
args.Add("--assetIndex");
args.Add(versionInfo.AssetIndex.Id);
args.Add("--uuid");
args.Add(placeholders["${auth_uuid}"]);
args.Add("--accessToken");
args.Add(placeholders["${auth_access_token}"]);
args.Add("--userType");
args.Add(placeholders["${user_type}"]);
args.Add("--versionType");
args.Add(versionInfo.Type);
}
for (int i = 0; i < args.Count; i++)
{
args[i] = ReplaceAllPlaceholders(args[i], placeholders);
if (args[i].Contains("${"))
{
int start;
while ((start = args[i].IndexOf("${")) != -1)
{
int end = args[i].IndexOf("}", start);
if (end != -1)
{
args[i] = args[i].Remove(start, end - start + 1).Insert(start, "0");
}
else break;
}
}
}
return args;
}
private static IEnumerable<string> SplitArguments(string args)
{
var result = new List<string>();
var current = new StringBuilder();
bool inQuotes = false;
for (int i = 0; i < args.Length; i++)
{
char c = args[i];
if (c == '\"') inQuotes = !inQuotes;
else if (c == ' ' && !inQuotes)
{
if (current.Length > 0)
{
result.Add(current.ToString());
current.Clear();
}
}
else current.Append(c);
}
if (current.Length > 0) result.Add(current.ToString());
return result;
}
private static void ProcessArguments(List<object> source, List<string> target, Dictionary<string, string> placeholders, string currentOs)
{
foreach (var item in source)
{
if (item is string arg)
{
target.Add(arg);
}
else if (item is JsonElement element)
{
if (element.ValueKind == JsonValueKind.String)
{
target.Add(element.GetString() ?? "");
}
else if (element.ValueKind == JsonValueKind.Object)
{
if (element.TryGetProperty("rules", out var rulesNode))
{
var rules = JsonSerializer.Deserialize<List<Rule>>(rulesNode.GetRawText());
if (CheckRules(rules, currentOs))
{
if (element.TryGetProperty("value", out var valueNode))
{
if (valueNode.ValueKind == JsonValueKind.Array)
{
foreach (var val in valueNode.EnumerateArray())
{
target.Add(val.GetString() ?? "");
}
}
else
{
target.Add(valueNode.GetString() ?? "");
}
}
}
}
}
}
}
}
private static bool CheckRules(List<Rule>? rules, string currentOs)
{
if (rules == null || rules.Count == 0) return true;
bool allow = false;
foreach (var rule in rules)
{
bool matches = true;
if (rule.Os != null && rule.Os.Name != null && rule.Os.Name != currentOs) matches = false;
if (rule.Features != null)
{
foreach (var feature in rule.Features)
{
if (feature.Value) { matches = false; break; }
}
}
if (rule.Action == "allow")
{
if (matches) allow = true;
}
else if (rule.Action == "disallow")
{
if (matches) allow = false;
}
}
return allow;
}
private static string ReplaceAllPlaceholders(string text, Dictionary<string, string> placeholders)
{
foreach (var kv in placeholders)
{
text = text.Replace(kv.Key, kv.Value);
}
return text;
}
private static string EscapePath(string path)
{
if (path.Contains(" ")) return "\"" + path + "\"";
return path;
}
}