Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rfuzzo committed Mar 11, 2024
1 parent fb854dd commit 1614f47
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 98 deletions.
70 changes: 31 additions & 39 deletions gui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,37 @@ impl eframe::App for TemplateApp {
});
ui.separator();

// accept button
ui.add_space(4_f32);

let button = egui::Button::new("Accept");
// disable button if new order is the same as old
let enabled = !data.old_order.eq(&data.new_order);
ui.add_enabled_ui(enabled, |ui| {
let r = ui.add_sized([ui.available_width(), 0_f32], button);

if r.clicked() {
// apply sorting
match update_new_load_order(data.game, &data.new_order) {
Ok(_) => {
info!("Update successful");
}
Err(e) => {
error!("Could not updae load order: {}", e);
}
}

// exit the app
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
}

r.on_disabled_hover_text("Mods are in correct order. No need to apply.");
});

ui.separator();

ui.add_space(4_f32);

// mod list
let order = match self.mod_list_view {
EModListView::NewOrder => &data.new_order,
Expand Down Expand Up @@ -249,31 +280,6 @@ impl eframe::App for TemplateApp {
});
}
});

// add spacing until bottom
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
// accept button
ui.add_space(4_f32);

let r =
ui.add_sized([ui.available_width(), 0_f32], egui::Button::new("Accept"));
if r.clicked() {
// apply sorting
match update_new_load_order(data.game, &data.new_order) {
Ok(_) => {
info!("Update successful");
}
Err(e) => {
error!("Could not updae load order: {}", e);
}
}

// exit the app
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
}

ui.separator();
});
});

// main panel
Expand Down Expand Up @@ -372,10 +378,6 @@ impl eframe::App for TemplateApp {
frame.paint(ui);
}
});
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
powered_by_egui_and_eframe(ui);
egui::warn_if_debug_build(ui);
});
});
}
}
Expand All @@ -388,13 +390,3 @@ fn get_color_for_rule(rule: &EWarningRule) -> Color32 {
EWarningRule::Patch(_) => Color32::BLUE,
}
}

fn powered_by_egui_and_eframe(ui: &mut egui::Ui) {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.hyperlink_to("plox", "https://github.com/rfuzzo/plox");
ui.label(" powered by ");
ui.hyperlink_to("egui", "https://github.com/emilk/egui");
ui.label(".");
});
}
11 changes: 11 additions & 0 deletions gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ fn init_parser(game: plox::ESupportedGame, tx: Sender<String>) -> Option<AppData
}

// sort
//let mut new_order = mods.clone();
// check order first
//match check_order(&mods, &parser.order_rules) {
// true => {
// // exit
// info!("Mods are in correct order, no sorting needed.");
// let _ = tx.send("Mods are in correct order, no sorting needed.".to_string());
// }
// false => {
let mut sorter = new_stable_sorter();
let _ = tx.send("Sorting mods".to_string());
let new_order = match sorter.topo_sort(game, &mods, &parser.order_rules) {
Expand All @@ -70,6 +79,8 @@ fn init_parser(game: plox::ESupportedGame, tx: Sender<String>) -> Option<AppData
return None;
}
};
//}
//}

let r = AppData {
game,
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ fn redate_mods(files: &[PathBuf]) -> Result<(), io::Error> {
let filename = mod_path.file_name().unwrap().to_str().unwrap();
if let Some(time) = fixed_file_times.get(&filename.to_lowercase()) {
let time = *time as i64;
current_time = time;
set_file_mtime(mod_path, filetime::FileTime::from_unix_time(time, 0))?;
} else {
// set the time to start time + 60
Expand All @@ -486,6 +487,29 @@ fn redate_mods(files: &[PathBuf]) -> Result<(), io::Error> {
Ok(())
}

/// Checks if the list of mods is in the correct order
pub fn check_order(result: &[String], order_rules: &[EOrderRule]) -> bool {
let order = get_ordering_from_order_rules(order_rules);
let pairs = order;
for (a, b) in pairs {
if let Some(results_for_a) = wild_contains(result, &a) {
if let Some(results_for_b) = wild_contains(result, &b) {
for i in &results_for_a {
for j in &results_for_b {
let pos_a = result.iter().position(|x| x == i).unwrap();
let pos_b = result.iter().position(|x| x == j).unwrap();
if pos_a > pos_b {
return false;
}
}
}
}
}
}

true
}

////////////////////////////////////////////////////////////////////////
/// HELPERS
////////////////////////////////////////////////////////////////////////
Expand Down
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ pub fn sort(
} else {
sorter::new_stable_sorter()
};

// check order first
// match check_order(&mods, &parser.order_rules) {
// true => {
// // exit
// info!("Mods are in correct order, no sorting needed.");
// return ExitCode::SUCCESS;
// }
// false => {}
// }

match sorter.topo_sort(game, &mods, &parser.order_rules) {
Ok(result) => {
if dry_run {
Expand Down
42 changes: 10 additions & 32 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod integration_tests {
use std::{fs::create_dir_all, io::Write};

use log::warn;
use plox::{parser::*, rules::EOrderRule, sorter::*, *};
use plox::{parser::*, sorter::*, *};
use rand::seq::SliceRandom;
use rand::thread_rng;

Expand Down Expand Up @@ -48,7 +48,7 @@ mod integration_tests {
{
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand All @@ -58,7 +58,7 @@ mod integration_tests {
match new_stable_sorter().topo_sort(ESupportedGame::Morrowind, &mods, &parser.order_rules) {
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand Down Expand Up @@ -200,7 +200,7 @@ mod integration_tests {
match new_stable_sorter().topo_sort(ESupportedGame::Morrowind, &mods, &parser.order_rules) {
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand All @@ -213,7 +213,7 @@ mod integration_tests {
{
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand All @@ -238,7 +238,7 @@ mod integration_tests {
match new_stable_sorter().topo_sort(ESupportedGame::Morrowind, &mods, &parser.order_rules) {
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand All @@ -251,7 +251,7 @@ mod integration_tests {
{
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand Down Expand Up @@ -279,7 +279,7 @@ mod integration_tests {
match new_stable_sorter().topo_sort(ESupportedGame::Morrowind, &mods, &parser.order_rules) {
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand All @@ -291,7 +291,7 @@ mod integration_tests {
) {
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand All @@ -305,7 +305,7 @@ mod integration_tests {
{
Ok(result) => {
assert!(
checkresult(&result, &parser.order_rules),
check_order(&result, &parser.order_rules),
"stable(true) order is wrong"
);
}
Expand Down Expand Up @@ -400,26 +400,4 @@ mod integration_tests {
]
)
}

fn checkresult(result: &[String], order_rules: &[EOrderRule]) -> bool {
let order = get_ordering_from_order_rules(order_rules);
let pairs = order;
for (a, b) in pairs {
if let Some(results_for_a) = wild_contains(result, &a) {
if let Some(results_for_b) = wild_contains(result, &b) {
for i in &results_for_a {
for j in &results_for_b {
let pos_a = result.iter().position(|x| x == i).unwrap();
let pos_b = result.iter().position(|x| x == j).unwrap();
if pos_a > pos_b {
return false;
}
}
}
}
}
}

true
}
}
32 changes: 5 additions & 27 deletions tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
mod unit_tests {

use plox::{
rules::{EOrderRule, Order},
rules::Order,
sorter::{self, Sorter},
wild_contains, *,
*,
};

fn init() {
Expand Down Expand Up @@ -75,45 +75,23 @@ mod unit_tests {

match sorter::new_unstable_sorter().topo_sort(ESupportedGame::Morrowind, &mods, &order) {
Ok(result) => {
assert!(checkresult(&result, &order), "stable(true) order is wrong");
assert!(check_order(&result, &order), "stable(true) order is wrong");
}
Err(e) => panic!("Error: {}", e),
}

match new_stable_full_sorter().topo_sort(ESupportedGame::Morrowind, &mods, &order) {
Ok(result) => {
assert!(checkresult(&result, &order), "stable(true) order is wrong");
assert!(check_order(&result, &order), "stable(true) order is wrong");
}
Err(e) => panic!("Error: {}", e),
}

match sorter::new_stable_sorter().topo_sort(ESupportedGame::Morrowind, &mods, &order) {
Ok(result) => {
assert!(checkresult(&result, &order), "stable(true) order is wrong");
assert!(check_order(&result, &order), "stable(true) order is wrong");
}
Err(e) => panic!("Error: {}", e),
}
}

fn checkresult(result: &[String], order_rules: &[EOrderRule]) -> bool {
let order = get_ordering_from_order_rules(order_rules);
let pairs = order;
for (a, b) in pairs {
if let Some(results_for_a) = wild_contains(result, &a) {
if let Some(results_for_b) = wild_contains(result, &b) {
for i in &results_for_a {
for j in &results_for_b {
let pos_a = result.iter().position(|x| x == i).unwrap();
let pos_b = result.iter().position(|x| x == j).unwrap();
if pos_a > pos_b {
return false;
}
}
}
}
}
}

true
}
}

0 comments on commit 1614f47

Please sign in to comment.