Skip to content

Commit

Permalink
Add TimingMethod to legacy XML (#46)
Browse files Browse the repository at this point in the history
* legacy_xml asr_settings_from_xml_nodes

* legacy_xml asr_list_from_splits

* legacy_xml: remove splits_from_xml_nodes

* Parse TimingMethod in legacy_xml

* Add TimingMethod to legacy XML examples
  • Loading branch information
AlexKnauth authored Feb 4, 2024
1 parent 81741c4 commit fc423c5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
1 change: 1 addition & 0 deletions splits-direct.lss
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,6 @@
<Split>MaskFragment3</Split>
<Split>Mask1</Split>
</Splits>
<TimingMethod>LoadRemovedTime</TimingMethod>
</AutoSplitterSettings>
</Run>
1 change: 1 addition & 0 deletions src/AutoSplitterSettings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
<Split>MaskFragment3</Split>
<Split>Mask1</Split>
</Splits>
<TimingMethod>LoadRemovedTime</TimingMethod>
19 changes: 3 additions & 16 deletions src/auto_splitter_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use asr::future::retry;
use std::path::Path;
use xmltree::{Element, XMLNode};

use ugly_widget::radio_button::options_str;

use crate::{file, legacy_xml, splits::Split};
use crate::{file, legacy_xml};

Check failure on line 8 in src/auto_splitter_settings.rs

View workflow job for this annotation

GitHub Actions / Check formatting

Diff in /home/runner/work/hollowknight-autosplit-wasm/hollowknight-autosplit-wasm/src/auto_splitter_settings.rs
pub async fn wait_asr_settings_init() -> asr::settings::Map {
let settings1 = asr::settings::Map::load();
Expand Down Expand Up @@ -42,19 +40,8 @@ fn asr_settings_from_xml_string(xml_string: &str) -> Option<asr::settings::Map>
}

fn asr_settings_from_xml_nodes(xml_nodes: Vec<XMLNode>) -> Option<asr::settings::Map> {
let splits = legacy_xml::splits_from_xml_nodes(xml_nodes)?;
Some(asr_settings_from_splits(&splits))
}

fn asr_settings_from_splits(splits: &[Split]) -> asr::settings::Map {
// new empty map, which will only include the new splits
let settings_map = asr::settings::Map::new();
let l = asr::settings::List::new();
for split in splits.iter() {
l.push(options_str(split));
}
settings_map.insert("splits", l);
settings_map
// TODO: deal with either Legacy XML or ASR XML
legacy_xml::asr_settings_from_xml_nodes(xml_nodes)
}

fn file_find_auto_splitter_settings<P: AsRef<Path>>(path: P) -> Option<Vec<XMLNode>> {
Expand Down
33 changes: 27 additions & 6 deletions src/legacy_xml.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@

use ugly_widget::radio_button::options_str;
use xmltree::XMLNode;

use crate::splits::Split;
use crate::{settings_gui::TimingMethod, splits::Split};

pub fn asr_settings_from_xml_nodes(xml_nodes: Vec<XMLNode>) -> Option<asr::settings::Map> {
let xml_settings = XMLSettings::from_xml_nodes(xml_nodes, &[("Splits", "Split")]);
let splits = splits_from_settings(&xml_settings)?;
// new empty map, which will only include the new splits
let settings_map = asr::settings::Map::new();
settings_map.insert("splits", asr_list_from_splits(&splits));
if let Some(timing_method) = xml_settings.dict_get("TimingMethod") {
let tm = timing_method_from_settings_str(timing_method).unwrap_or_default();
settings_map.insert("timing_method", options_str(&tm));
}
Some(settings_map)
}

fn asr_list_from_splits(splits: &[Split]) -> asr::settings::List {
let l = asr::settings::List::new();
for split in splits.iter() {
l.push(options_str(split));
}
l
}

#[derive(Clone, Debug)]
struct XMLSettings {
Expand Down Expand Up @@ -79,11 +101,6 @@ impl XMLSettings {
}
}

pub fn splits_from_xml_nodes(xml_nodes: Vec<XMLNode>) -> Option<Vec<Split>> {
let xml_settings = XMLSettings::from_xml_nodes(xml_nodes, &[("Splits", "Split")]);
splits_from_settings(&xml_settings)
}

fn splits_from_settings(s: &XMLSettings) -> Option<Vec<Split>> {
let maybe_ordered = s.dict_get("Ordered");
let maybe_start = s.dict_get("AutosplitStartRuns");
Expand Down Expand Up @@ -120,3 +137,7 @@ fn split_from_settings_split(s: XMLSettings) -> Option<Split> {
fn split_from_settings_str(s: XMLSettings) -> Option<Split> {
serde_json::value::from_value(serde_json::Value::String(s.as_string()?)).ok()
}

fn timing_method_from_settings_str(s: XMLSettings) -> Option<TimingMethod> {
serde_json::value::from_value(serde_json::Value::String(s.as_string()?)).ok()
}
8 changes: 5 additions & 3 deletions src/settings_gui.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use asr::{settings::gui::{FileSelect, Gui, Title, Widget}, watcher::Pair};

use serde::{Deserialize, Serialize};
use ugly_widget::{
ugly_list::{UglyList, UglyListArgs},
store::{StoreWidget, StoreGui},
args::SetHeadingLevel,
radio_button::RadioButtonOptions,
store::{StoreWidget, StoreGui},
ugly_list::{UglyList, UglyListArgs}
};

use crate::{
Expand Down Expand Up @@ -61,7 +63,7 @@ impl SettingsGui {
}
}

#[derive(Clone, Copy, Debug, Default, Eq, Gui, PartialEq)]
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Gui, Ord, PartialEq, PartialOrd, RadioButtonOptions, Serialize)]
pub enum TimingMethod {
/// Load Removed Time
#[default]
Expand Down

0 comments on commit fc423c5

Please sign in to comment.