Skip to content

Commit

Permalink
Fixing macos path not found issue (#15)
Browse files Browse the repository at this point in the history
* Fixing macos path not found issue

Fixing using a feature flag for debug builds instead of runtime checks of exe name

* adjust compilation flags

* formatting
  • Loading branch information
sbordeyne authored Aug 25, 2024
1 parent 26fba54 commit 46aa7dd
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "1.0.24"
edition = "2021"

[lib]
crate-type = ["cdylib"]
crate-type = ["lib", "cdylib", "staticlib"]

[dependencies]
mlua = { version = "0.9.9", features = ["lua51", "macros", "serialize", "module"] }
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::core::{inject, is_mod_present, json_to_lua, lua_to_json, need_update, setup_injection, validate_schema};
#[cfg(not(target_os = "android"))]
use crate::core::restart;
use crate::core::{
inject, is_mod_present, json_to_lua, lua_to_json, need_update, setup_injection, validate_schema,
};
use mlua::prelude::*;
use mlua::Value;

Expand Down Expand Up @@ -66,7 +68,10 @@ fn balalib(lua: &Lua) -> LuaResult<LuaTable> {
)?;
exports.set("inject", lua.create_function(|lua, (file, function, code_to_find, code_to_insert): (String, String, String, String)| inject(lua, file, function, code_to_find, code_to_insert))?)?;
exports.set("version", VERSION)?;
exports.set("sort_mods", lua.create_function(|lua, mods: LuaTable| sort_mods(lua, mods))?)?;
exports.set(
"sort_mods",
lua.create_function(|lua, mods: LuaTable| sort_mods(lua, mods))?,
)?;
lua.load(format!("G.VERSION = G.VERSION .. '\\nBalalib {}'", VERSION).as_str())
.exec()?;
Ok(exports)
Expand Down
48 changes: 36 additions & 12 deletions src/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,30 @@ pub fn get_local_mods(lua: &Lua) -> LuaResult<Vec<LocalMod>> {
'>' => {
let balalib_version = balalib_version.split(">").nth(1).unwrap();
if balalib_version <= VERSION {
println!("Balalib version too low: {} for mod {}", balalib_version, manifest.id);
println!(
"Balalib version too low: {} for mod {}",
balalib_version, manifest.id
);
continue;
}
}
'<' => {
let balalib_version = balalib_version.split("<").nth(1).unwrap();
if balalib_version >= VERSION {
println!("Balalib version too high: {} for mod {}", balalib_version, manifest.id);
println!(
"Balalib version too high: {} for mod {}",
balalib_version, manifest.id
);
continue;
}
}
'=' => {
let balalib_version = balalib_version.split("=").nth(1).unwrap();
if balalib_version != VERSION {
println!("Balalib version does not match: {} for mod {}", balalib_version, manifest.id);
println!(
"Balalib version does not match: {} for mod {}",
balalib_version, manifest.id
);
continue;
}
}
Expand All @@ -208,7 +217,10 @@ pub fn get_local_mods(lua: &Lua) -> LuaResult<Vec<LocalMod>> {
match manifest.clone().min_balamod_version {
Some(min_balamod_version) => {
if balamod_version < min_balamod_version {
println!("Balalib version too low: {} for mod {}", min_balamod_version, manifest.id);
println!(
"Balalib version too low: {} for mod {}",
min_balamod_version, manifest.id
);
continue;
}
}
Expand All @@ -218,7 +230,10 @@ pub fn get_local_mods(lua: &Lua) -> LuaResult<Vec<LocalMod>> {
match manifest.clone().max_balamod_version {
Some(max_balamod_version) => {
if balamod_version > max_balamod_version {
println!("Balalib version too high: {} for mod {}", max_balamod_version, manifest.id);
println!(
"Balalib version too high: {} for mod {}",
max_balamod_version, manifest.id
);
continue;
}
}
Expand All @@ -229,7 +244,10 @@ pub fn get_local_mods(lua: &Lua) -> LuaResult<Vec<LocalMod>> {
let folder_name = folder_name.split("\\").last().unwrap();

if manifest.id != folder_name {
println!("Mod id in manifest.json does not match folder name: {} != {}", manifest.id, folder_name);
println!(
"Mod id in manifest.json does not match folder name: {} != {}",
manifest.id, folder_name
);
continue;
}

Expand Down Expand Up @@ -353,7 +371,6 @@ enum VisitFlag {
}

pub fn sort_mods<'a>(lua: &'a Lua, mods_table: LuaTable<'a>) -> LuaResult<LuaTable<'a>> {

let mut mods: Vec<LuaTable> = vec![];
for pair in mods_table.clone().pairs::<String, Table>() {
let (_, value) = pair?;
Expand Down Expand Up @@ -386,10 +403,16 @@ pub fn sort_mods<'a>(lua: &'a Lua, mods_table: LuaTable<'a>) -> LuaResult<LuaTab
visited: &mut HashMap<String, VisitFlag>,
sorted_mod_ids: &mut Vec<String>,
) -> bool {
if visited.get(&id).is_some_and(|flag| *flag == VisitFlag::Permanent) {
if visited
.get(&id)
.is_some_and(|flag| *flag == VisitFlag::Permanent)
{
return true;
}
if visited.get(&id).is_some_and(|flag| *flag == VisitFlag::Temporary) {
if visited
.get(&id)
.is_some_and(|flag| *flag == VisitFlag::Temporary)
{
return false;
}
visited.insert(id.clone(), VisitFlag::Temporary);
Expand All @@ -411,9 +434,10 @@ pub fn sort_mods<'a>(lua: &'a Lua, mods_table: LuaTable<'a>) -> LuaResult<LuaTab
let mut sorted_mods: Vec<LuaTable> = Vec::new();
let mod_count = mods.len();
for (i, id) in sorted_mod_ids.iter().enumerate() {
let mod_table = mods.iter().find(
|mod_table| mod_table.get::<_, String>("id").unwrap() == id.to_owned(),
).unwrap();
let mod_table = mods
.iter()
.find(|mod_table| mod_table.get::<_, String>("id").unwrap() == id.to_owned())
.unwrap();
mod_table.set("order", mod_count - i).unwrap();
sorted_mods.push(mod_table.clone());
}
Expand Down
3 changes: 1 addition & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(test)]
mod tests {
use crate::updater::get_latest_cli_version;
use crate::utils::minify_lua;
use std::fs;
use crate::updater::get_latest_cli_version;

#[test]
fn test_update() {
Expand Down Expand Up @@ -33,7 +33,6 @@ mod tests {
println!("Latest CLI version: {}", get_latest_cli_version());
}


// TODO: Add test for sorted_mods
// {"id": "test", "load_before": ["foo"], "load_after": ["baz", "qux"]}
// {"id": "foo", "load_before": [], "load_after": ["baz", "qux"]}
Expand Down
9 changes: 6 additions & 3 deletions src/updater.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(any(target_os = "macos", target_os = "linux"))]
use crate::core::restart;
use mlua::prelude::LuaResult;
use crate::VERSION;
use mlua::prelude::LuaResult;

pub fn need_update(balamod_version: String) -> LuaResult<bool> {
let client = reqwest::blocking::Client::builder()
Expand Down Expand Up @@ -106,7 +106,10 @@ pub fn self_update(cli_ver: &str) -> LuaResult<()> {
filename.push_str("linux");
}

let url = format!("https://github.com/balamod/balamod/releases/download/{}/{}", cli_ver, filename);
let url = format!(
"https://github.com/balamod/balamod/releases/download/{}/{}",
cli_ver, filename
);
let client = reqwest::blocking::Client::builder()
.user_agent("balalib")
.build()
Expand Down Expand Up @@ -181,4 +184,4 @@ pub fn get_latest_cli_version() -> String {
},
Err(_) => "0.0.0".to_string(),
}
}
}
90 changes: 50 additions & 40 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use regex::Regex;
use std::collections::HashMap;
use std::io::Read;
#[cfg(debug_assertions)]
use std::path::Path;
use std::{env, fs};

Expand Down Expand Up @@ -120,6 +121,7 @@ pub fn extract_functions(minified_code: String) -> HashMap<String, String> {
functions
}

#[cfg(debug_assertions)]
fn traverse_dir(dir: &Path, prefix: &str, files: &mut Vec<String>) {
for entry in fs::read_dir(dir).unwrap() {
let entry = entry.unwrap();
Expand All @@ -138,50 +140,58 @@ fn traverse_dir(dir: &Path, prefix: &str, files: &mut Vec<String>) {
}
}

#[cfg(debug_assertions)]
pub fn get_lua_files() -> HashMap<String, String> {
let exe_name = env::current_exe()
.unwrap()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();

if exe_name == "love" || exe_name == "love.exe" {
// files are in raw folder (1 arg)
let path_arg = env::args().nth(1).unwrap();
let mut map = HashMap::new();
// only files in the directory
let mut entries = vec![];
traverse_dir(Path::new(&path_arg), "", &mut entries);

for entry in entries {
if entry.ends_with(".lua") {
let mut file = fs::File::open(format!("{}/{}", path_arg, entry)).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
map.insert(entry.replace(".lua", ""), content);
}
}

map
} else {
let exe_path = env::current_exe().unwrap();
let file = fs::File::open(exe_path).unwrap();
let mut archive = zip::ZipArchive::new(file).unwrap();
let mut map = HashMap::new();
for i in 0..archive.len() {
let mut file = archive.by_index(i).unwrap();
let name = file.name().to_string();
if !name.ends_with(".lua") {
continue;
}
// files are in raw folder (1 arg)
let path_arg = env::args().nth(1).unwrap();
let mut map = HashMap::new();
// only files in the directory
let mut entries = vec![];
traverse_dir(Path::new(&path_arg), "", &mut entries);

for entry in entries {
if entry.ends_with(".lua") {
let mut file = fs::File::open(format!("{}/{}", path_arg, entry)).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
map.insert(name, content);
map.insert(entry.replace(".lua", ""), content);
}
map
}

map
}

#[cfg(not(debug_assertions))]
pub fn get_lua_files() -> HashMap<String, String> {
let exe_path = env::current_exe().unwrap();
let mut map = HashMap::new();
match fs::File::open(exe_path) {
Ok(file) => {
match zip::ZipArchive::new(file) {
Ok(mut archive) => {
for i in 0..archive.len() {
match archive.by_index(i) {
Ok(mut file) => {
let name = file.name().to_string();
if !name.ends_with(".lua") {
continue;
}
let mut content = String::new();
match file.read_to_string(&mut content) {
Ok(_) => map.insert(name, content),
Err(_) => continue,
}
}
Err(_) => continue,
};
}
}
Err(_) => return HashMap::new(),
};
}
Err(_) => return HashMap::new(),
};
return map;
}

pub fn validate_schema(schema: String, data: String) -> String {
Expand Down Expand Up @@ -209,4 +219,4 @@ pub fn validate_schema(schema: String, data: String) -> String {
}
errors.join("\n")
}
}
}

0 comments on commit 46aa7dd

Please sign in to comment.