ArrayDeepDump

From the Oblivion ConstructionSet Wiki
Revision as of 09:10, 17 August 2009 by imported>QQuix (Fixed a bug in the code)
Jump to navigation Jump to search

A User Function for use with Oblivion Script Extender

Syntax:

ArrayDeepDump Arrayarray, ArrayNamestring, Modeshort, Depthshort 

Dumps the array to the console and to a text file . The arguments are:

  • Array - the array to dump
  • ArrayName - A name for the header line
  • Mode - output format 1 or 2 (see examples below)
  • Number of levels to dump (0 = All)

Notes

OBSE has a great debugging function to dump an array contents to the console: ar_Dump

This User Functions has the same debugging purpose as ar_Dump, but intended to show the contents of nested arrays, or arrays within arrays.

It dumps the array to the console and to a text file. Either can be easily removed, if you prefer. It is also easy to customize the contents and formatting to your liking.

The text file output requires Pluggy. If you don't use it, remove the lines marked with "#Pluggy#"

It has two output formats determined by the Mode argument (1 or 2).

The function is recursive, therefore limited to OBSE's maximum of 30 nested function calls.

There is a Catch 22 when you first compile a recursive function: the compiler will give an error at the line where the function calls itself because no such function exists yet. So I commented that line. After the first successful compilation, you should uncomment that line.

Examples:

Mode 1

Shows the Keys within brackets, followed by: #Array ID and array size (if an array) or the contents

Call ArrayDeepDump MyArray,"MyArrayName", 1, 5   ;== Mode 1 - 5 levels deep ==
===== Deep dump Mode 1 - Array: [ObjectTree] =====
[Architecture]  #410  2
= [Anvil]  #415  3
= = [AnvilFightersGuild01]  #416  3
= = = [EditorID] = 'AnvilFightersGuild01'
= = = [FormID] = 000496C5 <no name>
= = = [Size] = 22
= = [AnvilHouseMC01]  #417  0
= = [AnvilLighthouse01]  #418  0
= [Chorrol]  #419  1
= = [Interior]  #420  1
= = = [ChorrolHouseMiddle03Interior]  #421  3
= = = = [EditorID] = 'ChorrolHouseMiddle03Interior'
= = = = [FormID] = 0002B75F <no name>
= = = = [Size] = 11
[Clutter]  #414  0

Mode 2

Shows one line for every end-of branch (an array entry that is not another array)

Call ArrayDeepDump MyArray,"MyArrayName", 2, 0   ;== Mode 2 - All levels ==
===== Deep dump Mode 2 - Array: [ObjectTree] =====
[Architecture][Anvil][AnvilFightersGuild01][EditorID] = 'AnvilFightersGuild01'
[Architecture][Anvil][AnvilFightersGuild01][FormID] = 000496C5 <no name>
[Architecture][Anvil][AnvilFightersGuild01][Size] = 22
[Architecture][Anvil][AnvilHouseMC01] = #Empty#
[Architecture][Anvil][AnvilLighthouse01] = #Empty#
[Architecture][Chorrol][Interior][ChorrolHouseMiddle03Interior][EditorID] =  ChorrolHouseMiddle03Interior'
[Architecture][Chorrol][Interior][ChorrolHouseMiddle03Interior][FormID] = 0002B75F <no name>
[Architecture][Chorrol][Interior][ChorrolHouseMiddle03Interior][Size] = 11
[Clutter] = #Empty#

Code

scriptname ArrayDeepDump

array_var iaArray
string_var isArrayName
short iMode
short iDepth
ref xRef
array_var aArray
array_var aEntry
array_var aTemp
string_var sPrefix
string_var sKey1
string_var sType
string_var sText
string_var sLine
short xMode
short xDepth
short xLevel
short xNextLevel
short xxNum
;=== Pluggy string vars ===
long psFile 
long psLine

begin Function {iaArray, isArrayName, iMode, iDepth} 
  set xDepth to iDepth
  if xDepth == 0
    set xDepth to 999
  endif

  if iMode < 100
    set xMode to iMode   ; idetifies recursive call
    set xLevel to 0
  elseif iMode < 200
    set xMode to 1
    set xLevel to iMode - 100
  else
    set xMode to 2
    set xLevel to iMode - 200
  endif
  
  set psFile to CreateString -1 "MyMod - dump.log" 1 1   ;#Pluggy#
  set psLine to CreateString -1 0  1 1                   ;#Pluggy#
  
  if xLevel == 0
    ;=== Print Header ===
    ;====================
    let sLine :=  "===== Deep dump mode " + ( tostring iMode ) + " - Depth= " + ( tostring iDepth ) + " - Array: [" + isArrayName + "] ====="
                              printc "PrintArray>> %z " sLine
                              setString psLine $sLine         ;#Pluggy#
                              StringToTxtFile psFile  psLine  ;#Pluggy#
  endif

  ;=== Setup array and prefix ===
  ;==============================
  let aArray := iaArray
  let sPrefix := ""
  if xMode  == 1
    set xxNum to xLevel
    while xxNum > 0
      let sPrefix := sPrefix + "= "
      let xxNum := xxNum - 1
    loop
  elseif xLevel > 0
    let sPrefix := isArrayName
  endif

  if  0 == ar_size aArray  
    ;=== Empty array ===
    ;===================
    if iMode  == 2
      let sLine := sPrefix + " = " + "#EmptyArray#"
                              printc "PrintArray>> %z " sLine
                              setString psLine $sLine         ;#Pluggy#
                              StringToTxtFile psFile  psLine  ;#Pluggy#
    endif
  else
    foreach aEntry <- aArray
      ;=== Get key ===
      ;===============
      let sType := typeof aEntry [Key]  
      if eval (sType == "String")
        let sKey1 := "[" + aEntry [Key] + "]" 
      else  
        let sKey1 := "[" + ( tostring aEntry [Key] ) + "]" 
      endif

      ;=== Get value ===
      ;=================
      let sType := typeof aEntry [Value]  

      if eval (sType == "String")
        ;=== Print string value ===
        ;==========================
        let sLine := sPrefix + sKey1 + " = '" + aEntry [Value] + "'"
                              printc "PrintArray>> %z " sLine
                              setString psLine $sLine         ;#Pluggy#
                              StringToTxtFile psFile  psLine  ;#Pluggy#
      elseif eval (sType == "Number")
        ;=== Print number value ===
        ;==========================
        let sLine := sPrefix + sKey1 + " = " + tostring aEntry [Value] 
                              printc "PrintArray>> %z " sLine
                              setString psLine $sLine         ;#Pluggy#
                              StringToTxtFile psFile  psLine  ;#Pluggy#
      elseif eval (sType == "Form")
        ;=== Print form value ===
        ;========================
        let xRef := aEntry [Value]
        set sText to sv_Construct "%i %n" xRef xRef 
        let sLine := sPrefix + sKey1 + " = " + sText
                              printc "PrintArray>> %z " sLine
                              setString psLine $sLine         ;#Pluggy#
                              StringToTxtFile psFile  psLine  ;#Pluggy#
      else
        ;=== Print array contents ===
        ;============================
        if xMode  == 1
          let aTemp := aEntry[Value]
          let sLine := sPrefix + sKey1 + "   #" + sv_construct "%g" aTemp + "  " + ( tostring ar_size ( aEntry [Value] ) )  
                             printc "PrintArray>> %z " sLine
                             setString psLine $sLine         ;#Pluggy#
                             StringToTxtFile psFile  psLine  ;#Pluggy#
        endif

        if xDepth > 1     ; stops recursion at the last level initialized
          set xNextLevel to xMode * 100 + xLevel + 1
          let sText := sPrefix + sKey1
;;;; Uncomment the next line after the first successful compilation
;;;;      call ArrayDeepDump aEntry [Value] , sText , xNextLevel, xDepth - 1
        elseif xDepth == 1  && xMode  == 2
          let sLine := sPrefix + sKey1 + " . . ."
                              printc "PrintArray>> %z " sLine
                              setString psLine $sLine         ;#Pluggy#
                              StringToTxtFile psFile  psLine  ;#Pluggy#
        endif
      endif
    loop

  endif
end

See Also