Skip to content

Commit

Permalink
can now load/execute multiple scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
GoosiferIO committed Nov 6, 2023
1 parent 2969fde commit 1411575
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 104 deletions.
8 changes: 8 additions & 0 deletions EmuAPI/EmuAPI.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@
RelativePath=".\emu\EmuRegister.cpp"
>
</File>
<File
RelativePath=".\emu\EmuScriptMgr.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Expand Down Expand Up @@ -237,6 +241,10 @@
RelativePath=".\emu\EmuRegister.h"
>
</File>
<File
RelativePath=".\emu\EmuScriptMgr.h"
>
</File>
</Filter>
<Filter
Name="lua"
Expand Down
19 changes: 2 additions & 17 deletions EmuAPI/EmuLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,10 @@ DWORD WINAPI RunEmu(LPVOID lpParameter) {
std::ofstream f;
f.open("out.log", std::ios_base::app);

//------ Initializing Lua
lua_State *lua = luaL_newstate(); // Open Lua
int iErr = 0;
if (!lua) {
f << "[" << timestamp << "] " << "Failed to create Lua state." << std::endl;
return 0;
}

//------ Register API functions to Lua
RegZooState::register_zoo_state(lua);

//------ Load Lua libraries
luaL_openlibs (lua);

//------ Find/load script file directories with script manager
EmuScriptMgr sm(lua, f);
EmuScriptMgr sm(f, timestamp);
sm.findScripts();
sm.serializeScripts();
sm.storeScripts();

// main loop

Expand Down Expand Up @@ -141,7 +127,6 @@ DWORD WINAPI RunEmu(LPVOID lpParameter) {

Sleep(0);
}
lua_close (lua);
f.close();
return 1;
}
Expand Down
35 changes: 13 additions & 22 deletions EmuAPI/emu/EmuConsole.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "EmuConsole.h"
#include <iomanip>

Expand All @@ -17,33 +16,25 @@ void EmuConsole::tokenize()
{
std::string token = "";
// TODO: fix try/catch. Crashes if error found.
try
if (!std::getline(std::cin, token)) {
return;
}

if (token.size() > 100)
{

std::getline(std::cin, token);
// this limit will be increased, it's just a pre-emptive measure
std::cout << "100 char limit in buffer. Please try again." << std::endl;
// std::cin.ignore(32767, '\n');
} else {
std::istringstream iss(token);

if (token.size() > 100)
while (std::getline(iss, token, ' '))
{
// this limit will be increased, it's just a pre-emptive measure
std::cout << "100 char limit in buffer. Please try again." << std::endl;
throw;
tokens.push_back(token);
}
}
catch(const std::exception& e)
{
std::cout << e.what() << std::endl;
token = "";
std::cin.clear();
std::getline(std::cin, token);

}

std::istringstream iss(token);

while (std::getline(iss, token, ' '))
{
tokens.push_back(token);
}

}

/// <summary>
Expand Down
139 changes: 80 additions & 59 deletions EmuAPI/emu/EmuScriptMgr.cpp
Original file line number Diff line number Diff line change
@@ -1,86 +1,110 @@
#include "EmuScriptMgr.h"

// EmuScriptMgr::EmuScriptMgr() {

// EmuScriptMgr::EmuScriptMgr() : lua(NULL), f(std::ofstream("out.log", std::ios_base::app)), timestamp(*new char) {
// // do not use default constructor
// }

EmuScriptMgr::EmuScriptMgr(lua_State* lua, std::ofstream& fs) : lua(lua), f(fs) {
EmuScriptMgr::EmuScriptMgr(std::ofstream& fs, char* ts) : f(fs), timestamp(ts) {
//------ Initializing Lua
lua = luaL_newstate(); // Open Lua
int iErr = 0;
if (!lua) {
f << "[" << timestamp << "] " << "Failed to create Lua state." << std::endl;
}

//------ Register API functions to Lua
RegZooState::register_zoo_state(lua);

//------ Timestamp for logging
*t = std::time(0);
timestamp = new char[80]; // timestamp buffer
std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", std::localtime(t));
//------ Load Lua libraries
luaL_openlibs (lua);
}

EmuScriptMgr::~EmuScriptMgr() {
}

/// @brief Executes all emu scripts in a directory.
int EmuScriptMgr::executeScripts() {
std::string function_name;
try {
for (size_t i = 0; i < files.size() - 1; i++) {
// lua_CFunction script_fun = script_functions[i];
//script_fun(lua);
std::stringstream ss;
ss << "emu_run";// << i + 1;
function_name = ss.str();
lua_getglobal(lua, function_name.c_str()); // call emu_run from script
if (lua_isfunction(lua, -1)) {
int pcall = lua_pcall(lua, 0, 0, 0);
if (pcall != 0) {
const char* error_message = lua_tostring(lua, -1);
if(error_message != NULL) {
f << "[" << timestamp << "] " << "Lua pcall error: <" << function_name << "> " << error_message << std::endl;
} else {
f << "[" << timestamp << "] " << "Lua pcall error: <" << function_name << "> err unavailable" << std::endl;

for (int i = 0; i < scripts.size(); i++) {
lua = luaL_newstate(); // Open Lua

int iErr = 0;
if (!lua) {
f << "[" << timestamp << "] " << "Failed to create Lua state." << std::endl;
}
//------ Register API functions to Lua
RegZooState::register_zoo_state(lua);
luaL_openlibs (lua);
if (luaL_loadstring(lua, scripts[i].c_str()) == 0) {
if (lua_pcall(lua, 0, LUA_MULTRET, 0) == 0)
{
// script is loaded, now load specific function
lua_getglobal(lua, "emu_run");
// check if we can call function
if (lua_isfunction(lua, -1)) {
if (lua_pcall(lua, 0, LUA_MULTRET, 0) != 0) {
// errors if can't execute script
const char* error_message = lua_tostring(lua, -1);
f << "Error executing Lua function: " << error_message << std::endl;
lua_close (lua);
return 1;
}

lua_pop(lua, 1);
} else {
f << "[" << timestamp << "] " << "Function 'emu_run' not found or not callable" << std::endl;
lua_close (lua);
return 1;
}
lua_pop(lua, 1);
}
else {
} else {
// error handling
const char* error_message = lua_tostring(lua, -1);
f << "[" << timestamp << "] " << "Error executing Lua script " << i << ": " << error_message << std::endl;

// remove err from stack
lua_pop(lua, 1);
f << "[" << timestamp << "] " << "Error while finding function: <" << function_name << "> null function" << std::endl;
return 2;
lua_close (lua);
return 1;
}
} else {
// error handling
const char* error_message = lua_tostring(lua, -1);
f << "[" << timestamp << "] " << "Error loading Lua script " << i << ": " << error_message << std::endl;

// remove err from stack
lua_pop(lua, 1);
lua_close (lua);
return 1;
}
lua_close (lua);
}
catch (const std::bad_alloc& e) {
f << "[" << timestamp << "] " << "Error while executing function: <" << function_name << "> " << e.what() << std::endl;
}

return 0;
}

/// @brief Serializes all emu scripts in a directory as a binary format.
void EmuScriptMgr::serializeScripts() {
int err = 0;
/// @brief Stores all emu scripts in a directory in memory.
void EmuScriptMgr::storeScripts() {

//------ Load scripts
for (int i = 0; i < files.size(); i++) {
std::string file = files[i];
std::string file_name = file_names[i];

//------ Load script into Lua state (if no errors
if ((err = luaL_loadfile(lua, file.c_str())) == 0) {
lua_pcall(lua, 0, LUA_MULTRET, 0);
f << "[" << timestamp << "] " << "Loading script: " << file_name << std::endl;
//------ Read script file
std::ifstream file(files[i].c_str());
std::string script;
char c;
while (file.get(c)) {
script += c;
}
else
{
const char* error_message = lua_tostring(lua, -1);
if (error_message != NULL)
{
f << "[" << timestamp << "] " << "Error loading script: <" << file_name << "> " << error_message << std::endl;
}
else
{
f << "[" << timestamp << "] " << "Error loading script: <" << file_name << std::endl;
}

scripts.push_back(script);
file.close();
//------ Compile script
if ((luaL_loadstring(lua, script.c_str())) == 0) {
lua_CFunction script_funct = lua_tocfunction(lua, -1);
compiled_scripts.push_back(script_funct);
lua_pop(lua, 1);
} else {
f << "[" << timestamp << "] " << "Error loading script: " << files[i].substr(0, files[i].size() - (int)(files[i].size() * 0.30)) << " [..]" << std::endl;
}
}
lua_close (lua);

}

/// @brief Finds all scripts within the /scripts directory and stores their location in memory.
Expand All @@ -99,9 +123,6 @@ void EmuScriptMgr::findScripts() {
//------ Convert wide string to narrow string,
std::string path(wpath.begin(), wpath.end());

//------ Store all file directories into vector
std::vector<std::string> files;

//------ Find first .emu file in directory
WIN32_FIND_DATA find_emu_file;
HANDLE hFind = FindFirstFile((wpath + L"\\*.emu").c_str(), &find_emu_file); // wide string used here
Expand Down
20 changes: 14 additions & 6 deletions EmuAPI/emu/EmuScriptMgr.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef EMUSCRIPTMGR_H
#define EMUSCRIPTMGR_H

#include <vector>
#include <string>
#include <ctime>
Expand All @@ -7,24 +10,29 @@
#include <fstream>
#include "lua.hpp"
#include <sstream>
#include <string.h>
#include "RegZooState.h"

class EmuScriptMgr
{
public:
EmuScriptMgr();
EmuScriptMgr(lua_State*, std::ofstream&); // overloaded constructor
EmuScriptMgr(std::ofstream&, char*); // overloaded constructor
~EmuScriptMgr();
void findScripts();
void serializeScripts();
void storeScripts();
int executeScripts();

private:
std::vector<std::string> files;
std::vector<std::string> file_names;

std::time_t* t;
char* timestamp;
std::vector<lua_CFunction> compiled_scripts;
std::vector<std::string> scripts;
static int writer(const void*, size_t, void*);

char* timestamp;
lua_State *lua;
std::ofstream& f;
};
};

#endif

0 comments on commit 1411575

Please sign in to comment.