From bc4d2fcd8adfce3964eb8f7aa743806df178e334 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Wed, 22 Feb 2023 16:03:14 +0100 Subject: [PATCH] feat(dtmt): Add unique mod ID --- crates/dtmm/src/state.rs | 6 +++++ crates/dtmt/src/cmd/build.rs | 8 +++--- crates/dtmt/src/cmd/new.rs | 49 ++++++++++++++++++------------------ lib/sdk/src/lib.rs | 1 + 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/crates/dtmm/src/state.rs b/crates/dtmm/src/state.rs index 3241585..8775aff 100644 --- a/crates/dtmm/src/state.rs +++ b/crates/dtmm/src/state.rs @@ -82,6 +82,7 @@ impl ModResourceInfo { #[derive(Clone, Data, Debug, Lens)] pub(crate) struct ModInfo { + id: String, name: String, description: Arc, enabled: bool, @@ -94,6 +95,7 @@ pub(crate) struct ModInfo { impl ModInfo { pub fn new(cfg: ModConfig, packages: Vector) -> Self { Self { + id: cfg.id, name: cfg.name, description: Arc::new(cfg.description), enabled: false, @@ -114,6 +116,10 @@ impl ModInfo { &self.name } + pub(crate) fn get_id(&self) -> &String { + &self.id + } + pub(crate) fn get_enabled(&self) -> bool { self.enabled } diff --git a/crates/dtmt/src/cmd/build.rs b/crates/dtmt/src/cmd/build.rs index 0c34a11..a5a471f 100644 --- a/crates/dtmt/src/cmd/build.rs +++ b/crates/dtmt/src/cmd/build.rs @@ -24,7 +24,7 @@ pub(crate) fn command_definition() -> Command { .value_parser(value_parser!(PathBuf)) .help( "The path to the project to build. \ - If omitted, dtmt will search from the current working directory upward.", + If omitted, dtmt will search from the current working directory upward.", ), ) .arg(Arg::new("oodle").long("oodle").help( @@ -172,7 +172,7 @@ pub(crate) async fn run(_ctx: sdk::Context, matches: &ArgMatches) -> Result<()> }; let dest = { - let mut path = PathBuf::from(&cfg.name); + let mut path = PathBuf::from(&cfg.id); path.set_extension("zip"); Arc::new(path) }; @@ -214,9 +214,9 @@ pub(crate) async fn run(_ctx: sdk::Context, matches: &ArgMatches) -> Result<()> { let dest = dest.clone(); - let name = cfg.name.clone(); + let id = cfg.id.clone(); tokio::task::spawn_blocking(move || { - let mut archive = Archive::new(name); + let mut archive = Archive::new(id); archive.add_config(config_file); diff --git a/crates/dtmt/src/cmd/new.rs b/crates/dtmt/src/cmd/new.rs index e757f49..dd42172 100644 --- a/crates/dtmt/src/cmd/new.rs +++ b/crates/dtmt/src/cmd/new.rs @@ -11,18 +11,19 @@ use tokio::fs::{self, DirBuilder}; const TEMPLATES: [(&str, &str); 5] = [ ( "dtmt.cfg", - r#"name = "{{name}}" + r#"id = "{{id}}" +name = "{{name}}" description = "An elaborate description of my cool game mod!" version = "0.1.0" resources = { - script = "scripts/mods/{{name}}/init" - data = "scripts/mods/{{name}}/data" - localization = "scripts/mods/{{name}}/locationzation" + script = "scripts/mods/{{id}}/init" + data = "scripts/mods/{{id}}/data" + localization = "scripts/mods/{{id}}/localization" } packages = [ - "packages/{{name}}" + "packages/{{id}}" ] depends = [ @@ -31,32 +32,32 @@ depends = [ "#, ), ( - "packages/{{name}}.package", + "packages/{{id}}.package", r#"lua = [ - "scripts/mods/{{name}}/*" + "scripts/mods/{{id}}/*" ] "#, ), ( - "scripts/mods/{{name}}/init.lua", - r#"local mod = get_mod("{{name}}") + "scripts/mods/{{id}}/init.lua", + r#"local mod = get_mod("{{id}}") -- Your mod code goes here. -- https://vmf-docs.verminti.de "#, ), ( - "scripts/mods/{{name}}/data.lua", - r#"local mod = get_mod("{{name}}") + "scripts/mods/{{id}}/data.lua", + r#"local mod = get_mod("{{id}}") return { - name = "{{title}}", + name = "{{name}}", description = mod:localize("mod_description"), is_togglable = true, }"#, ), ( - "scripts/mods/{{name}}/localization.lua", + "scripts/mods/{{id}}/localization.lua", r#"return { mod_description = { en = "An elaborate description of my cool game mod!", @@ -69,8 +70,8 @@ pub(crate) fn command_definition() -> Command { Command::new("new") .about("Create a new project") .arg( - Arg::new("title") - .long("title") + Arg::new("name") + .long("name") .help("The display name of the new mod."), ) .arg(Arg::new("root").help( @@ -98,14 +99,14 @@ pub(crate) async fn run(_ctx: sdk::Context, matches: &ArgMatches) -> Result<()> } }; - let title = if let Some(title) = matches.get_one::("title") { - title.clone() + let name = if let Some(name) = matches.get_one::("name") { + name.clone() } else { - promptly::prompt("The mod display name")? + promptly::prompt("The unique mod ID")? }; - let name = { - let default = title + let id = { + let default = name .chars() .map(|c| { if c.is_ascii_alphanumeric() { @@ -115,14 +116,14 @@ pub(crate) async fn run(_ctx: sdk::Context, matches: &ArgMatches) -> Result<()> } }) .collect::(); - promptly::prompt_default("The mod identifier name", default)? + promptly::prompt_default("The unique mod ID", default)? }; - tracing::debug!(root = %root.display(), title, name); + tracing::debug!(root = %root.display(), name, id); let mut data = HashMap::new(); data.insert("name", name.as_str()); - data.insert("title", title.as_str()); + data.insert("id", id.as_str()); let templates = TEMPLATES .iter() @@ -158,7 +159,7 @@ pub(crate) async fn run(_ctx: sdk::Context, matches: &ArgMatches) -> Result<()> tracing::info!( "Created {} files for mod '{}' in '{}'.", TEMPLATES.len(), - title, + name, root.display() ); diff --git a/lib/sdk/src/lib.rs b/lib/sdk/src/lib.rs index 1ce68d6..003e400 100644 --- a/lib/sdk/src/lib.rs +++ b/lib/sdk/src/lib.rs @@ -21,6 +21,7 @@ pub struct ModConfigResources { pub struct ModConfig { #[serde(skip)] pub dir: std::path::PathBuf, + pub id: String, pub name: String, pub description: String, pub version: String,