Author Topic: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work  (Read 1692 times)

Offline K4thos

  • Planewalker
  • *****
  • Posts: 110
[Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« on: July 03, 2016, 05:23:05 PM »
Example code:
Code: [Select]

EDIT_SAV_FILE 1 ADD_IF_MISSING worldm25.wmp BEGIN
END
results in corrupted worldm25.wmp file added to the SAV file. Also tried it with ARE files that didn't exist is SAV files - same result. Tested on latest stable and latest beta releases of weidu.

No idea how to insert new bytes into compressed file manually (based on IESDP both Compressed and Uncompressed data length are required which seems problematic when we only have uncompressed value beforehand), so I hope this command will be fixed.
« Last Edit: July 03, 2016, 08:38:46 PM by K4thos »

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
Re: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« Reply #1 on: July 04, 2016, 06:46:37 AM »
As a workaround you can do all that stuff without touching EDIT_SAV_FILE. WeiDU already provides commands to compress or decompress data. To replace a file in BALDUR.SAV you could do the following:
Code: [Select]
// preparing new worldmap resource
COPY_EXISTING - ~worldmap.wmp~ ~override~
  COMPRESS_INTO_VAR 0 SOURCE_SIZE 9 wmpData
  SET uncLen = SOURCE_SIZE
  SET compLen = STRING_LENGTH ~%wmpData%~

// replacing old worldmap with new worldmap
COPY ~%SAVE_DIRECTORY%/000000001-Quick-Save/baldur.sav~ ~%SAVE_DIRECTORY%/000000001-Quick-Save/baldur.sav~
  PATCH_IF (SOURCE_SIZE >= 8) BEGIN
    READ_ASCII 0 sig ( 8 )
    PATCH_IF (~%sig%~ STRING_EQUAL ~SAV V1.0~) BEGIN
      SET curOfs = 8
      WHILE (curOfs < SOURCE_SIZE) BEGIN  // scanning resource entries...
        READ_LONG curOfs lenFileName
        READ_ASCII (curOfs + 4) fileName ( lenFileName ) NULL
        READ_LONG (curOfs + lenFileName + 8) lenCompressed
        SET entrySize = lenFileName + lenCompressed + 12
        PATCH_IF (~%fileName%~ STRING_EQUAL_CASE ~WORLDMAP.WMP~) BEGIN
          // removing old worldmap data from the BALDUR.SAV
          DELETE_BYTES (curOfs + lenFileName + 12) lenCompressed

          // inserting new worldmap data to the BALDUR.SAV
          WRITE_LONG (curOfs + lenFileName + 4) uncLen
          WRITE_LONG (curOfs + lenFileName + 8) compLen
          INSERT_BYTES (curOfs + lenFileName + 12) compLen
          WRITE_ASCIIE (curOfs + lenFileName + 12) ~%wmpData%~

          // optional if you want to continue scanning for files
          SET SOURCE_SIZE += compLen - lenCompressed
          SET entrySize = lenFileName + compLen + 12
          SET curOfs += entrySize

          // terminate WHILE loop
          SET curOfs = SOURCE_SIZE
        END ELSE BEGIN
          // skipping entry
          SET curOfs += entrySize
        END
      END
    END
  END
BUT_ONLY

Offline K4thos

  • Planewalker
  • *****
  • Posts: 110
Re: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« Reply #2 on: July 04, 2016, 10:25:16 AM »
Once again thank you Argent for help. I will stick with this implementation. Just tested your code and it works great.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« Reply #3 on: July 09, 2016, 06:19:34 AM »
Will fix.
« Last Edit: July 10, 2016, 06:31:36 AM by Wisp »

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« Reply #4 on: July 10, 2016, 06:31:19 AM »
Well, maybe not.

Quote from: Readme
If the optional ADD_IF_MISSING is provided, the patches are applied to the empty string, and the resulting file will be added to the SAV itself.
There does not seem to be any incongruity between behaviour and documentation. Though ADD_IF_MISSING seems ill-named, since it's unconditional.

Offline the bigg

  • The Avatar of Fighter / Thieves
  • Moderator
  • Planewalker
  • *****
  • Posts: 3804
  • Gender: Male
Re: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« Reply #5 on: July 10, 2016, 01:12:01 PM »
Given that the feature was never released in WeiDU stable, it could make sense to load the existing resource and patch that, instead of starting from an empty string (make sure to fall back to the empty string if the resource doesn't exist, or you'll break my tool[url]).
Author or Co-Author: WeiDU (http://j.mp/bLtjOn) - Widescreen (http://j.mp/aKAiqG) - Generalized Biffing (http://j.mp/aVgw3U) - Refinements (http://j.mp/bLHoCc) - TB#Tweaks (http://j.mp/ba02Eg) - IWD2Tweaks (http://j.mp/98OFYY) - TB#Characters (http://j.mp/ak8J55) - Traify Tool (http://j.mp/g1Ry9A) - Some mods that I won't mention in public
Maintainer: Semi-Multi Clerics (http://j.mp/9UeIwB) - Nalia Mod (http://j.mp/dng9l0) - Nvidia Fix (http://j.mp/aRWjjg)
Code dumps: Detect custom secondary types (http://j.mp/hVzzXG) - Stutter Investigator (http://j.mp/gdtBn8)

If possible, send diffs, translations and other contributions using Git (http://j.mp/aBZFrq).

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« Reply #6 on: July 10, 2016, 03:15:38 PM »
Any problem with having it look through the queue to see if the file already exists before pushing the file to the queue (potentially resulting in the same file existing twice in the SAV)?

Offline the bigg

  • The Avatar of Fighter / Thieves
  • Moderator
  • Planewalker
  • *****
  • Posts: 3804
  • Gender: Male
Re: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« Reply #7 on: July 11, 2016, 01:09:12 PM »
The code is already scanning all files and keeping track of the missing ones; also, duplicate entries in SAV files cannot be loaded (at least in the BG2 engine). I was suggesting this, however (hopefully it'll compile and work correctly):

Code: [Select]
diff --git a/src/tppatch.ml b/src/tppatch.ml
index 7a607f1..0ac272a 100755
--- a/src/tppatch.ml
+++ b/src/tppatch.ml
@@ -1644,9 +1644,12 @@ let rec process_patch2_real process_action tp patch_filename game buff p =
           List.iter (fun file ->
             let file = String.uppercase(eval_pe_str file) in
             Var.set_string "SAV_FILE" file;
+            let a, b = split filename in
+            let new_buff, path =
+              Load.load_resource "INNER_PATCH_FILE" game true a b in
             let result = List.fold_left (fun acc elt ->
                 process_patch2 patch_filename game acc elt)
-                  "" pl in
+                  new_buff pl in
               Queue.push {Sav.filename = file;
                           Sav.contents = result}  nsav;
           ) !files;
Author or Co-Author: WeiDU (http://j.mp/bLtjOn) - Widescreen (http://j.mp/aKAiqG) - Generalized Biffing (http://j.mp/aVgw3U) - Refinements (http://j.mp/bLHoCc) - TB#Tweaks (http://j.mp/ba02Eg) - IWD2Tweaks (http://j.mp/98OFYY) - TB#Characters (http://j.mp/ak8J55) - Traify Tool (http://j.mp/g1Ry9A) - Some mods that I won't mention in public
Maintainer: Semi-Multi Clerics (http://j.mp/9UeIwB) - Nalia Mod (http://j.mp/dng9l0) - Nvidia Fix (http://j.mp/aRWjjg)
Code dumps: Detect custom secondary types (http://j.mp/hVzzXG) - Stutter Investigator (http://j.mp/gdtBn8)

If possible, send diffs, translations and other contributions using Git (http://j.mp/aBZFrq).

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: [Bug] EDIT_SAV_FILE level ADD_IF_MISSING doesn't work
« Reply #8 on: August 04, 2016, 02:58:35 PM »
ADD_IF_MISSING will now load the file buffer if it exists (per FILE_EXISTS_IN_GAME) or an empty buffer if not.

 

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)?: