Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(piston): java runtime manifest support #12

Merged
merged 2 commits into from
May 23, 2024
Merged
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
1 change: 0 additions & 1 deletion crates/spuz_get/src/api.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/spuz_get/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod api;

9 changes: 4 additions & 5 deletions crates/spuz_piston/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ edition.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
readme.workspace = true
description = "1"
readme = "readme.md"

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }

cfg-if = { version = "1" }
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1" }
cfg-if = { version = "1" }

[dev-dependencies]
paste = { version = "1" }
Expand Down
43 changes: 43 additions & 0 deletions crates/spuz_piston/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# spuz_piston <sub><sub>_*by [coppebars](https://github.com/coppebars)*_<sub/><sub/>

Json specification of the minecraft json things, such as version manifests, version lists, jre components

## Supported documents

* Version
manifest ([example](https://piston-meta.mojang.com/v1/packages/111890b5a8c2fee9b77036f9f377b33df42c718a/1.20.6.json))

Contains information about the game files and how else to run the game
* Version list ([example](https://piston-meta.mojang.com/mc/game/version_manifest_v2.json))

Information about versions over time
* Asset Index ([example](https://piston-meta.mojang.com/v1/packages/70b356f90765d4b8da2ae93737d0f384e2343c4a/16.json))

Game assets files metadata
* Java
Runtimes ([example](https://launchermeta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json))

Java runtime components that minecraft runs on
* Java Runtime
Manifest ([example](https://piston-meta.mojang.com/v1/packages/8adc688f802f47a4e5e8f5c15c459448a8591a23/manifest.json))

All files you can download to get jre for target component

## Terminology

* **Manifest** - json file required by the game
* **Java Component** - Different versions of Minecraft require different versions of Java, this is expressed by
specifying the component in the version manifest. It doesn't make sense outside of the context of Minecraft. This is
an internal value that maps to the required version of jre. Usually the name of the component is: `java-runtime-gamma` or `java-runtime-delta`
* **Rule** - This is a simple condition that configures the version manifest (to launch or download files) for a particular OS version to not download unnecessary files and apply OS-specific optimizations.

## How to launch from any manifest
You can use **spuz_piston** along with [spuz_spawner](https://lib.rs/crates/spuz_spawner) and [spuz_wrench](https://lib.rs/crates/spuz_wrench) to configure and run the game process

## Example

```rust
// Read manifest from filesystem
let manifest_str = fs::read_to_string("./1.20.6.json")?;
let manifest = Manifest::from_str(&manifest_str)?;
```
8 changes: 5 additions & 3 deletions crates/spuz_piston/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub mod assets;
pub mod list;
mod manifest;
pub mod manifest;
pub mod platform;
mod profiles;
pub mod profiles;
pub mod rule;
pub mod runtime;
pub mod shared;
#[cfg(test)]
mod test;
Expand All @@ -15,4 +16,5 @@ pub use profiles::LauncherProfiles;
pub use rule::{
ConditionalValue, Feature, FeatureSet, PlatformRequirement, Rule, RuleAction, RuleCompilance, RuleCondition,
};
pub use shared::{Arr, BoxPath, Size, Str};
pub use runtime::{RuntimeComponents, RuntimeManifest};
pub(crate) use shared::{Arr, BoxPath, Size, Str};
227 changes: 227 additions & 0 deletions crates/spuz_piston/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
use std::{collections::HashMap, ops::Deref, str::FromStr};

use cfg_if::cfg_if;
use serde::{Deserialize, Serialize};

use crate::{shared::BoxPath, Size, Str};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RuntimeTarget {
#[serde(rename = "linux")]
Linux,
#[serde(rename = "linux-i386")]
LinuxI386,
#[serde(rename = "mac-os")]
Macos,
#[serde(rename = "mac-os-arm64")]
MacosArm64,
#[serde(rename = "windows-arm64")]
WindowsArm64,
#[serde(rename = "windows-x64")]
WindowsX64,
#[serde(rename = "windows-x86")]
WindowsX86,
#[serde(other)]
GamecoreOrUnknown,
}

cfg_if! {
if #[cfg(all(target_os = "linux", target_arch = "x86_64"))] {
pub const TARGET_RUNTIME: RuntimeTarget = RuntimeTarget::Linux;
} else if #[cfg(all(target_os = "linux", target_arch = "x86"))] {
pub const TARGET_RUNTIME: RuntimeTarget = RuntimeTarget::LinuxI386;
} else if #[cfg(all(target_os = "macos", target_arch = "x86_64"))] {
pub const TARGET_RUNTIME: RuntimeTarget = RuntimeTarget::Macos;
} else if #[cfg(all(target_os = "macos", target_arch = "aarch64"))] {
pub const TARGET_RUNTIME: RuntimeTarget = RuntimeTarget::MacosArm64;
} else if #[cfg(all(target_os = "windows", target_arch = "aarch64"))] {
pub const TARGET_RUNTIME: RuntimeTarget = RuntimeTarget::WindowsArm64;
} else if #[cfg(all(target_os = "windows", target_arch = "x86_64"))] {
pub const TARGET_RUNTIME: RuntimeTarget = RuntimeTarget::WindowsX64;
} else if #[cfg(all(target_os = "windows", target_arch = "x86"))] {
pub const TARGET_RUNTIME: RuntimeTarget = RuntimeTarget::WindowsX86;
}
}

impl RuntimeTarget {
pub fn is_target(self) -> bool {
self == TARGET_RUNTIME
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Component {
#[serde(rename = "java-runtime-alpha")]
Alpha,
#[serde(rename = "java-runtime-beta")]
Beta,
#[serde(rename = "java-runtime-delta")]
Delta,
#[serde(rename = "java-runtime-gamma")]
Gamma,
#[serde(rename = "java-runtime-gamma-snapshot")]
GammaSnapshot,
#[serde(rename = "jre-legacy")]
Legacy,
#[serde(rename = "minecraft-java-exe")]
Exe,
}

impl FromStr for Component {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"java-runtime-alpha" => Ok(Self::Alpha),
"java-runtime-beta" => Ok(Self::Beta),
"java-runtime-delta" => Ok(Self::Delta),
"java-runtime-gamma" => Ok(Self::Gamma),
"java-runtime-gamma-snapshot" => Ok(Self::GammaSnapshot),
"jre-legacy" => Ok(Self::Legacy),
"minecraft-java-exe" => Ok(Self::Exe),
_ => Err(()),
}
}
}

mod private {
pub(super) trait SealedParseComponent {}
impl<T: AsRef<str>> SealedParseComponent for T {}
}

pub trait ParseComponent {
fn parse_component(&self) -> Option<Component>;
}

impl<T: AsRef<str> + private::SealedParseComponent> ParseComponent for T {
fn parse_component(&self) -> Option<Component> {
self.as_ref().parse().ok()
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AvailabilityInfo {
pub group: u32,
pub progress: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VersionInfo {
pub name: Str,
pub released: Str,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ManifestInfo {
pub url: Str,
pub size: Size,
pub sha1: Str,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ComponentInfo {
pub availability: AvailabilityInfo,
pub version: VersionInfo,
pub manifest: ManifestInfo,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ComponentInfoItem {
pub inner: Box<[ComponentInfo]>,
}

impl Deref for ComponentInfoItem {
type Target = [ComponentInfo];

fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ComponentList {
pub inner: HashMap<Component, ComponentInfoItem>,
}

impl ComponentList {
pub fn get(&self, component: &impl ParseComponent) -> Option<&ComponentInfo> {
component.parse_component().and_then(|it| self.inner.get(&it).and_then(|it| it.first()))
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct RuntimeComponents {
pub targets: HashMap<RuntimeTarget, ComponentList>,
}

impl Deref for RuntimeComponents {
type Target = HashMap<RuntimeTarget, ComponentList>;

fn deref(&self) -> &Self::Target {
&self.targets
}
}

impl RuntimeComponents {
pub fn target(&self) -> Option<&ComponentList> {
self.targets.get(&TARGET_RUNTIME)
}

pub fn component(&self, component: &impl ParseComponent) -> Option<&ComponentInfo> {
self.target().and_then(|it| it.get(component))
}
}

impl FromStr for RuntimeComponents {
type Err = serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeFile {
pub sha1: Str,
pub url: Str,
pub size: Size,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeFileDownloads {
pub raw: RuntimeFile,
pub lzma: Option<RuntimeFile>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "lowercase")]
pub enum RuntimeSource {
File { downloads: RuntimeFileDownloads, executable: bool },
Link { target: BoxPath },
Directory,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuntimeManifest {
files: HashMap<BoxPath, RuntimeSource>,
}

impl Deref for RuntimeManifest {
type Target = HashMap<BoxPath, RuntimeSource>;

fn deref(&self) -> &Self::Target {
&self.files
}
}

impl FromStr for RuntimeManifest {
type Err = serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
Loading