You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmw-tes3coop/apps/openmw-mp/Script/Script.cpp

109 lines
2.0 KiB
C++

//
// Created by koncord on 19.03.16.
//
#include "Script.hpp"
#include "LangNative/LangNative.hpp"
#if defined (ENABLE_PAWN)
#include "LangPawn/LangPAWN.hpp"
#endif
#if defined (ENABLE_LUA)
#include "LangLua/LangLua.hpp"
#endif
using namespace std;
Script::ScriptList Script::scripts;
Script::Script(const char *path)
{
FILE *file = fopen(path, "rb");
if (!file)
throw runtime_error("Script not found: " + string(path));
fclose(file);
#ifdef __WIN32__
if (strstr(path, ".dll"))
#else
if (strstr(path, ".so"))
#endif
{
script_type = SCRIPT_CPP;
lang = new LangNative();
}
#if defined (ENABLE_PAWN)
else if (strstr(path, ".amx"))
{
lang = new LangPAWN();
script_type = SCRIPT_PAWN;
}
#endif
#if defined (ENABLE_LUA)
else if (strstr(path, ".lua") || strstr(path, ".t"))
{
lang = new LangLua();
script_type = SCRIPT_LUA;
}
#endif
else
throw runtime_error("Script type not recognized: " + string(path));
try
{
lang->LoadProgram(path);
}
catch (...)
{
lang->FreeProgram();
throw;
}
}
Script::~Script()
{
lang->FreeProgram();
delete lang;
}
void Script::LoadScripts(char *scripts, const char *base)
{
char *token = strtok(scripts, ",");
try
{
while (token)
{
char path[4096];
snprintf(path, sizeof(path), "%s/%s/%s", base, "scripts", token);
Script::scripts.emplace_back(new Script(path));
token = strtok(nullptr, ",");
}
}
catch (...)
{
UnloadScripts();
throw;
}
}
void Script::UnloadScripts()
{
//Public::DeleteAll();
scripts.clear();
#if defined (ENABLE_LUA)
terra_llvmshutdown();
#endif
}
void Script::LoadScript(const char *script, const char *base)
{
char path[4096];
snprintf(path, sizeof(path), "%s/%s/%s", base, "scripts", script);
Script::scripts.emplace_back(new Script(path));
}