BACKUP ~A7-TextureConvert/backup~
AUTHOR ~Argent77~
VERSION ~2.0~

ALWAYS
  INCLUDE ~%MOD_FOLDER%/lib/a7_tools.tpa~
  DEFINE_ACTION_FUNCTION a7_convert_pvrz
  INT_VAR
    square = 0
  STR_VAR
    format = ~~
  BEGIN
    ACTION_IF (NOT ~%format%~ STR_EQ ~~) BEGIN
      OUTER_TEXT_SPRINT folder_base ~%MOD_FOLDER%/temp~
      OUTER_TEXT_SPRINT folder_input ~%folder_base%/in~
      OUTER_TEXT_SPRINT folder_output ~%folder_base%/out~
      MKDIR ~%folder_input%~
      MKDIR ~%folder_output%~

      LAF GET_TOOL_BINARY
      STR_VAR
        tool_name = ~PVRTexToolCLI~
      RET
        tool_binary
      END

      OUTER_TEXT_SPRINT param_square ~-potcanvas +~
      ACTION_IF (square) BEGIN
        OUTER_TEXT_SPRINT param_square ~%param_square% -squarecanvas +~
      END

      OUTER_SET input_files = 0
      COPY_EXISTING_REGEXP - ~.+\.pvrz~ ~override~
        TEXT_SPRINT EVAL ~input_files_%input_files%~ ~%SOURCE_RES%~
        SET input_files += 1
      BUT_ONLY

      OUTER_FOR (idx = 0; idx < input_files; ++idx) BEGIN
        OUTER_TEXT_SPRINT fileRes EVAL ~%input_files_%idx%%~
        PRINT ~Converting %fileRes%.PVRZ...~
        COPY_EXISTING + ~%fileRes%.PVRZ~ ~%folder_input%/%fileRes%.PVR~
          READ_LONG 0 unc_size
          DECOMPRESS_REPLACE_FILE 4 (BUFFER_LENGTH - 4) unc_size
        BUT_ONLY
        LAF EXECUTE_TOOL
        INT_VAR
          exec_type = 0
        STR_VAR
          tool_binary = EVAL ~%tool_binary%~
          arg_0 = EVAL ~-i "%folder_input%/%fileRes%.PVR"~
          arg_1 = EVAL ~-o "%folder_output%/%fileRes%.PVR"~
          arg_2 = EVAL ~-f %format%,UBN,lRGB~
          arg_3 = EVAL ~%param_square%~
        END
        DELETE + ~%folder_input%/%fileRes%.PVR~
        COPY ~%folder_output%/%fileRes%.PVR~ ~override/%fileRes%.PVRZ~
          SET len = BUFFER_LENGTH
          COMPRESS_REPLACE_FILE 0 len 6
          INSERT_BYTES 0 4
          WRITE_LONG 0 len
        BUT_ONLY
        DELETE + ~%folder_output%/%fileRes%.PVR~
      END

      DELETE + ~%folder_base%~
    END
  END
END


BEGIN ~Texture format: PVRTC 2bpp~
  SUBCOMPONENT ~Convert PVRZ files~
  LAF a7_convert_pvrz INT_VAR square = 1 STR_VAR format = ~PVRTC1_2~ END

BEGIN ~Texture format: PVRTC 4bpp (recommended)~
  SUBCOMPONENT ~Convert PVRZ files~
  LAF a7_convert_pvrz INT_VAR square = 1 STR_VAR format = ~PVRTC1_4~ END

BEGIN ~Texture format: ETC1~
  SUBCOMPONENT ~Convert PVRZ files~
  LAF a7_convert_pvrz STR_VAR format = ~ETC1~ END

BEGIN ~Texture format: ETC2~
  SUBCOMPONENT ~Convert PVRZ files~
  LAF a7_convert_pvrz STR_VAR format = ~ETC2_RGBA~ END

