Skip to content

Commit

Permalink
refactor enum value mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust committed Aug 6, 2023
1 parent af54c0d commit 3afbe92
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/workflows/update-doc-and-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
-p="MOD" -p="mod_"
-m="MyModSettings"
-v="1.23"
--option-parsing-mode=enums
working-directory: settings-parser
- name: Parse DifficultyMod sample
run: >
Expand Down
13 changes: 3 additions & 10 deletions settings-parser/src/settings_enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use crate::{
xml::display_type::OptionsArray,
cli::CLI,
Expand Down Expand Up @@ -72,12 +70,7 @@ impl WitcherScriptTypeDef for SettingsEnum {
}


#[derive(Default)]
/// Describes the relationship between values in the unified enum and indices in the specific var in UserConfig.
pub struct SettingsEnumValueMapping {
//TODO turn into simple arrays
/// Converts from index in UserConfig's var to integer value of the unified enum.
pub config_to_unified: HashMap<i32, i32>,
/// Converts from integer value of the unified enum to index in UserConfig's var.
pub unified_to_config: HashMap<i32, i32>
}
/// This vec should be the same size as the `values` vec in SettingsEnum.
/// For each element in said 'values' vec it attributes the value of element in the unified enum for this enum type.
pub type SettingsEnumValueMapping = Vec<usize>;
30 changes: 10 additions & 20 deletions settings-parser/src/settings_master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ impl SettingsMaster {

for (val, val_mapping) in it {
let mut mapping = SettingsEnumValueMapping::default();
mapping.resize(val.values.len(), 0);

let unified_enum = self.enums.iter()
.find(|e| e.common_prefix == val.common_prefix)
.expect(&format!("Unified enum type not found for enum {}", val.type_name));
Expand All @@ -147,23 +149,12 @@ impl SettingsMaster {
for i in 0..val.values.len() {
for j in 0..unified_enum.values.len() {
if val.values[i] == unified_enum.values[j] {
mapping.config_to_unified.insert(i as i32, j as i32);
break;
}
}
}

for i in 0..unified_enum.values.len() {
for j in 0..val.values.len() {
if unified_enum.values[i] == val.values[j] {
mapping.unified_to_config.insert(i as i32, j as i32);
mapping[i] = j;
break;
}
mapping.unified_to_config.insert(i as i32, 0);
}
}


*val_mapping = Some(mapping);
}
}
Expand Down Expand Up @@ -423,14 +414,13 @@ fn enum_mapping_function(master: &SettingsMaster, buffer: &mut WitcherScript, co
buffer.push_line("switch(val)")
.push_line("{");

if config_to_unified {
for (config, unified) in &mapping.config_to_unified {
buffer.push_line(&format!("case {}: return {};", config, unified));
}
} else {
for (unified, config) in &mapping.unified_to_config {
buffer.push_line(&format!("case {}: return {};", unified, config));
}
for i in 0..mapping.len() {
let (k, v) = if config_to_unified {
(i, mapping[i])
} else {
(mapping[i], i)
};
buffer.push_line(&format!("case {}: return {};", k, v));
}

buffer.push_line("}");
Expand Down

0 comments on commit 3afbe92

Please sign in to comment.