Skip to content

Commit

Permalink
Fixed sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
UwUDev committed Aug 20, 2024
1 parent 39f0589 commit 56cb8fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ 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(|_, mods: Vec<Table>| sort_mods(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
25 changes: 19 additions & 6 deletions src/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::{HashMap, HashSet};
use crate::core::{get_love_dir, json_to_lua, lua_to_json};
use crate::utils::validate_schema;
use crate::VERSION;
use mlua::prelude::{LuaError, LuaResult, LuaValue};
use mlua::{FromLua, IntoLua, Lua, Table};
use mlua::prelude::{LuaError, LuaResult, LuaTable, LuaValue};
use mlua::{FromLua, IntoLua, Lua, Table, Value};

Check failure on line 7 in src/mods.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

unused import: `Value`

Check failure on line 7 in src/mods.rs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

unused import: `Value`
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -352,7 +352,14 @@ enum VisitFlag {
Permanent,
}

pub fn sort_mods(mods: Vec<Table>) -> LuaResult<Vec<Table>> {
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?;
mods.push(value);
}

// Initialize graph as a Map<String, Set<String>>
// where the key is the mod id and the value is a set of mod ids to load before said mod
let mut graph: HashMap<String, HashSet<String>> = HashMap::new();
Expand Down Expand Up @@ -393,15 +400,15 @@ pub fn sort_mods(mods: Vec<Table>) -> LuaResult<Vec<Table>> {
}
sorted_mod_ids.push(id.clone());
visited.insert(id.clone(), VisitFlag::Permanent);
return true;
true
}
for id in graph.keys() {
if !visited.contains_key(id) {
visit(id.clone(), &graph, &mut visited, &mut sorted_mod_ids);
}
}

let mut sorted_mods: Vec<Table> = Vec::new();
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(
Expand All @@ -410,5 +417,11 @@ pub fn sort_mods(mods: Vec<Table>) -> LuaResult<Vec<Table>> {
mod_table.set("order", mod_count - i).unwrap();
sorted_mods.push(mod_table.clone());
}
return Ok(sorted_mods);

let sorted_mods_table = lua.create_table()?;
for mod_table in sorted_mods {
sorted_mods_table.set(mod_table.get::<_, String>("id").unwrap(), mod_table)?;
}

Ok(sorted_mods_table)
}

0 comments on commit 56cb8fd

Please sign in to comment.