Skip to content

Commit 3b63647

Browse files
Merge Main to Release: v0.1.1 (#3)
* change auto build to release branch * Add checks to create config if it doesnt exist * Added sorting by last played * fix extra console opening on windows * move instance reading to js file * unify sites to use Topbar * make instance loading not guess when its finished * Add instance id generating + saving + reading * enhance instance page * Java Version Controll, mostly * Minor touch-ups * Java MC version selection * Add Notification System * Polish Notification System * Some Fixes for building * Increment version to 0.1.1
1 parent e50d9ea commit 3b63647

29 files changed

+887
-163
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rust-analyzer.linkedProjects": [
3+
"./src-tauri/Cargo.toml"
4+
]
5+
}

.vscode/tasks.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "Tauri Dev",
8+
"type": "shell",
9+
"command": "cargo tauri dev",
10+
"presentation": {"echo": true, "reveal": "always", "focus": false, "panel": "shared", "showReuseMessage": false, "clear": true},
11+
}
12+
]
13+
}

src-tauri/Cargo.lock

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

src-tauri/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ tauri-build = { version = "1.4.0", features = [] }
1717
[dependencies]
1818
serde_json = "1.0"
1919
serde = { version = "1.0", features = ["derive"] }
20-
tauri = { version = "1.4.0", features = ["dialog-all", "fs-all", "path-all", "protocol-asset"] }
20+
tauri = { version = "1.4.0", features = [ "fs-create-dir", "fs-read-file", "fs-exists", "fs-write-file", "fs-read-dir", "shell-open", "dialog-all", "path-all", "protocol-asset"] }
2121
configparser = "3.0.2"
22+
fastrand = "2.0.0"
2223

2324
[features]
2425
# by default Tauri runs in production mode

src-tauri/src/instances.rs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fs::DirEntry;
22
use std::fs::read_to_string;
3+
use std::fs::write;
34

45
use configparser::ini::Ini;
56
use serde_json::Value;
@@ -10,14 +11,15 @@ struct InstanceData {
1011
name: String,
1112
icon: String,
1213
path: String,
14+
id: u32,
1315
last_played_string: String,
1416
last_played_epoch: u64
1517
}
1618

1719

1820
pub fn handle_instance_cf(dir: DirEntry, app_handle: AppHandle) {
19-
let data = read_to_string(dir.path().join("minecraftinstance.json")).unwrap();
20-
let json: Value = serde_json::from_str(&data).unwrap();
21+
let file = read_to_string(dir.path().join("minecraftinstance.json")).unwrap_or(String::from(""));
22+
let json: Value = serde_json::from_str(&file).unwrap();
2123
let name = json["name"].as_str().unwrap_or("Name not found!").to_string();
2224
let mut icon = json["installedModpack"]["thumbnailUrl"].as_str().unwrap_or("").to_string();
2325
if icon.is_empty() {
@@ -26,27 +28,53 @@ pub fn handle_instance_cf(dir: DirEntry, app_handle: AppHandle) {
2628
let path = dir.path().to_str().unwrap_or("invalid_path").to_string();
2729
let last_played_string = json["lastPlayed"].as_str().unwrap_or("invalid").to_string();
2830

29-
println!("Curseforge: {}, Last Played: {}", &name, &last_played_string);
30-
emit_instance_create(InstanceData { name, icon, path, last_played_string, last_played_epoch: 0 }.into(), app_handle)
31+
let id = get_or_create_instance_id(dir);
32+
33+
println!("Curseforge: {}, instanceID: {}", &name, &id);
34+
emit_instance_create(InstanceData { name, icon, path, id, last_played_string, last_played_epoch: 0 }.into(), app_handle)
3135
}
3236

3337
pub fn handle_instance_mmc(dir: DirEntry, app_handle: AppHandle) {
34-
let mut data = read_to_string(dir.path().join("instance.cfg")).unwrap();
35-
data = data.replace("[General]", "");
38+
let mut file = read_to_string(dir.path().join("instance.cfg")).unwrap_or(String::from(""));
39+
file = file.replace("[General]", "");
3640

3741
let mut config = Ini::new();
38-
config.read(data).unwrap();
42+
config.read(file).unwrap();
3943

4044
let name = config.get("default","name").unwrap_or(String::from("Name not found!"));
4145
let icon = config.get("default","iconKey").unwrap_or(String::from("default"));
42-
let path = dir.path().join(".minecraft").to_str().unwrap_or("invalid_path").to_string();
46+
let path = dir.path().to_str().unwrap_or("invalid_path").to_string();
4347
let last_played_epoch: u64 = config.get("default","lastLaunchTime").unwrap_or(String::from("1")).parse::<u64>().unwrap_or(0);
44-
4548

46-
println!("MultiMC: {}, Last Played: {}", &name, &last_played_epoch);
47-
emit_instance_create(InstanceData { name, icon, path, last_played_string: "".to_string(), last_played_epoch }.into(), app_handle)
49+
let id = get_or_create_instance_id(dir);
50+
51+
println!("MultiMC: {}, instanceID: {}", &name, &id);
52+
emit_instance_create(InstanceData { name, icon, path, id, last_played_string: "".to_string(), last_played_epoch }.into(), app_handle)
53+
}
54+
55+
fn get_or_create_instance_id(dir: DirEntry) -> u32 {
56+
let file = read_to_string(dir.path().join("yamcl-data.json")).unwrap_or(String::from(""));
57+
let rand = fastrand::u32(..);
58+
59+
if file.is_empty() || !serde_json::from_str::<Value>(&file).unwrap().is_object() {
60+
let mut json = serde_json::json!({});
61+
json["instanceID"] = Value::from(rand);
62+
write(dir.path().join("yamcl-data.json"), serde_json::to_string(&json).unwrap()).unwrap();
63+
rand
64+
} else {
65+
let mut json: Value = serde_json::from_str(&file).unwrap();
66+
let id: u32 = json["instanceID"].as_u64().unwrap_or(0).try_into().unwrap_or(0);
67+
if id==0 {
68+
json["instanceID"] = Value::from(rand);
69+
write(dir.path().join("yamcl-data.json"), serde_json::to_string(&json).unwrap()).unwrap();
70+
rand
71+
} else {
72+
id
73+
}
74+
}
4875
}
4976

77+
5078
fn emit_instance_create(data: InstanceData, app_handle: AppHandle) {
5179
app_handle.emit_all("instance_create", data).unwrap()
5280
}

src-tauri/src/main.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
//Prevents console window on windows
2+
#![cfg_attr(
3+
all(not(debug_assertions), target_os = "windows"),
4+
windows_subsystem = "windows"
5+
)]
6+
17
use std::{fs::{self}, path::Path};
28

39
use tauri::{AppHandle, Manager};
410
mod instances;
11+
mod minecraft;
512

613
fn main() {
714
tauri::Builder::default()
815
.invoke_handler(tauri::generate_handler![
916
get_instances,
10-
unlock_icons
17+
unlock_icons,
18+
minecraft::launch_instance,
19+
minecraft::get_java_version
1120
])
1221
.run(tauri::generate_context!())
1322
.expect("failed to run app");
@@ -22,6 +31,7 @@ fn unlock_icons(path: String, app_handle: AppHandle) {
2231
#[tauri::command(async)]
2332
fn get_instances(path: String, app_handle: AppHandle) {
2433
let paths = fs::read_dir(path).unwrap();
34+
let mut instance_count: u16 = 0;
2535

2636
for path in paths {
2737
if path.as_ref().unwrap().file_type().unwrap().is_dir() {
@@ -31,11 +41,12 @@ fn get_instances(path: String, app_handle: AppHandle) {
3141

3242
for file in instance_contents {
3343
match file.unwrap().file_name().into_string().unwrap().as_ref() {
34-
"minecraftinstance.json" => {instances::handle_instance_cf(instance_folder, app_handle.clone()); break;},
35-
"instance.cfg" => {instances::handle_instance_mmc(instance_folder, app_handle.clone()); break;},
44+
"minecraftinstance.json" => {instances::handle_instance_cf(instance_folder, app_handle.clone()); instance_count += 1; break;},
45+
"instance.cfg" => {instances::handle_instance_mmc(instance_folder, app_handle.clone()); instance_count += 1; break;},
3646
_ => continue
3747
}
3848
}
3949
}
4050
}
51+
app_handle.emit_all("instance_finish", instance_count).unwrap();
4152
}

0 commit comments

Comments
 (0)