# log-to-sym.ps1 v2
# https://gist.github.com/nanaian/8e2faf8909d15ad393b0802c7aa60bf7
#
# compile.log to Project 64 symbols file script
# by alex (imalex.xyz)
#
# usage:
#   place this script in the $mod/logs directory
#   compile with Star Rod as usual
#   run this script (`.\log-to-sym.ps1` in PowerShell) and a PAPER MARIO.sym file will be produced

New-Item "PAPER MARIO.sym" -ItemType File -Force
Clear-Content "PAPER MARIO.sym"

function New-Symbol {
    $ptr = $args[0]
    $addr = $args[1]
    $description = $args[2]

    $type = 'data'
    if ($ptr -like '$Function*') {
        $type = 'code'
    }

    Add-Content "PAPER MARIO.sym" "$addr,$type,$ptr,$description"
}

$map_section = 'Executing patch: ([a-zA-Z0-9_]+).mpat'
$map_ptr     = '(\$[^ ]+) will be placed at (8024[0-9A-F]{4})'
$active_map  = ''

$global_ptr  = 'Writing global struct (\$[^ ]+) to (80[0-9A-F]{6})'
$global2_ptr = '(80[0-9A-F]{6}) = (\$Global_[^ ]+)'
$other_ptr   = '(Version information) will be stored at 0x(80[0-9A-F]{6})'

Get-Content compile.log | ForEach-Object {
    if ($_ -match $map_section) {
        $active_map = $matches[1]
    }
    if ($_ -match $map_section) {
        $active_map = $matches[1]
    }
    if ($_ -match $map_ptr) {
        New-Symbol $matches[1] $matches[2] "Map: $active_map"
    }

    if ($_ -match $global_ptr) {
        New-Symbol $matches[1] $matches[2] 'Globally loaded'
    }
    if ($_ -match $global2_ptr) {
        New-Symbol $matches[2] $matches[1] 'Globally loaded'
    }
    if ($_ -match $other_ptr) {
        New-Symbol $matches[1] $matches[2] 'Constant'
    }
}