script.cpp

main 100 lines · 3.1 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "otpch.h"
21
22#include "script.h"
23#include <boost/filesystem.hpp>
24#include "configmanager.h"
25
26extern LuaEnvironment g_luaEnvironment;
27extern ConfigManager g_config;
28
29Scripts::Scripts() :
30 scriptInterface("Scripts Interface")
31{
32 scriptInterface.initState();
33}
34
35Scripts::~Scripts()
36{
37 scriptInterface.reInitState();
38}
39
40bool Scripts::loadScripts(std::string folderName, bool isLib, bool reload)
41{
42 namespace fs = boost::filesystem;
43
44 const auto dir = fs::current_path() / "data" / folderName;
45 if(!fs::exists(dir) || !fs::is_directory(dir)) {
46 std::cout << "[Warning - Scripts::loadScripts] Can not load folder '" << folderName << "'." << std::endl;
47 return false;
48 }
49
50 fs::recursive_directory_iterator endit;
51 std::vector<fs::path> v;
52 std::string disable = ("#");
53 for(fs::recursive_directory_iterator it(dir); it != endit; ++it) {
54 auto fn = it->path().parent_path().filename();
55 if ((fn == "lib" && !isLib) || fn == "events") {
56 continue;
57 }
58 if(fs::is_regular_file(*it) && it->path().extension() == ".lua") {
59 size_t found = it->path().filename().string().find(disable);
60 if (found != std::string::npos) {
61 if (g_config.getBoolean(ConfigManager::SCRIPTS_CONSOLE_LOGS)) {
62 std::cout << "> " << it->path().filename().string() << " [disabled]" << std::endl;
63 }
64 continue;
65 }
66 v.push_back(it->path());
67 }
68 }
69 sort(v.begin(), v.end());
70 std::string redir;
71 for (auto it = v.begin(); it != v.end(); ++it) {
72 const std::string scriptFile = it->string();
73 if (!isLib) {
74 if (redir.empty() || redir != it->parent_path().string()) {
75 auto p = fs::path(it->relative_path());
76 if (g_config.getBoolean(ConfigManager::SCRIPTS_CONSOLE_LOGS)) {
77 std::cout << ">> [" << p.parent_path().filename() << "]" << std::endl;
78 }
79 redir = it->parent_path().string();
80 }
81 }
82
83 if(scriptInterface.loadFile(scriptFile) == -1) {
84 std::cout << "> " << it->filename().string() << " [error]" << std::endl;
85 std::cout << "^ " << scriptInterface.getLastLuaError() << std::endl;
86 continue;
87 }
88
89 if (g_config.getBoolean(ConfigManager::SCRIPTS_CONSOLE_LOGS)) {
90 if (!reload) {
91 std::cout << "> " << it->filename().string() << " [loaded]" << std::endl;
92 } else {
93 std::cout << "> " << it->filename().string() << " [reloaded]" << std::endl;
94 }
95 }
96 }
97
98 return true;
99}
100
Top