-
Notifications
You must be signed in to change notification settings - Fork 361
/
ElunaLoader.cpp
382 lines (311 loc) · 11.4 KB
/
ElunaLoader.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* 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
*/
#include "ElunaCompat.h"
#include "ElunaConfig.h"
#include "ElunaLoader.h"
#include "ElunaUtility.h"
#include <fstream>
#include <sstream>
#include <thread>
#if defined USING_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
#if defined ELUNA_WINDOWS
#include <Windows.h>
#endif
#if defined ELUNA_TRINITY || ELUNA_MANGOS
#include "MapManager.h"
#elif defined ELUNA_CMANGOS
#include "Maps/MapManager.h"
#endif
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#if defined ELUNA_TRINITY
void ElunaUpdateListener::handleFileAction(efsw::WatchID /*watchid*/, std::string const& dir, std::string const& filename, efsw::Action /*action*/, std::string /*oldFilename*/)
{
auto const path = fs::absolute(filename, dir);
if (!path.has_extension())
return;
std::string ext = path.extension().string();
std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return std::tolower(c); });
if (ext != ".lua" && ext != ".ext")
return;
sElunaLoader->ReloadElunaForMap(RELOAD_ALL_STATES);
}
#endif
ElunaLoader::ElunaLoader() : m_cacheState(SCRIPT_CACHE_NONE)
{
#if defined ELUNA_TRINITY
lua_scriptWatcher = -1;
#endif
}
ElunaLoader* ElunaLoader::instance()
{
static ElunaLoader instance;
return &instance;
}
ElunaLoader::~ElunaLoader()
{
// join any previously created reload thread so it can exit cleanly
if (m_reloadThread.joinable())
m_reloadThread.join();
#if defined ELUNA_TRINITY
if (lua_scriptWatcher >= 0)
{
lua_fileWatcher.removeWatch(lua_scriptWatcher);
lua_scriptWatcher = -1;
}
#endif
}
void ElunaLoader::ReloadScriptCache()
{
// if the internal cache state is anything other than ready, we return
if (m_cacheState != SCRIPT_CACHE_READY)
{
ELUNA_LOG_DEBUG("[Eluna]: Script cache not ready, skipping reload");
return;
}
// try to join any previous thread before starting a new one, just in case
if (m_reloadThread.joinable())
m_reloadThread.join();
// set the internal cache state to reinit
m_cacheState = SCRIPT_CACHE_REINIT;
// create new thread to load scripts asynchronously
m_reloadThread = std::thread(&ElunaLoader::LoadScripts, this);
ELUNA_LOG_DEBUG("[Eluna]: Script cache reload thread started");
}
void ElunaLoader::LoadScripts()
{
// only reload the cache if it is either in a reinit state or not loaded at all
if (m_cacheState != SCRIPT_CACHE_REINIT && m_cacheState != SCRIPT_CACHE_NONE)
return;
// set the cache state to loading
m_cacheState = SCRIPT_CACHE_LOADING;
uint32 oldMSTime = ElunaUtil::GetCurrTime();
std::string lua_folderpath = sElunaConfig->GetConfig(CONFIG_ELUNA_SCRIPT_PATH);
const std::string& lua_path_extra = sElunaConfig->GetConfig(CONFIG_ELUNA_REQUIRE_PATH_EXTRA);
const std::string& lua_cpath_extra = sElunaConfig->GetConfig(CONFIG_ELUNA_REQUIRE_CPATH_EXTRA);
#if !defined ELUNA_WINDOWS
if (lua_folderpath[0] == '~')
if (const char* home = getenv("HOME"))
lua_folderpath.replace(0, 1, home);
#endif
ELUNA_LOG_INFO("[Eluna]: Searching for scripts in `%s`", lua_folderpath.c_str());
// open a new temporary Lua state to compile bytecode in
lua_State* L = luaL_newstate();
luaL_openlibs(L);
// clear all cache variables
m_requirePath.clear();
m_requirecPath.clear();
// read and compile all scripts
ReadFiles(L, lua_folderpath);
// close temporary Lua state
lua_close(L);
// combine lists of Lua scripts and extensions
CombineLists();
// append our custom require paths and cpaths if the config variables are not empty
if (!lua_path_extra.empty())
m_requirePath += lua_path_extra;
if (!lua_cpath_extra.empty())
m_requirecPath += lua_cpath_extra;
// Erase last ;
if (!m_requirePath.empty())
m_requirePath.erase(m_requirePath.end() - 1);
if (!m_requirecPath.empty())
m_requirecPath.erase(m_requirecPath.end() - 1);
ELUNA_LOG_INFO("[Eluna]: Loaded and precompiled %u scripts in %u ms", uint32(m_scriptCache.size()), ElunaUtil::GetTimeDiff(oldMSTime));
// set the cache state to ready
m_cacheState = SCRIPT_CACHE_READY;
}
int ElunaLoader::LoadBytecodeChunk(lua_State* /*L*/, uint8* bytes, size_t len, BytecodeBuffer* buffer)
{
for (size_t i = 0; i < len; i++)
buffer->push_back(bytes[i]);
return 0;
}
// Finds lua script files from given path (including subdirectories) and pushes them to scripts
void ElunaLoader::ReadFiles(lua_State* L, std::string path)
{
std::string lua_folderpath = sElunaConfig->GetConfig(CONFIG_ELUNA_SCRIPT_PATH);
ELUNA_LOG_DEBUG("[Eluna]: ReadFiles from path `%s`", path.c_str());
fs::path someDir(path);
fs::directory_iterator end_iter;
if (fs::exists(someDir) && fs::is_directory(someDir) && !fs::is_empty(someDir))
{
m_requirePath +=
path + "/?.lua;" +
path + "/?.ext;" +
path + "/?.moon;";
m_requirecPath +=
path + "/?.dll;" +
path + "/?.so;";
for (fs::directory_iterator dir_iter(someDir); dir_iter != end_iter; ++dir_iter)
{
std::string fullpath = dir_iter->path().generic_string();
// Check if file is hidden
#if defined ELUNA_WINDOWS
DWORD dwAttrib = GetFileAttributes(fullpath.c_str());
if (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_HIDDEN))
continue;
#else
std::string name = dir_iter->path().filename().generic_string().c_str();
if (name[0] == '.')
continue;
#endif
// load subfolder
if (fs::is_directory(dir_iter->status()))
{
ReadFiles(L, fullpath);
continue;
}
if (fs::is_regular_file(dir_iter->status()))
{
int32 mapId;
// strip base folder path and trailing slash from fullpath
std::string subfolder = dir_iter->path().generic_string();
subfolder = subfolder.erase(0, lua_folderpath.size() + 1);
// stringstream used for conversion
std::stringstream ss;
// push subfolder int to subMapId
ss << subfolder;
ss >> mapId;
// if this failed, then we load the script for all maps
if (ss.fail())
mapId = -1;
// just in case we have a subfolder named an int less than all..
if (mapId < -1)
mapId = -1;
// was file, try add
std::string filename = dir_iter->path().filename().generic_string();
ProcessScript(L, filename, fullpath, mapId);
}
}
}
}
bool ElunaLoader::CompileScript(lua_State* L, LuaScript& script)
{
// Attempt to load the file
int err = 0;
if (script.fileext == ".moon")
{
std::string str = "return require('moonscript').loadfile([[" + script.filepath+ "]])";
err = luaL_dostring(L, str.c_str());
} else
err = luaL_loadfile(L, script.filepath.c_str());
// If something bad happened, try to find an error.
if (err != 0)
{
ELUNA_LOG_ERROR("[Eluna]: CompileScript failed to load the Lua script `%s`.", script.filename.c_str());
Eluna::Report(L);
return false;
}
ELUNA_LOG_DEBUG("[Eluna]: CompileScript loaded Lua script `%s`", script.filename.c_str());
// Everything's OK so far, the script has been loaded, now we need to start dumping it to bytecode.
err = lua_dump(L, (lua_Writer)LoadBytecodeChunk, &script.bytecode);
if (err || script.bytecode.empty())
{
ELUNA_LOG_ERROR("[Eluna]: CompileScript failed to dump the Lua script `%s` to bytecode.", script.filename.c_str());
Eluna::Report(L);
return false;
}
ELUNA_LOG_DEBUG("[Eluna]: CompileScript dumped Lua script `%s` to bytecode.", script.filename.c_str());
// pop the loaded function from the stack
lua_pop(L, 1);
return true;
}
void ElunaLoader::ProcessScript(lua_State* L, std::string filename, const std::string& fullpath, int32 mapId)
{
ELUNA_LOG_DEBUG("[Eluna]: ProcessScript checking file `%s`", fullpath.c_str());
// split file name
std::size_t extDot = filename.find_last_of('.');
if (extDot == std::string::npos)
return;
std::string ext = filename.substr(extDot);
filename = filename.substr(0, extDot);
// check extension and add path to scripts to load
if (ext != ".lua" && ext != ".ext" && ext != ".moon")
return;
bool extension = ext == ".ext";
LuaScript script;
script.fileext = ext;
script.filename = filename;
script.filepath = fullpath;
script.modulepath = fullpath.substr(0, fullpath.length() - filename.length() - ext.length());
script.mapId = mapId;
// if compilation fails, we don't add the script
if (!CompileScript(L, script))
return;
if (extension)
m_extensions.push_back(script);
else
m_scripts.push_back(script);
ELUNA_LOG_DEBUG("[Eluna]: ProcessScript processed `%s` successfully", fullpath.c_str());
}
#if defined ELUNA_TRINITY
void ElunaLoader::InitializeFileWatcher()
{
std::string lua_folderpath = sElunaConfig->GetConfig(CONFIG_ELUNA_SCRIPT_PATH);
lua_scriptWatcher = lua_fileWatcher.addWatch(lua_folderpath, &elunaUpdateListener, true);
if (lua_scriptWatcher >= 0)
{
ELUNA_LOG_INFO("[Eluna]: Script reloader is listening on `%s`.", lua_folderpath.c_str());
}
else
{
ELUNA_LOG_INFO("[Eluna]: Failed to initialize the script reloader on `%s`.", lua_folderpath.c_str());
}
lua_fileWatcher.watch();
}
#endif
static bool ScriptPathComparator(const LuaScript& first, const LuaScript& second)
{
return first.filepath < second.filepath;
}
void ElunaLoader::CombineLists()
{
m_extensions.sort(ScriptPathComparator);
m_scripts.sort(ScriptPathComparator);
m_scriptCache.clear();
m_scriptCache.insert(m_scriptCache.end(), m_extensions.begin(), m_extensions.end());
m_scriptCache.insert(m_scriptCache.end(), m_scripts.begin(), m_scripts.end());
m_extensions.clear();
m_scripts.clear();
}
void ElunaLoader::ReloadElunaForMap(int mapId)
{
// reload the script cache asynchronously
ReloadScriptCache();
// If a mapid is provided but does not match any map or reserved id then only script storage is loaded
if (mapId != RELOAD_CACHE_ONLY)
{
if (mapId == RELOAD_GLOBAL_STATE || mapId == RELOAD_ALL_STATES)
#if defined ELUNA_TRINITY
if (Eluna* e = sWorld->GetEluna())
#else
if (Eluna* e = sWorld.GetEluna())
#endif
e->ReloadEluna();
#if defined ELUNA_TRINITY
sMapMgr->DoForAllMaps([&](Map* map)
#else
sMapMgr.DoForAllMaps([&](Map* map)
#endif
{
if (mapId == RELOAD_ALL_STATES || mapId == static_cast<int>(map->GetId()))
if (Eluna* e = map->GetEluna())
e->ReloadEluna();
}
);
}
}