HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Caster (I2H)

12-08-2009, 04:28 AM#1
weaaddar
Caster is a library to facilitate typecasting and move old code over to new system without great deals of effort and clean syntax. It can also be used in modern maps that are okay with suffering performance penalties, like for test code.

Expand Zinc:

Here is some old DT4a A4 code to show how to replace old syntax with new cleaner syntax that might inline and won't be horrible::
Collapse JASS:
function H2I takes handle h returns integer
    return h
    return 0
endfunction

function Hero_getBag takes unit hero returns item
    return GetStoredInteger(udg_gc,I2S(H2I(hero)),"m_bag")
    return null
endfunction

function Hero_getHolder takes unit hero returns unit
    return GetStoredInteger(udg_gc,I2S(H2I(hero)),"m_holder")
    return null
endfunction
Translates to
Collapse JASS:
function H2I takes handle h returns integer
    return GetHandleId(h)
endfunction

function Hero_getBag takes unit hero returns item
    return CastInt[GetStoredInteger(udg_gc,I2S(H2I(hero)),"m_bag")].as_item
endfunction

function Hero_getHolder takes unit hero returns unit
    return CastInt[GetStoredInteger(udg_gc,I2S(H2I(hero)),"m_holder")].as_unit
endfunction

Which compiles to::
Collapse Zinc:

function Hero_getBag takes unit hero returns item
    return (LoadItemHandle(Caster__Table, 0, (s__CastInt__staticgetindex(GetStoredInteger(udg_gc, I2S((GetHandleId((hero)))), "m_bag"))))) // INLINED!!
endfunction

function Hero_getHolder takes unit hero returns unit
    return (LoadUnitHandle(Caster__Table, 0, (s__CastInt__staticgetindex(GetStoredInteger(udg_gc, I2S((GetHandleId((hero)))), "m_holder"))))) // INLINED!!
endfunction
Not so bad really.

This is probably the easiest way to translate all old abusers and if you are using speed mode it'll translate pretty cleanly.
12-08-2009, 05:32 AM#2
Rising_Dusk
We can't endorse I2H in the database... We are trying to move people away from it, not give them a reason to keep using it. I can move this to the T&S forum and leave it there as a link for people who need it, but I really can't let this slip into the script database like some supported library for reasons I hope are obvious.
12-08-2009, 06:49 AM#3
weaaddar
I agree. I'm using it as a clutch to help translate an old map. Its a debug script and obviously not something you should be using for a resource, but if you want an old map to work without much pain this is better then recoding to use modern facilities like hashtables, GetHandleId, and structs.

You can move to T&S if you must.
12-08-2009, 01:06 PM#4
Rising_Dusk
Okay cool, glad we agreed. Moved. :)