Things you may not know.

Discussion in 'Mods & Lua Functions' started by MatheusMkalo, Feb 25, 2016.

  1. MatheusMkalo

    MatheusMkalo ボーカロイド

    Joined:
    Jun 1, 2011
    Messages:
    965
    If you know about debug.setmetatable, you can leave now. If you didn't here it is some nice things you can do with it:
    Code:
    COUNTER = 1
    
    function toTable(object, name)
        if not name then
            name = COUNTER
            COUNTER = COUNTER+1
        end
        _G["__" .. name] = _G["__" .. name] or {__name = name}
        debug.setmetatable(object, {__newindex = function (a, b, c) rawset(_G["__" .. name], b, c) end, __index = function(a, b) return rawget(_G["__" .. name], b) end, __len = function() return #_G["__" .. name] end})
        return object
    end
    
    function getTable(object)
        if object.__name and _G["__" .. object.__name] then
            return _G["__" .. object.__name]
        end
    end
    
    local a = 10
    toTable(a)
    debug.getmetatable(a).__call = function() print("Why are you calling a number value? XD") end
    a[1] = 2
    a[2] = 4
    a[3] = 6
    
    print(a) -- 10
    print(a[1], a[2], a[3]) -- 2    4    6
    print(table.concat(getTable(a), ", ")) -- 2, 4, 6
    a() -- Why are you calling a number value? XD
    That's it bye.
     
    Critico, snowsz and Marcelo Druida like this.
  2. Marcelo Druida

    Marcelo Druida Active Member

    Joined:
    Nov 17, 2014
    Messages:
    373
    Very cool, I like to set new call and newindex behaviours they are very useful
     
  3. MatheusMkalo

    MatheusMkalo ボーカロイド

    Joined:
    Jun 1, 2011
    Messages:
    965
    Code:
    print("Recursive:");
    (function(t) local function recursive(i) if t[i] then print(t[i]) return recursive(i+1) end end recursive(1) end)({1, 2, 3, 4, 5});
    print("For loop:");
    (function(t) for i = 1, #t do print(t[i]) end end)({1, 2, 3, 4, 5});
    (function(i, m) if i <= m then print(i) return debug.getinfo(1).func(i + 1, m) end end )(1, 10);
    Thats actually pretty cool and yes its Lua :|
     
    Last edited: May 1, 2016
  4. MatheusMkalo

    MatheusMkalo ボーカロイド

    Joined:
    Jun 1, 2011
    Messages:
    965
    How to see every function call in your server: (Works for any version, this is Lua debug functions.)
    Code:
    local ignore = {"=%[C%]", "@data/lib/.+"}
    function isInPatternArray(str, array)
       for i, x in pairs(array) do
         if str:match(x) then
           return true
         end
       end
    
       return false
    end
    
    debug.sethook(function()
       local info = debug.getinfo(2)
       if info and info.source and not isInPatternArray(info.source, ignore) then
         if info.source == "=[C]" then
           print(info.name or "=[C]")
         else
           print(info.source)
         end
       end
    end, "c")
    In some place of your lib files or a new lib file whatever will do.
    What you will see:
    [​IMG]
    With this you can see everything I did while online.

    Did some changes to print the name of C functions however I would let those ignoreds however if you remove it from the table of ignored you will see this:
    http://pastebin.com/bMAHaeRN

    And thats just from me logging in and out, so not that good :)
     
    Last edited: May 6, 2016
    milbradt, duduprec, kito2 and 9 others like this.
  5. Zoe Skandinavia

    Zoe Skandinavia Member

    Joined:
    Jul 18, 2014
    Messages:
    159
    Im testing it but when i open the server, in console appears too many prints about npc's with endless. How can i fix it?
     
  6. Kvn007

    Kvn007 New Member

    Joined:
    Aug 20, 2009
    Messages:
    3
    me too :/
     
  7. MatheusMkalo

    MatheusMkalo ボーカロイド

    Joined:
    Jun 1, 2011
    Messages:
    965
    Again, thats why I placed the "ignore" table. You should add patterns that you want to ignore to avoid spam.
    Code:
    local ignore = {"=%[C%]", "@data/lib/.+"}
    Thats ignoring C function calls and lib function calls.
     
    Kvn007 likes this.
  8. Zoe Skandinavia

    Zoe Skandinavia Member

    Joined:
    Jul 18, 2014
    Messages:
    159
    Yes, i could solve it minutes later. Any way, thanks.
     
  9. Vanisto 8.0

    Vanisto 8.0 New Member

    Joined:
    Sep 5, 2015
    Messages:
    23
    Okay, call me a noob :p
    Where should i put this script? It seems pretys usefull
     
  10. MatheusMkalo

    MatheusMkalo ボーカロイド

    Joined:
    Jun 1, 2011
    Messages:
    965
    This script is just to show a neat usage of closures and debug.getinfo. This script is useless, just going to print stuff.
     

Share This Page