Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ serde_json = {version = "=1.0.150"}
serde_repr = {version = "=0.1.20"}
sysinfo = {version = "=0.39.5"}
trash = {version = "=5.2.6"}
percent-encoding = {version = "=2.3.2"}
url = {version = "=2.5.8", features = ["serde"]}
uuid = {version = "=1.23.1", features = ["serde", "v4"]}

Expand Down
10 changes: 10 additions & 0 deletions packages/oneclient_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ notify.workspace = true

anyhow.workspace = true
regex.workspace = true
directories.workspace = true
dunce.workspace = true
percent-encoding.workspace = true

cargo-packager-updater = "=0.2.3"

Expand All @@ -58,6 +61,12 @@ freya.workspace = true
[target.'cfg(target_os = "macos")'.dependencies]
mimalloc = "=0.1.52"

[target.'cfg(windows)'.dependencies]
windows = { version = "=0.62.2", features = [
"Win32_Foundation",
"Win32_System_Registry",
] }

[target.'cfg(windows)'.build-dependencies]
winresource = "=0.1.31"

Expand All @@ -71,6 +80,7 @@ homepage = "https://polyfrost.org/projects/oneclient"
long-description = "Next-generation open source Minecraft launcher"
formats = ["nsis", "app", "dmg", "deb", "appimage"]
binaries = [{ path = "oneclient_app", main = true }]
deep-link-protocols = [{ schemes = ["oneclient"] }]
icons = [
"icons/32x32.png",
"icons/64x64.png",
Expand Down
84 changes: 84 additions & 0 deletions packages/oneclient_app/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Cli {
pub launch: Option<String>,
}

impl Cli {
fn from_args(args: impl IntoIterator<Item = String>) -> Self {
let mut cli = Self::default();
let mut args = args.into_iter();

while let Some(arg) = args.next() {
if let Some(value) = arg.strip_prefix("--launch=") {
cli.launch = non_empty(value.to_string());
} else if arg == "--launch" {
cli.launch = args.next().and_then(non_empty);
} else if let Some(folder) = crate::protocol::parse_launch_url(&arg) {
cli.launch = Some(folder);
}
}

cli
}
}

fn non_empty(value: String) -> Option<String> {
let trimmed = value.trim();
(!trimmed.is_empty()).then(|| trimmed.to_string())
}

#[must_use]
pub fn parse() -> Cli {
Cli::from_args(std::env::args().skip(1))
}

#[cfg(test)]
mod tests {
use super::*;

fn parse(args: &[&str]) -> Cli {
Cli::from_args(args.iter().map(|s| (*s).to_string()))
}

#[test]
fn no_arguments_is_an_ordinary_start() {
assert_eq!(parse(&[]).launch, None);
}

#[test]
fn both_spellings_carry_the_folder() {
assert_eq!(parse(&["--launch", "fabric-1-20"]).launch.as_deref(), Some("fabric-1-20"));
assert_eq!(parse(&["--launch=fabric-1-20"]).launch.as_deref(), Some("fabric-1-20"));
}

#[test]
fn a_blank_folder_is_no_request_at_all() {
assert_eq!(parse(&["--launch", " "]).launch, None);
assert_eq!(parse(&["--launch="]).launch, None);
assert_eq!(parse(&["--launch"]).launch, None);
}

#[test]
fn spaces_survive_the_round_trip() {
assert_eq!(
parse(&["--launch", "My Pack (1.8.9)"]).launch.as_deref(),
Some("My Pack (1.8.9)"),
);
}

#[test]
fn unknown_flags_are_ignored_not_fatal() {
assert_eq!(parse(&["--verbose", "--launch", "x", "leftover"]).launch.as_deref(), Some("x"));
}

#[test]
fn a_launch_url_is_accepted_as_a_bare_argument() {
let url = crate::protocol::launch_url("26.1.2 Fabric");
assert_eq!(parse(&[&url]).launch.as_deref(), Some("26.1.2 Fabric"));
}

#[test]
fn a_url_that_is_not_ours_is_not_a_launch() {
assert_eq!(parse(&["https://polyfrost.org"]).launch, None);
}
}
233 changes: 233 additions & 0 deletions packages/oneclient_app/src/file_content.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
#![allow(dead_code)]

use std::path::Path;

pub fn single_line(value: &str) -> String {
let cleaned: String = value
.chars()
.map(|c| if c.is_control() { ' ' } else { c })
.collect();
cleaned.split_whitespace().collect::<Vec<_>>().join(" ")
}

pub fn desktop_exec_arg(value: &str) -> String {
let mut out = String::with_capacity(value.len() + 2);
out.push('"');
for c in value.chars() {
match c {
'"' | '\\' | '$' | '`' => {
out.push('\\');
out.push(c);
}
'%' => out.push_str("%%"),
_ => out.push(c),
}
}
out.push('"');
out
}

pub fn desktop_entry(name: &str, exe: &Path, folder: &str, icon: &str) -> String {
let name = single_line(name);
let exec = format!(
"{} --launch {}",
desktop_exec_arg(&exe.to_string_lossy()),
desktop_exec_arg(folder),
);

format!(
"[Desktop Entry]\n\
Type=Application\n\
Version=1.0\n\
Name={name}\n\
Comment=Launch {name} with OneClient\n\
Exec={exec}\n\
Icon={icon}\n\
Terminal=false\n\
Categories=Game;\n\
StartupNotify=true\n"
)
}

pub fn url_handler_entry(exe: &Path, scheme: &str, icon: &str) -> String {
let exec = desktop_exec_arg(&exe.to_string_lossy());

format!(
"[Desktop Entry]\n\
Type=Application\n\
Name=OneClient\n\
Exec={exec} %u\n\
Icon={icon}\n\
Terminal=false\n\
NoDisplay=true\n\
MimeType=x-scheme-handler/{scheme};\n"
)
}

pub fn sh_quote(value: &str) -> String {
format!("'{}'", value.replace('\'', r"'\''"))
}

pub fn shell_script(exe: &Path, folder: &str) -> String {
format!(
"#!/bin/sh\nexec {} --launch {}\n",
sh_quote(&exe.to_string_lossy()),
sh_quote(folder),
)
}

fn xml_escape(value: &str) -> String {
value
.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;")
.replace('"', "&quot;")
.replace('\'', "&apos;")
}

pub fn bundle_slug(folder: &str) -> String {
let slug: String = folder
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect();
let trimmed = slug.trim_matches('-').to_string();
if trimmed.is_empty() {
"cluster".to_string()
} else {
trimmed
}
}

pub fn info_plist(name: &str, executable: &str, folder: &str, icon: Option<&str>) -> String {
let display = xml_escape(&single_line(name));
let icon_entry = icon.map_or_else(String::new, |icon| {
format!("\t<key>CFBundleIconFile</key>\n\t<string>{}</string>\n", xml_escape(icon))
});

format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
<plist version=\"1.0\">\n\
<dict>\n\
\t<key>CFBundleName</key>\n\t<string>{display}</string>\n\
\t<key>CFBundleDisplayName</key>\n\t<string>{display}</string>\n\
\t<key>CFBundleIdentifier</key>\n\t<string>org.polyfrost.OneClient.shortcut.{slug}</string>\n\
\t<key>CFBundleExecutable</key>\n\t<string>{executable}</string>\n\
{icon_entry}\
\t<key>CFBundlePackageType</key>\n\t<string>APPL</string>\n\
\t<key>CFBundleInfoDictionaryVersion</key>\n\t<string>6.0</string>\n\
\t<key>CFBundleShortVersionString</key>\n\t<string>1.0</string>\n\
\t<key>LSUIElement</key>\n\t<true/>\n\
</dict>\n\
</plist>\n",
executable = xml_escape(executable),
slug = bundle_slug(folder),
)
}

pub fn url_shortcut(url: &str, icon: &Path) -> String {
format!(
"[InternetShortcut]\r\nURL={url}\r\nIconFile={}\r\nIconIndex=0\r\n",
icon.display(),
)
}

#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;

#[test]
fn a_newline_in_a_cluster_name_cannot_forge_a_second_key() {
let entry = desktop_entry(
"Pack\nExec=/bin/sh",
&PathBuf::from("/usr/bin/oneclient_app"),
"pack",
"oneclient_app",
);
assert!(!entry.contains("Exec=/bin/sh"));
assert!(entry.contains("Name=Pack Exec=/bin/sh"));
}

#[test]
fn a_space_in_the_install_path_stays_one_argument() {
let entry = desktop_entry(
"Pack",
&PathBuf::from("/opt/One Client/oneclient_app"),
"my pack",
"oneclient_app",
);
assert!(entry.contains(r#"Exec="/opt/One Client/oneclient_app" --launch "my pack""#));
}

#[test]
fn the_url_handler_quotes_an_install_path_with_a_space() {
let entry = url_handler_entry(
&PathBuf::from("/opt/One Client/oneclient_app"),
"oneclient",
"oneclient_app",
);
assert!(entry.contains(r#"Exec="/opt/One Client/oneclient_app" %u"#));
assert!(entry.contains("MimeType=x-scheme-handler/oneclient;"));
}

#[test]
fn the_url_handler_keeps_the_field_code_outside_the_quoted_path() {
let entry = url_handler_entry(
&PathBuf::from("/usr/bin/oneclient_app"),
"oneclient",
"oneclient_app",
);
assert!(entry.contains(r#"Exec="/usr/bin/oneclient_app" %u"#));
assert!(!entry.contains("%%u"));
}

#[test]
fn percent_is_doubled_for_the_desktop_spec() {
assert_eq!(desktop_exec_arg("100%"), r#""100%%""#);
}

#[test]
fn desktop_reserved_characters_are_escaped() {
assert_eq!(desktop_exec_arg("a$b`c\"d"), r#""a\$b\`c\"d""#);
}

#[test]
fn the_mac_wrapper_survives_a_quote_in_the_path() {
let script = shell_script(&PathBuf::from("/Users/o'brien/OneClient"), "pack");
assert!(script.contains(r"'/Users/o'\''brien/OneClient'"));
assert!(script.starts_with("#!/bin/sh\n"));
}

#[test]
fn the_plist_escapes_a_name_that_looks_like_markup() {
let plist = info_plist("A & <B>", "launch", "pack", Some("icon"));
assert!(plist.contains("<string>A &amp; &lt;B&gt;</string>"));
assert!(!plist.contains("<B>"));
}

#[test]
fn the_plist_omits_the_icon_key_when_there_is_no_icon() {
let plist = info_plist("Pack", "launch", "pack", None);
assert!(!plist.contains("CFBundleIconFile"));
}

#[test]
fn a_bundle_id_keeps_only_what_it_is_allowed() {
assert_eq!(bundle_slug("My Pack (1.8.9)"), "My-Pack--1-8-9");
assert_eq!(bundle_slug("---"), "cluster");
}

#[test]
fn the_url_shortcut_is_a_single_ini_section() {
let file = url_shortcut(
"oneclient://launch/My%20Pack",
&PathBuf::from(r"C:\Program Files\OneClient\oneclient_app.exe"),
);

assert!(file.starts_with("[InternetShortcut]\r\n"));
assert!(file.contains("\r\nURL=oneclient://launch/My%20Pack\r\n"));
assert!(file.contains(r"IconFile=C:\Program Files\OneClient\oneclient_app.exe"));
assert!(file.ends_with("IconIndex=0\r\n"));
}
}
2 changes: 0 additions & 2 deletions packages/oneclient_app/src/hooks/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,6 @@ impl Actions {
.progress(id, "Downloading assets", current, total);
}

/// The pending flag is raised synchronously before the first `await` the
/// claim itself is the guard against a double-click spawning two games
pub fn launch_cluster(&self, cluster_id: ClusterId) {
let claimed = self
.station
Expand Down
Loading