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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use crate::error::{CobbleError, CobbleResult};
use crate::minecraft::argument_replacements::ArgumentReplacements;
use crate::minecraft::launch_options::LaunchOptions;
use crate::minecraft::models::version_data::VersionData;
use crate::minecraft::process_handle::GameProcessHandle;
use crate::utils::os::Platform;
use ipc_channel::ipc;
use std::path::{Path, PathBuf};
use std::process::{exit, Command};
use std::thread::sleep;
use std::time::Duration;
pub fn launch(mut command: Command) -> CobbleResult<GameProcessHandle> {
debug!("Launching minecraft");
trace!("{:?}", command);
let (stop_sender, stop_receiver) = ipc::bytes_channel()?;
let (stopped_sender, stopped_receiver) = ipc::bytes_channel()?;
let handle = GameProcessHandle::new(stop_sender, stopped_receiver);
match fork::fork() {
Ok(fork::Fork::Parent(_)) => {
return Ok(handle);
}
Ok(fork::Fork::Child) => {
let mut child = match command.spawn() {
Ok(child) => child,
Err(err) => {
error!("{}", err);
exit(1);
}
};
let game_pid = nix::unistd::Pid::from_raw(child.id() as i32);
loop {
match child.try_wait() {
Ok(Some(status)) => {
if !status.success() {
debug!("Game exited with status '{:?}'", status.code());
} else {
debug!("Game exited successfully");
}
if let Err(err) = stopped_sender.send(&[]) {
error!("{}", err);
}
break;
}
Ok(None) => {
}
Err(err) => {
error!("Could not check if game still running: {}", err);
break;
}
}
match stop_receiver.try_recv() {
Ok(_) => {
let signal = nix::sys::signal::Signal::SIGINT;
if let Err(err) = nix::sys::signal::kill(game_pid, signal) {
error!("{}", err);
}
}
Err(ipc::TryRecvError::Empty) => {
}
Err(err) => {
error!("{}", err);
break;
}
}
sleep(Duration::from_millis(500));
}
}
Err(_) => {
return Err(CobbleError::ProcessForking);
}
}
Ok(handle)
}
pub fn launch_command(
java_exec: String,
minecraft_path: impl AsRef<Path>,
jvm_args: Vec<String>,
log_argument: Option<String>,
main_class: String,
game_args: Vec<String>,
) -> Command {
let mut command = Command::new(java_exec);
command.current_dir(minecraft_path);
command.args(jvm_args);
if let Some(log_argument) = log_argument {
command.arg(log_argument);
}
command.arg(main_class);
command.args(game_args);
command
}
pub fn build_launch_command(
version_data: &VersionData,
minecraft_path: impl AsRef<Path>,
libraries_path: impl AsRef<Path>,
assets_path: impl AsRef<Path>,
natives_path: impl AsRef<Path>,
log_configs_path: impl AsRef<Path>,
launch_options: &LaunchOptions,
) -> CobbleResult<Command> {
debug!("Building launch command");
trace!("Version: {}", &version_data.id);
trace!(
"Minecraft Path: {}",
minecraft_path.as_ref().to_string_lossy()
);
trace!(
"Libraries Path: {}",
libraries_path.as_ref().to_string_lossy()
);
trace!("Assets Path: {}", assets_path.as_ref().to_string_lossy());
trace!("Natives Path: {}", natives_path.as_ref().to_string_lossy());
trace!(
"Log Configs Path: {}",
log_configs_path.as_ref().to_string_lossy()
);
let classpath = build_classpath(version_data, &minecraft_path, &libraries_path)?;
let argument_replacements = ArgumentReplacements::build(
launch_options,
version_data,
classpath,
minecraft_path.as_ref().to_string_lossy().to_string(),
assets_path.as_ref().to_string_lossy().to_string(),
natives_path.as_ref().to_string_lossy().to_string(),
);
let jvm_args = build_jvm_args(version_data, &argument_replacements, launch_options);
let game_args = build_game_args(version_data, &argument_replacements, launch_options);
let log_argument = version_data.logging.clone().map(|logging_info| {
let mut log_config_path = PathBuf::from(log_configs_path.as_ref());
log_config_path.push(
logging_info
.client
.file
.id
.as_ref()
.expect("Logging info has no ID"),
);
logging_info
.client
.argument
.clone()
.replace("${path}", &log_config_path.to_string_lossy())
});
let command = launch_command(
get_java_exec(launch_options),
minecraft_path,
jvm_args,
log_argument,
version_data.main_class.clone(),
game_args,
);
Ok(command)
}
pub fn get_java_exec(launch_options: &LaunchOptions) -> String {
if !launch_options.java_exec.is_empty() {
return launch_options.java_exec.clone();
}
String::from("java")
}
pub fn build_game_args(
version_data: &VersionData,
argument_replacements: &ArgumentReplacements,
launch_options: &LaunchOptions,
) -> Vec<String> {
debug!("Building Minecraft Args");
let mut arguments = vec![];
if let Some(args) = &version_data.arguments {
for argument in args.game_arguments() {
arguments.push(argument_replacements.replace(&argument));
}
}
if let Some(args) = &version_data.minecraft_arguments {
for argument in args.split_whitespace() {
arguments.push(argument_replacements.replace(argument));
}
}
if launch_options.use_fullscreen {
arguments.push("--fullscreen".to_string());
} else if launch_options.enable_window_size {
arguments.extend_from_slice(&[
"--width".to_string(),
launch_options.window_width.to_string(),
"--height".to_string(),
launch_options.window_height.to_string(),
]);
}
arguments
}
pub fn build_jvm_args(
version_data: &VersionData,
argument_replacements: &ArgumentReplacements,
launch_options: &LaunchOptions,
) -> Vec<String> {
debug!("Building JVM Args");
let mut arguments = vec![];
if launch_options.enable_memory {
arguments.push(format!("-Xms{}M", launch_options.min_memory));
arguments.push(format!("-Xmx{}M", launch_options.max_memory));
}
if launch_options.enable_jvm_args {
for argument in launch_options.jvm_args.split_whitespace() {
arguments.push(argument_replacements.replace(argument));
}
} else if let Some(args) = &version_data.arguments {
for argument in args.jvm_arguments() {
arguments.push(argument_replacements.replace(&argument));
}
}
if !arguments
.iter()
.any(|arg| arg.starts_with("-Djava.library.path="))
{
let arg = "-Djava.library.path=${natives_directory}";
arguments.push(argument_replacements.replace(arg));
}
if !arguments.iter().any(|arg| arg.starts_with("-cp")) {
let arg = "${classpath}";
arguments.push("-cp".to_string());
arguments.push(argument_replacements.replace(arg));
}
arguments
}
pub fn build_classpath(
version_data: &VersionData,
minecraft_path: impl AsRef<Path>,
libraries_path: impl AsRef<Path>,
) -> CobbleResult<String> {
debug!("Building classpath");
let mut classes: Vec<String> = vec![];
for library in version_data.needed_libraries() {
let jar_path = match &library.get_native() {
Some(native) => library.jar_path(libraries_path.as_ref(), Some(native))?,
None => library.jar_path(libraries_path.as_ref(), None)?,
};
classes.push(jar_path.to_string_lossy().to_string());
}
let mut minecraft_path = PathBuf::from(minecraft_path.as_ref());
minecraft_path.push("bin");
minecraft_path.push(format!("minecraft-{}-client.jar", &version_data.id));
classes.push(minecraft_path.to_string_lossy().to_string());
Ok(classes.join(&Platform::current().classpath_seperator().to_string()))
}