Things you may not know.

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

  1. Mkalo

    Mkalo ボーカロイド Support Team

    Joined:
    Jun 1, 2011
    Messages:
    1,117
    Likes Received:
    879
    Best Answers:
    53
    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 (Text):
    1. COUNTER = 1
    2.  
    3. function toTable(object, name)
    4.     if not name then
    5.         name = COUNTER
    6.         COUNTER = COUNTER+1
    7.     end
    8.     _G["__" .. name] = _G["__" .. name] or {__name = name}
    9.     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})
    10.     return object
    11. end
    12.  
    13. function getTable(object)
    14.     if object.__name and _G["__" .. object.__name] then
    15.         return _G["__" .. object.__name]
    16.     end
    17. end
    18.  
    19. local a = 10
    20. toTable(a)
    21. debug.getmetatable(a).__call = function() print("Why are you calling a number value? XD") end
    22. a[1] = 2
    23. a[2] = 4
    24. a[3] = 6
    25.  
    26. print(a) -- 10
    27. print(a[1], a[2], a[3]) -- 2    4    6
    28. print(table.concat(getTable(a), ", ")) -- 2, 4, 6
    29. 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:
    390
    Likes Received:
    111
    Best Answers:
    0
    Very cool, I like to set new call and newindex behaviours they are very useful
     
  3. Mkalo

    Mkalo ボーカロイド Support Team

    Joined:
    Jun 1, 2011
    Messages:
    1,117
    Likes Received:
    879
    Best Answers:
    53
    Code (Text):
    1. print("Recursive:");
    2. (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});
    3. print("For loop:");
    4. (function(t) for i = 1, #t do print(t[i]) end end)({1, 2, 3, 4, 5});
    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. Mkalo

    Mkalo ボーカロイド Support Team

    Joined:
    Jun 1, 2011
    Messages:
    1,117
    Likes Received:
    879
    Best Answers:
    53
    How to see every function call in your server: (Works for any version, this is Lua debug functions.)
    Code (Text):
    1. local ignore = {"=%[C%]", "@data/lib/.+"}
    2. function isInPatternArray(str, array)
    3.    for i, x in pairs(array) do
    4.      if str:match(x) then
    5.        return true
    6.      end
    7.    end
    8.  
    9.    return false
    10. end
    11.  
    12. debug.sethook(function()
    13.    local info = debug.getinfo(2)
    14.    if info and info.source and not isInPatternArray(info.source, ignore) then
    15.      if info.source == "=[C]" then
    16.        print(info.name or "=[C]")
    17.      else
    18.        print(info.source)
    19.      end
    20.    end
    21. 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:
    179
    Likes Received:
    13
    Best Answers:
    2
    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:
    10
    Likes Received:
    0
    Best Answers:
    0
    me too :/
     
  7. Mkalo

    Mkalo ボーカロイド Support Team

    Joined:
    Jun 1, 2011
    Messages:
    1,117
    Likes Received:
    879
    Best Answers:
    53
    Again, thats why I placed the "ignore" table. You should add patterns that you want to ignore to avoid spam.
    Code (Text):
    1. 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:
    179
    Likes Received:
    13
    Best Answers:
    2
    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
    Likes Received:
    1
    Best Answers:
    0
    Okay, call me a noob :p
    Where should i put this script? It seems pretys usefull
     
  10. Mkalo

    Mkalo ボーカロイド Support Team

    Joined:
    Jun 1, 2011
    Messages:
    1,117
    Likes Received:
    879
    Best Answers:
    53
    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

Loading...