Plugin example code

MicroMacro plugins and plugins only. You can request or share your plugin (either compiled or source code) here.
Post Reply
Message
Author
User avatar
Administrator
Site Admin
Posts: 5313
Joined: Sat Jan 05, 2008 4:21 pm

Plugin example code

#1 Post by Administrator » Mon Mar 10, 2008 3:46 am

Edit: This example is no longer relevant for MicroMacro 1.0 beta 7+.

Code: Select all

extern "C"
{
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
  #include <windows.h>
  #include <string>

  // we use this define just to make our install function look cleaner.
  #define PLUGIN_INSTALL_FUNC      __declspec(dllexport) int __cdecl


  // forward declare our functions
  PLUGIN_INSTALL_FUNC install(lua_State *);
  int testfunc(lua_State *);
  int testfunc2(lua_State *);
  int add(lua_State *);


  // actual function definition
  PLUGIN_INSTALL_FUNC install(lua_State *state)
  {
    lua_register(state, "testfunc", testfunc);
    lua_register(state, "testfunc2", testfunc2);
    lua_register(state, "add", add);
    return 0; // assume there were no errors
  }

  int testfunc(lua_State *state)
  {
    // make sure they haven't supplied any arguments
    if( lua_gettop(state) != 0 ) {
      printf("Wrong number of arguments passed to testfunc()\n");
      return 0; } // we haven't pushed anything, so return 0

    printf("This text is coming from testfunc()\n");
    return 0; // we haven't pushed anything, so return 0
  }

  int testfunc2(lua_State *state)
  {
    // make sure they supplied one argument
    if( lua_gettop(state) != 1 ) {
      printf("Wrong number of arguments passed to testfunc2()\n");
      return 0; } // we haven't pushed anything, so return 0

    if( !lua_isstring(state, 1) )
    { // only accept a string
      printf("Invalid data was passed to testfunc1()\n");
      return 0; // we haven't pushed anything, so return 0
    }

    std::string instr = lua_tostring(state, 1); // get the string they passed in, store it in instr
    printf("Your string is: \"%s\"\n", instr.c_str());
    return 0; // we haven't pushed anything, so return 0
  }

  int add(lua_State *state)
  {
    // make sure they supplied two arguments
    if( lua_gettop(state) != 2 ) {
      printf("Wrong number of arguments passed to add()\n");
      return 0; } // we haven't pushed anything, so return 0

    if( !lua_isnumber(state, 1) || !lua_isnumber(state, 2) )
    { // only accept 2 integers
      printf("Invalid data was passed to add()\n");
      return 0; // we haven't pushed anything, so return 0
    }

    // we've error checked, now get the information from Lua
    int number1 = lua_tonumber(state, 1);
    int number2 = lua_tonumber(state, 2);

    // add the two numbers, and push (return) it onto Lua's stack.
    lua_pushnumber(state, number1 + number2);

    return 1; // we pushed one number, so we return 1.
  }
}
Now you can compile this as a DLL, and place a copy into MicroMacro's plugin directory. It will automatically be loaded when MicroMacro opens.
testfunc() accepts nothing, returns nothing. It simply will display "This text is coming from testfunc()" when called.

testfunc2() accepts 1 string, returns nothing. It will display 'Your string is: "<whatever your string was>"'

add() accepts 2 numbers, and returns one number. Example: somenumber = add(3, 5); somenumber would now contain 8.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests