Skip to content

Commit

Permalink
libdoomsday: Added DataFile
Browse files Browse the repository at this point in the history
DataFile represents classic data files (WAD, LMP, etc.) in the file
tree. Data files are accessible as byte arrays.
  • Loading branch information
skyjake committed Jan 9, 2016
1 parent 1b6948f commit d2d851b
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 4 deletions.
72 changes: 72 additions & 0 deletions doomsday/apps/libdoomsday/include/doomsday/filesys/datafile.h
@@ -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
6 changes: 5 additions & 1 deletion doomsday/apps/libdoomsday/src/doomsdayapp.cpp
Expand Up @@ -13,12 +13,13 @@
* 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>
* http://www.gnu.org/licenses</small>
*/

#include "doomsday/doomsdayapp.h"
#include "doomsday/filesys/sys_direc.h"
#include "doomsday/filesys/fs_util.h"
#include "doomsday/filesys/datafile.h"
#include "doomsday/paths.h"

#include <de/App>
Expand Down Expand Up @@ -173,6 +174,9 @@ DoomsdayApp::DoomsdayApp(Players::Constructor playerConstructor)
{
DENG2_ASSERT(!theDoomsdayApp);
theDoomsdayApp = this;

static DataFile::Interpreter intrpDataFile;
App::fileSystem().addInterpreter(intrpDataFile);
}

void DoomsdayApp::determineGlobalPaths()
Expand Down
107 changes: 107 additions & 0 deletions doomsday/apps/libdoomsday/src/filesys/datafile.cpp
@@ -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;
}
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/filesys/file.h
Expand Up @@ -64,7 +64,7 @@ class Feed;
*
* Subclasses have some special requirements for their destructors:
* - deindex() must be called in all subclass destructors so that the instances indexed
* under the subclasses' type are removed from the file system's index also.
* under the subclasses' type are removed from the file system's index, too.
* - The file must be automatically flushed before it gets destroyed (see flush()).
* - The deletion audience must be notified and @c audienceForDeletion must be cleared
* afterwards.
Expand Down
4 changes: 2 additions & 2 deletions doomsday/sdk/libcore/src/filesys/file.cpp
Expand Up @@ -145,7 +145,7 @@ String File::description() const
{
if(parent())
{
desc += " [path \"" + path() + "\"]";
desc += " (path \"" + path() + "\")";
}
}

Expand All @@ -161,7 +161,7 @@ String File::description() const
{
if(source() != this)
{
desc += " (data sourced from " + source()->description() + ")";
desc += "; data sourced from " + source()->description();
}
}

Expand Down

0 comments on commit d2d851b

Please sign in to comment.