Author Topic: Patching SoD saves  (Read 901 times)

Offline Andrea C.

  • Planewalker
  • *****
  • Posts: 80
  • Gender: Male
Patching SoD saves
« on: April 11, 2017, 01:50:29 PM »
Hello.

I've got the following code in my tp2 to patch saved games:

Code: [Select]
GET_DIRECTORY_ARRAY save_dir ~%USER_DIRECTORY%/save~ ~~

ACTION_PHP_EACH save_dir AS from => to BEGIN
  ACTION_IF FILE_EXISTS ~%to%/baldur.sav~ AND FILE_EXISTS ~%to%/baldur.gam~  BEGIN
    PRINT ~Updating saved game %to% ...~
    COPY ~%to%/baldur.sav~ ~%to%~
      EDIT_SAV_FILE 1 BEGIN
        SPRINT file ~%SAV_FILE%~
        PATCH_IF ((~%file%~ STRING_MATCHES_REGEXP ~^.*\.are$~)=0) BEGIN
          READ_LONG 0x54 "actors_off"
          READ_SHORT 0x58 "actors_cnt"
          FOR (cnt=0; cnt<"%actors_cnt%"; cnt=cnt+1) BEGIN
            READ_LONG ("%actors_off%"+0x110*cnt+0x30) "old"
            SPRINTF old ~%x~ (old) //%~
            PATCH_IF (VARIABLE_IS_SET $cd_animation_map(~%old%~)) BEGIN
              TEXT_SPRINT new $cd_animation_map(~%old%~)
              PATCH_IF (NOT ~%old%~ STRING_EQUAL_CASE ~%new%~) BEGIN
                PATCH_PRINT ~Patching %file%: cre animation %old% => %new%~
                WRITE_LONG ("%actors_off%"+0x110*cnt+0x30) "new"
              END
            END
            READ_BYTE ("%actors_off%"+0x110*cnt+0x28) "actors_flags"
            PATCH_IF (~%actors_flags%~ BAND BIT0) BEGIN
            END ELSE BEGIN //CRE attached
              READ_LONG ("%actors_off%"+0x110*cnt+0x88) "actors_emb_off"
              PATCH_IF (actors_emb_off != 0) BEGIN
                READ_LONG ("%actors_emb_off%"+0x28) "old"
                SPRINTF old ~%x~ (old) //%~
                PATCH_IF (VARIABLE_IS_SET $cd_animation_map(~%old%~)) BEGIN
                  TEXT_SPRINT new $cd_animation_map(~%old%~)
                  PATCH_IF (NOT ~%old%~ STRING_EQUAL_CASE ~%new%~) BEGIN
                    PATCH_PRINT ~Patching %file%: attached cre animation %old% => %new%~
                    WRITE_LONG ("%actors_emb_off%"+0x28) "new"
                  END
                END
              END
            END
          END
        END
      END
    BUT_ONLY
    COPY ~%to%/baldur.gam~ ~%to%~
      READ_LONG 0x20 party_off
      READ_LONG 0x24 party_cnt
      FOR (cnt=0; cnt<"%party_cnt%"; cnt=cnt+1) BEGIN
        READ_LONG ("%party_off%"+0x160*cnt+0x04) "cre_off"
        READ_LONG ("%cre_off%"+0x28) "old"
        SPRINTF old ~%x~ (old) //%~
        PATCH_IF (VARIABLE_IS_SET $cd_animation_map(~%old%~)) BEGIN
          TEXT_SPRINT new $cd_animation_map(~%old%~)
          PATCH_IF (NOT ~%old%~ STRING_EQUAL_CASE ~%new%~) BEGIN
            PATCH_PRINT ~Patching GAM file: party cre animation %old% => %new%~
            WRITE_LONG ("%cre_off%"+0x28) "new"
          END
        END
      END
    BUT_ONLY
  END
END


It works perfectly on BG:EE and BGII:EE, but fails to patch Siege of Dragonspear saves as they are stored in a different folder called "sodsave". I would like to change my code so that it checks whether "sodsave" exists and, if it does, it runs the patching routine for that folder as well; if it doesn't, it only runs the routine for "save".

My WeiDU is way rusty as it's been over a year since the last time I've written any. Could any of you lend a hand?

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
Re: Patching SoD saves
« Reply #1 on: April 11, 2017, 04:49:31 PM »
Try DIRECTORY_EXISTS.

If you want to make it more foolproof you could also examine entries in CAMPAIGN.2DA since save folders are not hardcoded anymore (e.g. EET uses "save" for all campaigns except Black Pits).

Example code:
Code: [Select]
COPY_EXISTING - ~campaign.2da~ ~override~
  COUNT_2DA_COLS numCols
  COUNT_2DA_ROWS numCols numRows
  FOR (row = 0; row < numRows; ++row) BEGIN
    READ_2DA_ENTRY row 0 numCols campaign
    PATCH_IF (~%campaign%~ STRING_EQUAL_CASE ~SOD~) BEGIN
      READ_2DA_ENTRY row 12 numCols folderName

      // patch saves in "%USER_DIRECTORY%/%folderName%" ...

      SET row = numRows   // terminate FOR loop
    END
  END

Offline Andrea C.

  • Planewalker
  • *****
  • Posts: 80
  • Gender: Male
Re: Patching SoD saves
« Reply #2 on: April 14, 2017, 09:09:03 AM »
Thank you, Argent.

DIRECTORY_EXISTS seems to have done the trick, although the code is possibly a bit clunky now as it essentially repeats the same patching routing for SOD is the relative save folder is found. I'll look into using CAMPAIGN.2DA farther down the road as it would probably make for a sleeker code.

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: