Calling C# from ingame LUA + running external LUA menu

Shout-Out

User Tag List

Results 1 to 15 of 15
  1. #1
    jarjar1's Avatar Sergeant
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    52
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling C# from ingame LUA + running external LUA

    So after loading your C# app into wow with a c loader. Is there a way to run an external .lua script ExecuteScript? And once that script is running to have it call functions inside your C# app. Ex: LUA calling the function that gets your players location or an objects location from the object manager, and using the value in the rest of the LUA script.

    Calling C# from ingame LUA + running external LUA
  2. #2
    Cromon's Avatar Legendary


    Reputation
    840
    Join Date
    Mar 2008
    Posts
    714
    Thanks G/R
    0/7
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes of course that works. I created a managed wrapper that creates metatables and regular tables to expose types from .NET using Reflection. You can export classes, fields, methods which can be called from LUA. You can execute code either through the internal scripting framework of WoW using FrameScript::ExecuteBuffer or you can grab the lua-state of wow and use LUA as usual.

  3. #3
    jarjar1's Avatar Sergeant
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    52
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So loading a 10,000 line LUA script would not break ExecuteBuffer? How would you turn it off and make it stop running?

  4. #4
    Cromon's Avatar Legendary


    Reputation
    840
    Join Date
    Mar 2008
    Posts
    714
    Thanks G/R
    0/7
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by break? Its a function, as long as you are sticking to the rules of the wow lua api wont cause problems.

  5. #5
    Frosttall's Avatar Active Member
    Reputation
    64
    Join Date
    Feb 2011
    Posts
    261
    Thanks G/R
    16/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by jarjar1 View Post
    So loading a 10,000 line LUA script would not break ExecuteBuffer? How would you turn it off and make it stop running?
    Just increase the size of the buffer and you will be fine

  6. #6
    jarjar1's Avatar Sergeant
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    52
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But how do the frameworks like fh-wow or wow-plus do it? They can enable or disable a lua script at will. Im pretty sure there is a function somewhere that wow uses to load addons / lua scripts. And I think to be able to call C# code from the loaded LUA I need to call RegisterFunction or something?

    Ex: I have lua script with calls like Class.GetMyLocation() Class.InteractWithObject() Class.AcceptAll() which are C# funcs in my loaded exe. But I am calling them from the LUA script.

    Wondering how to get that to work.
    Last edited by jarjar1; 10-26-2012 at 03:46 AM.

  7. #7
    Cromon's Avatar Legendary


    Reputation
    840
    Join Date
    Mar 2008
    Posts
    714
    Thanks G/R
    0/7
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do what? I dont get your problem, as we said above there is a function that executes lua code, FrameScript::ExecuteBuffer.

  8. #8
    jarjar1's Avatar Sergeant
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    52
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit: Post on hold, mistyped the offset. Dealing with invalid function pointer currently.

    Edit2: Mm.

    Code:
            public static void PatchInvalidPointer()
            {
                byte[] invptrPatch = {0xEB, 0x1E}; 
                Class1.MemoryIn.Patches.CreateAndApply((IntPtr)(Class1.MemoryIn.ImageBase + 0x16595E), invptrPatch, "InvPtrPatch");
            }
    Now I am wondering how to load an entire .lua script lets say from the WoW folder, and run it almost as running an addon. Is there a function for this? I am sure I should not be parsing a huge .lua file and then calling ExecuteBuffer on it. Because then how would I turn it off?

    There must be a function that gets called when a user loads / unloads an addon or something similar.

    EX: If I can make a function in C# then from ingame call it via like /script Loadscript1(). Would I basically need to have the function read every single line of that script and Load it using ExecuteBuffer (which I think I would have to end up doing) or is there another function that perhaps takes a param like FilePath and loads the specified .lua file and executes it. Meanwhile looking at ArcEmu source, as that seems to be able to deal with loading .lua scripts.

    EDIT2: (thinking now here) Or is it done by calling ExecuteBuffer regardless, and a variable is assigned to the whole script. Like var script1 = StartScript(); And StartScript registers its own ingame timer and goes off. Then when I call /script Unloadscript1() the C# function simply calls ExecuteBuffer that sets var script1 = nil; Would that kill the executing script?
    Last edited by jarjar1; 10-28-2012 at 11:51 PM.

  9. #9
    jarjar1's Avatar Sergeant
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    52
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    51DFC0 lua_load is the winner!

    Code:
    (from ArcEmu)
    success = (0 == lua_load(context->lu /*lua_state*/, readScript, (void*)&cached , fullpath.c_str())) 
    && (0 == lua_pcall(context->lu, 0, 0, 0))
    As I thought. It looks like you need to first load the script then use pcall to call the entry function of it and get it running.

    I don't just understand the purpose of the lua_state tho. Can anyone explain?

    Also ArcEny says the 4th arg is the fullpath but the 4th arg seems to be things like *:OnLeave, *:OnLoad, *:OnStart. I am guessing this is the function to call to start the loaded script?

    EDIT: It seems these are built-in to WOW hence the wildcard. other lua gets loaded with the correct path, ex: @Interface\\FrameXML\\ItemButtonTemplate.lua.
    Last edited by jarjar1; 10-29-2012 at 11:25 AM.

  10. #10
    nerdtopia's Avatar Banned
    Reputation
    4
    Join Date
    Apr 2012
    Posts
    37
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kinda stealing your thread here but how do I perform the opposite of what you're trying to do? I want to invoke the Lua functions from my C# code.

  11. #11
    jarjar1's Avatar Sergeant
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    52
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use ExecuteBuffer and call it as if you would ingame. ex: If you wanna click a button on your custom addon ingame is "/script MyCustomButton:Click()" while the ExecuteBuffer call from C# would be 'MyCustomButton:Click()'

    Then it would execute all relevant code associated with the button click handler in your lua.

    A more indepth way to do it would be as in ArcEmu.

    Code:
    oid lua_engine::BeginLuaFunctionCall(lua_function ref)
    {
    	ReferenceHandler::getReference(lua_state, (ptrdiff_t)ref);
    }
    bool lua_engine::ExecuteLuaFunction(int params, int res, variadic_parameter** results, bool getparams)
    {
    	bool ret = true;
    	lua_State* lu = lua_state;
    	int top = lua_gettop(lu);
    	if(lua_type(lu, top - params) != LUA_TFUNCTION)
    	{
    		ret = false;
    		if(params++ > 0)
    		{
    			for(int i = top; i > 0 && i >= (top - params); i--)
    			{
    				if(!lua_isnone(lu, i))
    					lua_remove(lu, i);
    			}
    		}
    		else
    			lua_pop(lu, 1); //pop the nil pushed by BeginFunctionCall.
    	}
    	else
    	{
    		if(lua_pcall(lu, params, res, 0))
    		{
    			report(lu);
    			ret = false;
    		}
    		//Get result arguments from lua.
    		else if(getparams)
    			*results = luabridge::tdstack<variadic_parameter*>::get(lu, (top - params));
    	}
    	return ret;
    }
    
    void lua_engine::EndLuaFunctionCall(int res)
    {
    	lua_State* lu = lua_state;
    	for(int i = res; i > 0; i--)
    	{
    		if(!lua_isnone(lu, res))
    			lua_remove(lu, res);
    	}
    }
    Last edited by jarjar1; 10-29-2012 at 12:39 PM.

  12. #12
    jarjar1's Avatar Sergeant
    Reputation
    1
    Join Date
    Oct 2012
    Posts
    52
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I am overcomplicating things and this can be accomplished simply by

    Code:
    Parse a string as Lua code and return it as a reference to a function.
    
    func, errorMessage = loadstring("luaCodeBlock"[, "chunkName"]);
    If anyone has done this before would be great if you can share your insight.

    Basically what I am trying to do is run a block of lua code inside a frame, so I can start / stop it at will. EX: Run block3 which creates its own frames for the GUI, then when I set 'func to nil' (or some otherway to unlock block3) it kills all its created frames and timers and registered events. Something like loading your own addons, but without the overhead of having to reload the UI.

    EDIT3: Would then .. work?
    func, errorMessage = loadstring( getcontents(script1.lua) );

    Code:
    /script func:Start();
    /script func:Stop(); func = nil;
    
    script1.lua
    function Start()
        print("hi");
        1frame = frame:create(xxxxxxx);
        2frame = frame:create(xxxxxxx);
    end
    
    function Stop()
        print("bye");
    end
    Last edited by jarjar1; 10-29-2012 at 01:08 PM.

  13. #13
    Bananenbrot's Avatar Contributor
    Reputation
    153
    Join Date
    Nov 2009
    Posts
    384
    Thanks G/R
    1/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by nerdtopia View Post
    Kinda stealing your thread here but how do I perform the opposite of what you're trying to do? I want to invoke the Lua functions from my C# code.
    I really don't mean to hack on you... But http://www.ownedcore.com/forums/worl...nctions-c.html (Executing WoW Lua functions from C#)
    this was two years ago, when there already was sufficient information on calling lua functions. How about a simple search? http://www.google.de/search?q=site%3Aownedcore.com+c%23+lua&oq=site%3Aownedcore.com+c%23+lua&sugexp=c hrome,mod=0&sourceid=chrome&ie=UTF-8 there are plenty of useful results... which were obtaines much faster than by hijacking another semi-related thread...

  14. #14
    nerdtopia's Avatar Banned
    Reputation
    4
    Join Date
    Apr 2012
    Posts
    37
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Bananenbrot View Post
    I really don't mean to hack on you... But http://www.ownedcore.com/forums/worl...nctions-c.html (Executing WoW Lua functions from C#)
    this was two years ago, when there already was sufficient information on calling lua functions. How about a simple search? http://www.google.de/search?q=site%3Aownedcore.com+c%23+lua&oq=site%3Aownedcore.com+c%23+lua&sugexp=c hrome,mod=0&sourceid=chrome&ie=UTF-8 there are plenty of useful results... which were obtaines much faster than by hijacking another semi-related thread...
    Wait, are you saying asking a question invites the ban hammer or Executing WoW Lua functions from C# invites the ban hammer? xDD

    I actually have another project of reverse engineering a Java app so I'll come back to this thread later. Thanks for being so patient with me.

  15. #15
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1627
    Join Date
    May 2010
    Posts
    1,846
    Thanks G/R
    193/539
    Trade Feedback
    16 (100%)
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    They dont normally ban people... Temp ban meh.. some times.. But if you ask questions like "I opened wow with notepad and I dont see any code to hackz", then people like to flip a table... They removed rep for the longest time, but I dont think they do that any more... Lots of people ask searchable questions.... Most users are to lazy to reply with a simple GTFO or googd it.

Similar Threads

  1. Getting this without ingame lua possible?
    By blamani in forum WoW Memory Editing
    Replies: 2
    Last Post: 12-04-2016, 03:58 PM
  2. Use return value from a lua function as a PE condition
    By Filint in forum PE Support forum
    Replies: 3
    Last Post: 11-23-2014, 09:23 PM
  3. Calling Dll's with lua.
    By vondarkness in forum WoW Memory Editing
    Replies: 5
    Last Post: 07-23-2010, 01:19 PM
  4. 5 Badges of Justice from Heroic Mech Run
    By vectorian in forum World of Warcraft Exploits
    Replies: 27
    Last Post: 03-14-2008, 07:06 AM
All times are GMT -5. The time now is 12:42 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search