Skip to content

Commit

Permalink
compressed bif check
Browse files Browse the repository at this point in the history
  • Loading branch information
weigo87 committed Jan 10, 2025
1 parent 9ce9b5e commit e6f1a9d
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 1 deletion.
1 change: 1 addition & 0 deletions bggo/bggo.tp2
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ALWAYS
INCLUDE ~%MOD_FOLDER%/lib/alter_searchmap.tpa~
INCLUDE ~%MOD_FOLDER%/lib/debug.tpa~
INCLUDE ~%MOD_FOLDER%/lib/a7_pvrz_tis.tpa~
INCLUDE ~%MOD_FOLDER%/lib/a7_area_references.tpa~

ACTION_IF GAME_IS ~eet bgee bg2ee~ BEGIN
OUTER_SPRINT ee ~ee~
Expand Down
130 changes: 130 additions & 0 deletions bggo/lib/a7_area_references.tpa
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* This function scans ARE resources of the game to find referenced game files.
*
* INT_VAR compressed_only Whether only resources in compressed BIFF archives should be returned. Default: 1
* INT_VAR bmp Whether to consider BMP resources in the scan. Default: 0
* INT_VAR mos Whether to consider MOS resources in the scan. Default: 0
* INT_VAR tis Whether to consider TIS resources in the scan. Default: 0
* INT_VAR wav Whether to consider WAV resources in the scan. Default: 0
* INT_VAR wed Whether to consider WED resources in the scan. Default: 0
* STR_VAR are_regexp File name pattern as regular expression to filter ARE resources.
* The pattern should always end with "\.are$" to match ARE resources.
* Default: "^.+\.are$"
* RET_ARRAY resources Associative array with the referenced game resources.
* (key: resource name, value: reference count)
*/
DEFINE_ACTION_FUNCTION a7#get_are_resources
INT_VAR
compressed_only = 1
bmp = 0
mos = 0
tis = 0
wav = 0
wed = 0
STR_VAR
are_regexp = "^.+\.are$"
RET_ARRAY
resources
BEGIN
ACTION_IF (~%are_regexp%~ STR_EQ ~~) BEGIN
OUTER_SPRINT are_regexp ~^.+\.are$~
END

COPY_EXISTING_REGEXP ~%are_regexp%~ ~override~
READ_ASCII 0x00 sig (8)
PATCH_IF (~%sig%~ STR_EQ ~AREAV1.0~) BEGIN
// bmp
PATCH_IF (bmp) BEGIN
PATCH_FOR_EACH suffix IN ~HT~ ~LM~ ~LN~ ~SR~ BEGIN
LPF a7#add_if_compressed INT_VAR compressed_only STR_VAR resref = EVAL ~%SOURCE_RES%%suffix%~ ext = ~BMP~ RET_ARRAY resources END
END
END

// mos
PATCH_IF (mos) BEGIN
PATCH_FOR_EACH suffix IN ~~ ~N~ BEGIN
LPF a7#add_if_compressed INT_VAR compressed_only STR_VAR resref = EVAL ~%SOURCE_RES%%suffix%~ ext = ~MOS~ RET_ARRAY resources END
END
END

PATCH_IF (wed || tis) BEGIN
READ_ASCII 0x08 resref (8) NULL
// wed
PATCH_IF (wed) BEGIN
LPF a7#add_if_compressed INT_VAR compressed_only STR_VAR resref ext = ~WED~ RET_ARRAY resources END
END

// tis
PATCH_IF (tis) BEGIN
INNER_ACTION BEGIN
COPY_EXISTING ~%resref%.WED~ ~override~
READ_ASCII 0x00 sig (8)
PATCH_IF (~%sig%~ STR_EQ ~WED V1.3~) BEGIN
READ_LONG 0x08 num_ovl
READ_LONG 0x10 ofs_ovl
FOR (i = 0; i < num_ovl; ++i) BEGIN
SET cur_ofs = ofs_ovl + i * 0x18
READ_ASCII (cur_ofs + 0x04) resref (8) NULL
LPF a7#add_if_compressed INT_VAR compressed_only STR_VAR resref ext = ~TIS~ RET_ARRAY resources END
END
END
BUT_ONLY IF_EXISTS
END
END
END

// ambient sounds (wav)
PATCH_IF (wav) BEGIN
READ_SHORT 0x82 num_ambients
READ_LONG 0x84 ofs_ambients
FOR (i = 0; i < num_ambients; ++i) BEGIN
SET cur_ofs = ofs_ambients + i * 0xd4
READ_SHORT (cur_ofs + 0x80) num_sounds
SET num_sounds = (num_sounds > 10) ? 10 : num_sounds
FOR (j = 0; j < num_sounds; ++j) BEGIN
READ_ASCII (cur_ofs + 0x30 + j * 8) resref (8) NULL
LPF a7#add_if_compressed INT_VAR compressed_only STR_VAR resref ext = ~WAV~ RET_ARRAY resources END
END
END
END

END ELSE BEGIN
PATCH_WARN ~WARNING: a7#get_are_resources: Not an area file: %SOURCE_FILE%~
END
BUT_ONLY IF_EXISTS
END


/**
*Adds the specified resource to the "resources" array if it's located in a compressed BIFF archive.
*
* INT_VAR compressed_only Whether only resources in compressed BIFF archives should be returned. Default: 1
* STR_VAR resref Resref of the resource.
* STR_VAR ext File extension of the resource.
* RET_ARRAY resources Array entry with the resource if all conditions are met.
*/
DEFINE_PATCH_FUNCTION a7#add_if_compressed
INT_VAR
compressed_only = 1
STR_VAR
resref = ~~
ext = ~~
RET_ARRAY
resources
BEGIN
SPRINT resource ~%resref%.%ext%~

SET condition = NOT (~%resref%~ STR_EQ ~~ || ~%ext%~ STR_EQ ~~)
PATCH_IF (condition && compressed_only) BEGIN
SET condition = NOT FILE_EXISTS ~override/%resource%~ && FILE_IS_IN_COMPRESSED_BIFF ~%resource%~
END

PATCH_IF (condition) BEGIN
TO_UPPER ~resource~
PATCH_IF (VARIABLE_IS_SET $resources(~%resource%~)) BEGIN
SET $resources(~%resource%~) = $resources(~%resource%~) + 1
END ELSE BEGIN
SET $resources(~%resource%~) = 1
END
END
END
23 changes: 22 additions & 1 deletion bggo/lib/macros.tpa
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,30 @@ BEGIN
END
PATCH_IF FILE_EXISTS ~%MOD_FOLDER%/base/are_patch/%are%_are.tpa~ BEGIN // add animation for braziers, candles ...
PATCH_INCLUDE ~%MOD_FOLDER%/base/are_patch/%are%_are.tpa~
END
END
BUT_ONLY

// Get files from compressed biffs
//code by Argent77 https://github.com/Argent77/A7-BiffedAreasFix
LAF a7#get_are_resources
INT_VAR
compressed_only = 1
bmp = 1
mos = 1
tis = 1
wav = 1
wed = 1
STR_VAR
are_regexp = EVAL "^%are2%\.are$"
RET_ARRAY
resources
END
ACTION_PHP_EACH resources AS resource => ref_count BEGIN
PRINT ~Test~
COPY_EXISTING ~%resource%~ ~override~ IF_EXISTS
END
BUT_ONLY
ACTION_CLEAR_ARRAY resources

// biff only areas with tis and pvrz files
OUTER_SET area_biff = 0
Expand Down

0 comments on commit e6f1a9d

Please sign in to comment.