/
module.cpp
202 lines (169 loc) · 5.83 KB
/
module.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
/** @file module.cpp Action Code Script (ACS) module.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2015 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 1999 Activision
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "common.h" // IS_CLIENT
#include "acs/module.h"
#include <QList>
#include <QMap>
#include <QVector>
#include <de/Log>
#include "acs/interpreter.h" // ACS_INTERPRETER_MAX_SCRIPT_ARGS
#include "gamesession.h"
using namespace de;
namespace acs {
DENG2_PIMPL_NOREF(Module)
{
Block pcode;
QVector<EntryPoint> entryPoints;
QMap<int, EntryPoint *> epByScriptNumberLut;
QList<String> constants;
void buildEntryPointLut()
{
epByScriptNumberLut.clear();
for(auto &ep : entryPoints)
{
epByScriptNumberLut.insert(ep.scriptNumber, &ep);
}
}
};
Module::Module() : d(new Instance)
{}
bool Module::recognize(File1 const &file) // static
{
if(file.size() <= 4) return false;
// ACS bytecode begins with the magic identifier "ACS".
Block magic(4);
const_cast<File1 &>(file).read(magic.data(), 0, 4);
if(!magic.startsWith("ACS")) return false;
// ZDoom uses the fourth byte for versioning of their extended formats.
// Currently such formats are not supported.
return magic.at(3) == 0;
}
Module *Module::newFromBytecode(Block const &bytecode) // static
{
DENG2_ASSERT(!IS_CLIENT);
LOG_AS("acs::Module");
std::unique_ptr<Module> module(new Module);
// Copy the complete bytecode data into a local buffer (we'll be randomly
// accessing this frequently).
module->d->pcode = bytecode;
de::Reader from(module->d->pcode);
dint32 magic, scriptInfoOffset;
from >> magic >> scriptInfoOffset;
// Read script entry point info.
from.seek(scriptInfoOffset - from.offset());
dint32 numEntryPoints;
from >> numEntryPoints;
module->d->entryPoints.reserve(numEntryPoints);
for(dint32 i = 0; i < numEntryPoints; ++i)
{
#define OPEN_SCRIPTS_BASE 1000
EntryPoint ep;
from >> ep.scriptNumber;
if(ep.scriptNumber >= OPEN_SCRIPTS_BASE)
{
ep.scriptNumber -= OPEN_SCRIPTS_BASE;
ep.startWhenMapBegins = true;
}
dint32 offset;
from >> offset;
if(offset > module->d->pcode.size())
{
throw FormatError("acs::Module", "Invalid script entrypoint offset");
}
ep.pcodePtr = (int const *)(module->d->pcode.constData() + offset);
from >> ep.scriptArgCount;
if(ep.scriptArgCount > ACS_INTERPRETER_MAX_SCRIPT_ARGS)
{
throw FormatError("acs::Module", "Too many script arguments (" + String::number(ep.scriptArgCount) + " > " + String::number(ACS_INTERPRETER_MAX_SCRIPT_ARGS) + ")");
}
module->d->entryPoints << ep; // makes a copy.
#undef OPEN_SCRIPTS_BASE
}
// Prepare a script-number => EntryPoint LUT.
module->d->buildEntryPointLut();
// Read constant (string-)values.
dint32 numConstants;
from >> numConstants;
QVector<dint32> constantOffsets;
constantOffsets.reserve(numConstants);
for(dint32 i = 0; i < numConstants; ++i)
{
dint32 offset;
from >> offset;
constantOffsets << offset;
}
for(auto const &offset : constantOffsets)
{
Block utf;
from.setOffset(offset);
from.readUntil(utf, 0);
module->d->constants << String::fromUtf8(utf);
}
return module.release();
}
Module *Module::newFromFile(File1 const &file) // static
{
DENG2_ASSERT(!IS_CLIENT);
LOG_AS("acs::Module");
LOG_SCR_VERBOSE("Loading from %s:%s...")
<< NativePath(file.container().composePath()).pretty()
<< file.name();
// Buffer the whole file.
Block buffer(file.size());
const_cast<File1 &>(file).read(buffer.data());
return newFromBytecode(buffer);
}
String Module::constant(int stringNumber) const
{
if(stringNumber >= 0 && stringNumber < d->constants.count())
{
return d->constants[stringNumber];
}
/// @throw MissingConstantError Invalid constant (string-)value number specified.
throw MissingConstantError("acs::Module::constant", "Unknown constant #" + String::number(stringNumber));
}
int Module::entryPointCount() const
{
return d->entryPoints.count();
}
bool Module::hasEntryPoint(int scriptNumber) const
{
return d->epByScriptNumberLut.contains(scriptNumber);
}
Module::EntryPoint const &Module::entryPoint(int scriptNumber) const
{
if(hasEntryPoint(scriptNumber)) return *d->epByScriptNumberLut[scriptNumber];
/// @throw MissingEntryPointError Invalid script number specified.
throw MissingEntryPointError("acs::Module::entryPoint", "Unknown script #" + String::number(scriptNumber));
}
LoopResult Module::forAllEntryPoints(std::function<LoopResult (EntryPoint &)> func) const
{
for(EntryPoint &ep : d->entryPoints)
{
if(auto result = func(ep)) return result;
}
return LoopContinue;
}
Block const &Module::pcode() const
{
return d->pcode;
}
} // namespace acs