2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-08 17:44:56 +00:00

General: Make use of FopenUnique where applicable

Migrates to the hecl Fopen variant that automatically closes its
contained file handle if it goes out of scope.
This commit is contained in:
Lioncash
2019-08-26 14:37:19 -04:00
parent f48ebefa84
commit 1d3062b33f
9 changed files with 60 additions and 73 deletions

View File

@@ -466,9 +466,8 @@ bool PAKRouter<BRIDGETYPE>::extractResources(const BRIDGETYPE& pakBridge, bool f
if (force || cooked.isNone()) {
cooked.makeDirChain(false);
PAKEntryReadStream s = entryPtr->beginReadStream(*node);
FILE* fout = hecl::Fopen(cooked.getAbsolutePath().data(), _SYS_STR("wb"));
fwrite(s.data(), 1, s.length(), fout);
fclose(fout);
const auto fout = hecl::FopenUnique(cooked.getAbsolutePath().data(), _SYS_STR("wb"));
std::fwrite(s.data(), 1, s.length(), fout.get());
}
if (extractor.func_a) /* Doesn't need PAKRouter access */

View File

@@ -812,13 +812,13 @@ bool TXTR::Extract(PAKEntryReadStream& rs, const hecl::ProjectPath& outPath) {
uint16_t height = rs.readUint16Big();
uint32_t numMips = rs.readUint32Big();
FILE* fp = hecl::Fopen(outPath.getAbsolutePath().data(), _SYS_STR("wb"));
if (!fp) {
auto fp = hecl::FopenUnique(outPath.getAbsolutePath().data(), _SYS_STR("wb"));
if (fp == nullptr) {
Log.report(logvisor::Error, fmt(_SYS_STR("Unable to open '{}' for writing")), outPath.getAbsolutePath());
return false;
}
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, PNGErr, PNGWarn);
png_init_io(png, fp);
png_init_io(png, fp.get());
png_infop info = png_create_info_struct(png);
png_text textStruct = {};
@@ -862,7 +862,6 @@ bool TXTR::Extract(PAKEntryReadStream& rs, const hecl::ProjectPath& outPath) {
png_write_end(png, info);
png_write_flush(png);
png_destroy_write_struct(&png, &info);
fclose(fp);
return true;
}
@@ -1017,18 +1016,17 @@ static int GetNumPaletteEntriesForGCN(png_structp png, png_infop info) {
}
bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPath) {
FILE* inf = hecl::Fopen(inPath.getAbsolutePath().data(), _SYS_STR("rb"));
if (!inf) {
auto inf = hecl::FopenUnique(inPath.getAbsolutePath().data(), _SYS_STR("rb"));
if (inf == nullptr) {
Log.report(logvisor::Error, fmt(_SYS_STR("Unable to open '{}' for reading")), inPath.getAbsolutePath());
return false;
}
/* Validate PNG */
char header[8];
fread(header, 1, 8, inf);
std::fread(header, 1, sizeof(header), inf.get());
if (png_sig_cmp((png_const_bytep)header, 0, 8)) {
Log.report(logvisor::Error, fmt(_SYS_STR("invalid PNG signature in '{}'")), inPath.getAbsolutePath());
fclose(inf);
return false;
}
@@ -1036,25 +1034,22 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat
png_structp pngRead = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!pngRead) {
Log.report(logvisor::Error, fmt("unable to initialize libpng"));
fclose(inf);
return false;
}
png_infop info = png_create_info_struct(pngRead);
if (!info) {
Log.report(logvisor::Error, fmt("unable to initialize libpng info"));
fclose(inf);
png_destroy_read_struct(&pngRead, nullptr, nullptr);
return false;
}
if (setjmp(png_jmpbuf(pngRead))) {
Log.report(logvisor::Error, fmt(_SYS_STR("unable to initialize libpng I/O for '{}'")), inPath.getAbsolutePath());
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
png_init_io(pngRead, inf);
png_init_io(pngRead, inf.get());
png_set_sig_bytes(pngRead, 8);
png_read_info(pngRead, info);
@@ -1066,7 +1061,6 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat
if (width < 4 || height < 4) {
Log.report(logvisor::Error, fmt("image must be 4x4 or larger"));
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
@@ -1092,7 +1086,6 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat
if (bitDepth != 8) {
Log.report(logvisor::Error, fmt(_SYS_STR("'{}' is not 8 bits-per-channel")), inPath.getAbsolutePath());
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
@@ -1124,7 +1117,6 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat
break;
default:
Log.report(logvisor::Error, fmt(_SYS_STR("unsupported color type in '{}'")), inPath.getAbsolutePath());
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
@@ -1145,7 +1137,6 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat
if (setjmp(png_jmpbuf(pngRead))) {
Log.report(logvisor::Fatal, fmt(_SYS_STR("unable to read image in '{}'")), inPath.getAbsolutePath());
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
@@ -1178,7 +1169,7 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat
}
png_destroy_read_struct(&pngRead, &info, nullptr);
fclose(inf);
inf.reset();
/* Reduce mipmaps to minimum allowed dimensions */
unsigned minDimX, minDimY;
@@ -1338,18 +1329,17 @@ bool TXTR::Cook(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPat
}
bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outPath) {
FILE* inf = hecl::Fopen(inPath.getAbsolutePath().data(), _SYS_STR("rb"));
if (!inf) {
auto inf = hecl::FopenUnique(inPath.getAbsolutePath().data(), _SYS_STR("rb"));
if (inf == nullptr) {
Log.report(logvisor::Error, fmt(_SYS_STR("Unable to open '{}' for reading")), inPath.getAbsolutePath());
return false;
}
/* Validate PNG */
char header[8];
fread(header, 1, 8, inf);
std::fread(header, 1, sizeof(header), inf.get());
if (png_sig_cmp((png_const_bytep)header, 0, 8)) {
Log.report(logvisor::Error, fmt(_SYS_STR("invalid PNG signature in '{}'")), inPath.getAbsolutePath());
fclose(inf);
return false;
}
@@ -1357,25 +1347,22 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP
png_structp pngRead = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!pngRead) {
Log.report(logvisor::Error, fmt("unable to initialize libpng"));
fclose(inf);
return false;
}
png_infop info = png_create_info_struct(pngRead);
if (!info) {
Log.report(logvisor::Error, fmt("unable to initialize libpng info"));
fclose(inf);
png_destroy_read_struct(&pngRead, nullptr, nullptr);
return false;
}
if (setjmp(png_jmpbuf(pngRead))) {
Log.report(logvisor::Error, fmt(_SYS_STR("unable to initialize libpng I/O for '{}'")), inPath.getAbsolutePath());
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
png_init_io(pngRead, inf);
png_init_io(pngRead, inf.get());
png_set_sig_bytes(pngRead, 8);
png_read_info(pngRead, info);
@@ -1406,7 +1393,6 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP
if (bitDepth != 8) {
Log.report(logvisor::Error, fmt(_SYS_STR("'{}' is not 8 bits-per-channel")), inPath.getAbsolutePath());
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
@@ -1436,7 +1422,6 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP
break;
default:
Log.report(logvisor::Error, fmt(_SYS_STR("unsupported color type in '{}'")), inPath.getAbsolutePath());
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
@@ -1455,7 +1440,6 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP
if (setjmp(png_jmpbuf(pngRead))) {
Log.report(logvisor::Fatal, fmt(_SYS_STR("unable to read image in '{}'")), inPath.getAbsolutePath());
fclose(inf);
png_destroy_read_struct(&pngRead, &info, nullptr);
return false;
}
@@ -1521,7 +1505,7 @@ bool TXTR::CookPC(const hecl::ProjectPath& inPath, const hecl::ProjectPath& outP
}
png_destroy_read_struct(&pngRead, &info, nullptr);
fclose(inf);
inf.reset();
/* Perform box-filter mipmap */
if (numMips > 1) {