-
Notifications
You must be signed in to change notification settings - Fork 361
/
ElunaLoader.h
102 lines (82 loc) · 2.65 KB
/
ElunaLoader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* Copyright (C) 2022 - 2022 Hour of Twilight <https://www.houroftwilight.net/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#ifndef _ELUNALOADER_H
#define _ELUNALOADER_H
#include "LuaEngine.h"
#if defined ELUNA_TRINITY
#include <efsw/efsw.hpp>
#endif
extern "C"
{
#include "lua.h"
};
enum ElunaReloadActions
{
RELOAD_CACHE_ONLY = -3,
RELOAD_ALL_STATES = -2,
RELOAD_GLOBAL_STATE = -1
};
enum ElunaScriptCacheState
{
SCRIPT_CACHE_NONE = 0,
SCRIPT_CACHE_REINIT = 1,
SCRIPT_CACHE_LOADING = 2,
SCRIPT_CACHE_READY = 3
};
struct LuaScript;
class ElunaLoader
{
private:
ElunaLoader();
~ElunaLoader();
public:
ElunaLoader(ElunaLoader const&) = delete;
ElunaLoader(ElunaLoader&&) = delete;
ElunaLoader& operator= (ElunaLoader const&) = delete;
ElunaLoader& operator= (ElunaLoader&&) = delete;
static ElunaLoader* instance();
void LoadScripts();
void ReloadElunaForMap(int mapId);
uint8 GetCacheState() const { return m_cacheState; }
const std::vector<LuaScript>& GetLuaScripts() const { return m_scriptCache; }
const std::string& GetRequirePath() const { return m_requirePath; }
const std::string& GetRequireCPath() const { return m_requirecPath; }
#if defined ELUNA_TRINITY
// efsw file watcher
void InitializeFileWatcher();
efsw::FileWatcher lua_fileWatcher;
efsw::WatchID lua_scriptWatcher;
#endif
private:
void ReloadScriptCache();
void ReadFiles(lua_State* L, std::string path);
void CombineLists();
void ProcessScript(lua_State* L, std::string filename, const std::string& fullpath, int32 mapId);
bool CompileScript(lua_State* L, LuaScript& script);
static int LoadBytecodeChunk(lua_State* L, uint8* bytes, size_t len, BytecodeBuffer* buffer);
std::atomic<uint8> m_cacheState;
std::vector<LuaScript> m_scriptCache;
std::string m_requirePath;
std::string m_requirecPath;
std::list<LuaScript> m_scripts;
std::list<LuaScript> m_extensions;
std::thread m_reloadThread;
};
#if defined ELUNA_TRINITY
/// File watcher responsible for watching lua scripts
class ElunaUpdateListener : public efsw::FileWatchListener
{
public:
ElunaUpdateListener() { }
virtual ~ElunaUpdateListener() { }
void handleFileAction(efsw::WatchID /*watchid*/, std::string const& dir,
std::string const& filename, efsw::Action /*action*/, std::string oldFilename = "") final override;
};
static ElunaUpdateListener elunaUpdateListener;
#endif
#define sElunaLoader ElunaLoader::instance()
#endif