The BG1 NPC beta version, which uses on the fly conversion of the tra files, uses a similar method in order to keep the files in the directory specified through LANGUAGE and maintain compatibility with AUTO_TRA. Although, instead of COPY, it uses MOVE, which is also reversed at uninstall. Another difference is that I used MOVE on a file by file basis.
The example of BG1 NPC also taught me another lesson: if your mod has several components, beware of ALWAYS, as it is run with every component. So you're going to copy the tra files over and over and your backup directory will grow unexpectedely.
In BG1 NPC, due to the fact that the first component, required, evaluates tra files using the suffix _tmp and generates tra files without the _tmp part, I used an ACTION_IF to check for the existence of the resulting file to determine whether the operation had already been done and avoid repeating it. In case of on the fly conversion, this is very important since the file in the directory could already be converted and iconv doesn't behave well when you tell it to convert from CP1252 while it's actually already UTF-8, as the secondary byte in UTF8 for a special character can be seen as a special character as well in CP1252, so it converts it and the result is garbage and probably invalid UTF-8 code.
When using premade separate sets for CP1252 and UTF-8 in a multi-component mod, I would still advise some protection be used in ALWAYS. I'm not sure it's possible to use a variable. I have the feeling they are reset when we move to another component. I believe creating an fake file after copying the tra files and checking for its presence to avoid copying again would work.
Here is an example of how it's done in BG1 NPC (this code is slightly different from the GitHub code as I added Linux and Mac handling, and I didn't submit it to cmorgan yet)
ACTION_IF FILE_EXISTS_IN_GAME ~neera.dlg~ THEN BEGIN
/*Tell the player it is using BG:EE stuff */
PRINT @5000
INCLUDE ~bg1npc\lib\liam_bgee_vars.tpa~
OUTER_SPRINT ~bgee~ ~bgee/~
// BG:EE tra conversion
// Character encoding for BG II for various languages: ensure the name matches the menu choices
ACTION_DEFINE_ASSOCIATIVE_ARRAY languageencoding BEGIN
"french" => "CP1252"
"spanish" => "CP1252"
END
OUTER_SPRINT initialencoding ~~
ACTION_PHP_EACH languageencoding AS lang => encoding BEGIN
ACTION_IF ("%LANGUAGE%" STRING_EQUAL_CASE ~%lang%~ = 0) THEN BEGIN
OUTER_SPRINT initialencoding ~%encoding%~
END
END
// Isaya: check for existence of BG1NPC/TRA/%LANGUAGE%/BG1NPC.tra as a marker that the mod
// has already installed at least one component, so conversion is already done, in order
// to avoid repeating for each component (as a backup of all tra files is then done in each
// subdirectory of backup)
// This is a handy trick with BG1 NPC, since BG1NPC.tra is created when the mod is installed
// and removed with the mod. But it cannot be used as a generic trick for other mods.
ACTION_IF NOT (FILE_EXISTS_IN_GAME ~bg1npc/tra/%LANGUAGE%/BG1NPC.tra~) AND
(STRING_LENGTH ~%initialencoding%~ != 0) THEN BEGIN
ACTION_IF ("%WEIDU_OS%" STRING_EQUAL_CASE ~WIN32~ = 0) THEN BEGIN
// Only convert for the language selected by passing the directory as parameter
AT_NOW ~bg1npc/conv_tra.bat %LANGUAGE% %initialencoding%~
END
ACTION_IF ("%WEIDU_OS%" STRING_EQUAL_CASE ~OSX~ = 0) OR ("%WEIDU_OS%" STRING_EQUAL_CASE ~UNIX~ = 0) THEN BEGIN
// Only convert for the language selected by passing the directory as parameter
AT_NOW ~bg1npc/conv_tra.sh %LANGUAGE% %initialencoding%~
END
// Isaya: all tra files have to be renamed from .tra_utf8 to .tra!
// Otherwise the tra files for dialogs are still with the Windows 1252 encoding
MOVE ~bg1npc/tra/%LANGUAGE%/bg1npc_tmp.tra_utf8~ ~bg1npc/tra/%LANGUAGE%/bg1npc_tmp.tra~
... MOVE all tra files
LOAD_TRA ~bg1npc/tra/%LANGUAGE%/setup.tra~
PRINT ~re-loaded %LANGUAGE% TRA files~
END
END ELSE BEGIN
ACTION_IF FILE_EXISTS_IN_GAME ~FW0100.are~ THEN BEGIN
/* Tell the player it is using Tutu stuff */
PRINT @1000
INCLUDE ~bg1npc\lib\g3_tutu_cpmvars.tpa~
OUTER_SPRINT ~bgee~ ~~
END ELSE BEGIN
ACTION_IF FILE_EXISTS_IN_GAME ~AR7200.are~ THEN BEGIN
/* Tell the player it is using BGT stuff */
PRINT @1001
INCLUDE ~bg1npc\lib\g3_bgt_cpmvars.tpa~
OUTER_SPRINT ~bgee~ ~~
/* Tell the player it is not Tutu or BGT */
END ELSE BEGIN FAIL ~Please install on BG:EE, Tutu or BGT.~
END
END
END
with conv_tra.bat
:: Use a parameter to the script (%1) to specify the directory that must be converted
:: Use a parameter to the script (%2) to specify the encoding of the files to convert
:: Prevent display of DOS command lines
@echo off
:: Since the pattern matching in Windows considers that "fu.tra_utf8" also matches "*.tra", we have to
:: handle the file extension by ourselfves in order to avoid all those tra_utf8_utf8 etc files
:: Use %%~ni instead of %%~nxi (x is for extension) and specify the tra extension
for %%i in (bg1npc\tra\%1\*.tra) do bg1npc\iconv -f %2 -t UTF-8 "bg1npc\tra\%1\%%~ni.tra" > "bg1npc\tra\%1\%%~ni.tra_utf8"
and conv_tra.sh
# Use a parameter to the script ($1) to specify the directory that must be converted
# Use a parameter to the script ($2) to specify the encoding of the files to convert
for file in $(ls bg1npc/tra/$1/*.tra)
do
iconv -f "$2" -t UTF-8 "$file" > "$file"_utf8
done
I couldn't test conv_tra.sh in a real installation as I don't have the game under my Linux virtual machine but I checked it the script standalone nonetheless.
Wisp, ever since I wrote the code to use MOVE in V231 and now in V235, I'm finding that the foo.tra_utf8 file I ask to move are actually copied under the new name but foo.tra_utf8 is not removed. Do you know if it's this a bug or if there's a reason for that?