Skip to content

Commit

Permalink
Merge pull request #19 from SpontanCombust/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust authored Sep 25, 2023
2 parents 0b86bd9 + d6d91c1 commit a23d82a
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 50 deletions.
2 changes: 1 addition & 1 deletion doc/cli_specification.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```
TW3 Settings Framework Parser 0.6.0
TW3 Settings Framework Parser 0.6.1
Parses a mod menu XML file and outputs witcher script code representing settings of this menu
USAGE:
Expand Down
4 changes: 2 additions & 2 deletions doc/mod-menu.ws
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated using Mod Settings Framework v0.6.0 by SpontanCombust & Aeltoth
// Code generated using Mod Settings Framework v0.6.1 by SpontanCombust & Aeltoth

class MyModSettings extends ISettingsMaster
{
Expand Down Expand Up @@ -76,7 +76,7 @@ class MyModSettings_tab2 extends ISettingsGroup

protected /* override */ function Parser_ValidateSettings() : void
{
anotherSlider = ClampF(anotherSlider, -100, 100);
anotherSlider = ClampF(anotherSlider, -1.5, 1.5);
}

protected /* override */ function Parser_ReadSettings(config: CInGameConfigWrapper) : void
Expand Down
2 changes: 1 addition & 1 deletion doc/mod-menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

<Group id="MODtab2subtab1" msfVariable="tab2" displayName="Mods.my_mod.my_mod_tab2.subtap1">
<VisibleVars>
<Var id="anotherSlider" displayName="mod_another_slider" displayType="SLIDER;-100;100;1000"/>
<Var id="anotherSlider" displayName="mod_another_slider" displayType="SLIDER;-1.5;1.5;30"/>
</VisibleVars>
</Group>
<Group id="MODtab2subtab2" msfVariable="tab3" displayName="Mods.my_mod.my_mod_tab2.subtap2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CSettingsMasterRegistry
{
public const var FRAMEWORK_VERSION : name;
default FRAMEWORK_VERSION = '0.6.0';
default FRAMEWORK_VERSION = '0.6.1';

private var m_settingsEntries : array<ISettingsMaster>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated using Mod Settings Framework v0.6.0 by SpontanCombust & Aeltoth
// Code generated using Mod Settings Framework v0.6.1 by SpontanCombust & Aeltoth

class ModDifficultySettingsBase extends ISettingsMaster
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated using Mod Settings Framework v0.6.0 by SpontanCombust & Aeltoth
// Code generated using Mod Settings Framework v0.6.1 by SpontanCombust & Aeltoth

class MonsterOfTheWeekSettings extends ISettingsMaster
{
Expand Down
2 changes: 1 addition & 1 deletion settings-parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tw3-mod-settings-framework-parser"
version = "0.6.0"
version = "0.6.1"
edition = "2021"

[[bin]]
Expand Down
56 changes: 28 additions & 28 deletions settings-parser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ fn main() -> Result<(), String>{
return Err(format!("Error opening menu xml file: {}", e));
}
};

let ws_path = match &cli.output_ws_file_path {
Some(path) => PathBuf::from(&path),
None => input_file_path.with_extension("ws")
};

let ws_file = OpenOptions::new()
.write(true)
.create(true)
.open(&ws_path);

let mut ws_file = match ws_file {
Ok(f) => f,
Err(e) => {
return Err(format!("Error creating witcher script output file: {}", e));
}
};



let mut xml_text = String::new();
Expand All @@ -69,29 +51,47 @@ fn main() -> Result<(), String>{
}
};

let mut buffer = WitcherScript::new();
match UserConfig::try_from(&doc) {
Ok(user_config) => {
let settings_master = SettingsMaster::try_from(user_config, &cli)?;

let mut buffer = WitcherScript::new();

buffer.push_line(&format!("// Code generated using Mod Settings Framework v{} by SpontanCombust & Aeltoth", option_env!("CARGO_PKG_VERSION").unwrap()))
.new_line();

settings_master.ws_type_definition(&mut buffer);

// clear file content if there is any
ws_file.set_len(0).unwrap();

if let Err(e) = ws_file.write_all(buffer.text.as_bytes()) {
return Err(format!("Error writing witcher script output file: {}", e));
}
settings_master.ws_type_definition(&mut buffer);
},
Err(err) => {
return Err(format!("Error parsing menu xml file: {}", err));
}
}


let ws_path = match &cli.output_ws_file_path {
Some(path) => PathBuf::from(&path),
None => input_file_path.with_extension("ws")
};

let ws_file = OpenOptions::new()
.write(true)
.create(true)
.open(&ws_path);

let mut ws_file = match ws_file {
Ok(f) => f,
Err(e) => {
return Err(format!("Error creating witcher script output file: {}", e));
}
};

// clear file content if there is any
ws_file.set_len(0).unwrap();

if let Err(e) = ws_file.write_all(buffer.text.as_bytes()) {
return Err(format!("Error writing witcher script output file: {}", e));
}


println!("Successfully parsed {} into {}", cli.xml_file_path, ws_path.to_str().unwrap_or(""));
return Ok(())
}
8 changes: 4 additions & 4 deletions settings-parser/src/settings_var_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ impl SettingsVarType {
DisplayType::Slider { min, max, div } => {
if is_integral_range(*min, *max, *div) {
Ok(Some(SettingsVarType::Int {
min: *min,
max: *max
min: *min as i32,
max: *max as i32
}))
} else {
Ok(Some(SettingsVarType::Float {
min: *min as f32,
max: *max as f32
min: *min,
max: *max
}))
}
},
Expand Down
8 changes: 6 additions & 2 deletions settings-parser/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ pub(crate) fn strip_prefixes<'a>(s: &'a str, prefixes: &'a [String]) -> &'a str
stripped_s
}

pub(crate) fn is_integral_range(min: i32, max: i32, div: i32) -> bool {
(max - min) % div == 0
pub(crate) fn is_integral_range(min: f32, max: f32, div: f32) -> bool {
if min.floor() != min || max.floor() != max || div.floor() != div {
return false;
} else {
(max as i32 - min as i32) % div as i32 == 0
}
}

pub(crate) fn common_str_prefix(v: &[String]) -> &str {
Expand Down
14 changes: 7 additions & 7 deletions settings-parser/src/xml/display_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub enum DisplayType {
Toggle,
Options(OptionsArray), // array of displayNames
Slider {
min: i32,
max: i32,
div: i32
min: f32,
max: f32,
div: f32
},
SubtleSeparator
}
Expand Down Expand Up @@ -49,29 +49,29 @@ impl TryFrom<&Node<'_, '_>> for DisplayType {
} else if spl.len() != 4 {
Err(format!("Invalid amount of slider parameters. Should be 3, is {}", spl.len() - 1))
} else {
let min = spl[1].parse::<i32>();
let min = spl[1].parse::<f32>();
if min.is_err() {
return Err(format!("Slider min value parse error: {}", min.unwrap_err()));
}

let min = min.unwrap();


let max = spl[2].parse::<i32>();
let max = spl[2].parse::<f32>();
if max.is_err() {
return Err(format!("Slider max value parse error: {}", max.unwrap_err()));
}

let max = max.unwrap();


let div = spl[3].parse::<i32>();
let div = spl[3].parse::<f32>();
if div.is_err() {
return Err(format!("Slider div value parse error: {}", div.unwrap_err()));
}

let div = div.unwrap();
if div <= 0 {
if div <= 0.0 {
return Err("Slider div value must be greater than 0".to_string());
}

Expand Down
9 changes: 8 additions & 1 deletion settings-parser/src/xml/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ impl TryFrom<&Node<'_, '_>> for Group {
if let Some(visible_vars_node) = node.children().find(|n| n.has_tag_name("VisibleVars")) {
let var_nodes = visible_vars_node.children().filter(|n| n.has_tag_name("Var"));
for var_node in var_nodes {
visible_vars.push(Var::try_from(&var_node)?);
match Var::try_from(&var_node) {
Ok(var) => {
visible_vars.push(var);
}
Err(err) => {
return Err(format!("Error parsing var at {}: {}", node_pos(&var_node), err));
}
}
}
}

Expand Down

0 comments on commit a23d82a

Please sign in to comment.