feat: Make mod resource scripts optional

With splitting DMF and DML, there is now more than one case where this
is needed, so it may well be made proper now.
The template still defines them, and, as with VT2 most creators will
probably stick with it, but they do have the option to make a non-DMF
mod now.
This commit is contained in:
Lucas Schwiderski 2023-02-22 09:23:40 +01:00
parent 58a3df2d40
commit 701516aa7c
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
4 changed files with 40 additions and 19 deletions

View file

@ -156,15 +156,31 @@ fn build_mod_data_lua(state: Arc<State>) -> String {
lua.push_str(mod_info.get_id()); lua.push_str(mod_info.get_id());
lua.push_str("\",\n run = function()\n"); lua.push_str("\",\n run = function()\n");
lua.push_str(" return new_mod(\"");
lua.push_str(mod_info.get_name()); let resources = mod_info.get_resources();
if resources.get_data().is_some() || resources.get_localization().is_some() {
lua.push_str(" new_mod(\"");
lua.push_str(mod_info.get_id());
lua.push_str("\", {\n init = \""); lua.push_str("\", {\n init = \"");
lua.push_str(mod_info.get_resources().get_init()); lua.push_str(resources.get_init());
if let Some(data) = resources.get_data() {
lua.push_str("\",\n data = \""); lua.push_str("\",\n data = \"");
lua.push_str(mod_info.get_resources().get_data()); lua.push_str(data);
}
if let Some(localization) = resources.get_localization() {
lua.push_str("\",\n localization = \""); lua.push_str("\",\n localization = \"");
lua.push_str(mod_info.get_resources().get_localization()); lua.push_str(localization);
}
lua.push_str("\",\n })\n"); lua.push_str("\",\n })\n");
} else {
lua.push_str(" return dofile(\"");
lua.push_str(resources.get_init());
lua.push_str("\")");
}
lua.push_str(" end,\n packages = [\n"); lua.push_str(" end,\n packages = [\n");
for pkg_info in mod_info.get_packages() { for pkg_info in mod_info.get_packages() {

View file

@ -62,8 +62,8 @@ impl PackageInfo {
#[derive(Clone, Data, Debug)] #[derive(Clone, Data, Debug)]
pub(crate) struct ModResourceInfo { pub(crate) struct ModResourceInfo {
init: String, init: String,
data: String, data: Option<String>,
localization: String, localization: Option<String>,
} }
impl ModResourceInfo { impl ModResourceInfo {
@ -71,12 +71,12 @@ impl ModResourceInfo {
&self.init &self.init
} }
pub(crate) fn get_data(&self) -> &String { pub(crate) fn get_data(&self) -> Option<&String> {
&self.data self.data.as_ref()
} }
pub(crate) fn get_localization(&self) -> &String { pub(crate) fn get_localization(&self) -> Option<&String> {
&self.localization self.localization.as_ref()
} }
} }

View file

@ -72,9 +72,12 @@ async fn find_project_config(dir: Option<PathBuf>) -> Result<ModConfig> {
}; };
let mut buf = String::new(); let mut buf = String::new();
file.read_to_string(&mut buf).await?; file.read_to_string(&mut buf)
.await
.wrap_err("invalid UTF-8")?;
let mut cfg: ModConfig = serde_sjson::from_str(&buf)?; let mut cfg: ModConfig =
serde_sjson::from_str(&buf).wrap_err("failed to deserialize mod config")?;
cfg.dir = path; cfg.dir = path;
Ok(cfg) Ok(cfg)
} }

View file

@ -13,8 +13,10 @@ pub use context::Context;
#[derive(Clone, Debug, Default, serde::Deserialize)] #[derive(Clone, Debug, Default, serde::Deserialize)]
pub struct ModConfigResources { pub struct ModConfigResources {
pub init: String, pub init: String,
pub data: String, #[serde(default)]
pub localization: String, pub data: Option<String>,
#[serde(default)]
pub localization: Option<String>,
} }
#[derive(Clone, Debug, Default, serde::Deserialize)] #[derive(Clone, Debug, Default, serde::Deserialize)]