New UI framework stuff + lua
This commit is contained in:
28
Nuake/src/Scripting/ScriptLoader.cpp
Normal file
28
Nuake/src/Scripting/ScriptLoader.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "ScriptLoader.h"
|
||||
#include <windows.h>
|
||||
#include <libloaderapi.h>
|
||||
|
||||
bool ScriptLoader::RegisterScript(ScriptableEntity* script)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ScriptLoader::LoadModule(const std::string& path)
|
||||
{
|
||||
HMODULE loadedLib = LoadLibraryA(path.c_str());
|
||||
if (!loadedLib) {
|
||||
printf("Failed to load library\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Library loaded\n");
|
||||
|
||||
ScriptModuleRegister* functionPointer = (ScriptModuleRegister*)GetProcAddress(loadedLib, "Register");
|
||||
|
||||
functionPointer();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
31
Nuake/src/Scripting/ScriptLoader.h
Normal file
31
Nuake/src/Scripting/ScriptLoader.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "ScriptModule.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "../Scene/Entities/ScriptableEntity.h"
|
||||
|
||||
|
||||
typedef void (ScriptModuleRegister)();
|
||||
//#define X_INPUT_GET_STATE(name) DWORD WINAPI name(DWORD dwUserIndex, XINPUT_STATE* pState)
|
||||
// Now we create a typedef for this function pointer
|
||||
//typedef X_INPUT_GET_STATE(x_input_get_state);
|
||||
extern "C"
|
||||
{
|
||||
class ScriptLoader
|
||||
{
|
||||
private:
|
||||
static std::map<std::string, ScriptableEntity*> m_Scripts;
|
||||
|
||||
public:
|
||||
static bool RegisterScript(ScriptableEntity* script);
|
||||
|
||||
|
||||
static ScriptableEntity* GetScript(std::string script) {
|
||||
return m_Scripts[script];
|
||||
}
|
||||
|
||||
static bool LoadModule(const std::string& path);
|
||||
|
||||
};
|
||||
}
|
||||
7
Nuake/src/Scripting/ScriptModule.h
Normal file
7
Nuake/src/Scripting/ScriptModule.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
class ScriptModule
|
||||
{
|
||||
public:
|
||||
virtual void Register();
|
||||
};
|
||||
77
Nuake/src/Scripting/ScriptingEngine.cpp
Normal file
77
Nuake/src/Scripting/ScriptingEngine.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "ScriptingEngine.h"
|
||||
#include <src/Vendors/sol/sol.hpp>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
inline void my_panic(sol::optional<std::string> maybe_msg) {
|
||||
std::cerr << "Lua script error detected!" << std::endl;
|
||||
if (maybe_msg) {
|
||||
const std::string& msg = maybe_msg.value();
|
||||
std::cerr << "\terror message: " << msg << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
sol::state ScriptingEngine::lua = sol::state(sol::c_call<decltype(&my_panic), &my_panic>);
|
||||
|
||||
void ScriptingEngine::Init()
|
||||
{
|
||||
lua.open_libraries(sol::lib::base, sol::lib::package);
|
||||
|
||||
}
|
||||
|
||||
void ScriptingEngine::RunFile(const std::string& path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ScriptingEngine::RunCode(const std::string& code)
|
||||
{
|
||||
lua.script(code);
|
||||
bool isfullscreen = lua["config"]["fullscreen"];
|
||||
sol::table config = lua["config"];
|
||||
float x = lua["config"]["resolution"]["x"];
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
void ScriptingEngine::UpdateScript(const std::string& path)
|
||||
{
|
||||
std::string fileContent;
|
||||
std::string allFile = "";
|
||||
// Read from the text file
|
||||
std::ifstream MyReadFile("resources/Scripts/test.lua");
|
||||
|
||||
// Use a while loop together with the getline() function to read the file line by line
|
||||
while (getline(MyReadFile, fileContent)) {
|
||||
allFile.append(fileContent);
|
||||
}
|
||||
|
||||
// Close the file
|
||||
MyReadFile.close();
|
||||
|
||||
try {
|
||||
auto bad_code_result = lua.script(allFile);
|
||||
// it did not work
|
||||
if (!bad_code_result.valid())
|
||||
{
|
||||
printf(std::to_string((int)(bad_code_result.status())).c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
int result = lua["update"]();
|
||||
}
|
||||
catch (int e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ScriptingEngine::Close()
|
||||
{
|
||||
|
||||
}
|
||||
26
Nuake/src/Scripting/ScriptingEngine.h
Normal file
26
Nuake/src/Scripting/ScriptingEngine.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include "sol/forward.hpp"
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class ScriptingEngine
|
||||
{
|
||||
private:
|
||||
static sol::state lua;
|
||||
static std::map<std::string, std::string> m_Scripts;
|
||||
|
||||
public:
|
||||
static void Init();
|
||||
|
||||
static void RegisterScript(std::string path);
|
||||
|
||||
static void InitScript(std::string path);
|
||||
static void UpdateScript(std::string path);
|
||||
static void ExitScript(std::string path);
|
||||
|
||||
static void RunFile(const std::string& path);
|
||||
static void UpdateScript(const std::string& path);
|
||||
static void RunCode(const std::string& code);
|
||||
static void Close();
|
||||
};
|
||||
Reference in New Issue
Block a user