tdauth / wowr

Warcraft III: Reforged funmap World of Warcraft Reforged.
https://wowreforged.org
8 stars 0 forks source link

Add handle counter for timers and groups etc. with hook #2955

Open tdauth opened 2 months ago

tdauth commented 2 months ago

The main issue are compile errors because of hooks for functions called in globals. Try in a small map if this is even possible.

hook CreateGroup hook DestroyGroup

etc.

It works for GUI global variables since they are initialized in some Blizzard function which is generated:

function InitGlobals takes nothing returns nothing
    set udg_Force=h__CreateForce()
endfunction

but it never works for vJass globals with direct initialization. Hence, initialize ALL vJass globals in the init function instead.

Show the multiboard via "-handles" and "-nohandles".

tdauth commented 1 month ago

library HandleCounters

globals
    private integer totalCounter = 0
    private integer rectCounter = 0
    private integer locationCounter = 0
    private integer timerCounter = 0
    private integer groupCounter = 0
    private integer frameHandleCounter = 0
    private integer triggerCounter = 0
    private integer forceCounter = 0
    private integer unitCounter = 0
endglobals

function GetTotalCounter takes nothing returns integer
    return totalCounter
endfunction

function IncreaseTotalCounter takes nothing returns nothing
    set totalCounter = totalCounter + 1
endfunction

function DecreaseTotalCounter takes nothing returns nothing
    set totalCounter = totalCounter - 1
endfunction

function GetRectCounter takes nothing returns integer
    return rectCounter
endfunction

function IncreaseRectCounter takes nothing returns nothing
    set rectCounter = rectCounter + 1
endfunction

function DecreaseRectCounter takes nothing returns nothing
    set rectCounter = rectCounter - 1
endfunction

function GetLocationCounter takes nothing returns integer
    return locationCounter
endfunction

function IncreaseLocationCounter takes nothing returns nothing
    set locationCounter = locationCounter + 1
endfunction

function DecreaseLocationCounter takes nothing returns nothing
    set locationCounter = locationCounter - 1
endfunction

function GetTimerCounter takes nothing returns integer
    return timerCounter
endfunction

function IncreaseTimerCounter takes nothing returns nothing
    set timerCounter = timerCounter + 1
endfunction

function DecreaseTimerCounter takes nothing returns nothing
    set timerCounter = timerCounter - 1
endfunction

function GetGroupCounter takes nothing returns integer
    return groupCounter
endfunction

function IncreaseGroupCounter takes nothing returns nothing
    set groupCounter = groupCounter + 1
endfunction

function DecreaseGroupCounter takes nothing returns nothing
    set groupCounter = groupCounter - 1
endfunction

function GetFrameHandleCounter takes nothing returns integer
    return frameHandleCounter
endfunction

function IncreaseFrameHandleCounter takes nothing returns nothing
    set frameHandleCounter = frameHandleCounter + 1
endfunction

function DecreaseFrameHandleCounter takes nothing returns nothing
    set frameHandleCounter = frameHandleCounter - 1
endfunction

function GetTriggerCounter takes nothing returns integer
    return triggerCounter
endfunction

function IncreaseTriggerCounter takes nothing returns nothing
    set triggerCounter = triggerCounter + 1
endfunction

function DecreaseTriggerCounter takes nothing returns nothing
    set triggerCounter = triggerCounter - 1
endfunction

function GetForceCounter takes nothing returns integer
    return forceCounter
endfunction

function IncreaseForceCounter takes nothing returns nothing
    set forceCounter = forceCounter + 1
endfunction

function DecreaseForceCounter takes nothing returns nothing
    set forceCounter = forceCounter - 1
endfunction

function GetUnitCounter takes nothing returns integer
    return unitCounter
endfunction

function IncreaseUnitCounter takes nothing returns nothing
    set unitCounter = unitCounter + 1
endfunction

function DecreaseUnitCounter takes nothing returns nothing
    set unitCounter = unitCounter - 1
endfunction

private function HookRect takes real minx, real miny, real maxx, real maxy returns nothing
    call IncreaseTotalCounter()
    call IncreaseRectCounter()
endfunction

private function HookRemoveRect takes rect whichRect returns nothing
    call DecreaseTotalCounter()
    call DecreaseRectCounter()
endfunction

hook Rect HookRect
hook RemoveRect HookRemoveRect

private function HookLocation takes real x, real y returns nothing
    call IncreaseTotalCounter()
    call IncreaseLocationCounter()
endfunction

private function HookRemoveLocation takes location whichLocation returns nothing
    call DecreaseTotalCounter()
    call DecreaseLocationCounter()
endfunction

hook Location HookLocation
hook RemoveLocation HookRemoveLocation

private function HookCreateTimer takes nothing returns nothing
    call IncreaseTotalCounter()
    call IncreaseTimerCounter()
endfunction

private function HookCreateTimerBJ takes boolean periodic, real timeout returns nothing
    call HookCreateTimer()
endfunction

private function HookDestroyTimer takes timer whichTimer returns nothing
    call DecreaseTotalCounter()
    call DecreaseTimerCounter()
endfunction

hook CreateTimer HookCreateTimer
hook CreateTimerBJ HookCreateTimerBJ
hook DestroyTimer HookDestroyTimer
hook DestroyTimerBJ HookDestroyTimer

private function HookCreateGroup takes nothing returns nothing
    call IncreaseTotalCounter()
    call IncreaseGroupCounter()
endfunction

private function HookDestroyGroup takes group whichGroup returns nothing
    call DecreaseTotalCounter()
    call DecreaseGroupCounter()
endfunction

hook CreateGroup HookCreateGroup
hook DestroyGroup HookDestroyGroup

private function HookDestroyFrame takes group whichGroup returns nothing
    call DecreaseTotalCounter()
    call DecreaseFrameHandleCounter()
endfunction

private function HookBlzCreateFrame takes string name, framehandle owner, integer priority, integer createContext returns nothing
    call IncreaseTotalCounter()
    call IncreaseFrameHandleCounter()
endfunction

private function HookBlzCreateSimpleFrame takes string name, framehandle owner, integer createContext returns nothing
    call IncreaseTotalCounter()
    call IncreaseFrameHandleCounter()
endfunction

private function HookBlzCreateFrameByType takes string typeName, string name, framehandle owner, string inherits, integer createContext returns nothing
    call IncreaseTotalCounter()
    call IncreaseFrameHandleCounter()
endfunction

private function HookBlzDestroyFrame takes framehandle frame returns nothing
    call DecreaseTotalCounter()
    call DecreaseFrameHandleCounter()
endfunction

hook BlzCreateFrame HookBlzCreateFrame
hook BlzCreateSimpleFrame HookBlzCreateSimpleFrame
hook BlzCreateSimpleFrame HookBlzCreateFrameByType
hook BlzDestroyFrame HookDestroyFrame

private function HookCreateTrigger takes nothing returns nothing
    call IncreaseTotalCounter()
    call IncreaseTriggerCounter()
endfunction

private function HookDestroyTrigger takes trigger whichTrigger returns nothing
    call DecreaseTotalCounter()
    call DecreaseTriggerCounter()
endfunction

hook CreateTrigger HookCreateTrigger
hook DestroyTrigger HookDestroyTrigger

private function HookCreateForce takes nothing returns nothing
    call IncreaseTotalCounter()
    call IncreaseForceCounter()
endfunction

private function HookGetForceOfPlayer takes player whichPlayer returns nothing
    call HookCreateForce()
endfunction

private function HookGetPlayersByMapControl takes mapcontrol whichControl returns nothing
    call HookCreateForce()
endfunction

private function HookGetPlayersAllies takes player whichPlayer returns nothing
    call HookCreateForce()
endfunction

private function HookGetPlayersEnemies takes player whichPlayer returns nothing
    call HookCreateForce()
endfunction

private function HookGetPlayersMatching takes boolexpr filter returns nothing
    call HookCreateForce()
endfunction

private function HookDestroyForce takes force whichForce returns nothing
    call DecreaseTotalCounter()
    call DecreaseForceCounter()
endfunction

hook CreateForce HookCreateForce
hook GetForceOfPlayer HookGetForceOfPlayer
hook GetPlayersByMapControl HookGetPlayersByMapControl
hook GetPlayersAllies HookGetPlayersAllies
hook GetPlayersEnemies HookGetPlayersEnemies
hook GetPlayersMatching HookGetPlayersMatching
hook DestroyForce HookDestroyForce

private function HookCreateUnit takes player id, integer unitid, real x, real y, real face returns nothing
    call IncreaseTotalCounter()
    call IncreaseUnitCounter()
endfunction

private function HookCreateUnitByName takes player whichPlayer, string unitname, real x, real y, real face returns nothing
    call IncreaseTotalCounter()
    call IncreaseUnitCounter()
endfunction

private function HookCreateUnitAtLoc takes player id, integer unitid, location whichLocation, real face returns nothing
    call IncreaseTotalCounter()
    call IncreaseUnitCounter()
endfunction

private function HookCreateUnitAtLocByName takes player id, string unitname, location whichLocation, real face returns nothing
    call IncreaseTotalCounter()
    call IncreaseUnitCounter()
endfunction

private function HookCreateCorpse takes player whichPlayer, integer unitid, real x, real y, real face returns nothing
    call IncreaseTotalCounter()
    call IncreaseUnitCounter()
endfunction

private function HookRemoveUnit takes unit whichUnit returns nothing
    call DecreaseTotalCounter()
    call DecreaseUnitCounter()
endfunction

hook CreateUnit HookCreateUnit
hook CreateUnitByName HookCreateUnitByName
hook CreateUnitAtLoc HookCreateUnitAtLoc
hook CreateUnitAtLocByName HookCreateUnitAtLocByName
hook CreateCorpse HookCreateCorpse
hook RemoveUnit HookRemoveUnit

endlibrary

library HandleCountersMultiboard initializer Init requires HandleCounters

globals
    private multiboard mb
    private timer t = CreateTimer()
endglobals

function SetHandleCountersMultiboardVisible takes boolean visible returns nothing
    call MultiboardSuppressDisplay(mb, not visible)
endfunction

private function UpdateMultiboard takes nothing returns nothing
    local multiboarditem mbi

    set mbi = MultiboardGetItem(mbi, 0, 0)
    call MultiboardSetItemValue(mbi, GetTotalCounter())
    call MultiboardReleaseItem(mbi)

    set mbi = MultiboardGetItem(mbi, 1, 0)
    call MultiboardSetItemValue(mbi, GetRectCounter())
    call MultiboardReleaseItem(mbi)

    set mbi = MultiboardGetItem(mbi, 2, 0)
    call MultiboardSetItemValue(mbi, GetLocationsCounter())
    call MultiboardReleaseItem(mbi)

    set mbi = MultiboardGetItem(mbi, 3, 0)
    call MultiboardSetItemValue(mbi, GetTimerCounter())
    call MultiboardReleaseItem(mbi)

    set mbi = MultiboardGetItem(mbi, 4, 0)
    call MultiboardSetItemValue(mbi, GetGroupCounter())
    call MultiboardReleaseItem(mbi)

    set mbi = MultiboardGetItem(mbi, 5, 0)
    call MultiboardSetItemValue(mbi, GetFrameHandleCounter())
    call MultiboardReleaseItem(mbi)

    set mbi = MultiboardGetItem(mbi, 6, 0)
    call MultiboardSetItemValue(mbi, GetTriggerCounter())
    call MultiboardReleaseItem(mbi)

    set mbi = MultiboardGetItem(mbi, 7, 0)
    call MultiboardSetItemValue(mbi, GetForceCounter())
    call MultiboardReleaseItem(mbi)
endfunction

private function TimerFunctionStart takes nothing returns nothing
    local multiboarditem mbi

    set mb = CreateMultiboard()
    call MultiboardSetTitleText(mb, "Handle Counters")
    call MultiboardSetColumnCount(mb, 2)
    call MultiboardSetRowCount(mb, 8)
    call MultiboardSetItemsStyle(mb, true, false)
    call MultiboardSetItemsWidth(mb, 0.03)

    // total
    set mbi = MultiboardGetItem(mbi, 0, 0)
    call MultiboardSetItemValue(mbi, "Total:")
    call MultiboardReleaseItem(mbi)

    // rects
    set mbi = MultiboardGetItem(mbi, 1, 0)
    call MultiboardSetItemValue(mbi, "Rects:")
    call MultiboardReleaseItem(mbi)

    // locations
    set mbi = MultiboardGetItem(mbi, 2, 0)
    call MultiboardSetItemValue(mbi, "Locations:")
    call MultiboardReleaseItem(mbi)

    // timers
    set mbi = MultiboardGetItem(mbi, 3, 0)
    call MultiboardSetItemValue(mbi, "Timers:")
    call MultiboardReleaseItem(mbi)

    // group
    set mbi = MultiboardGetItem(mbi, 4, 0)
    call MultiboardSetItemValue(mbi, "Groups:")
    call MultiboardReleaseItem(mbi)

    // framehandles
    set mbi = MultiboardGetItem(mbi, 5, 0)
    call MultiboardSetItemValue(mbi, "Frame Handles:")
    call MultiboardReleaseItem(mbi)

    // triggers
    set mbi = MultiboardGetItem(mbi, 6, 0)
    call MultiboardSetItemValue(mbi, "Triggers:")
    call MultiboardReleaseItem(mbi)

    // forces
    set mbi = MultiboardGetItem(mbi, 7, 0)
    call MultiboardSetItemValue(mbi, "Forces:")
    call MultiboardReleaseItem(mbi)

    call TimerStart(t, 0.03, true, function UpdateMultiboard)

    call PauseTimer(GetExpiredTimer())
    call DestroyTimer(GetExpiredTimer())
endfunction

private function Init takes nothing returns nothing
    call TimerStart(CreateTimer(), 0.0, false, function TimerFunctionStart)
endfunction

endlibrary
``