00001 00002 // Copyright (C) 2004-2007 by The Allacrost Project 00003 // All Rights Reserved 00004 // 00005 // This code is licensed under the GNU GPL version 2. It is free software 00006 // and you may modify it and/or redistribute it under the terms of this license. 00007 // See http://www.gnu.org/copyleft/gpl.html for details. 00009 00025 #ifndef __SCRIPT_HEADER__ 00026 #define __SCRIPT_HEADER__ 00027 00028 #include <sstream> 00029 #include <fstream> 00030 extern "C" { 00031 #include <lua.h> 00032 #include <lauxlib.h> 00033 #include <lualib.h> 00034 } 00035 00036 // This needs a comment: what is check and why is it undefined for darwin? 00037 #ifdef __MACH__ 00038 #undef check 00039 #endif 00040 00041 #include <luabind/luabind.hpp> 00042 #include <luabind/object.hpp> 00043 #include <luabind/adopt_policy.hpp> 00044 00045 #include "utils.h" 00046 #include "defs.h" 00047 00053 #define ScriptObject luabind::object 00054 00056 #define ScriptCallFunction luabind::call_function 00057 00058 00060 namespace hoa_script { 00061 00063 extern GameScript* ScriptManager; 00064 00066 extern bool SCRIPT_DEBUG; 00067 00071 enum SCRIPT_ACCESS_MODE { 00072 SCRIPT_CLOSED = 0, 00073 SCRIPT_READ = 1, 00074 SCRIPT_WRITE = 2, 00075 SCRIPT_MODIFY = 3 00076 }; 00077 00079 namespace private_script { 00080 00082 const int32 STACK_TOP = -1; 00083 00085 const luabind::iterator TABLE_END; 00086 00087 } // namespace private_script 00088 00098 class ScriptDescriptor { 00099 friend class GameScript; 00100 00101 public: 00102 ScriptDescriptor () 00103 { _filename = ""; _access_mode = SCRIPT_CLOSED; _error_messages.clear(); } 00104 00106 virtual ~ScriptDescriptor () 00107 {} 00108 00110 00111 00116 virtual bool OpenFile(const std::string& file_name) = 0; 00117 00121 virtual bool OpenFile() = 0; 00122 00124 virtual void CloseFile() = 0; 00126 00128 bool IsFileOpen() 00129 { return (_access_mode != SCRIPT_CLOSED); } 00130 00132 bool IsErrorDetected() 00133 { return (_error_messages.str() != ""); } 00134 00136 00137 const std::string& GetFilename() 00138 { return _filename; } 00139 00141 const SCRIPT_ACCESS_MODE GetAccessMode() 00142 { return _access_mode; } 00143 00148 void ClearErrors() 00149 { _error_messages.clear(); } 00150 00160 std::string GetErrorMessages() 00161 { std::string errors = _error_messages.str(); ClearErrors(); return errors; } 00162 00166 std::vector<std::string> GetOpenTables() 00167 { return _open_tables; } 00169 00170 protected: 00172 std::string _filename; 00173 00175 SCRIPT_ACCESS_MODE _access_mode; 00176 00178 std::ostringstream _error_messages; 00179 00181 std::vector<std::string> _open_tables; 00182 }; // class ScriptDescriptor 00183 00184 00194 class GameScript : public hoa_utils::Singleton<GameScript> { 00195 friend class hoa_utils::Singleton<GameScript>; 00196 friend class ScriptDescriptor; 00197 friend class ReadScriptDescriptor; 00198 friend class WriteScriptDescriptor; 00199 friend class ModifyScriptDescriptor; 00200 public: 00201 ~GameScript(); 00202 00203 bool SingletonInitialize (); 00204 00206 lua_State *GetGlobalState() 00207 { return _global_state; } 00208 00213 bool IsFileOpen(const std::string& filename); 00214 00225 void HandleLuaError(luabind::error& err); 00226 00233 void HandleCastError(luabind::cast_failed& err); 00234 00235 private: 00236 GameScript(); 00237 00239 std::map<std::string, ScriptDescriptor*> _open_files; 00240 00242 lua_State *_global_state; 00243 00245 void _AddOpenFile(ScriptDescriptor* sd); 00246 00248 void _RemoveOpenFile(ScriptDescriptor* sd); 00249 }; // class GameScript 00250 00251 } // namespace hoa_script 00252 00253 // These #includes are made here rather than the top of the file to avoid a recurisve inclusion problem. 00254 // Each of these files contains a class that dervies from the ScriptDescriptor class that is defined in 00255 // this file. Thus these headers must all include this header file, "script.h". However we would like to 00256 // make the entire script engine API available to the user through one header inclusion (script.h), so 00257 // this file must likewise include these files. When these are placed at the top of this file, the 00258 // ScriptDescriptor definition is not fully parsed by the time control reaches the other script headers, 00259 // hence why they are included here so that the ScriptDescriptor definition is available for the 00260 // other script headers to use. 00261 #include "script_read.h" 00262 #include "script_write.h" 00263 #include "script_modify.h" 00264 00265 #endif // __SCRIPT_HEADER__
1.5.1