Skip to content

Commit

Permalink
v0.3.1 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust authored Jul 17, 2023
1 parent 74c04db commit 3b7d4f6
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 206 deletions.
20 changes: 15 additions & 5 deletions .github/workflows/check-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ env:

jobs:
build:
strategy:
matrix:
target:
- x86_64-pc-windows-msvc
- x86_64-unknown-linux-gnu
include:
- target: x86_64-pc-windows-msvc
os: windows-latest
- target: x86_64-unknown-linux-gnu
os: ubuntu-latest

runs-on: windows-latest
runs-on: ${{matrix.os}}

steps:
- name: Checkout
Expand All @@ -21,11 +31,11 @@ jobs:
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-pc-windows-msvc
target: ${{matrix.target}}
override: true
- name: Build with Cargo
run: cargo build --release --target=x86_64-pc-windows-msvc
working-directory: ./settings-parser
run: cargo build --release --target=${{matrix.target}}
working-directory: settings-parser
- name: Run tests
run: cargo test --verbose
working-directory: ./settings-parser
working-directory: settings-parser
55 changes: 55 additions & 0 deletions .github/workflows/regenerate-samples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: regenerate-samples

on:
push:
branches: '*'
pull_request:
branches: '*'

env:
CARGO_TERM_COLOR: always

jobs:
regenerate-samples:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Rust stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-unknown-linux-gnu
override: true
- name: Build with Cargo
run: cargo build --release --target=x86_64-unknown-linux-gnu
working-directory: settings-parser
- name: Parse doc example
run: >
cargo run --release --
-f="../doc/mod-menu.xml"
-o="../doc/mod-menu.ws"
--omit-prefix="MOD"
-m="MyModSettings"
-v="1.23"
working-directory: settings-parser
- name: Parse DifficultyMod sample
run: >
cargo run --release --
-f="../samples/DifficultyMod/bin/config/r4game/user_config_matrix/pc/modSettingsFrameworkSampleDifficultyMod.xml"
-o="../samples/DifficultyMod/modSettingsFrameworkSampleDifficultyMod/content/scripts/local/difficulty_mod_base.ws"
-m="ModDifficultySettingsBase"
--omit-prefix="DM"
--default-preset-keyword="DEFAULT"
-v="1.1"
working-directory: settings-parser
- name: Check for changes in the project
id: get_changes
run: echo "changed=$(git status --porcelain | wc -l)" >> $GITHUB_OUTPUT
- name: Committing changes if there are any
if: steps.get_changes.outputs.changed != 0
uses: EndBug/add-and-commit@v7
with:
message: "Update samples"

11 changes: 7 additions & 4 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 & Utilites v0.2.0 by SpontanCombust
// Code generated using Mod Settings Framework v0.3.1 by SpontanCombust & Aeltoth

class MyModSettings extends ISettingsMaster
{
Expand Down Expand Up @@ -57,13 +57,16 @@ class MyModSettings extends ISettingsMaster

public function ResetSettingsToDefault() : void
{
tab1.ResetSettingsToDefault();
tab2subtab1.ResetSettingsToDefault();
tab2subtab2.ResetSettingsToDefault();
tab1.ResetToDefault();
tab2subtab1.ResetToDefault();
tab2subtab2.ResetToDefault();
}

public function ShouldResetSettingsToDefaultOnInit() : bool
{
var config : CInGameConfigWrapper;
config = theGame.GetInGameConfigWrapper();

return config.GetVarValue('MODtab1','MODoption') == "";
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated using Mod Settings Framework & Utilites v0.2.0 by SpontanCombust
// Code generated using Mod Settings Framework v0.3.1 by SpontanCombust & Aeltoth

class ModDifficultySettingsBase 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.3.0"
version = "0.3.1"
edition = "2021"

[[bin]]
Expand Down
26 changes: 13 additions & 13 deletions settings-parser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ mod var_type;
mod settings_var;
mod settings_group;
mod settings_master;
mod xml_parsing;
mod to_witcher_script;
mod cli;
mod utils;

use std::{fs::OpenOptions, io::{Read, Write}, path::{Path, PathBuf}};

use clap::Parser;
use cli::CLI;
use to_witcher_script::ToWitcherScript;
use settings_master::SettingsMaster;

use crate::to_witcher_script::ToWitcherScript;

fn main() {

fn main() -> Result<(), String>{
let cli = CLI::parse();

let input_file_path = Path::new(&cli.xml_file_path);
Expand All @@ -26,8 +28,7 @@ fn main() {
let mut xml_file = match xml_file {
Ok(f) => f,
Err(e) => {
println!("Error opening menu xml file: {}", e);
return;
return Err(format!("Error opening menu xml file: {}", e));
}
};

Expand All @@ -45,20 +46,18 @@ fn main() {
let mut ws_file = match ws_file {
Ok(f) => f,
Err(e) => {
println!("Error creating witcher script output file: {}", e);
return;
return Err(format!("Error creating witcher script output file: {}", e));
}
};



let mut xml_text = String::new();
if let Err(e) = xml_file.read_to_string(&mut xml_text) {
println!("Error reading menu xml file: {}", e);
return;
return Err(format!("Error reading menu xml file: {}", e));
};

match xml_parsing::parse_settings_xml(xml_text, &cli) {
match SettingsMaster::from_xml(xml_text, &cli) {
Ok(master) => {
let mut code = String::new();

Expand All @@ -70,14 +69,15 @@ fn main() {
}

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

println!("Successfully parsed {} into {}", cli.xml_file_path, ws_path.to_str().unwrap_or(""));
}
Err(e) => {
println!("Error parsing menu xml file: {}", e);
return Err(format!("Error parsing menu xml file: {}", e));
}
}

return Ok(())
}
73 changes: 72 additions & 1 deletion settings-parser/src/settings_group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{settings_var::SettingsVar, to_witcher_script::ToWitcherScript};
use roxmltree::Node;

use crate::{settings_var::SettingsVar, to_witcher_script::ToWitcherScript, cli::CLI, utils::{validate_name, node_pos, id_to_script_name}};

#[derive(Default)]
pub struct SettingsGroup {
Expand All @@ -9,6 +11,75 @@ pub struct SettingsGroup {
pub vars: Vec<SettingsVar>
}

impl SettingsGroup {
pub fn from_xml(group_node: &Node, cli: &CLI) -> Result<Option<SettingsGroup>, String> {
if let Some(group_id) = group_node.attribute("id") {

if let Err(err) = validate_name(group_id) {
return Err(format!("Invalid Group id {} at {}: {}", group_id, node_pos(group_node), err));
}

let mut default_preset_index: Option<u8> = None;
if let Some(presets_array_node) = group_node.children().find(|n| n.has_tag_name("PresetsArray")) {
default_preset_index = SettingsGroup::parse_presets_array_node(&presets_array_node, cli);
}

if let Some(visible_vars_node) = group_node.children().find(|n| n.has_tag_name("VisibleVars")) {
let var_nodes: Vec<Node> = visible_vars_node.children().filter(|n| n.has_tag_name("Var")).collect();

if var_nodes.is_empty() {
println!("Group {} at {} has no vars and will be ignored.", group_id, node_pos(group_node));
return Ok(None);
}

let mut sg = SettingsGroup::default();
sg.master_name = cli.settings_master_name.to_owned();
sg.id = group_id.to_owned();
sg.name = id_to_script_name(group_id, &cli.omit_prefix);
sg.default_preset_index = default_preset_index;

for var_node in &var_nodes {
match SettingsVar::from_xml(&var_node, group_id, cli) {
Ok(var_opt) => {
if let Some(var) = var_opt {
sg.vars.push(var);
}
}
Err(err) => {
return Err(err);
}
}
}

return Ok(Some(sg));
}
else {
println!("Group {} at {} has no vars and will be ignored.", group_id, node_pos(group_node));
return Ok(None);
}
}
else {
println!("No id attribute found for Group tag at {}", node_pos(group_node));
return Ok(None);
}
}

fn parse_presets_array_node(presets_array_node: &Node, cli: &CLI) -> Option<u8> {
for preset_node in presets_array_node.children() {
if preset_node.has_tag_name("Preset") && preset_node.has_attribute("id") && preset_node.has_attribute("displayName") {
if preset_node.attribute("displayName").unwrap().contains(&cli.default_preset_keyword.to_lowercase()) {
return preset_node.attribute("id").unwrap().parse::<u8>().ok();
}
}
}

return None;
}
}




const SETTINGS_GROUP_PARENT_CLASS: &str = "ISettingsGroup";
const SETTINGS_GROUP_ID_VAR_NAME: &str = "id";
const SETTINGS_GROUP_DEFAULT_PRESET_VAR_NAME: &str = "defaultPresetIndex";
Expand Down
54 changes: 52 additions & 2 deletions settings-parser/src/settings_master.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{settings_group::SettingsGroup, to_witcher_script::ToWitcherScript, var_type::VarType};
use roxmltree::{Document, Node};

use crate::{settings_group::SettingsGroup, to_witcher_script::ToWitcherScript, var_type::VarType, cli::CLI, utils::validate_name};

#[derive(Default)]
pub struct SettingsMaster {
Expand All @@ -7,6 +9,54 @@ pub struct SettingsMaster {
pub groups: Vec<SettingsGroup>
}

impl SettingsMaster {
pub fn from_xml(xml_text: String, cli: &CLI) -> Result<SettingsMaster, String> {
if let Err(err) = validate_name(&cli.settings_master_name) {
return Err(format!("Invalid settings master name: {}", err));
}

let doc = match Document::parse(&xml_text) {
Ok(doc) => doc,
Err(err) => {
return Err(err.to_string())
}
};

let mut master = SettingsMaster::default();
master.name = cli.settings_master_name.clone();
master.mod_version = cli.mod_version.clone();

if let Some(root_node) = doc.descendants().find(|n| n.has_tag_name("UserConfig")) {
let group_nodes: Vec<Node> = root_node.children().filter(|n| n.has_tag_name("Group")).collect();

if group_nodes.is_empty() {
return Err("No Groups found inside UserConfig".to_string());
}

for group_node in &group_nodes {
match SettingsGroup::from_xml(group_node, cli) {
Ok(group_opt) => {
if let Some(group) = group_opt {
master.groups.push(group);
}
}
Err(err) => {
return Err(err);
}
}
}
}
else {
return Err("No UserConfig root node found".to_string());
}

return Ok(master);
}
}




const MASTER_BASE_CLASS_NAME: &str = "ISettingsMaster";
const MASTER_MOD_VERSION_VAR_NAME: &str = "modVersion";
const MASTER_INIT_FUNC_NAME: &str = "Init";
Expand All @@ -26,7 +76,7 @@ impl ToWitcherScript for SettingsMaster {
fn ws_code_body(&self) -> String {
let mut code = String::new();

code += &format!("// Code generated using Mod Settings Framework & Utilites v{} by SpontanCombust\n\n", option_env!("CARGO_PKG_VERSION").unwrap());
code += &format!("// Code generated using Mod Settings Framework v{} by SpontanCombust & Aeltoth\n\n", option_env!("CARGO_PKG_VERSION").unwrap());

code += &format!("class {} extends {}\n", self.name, MASTER_BASE_CLASS_NAME);
code += "{\n";
Expand Down
Loading

0 comments on commit 3b7d4f6

Please sign in to comment.