From ecfee2ec424c5ee30d4e7d8e37e0b9cf8b462ac5 Mon Sep 17 00:00:00 2001 From: Philip Kristoffersen Date: Fri, 31 Dec 2021 14:32:44 +0100 Subject: [PATCH] Add tests for egs launcher shortcut --- src/egs/example_item.json | 61 +++++++++++++++++++++ src/egs/manifest_item.rs | 108 ++++++++++++++++++++++++++++++-------- 2 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 src/egs/example_item.json diff --git a/src/egs/example_item.json b/src/egs/example_item.json new file mode 100644 index 0000000..00c4fba --- /dev/null +++ b/src/egs/example_item.json @@ -0,0 +1,61 @@ +{ + "FormatVersion": 0, + "bIsIncompleteInstall": false, + "LaunchCommand": "", + "LaunchExecutable": "retail/gotg.exe", + "ManifestLocation": "C:\\Games\\MarvelGOTG/.egstore", + "bIsApplication": true, + "bIsExecutable": true, + "bIsManaged": false, + "bNeedsValidation": false, + "bRequiresAuth": true, + "bAllowMultipleInstances": false, + "bCanRunOffline": true, + "bAllowUriCmdArgs": false, + "BaseURLs": [ + "http://download.epicgames.com/Builds/Org/o-7petn7mrlk8g86ktqm7uglcr7lfaja/fb40e48d8e454558a2e02ad7f9a991ff/default", + "http://download2.epicgames.com/Builds/Org/o-7petn7mrlk8g86ktqm7uglcr7lfaja/fb40e48d8e454558a2e02ad7f9a991ff/default", + "http://download3.epicgames.com/Builds/Org/o-7petn7mrlk8g86ktqm7uglcr7lfaja/fb40e48d8e454558a2e02ad7f9a991ff/default", + "http://download4.epicgames.com/Builds/Org/o-7petn7mrlk8g86ktqm7uglcr7lfaja/fb40e48d8e454558a2e02ad7f9a991ff/default", + "http://epicgames-download1.akamaized.net/Builds/Org/o-7petn7mrlk8g86ktqm7uglcr7lfaja/fb40e48d8e454558a2e02ad7f9a991ff/default", + "http://fastly-download.epicgames.com/Builds/Org/o-7petn7mrlk8g86ktqm7uglcr7lfaja/fb40e48d8e454558a2e02ad7f9a991ff/default" + ], + "BuildLabel": "Live", + "AppCategories": [ + "public", + "games", + "applications" + ], + "ChunkDbs": [], + "CompatibleApps": [], + "DisplayName": "Marvel's Guardians of the Galaxy", + "InstallationGuid": "F222C4654922E0B5AD85FEAA13041D0C", + "InstallLocation": "C:\\Games\\MarvelGOTG", + "InstallSessionId": "5F9B61224533B88E5B286BAE553606FD", + "InstallTags": [], + "InstallComponents": [], + "HostInstallationGuid": "00000000000000000000000000000000", + "PrereqIds": [], + "StagingLocation": "C:\\Games\\MarvelGOTG/.egstore/bps", + "TechnicalType": "public,games,applications", + "VaultThumbnailUrl": "", + "VaultTitleText": "", + "InstallSize": 81199814148, + "MainWindowProcessName": "", + "ProcessNames": [], + "BackgroundProcessNames": [], + "ExpectingDLCInstalled": + { + "2a09fb19b47f46dfb11ebd382f132a8f:b2666e211d334d15a0c29188d0abb6c8:9596620d263b40c083ddf1f0c662c8ff": true + }, + "MandatoryAppFolderName": "MarvelGOTG", + "OwnershipToken": "true", + "CatalogNamespace": "2a09fb19b47f46dfb11ebd382f132a8f", + "CatalogItemId": "88f4bb0bb06e4962a2042d5e20fb6ace", + "AppName": "63a665088eb1480298f1e57943b225d8", + "AppVersionString": "2982110", + "MainGameCatalogNamespace": "2a09fb19b47f46dfb11ebd382f132a8f", + "MainGameCatalogItemId": "88f4bb0bb06e4962a2042d5e20fb6ace", + "MainGameAppName": "63a665088eb1480298f1e57943b225d8", + "AllowedUriEnvVars": [] +} diff --git a/src/egs/manifest_item.rs b/src/egs/manifest_item.rs index 7583f72..af31036 100644 --- a/src/egs/manifest_item.rs +++ b/src/egs/manifest_item.rs @@ -1,7 +1,7 @@ use std::path::Path; use serde::{Deserialize, Serialize}; -use steam_shortcuts_util::{Shortcut, shortcut::ShortcutOwned}; +use steam_shortcuts_util::{shortcut::ShortcutOwned, Shortcut}; #[derive(Serialize, Deserialize, Debug, Clone)] pub(crate) struct ManifestItem { @@ -20,35 +20,101 @@ pub(crate) struct ManifestItem { #[serde(alias = "AppName")] pub app_name: String, - #[serde(alias = "LaunchCommand")] - pub launch_command: String, + #[serde(alias = "CatalogNamespace")] + pub catalog_namespace: String, + + #[serde(alias = "CatalogItemId")] + pub catalog_item_id: String, } +fn exe_shortcut(manifest: ManifestItem) -> ShortcutOwned { + let exe = manifest.exe(); + let mut start_dir = manifest.install_location.clone(); + if !manifest.install_location.starts_with('"') { + start_dir = format!("\"{}\"", manifest.install_location); + } + Shortcut::new( + 0, + manifest.display_name.as_str(), + exe.as_str(), + start_dir.as_str(), + "", + "", + "", + ) + .to_owned() +} +fn launcher_shortcut(manifest: ManifestItem) -> ShortcutOwned { + let icon = manifest.exe(); + let options = manifest.get_launch_url(); + Shortcut::new( + 0, + manifest.display_name.as_str(), + "explorer.exe", + "", + icon.as_str(), + "", + options.as_str(), + ) + .to_owned() +} impl From for ShortcutOwned { fn from(manifest: ManifestItem) -> Self { - - let exe_path = Path::new(&manifest.install_location).join(manifest.launch_executable).to_string_lossy().to_string(); - let exe = format!("\"{}\"",exe_path); - let mut start_dir = manifest.install_location.clone(); - if !manifest.install_location.starts_with('"') { - start_dir = format!("\"{}\"", manifest.install_location); - } - let shortcut = Shortcut::new( - 0, - manifest.display_name.as_str(), - exe.as_str(), - start_dir.as_str(), - "", - "", - manifest.launch_command.as_str(), - ); - let mut owned_shortcut = shortcut.to_owned(); + let mut owned_shortcut = if manifest.needs_launcher() { + launcher_shortcut(manifest) + } else { + exe_shortcut(manifest) + }; owned_shortcut.tags.push("EGS".to_owned()); owned_shortcut.tags.push("Ready TO Play".to_owned()); owned_shortcut.tags.push("Installed".to_owned()); - owned_shortcut } } + +impl ManifestItem { + fn exe(&self) -> String { + let manifest = self; + let exe_path = Path::new(&manifest.install_location) + .join(&manifest.launch_executable) + .to_string_lossy() + .to_string(); + let exe = format!("\"{}\"", exe_path); + exe + } + + fn get_launch_url(&self) -> String { + format!( + "com.epicgames.launcher://apps/{}%3A{}%3A{}?action=launch&silent=true", + self.catalog_namespace, self.catalog_item_id, self.app_name + ) + } + fn needs_launcher(&self) -> bool { + false + } +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn can_parse_item() { + let json = include_str!("example_item.json"); + let _: ManifestItem = serde_json::from_str(json).unwrap(); + } + + #[test] + fn generates_launch_string() { + let json = include_str!("example_item.json"); + let expected ="com.epicgames.launcher://apps/2a09fb19b47f46dfb11ebd382f132a8f%3A88f4bb0bb06e4962a2042d5e20fb6ace%3A63a665088eb1480298f1e57943b225d8?action=launch&silent=true"; + + let manifest: ManifestItem = serde_json::from_str(json).unwrap(); + let actual = manifest.get_launch_url(); + + assert_eq!(expected, actual); + } +}