Skip to content

Commit cf57c77

Browse files
committed
Small progress on rust rewrite
1 parent 227f300 commit cf57c77

File tree

5 files changed

+301
-0
lines changed

5 files changed

+301
-0
lines changed

rust/Cargo.lock

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
serde_json = "1.0"
10+
anyhow = "*"

rust/src/context.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! A Minecraft's installation context.
2+
3+
use std::path::{Path, PathBuf};
4+
5+
6+
/// This structure represents the context of a Minecraft's installation.
7+
#[derive(Debug)]
8+
pub struct Context {
9+
/// The working directory from where the game is run, the game stores thing like
10+
/// saves, resource packs, options and mods if relevant.
11+
pub work_dir: PathBuf,
12+
/// The versions directory contains one directory per version, each containing the
13+
/// version metadata and potentially the version jar file.
14+
pub versions_dir: PathBuf,
15+
/// The assets directory contains the whole assets index.
16+
pub assets_dir: PathBuf,
17+
/// The libraries directory contains the various Java libraries required by the game.
18+
pub libraries_dir: PathBuf,
19+
/// The JVM directory is specific to PortableMC, and contains the official Java
20+
/// versions provided by Microsoft for some common architectures.
21+
pub jvm_dir: PathBuf,
22+
/// The binary directory contains temporary directories that are used only during the
23+
/// game's runtime, modern versions no longer use it but it.
24+
pub bin_dir: PathBuf,
25+
}
26+
27+
impl Default for Context {
28+
29+
fn default() -> Self {
30+
todo!("new with default minecraft dir");
31+
}
32+
33+
}
34+
35+
impl Context {
36+
37+
/// Create a basic context with all common directories derived from the given main
38+
/// directory. The work directory is also set to the main directory.
39+
pub fn new(main_dir: impl AsRef<Path>) -> Self {
40+
41+
let main_dir: &Path = main_dir.as_ref();
42+
43+
Self {
44+
work_dir: main_dir.to_path_buf(),
45+
versions_dir: main_dir.join("versions"),
46+
assets_dir: main_dir.join("assets"),
47+
libraries_dir: main_dir.join("libraries"),
48+
jvm_dir: main_dir.join("jvm"),
49+
bin_dir: main_dir.join("bin"),
50+
}
51+
52+
}
53+
54+
/// Change the work directory to the given one.
55+
pub fn with_work_dir(&mut self, work_dir: impl Into<PathBuf>) -> &mut Self {
56+
self.work_dir = work_dir.into();
57+
self
58+
}
59+
60+
/// Get a version directory from its version id.
61+
pub fn get_version_dir(&self, id: &str) -> PathBuf {
62+
self.versions_dir.join(id)
63+
}
64+
65+
}

rust/src/install.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//! Standard installer actions.
2+
3+
use std::default;
4+
5+
use serde_json::Value;
6+
7+
use crate::context::Context;
8+
9+
10+
/// This is the standard version installer that provides minimal and common installation
11+
/// of Minecraft versions. The install procedure given by this installer is idempotent,
12+
/// which mean that if the installer's configuration has not been modified, running it a
13+
/// second time won't do any modification.
14+
pub struct Installer {
15+
/// The directory context to install the game.
16+
context: Context,
17+
/// The version identifier to ultimately install.
18+
version: Version,
19+
}
20+
21+
impl Installer {
22+
23+
/// Create a new installer for the latest release and a default context.
24+
pub fn new() -> Self {
25+
Self::with_version(MojangVersion::Release.into())
26+
}
27+
28+
/// Create a new installer for the given version and a default context.
29+
#[inline]
30+
pub fn with_version(version: Version) -> Self {
31+
Self::with_context(Context::default(), version)
32+
}
33+
34+
/// Create a new installer with the given version and context.
35+
pub fn with_context(context: Context, version: Version) -> Self {
36+
Self {
37+
context,
38+
version,
39+
}
40+
}
41+
42+
/// Get a reference to the context used by this installer.
43+
#[inline]
44+
pub fn context(&self) -> &Context {
45+
&self.context
46+
}
47+
48+
/// Get a reference to the version to install.
49+
#[inline]
50+
pub fn version(&self) -> &Version {
51+
&self.version
52+
}
53+
54+
pub fn install(&mut self) -> () {
55+
56+
let main_id = match &self.version {
57+
Version::Local(id) => &id[..],
58+
Version::Mojang(_) => todo!(),
59+
Version::Fabric(_) => todo!(),
60+
Version::Forge(_) => todo!(),
61+
};
62+
63+
let mut resolving_id = main_id;
64+
65+
loop {
66+
67+
let dir = self.context.get_version_dir(main_id);
68+
69+
70+
}
71+
72+
}
73+
74+
fn resolve_hierarchy(&mut self) {
75+
76+
}
77+
78+
}
79+
80+
/// An event handler for installation process.
81+
pub trait Handler {
82+
83+
}
84+
85+
/// Describe a version to install.
86+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
87+
pub enum Version {
88+
/// Install a version from its local metadata only.
89+
Local(String),
90+
/// Install a Mojang's official version from manifest.
91+
Mojang(MojangVersion),
92+
/// Install a Fabric mod loader version.
93+
Fabric(FabricVersion),
94+
/// Install a Forge mod loader version.
95+
Forge(ForgeVersion),
96+
}
97+
98+
/// Describe a Mojang version from manifest.
99+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
100+
pub enum MojangVersion {
101+
/// Target the latest version, this will be resolved against the manifest.
102+
Release,
103+
/// Target the latest snapshot, this will be resolved against the manifest.
104+
Snapshot,
105+
/// Target a version from its identifier.
106+
Specific(String),
107+
}
108+
109+
/// Describe a fabric version to install.
110+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
111+
pub struct FabricVersion {
112+
pub api: (),
113+
pub mojang_version: MojangVersion,
114+
pub loader_version: Option<String>,
115+
}
116+
117+
/// Describe a forge version to install.
118+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
119+
pub struct ForgeVersion {
120+
pub mojang_version: MojangVersion,
121+
}
122+
123+
impl From<MojangVersion> for Version {
124+
#[inline]
125+
fn from(value: MojangVersion) -> Self {
126+
Self::Mojang(value)
127+
}
128+
}
129+
130+
impl From<FabricVersion> for Version {
131+
#[inline]
132+
fn from(value: FabricVersion) -> Self {
133+
Self::Fabric(value)
134+
}
135+
}
136+
137+
impl From<ForgeVersion> for Version {
138+
#[inline]
139+
fn from(value: ForgeVersion) -> Self {
140+
Self::Forge(value)
141+
}
142+
}

rust/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use std::collections::HashMap;
22
use std::num::NonZeroU16;
33

44

5+
pub mod context;
6+
pub mod install;
7+
8+
59
/// Handlers can be added to a version to alter the game while resolving it.
610
pub trait Handler {
711

0 commit comments

Comments
 (0)