Skip to content

Commit

Permalink
cli: Added support for #pragma example to format subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Feb 2, 2025
1 parent 6993825 commit e109bed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
38 changes: 33 additions & 5 deletions cli/source/helpers/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@

namespace pl::cli {

static std::vector<pl::u8> parseByteString(const std::string &string) {
auto byteString = std::string(string);
std::erase(byteString, ' ');

if ((byteString.length() % 2) != 0) return {};

std::vector<pl::u8> result;
for (pl::u32 i = 0; i < byteString.length(); i += 2) {
if (!std::isxdigit(byteString[i]) || !std::isxdigit(byteString[i + 1]))
return {};

result.push_back(std::strtoul(byteString.substr(i, 2).c_str(), nullptr, 16));
}

return result;
}

void executePattern(
PatternLanguage &runtime,
wolv::io::File &inputFile,
Expand All @@ -27,11 +44,22 @@ namespace pl::cli {
for (const auto &define : defines)
runtime.addDefine(define);

// Include baseAddress as a copy to prevent it from going out of scope in the lambda
runtime.setDataSource(baseAddress, inputFile.getSize(), [&inputFile, baseAddress](u64 address, void *buffer, size_t size) {
inputFile.seek(address - baseAddress);
inputFile.readBuffer(static_cast<u8*>(buffer), size);
});
if (inputFile.isValid()) {
// Include baseAddress as a copy to prevent it from going out of scope in the lambda
runtime.setDataSource(baseAddress, inputFile.getSize(), [&inputFile, baseAddress](u64 address, void *buffer, size_t size) {
inputFile.seek(address - baseAddress);
inputFile.readBuffer(static_cast<u8*>(buffer), size);
});
} else {
runtime.addPragma("example", [](pl::PatternLanguage &runtime, const std::string &value) {
auto data = parseByteString(value);
runtime.setDataSource(0, data.size(), [data](pl::u64 address, pl::u8 *buffer, pl::u64 size) {
std::memcpy(buffer, data.data() + address, size);
});

return true;
});
}

// Execute pattern file
if (!runtime.executeString(patternFile.readString(), wolv::util::toUTF8String(patternFile.getPath()))) {
Expand Down
10 changes: 7 additions & 3 deletions cli/source/subcommands/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace pl::cli::sub {
auto subcommand = app->add_subcommand("format", "Executes the given pattern on the given file, and output the pattern data in the wanted format");

// Add command line arguments
subcommand->add_option("-i,--input,INPUT_FILE", inputFilePath, "Input file to extract data from")->required()->check(CLI::ExistingFile);
subcommand->add_option("-i,--input,INPUT_FILE", inputFilePath, "Input file to extract data from")->check(CLI::ExistingFile);
subcommand->add_option("-p,--pattern,PATTERN_FILE", patternFilePath, "Pattern file")->required()->check(CLI::ExistingFile);
subcommand->add_option("-o,--output,OUTPUT_FILE", outputFilePath, "File to write the pattern data to")->check(CLI::NonexistentPath);
subcommand->add_option("-I,--includes", includePaths, "Include file paths")->take_all()->check(CLI::ExistingDirectory);
Expand Down Expand Up @@ -61,13 +61,18 @@ namespace pl::cli::sub {

// If no output path was given, use the input path with the formatter's file extension
if (outputFilePath.empty()) {
if (inputFilePath.empty()) {
::fmt::print("Input file path is required if no output file path is specified!\n");
std::exit(EXIT_FAILURE);
}

outputFilePath = inputFilePath;
outputFilePath.replace_extension("." + formatter->getFileExtension());
}

// Open input file
wolv::io::File inputFile(inputFilePath, wolv::io::File::Mode::Read);
if (!inputFile.isValid()) {
if (!inputFilePath.empty() && !inputFile.isValid()) {
::fmt::print("Failed to open file '{}'\n", inputFilePath.string());
std::exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -120,7 +125,6 @@ namespace pl::cli::sub {
}

outputFile.writeVector(result);
::fmt::print("Wrote pattern data to {}\n", outputFilePath.string());
});
}

Expand Down

0 comments on commit e109bed

Please sign in to comment.