/
importdeh.cpp
218 lines (184 loc) · 6.37 KB
/
importdeh.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
/** @file importdeh.cpp DeHackEd patch reader plugin for Doomsday Engine.
*
* @authors Copyright © 2013-2014 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2012-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @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 "importdeh.h"
#include <QDir>
#include <QFile>
#include <doomsday/filesys/lumpindex.h>
#include <doomsday/resource/bundles.h>
#include <doomsday/DoomsdayApp>
#include <de/App>
#include <de/CommandLine>
#include <de/Block>
#include <de/Log>
#include <de/String>
#include "dehreader.h"
using namespace de;
// Global handle on the engine's definition databases.
ded_t *ded;
// This is the original data before it gets replaced by any patches.
ded_sprid_t origSpriteNames[NUMSPRITES];
String origActionNames[NUMSTATES];
static void backupData()
{
for (int i = 0; i < NUMSPRITES && i < ded->sprites.size(); i++)
{
qstrncpy(origSpriteNames[i].id, ded->sprites[i].id, DED_SPRITEID_LEN + 1);
}
for (int i = 0; i < NUMSTATES && i < ded->states.size(); i++)
{
origActionNames[i] = ded->states[i].gets("action");
}
}
static void readLump(LumpIndex const &lumpIndex, lumpnum_t lumpNum)
{
if (0 > lumpNum || lumpNum >= lumpIndex.size())
{
LOG_AS("DehRead::readLump");
LOG_WARNING("Invalid lump index #%i, ignoring.") << lumpNum;
return;
}
File1 &lump = lumpIndex[lumpNum];
size_t len = lump.size();
Block deh = Block::fromRawData(reinterpret_cast<char const *>(lump.cache()), len);
/// @attention Results in a deep-copy of the lump data into the Block
/// thus the cached lump can be released after this call.
///
/// @todo Do not use a local buffer - read using QTextStream.
lump.unlock();
/// @todo Custom status for contained files is not inherited from the container?
bool lumpIsCustom = (lump.isContained()? lump.container().hasCustom() : lump.hasCustom());
LOG_RES_MSG("Applying DeHackEd patch lump #%i \"%s:%s\"%s")
<< lumpNum
<< NativePath(lump.container().composePath()).pretty()
<< lump.name()
<< (lumpIsCustom? " (custom)" : "");
readDehPatch(deh, lumpIsCustom, NoInclude | IgnoreEOF);
}
static void readFile(String const &sourcePath, bool sourceIsCustom = true)
{
LOG_AS("DehRead::readFile");
QFile file(sourcePath);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
LOG_WARNING("Failed opening \"%s\" for read, aborting...") << QDir::toNativeSeparators(sourcePath);
return;
}
LOG_RES_MSG("Applying DeHackEd patch file \"%s\"%s")
<< NativePath(sourcePath).pretty()
<< (sourceIsCustom? " (custom)" : "");
readDehPatch(file.readAll(), sourceIsCustom, IgnoreEOF);
}
static void readFile2(String const &path, bool sourceIsCustom = true)
{
LOG_AS("DehRead::readFile2");
if (File const *file = App::rootFolder().tryLocate<File const>(path))
{
LOG_RES_MSG("Applying %s%s")
<< file->description()
<< (sourceIsCustom? " (custom)" : "");
Block deh;
*file >> deh;
readDehPatch(deh, sourceIsCustom, IgnoreEOF);
}
else
{
LOG_RES_WARNING("\"%s\" not found") << path;
}
}
static void readPatchLumps(LumpIndex const &lumpIndex)
{
bool const readAll = DENG2_APP->commandLine().check("-alldehs");
for (int i = lumpIndex.size() - 1; i >= 0; i--)
{
if (lumpIndex[i].name().fileNameExtension().toLower() == ".deh")
{
readLump(lumpIndex, i);
if (!readAll) return;
}
}
}
static void readPatchFiles()
{
// Patches may be loaded as data bundles.
for (DataBundle const *bundle : DataBundle::loadedBundles())
{
if (bundle->format() == DataBundle::Dehacked)
{
for (Value const *path : bundle->packageMetadata().geta("dataFiles").elements())
{
readFile2(path->asText());
}
}
}
// Command line options.
CommandLine &cmdLine = DENG2_APP->commandLine();
for (int p = 0; p < cmdLine.count(); ++p)
{
char const *arg = *(cmdLine.argv() + p);
if (!cmdLine.matches("-deh", arg)) continue;
while (++p != cmdLine.count() && !cmdLine.isOption(p))
{
cmdLine.makeAbsolutePath(p);
readFile(NativePath(*(cmdLine.argv() + p)));
}
p--;/* For ArgIsOption(p) necessary, for p==Argc() harmless */
}
}
/**
* This will be called after the engine has loaded all definitions but before
* the data they contain has been initialized.
*/
int DefsHook(int /*hook_type*/, int /*parm*/, void *data)
{
// Grab the DED definition handle supplied by the engine.
ded = reinterpret_cast<ded_t *>(data);
backupData();
// Check for DEHACKED lumps.
readPatchLumps(*reinterpret_cast<de::LumpIndex const *>(F_LumpIndex()));
// Process all patch files specified with -deh options on the command line.
readPatchFiles();
return true;
}
/**
* This function is called automatically when the plugin is loaded.
* We let the engine know what we'd like to do.
*/
void DP_Initialize()
{
Plug_AddHook(HOOK_DEFS, DefsHook);
}
/**
* Declares the type of the plugin so the engine knows how to treat it. Called
* automatically when the plugin is loaded.
*/
extern "C" char const *deng_LibraryType()
{
return "deng-plugin/generic";
}
DENG_DECLARE_API(Base);
DENG_DECLARE_API(Con);
DENG_DECLARE_API(Def);
DENG_DECLARE_API(F);
DENG_API_EXCHANGE(
DENG_GET_API(DE_API_BASE, Base);
DENG_GET_API(DE_API_CONSOLE, Con);
DENG_GET_API(DE_API_DEFINITIONS, Def);
DENG_GET_API(DE_API_FILE_SYSTEM, F);
)