/
databundle.cpp
122 lines (107 loc) · 3.21 KB
/
databundle.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
/** @file databundle.cpp Classic data files: PK3, WAD, LMP, DED, DEH.
*
* @authors Copyright (c) 2016 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, see:
* http://www.gnu.org/licenses</small>
*/
#include "doomsday/filesys/databundle.h"
#include "doomsday/filesys/datafolder.h"
#include "doomsday/filesys/datafile.h"
#include <de/ArchiveFeed>
using namespace de;
namespace internal
{
static char const *formatDescriptions[] =
{
"PK3 archive",
"WAD file",
"data lump",
"DED definitions",
"DeHackEd patch"
};
}
DENG2_PIMPL(DataBundle)
{
SafePtr<File const> source;
Format format;
Instance(Public *i, Format fmt) : Base(i), format(fmt)
{}
};
DataBundle::DataBundle(Format format, File const &source)
: d(new Instance(this, format))
{
d->source.reset(&source);
}
DataBundle::~DataBundle()
{}
String DataBundle::description() const
{
if(!d->source)
{
return "invalid data bundle";
}
return QString("%1 \"%2\"")
.arg(internal::formatDescriptions[d->format])
.arg(d->source->name().fileNameWithoutExtension());
}
IByteArray::Size DataBundle::size() const
{
if(d->source)
{
return d->source->size();
}
return 0;
}
void DataBundle::get(Offset at, Byte *values, Size count) const
{
if(!d->source)
{
throw File::InputError("DataBundle::get", "Source file has been destroyed");
}
d->source->as<ByteArrayFile>().get(at, values, count);
}
void DataBundle::set(Offset, Byte const *, Size)
{
throw File::OutputError("DataBundle::set", "Classic data formats are read-only");
}
File *DataBundle::Interpreter::interpretFile(File *sourceData) const
{
// Naive check using the file extension.
static struct { String str; Format format; } formats[] = {
{ ".pk3", Pk3 },
{ ".wad", Wad },
{ ".lmp", Lump },
{ ".ded", Ded },
{ ".deh", Dehacked }
};
String const ext = sourceData->name().fileNameExtension();
for(auto const &fmt : formats)
{
if(ext == fmt.str)
{
LOG_RES_VERBOSE("Interpreted ") << sourceData->description()
<< " as "
<< internal::formatDescriptions[fmt.format];
switch(fmt.format)
{
case Pk3:
return new DataFolder(fmt.format, *sourceData);
default:
return new DataFile(fmt.format, *sourceData);
}
}
}
// Was not interpreted.
return nullptr;
}