NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
ScriptModule.h
Go to the documentation of this file.
00001 
00010 #pragma once
00011 
00012 #define NOMINMAX
00013 #define AS_USE_FLOAT 1
00014 
00015 // Standard Includes
00016 #include <map>
00017 #include <set>
00018 #include <queue>
00019 #include <utility>
00020 
00021 // Library Includes
00022 #include <windows.h>
00023 #include <boost/foreach.hpp>
00024 #include <boost/function.hpp>
00025 #include <angelscript/scriptany/ScriptAny.h>
00026 #include <angelscript/scriptmath/scriptmath.h>
00027 #include <threading.h>
00028 
00029 // Local Includes
00030 #include "../sharedbase/ModuleInterface.h"
00031 #include "ScriptManager.h"
00032 #include "../sharedbase/MessageRouter.h"
00033 #include "../sharedbase/Envelope_fwd.h"
00034 #include "../sharedbase/Entity_fwd.h"
00035 #include "../sharedbase/OSInterface_fwd.h"
00036 
00037 // Forward Declarations
00038 class PropertyMap;
00039 class CreateEntityFactory;
00040 class EntityList;
00041 
00042 // Typedefs
00043 typedef int                          event_handler_type;
00044 typedef unsigned int                 event_handler_key_type;
00045 typedef std::set<event_handler_type> event_handler_list_type;
00046 typedef std::map<event_handler_key_type, event_handler_list_type> event_handler_map_type;
00047 
00048 extern "C"
00049 {
00050   __declspec(dllexport) ModuleInterface* ModuleFactory(PropertyMap*, MessageRouter*, EntityList*, EventLogger*, OSInterfaceSPTR);
00051 };
00052 
00053 namespace ScriptException {
00054   enum TYPE {
00055     FAILED_LOAD,
00056     FAILED_BUILD
00057   };
00058 }
00059 
00060 class ScriptModule : public ModuleInterface {
00061 public:
00062   ScriptModule(PropertyMap*, MessageRouter*, EntityList* );
00063 
00064   ~ScriptModule();
00065 
00066   void SendMessage(EnvelopeSPTR envelope) const; // Send a message with a shared pointer envelope
00067   void PrioritySend( EnvelopeSPTR envelope ) const;
00068   EnvelopeSPTR DirectedSend( EnvelopeSPTR envelope ) const;
00069 
00070   void Message(EnvelopeSPTR envelope);
00071 
00075   unsigned int RegisterMessageHandler(std::string funcName, event_handler_key_type eventID);
00076   
00080   unsigned int RegisterMessageHandlerByName(std::string funcName, event_handler_key_type eventID);
00081   
00085   unsigned int RegisterMessageHandlerByDecl(std::string funcDeclaration, event_handler_key_type eventID);
00086 
00090   unsigned int RegisterMessageHandlerByID( unsigned int funcID, event_handler_key_type eventID );
00091 
00095   unsigned int UnregisterMessageHandler(std::string funcName, event_handler_key_type eventID);
00096   
00100   unsigned int UnregisterMessageHandlerByName(std::string funcName, event_handler_key_type eventID);
00101   
00105   unsigned int UnregisterMessageHandlerByDecl(std::string funcDeclaration, event_handler_key_type eventID);
00106 
00110   unsigned int UnregisterMessageHandlerByID( unsigned int funcID, event_handler_key_type eventID );
00111 
00112   // Wrapper template for shared pointer behavior.
00113   template <typename Type>
00114   struct SharedPointerWrapper {
00115     // Constructor wrapper.
00116     static void construct(void* memory) {
00117       new(memory) std::shared_ptr<Type>();
00118     }
00119     // Destructor wrapper.
00120     static void destruct(void* memory) {
00121       ((std::shared_ptr<Type>*)memory)->~shared_ptr();
00122     }
00123   };
00124 
00125   template <typename Type>
00126   void RegisterSharedPointer(const std::string&, const std::string&);
00127 
00128   EnvelopeSPTR EnvelopeSPTRFactory(); // Factory used by AS to get an instance of EnvelopeSPTR
00129 
00130   void CreateComponent(std::string, std::map< std::string, boost::any > &, EntitySPTR);
00131   WHO_DELETES::TYPE RemoveComponent(ComponentInterface*);
00132 
00133   void ExecuteFile(const std::string&, const std::string&) throw(ScriptException::TYPE);
00134 
00135   void Update(double dt);
00136 
00137 
00141   unsigned int RegisterUpdateHandler(std::string funcName);
00142   
00146   unsigned int RegisterUpdateHandlerByName(std::string funcName);
00147   
00151   unsigned int RegisterUpdateHandlerByID(unsigned int funcID);
00152 
00156   std::string OSGetFolder(const ::DIRS::TYPE&);
00157 
00158 
00159 private:
00160   ScriptManager updateScriptManager;
00161   ScriptManager eventScriptManager;
00162 
00163   double deltaAccumulator; // Accumulator for the change in time between each call to update
00164 
00165   mutable Threading::ReadWriteMutex eventHandlersMutex;
00166   event_handler_map_type eventHandlers;
00167   
00168   mutable Threading::ReadWriteMutex updateHandlersMutex;
00169   std::queue<unsigned int> updateHandlers;
00170 };