Skip to content

Commit

Permalink
Driver now uses fatalerror method
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Aug 8, 2023
1 parent 6821d3f commit e6c052a
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 44 deletions.
3 changes: 2 additions & 1 deletion include/errors_warnings.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct ErrorsAndWarnings {
};

// Internal compiler errors (due to bug in the compiler)
void internalerror_custom(std::string customMessage);
void internalerror(std::string message);
void internalerror_numPalettesInPrimaryNeqPrimaryPalettesSize(std::string context, std::size_t configNumPalettesPrimary,
std::size_t primaryPalettesSize);
void internalerror_unknownCompilerMode(std::string context);
Expand All @@ -45,6 +45,7 @@ void error_invalidAlphaValue(ErrorsAndWarnings &err, const RGBATile &tile, std::
std::size_t col);

// Fatal compilation errors (due to bad user input), fatal errors die immediately
void fatalerror(ErrorsAndWarnings &err, const InputPaths &inputs, CompilerMode mode, std::string message);
void fatalerror_missingRequiredAnimFrameFile(ErrorsAndWarnings &err, const InputPaths &inputs, CompilerMode mode,
const std::string &animation, std::size_t index);
void fatalerror_tooManyUniqueColorsTotal(ErrorsAndWarnings &err, const InputPaths &inputs, CompilerMode mode,
Expand Down
1 change: 1 addition & 0 deletions include/ptcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct PtContext {
void validate() const
{
// TODO : handle this in a better way
// TODO : better error context
if (fieldmapConfig.numTilesInPrimary > fieldmapConfig.numTilesTotal) {
throw PtException{"fieldmap parameter `numTilesInPrimary' (" + std::to_string(fieldmapConfig.numTilesInPrimary) +
") exceeded `numTilesTotal' (" + std::to_string(fieldmapConfig.numTilesTotal) + ")"};
Expand Down
2 changes: 1 addition & 1 deletion src/cli_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void parseOptions(PtContext &ctx, int argc, char **argv)
parseCompile(ctx, argc, argv);
break;
default:
internalerror_custom("cli_parser::parseOptions unknown subcommand setting");
internalerror("cli_parser::parseOptions unknown subcommand setting");
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ buildColorIndexMaps(PtContext &ctx, const std::vector<IndexedNormTile> &normaliz
for (const auto &[color, index] : primaryIndexMap) {
auto [insertedValue, wasInserted] = colorIndexes.insert({color, index});
if (!wasInserted) {
internalerror_custom("compiler::buildColorIndexMaps colorIndexes.insert failed");
internalerror("compiler::buildColorIndexMaps colorIndexes.insert failed");
}
auto [_, wasInserted2] = indexesToColors.insert(std::pair{index, color});
if (!wasInserted2) {
internalerror_custom("compiler::buildColorIndexMaps indexesToColors.insert failed");
internalerror("compiler::buildColorIndexMaps indexesToColors.insert failed");
}
}
}
Expand Down Expand Up @@ -406,7 +406,7 @@ static GBATile makeTile(const NormalizedTile &normalizedTile, std::size_t frame,
for (int i = 1; i < normalizedTile.palette.size; i++) {
auto it = std::find(std::begin(palette.colors) + 1, std::end(palette.colors), normalizedTile.palette.colors[i]);
if (it == std::end(palette.colors)) {
internalerror_custom("compiler::makeTile it == std::end(palette.colors)");
internalerror("compiler::makeTile it == std::end(palette.colors)");
}
paletteIndexes.at(i) = it - std::begin(palette.colors);
}
Expand Down Expand Up @@ -450,7 +450,7 @@ static void assignTilesPrimary(PtContext &ctx, CompiledTileset &compiled,
return (colorSet & ~assignedPal).none();
});
if (it == std::end(assignedPalsSolution)) {
internalerror_custom("compiler::assignTilesPrimary it == std::end(assignedPalsSolution)");
internalerror("compiler::assignTilesPrimary it == std::end(assignedPalsSolution)");
}
std::size_t paletteIndex = it - std::begin(assignedPalsSolution);

Expand Down Expand Up @@ -520,7 +520,7 @@ static void assignTilesPrimary(PtContext &ctx, CompiledTileset &compiled,
return (colorSet & ~assignedPal).none();
});
if (it == std::end(assignedPalsSolution)) {
internalerror_custom("compiler::assignTilesPrimary it == std::end(assignedPalsSolution)");
internalerror("compiler::assignTilesPrimary it == std::end(assignedPalsSolution)");
}
std::size_t paletteIndex = it - std::begin(assignedPalsSolution);
GBATile gbaTile =
Expand Down Expand Up @@ -575,7 +575,7 @@ static void assignTilesSecondary(PtContext &ctx, CompiledTileset &compiled,
return (colorSet & ~assignedPal).none();
});
if (it == std::end(allColorSets)) {
internalerror_custom("compiler::assignTilesSecondary it == std::end(allColorSets)");
internalerror("compiler::assignTilesSecondary it == std::end(allColorSets)");
}
std::size_t paletteIndex = it - std::begin(allColorSets);

Expand Down Expand Up @@ -645,7 +645,7 @@ static void assignTilesSecondary(PtContext &ctx, CompiledTileset &compiled,
return (colorSet & ~assignedPal).none();
});
if (it == std::end(allColorSets)) {
internalerror_custom("compiler::assignTilesSecondary it == std::end(allColorSets)");
internalerror("compiler::assignTilesSecondary it == std::end(allColorSets)");
}
std::size_t paletteIndex = it - std::begin(allColorSets);
GBATile gbaTile = makeTile(normTile, NormalizedTile::representativeFrameIndex(), compiled.palettes[paletteIndex]);
Expand Down
73 changes: 50 additions & 23 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,45 +136,63 @@ static void driveCompileFreestanding(PtContext &ctx) {}
static void driveCompile(PtContext &ctx)
{
if (std::filesystem::exists(ctx.output.path) && !std::filesystem::is_directory(ctx.output.path)) {
throw PtException{ctx.output.path + ": exists but is not a directory"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: exists but is not a directory", ctx.output.path));
}
if (ctx.subcommand == Subcommand::COMPILE_SECONDARY) {
if (!std::filesystem::exists(ctx.inputPaths.bottomSecondaryTilesheetPath())) {
throw PtException{ctx.inputPaths.bottomSecondaryTilesheetPath().string() + ": file does not exist"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: file does not exist", ctx.inputPaths.bottomSecondaryTilesheetPath().string()));
}
if (!std::filesystem::is_regular_file(ctx.inputPaths.bottomSecondaryTilesheetPath())) {
throw PtException{ctx.inputPaths.bottomSecondaryTilesheetPath().string() + ": exists but was not a regular file"};
fatalerror(
ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: exists but was not a regular file", ctx.inputPaths.bottomSecondaryTilesheetPath().string()));
}
if (!std::filesystem::exists(ctx.inputPaths.middleSecondaryTilesheetPath())) {
throw PtException{ctx.inputPaths.middleSecondaryTilesheetPath().string() + ": file does not exist"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: file does not exist", ctx.inputPaths.middleSecondaryTilesheetPath().string()));
}
if (!std::filesystem::is_regular_file(ctx.inputPaths.middleSecondaryTilesheetPath())) {
throw PtException{ctx.inputPaths.middleSecondaryTilesheetPath().string() + ": exists but was not a regular file"};
fatalerror(
ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: exists but was not a regular file", ctx.inputPaths.middleSecondaryTilesheetPath().string()));
}
if (!std::filesystem::exists(ctx.inputPaths.topSecondaryTilesheetPath())) {
throw PtException{ctx.inputPaths.topSecondaryTilesheetPath().string() + ": file does not exist"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: file does not exist", ctx.inputPaths.topSecondaryTilesheetPath().string()));
}
if (!std::filesystem::is_regular_file(ctx.inputPaths.topSecondaryTilesheetPath())) {
throw PtException{ctx.inputPaths.topSecondaryTilesheetPath().string() + ": exists but was not a regular file"};
fatalerror(
ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: exists but was not a regular file", ctx.inputPaths.topSecondaryTilesheetPath().string()));
}
}
if (!std::filesystem::exists(ctx.inputPaths.bottomPrimaryTilesheetPath())) {
throw PtException{ctx.inputPaths.bottomPrimaryTilesheetPath().string() + ": file does not exist"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: file does not exist", ctx.inputPaths.bottomPrimaryTilesheetPath().string()));
}
if (!std::filesystem::is_regular_file(ctx.inputPaths.bottomPrimaryTilesheetPath())) {
throw PtException{ctx.inputPaths.bottomPrimaryTilesheetPath().string() + ": exists but was not a regular file"};
fatalerror(
ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: exists but was not a regular file", ctx.inputPaths.bottomPrimaryTilesheetPath().string()));
}
if (!std::filesystem::exists(ctx.inputPaths.middlePrimaryTilesheetPath())) {
throw PtException{ctx.inputPaths.middlePrimaryTilesheetPath().string() + ": file does not exist"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: file does not exist", ctx.inputPaths.middlePrimaryTilesheetPath().string()));
}
if (!std::filesystem::is_regular_file(ctx.inputPaths.middlePrimaryTilesheetPath())) {
throw PtException{ctx.inputPaths.middlePrimaryTilesheetPath().string() + ": exists but was not a regular file"};
fatalerror(
ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: exists but was not a regular file", ctx.inputPaths.middlePrimaryTilesheetPath().string()));
}
if (!std::filesystem::exists(ctx.inputPaths.topPrimaryTilesheetPath())) {
throw PtException{ctx.inputPaths.topPrimaryTilesheetPath().string() + ": file does not exist"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: file does not exist", ctx.inputPaths.topPrimaryTilesheetPath().string()));
}
if (!std::filesystem::is_regular_file(ctx.inputPaths.topPrimaryTilesheetPath())) {
throw PtException{ctx.inputPaths.topPrimaryTilesheetPath().string() + ": exists but was not a regular file"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{}: exists but was not a regular file", ctx.inputPaths.topPrimaryTilesheetPath().string()));
}

if (ctx.subcommand == Subcommand::COMPILE_SECONDARY) {
Expand All @@ -183,43 +201,49 @@ static void driveCompile(PtContext &ctx)
png::image<png::rgba_pixel> tilesheetPng{ctx.inputPaths.bottomSecondaryTilesheetPath()};
}
catch (const std::exception &exception) {
throw PtException{ctx.inputPaths.bottomSecondaryTilesheetPath().string() + " is not a valid PNG file"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{} is not a valid PNG file", ctx.inputPaths.bottomSecondaryTilesheetPath().string()));
}
try {
// We do this here so if the input is not a PNG, we can catch and give a better error
png::image<png::rgba_pixel> tilesheetPng{ctx.inputPaths.middleSecondaryTilesheetPath()};
}
catch (const std::exception &exception) {
throw PtException{ctx.inputPaths.middleSecondaryTilesheetPath().string() + " is not a valid PNG file"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{} is not a valid PNG file", ctx.inputPaths.middleSecondaryTilesheetPath().string()));
}
try {
// We do this here so if the input is not a PNG, we can catch and give a better error
png::image<png::rgba_pixel> tilesheetPng{ctx.inputPaths.topSecondaryTilesheetPath()};
}
catch (const std::exception &exception) {
throw PtException{ctx.inputPaths.topSecondaryTilesheetPath().string() + " is not a valid PNG file"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{} is not a valid PNG file", ctx.inputPaths.topSecondaryTilesheetPath().string()));
}
}
try {
// We do this here so if the input is not a PNG, we can catch and give a better error
png::image<png::rgba_pixel> tilesheetPng{ctx.inputPaths.bottomPrimaryTilesheetPath()};
}
catch (const std::exception &exception) {
throw PtException{ctx.inputPaths.bottomPrimaryTilesheetPath().string() + " is not a valid PNG file"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{} is not a valid PNG file", ctx.inputPaths.bottomPrimaryTilesheetPath().string()));
}
try {
// We do this here so if the input is not a PNG, we can catch and give a better error
png::image<png::rgba_pixel> tilesheetPng{ctx.inputPaths.middlePrimaryTilesheetPath()};
}
catch (const std::exception &exception) {
throw PtException{ctx.inputPaths.middlePrimaryTilesheetPath().string() + " is not a valid PNG file"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{} is not a valid PNG file", ctx.inputPaths.middlePrimaryTilesheetPath().string()));
}
try {
// We do this here so if the input is not a PNG, we can catch and give a better error
png::image<png::rgba_pixel> tilesheetPng{ctx.inputPaths.topPrimaryTilesheetPath()};
}
catch (const std::exception &exception) {
throw PtException{ctx.inputPaths.topPrimaryTilesheetPath().string() + " is not a valid PNG file"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("{} is not a valid PNG file", ctx.inputPaths.topPrimaryTilesheetPath().string()));
}

std::unique_ptr<CompiledTileset> compiledTiles;
Expand Down Expand Up @@ -266,13 +290,16 @@ static void driveCompile(PtContext &ctx)
std::filesystem::path animsPath = ctx.output.path / animsDir;

if (std::filesystem::exists(tilesetPath) && !std::filesystem::is_regular_file(tilesetPath)) {
throw PtException{"`" + tilesetPath.string() + "' exists in output directory but is not a file"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("'{}' exists in output directory but is not a file", tilesetPath.string()));
}
if (std::filesystem::exists(palettesPath) && !std::filesystem::is_directory(palettesPath)) {
throw PtException{"`" + palettesDir.string() + "' exists in output directory but is not a directory"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("'{}' exists in output directory but is not a directory", palettesDir.string()));
}
if (std::filesystem::exists(animsPath) && !std::filesystem::is_directory(animsPath)) {
throw PtException{"`" + animsDir.string() + "' exists in output directory but is not a directory"};
fatalerror(ctx.err, ctx.inputPaths, ctx.compilerConfig.mode,
fmt::format("'{}' exists in output directory but is not a directory", animsDir.string()));
}
std::filesystem::create_directories(palettesPath);
std::filesystem::create_directories(animsPath);
Expand Down Expand Up @@ -300,7 +327,7 @@ void drive(PtContext &ctx)
driveCompileFreestanding(ctx);
break;
default:
internalerror_custom("driver::drive unknown subcommand setting");
internalerror("driver::drive unknown subcommand setting");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void configurePngPalette(TilesPngPaletteMode paletteMode, png::image<png:
}
}
else {
internalerror_custom("emitter::configurePngPalette unknown TilesPngPaletteMode");
internalerror("emitter::configurePngPalette unknown TilesPngPaletteMode");
}
out.set_palette(pngPal);
}
Expand Down Expand Up @@ -101,7 +101,7 @@ void emitTilesPng(PtContext &ctx, png::image<png::index_pixel> &out, const Compi
out[pixelRow][pixelCol] = (paletteIndex << 4) | indexInPalette;
break;
default:
internalerror_custom("emitter::emitTilesPng unknown TilesPngPalMode");
internalerror("emitter::emitTilesPng unknown TilesPngPalMode");
}
}
else {
Expand Down Expand Up @@ -129,7 +129,7 @@ void emitAnim(PtContext &ctx, std::vector<png::image<png::index_pixel>> &outFram
const std::vector<GBAPalette> &palettes)
{
if (outFrames.size() != animation.frames.size()) {
internalerror_custom("emitter::emitAnim outFrames.size() != animation.frames.size()");
internalerror("emitter::emitAnim outFrames.size() != animation.frames.size()");
}
for (std::size_t frameIndex = 0; frameIndex < animation.frames.size(); frameIndex++) {
png::image<png::index_pixel> &out = outFrames.at(frameIndex);
Expand Down
16 changes: 12 additions & 4 deletions src/errors_warnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

namespace porytiles {

void internalerror_custom(std::string customMessage) { throw std::runtime_error(customMessage); }
void internalerror(std::string message) { throw std::runtime_error(message); }

void internalerror_numPalettesInPrimaryNeqPrimaryPalettesSize(std::string context, std::size_t configNumPalettesPrimary,
std::size_t primaryPalettesSize)
{
internalerror_custom("config.numPalettesInPrimary did not match primary palette set size (" +
std::to_string(configNumPalettesPrimary) + " != " + std::to_string(primaryPalettesSize) + ")");
internalerror("config.numPalettesInPrimary did not match primary palette set size (" +
std::to_string(configNumPalettesPrimary) + " != " + std::to_string(primaryPalettesSize) + ")");
}

void internalerror_unknownCompilerMode(std::string context) { internalerror_custom(context + " unknown CompilerMode"); }
void internalerror_unknownCompilerMode(std::string context) { internalerror(context + " unknown CompilerMode"); }

void error_freestandingDimensionNotDivisibleBy8(ErrorsAndWarnings &err, const InputPaths &inputs,
std::string dimensionName, png::uint_32 dimension)
Expand Down Expand Up @@ -102,6 +102,14 @@ void error_invalidAlphaValue(ErrorsAndWarnings &err, const RGBATile &tile, std::
}
}

void fatalerror(ErrorsAndWarnings &err, const InputPaths &inputs, CompilerMode mode, std::string message)
{
if (err.printErrors) {
pt_fatal_err("{}", message);
}
die_compilationTerminated(err, inputs.modeBasedInputPath(mode), message);
}

void fatalerror_missingRequiredAnimFrameFile(ErrorsAndWarnings &err, const InputPaths &inputs, CompilerMode mode,
const std::string &animation, std::size_t index)
{
Expand Down
2 changes: 1 addition & 1 deletion src/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void importAnimTiles(PtContext &ctx, const std::vector<std::vector<AnimationPng<

for (const auto &rawAnim : rawAnims) {
if (rawAnim.empty()) {
internalerror_custom("importer::importAnimTiles rawAnim was empty");
internalerror("importer::importAnimTiles rawAnim was empty");
}

std::set<png::uint_32> frameWidths{};
Expand Down
2 changes: 1 addition & 1 deletion src/tmpfiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::filesystem::path createTmpdir()
break;
}
if (i == maxTries) {
internalerror_custom("tmpfiles::createTmpdir getTmpdirPath took too many tries");
internalerror("tmpfiles::createTmpdir getTmpdirPath took too many tries");
}
i++;
}
Expand Down
6 changes: 3 additions & 3 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::string tileTypeString(TileType type)
case TileType::ANIM:
return "anim";
default:
internalerror_custom("types::tileTypeString unknown TileType");
internalerror("types::tileTypeString unknown TileType");
}
// unreachable, here to satisfy compiler
return "";
Expand All @@ -83,7 +83,7 @@ std::string layerString(TileLayer layer)
case TileLayer::TOP:
return "top";
default:
internalerror_custom("types::layerString unknown TileLayer");
internalerror("types::layerString unknown TileLayer");
}
// unreachable, here to satisfy compiler
return "";
Expand All @@ -101,7 +101,7 @@ std::string subtileString(Subtile subtile)
case Subtile::SOUTHEAST:
return "southeast";
default:
internalerror_custom("types::subtileString unknown Subtile");
internalerror("types::subtileString unknown Subtile");
}
// unreachable, here to satisfy compiler
return "";
Expand Down

0 comments on commit e6c052a

Please sign in to comment.