Jump to content

CallHack - Easifies calling exported functions


Guest SinnerG

Please rate this code!  

1 member has voted

  1. 1. Please rate this code!

    • 5 - Excellent!
      1
    • 4 - Good
      0
    • 3 - Average
      0
    • 2 - Bad
      0
    • 1 - Very bad
      0


Recommended Posts

Hello all,

I'm releasing the following under the LGPL license.

Using CallHack you can easifiy access to other resources their exported functions.

Example usage when using the scoreboard resource:

  
resources.scoreboard:addScoreboardColumn("Score") 
-- Or 
call.scoreboard:addScoreboardColumn("Score") 
-- Or (regular, still works with CallHack loaded) 
call(getResourceFromName("scoreboard"), "addScoreboardColumn", "Score") 
  

You must include the CallHack.lua file to your meta.xml and give it both to client & server.

--[[ 
CallHack.lua - Easifies calling exported functions from other resources. 
Copyright (C) 2008 Tim Mylemans 
  
This library is free software; you can redistribute it and/or 
modify it under the terms of the GNU Lesser General Public 
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version. 
  
This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
Lesser General Public License for more details. 
  
You should have received a copy of the GNU Lesser General Public 
License along with this library; if not, write to the Free Software 
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
]] 
  
--[[ 
Export Wrapper (CallHack) 
  
Easifies calling exported functions from other resources. 
  
Example usage: 
    local maps = resources.mapmanager:getMaps() 
Alternative usage: 
    local maps = resources.mapmanager.getMaps() 
  
For readability, the first usage is advised.  
  
Also replaces call with a compatible replacement. 
Meaning you can use call.mapmanger:getMaps() as well while still using the old call() syntax,  
but please us the resources replacement, as the call replacement might get removed/altered in  
the future. 
  
To use this feature, add the following to your meta.xml file 
  
    <script src="CallHack.lua" />    
    <script src="CallHack" type="client" /> 
]] 
  
-- COPY FUNCTION REFERENCE 
local _call = call 
  
-- CREATE THE METATABLE 
local call_mt = {} 
  
-- ADD FUNCTIONS 
function call_mt.__index(_table, resource) 
    local ret = {} 
    local ret_mt = {} 
    ret.resourceName = resource 
    function ret_mt.__index(_table, _function) 
        local self = _table 
        local _call_handler 
         
        self.functionName = _function 
         
        function _call_handler(arg1, ...)                    
            if (arg1 == _table) then 
                -- Called by using call.resource:function() instead of call.resource.function() 
                self = arg1 
                return _call(getResourceFromName(self.resourceName), self.functionName, ...) 
            end 
             
            return _call(getResourceFromName(self.resourceName), self.functionName, arg1, ...) 
        end 
         
        return _call_handler -- Return  
    end  
     
    setmetatable(ret, ret_mt) 
    return ret 
end 
  
function call_mt.__call(func, theResource, theFunction, ...) 
    if ((func == call) or (func == resources)) then 
        return _call(theResource, theFunction, ...) 
    end  
    return nil 
end 
  
-- Assign the variables 
call = {} 
setmetatable(call, call_mt) 
resources = call 

Documentation:

Export Wrapper (CallHack)

Easifies calling exported functions from other resources.

Example usage:

local maps = resources.mapmanager:getMaps()

Alternative usage:

local maps = resources.mapmanager.getMaps()

For readability, the first usage is advised.

Also replaces call with a compatible replacement.

Meaning you can use call.mapmanger:getMaps() as well while still using the old call() syntax,

but please us the resources replacement, as the call replacement might get removed/altered in

the future.

To use this feature, add the following to your meta.xml file

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...