Skip to content

Commit

Permalink
Better errors for missing decompile behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Sep 22, 2023
1 parent 083fe66 commit 197b18d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
3 changes: 3 additions & 0 deletions include/errors_warnings.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ void error_invalidCsvRowFormat(ErrorsAndWarnings &err, std::string filePath, std
void error_unknownMetatileBehavior(ErrorsAndWarnings &err, std::string filePath, std::size_t line,
std::string behavior);

void error_unknownMetatileBehaviorValue(ErrorsAndWarnings &err, std::string filePath, std::size_t entry,
std::uint16_t behaviorValue);

void error_duplicateAttribute(ErrorsAndWarnings &err, std::string filePath, std::size_t line, std::size_t id,
std::size_t previousLine);

Expand Down
7 changes: 7 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,14 @@ struct DecompilerSourcePaths {
return path / "anim";
}

std::filesystem::path secondaryAttributesBin() const
{
std::filesystem::path path{secondarySourcePath};
return path / "metatile_attributes.bin";
}

std::filesystem::path modeBasedSrcPath(DecompilerMode mode) const;
std::filesystem::path modeBasedAttrPath(DecompilerMode mode) const;
};

struct Output {
Expand Down
1 change: 1 addition & 0 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ static void driveDecompile(PtContext &ctx)
png::image<png::rgba_pixel> bottomPrimaryPng{128, static_cast<png::uint_32>(imageHeight)};
png::image<png::rgba_pixel> middlePrimaryPng{128, static_cast<png::uint_32>(imageHeight)};
png::image<png::rgba_pixel> topPrimaryPng{128, static_cast<png::uint_32>(imageHeight)};
// FIXME : if any errors are thrown in this method, it will partially emit the attr file which is not intuitive
porytiles::emitDecompiled(ctx, bottomPrimaryPng, middlePrimaryPng, topPrimaryPng, outAttributes, *decompiled,
attributesMap, behaviorReverseMap);
outAttributes.close();
Expand Down
23 changes: 9 additions & 14 deletions src/emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,8 @@ void emitDecompiled(PtContext &ctx, png::image<png::rgba_pixel> &bottom, png::im
<< encounterTypeString(attributesMap.at(metatileIndex).encounterType) << std::endl;
}
else {
/*
* TODO : print warning that reverse map did not contain a mapping for this behavior, this case also occurs if
* the reverse map has 0 size, which may occur if user does not specify a behavior header (right now this is not
* allowed, but in the future we may make it allowed)
*/
outCsv << metatileIndex << "," << attributesMap.at(metatileIndex).metatileBehavior << ","
<< terrainTypeString(attributesMap.at(metatileIndex).terrainType) << ","
<< encounterTypeString(attributesMap.at(metatileIndex).encounterType) << std::endl;
error_unknownMetatileBehaviorValue(ctx.err, ctx.decompilerSrcPaths.modeBasedAttrPath(ctx.decompilerConfig.mode),
metatileIndex, attributesMap.at(metatileIndex).metatileBehavior);
}
}
else {
Expand All @@ -369,15 +363,16 @@ void emitDecompiled(PtContext &ctx, png::image<png::rgba_pixel> &bottom, png::im
<< std::endl;
}
else {
/*
* TODO : print warning that reverse map did not contain a mapping for this behavior, this case also occurs if
* the reverse map has 0 size, which may occur if user does not specify a behavior header (right now this is not
* allowed, but in the future we may make it allowed)
*/
outCsv << metatileIndex << "," << attributesMap.at(metatileIndex).metatileBehavior << std::endl;
error_unknownMetatileBehaviorValue(ctx.err, ctx.decompilerSrcPaths.modeBasedAttrPath(ctx.decompilerConfig.mode),
metatileIndex, attributesMap.at(metatileIndex).metatileBehavior);
}
}
}

if (ctx.err.errCount > 0) {
die_errorCount(ctx.err, ctx.decompilerSrcPaths.modeBasedSrcPath(ctx.decompilerConfig.mode),
"behavior value did not have reverse mapping");
}
}

} // namespace porytiles
Expand Down
11 changes: 11 additions & 0 deletions src/errors_warnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ void error_unknownMetatileBehavior(ErrorsAndWarnings &err, std::string filePath,
}
}

void error_unknownMetatileBehaviorValue(ErrorsAndWarnings &err, std::string filePath, std::size_t entry,
std::uint16_t behaviorValue)
{
err.errCount++;
if (err.printErrors) {
pt_err("{}: in metatile entry {}: unmapped metatile behavior value '{}'", filePath, entry,
fmt::styled(behaviorValue, fmt::emphasis::bold));
pt_println(stderr, "");
}
}

void error_duplicateAttribute(ErrorsAndWarnings &err, std::string filePath, std::size_t line, std::size_t id,
std::size_t previousLine)
{
Expand Down
17 changes: 16 additions & 1 deletion src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,22 @@ std::filesystem::path DecompilerSourcePaths::modeBasedSrcPath(DecompilerMode mod
internalerror_unknownDecompilerMode("types::InputPaths::modeBasedInputPath");
}
// unreachable, here for compiler
throw std::runtime_error("types::InputPaths::modeBasedInputPath (decompile) reached unreachable code path");
throw std::runtime_error(
"types::InputPaths::DecompilerSourcePaths::modeBasedInputPath reached unreachable code path");
}

std::filesystem::path DecompilerSourcePaths::modeBasedAttrPath(DecompilerMode mode) const
{
switch (mode) {
case DecompilerMode::PRIMARY:
return primaryAttributesBin();
case DecompilerMode::SECONDARY:
return secondaryAttributesBin();
default:
internalerror_unknownDecompilerMode("types::InputPaths::modeBasedAttrPath");
}
// unreachable, here for compiler
throw std::runtime_error("types::InputPaths::DecompilerSourcePaths::modeBasedAttrPath reached unreachable code path");
}

std::string compilerModeString(CompilerMode mode)
Expand Down

0 comments on commit 197b18d

Please sign in to comment.