From 56cb8fdd6fb653f3c075d7a96c5bc9678a084ab6 Mon Sep 17 00:00:00 2001 From: UwU Date: Tue, 20 Aug 2024 02:15:46 +0200 Subject: [PATCH] Fixed sorting --- src/lib.rs | 2 +- src/mods.rs | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8cc2a98..48ab2d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ fn balalib(lua: &Lua) -> LuaResult { )?; 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| 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) diff --git a/src/mods.rs b/src/mods.rs index a861955..c90fdab 100644 --- a/src/mods.rs +++ b/src/mods.rs @@ -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}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone)] @@ -352,7 +352,14 @@ enum VisitFlag { Permanent, } -pub fn sort_mods(mods: Vec
) -> LuaResult> { +pub fn sort_mods<'a>(lua: &'a Lua, mods_table: LuaTable<'a>) -> LuaResult> { + + let mut mods: Vec = vec![]; + for pair in mods_table.clone().pairs::() { + let (_, value) = pair?; + mods.push(value); + } + // Initialize graph as a Map> // 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> = HashMap::new(); @@ -393,7 +400,7 @@ pub fn sort_mods(mods: Vec
) -> LuaResult> { } 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) { @@ -401,7 +408,7 @@ pub fn sort_mods(mods: Vec
) -> LuaResult> { } } - let mut sorted_mods: Vec
= Vec::new(); + let mut sorted_mods: Vec = Vec::new(); let mod_count = mods.len(); for (i, id) in sorted_mod_ids.iter().enumerate() { let mod_table = mods.iter().find( @@ -410,5 +417,11 @@ pub fn sort_mods(mods: Vec
) -> LuaResult> { 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) }