Perform various optimizations #173

Merged
lucas merged 19 commits from feat/optimization into master 2024-07-10 19:45:52 +02:00
4 changed files with 18 additions and 12 deletions
Showing only changes of commit 0f14834e2d - Show all commits

1
Cargo.lock generated
View file

@ -910,7 +910,6 @@ dependencies = [
"sdk", "sdk",
"serde", "serde",
"serde_sjson", "serde_sjson",
"string_template",
"strip-ansi-escapes", "strip-ansi-escapes",
"time", "time",
"tokio", "tokio",

View file

@ -29,7 +29,6 @@ path-slash = "0.2.1"
sdk = { path = "../../lib/sdk", version = "*" } sdk = { path = "../../lib/sdk", version = "*" }
serde = { version = "1.0.152", features = ["derive", "rc"] } serde = { version = "1.0.152", features = ["derive", "rc"] }
serde_sjson = { path = "../../lib/serde_sjson", version = "*" } serde_sjson = { path = "../../lib/serde_sjson", version = "*" }
string_template = "0.2.1"
strip-ansi-escapes = "0.2.0" strip-ansi-escapes = "0.2.0"
time = { version = "0.3.20", features = ["serde", "serde-well-known", "local-offset"] } time = { version = "0.3.20", features = ["serde", "serde-well-known", "local-offset"] }
tokio = { version = "1.23.0", features = ["rt", "fs", "tracing", "sync"] } tokio = { version = "1.23.0", features = ["rt", "fs", "tracing", "sync"] }

View file

@ -17,7 +17,7 @@ local require_store = {}
-- This token is treated as a string template and filled by DTMM during deployment. -- This token is treated as a string template and filled by DTMM during deployment.
-- This allows hiding unsafe I/O functions behind a setting. -- This allows hiding unsafe I/O functions behind a setting.
-- It's also a valid table definition, thereby degrading gracefully when not replaced. -- When not replaced, it's also a valid table definition, thereby degrading gracefully.
local is_io_enabled = {{ is_io_enabled }} -- luacheck: ignore 113 local is_io_enabled = {{ is_io_enabled }} -- luacheck: ignore 113
local lua_libs = { local lua_libs = {
debug = debug, debug = debug,
@ -207,3 +207,5 @@ function init()
Main:init() Main:init()
end end
-- vim: ft=lua

View file

@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::io::{Cursor, ErrorKind}; use std::io::{Cursor, ErrorKind};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
@ -16,7 +15,6 @@ use sdk::{
Bundle, BundleDatabase, BundleFile, BundleFileType, BundleFileVariant, FromBinary, ToBinary, Bundle, BundleDatabase, BundleFile, BundleFileType, BundleFileVariant, FromBinary, ToBinary,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use string_template::Template;
use time::OffsetDateTime; use time::OffsetDateTime;
use tokio::fs::{self, DirEntry}; use tokio::fs::{self, DirEntry};
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
@ -572,12 +570,17 @@ async fn patch_boot_bundle(state: Arc<ActionState>) -> Result<Vec<Bundle>> {
let span = tracing::debug_span!("Importing mod main script"); let span = tracing::debug_span!("Importing mod main script");
let _enter = span.enter(); let _enter = span.enter();
let is_io_enabled = format!("{}", state.is_io_enabled); let mut env = Environment::new();
let mut data = HashMap::new(); env.add_template("mod_main.lua", include_str!("../../assets/mod_main.lua.j2"))
data.insert("is_io_enabled", is_io_enabled.as_str()); .wrap_err("Failed to compile template for `mod_main.lua`")?;
let tmpl = env
.get_template("mod_main.lua")
.wrap_err("Failed to get template `mod_main.lua`")?;
let lua = tmpl
.render(minijinja::context!(is_io_enabled => if state.is_io_enabled { "true" } else {"false"}))
.wrap_err("Failed to render template `mod_main.lua`")?;
let tmpl = include_str!("../../assets/mod_main.lua");
let lua = Template::new(tmpl).render(&data);
tracing::trace!("Main script rendered:\n===========\n{}\n=============", lua); tracing::trace!("Main script rendered:\n===========\n{}\n=============", lua);
let file = let file =
lua::compile(MOD_BOOT_SCRIPT, lua).wrap_err("Failed to compile mod main Lua file")?; lua::compile(MOD_BOOT_SCRIPT, lua).wrap_err("Failed to compile mod main Lua file")?;
@ -707,7 +710,7 @@ pub(crate) async fn deploy_mods(state: ActionState) -> Result<()> {
}, },
async { async {
let path = state.game_dir.join(DEPLOYMENT_DATA_PATH); let path = state.game_dir.join(DEPLOYMENT_DATA_PATH);
match read_sjson_file::<_, DeploymentData>(path).await { match read_sjson_file::<_, DeploymentData>(&path).await {
Ok(data) => Ok(Some(data)), Ok(data) => Ok(Some(data)),
Err(err) => { Err(err) => {
if let Some(err) = err.downcast_ref::<std::io::Error>() if let Some(err) = err.downcast_ref::<std::io::Error>()
@ -715,7 +718,10 @@ pub(crate) async fn deploy_mods(state: ActionState) -> Result<()> {
{ {
Ok(None) Ok(None)
} else { } else {
Err(err).wrap_err("Failed to read deployment data") Err(err).wrap_err(format!(
"Failed to read deployment data from: {}",
path.display()
))
} }
} }
} }