forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseevents.cpp
177 lines (144 loc) · 4.86 KB
/
baseevents.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
// Copyright 2023 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#include "otpch.h"
#include "baseevents.h"
#include "luascript.h"
#include "tools.h"
extern LuaEnvironment g_luaEnvironment;
bool BaseEvents::loadFromXml()
{
if (loaded) {
std::cout << "[Error - BaseEvents::loadFromXml] It's already loaded." << std::endl;
return false;
}
std::string scriptsName{getScriptBaseName()};
std::string basePath = "data/" + scriptsName + "/";
if (getScriptInterface().loadFile(basePath + "lib/" + scriptsName + ".lua") == -1) {
std::cout << "[Warning - BaseEvents::loadFromXml] Can not load " << scriptsName << " lib/" << scriptsName
<< ".lua" << std::endl;
}
std::string filename = basePath + scriptsName + ".xml";
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename.c_str());
if (!result) {
printXMLError("Error - BaseEvents::loadFromXml", filename, result);
return false;
}
loaded = true;
for (auto node : doc.child(scriptsName.c_str()).children()) {
Event_ptr event = getEvent(node.name());
if (!event) {
continue;
}
if (!event->configureEvent(node)) {
std::cout << "[Warning - BaseEvents::loadFromXml] Failed to configure event" << std::endl;
continue;
}
bool success;
pugi::xml_attribute scriptAttribute = node.attribute("script");
if (scriptAttribute) {
std::string scriptFile = "scripts/" + std::string(scriptAttribute.as_string());
success = event->checkScript(basePath, scriptsName, scriptFile) && event->loadScript(basePath + scriptFile);
if (node.attribute("function")) {
event->loadFunction(node.attribute("function"), true);
}
} else {
success = event->loadFunction(node.attribute("function"), false);
}
if (success) {
registerEvent(std::move(event), node);
}
}
return true;
}
bool BaseEvents::reload()
{
loaded = false;
clear(false);
return loadFromXml();
}
void BaseEvents::reInitState(bool fromLua)
{
if (!fromLua) {
getScriptInterface().reInitState();
}
}
Event::Event(LuaScriptInterface* interface) : scriptInterface(interface) {}
bool Event::checkScript(const std::string& basePath, const std::string& scriptsName,
const std::string& scriptFile) const
{
LuaScriptInterface* testInterface = g_luaEnvironment.getTestInterface();
testInterface->reInitState();
if (testInterface->loadFile(std::string(basePath + "lib/" + scriptsName + ".lua")) == -1) {
std::cout << "[Warning - Event::checkScript] Can not load " << scriptsName << " lib/" << scriptsName << ".lua"
<< std::endl;
}
if (scriptId != 0) {
std::cout << "[Failure - Event::checkScript] scriptid = " << scriptId << std::endl;
return false;
}
if (testInterface->loadFile(basePath + scriptFile) == -1) {
std::cout << "[Warning - Event::checkScript] Can not load script: " << scriptFile << std::endl;
std::cout << testInterface->getLastLuaError() << std::endl;
return false;
}
int32_t id = testInterface->getEvent(getScriptEventName());
if (id == -1) {
std::cout << "[Warning - Event::checkScript] Event " << getScriptEventName() << " not found. " << scriptFile
<< std::endl;
return false;
}
return true;
}
bool Event::loadScript(const std::string& scriptFile)
{
if (!scriptInterface || scriptId != 0) {
std::cout << "Failure: [Event::loadScript] scriptInterface == nullptr. scriptid = " << scriptId << std::endl;
return false;
}
if (scriptInterface->loadFile(scriptFile) == -1) {
std::cout << "[Warning - Event::loadScript] Can not load script. " << scriptFile << std::endl;
std::cout << scriptInterface->getLastLuaError() << std::endl;
return false;
}
int32_t id = scriptInterface->getEvent(getScriptEventName());
if (id == -1) {
std::cout << "[Warning - Event::loadScript] Event " << getScriptEventName() << " not found. " << scriptFile
<< std::endl;
return false;
}
scripted = true;
scriptId = id;
return true;
}
bool Event::loadCallback()
{
if (!scriptInterface || scriptId != 0) {
std::cout << "Failure: [Event::loadCallback] scriptInterface == nullptr. scriptid = " << scriptId << std::endl;
return false;
}
int32_t id = scriptInterface->getEvent();
if (id == -1) {
std::cout << "[Warning - Event::loadCallback] Event " << getScriptEventName() << " not found. " << std::endl;
return false;
}
scripted = true;
scriptId = id;
return true;
}
bool CallBack::loadCallBack(LuaScriptInterface* interface, const std::string& name)
{
if (!interface) {
std::cout << "Failure: [CallBack::loadCallBack] scriptInterface == nullptr" << std::endl;
return false;
}
scriptInterface = interface;
int32_t id = scriptInterface->getEvent(name);
if (id == -1) {
std::cout << "[Warning - CallBack::loadCallBack] Event " << name << " not found." << std::endl;
return false;
}
scriptId = id;
loaded = true;
return true;
}