Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataFile represents classic data files (WAD, LMP, etc.) in the file tree. Data files are accessible as byte arrays.
- Loading branch information
Showing
5 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
doomsday/apps/libdoomsday/include/doomsday/filesys/datafile.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** @file datafile.h FS2 File for 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> | ||
*/ | ||
|
||
#ifndef LIBDOOMSDAY_DATAFILE_H | ||
#define LIBDOOMSDAY_DATAFILE_H | ||
|
||
#include "../libdoomsday.h" | ||
#include <de/filesys/IInterpreter> | ||
#include <de/ByteArrayFile> | ||
|
||
/** | ||
* Interpreter (for libcore FS) for classic data files: PK3, WAD, LMP, DED, DEH. | ||
* | ||
* Generates Doomsday 2 compatible metadata for data files, allowing them to | ||
* be treated as packages at runtime. | ||
* | ||
* As packages, DataFile makes sure that the data gets loaded and unloaded | ||
* when games are loaded (and in the right order). The data is actually loaded | ||
* using the resource subsystem. To facilitate that, data file contents are | ||
* available as plain byte arrays. | ||
*/ | ||
class LIBDOOMSDAY_PUBLIC DataFile : public de::ByteArrayFile | ||
{ | ||
public: | ||
enum Format { Pk3, Wad, Lump, Ded, Dehacked }; | ||
|
||
public: | ||
~DataFile(); | ||
|
||
de::String describe() const; | ||
Format format() const; | ||
|
||
// Implements IByteArray. | ||
Size size() const; | ||
void get(Offset at, Byte *values, Size count) const; | ||
void set(Offset at, Byte const *values, Size count); | ||
|
||
public: | ||
struct LIBDOOMSDAY_PUBLIC Interpreter : public de::filesys::IInterpreter { | ||
de::File *interpretFile(de::File *sourceData) const override; | ||
}; | ||
|
||
protected: | ||
/** | ||
* Constructs a DataFile. The constructor is protected because DataFile is | ||
* used as an interpreter so it gets created via IInterpreter. | ||
* | ||
* @param format Classic data file format. | ||
* @param name Name of the file. | ||
*/ | ||
DataFile(Format format, de::String const &name = ""); | ||
|
||
private: | ||
DENG2_PRIVATE(d) | ||
}; | ||
|
||
#endif // LIBDOOMSDAY_DATAFILE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** @file datafile.cpp FS2 File for 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/datafile.h" | ||
|
||
using namespace de; | ||
|
||
namespace internal | ||
{ | ||
static char const *formatDescriptions[] = | ||
{ | ||
"PK3 archive", | ||
"WAD file", | ||
"data lump", | ||
"DED definitions", | ||
"DeHackEd patch" | ||
}; | ||
} | ||
|
||
DENG2_PIMPL(DataFile) | ||
{ | ||
Format format; | ||
|
||
Instance(Public *i, Format fmt) : Base(i), format(fmt) | ||
{} | ||
}; | ||
|
||
DataFile::DataFile(Format format, String const &name) | ||
: ByteArrayFile(name) | ||
, d(new Instance(this, format)) | ||
{} | ||
|
||
DataFile::~DataFile() | ||
{ | ||
DENG2_FOR_AUDIENCE2(Deletion, i) i->fileBeingDeleted(*this); | ||
audienceForDeletion().clear(); | ||
deindex(); | ||
} | ||
|
||
String DataFile::describe() const | ||
{ | ||
return QString("%1 \"%2\"") | ||
.arg(internal::formatDescriptions[d->format]) | ||
.arg(name().fileNameWithoutExtension()); | ||
} | ||
|
||
IByteArray::Size DataFile::size() const | ||
{ | ||
DENG2_ASSERT(source()); | ||
return source()->size(); | ||
} | ||
|
||
void DataFile::get(Offset at, Byte *values, Size count) const | ||
{ | ||
DENG2_ASSERT(source()); | ||
source()->as<ByteArrayFile>().get(at, values, count); | ||
} | ||
|
||
void DataFile::set(Offset at, Byte const *values, Size count) | ||
{ | ||
DENG2_ASSERT(source()); | ||
source()->as<ByteArrayFile>().set(at, values, count); | ||
} | ||
|
||
File *DataFile::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]; | ||
|
||
auto *data = new DataFile(fmt.format, sourceData->name()); | ||
data->setSource(sourceData); | ||
return data; | ||
} | ||
} | ||
|
||
// Was not interpreted. | ||
return nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters