aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src-tauri/src/core/fabric.rs
blob: a6ef2361772cda6f13028d4af752b3b833970b29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Fabric Loader support module.
//!
//! This module provides functionality to:
//! - Fetch available Fabric loader versions from the Fabric Meta API
//! - Generate version JSON files for Fabric-enabled Minecraft versions
//! - Install Fabric loader for a specific Minecraft version

use serde::{Deserialize, Serialize};
use std::error::Error;
use std::path::PathBuf;
use ts_rs::TS;

const FABRIC_META_URL: &str = "https://meta.fabricmc.net/v2";

/// Represents a Fabric loader version from the Meta API.
#[derive(Debug, Deserialize, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts")]
pub struct FabricLoaderVersion {
    pub separator: String,
    pub build: i32,
    pub maven: String,
    pub version: String,
    pub stable: bool,
}

/// Represents a Fabric intermediary mapping version.
#[derive(Debug, Deserialize, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts")]
pub struct FabricIntermediaryVersion {
    pub maven: String,
    pub version: String,
    pub stable: bool,
}

/// Represents a combined loader + intermediary version entry.
#[derive(Debug, Deserialize, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts")]
pub struct FabricLoaderEntry {
    pub loader: FabricLoaderVersion,
    pub intermediary: FabricIntermediaryVersion,
    #[serde(rename = "launcherMeta")]
    pub launcher_meta: FabricLauncherMeta,
}

/// Launcher metadata from Fabric Meta API.
#[derive(Debug, Deserialize, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts")]
pub struct FabricLauncherMeta {
    pub version: i32,
    pub libraries: FabricLibraries,
    #[serde(rename = "mainClass")]
    pub main_class: FabricMainClass,
}

/// Libraries required by Fabric loader.
#[derive(Debug, Deserialize, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts")]
pub struct FabricLibraries {
    pub client: Vec<FabricLibrary>,
    pub common: Vec<FabricLibrary>,
    pub server: Vec<FabricLibrary>,
}

/// A single Fabric library dependency.
#[derive(Debug, Deserialize, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts")]
pub struct FabricLibrary {
    pub name: String,
    pub url: Option<String>,
}

/// Main class configuration for Fabric.
/// Can be either a struct with client/server fields or a simple string.
#[derive(Debug, Deserialize, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts", untagged)]
#[serde(untagged)]
pub enum FabricMainClass {
    Structured { client: String, server: String },
    Simple(String),
}

#[allow(dead_code)]
impl FabricMainClass {
    pub fn client(&self) -> &str {
        match self {
            FabricMainClass::Structured { client, .. } => client,
            FabricMainClass::Simple(s) => s,
        }
    }

    pub fn server(&self) -> &str {
        match self {
            FabricMainClass::Structured { server, .. } => server,
            FabricMainClass::Simple(s) => s,
        }
    }
}

/// Represents a Minecraft version supported by Fabric.
#[derive(Debug, Deserialize, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts")]
pub struct FabricGameVersion {
    pub version: String,
    pub stable: bool,
}

/// Information about an installed Fabric version.
#[derive(Debug, Serialize, Clone, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "fabric.ts")]
pub struct InstalledFabricVersion {
    pub id: String,
    pub minecraft_version: String,
    pub loader_version: String,
    pub path: PathBuf,
}

/// Fetch all Minecraft versions supported by Fabric.
///
/// # Returns
/// A list of game versions that have Fabric intermediary mappings available.
pub async fn fetch_supported_game_versions()
-> Result<Vec<FabricGameVersion>, Box<dyn Error + Send + Sync>> {
    let url = format!("{}/versions/game", FABRIC_META_URL);
    let resp = reqwest::get(&url)
        .await?
        .json::<Vec<FabricGameVersion>>()
        .await?;
    Ok(resp)
}

/// Fetch all available Fabric loader versions.
///
/// # Returns
/// A list of all Fabric loader versions, ordered by build number (newest first).
pub async fn fetch_loader_versions()
-> Result<Vec<FabricLoaderVersion>, Box<dyn Error + Send + Sync>> {
    let url = format!("{}/versions/loader", FABRIC_META_URL);
    let resp = reqwest::get(&url)
        .await?
        .json::<Vec<FabricLoaderVersion>>()
        .await?;
    Ok(resp)
}

/// Fetch Fabric loader versions available for a specific Minecraft version.
///
/// # Arguments
/// * `game_version` - The Minecraft version (e.g., "1.20.4")
///
/// # Returns
/// A list of loader entries with full metadata for the specified game version.
pub async fn fetch_loaders_for_game_version(
    game_version: &str,
) -> Result<Vec<FabricLoaderEntry>, Box<dyn Error + Send + Sync>> {
    let url = format!("{}/versions/loader/{}", FABRIC_META_URL, game_version);
    let resp = reqwest::get(&url)
        .await?
        .json::<Vec<FabricLoaderEntry>>()
        .await?;
    Ok(resp)
}

/// Fetch the version JSON profile for a specific Fabric loader + game version combination.
///
/// # Arguments
/// * `game_version` - The Minecraft version (e.g., "1.20.4")
/// * `loader_version` - The Fabric loader version (e.g., "0.15.6")
///
/// # Returns
/// The raw version JSON as a `serde_json::Value` that can be saved to the versions directory.
pub async fn fetch_version_profile(
    game_version: &str,
    loader_version: &str,
) -> Result<serde_json::Value, Box<dyn Error + Send + Sync>> {
    let url = format!(
        "{}/versions/loader/{}/{}/profile/json",
        FABRIC_META_URL, game_version, loader_version
    );
    let resp = reqwest::get(&url)
        .await?
        .json::<serde_json::Value>()
        .await?;
    Ok(resp)
}

/// Generate the version ID for a Fabric installation.
///
/// # Arguments
/// * `game_version` - The Minecraft version
/// * `loader_version` - The Fabric loader version
///
/// # Returns
/// The version ID string (e.g., "fabric-loader-0.15.6-1.20.4")
pub fn generate_version_id(game_version: &str, loader_version: &str) -> String {
    format!("fabric-loader-{}-{}", loader_version, game_version)
}

/// Install Fabric loader for a specific Minecraft version.
///
/// This creates the version JSON file in the versions directory.
/// The actual library downloads happen during game launch.
///
/// # Arguments
/// * `game_dir` - The .minecraft directory path
/// * `game_version` - The Minecraft version (e.g., "1.20.4")
/// * `loader_version` - The Fabric loader version (e.g., "0.15.6")
///
/// # Returns
/// Information about the installed version.
pub async fn install_fabric(
    game_dir: &std::path::Path,
    game_version: &str,
    loader_version: &str,
) -> Result<InstalledFabricVersion, Box<dyn Error + Send + Sync>> {
    // Fetch the version profile from Fabric Meta
    let profile = fetch_version_profile(game_version, loader_version).await?;

    // Get the version ID from the profile or generate it
    let version_id = profile
        .get("id")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string())
        .unwrap_or_else(|| generate_version_id(game_version, loader_version));

    // Create the version directory
    let version_dir = game_dir.join("versions").join(&version_id);
    tokio::fs::create_dir_all(&version_dir).await?;

    // Write the version JSON
    let json_path = version_dir.join(format!("{}.json", version_id));
    let json_content = serde_json::to_string_pretty(&profile)?;
    tokio::fs::write(&json_path, json_content).await?;

    Ok(InstalledFabricVersion {
        id: version_id,
        minecraft_version: game_version.to_string(),
        loader_version: loader_version.to_string(),
        path: json_path,
    })
}

/// Check if Fabric is installed for a specific version combination.
///
/// # Arguments
/// * `game_dir` - The .minecraft directory path
/// * `game_version` - The Minecraft version
/// * `loader_version` - The Fabric loader version
///
/// # Returns
/// `true` if the version JSON exists, `false` otherwise.
pub fn is_fabric_installed(
    game_dir: &std::path::Path,
    game_version: &str,
    loader_version: &str,
) -> bool {
    let version_id = generate_version_id(game_version, loader_version);
    let json_path = game_dir
        .join("versions")
        .join(&version_id)
        .join(format!("{}.json", version_id));
    json_path.exists()
}

/// List all installed Fabric versions in the game directory.
///
/// # Arguments
/// * `game_dir` - The .minecraft directory path
///
/// # Returns
/// A list of installed Fabric version IDs.
pub async fn list_installed_fabric_versions(
    game_dir: &std::path::Path,
) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
    let versions_dir = game_dir.join("versions");
    let mut installed = Vec::new();

    if !versions_dir.exists() {
        return Ok(installed);
    }

    let mut entries = tokio::fs::read_dir(&versions_dir).await?;
    while let Some(entry) = entries.next_entry().await? {
        let name = entry.file_name().to_string_lossy().to_string();
        if name.starts_with("fabric-loader-") {
            // Verify the JSON file exists
            let json_path = entry.path().join(format!("{}.json", name));
            if json_path.exists() {
                installed.push(name);
            }
        }
    }

    Ok(installed)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_version_id() {
        assert_eq!(
            generate_version_id("1.20.4", "0.15.6"),
            "fabric-loader-0.15.6-1.20.4"
        );
    }
}