POC Class to Manage Lua Calls and Return Values menu

User Tag List

Results 1 to 8 of 8
  1. #1
    Xarg0's Avatar Member
    Reputation
    61
    Join Date
    Jan 2008
    Posts
    389
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    POC Class to Manage Lua Calls and Return Values

    I've been a little bit bored today so I decided to finish up an old project I've found on my harddisk.
    Here's the Code, it isn't a complete solution for executing lua functions but you can use it as a starting point if you've no Idea how to retrive lua return values:
    CLua_Manager.h:
    PHP Code:
    #ifndef _H_CLUA_MANAGER_
    #define _H_CLUA_MANAGER_
    #include <vector>
    #include <boost/format.hpp>
    #include "Lua/lua.h"
    #include "WoWDefines.h"
    typedef void (_cdecl *LUA_DOSTRING)(std::stringstd::stringvoid*);
    typedef void (_cdecl *LUA_REGISTER)(std::stringvoid*);
    LUA_DOSTRING lua_DoString reinterpret_cast<LUA_DOSTRING>(gpLuaDoString); 
    LUA_REGISTER lua_Register reinterpret_cast<LUA_REGISTER>(gpLuaRegister);
    int MyCallback (lua_State *L);

    class 
    CLua_Manager{
        private:
            
            
    CLua_Manager();
            ~
    CLua_Manager();
            static 
    CLua_Managersingleton;
        public:
            static 
    CLua_ManagerGet(){if(!singleton){singleton = new CLua_Manager();lua_Register("MyCallback",(void*)MyCallback);return singleton;}return singleton;}
            static 
    void Destroy(){delete singletonsingleton 0;}
            
    std::vector<std::stringvResults;
            
    void DoString(std::strings);
            
    std::string GetResult(int n);
            
    };


    #endif 
    CLua_Manager.cc
    PHP Code:
    #include "CLua_Manager.h"

    int MyCallback (lua_State *L)
    {
          
    //clear the vector if its not empty
        
         
    if ( !CLua_Manager::Get()->vResults.empty() )
            
    CLua_Manager::Get()->vResults.clear();

         
    //get count of returns on stack
         
    int n lua_gettop(L);

         
    //loop to retreive these
         
    for (int i 1;i<=ni++)
            {
                  
    //using lua_tostring to get the result
                  
    const char pszResult lua_tostring(Li);
                  
    //add result to vector
                  
    CLua_Manager::Get()->vResults.push_backpszResult );
                 

            }


    return 
    0;

    }

    std::string CLua_Manager::GetResult(int n){
        try{
            return 
    vResults[n];
        }
        catch(
    std::string e){
            return 
    e;
        }
    }

    void CLua_Manager::DoString(std::strings){
        
    std::string Command = (boost::format("MyCallback(%1%)") % s).str();
        
    lua_DoString(Command.c_str(), Command.c_str(), 0);

    as you can see I don't provide the offsets for the two wow functions I'm using, but I guess you can figure them out yourself.
    I also leave it up to you to bypass the IsFunctionInRange check.

    The Code is quick and dirty but I guess it's enough to make the concept understandable.
    Last edited by Xarg0; 01-21-2010 at 01:31 PM.
    I hacked 127.0.0.1

    These ads disappear when you log in.

  2. #2
    Azzie2k8's Avatar Member
    Reputation
    11
    Join Date
    Apr 2009
    Posts
    190
    Thanks G/R
    0/0
    Trade Feedback
    1 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Although this is not C# it made some things clearer for me thanks alot +Rep

  3. #3
    amadmonk's Avatar Active Member
    Reputation
    124
    Join Date
    Apr 2008
    Posts
    772
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is one of the more common ways to get data back from a call to Lua's DoString: register a callback, and pipe all of your results through the callback.

    The other method is to reverse the DoString function (it's not really that hard) and reimplement it to return values.
    Don't believe everything you think.

  4. #4
    lanman92's Avatar Active Member
    Reputation
    50
    Join Date
    Mar 2007
    Posts
    1,033
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This isn't thread-safe, is it? I see conditions that could happen accessing the vector.

  5. #5
    amadmonk's Avatar Active Member
    Reputation
    124
    Join Date
    Apr 2008
    Posts
    772
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not thread-safe. Of course, for the most part, WoW's Lua engine isn't thread-safe either, so... it's not a huge loss.

    For an example of why it's not thread safe consider:

    1. WoW uses a global Lua state object. It's stored in a static and fairly easy to look up.
    2. You can have Lua processing at almost any time in the render thread (not just during EndScene). I've discovered this to my dismay.

    When you're processing a Lua call in another thread, and you're in between the evaluation of the call and the return of the result to your function, and the render thread signals a framescript event (which uses the global stack), Very Bad Things happen.

    So, in general, I think it's safe to say that WoW's Lua engine is not threadsafe. Some people get away with calling it from another thread, but they're just lucky. Do it hundreds of times a second and you'll very quickly trap.
    Don't believe everything you think.

  6. #6
    tanis2000's Avatar Active Member
    Reputation
    39
    Join Date
    Feb 2009
    Posts
    123
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with madmonk. I've been using LUA calls extensively in my bot lately and it's almost impossible not to end up crashing the client if you call LUA functions at a high rate.

    Have you ever found a way to solve this issue madmonk?

  7. #7
    Xarg0's Avatar Member
    Reputation
    61
    Join Date
    Jan 2008
    Posts
    389
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you're worring about thread safty create a lock around the vector or something, no one prohibited you to change the code as you like
    It's public domain so you can use it however you want
    I hacked 127.0.0.1

  8. #8
    amadmonk's Avatar Active Member
    Reputation
    124
    Join Date
    Apr 2008
    Posts
    772
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by tanis2000 View Post
    I agree with madmonk. I've been using LUA calls extensively in my bot lately and it's almost impossible not to end up crashing the client if you call LUA functions at a high rate.

    Have you ever found a way to solve this issue madmonk?
    Sadly, no. Namreeb and I toyed with the idea of using a custom Lua state, but I'm not sure that this is safe (from a Warden point of view) or feasible (the "constructor" for the Lua state is fairly complex, when you flush out all of the call trees).

    So unfortunately, it appears that we're stuck just trying to not call Lua outside of the bounds of the render thread's EndScene.
    Don't believe everything you think.

Similar Threads

  1. LUA function to return class of target?
    By Dorin85 in forum WoW Bots Questions & Requests
    Replies: 1
    Last Post: 12-01-2012, 06:51 PM
  2. Call lua function and get result
    By starfish99 in forum WoW Memory Editing
    Replies: 4
    Last Post: 12-26-2008, 05:15 AM
  3. Honor's call and quel'sarrar Reskin PLZ
    By demoriel in forum WoW ME Questions and Requests
    Replies: 1
    Last Post: 02-26-2008, 08:48 AM
  4. [Ascent MOD] Reload LUA scripts and Script_bin!
    By Le Froid in forum World of Warcraft Emulator Servers
    Replies: 5
    Last Post: 01-03-2008, 10:29 PM
  5. (Mac) Exit BG's and return to same group 1 min later.
    By ynnuf in forum World of Warcraft Exploits
    Replies: 5
    Last Post: 06-01-2007, 02:11 PM
All times are GMT -5. The time now is 02:23 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2023 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2023 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search