Technology Sharing

Getting Started with Rust and Writing a Minecraft Launcher #2 Building a Resource Model

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

First published onEnaium's personal blog


We need to declare several structures to store the game's resource information, and then we need tojsonThe file is parsed into these structures, so we need to addserderely.

serde = { version = "1.0", features = ["derive"] }
  • 1

Resourcesasset.rs

use serde::Deserialize;
use std::collections::HashMap;

#[derive(Deserialize)]
pub struct AssetIndex {
    pub id: String,
    pub sha1: String,
    pub size: u32,
    #[serde(alias = "totalSize")]
    pub total_size: u32,
    pub url: String,
}

#[derive(Deserialize)]
pub struct Index {
    pub objects: HashMap<String, Object>,
}

#[derive(Deserialize)]
pub struct Object {
    pub hash: String,
    pub size: u32,
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Gameversion.rs

use serde::Deserialize;

use crate::{asset::AssetIndex, library::Library};

pub type Libraries = Vec<Library>;

#[derive(Deserialize)]
pub struct Version {
    #[serde(alias = "assetIndex")]
    pub asset_index: AssetIndex,
    pub downloads: Download,
    pub id: String,
    pub libraries: Libraries,
    #[serde(alias = "mainClass")]
    pub main_class: String,
    #[serde(alias = "releaseTime")]
    pub release_time: String,
    pub time: String,
    #[serde(alias = "type")]
    pub type_: String,
}

#[derive(Deserialize)]
pub struct Download {
    pub client: Client,
}

#[derive(Deserialize)]
pub struct Client {
    pub sha1: String,
    pub size: u32,
    pub url: String,
}
  • 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

Game dependency librarylibrary.rs

use serde::Deserialize;

#[derive(Deserialize)]
pub struct Library {
    pub downloads: Download,
    pub name: String,
    pub rules: Option<Vec<Rule>>,
}

#[derive(Deserialize)]
pub struct Rule {
    pub action: String,
    pub os: Os,
}

#[derive(Deserialize)]
pub struct Os {
    pub name: String,
}

#[derive(Deserialize)]
pub struct Download {
    pub artifact: Artifact,
}

#[derive(Deserialize)]
pub struct Artifact {
    pub path: String,
    pub sha1: String,
    pub size: i32,
    pub url: String,
}
  • 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

There is also a version listversion_manifest.rs

use serde::Deserialize;

#[derive(Deserialize)]
pub struct VersionManifest {
    pub latest: Latest,
    pub versions: Vec<Version>,
}

#[derive(Deserialize)]
pub struct Latest {
    pub release: String,
    pub snapshot: String,
}

#[derive(Deserialize)]
pub struct Version {
    pub id: String,
    #[serde(alias = "type")]
    pub type_: String,
    pub url: String,
    pub time: String,
    #[serde(alias = "releaseTime")]
    pub release_time: String,
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Finally, we import these modules intolib.rsmiddle.

pub mod asset;
pub mod library;
pub mod version;
pub mod version_manifest;
  • 1
  • 2
  • 3
  • 4

project address