1 | /*! |
---|
2 | * @file scrip.h |
---|
3 | * wrapper for a lua_State |
---|
4 | */ |
---|
5 | |
---|
6 | #ifndef _SCRIPT_H |
---|
7 | #define _SCRIPT_H |
---|
8 | |
---|
9 | |
---|
10 | #include "base_object.h" |
---|
11 | |
---|
12 | #include <list> |
---|
13 | |
---|
14 | struct lua_State; |
---|
15 | |
---|
16 | struct WorldObject |
---|
17 | { |
---|
18 | std::string name; |
---|
19 | std::string type; |
---|
20 | }; |
---|
21 | |
---|
22 | class Script : public BaseObject |
---|
23 | { |
---|
24 | ObjectListDeclaration(Script); |
---|
25 | public: |
---|
26 | Script(const TiXmlElement* root = NULL); |
---|
27 | Script(const std::string& filename); |
---|
28 | ~Script(); |
---|
29 | |
---|
30 | /// LOADING |
---|
31 | void loadParams(const TiXmlElement* root); |
---|
32 | |
---|
33 | void loadFileNoRet(const std::string& filename) { loadFile(filename); }; |
---|
34 | bool loadFile(const std::string& filename); |
---|
35 | void addObject( const std::string& className,const std::string& objectName); |
---|
36 | void addObjectAsName( const std::string& className,const std::string& objectName, const std::string& nameInScript); |
---|
37 | void registerClass(const std::string& className); //!< Register a class but dont add any instances |
---|
38 | |
---|
39 | /// QUERRYING |
---|
40 | /** @returns fileName */ |
---|
41 | std::string getFileName() { return currentFile; } |
---|
42 | lua_State* getLuaState() const { return luaState; } |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | /// EXECUTING |
---|
47 | // first select function |
---|
48 | bool selectFunction(const std::string& functionName, int retCount); |
---|
49 | |
---|
50 | // push parameters for luafunction |
---|
51 | bool pushParam(int param, std::string& toFunction); |
---|
52 | bool pushParam(float param, std::string& toFunction); |
---|
53 | bool pushParam(double param, std::string& toFunction); |
---|
54 | |
---|
55 | // Execute the Function |
---|
56 | bool executeFunction(); |
---|
57 | |
---|
58 | // get returned values in the order the lua function returns it |
---|
59 | int getReturnedInt(); |
---|
60 | bool getReturnedBool(); |
---|
61 | float getReturnedFloat(); |
---|
62 | void getReturnedString(std::string& string); |
---|
63 | |
---|
64 | bool executeFile(); |
---|
65 | |
---|
66 | private: |
---|
67 | void addThisScript(); |
---|
68 | int reportError(int error); //!< Get errormessage from the lua stack and print it. |
---|
69 | bool registerStandartClasses(); //!< Register all the classes that the script might need |
---|
70 | bool classIsRegistered(const std::string& type); //!< Checks wheter the class "type" has already been registered with the script |
---|
71 | bool objectIsAdded(const std::string& name); //!< Checks wheter the object "name" has already been added to the script |
---|
72 | |
---|
73 | lua_State* luaState; //!< The lua_State that the Script works on |
---|
74 | std::string currentFile; //!< The file that is loaded in the script |
---|
75 | std::string currentFunction; //!< The name of the current active function (the one that gets the pushed parameter) |
---|
76 | int argumentCount; //!< Number of arguments for the current function |
---|
77 | int returnCount; //!< Number of return values of the current function |
---|
78 | |
---|
79 | std::list<WorldObject> registeredObjects; //!< The list of all the objects and their type that have been registered with this script |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | }; |
---|
84 | #endif /* _SCRIPT_H */ |
---|