Skip to content

Commit

Permalink
Working more on compiled palette import
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Sep 4, 2023
1 parent 09c1406 commit ca0c17c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 15 deletions.
21 changes: 21 additions & 0 deletions include/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@

namespace porytiles {

template <typename T> static T parseInteger(const char *integerString)
{
// TODO : rewrite cli_parser::parseIntegralOption to use this function
try {
size_t pos;
T arg = std::stoi(integerString, &pos, 0);
if (std::string{integerString}.size() != pos) {
// throw here so it catches below and prints an error message
throw std::runtime_error{"invalid integral string: " + std::string{integerString}};
}
return arg;
}
catch (const std::exception &e) {
throw std::runtime_error{e.what()};
}
// unreachable, here for compiler
throw std::runtime_error("cli_parser::parseIntegralOption reached unreachable code path");
}

std::vector<std::string> split(std::string input, const std::string &delimiter);

std::filesystem::path getTmpfilePath(const std::filesystem::path &parentDir, const std::string &fileName);

std::filesystem::path createTmpdir();
Expand Down
15 changes: 1 addition & 14 deletions src/cli_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "logger.h"
#include "program_name.h"
#include "ptexception.h"
#include "utilities.h"

namespace porytiles {

Expand Down Expand Up @@ -65,20 +66,6 @@ static T parseIntegralOption(const ErrorsAndWarnings &err, const std::string &op
throw std::runtime_error("cli_parser::parseIntegralOption reached unreachable code path");
}

static std::vector<std::string> split(std::string input, const std::string &delimiter)
{
std::vector<std::string> result;
size_t pos;
std::string token;
while ((pos = input.find(delimiter)) != std::string::npos) {
token = input.substr(0, pos);
result.push_back(token);
input.erase(0, pos + delimiter.length());
}
result.push_back(input);
return result;
}

static RGBA32 parseRgbColor(const ErrorsAndWarnings &err, std::string optionName, const std::string &colorString)
{
std::vector<std::string> colorComponents = split(colorString, ",");
Expand Down
77 changes: 76 additions & 1 deletion src/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,16 +572,91 @@ importAttributesFromCsv(PtContext &ctx, const std::unordered_map<std::string, st
return attributeMap;
}

static RGBA32 parseJascLine(const ErrorsAndWarnings &err, const std::string &jascLine)
{
// TODO : this logic duplicates cli_parser::parseRgbColor
std::vector<std::string> colorComponents = split(jascLine, " ");
if (colorComponents.size() != 3) {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"expected valid JASC line in pal file, saw " + jascLine};
}

if (colorComponents[0].at(colorComponents[0].size() - 1) == '\r') {
colorComponents[0].pop_back();
}
if (colorComponents[1].at(colorComponents[1].size() - 1) == '\r') {
colorComponents[1].pop_back();
}
if (colorComponents[2].at(colorComponents[2].size() - 1) == '\r') {
colorComponents[2].pop_back();
}

int red = parseInteger<int>(colorComponents[0].c_str());
int green = parseInteger<int>(colorComponents[1].c_str());
int blue = parseInteger<int>(colorComponents[2].c_str());

if (red < 0 || red > 255) {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"invalid red component: range must be 0 <= red <= 255"};
}
if (green < 0 || green > 255) {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"invalid green component: range must be 0 <= green <= 255"};
}
if (blue < 0 || blue > 255) {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"invalid blue component: range must be 0 <= blue <= 255"};
}

return RGBA32{static_cast<std::uint8_t>(red), static_cast<std::uint8_t>(green), static_cast<std::uint8_t>(blue),
ALPHA_OPAQUE};
}

static std::vector<GBAPalette> importCompiledPalettes(PtContext &ctx,
std::vector<std::shared_ptr<std::ifstream>> &paletteFiles)
{
std::vector<GBAPalette> palettes{};

for (std::shared_ptr<std::ifstream> stream : paletteFiles) {
std::string line;

std::getline(*stream, line);
if (line.size() == 0) {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"invalid blank line in pal file"};
}
line.pop_back();
if (line != "JASC-PAL") {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"expected 'JASC-PAL' in pal file, saw " + line};
}

std::getline(*stream, line);
if (line.size() == 0) {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"invalid blank line in pal file"};
}
line.pop_back();
if (line != "0100") {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"expected '0100' in pal file, saw " + line};
}

std::getline(*stream, line);
if (line.size() == 0) {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"invalid blank line in pal file"};
}
line.pop_back();
if (line != "16") {
// TODO : need fatalerror to also work for decompile mode
throw std::runtime_error{"expected '16' in pal file, saw " + line};
}

while (std::getline(*stream, line)) {
// TODO : read lines instead of printing
std::cout << line << std::endl;
BGR15 bgr = rgbaToBgr(parseJascLine(ctx.err, line));
std::cout << bgr << std::endl;
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@

namespace porytiles {

std::vector<std::string> split(std::string input, const std::string &delimiter)
{
std::vector<std::string> result;
size_t pos;
std::string token;
while ((pos = input.find(delimiter)) != std::string::npos) {
token = input.substr(0, pos);
result.push_back(token);
input.erase(0, pos + delimiter.length());
}
result.push_back(input);
return result;
}

std::filesystem::path getTmpfilePath(const std::filesystem::path &parentDir, const std::string &fileName)
{
return std::filesystem::temp_directory_path() / parentDir / fileName;
Expand Down

0 comments on commit ca0c17c

Please sign in to comment.