Author Topic: Sample code for documentation  (Read 2115 times)

Offline Miloch

  • Barbarian
  • Planewalker
  • *****
  • Posts: 1032
  • Gender: Male
Sample code for documentation
« on: September 22, 2009, 06:47:57 AM »
[ACTION_]DEFINE_ASSOCIATIVE_ARRAY still isn't documented, and isn't even listed under "undocumented features." I don't know how long you're going to let this and other "more exaustive" (sp.) documentation slide, but I clipped some examples out of working code.

Buried in the WeiDU change history is a reference to me providing usage for COMPRESS/DECOMPRESS functions. Well here they are too, excerpted from BAM Batcher.

I'm not much for long-winded tutorials, so the commented code is intended to speak for itself, if you're considering using such commands to begin with.

This includes samples of the following commands:
ACTION_FOR_EACH (some folks still don't get this one)
ACTION_DEFINE_ASSOCIATIVE_ARRAY
ACTION_PHP_EACH
COMPRESS_REPLACE_FILE
DECOMPRESS_REPLACE_FILE

I'll leave stuff like GET_OFFSET_ARRAY for the rest of you to explain, since I don't get what it does (and still don't after reading the latest post).
Code: [Select]
//For each dialogue, change GivePartyGold() to GiveGoldForce() actions
ACTION_FOR_EACH dialog IN ~arkion~ ~ardrou~ ~berrun~ ~brevli~ ~brielb~ ~forthe~ ~gurke~ BEGIN
  ACTION_IF FILE_EXISTS_IN_GAME ~%dialog%.dlg~ BEGIN
    COPY_EXISTING ~%dialog%.dlg~ ~override~
      PATCH_IF SOURCE_SIZE > 0x34 BEGIN
        DECOMPILE_DLG_TO_D
        REPLACE_TEXTUALLY CASE_INSENSITIVE EXACT_MATCH ~GivePartyGold~ ~GiveGoldForce~
        COMPILE_D_TO_DLG
      END
    BUT_ONLY
  END
END

//Defines an array associating creatures with a new amount of gold
ACTION_DEFINE_ASSOCIATIVE_ARRAY newgold BEGIN
  ~arkion~ => 100
  ~ardrou~ => 10
  ~berrun~ => 58
  ~brevli~ => 5
  ~brielb~ => 200
  ~gurke~ => 20
  ~oublek~ => 1000
  ~perdue%~ => 75
  ~pheirk%~ => 38
  ~scar~ => 1026
  ~sonner~ => 150
  ~wenric~ => 170
  ~jarlaxle~ => 25
  ~ppsime~ => 50
  ~yschearl~ => 150
END

//Writes the new amount of gold for each creature above if it exists
ACTION_PHP_EACH newgold AS crename => gold BEGIN
  ACTION_IF FILE_EXISTS_IN_GAME ~%crename%.cre~ BEGIN
    COPY_EXISTING ~%crename%.cre~ ~override~
      PATCH_IF SOURCE_SIZE > 0x2d3 BEGIN
        WRITE_LONG 0x1c gold
      END
    BUT_ONLY
  END
END

//Compress a BAM
COPY ~bambatch/bam~ ~bambatch/bam~
  READ_ASCII 0x0 sg (4) //Signature
  PATCH_IF (~%sg%~ STRING_EQUAL_CASE ~BAM ~ = 1) BEGIN
    dl = SOURCE_SIZE //Data length
    COMPRESS_REPLACE_FILE 0 dl 9
    INSERT_BYTES 0x0 0xc
    WRITE_ASCII 0x0 ~BAMCV1  ~
    WRITE_LONG 0x8 dl
  END ELSE BEGIN
    PATCH_PRINT ~%SOURCE_FILE% is not an uncompressed BAM.~
  END
BUT_ONLY

//Decompress a BAM
COPY ~bambatch/bam~ ~bambatch/bam~
  READ_ASCII 0x0 sg (4) //Signature
  PATCH_IF (~%sg%~ STRING_EQUAL_CASE ~BAMC~ = 1) BEGIN
    READ_LONG 0x8 dl //Uncompressed data length
    DECOMPRESS_REPLACE_FILE 0xc (SOURCE_SIZE - 0xc) dl
  END ELSE BEGIN
    PATCH_PRINT ~%SOURCE_FILE% is not a compressed BAM.~
  END
BUT_ONLY
« Last Edit: September 22, 2009, 07:00:02 AM by Miloch »

Offline the bigg

  • The Avatar of Fighter / Thieves
  • Moderator
  • Planewalker
  • *****
  • Posts: 3804
  • Gender: Male
Re: Sample code for documentation
« Reply #1 on: September 22, 2009, 07:06:37 AM »
Cheers. I'd rather not dump code into the readme without some explaination, but I'm sure somebody will turn that into something formatted.

regarding GET_OFFSET_ARRAY[2]: it's a shorthand for the usual loop-through-a-structure construct, when you don't need to add/remove stuff; unfortunately, it only has a few symbols defined.

COPY_EXISTING_REGEXP ~.*\.itm~ ~override~
  GET_OFFSET_ARRAY headers ITM_V10_HEADERS
  PHP_FOR_EACH headers AS index => beginning BEGIN
      // beginning is the first byte of the index-th extended header
      GET_OFFSET_ARRAY2 effects index ITM_V10_HEAD_EFFECTS
      PHP_FOR_EACH effects AS eff_index => eff_beginning BEGIN
        // eff_beginning is the first byte of the eff_index-th effect belonging to the index-th extended header
      END
    END
  END
BUT_ONLY
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 GeN1e

  • Planewalker
  • *****
  • Posts: 267
  • Gender: Male
Re: Sample code for documentation
« Reply #2 on: September 22, 2009, 10:23:11 AM »
I can write a tutorial on the array stuff, if nobody else wishes to. When v.212 is scheduled to be out?

Offline the bigg

  • The Avatar of Fighter / Thieves
  • Moderator
  • Planewalker
  • *****
  • Posts: 3804
  • Gender: Male
Re: Sample code for documentation
« Reply #3 on: September 22, 2009, 10:30:24 AM »
what's that schedule thing you mention?
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).

 

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