Last active
August 29, 2015 14:00
-
-
Save sclark39/e54e4da911b554f75578 to your computer and use it in GitHub Desktop.
NS2 Modding Utility function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Script.Load( "lua/Class.lua" ) | |
function DPrint( string ) | |
//Shared.Message( string ) | |
end | |
function Class_AddMethod( className, methodName, method ) | |
if _G[className][methodName] and _G[className][methodName] ~= method then | |
return | |
end | |
_G[className][methodName] = method | |
local classes = Script.GetDerivedClasses(className) | |
assert(classes ~= nil) | |
for _, c in ipairs(classes) do | |
Class_AddMethod(c, methodName, method ) | |
end | |
end | |
local function upvalues( func ) | |
local i = 0; | |
return function() | |
i = i + 1 | |
local name, val = debug.getupvalue (func, i) | |
if name then | |
return i,name,val | |
end -- if | |
end | |
end | |
function PrintUpValues( func ) | |
local vals = nil; | |
for _,name,_ in upvalues( func ) do | |
vals = vals and vals..", "..name or name | |
end | |
Shared.Message( "Upvalues for "..tostring(func)..": local "..vals ); | |
end | |
function LocateUpValue( func, upname, recurse ) | |
for i,name,val in upvalues( func ) do | |
if name == upname then | |
DPrint( "LocateUpValue found "..upname ) | |
return func,i,val | |
end | |
end | |
if recurse then | |
for i,name,innerfunc in upvalues( func ) do | |
if type( innerfunc ) == "function" then | |
local r = { LocateUpValue( innerfunc, upname, recurse ) } | |
if #r > 0 then | |
DPrint( "\ttrace: "..name ) | |
return unpack( r ) | |
end | |
end | |
end | |
end | |
end | |
function GetUpValues( func ) | |
local data = {} | |
for _,name,val in upvalues( func ) do | |
data[name] = val; | |
end | |
return data | |
end | |
function SetUpValues( func, source ) | |
DPrint( "Setting upvalue for "..tostring(func) ) | |
for i,name,val in upvalues( func ) do | |
if source[name] then | |
if val == nil then | |
DPrint( "Setting upvalue "..name.." to "..tostring(source[name]) ) | |
assert( val == nil ) | |
debug.setupvalue( func, i, source[name] ) | |
else | |
DPrint( "Upvalue "..name.." already overwritten by new function" ) | |
end | |
source[name] = nil | |
end | |
end | |
for name,v in pairs( source ) do | |
if v then | |
DPrint( "Upvalue "..name.." was not ported to new function. Was this intentional?" ) | |
end | |
end | |
end | |
function CopyUpValues( dst, src ) | |
SetUpValues( dst, GetUpValues( src ) ) | |
end | |
function ReplaceUpValue( func, localname, newval, options ) | |
local i,val; | |
options = options or {} | |
DPrint( "Replacing upvalue "..localname ) | |
func, i, val = LocateUpValue( func, localname, options.LocateRecurse ); | |
if options.CopyUpValues then | |
CopyUpValues( newval, val ) | |
end | |
debug.setupvalue( func, i, newval ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of usage:
ReplaceUpValue( GUIMinimap.Update, "UpdateStaticBlips", NewUpdateStaticBlips, { LocateRecurse = true; CopyUpValues = true; } )