Home - Forums - Documentation - Gallery - Bugs
m (work in progress 3)
Current revision (19:03, 14 May 2008) (edit) (undo)
 
(8 intermediate revisions not shown.)
Line 9: Line 9:
A note about the history of this plugin.
A note about the history of this plugin.
-
The source itself is just partially my work. The original code written by Brandon Ehle in 1999. I started to update the code, but the greater part of the code written by Mildred. Many thanks, Mildred!
+
The source itself is just partially my work. The original code written by Brandon Ehle in 1999. I started to update the code, but the greater part of the code written by Mildred. Many thanks for her efforts.
-
 
+
== Setup your enviroment==
== Setup your enviroment==
-
The very first step is download, complie and install LUA. You can get it from the official site:
+
The very first step is download, compile and install LUA. You can get it from the official site:
[http://www.lua.org/ LUA homepage]
[http://www.lua.org/ LUA homepage]
Line 20: Line 19:
This tutorial uses the LUA 5.1 version.
This tutorial uses the LUA 5.1 version.
-
The next step is setting up an external CS project within your enviroment. Don't forget to set up your project as a dynamic link library, and the neccessary CS and LUA libs and include director, . The concrete steps depending from your IDE and operating system. ( This is a so called "professional" tutorial. Professional tutorials are not just for C++ masters , but I assume, you can set up this project without any external help.)
+
The next step is setting up an external CS project within your enviroment. Don't forget to set up your project as a dynamic link library, and the neccessary CS and LUA libs and include directories . The concrete steps depending from your IDE and operating system. ( This is a so called ''professional'' tutorial. Professional tutorials are not just for C++ masters , but I assume, you can set up this project without any external help.)
Line 33: Line 32:
#include <csutil/csinput.h>
#include <csutil/csinput.h>
#include <iutil/string.h>
#include <iutil/string.h>
 +
 +
#include "lua.h"
</pre>
</pre>
-
Then we introduce a new namespace to avoid name conflicts, in Crystal Space way:
+
Then we introduce a new namespace to avoid name conflicts, in Crystal Space style:
<pre>
<pre>
CS_PLUGIN_NAMESPACE_BEGIN(cslua) {
CS_PLUGIN_NAMESPACE_BEGIN(cslua) {
</pre>
</pre>
-
This macro creates for us a new cslua namespace.
+
This macro creates for us a new cslua namespace. The namespace will be closed with the
 +
<pre>
 +
CS_PLUGIN_NAMESPACE_END(cslua)
 +
</pre>
 +
macro.
 +
 
 +
Tha ''csLua'' scf class will encapsulate the lua related stuff.
 +
 
 +
<pre>
 +
class csLua: public scfImplementation2<csLua, iScript, iComponent> {
 +
 
 +
public:
 +
csLua(iBase* iParent);
 +
virtual ~csLua();
 +
 
 +
static csLua* shared_instance;
 +
iObjectRegistry* object_reg;
 +
 
 +
lua_State* L;
 +
int varTable;
 +
</pre>
 +
 
 +
First we define the constructor and the virtual destructor. The shared_instance is a static ''csLua'' instance, this object will be used by ''Crystal Space''. We need a know about the ObjectRegistry, object_reg will store a pointer to this. The ''lua_State*'' is a special statment indicator, when you knows Lua, yo know, what is it.
 +
 
 +
The public filed continues with functions:
 +
 
 +
<pre>
 +
virtual bool Initialize(iObjectRegistry* object_reg);
 +
virtual bool HandleEvent(iEvent&);
 +
</pre>
 +
 
 +
The ''Initialize'' function will warm up the plugin. The ''HandleEvent'' function will deal with the plugin-related events (these are spcial events, like the command line help event.
 +
 
 +
Here comes the ''iScript'' interface functions:
 +
 
 +
The ''iScript'' allows us to run text form a char arrray (''RunTex'' or from file(LoadModule):
 +
 +
<pre>
 +
virtual bool RunText(const char *Text);
 +
 
 +
virtual bool LoadModule(const char *name);
 +
virtual bool LoadModule (const char *path, const char *name);
 +
</pre>
 +
 
 +
Then provides us many Store function to store different types of variables.
 +
<pre>
 +
virtual bool Store(const char *name, char const *data);
 +
virtual bool Store(const char *name, iScriptObject *data);
 +
virtual bool Store(const char *name, double data);
 +
virtual bool Store(const char *name, int data)
 +
{ return Store(name, static_cast<double>(data)); }
 +
virtual bool Store(const char *name, float data)
 +
{ return Store(name, static_cast<double>(data)); }
 +
virtual bool Store(const char* name, void* data, void* tag)
 +
{ return false; } /* deprecated */
 +
</pre>
 +
 
 +
The following functions read the stored varibles value by name:
 +
 
 +
<pre>
 +
virtual bool Retrieve(const char *name, csRef<iString> &data) const;
 +
 +
virtual bool Retrieve(const char *name, csRef<iScriptObject> &data) const
 +
{ return false; }
 +
virtual bool Retrieve(const char *name, double &data) const;
 +
virtual bool Retrieve(const char *name, int &data) const
 +
{ double n; bool res = Retrieve(name, n); data = static_cast<int>(n); return res; }
 +
virtual bool Retrieve(const char *name, float &data) const
 +
{ double n; bool res = Retrieve(name, n); data = static_cast<float>(n); return res; }
 +
 
 +
</pre>
 +
 
 +
Next ''iScript'' ensures us many ''Call()'' to call stored functions:
 +
<pre>
 +
virtual bool Call(const char *name, const char *fmt, ...)
 +
{ return false; } /* TODO */
 +
virtual bool Call(const char *name, double &ret, const char *fmt, ...)
 +
{ return false; } /* TODO */
 +
virtual bool Call(const char *name, int &ret, const char *fmt, ...)
 +
{ return false; } /* TODO */
 +
virtual bool Call(const char *name, float &ret, const char *fmt, ...)
 +
{ return false; } /* TODO */
 +
virtual bool Call(const char *name, csRef<iString> &ref, const char *fmt, ...)
 +
{ return false; } /* TODO */
 +
virtual bool Call(const char *name, csRef<iScriptObject> &ref, const char *fmt, ...)
 +
{ return false; } /* TODO */
 +
</pre>
 +
 
 +
And finally some special funtion for objects and bool variables :
 +
<pre>
 +
virtual csRef<iScriptObject> NewObject(const char *type, const char *fmt, ...)
 +
{ return 0; } /* TODO */
 +
 
 +
virtual bool SetTruth(const char *name, bool data);
 +
virtual bool GetTruth(const char *name, bool &data) const;
 +
virtual bool Remove(const char *name);
 +
</pre>
 +
 
 +
The plugin needs an event handler. We hide this handler inside the ''csLua'' class:
 +
 
 +
<pre>
 +
struct EventHandler :
 +
public scfImplementation1<EventHandler, iEventHandler>
 +
{
 +
csLua* parent;
 +
 
 +
EventHandler (csLua* parent) :
 +
scfImplementationType(this),
 +
parent (parent)
 +
{}
 +
virtual ~EventHandler()
 +
{}
 +
virtual bool HandleEvent (iEvent& e)
 +
{ return parent->HandleEvent(e); }
 +
 
 +
CS_EVENTHANDLER_NAMES("crystalspace.cslua")
 +
CS_EVENTHANDLER_NIL_CONSTRAINTS
 +
};
 +
</pre>
 +
 
 +
The event handler simply delegates the events to ''csLua'' class. So ''csLua'' needs a reference to the handler:
 +
 
 +
<pre>
 +
csRef<EventHandler> eventHandler;
 +
</pre>
 +
 
 +
The header is ready, let's go to create the implementation.
 +
 
 +
==Implementation ==
 +
The ''cslua.cpp'' will start some Lua header:
 +
 
 +
<pre>
 +
extern "C" {
 +
#include <lua.h>
 +
#include <lualib.h>
 +
#include "lauxlib.h"
 +
}
 +
</pre>
 +
 
 +
Don't forget the ''extern "C"'' section, lua is written in clean C, so this is neccessary in a C++ project.
 +
 
 +
Next comes the CS headers:
 +
 
 +
<pre>
 +
#include <cssysdef.h>
 +
#include <csutil/sysfunc.h>
 +
#include <csutil/syspath.h>
 +
#include <csutil/csstring.h>
 +
#include <csutil/util.h>
 +
#include <iutil/cmdline.h>
 +
#include <iutil/eventq.h>
 +
#include <csutil/event.h>
 +
#include <csutil/eventhandlers.h>
 +
#include <iutil/objreg.h>
 +
#include <iutil/vfs.h>
 +
#include <ivaria/reporter.h>
 +
 
 +
#include "cslua.h"
 +
 +
</pre>
 +
 
 +
Nowe some SWIG-related stuff:
 +
 
 +
<pre>
 +
extern "C" {
 +
int SWIG_luaopen_cspace(lua_State* L);
 +
}
 +
</pre>
 +
 
 +
The cspace is the great SWIG interface file. Swig will generate from the CS API the bindings after this file. You can find it in your ''CS/include/bindings/'' directory. We need to modify it later.
 +
 
 +
The code continues with more macro:
 +
 
 +
<pre>
 +
CS_IMPLEMENT_PLUGIN
 +
CS_PLUGIN_NAMESPACE_BEGIN(cslua){
 +
 
 +
/*****************************************************************************/
 +
 
 +
SCF_IMPLEMENT_FACTORY(csLua)
 +
csLua* csLua::shared_instance = 0;
 +
 
 +
</pre>
 +
 
 +
The <pre>CS_IMPLEMENT_PLUGIN </pre> is a neccessary macro to indicate to CS, we want to implemment a CS plugin now. The csLua shared_intance pointer is need set to null.
 +
 
 +
The <pre>SCF_IMPLEMENT_FACTORY(csLua)</pre> macro implements the factory function for our csLua class which returns new instances of the class to callers.
 +
 
 +
 
 +
== Links ==
-
Now we can to start
+
Here is some code you might want to look at: https://code.launchpad.net/~cslua/cslua/CS
 +
[[User:Mildred|Mildred]] 01:33, 2 May 2008 (CEST)

Current revision


Contents

Creating a LUA plugin

Short indtroduction

In this tutorial we will create a plugin for integrating Lua. That means, you can reach the the whole CS API with LUA scripting. Note, you can any SWIG supported language integrate easily into Crystal Space. In that case, when SWIG is new for you, please have a look at the project here:

SWIG homepage

A note about the history of this plugin. The source itself is just partially my work. The original code written by Brandon Ehle in 1999. I started to update the code, but the greater part of the code written by Mildred. Many thanks for her efforts.

Setup your enviroment

The very first step is download, compile and install LUA. You can get it from the official site:

LUA homepage

You can find here also some precompiled version, you can use them, when you want. This tutorial uses the LUA 5.1 version.

The next step is setting up an external CS project within your enviroment. Don't forget to set up your project as a dynamic link library, and the neccessary CS and LUA libs and include directories . The concrete steps depending from your IDE and operating system. ( This is a so called professional tutorial. Professional tutorials are not just for C++ masters , but I assume, you can set up this project without any external help.)


Start coding

Crystal Space uses the iScript interface to communicate with scripting languages, so we have to implement this interface to integrate LUA. So we include some CS headers:

#include <ivaria/script.h>
#include <iutil/eventh.h>
#include <iutil/comp.h>
#include <csutil/csinput.h>
#include <iutil/string.h>

#include "lua.h"

Then we introduce a new namespace to avoid name conflicts, in Crystal Space style:

CS_PLUGIN_NAMESPACE_BEGIN(cslua) {

This macro creates for us a new cslua namespace. The namespace will be closed with the

CS_PLUGIN_NAMESPACE_END(cslua) 

macro.

Tha csLua scf class will encapsulate the lua related stuff.

class csLua: public scfImplementation2<csLua, iScript, iComponent> {

public:
    csLua(iBase* iParent);
    virtual ~csLua();

    static csLua* shared_instance;
    iObjectRegistry* object_reg;

    lua_State* L;
    int varTable;

First we define the constructor and the virtual destructor. The shared_instance is a static csLua instance, this object will be used by Crystal Space. We need a know about the ObjectRegistry, object_reg will store a pointer to this. The lua_State* is a special statment indicator, when you knows Lua, yo know, what is it.

The public filed continues with functions:

virtual bool Initialize(iObjectRegistry* object_reg);
virtual bool HandleEvent(iEvent&);

The Initialize function will warm up the plugin. The HandleEvent function will deal with the plugin-related events (these are spcial events, like the command line help event.

Here comes the iScript interface functions:

The iScript allows us to run text form a char arrray (RunTex or from file(LoadModule):

 virtual bool RunText(const char *Text);

    virtual bool LoadModule(const char *name);
    virtual bool LoadModule (const char *path, const char *name);

Then provides us many Store function to store different types of variables.

    virtual bool Store(const char *name, char const *data);
    virtual bool Store(const char *name, iScriptObject *data);
    virtual bool Store(const char *name, double data);
    virtual bool Store(const char *name, int data)
        { return Store(name, static_cast<double>(data)); }
    virtual bool Store(const char *name, float data)
        { return Store(name, static_cast<double>(data)); }
    virtual bool Store(const char* name, void* data, void* tag)
        { return false; } /* deprecated */

The following functions read the stored varibles value by name:

    virtual bool Retrieve(const char *name, csRef<iString> &data) const;
       
    virtual bool Retrieve(const char *name, csRef<iScriptObject> &data) const
        { return false; } 
    virtual bool Retrieve(const char *name, double &data) const;
    virtual bool Retrieve(const char *name, int &data) const
        { double n; bool res = Retrieve(name, n); data = static_cast<int>(n); return res; }
    virtual bool Retrieve(const char *name, float &data) const
        { double n; bool res = Retrieve(name, n); data = static_cast<float>(n); return res; }

Next iScript ensures us many Call() to call stored functions:

    virtual bool Call(const char *name, const char *fmt, ...)
        { return false; } /* TODO */
    virtual bool Call(const char *name, double &ret, const char *fmt, ...)
        { return false; } /* TODO */
    virtual bool Call(const char *name, int &ret, const char *fmt, ...)
        { return false; } /* TODO */
    virtual bool Call(const char *name, float &ret, const char *fmt, ...)
        { return false; } /* TODO */
    virtual bool Call(const char *name, csRef<iString> &ref, const char *fmt, ...)
        { return false; } /* TODO */
    virtual bool Call(const char *name, csRef<iScriptObject> &ref, const char *fmt, ...)
        { return false; } /* TODO */

And finally some special funtion for objects and bool variables :

virtual csRef<iScriptObject> NewObject(const char *type, const char *fmt, ...)
        { return 0; } /* TODO */

    virtual bool SetTruth(const char *name, bool data);
    virtual bool GetTruth(const char *name, bool &data) const;
    virtual bool Remove(const char *name);

The plugin needs an event handler. We hide this handler inside the csLua class:

struct EventHandler :
        public scfImplementation1<EventHandler, iEventHandler>
    {
        csLua* parent;

        EventHandler (csLua* parent) :
            scfImplementationType(this),
            parent (parent)
            {}
        virtual ~EventHandler()
            {}
        virtual bool HandleEvent (iEvent& e)
            { return parent->HandleEvent(e); }

        CS_EVENTHANDLER_NAMES("crystalspace.cslua")
        CS_EVENTHANDLER_NIL_CONSTRAINTS
    };

The event handler simply delegates the events to csLua class. So csLua needs a reference to the handler:

 csRef<EventHandler> eventHandler;

The header is ready, let's go to create the implementation.

Implementation

The cslua.cpp will start some Lua header:

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

Don't forget the extern "C" section, lua is written in clean C, so this is neccessary in a C++ project.

Next comes the CS headers:

#include <cssysdef.h>
#include <csutil/sysfunc.h>
#include <csutil/syspath.h>
#include <csutil/csstring.h>
#include <csutil/util.h>
#include <iutil/cmdline.h>
#include <iutil/eventq.h>
#include <csutil/event.h>
#include <csutil/eventhandlers.h>
#include <iutil/objreg.h>
#include <iutil/vfs.h>
#include <ivaria/reporter.h>

#include "cslua.h"
 

Nowe some SWIG-related stuff:

extern "C" {
    int SWIG_luaopen_cspace(lua_State* L);
}

The cspace is the great SWIG interface file. Swig will generate from the CS API the bindings after this file. You can find it in your CS/include/bindings/ directory. We need to modify it later.

The code continues with more macro:

CS_IMPLEMENT_PLUGIN
CS_PLUGIN_NAMESPACE_BEGIN(cslua){

/*****************************************************************************/

SCF_IMPLEMENT_FACTORY(csLua)
csLua* csLua::shared_instance = 0;

The
CS_IMPLEMENT_PLUGIN 
is a neccessary macro to indicate to CS, we want to implemment a CS plugin now. The csLua shared_intance pointer is need set to null. The
SCF_IMPLEMENT_FACTORY(csLua)
macro implements the factory function for our csLua class which returns new instances of the class to callers.


Links

Here is some code you might want to look at: https://code.launchpad.net/~cslua/cslua/CS Mildred 01:33, 2 May 2008 (CEST)

| Article | Discussion | View source | History |