Przeglądaj źródła

ScriptSystem implemented

mikymod 12 lat temu
rodzic
commit
9b7ca98d62
2 zmienionych plików z 183 dodań i 0 usunięć
  1. 104 0
      src/ScriptSystem.cpp
  2. 79 0
      src/ScriptSystem.h

+ 104 - 0
src/ScriptSystem.cpp

@@ -0,0 +1,104 @@
+#include "ScriptSystem.h"
+#include <cassert>
+
+namespace crown
+{
+
+//-----------------------------------------------------------
+LuaState::LuaState()
+{
+	m_state = luaL_newstate();
+
+    luaL_openlibs(m_state);
+}
+
+//-----------------------------------------------------------
+LuaState::~LuaState()
+{
+	if (m_instance != NULL)
+	{
+		lua_close(m_state);
+		delete m_instance;
+	}
+}
+
+//-----------------------------------------------------------
+LuaState* LuaState::instance()
+{
+	if (m_instance == NULL)
+	{
+		m_instance = new LuaState();
+	}
+
+	return m_instance;
+}
+
+//-----------------------------------------------------------
+int32_t LuaState::load_buffer(const char* buf, size_t len)
+{
+	int32_t s = luaL_loadbuffer(m_state, buf, len, "");
+
+	return s;
+}
+
+//-----------------------------------------------------------
+int32_t LuaState::load_string(const char* str)
+{
+	int32_t s = luaL_loadstring(m_state, str);
+
+	return s;
+}
+
+//-----------------------------------------------------------
+int32_t LuaState::execute()
+{
+    int32_t s = lua_pcall(m_state, 0, LUA_MULTRET, 0);
+
+    return s;
+}
+
+
+//-----------------------------------------------------------
+ScriptSystem::ScriptSystem() :
+	m_script_count(0)
+{
+}
+
+//-----------------------------------------------------------
+ScriptSystem::~ScriptSystem()
+{
+	// FIXME
+}
+
+//-----------------------------------------------------------
+ScriptId ScriptSystem::load(ScriptResource* script)
+{
+	assert(LuaState::instance()->load_buffer((char*)script->data(), script->length()) == 0);
+
+	ScriptId id;
+	id.index = m_script_count;
+	id.id = 0;
+
+	m_script[id.index].id = id;
+	m_script[id.index].script_resource = script;
+
+	m_script_count++;
+
+	return id;
+}
+
+//-----------------------------------------------------------
+void ScriptSystem::execute()
+{
+	assert(LuaState::instance()->execute() == 0);	
+}
+
+//-----------------------------------------------------------
+void unload(ScriptResource* resource)
+{
+	(void*) resource;
+	// FIXME
+}
+
+
+} // namespace crown

+ 79 - 0
src/ScriptSystem.h

@@ -0,0 +1,79 @@
+#pragma once
+
+#include "Types.h"
+#include "String.h"
+#include "lua.hpp"
+#include "ScriptResource.h"
+
+namespace crown
+{
+
+//------------------------------------------------------------------
+struct ScriptId
+{
+	uint16_t	index;
+	uint16_t	id;
+};
+
+//------------------------------------------------------------------
+struct Script
+{
+	ScriptId		id;
+	ScriptResource* script_resource;
+};
+
+/// Singleton class that abstracts lua state
+class LuaState
+{
+public:
+							/// Destructor
+							~LuaState();
+							/// Get singleton instance
+	static LuaState*		instance();
+							/// Load lua chunk as buffer
+	int32_t 				load_buffer(const char* buf, size_t len);
+							/// Load lua chunk as string
+	int32_t					load_string(const char* str);
+							/// Executes lua chunk loaded in stack
+	int32_t 				execute();
+
+private:
+							/// Constructor, private for singleton
+							LuaState();
+							/// Lua state incapsulated by this class
+	lua_State*				m_state;
+							/// LuaState pointer for singleton
+	static LuaState* 		m_instance;
+};
+
+LuaState* LuaState::m_instance = NULL;
+
+/// Script System allows to execute lua code or lua bytecode chunk
+class ScriptSystem
+{
+public:
+							/// Max number of scripts which can be loaded by this system
+	static const int32_t	MAX_SCRIPTS = 256;
+
+
+							/// Constructor
+							ScriptSystem();
+							///	Destructor
+							~ScriptSystem();
+							/// Load scripr resource
+	ScriptId				load(ScriptResource* script);
+							/// Execute 
+	void					execute();
+							/// Unload script resource
+	void					unload(ScriptResource* script);
+
+private:
+							/// Number of scripts already loaded
+	uint32_t				m_script_count;
+							/// Resource handle
+	Script					m_script[MAX_SCRIPTS];
+
+	friend class ScriptResource;
+};
+
+} // namespace crown