Skip to content

Commit

Permalink
feat: add a way to update jvm args during the launch process
Browse files Browse the repository at this point in the history
  • Loading branch information
Suya1671 committed Jun 4, 2024
1 parent f02cf77 commit d131fd4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
12 changes: 6 additions & 6 deletions flake.lock

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

4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
libraries = with pkgs; [
openssl_3
pkg-config
libGL
mesa_drivers
glfw-wayland
libglvnd
];

packages = with pkgs; [
Expand Down
10 changes: 10 additions & 0 deletions libs/copper/examples/full_launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,20 @@ async fn main() {
.build()
.unwrap();

// launcher.add_jvm_argument(Jvm::String(
// "-Dorg.lwjgl.glfw.libname=glfw_wayland".to_string(),
// ));

info!("Launcher created. Downloading files");

let index = manifest.asset_index().download().await.unwrap();

let index_dir = minecraft_dir.join("assets").join("indexes");
index
.save_to_disk(&index_dir.join("Vanilla.json"))
.await
.unwrap();

let mut downloader =
launcher::Downloader::new(&launcher, index, manifest.libraries().to_vec(), 16);
let mut reciever = downloader.create_channel();
Expand Down
39 changes: 38 additions & 1 deletion libs/copper/src/assets/asset_index.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,56 @@
use error_stack::{report, Result, ResultExt};
use futures::{stream, StreamExt, TryStreamExt};
use serde::{Deserialize, Serialize};
use std::{cmp::min, collections::HashMap, path::PathBuf, sync::Arc};
use serde_json::to_string;
use std::{
cmp::min,
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
};
use tokio::{
fs,
io::AsyncWriteExt,
sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
};
use tracing::debug;

use crate::downloader::{DownloadError, DownloadMessage, Downloader};

use super::client::SaveError;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Assets {
objects: HashMap<String, Object>,
}

impl Assets {
/// Saves the manifest to disk.
///
/// # Errors
/// Returns a [`SaveError`] if the manifest could not be serialized or if an IO error occurred.
#[tracing::instrument]
pub async fn save_to_disk(&self, path: &Path) -> error_stack::Result<(), SaveError> {
debug!("Saving Asset index to disk");
debug!("Serializing index to JSON");
let value = to_string(self).change_context(SaveError::SerializeError)?;

let directory = path.parent().ok_or_else(|| report!(SaveError::IOError))?;

if !directory.exists() {
debug!("Creating directory {}", directory.display());
fs::create_dir_all(directory)
.await
.change_context(SaveError::IOError)?;
}

debug!("Writing asset index to {}", path.display());
fs::write(path, value)
.await
.change_context(SaveError::IOError)
}
}

#[derive(Debug, Clone)]
pub struct AssetDownloader {
assets: Assets,
Expand Down
11 changes: 11 additions & 0 deletions libs/copper/src/assets/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ impl Manifest {
)
}

/// Gets the mutable arguments for a manifest
///
/// # Panics
/// Panics if there are no jvm arguments. This is a TODO
#[must_use]
pub(crate) fn arguments_mut(&mut self) -> &mut Arguments {
self.arguments.as_mut().unwrap_or_else(|| {
todo!("Handle older versions of minecraft that don't have new argument syntax")
})
}

/// Gets the asset index for a manifest
#[must_use]
pub const fn asset_index(&self) -> &AssetIndex {
Expand Down
9 changes: 8 additions & 1 deletion libs/copper/src/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use winsafe::IsWindows10OrGreater;
use crate::{
assets::{
asset_index::{AssetDownloader, Assets, Object},
client::{self, Artifact, ClassDownloader, DownloadClass, Library, LibraryDownloader},
client::{self, Artifact, ClassDownloader, DownloadClass, Jvm, Library, LibraryDownloader},
},
auth::{
structs::{MinecraftProfile, MinecraftToken},
Expand Down Expand Up @@ -498,3 +498,10 @@ impl Launcher {
self.quickplay.as_ref()
}
}

/// Manipulation methods for the launcher
impl Launcher {
pub fn add_jvm_argument(&mut self, arg: Jvm) {
self.manifest.arguments_mut().jvm.push(arg);
}
}

0 comments on commit d131fd4

Please sign in to comment.