mirror of
				https://github.com/TES3MP/openmw-tes3mp.git
				synced 2025-11-04 10:56:48 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			96 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			96 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
//
 | 
						|
// Created by koncord on 19.03.16.
 | 
						|
//
 | 
						|
 | 
						|
#include "Script.hpp"
 | 
						|
#include "LangNative/LangNative.hpp"
 | 
						|
 | 
						|
#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_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), Utils::convertPath("%s/%s/%s").c_str(), base, "scripts", token);
 | 
						|
            Script::scripts.emplace_back(new Script(path));
 | 
						|
            token = strtok(nullptr, ",");
 | 
						|
        }
 | 
						|
    }
 | 
						|
    catch (...)
 | 
						|
    {
 | 
						|
        UnloadScripts();
 | 
						|
        throw;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
void Script::UnloadScripts()
 | 
						|
{
 | 
						|
    //Public::DeleteAll();
 | 
						|
    scripts.clear();
 | 
						|
}
 | 
						|
 | 
						|
void Script::LoadScript(const char *script, const char *base)
 | 
						|
{
 | 
						|
    char path[4096];
 | 
						|
    snprintf(path, sizeof(path), Utils::convertPath("%s/%s/%s").c_str(), base, "scripts", script);
 | 
						|
    Script::scripts.emplace_back(new Script(path));
 | 
						|
}
 |