Skip to content

Commit

Permalink
AURORA: Use ScopedPtr/ScopedArray in the Nintendo DS classes
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 26, 2016
1 parent f6e64fd commit 703be64
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 86 deletions.
30 changes: 9 additions & 21 deletions src/aurora/cdpth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cstring>

#include "src/common/util.h"
#include "src/common/scopedptr.h"
#include "src/common/error.h"
#include "src/common/readstream.h"

Expand All @@ -43,17 +44,15 @@ struct ReadContext {
uint32 width;
uint32 height;

uint16 *depth;
Common::ScopedArray<uint16> depth;

ReadContext(Common::SeekableReadStream &c, uint32 w, uint32 h) :
cdpth(&c), width(w), height(h), depth(0) {
cdpth(&c), width(w), height(h) {
}

~ReadContext() {
for (Cells::iterator c = cells.begin(); c != cells.end(); ++c)
delete *c;

delete depth;
}
};

Expand All @@ -78,24 +77,13 @@ const uint16 *CDPTH::load(Common::SeekableReadStream &cdpth, uint32 width, uint3
throw;
}

const uint16 *depth = ctx.depth;
ctx.depth = 0;

return depth;
return ctx.depth.release();
}

const uint16 *CDPTH::load(Common::SeekableReadStream *cdpth, uint32 width, uint32 height) {
const uint16 *depth = 0;

try {
depth = load(*cdpth, width, height);
} catch (...) {
delete cdpth;
throw;
}
Common::ScopedPtr<Common::SeekableReadStream> stream(cdpth);

delete cdpth;
return depth;
return load(*stream, width, height);
}

static void loadCDPTH(ReadContext &ctx) {
Expand Down Expand Up @@ -152,14 +140,14 @@ static void checkConsistency(ReadContext &ctx) {
static void createDepth(ReadContext &ctx) {
/* Create the actual depth data, which is made up of 64x64 pixel cells. */

ctx.depth = new uint16[ctx.width * ctx.height];
std::memset(ctx.depth, 0xFF, ctx.width * ctx.height * sizeof(uint16));
ctx.depth.reset(new uint16[ctx.width * ctx.height]);
std::memset(ctx.depth.get(), 0xFF, ctx.width * ctx.height * sizeof(uint16));

const uint32 cellWidth = 64;
const uint32 cellHeight = 64;
const uint32 cellsX = ctx.width / cellWidth;

uint16 *data = ctx.depth;
uint16 *data = ctx.depth.get();
for (size_t i = 0; i < ctx.cells.size(); i++) {
Common::SeekableReadStream *cell = ctx.cells[i];
if (!cell)
Expand Down
21 changes: 9 additions & 12 deletions src/aurora/nitrofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/

#include "src/common/types.h"
#include "src/common/scopedptr.h"
#include "src/common/readstream.h"

#include "src/aurora/nitrofile.h"
Expand All @@ -58,18 +59,14 @@ Common::SeekableSubReadStreamEndian *NitroFile::open(Common::SeekableReadStream
}

Common::SeekableSubReadStreamEndian *NitroFile::open(Common::SeekableReadStream *stream) {
const size_t begin = stream->pos();
const size_t end = stream->size();

bool bigEndian = false;
try {
bigEndian = isBigEndian(*stream);
} catch (...) {
delete stream;
throw;
}

return new Common::SeekableSubReadStreamEndian(stream, begin, end, bigEndian, true);
Common::ScopedPtr<Common::SeekableReadStream> nitroStream(stream);

const size_t begin = nitroStream->pos();
const size_t end = nitroStream->size();

const bool bigEndian = isBigEndian(*nitroStream);

return new Common::SeekableSubReadStreamEndian(nitroStream.release(), begin, end, bigEndian, true);
}

} // End of namespace Aurora
64 changes: 28 additions & 36 deletions src/aurora/nsbtxfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

#include <cassert>

#include <boost/scope_exit.hpp>

#include "src/common/util.h"
#include "src/common/strutil.h"
#include "src/common/error.h"
Expand All @@ -74,28 +76,22 @@ namespace Aurora {

NSBTXFile::ReadContext::ReadContext(Common::SeekableSubReadStreamEndian &n, const Texture &t,
Common::WriteStream &s) :
texture(&t), palette(0), nsbtx(&n), stream(&s) {
texture(&t), nsbtx(&n), stream(&s) {
}

NSBTXFile::ReadContext::~ReadContext() {
delete[] palette;
}


NSBTXFile::NSBTXFile(Common::SeekableReadStream *nsbtx) : _nsbtx(0) {
NSBTXFile::NSBTXFile(Common::SeekableReadStream *nsbtx) {
assert(nsbtx);

try {
_nsbtx = open(nsbtx);
load(*_nsbtx);
} catch (...) {
delete _nsbtx;
throw;
}
_nsbtx.reset(open(nsbtx));

load(*_nsbtx);
}

NSBTXFile::~NSBTXFile() {
delete _nsbtx;
}

const Archive::ResourceList &NSBTXFile::getResources() const {
Expand Down Expand Up @@ -278,28 +274,22 @@ void NSBTXFile::getPalette(ReadContext &ctx) const {
if (!palette)
throw Common::Exception("Couldn't find a palette for texture \"%s\"", ctx.texture->name.c_str());

byte *palData = new byte[size];
memset(palData, 0, size);

try {
ctx.nsbtx->seek(palette->offset);
Common::ScopedArray<byte> palData(new byte[size]);
memset(palData.get(), 0, size);

const uint16 palDataSize = MIN<size_t>(size, ((ctx.nsbtx->size() - ctx.nsbtx->pos()) / 2) * 3);
ctx.nsbtx->seek(palette->offset);

for (uint16 i = 0; i < palDataSize; i += 3) {
const uint16 pixel = ctx.nsbtx->readUint16();
const uint16 palDataSize = MIN<size_t>(size, ((ctx.nsbtx->size() - ctx.nsbtx->pos()) / 2) * 3);

palData[i + 0] = ( pixel & 0x1F) << 3;
palData[i + 1] = ((pixel >> 5) & 0x1F) << 3;
palData[i + 2] = ((pixel >> 10) & 0x1F) << 3;
}
for (uint16 i = 0; i < palDataSize; i += 3) {
const uint16 pixel = ctx.nsbtx->readUint16();

} catch (...) {
delete[] palData;
throw;
palData[i + 0] = ( pixel & 0x1F) << 3;
palData[i + 1] = ((pixel >> 5) & 0x1F) << 3;
palData[i + 2] = ((pixel >> 10) & 0x1F) << 3;
}

ctx.palette = palData;
ctx.palette.reset(palData.release());
}

void NSBTXFile::getTexture(const ReadContext &ctx) {
Expand Down Expand Up @@ -341,18 +331,20 @@ Common::SeekableReadStream *NSBTXFile::getResource(uint32 index, bool UNUSED(try
throw Common::Exception("Texture index out of range (%u/%u)", index, (uint)_textures.size());

Common::MemoryWriteStreamDynamic stream(false, getITEXSize(_textures[index]));
bool success = false;

try {
ReadContext ctx(*_nsbtx, _textures[index], stream);
writeITEXHeader(ctx);
BOOST_SCOPE_EXIT( (&success) (&stream)) {
if (!success)
stream.dispose();
} BOOST_SCOPE_EXIT_END

getPalette(ctx);
getTexture(ctx);
ReadContext ctx(*_nsbtx, _textures[index], stream);
writeITEXHeader(ctx);

} catch (...) {
delete[] stream.getData();
throw;
}
getPalette(ctx);
getTexture(ctx);

success = true;

return new Common::MemoryReadStream(stream.getData(), stream.size(), true);
}
Expand Down
6 changes: 4 additions & 2 deletions src/aurora/nsbtxfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <vector>

#include "src/common/types.h"
#include "src/common/scopedptr.h"
#include "src/common/ustring.h"

#include "src/aurora/types.h"
Expand Down Expand Up @@ -100,7 +101,8 @@ class NSBTXFile : public Archive, public NitroFile {

struct ReadContext {
const Texture *texture;
const byte *palette;

Common::ScopedArray<const byte> palette;

Common::SeekableSubReadStreamEndian *nsbtx;
Common::WriteStream *stream;
Expand All @@ -114,7 +116,7 @@ class NSBTXFile : public Archive, public NitroFile {


/** The name of the NSBTX file. */
Common::SeekableSubReadStreamEndian *_nsbtx;
Common::ScopedPtr<Common::SeekableSubReadStreamEndian> _nsbtx;

/** External list of resource names and types. */
ResourceList _resources;
Expand Down
25 changes: 10 additions & 15 deletions src/aurora/smallfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Decompressing "small" files, Nintendo DS LZSS (types 0x00 and 0x10), found in Sonic.
*/

#include "src/common/scopedptr.h"
#include "src/common/error.h"
#include "src/common/memreadstream.h"
#include "src/common/memwritestream.h"
Expand Down Expand Up @@ -131,26 +132,28 @@ void Small::decompress(Common::ReadStream &small, Common::WriteStream &out) {
}

Common::SeekableReadStream *Small::decompress(Common::SeekableReadStream *small) {
Common::ScopedPtr<Common::SeekableReadStream> in(small);

uint32 type, size;
readSmallHeader(*small, type, size);
readSmallHeader(*in, type, size);

const size_t pos = in->pos();

if (type == 0x00)
// Uncompressed. Just return a sub stream for the raw data
return new Common::SeekableSubReadStream(small, small->pos(), small->pos() + size, true);
return new Common::SeekableSubReadStream(in.release(), pos, pos + size, true);

Common::MemoryWriteStreamDynamic out(false, size);

try {
::Aurora::decompress(*small, out, type, size);
::Aurora::decompress(*in, out, type, size);
} catch (Common::Exception &e) {
delete small;
out.dispose();

e.add("Failed to decompress \"small\" file");
throw e;
}

delete small;
return new Common::MemoryReadStream(out.getData(), out.size(), true);
}

Expand All @@ -173,17 +176,9 @@ Common::SeekableReadStream *Small::decompress(Common::ReadStream &small) {
}

Common::SeekableReadStream *Small::decompress(Common::ReadStream *small) {
Common::SeekableReadStream *decompressed = 0;

try {
decompressed = decompress(*small);
} catch (...) {
delete small;
throw;
}
Common::ScopedPtr<Common::ReadStream> in(small);

delete small;
return decompressed;
return decompress(*in);
}

} // End of namespace Aurora

0 comments on commit 703be64

Please sign in to comment.