Skip to content

Commit

Permalink
Make 2DA parser more robust
Browse files Browse the repository at this point in the history
Lines with different numbers of 2DA columns should not cause the installation process to fail.
  • Loading branch information
Argent77 committed May 29, 2023
1 parent add919b commit 649c433
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
33 changes: 20 additions & 13 deletions HiddenGameplayOptions/lib/keybinding.tph
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,29 @@ BEGIN
BUT_ONLY IF_EXISTS

ACTION_PHP_EACH clabs AS clab => _ BEGIN
COPY_EXISTING ~%clab%.2DA~ ~override~
COUNT_2DA_COLS num_cols
READ_2DA_ENTRIES_NOW table num_cols
FOR (row = 0; row < table; ++row) BEGIN
FOR (col = 0; col < num_cols; ++col) BEGIN
READ_2DA_ENTRY_FORMER table row col value
PATCH_IF (~%value%~ STRING_MATCHES_REGEXP ~GA_%prefix%[1-9][0-9][0-9]~ = 0) BEGIN
LPF SUBSTRING INT_VAR start = 3 length = 7 STR_VAR string = EVAL ~%value%~ RET substring END
TO_UPPER ~substring~
PATCH_IF (FILE_EXISTS_IN_GAME ~%substring%.SPL~) BEGIN
SET $entries(~%substring%~) = "-1"
WITH_SCOPE BEGIN
COPY_EXISTING ~%clab%.2DA~ ~override~
COUNT_2DA_COLS num_cols
READ_2DA_ENTRIES_NOW clab_table num_cols
FOR (row = 0; row < clab_table; ++row) BEGIN
FOR (col = 0; col < num_cols; ++col) BEGIN
PATCH_IF (VARIABLE_IS_SET $clab_table(~%row%~ ~%col%~)) BEGIN
READ_2DA_ENTRY_FORMER clab_table row col value
PATCH_IF (~%value%~ STRING_MATCHES_REGEXP ~GA_%prefix%[1-9][0-9][0-9]~ = 0) BEGIN
LPF SUBSTRING INT_VAR start = 3 length = 7 STR_VAR string = EVAL ~%value%~ RET substring END
TO_UPPER ~substring~
PATCH_IF (FILE_EXISTS_IN_GAME ~%substring%.SPL~) BEGIN
SET $entries(~%substring%~) = "-1"
END
END
END ELSE BEGIN
PATCH_WARN ~WARNING: [%SOURCE_FILE%] Unexpected number of columns in row %row% (expected: %num_cols%, found: %col%)~
SET col = num_cols
END
END
END
END
BUT_ONLY IF_EXISTS
BUT_ONLY IF_EXISTS
END
END

// collecting spells from HLA tables
Expand Down
39 changes: 23 additions & 16 deletions HiddenGameplayOptions/lib/lua_spells.tph
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,32 @@ BUT_ONLY IF_EXISTS

// Scanning CLABs for spells
ACTION_PHP_EACH clab_list AS clab_res => _ BEGIN
COPY_EXISTING ~%clab_res%.2DA~ ~override~
COUNT_2DA_COLS num_cols
READ_2DA_ENTRIES_NOW clab_table 1
FOR (row = 3; row < clab_table; ++row) BEGIN
FOR (col = 1; col < num_cols; ++col) BEGIN
READ_2DA_ENTRY_FORMER clab_table row col value
PATCH_IF (~%value%~ STRING_MATCHES_REGEXP ~GA_.+~ = 0) BEGIN
INNER_PATCH_SAVE resref ~%value%~ BEGIN REPLACE_TEXTUALLY ~GA_~ ~~ END
TO_UPPER ~resref~
LPF VALIDATE_SPL STR_VAR resref RET valid type name END
PATCH_IF (valid) BEGIN
SET $spells(~%resref%~) = 1
SET $spell_types(~%resref%~) = type
SPRINT $spell_names(~%resref%~) ~%name%~
WITH_SCOPE BEGIN
COPY_EXISTING ~%clab_res%.2DA~ ~override~
COUNT_2DA_COLS num_cols
READ_2DA_ENTRIES_NOW clab_table 1
FOR (row = 3; row < clab_table; ++row) BEGIN
FOR (col = 1; col < num_cols; ++col) BEGIN
PATCH_IF (VARIABLE_IS_SET $clab_table(~%row%~ ~%col%~)) BEGIN
READ_2DA_ENTRY_FORMER clab_table row col value
PATCH_IF (~%value%~ STRING_MATCHES_REGEXP ~GA_.+~ = 0) BEGIN
INNER_PATCH_SAVE resref ~%value%~ BEGIN REPLACE_TEXTUALLY ~GA_~ ~~ END
TO_UPPER ~resref~
LPF VALIDATE_SPL STR_VAR resref RET valid type name END
PATCH_IF (valid) BEGIN
SET $spells(~%resref%~) = 1
SET $spell_types(~%resref%~) = type
SPRINT $spell_names(~%resref%~) ~%name%~
END
END
END ELSE BEGIN
PATCH_WARN ~WARNING: [%SOURCE_FILE%] Unexpected number of columns in row %row% (expected: %num_cols%, found: %col%)~
SET col = num_cols
END
END
END
END
BUT_ONLY IF_EXISTS
BUT_ONLY IF_EXISTS
END
END


Expand Down

0 comments on commit 649c433

Please sign in to comment.