Skip to content

Commit

Permalink
Merge pull request #1667 from heinezen/fix/newline_split
Browse files Browse the repository at this point in the history
Remove string splits with `\r` from renderer resource parsers
  • Loading branch information
TheJJ authored Aug 7, 2024
2 parents 99e355e + f4ff6cd commit 641127f
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 102 deletions.
8 changes: 1 addition & 7 deletions libopenage/renderer/resources/parser/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ TextureData parse_texture(const std::vector<std::string> &args) {
texture.texture_id = std::stoul(args[1]);

// Call substr() to get rid of the quotes
// If the line ends in a carriage return, remove it as well
if (args[2][args[2].size() - 1] == '\r') {
texture.path = args[2].substr(1, args[2].size() - 3);
}
else {
texture.path = args[2].substr(1, args[2].size() - 2);
}
texture.path = args[2].substr(1, args[2].size() - 2);

return texture;
}
Expand Down
20 changes: 10 additions & 10 deletions libopenage/renderer/resources/parser/parse_blendmask.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
// Copyright 2023-2024 the openage authors. See copying.md for legal info.

#include "parse_blendmask.h"

Expand Down Expand Up @@ -40,7 +40,7 @@ blending_mask parse_mask(const std::vector<std::string> &args) {
if (args[0].starts_with("0b")) {
// discard prefix because std::stoul doesn't understand binary prefixes
std::string strip = args[0].substr(2, args[2].size() - 1);
dir = std::stoul(args[0], nullptr, 2);
dir = std::stoul(strip, nullptr, 2);
}
else {
dir = std::stoul(args[0]);
Expand All @@ -58,16 +58,16 @@ blending_mask parse_mask(const std::vector<std::string> &args) {
return mask;
}

BlendPatternInfo parse_blendmask_file(const util::Path &file,
BlendPatternInfo parse_blendmask_file(const util::Path &path,
const std::shared_ptr<AssetCache> &cache) {
if (not file.is_file()) [[unlikely]] {
if (not path.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .blmask file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: File not found");
}

auto content = file.open();
auto lines = content.get_lines();
auto file = path.open();
auto lines = file.get_lines();

float scalefactor = 1.0;
std::vector<TextureData> textures;
Expand All @@ -79,7 +79,7 @@ BlendPatternInfo parse_blendmask_file(const util::Path &file,

if (version_no != 2) {
throw Error(MSG(err) << "Reading .blmask file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Version "
<< version_no << " not supported");
}
Expand All @@ -104,7 +104,7 @@ BlendPatternInfo parse_blendmask_file(const util::Path &file,
// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .blmask file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Keyword "
<< args[0] << " is not defined");
}
Expand All @@ -128,7 +128,7 @@ BlendPatternInfo parse_blendmask_file(const util::Path &file,
// Parse textures
std::vector<std::shared_ptr<Texture2dInfo>> texture_infos;
for (auto texture : textures) {
util::Path texturepath = (file.get_parent() / texture.path);
util::Path texturepath = (path.get_parent() / texture.path);

if (cache && cache->check_texture_cache(texturepath)) {
// already loaded
Expand Down
20 changes: 10 additions & 10 deletions libopenage/renderer/resources/parser/parse_blendtable.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
// Copyright 2023-2024 the openage authors. See copying.md for legal info.

#include "parse_blendtable.h"

Expand Down Expand Up @@ -67,16 +67,16 @@ PatternData parse_pattern(const std::vector<std::string> &args) {
return pattern;
}

BlendTableInfo parse_blendtable_file(const util::Path &file,
BlendTableInfo parse_blendtable_file(const util::Path &path,
const std::shared_ptr<AssetCache> &cache) {
if (not file.is_file()) [[unlikely]] {
if (not path.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .bltable file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: File not found");
}

auto content = file.open();
auto lines = content.get_lines();
auto file = path.open();
auto lines = file.get_lines();

std::vector<size_t> blendtable;
std::vector<PatternData> patterns;
Expand All @@ -87,7 +87,7 @@ BlendTableInfo parse_blendtable_file(const util::Path &file,

if (version_no != 1) {
throw Error(MSG(err) << "Reading .bltable file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Version "
<< version_no << " not supported");
}
Expand All @@ -110,7 +110,7 @@ BlendTableInfo parse_blendtable_file(const util::Path &file,
// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .bltable file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Keyword "
<< args[0] << " is not defined");
}
Expand All @@ -125,7 +125,7 @@ BlendTableInfo parse_blendtable_file(const util::Path &file,
i += 1;
if (i >= lines.size()) {
throw Error(MSG(err) << "Reading .bltable file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Matrix for keyword "
<< args[0] << " is malformed");
}
Expand All @@ -142,7 +142,7 @@ BlendTableInfo parse_blendtable_file(const util::Path &file,

std::vector<std::shared_ptr<BlendPatternInfo>> pattern_infos;
for (auto pattern : patterns) {
util::Path maskpath = (file.get_parent() / pattern.path);
util::Path maskpath = (path.get_parent() / pattern.path);

if (cache && cache->check_blpattern_cache(maskpath)) {
// already loaded
Expand Down
18 changes: 9 additions & 9 deletions libopenage/renderer/resources/parser/parse_palette.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
// Copyright 2023-2024 the openage authors. See copying.md for legal info.

#include "parse_palette.h"

Expand Down Expand Up @@ -63,15 +63,15 @@ std::vector<uint8_t> parse_colours(const std::vector<std::string> &lines) {
return entries;
}

PaletteInfo parse_palette_file(const util::Path &file) {
if (not file.is_file()) [[unlikely]] {
PaletteInfo parse_palette_file(const util::Path &path) {
if (not path.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .opal file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: File not found");
}

auto content = file.open();
auto lines = content.get_lines();
auto file = path.open();
auto lines = file.get_lines();

size_t entries = 0;
std::vector<uint8_t> colours;
Expand All @@ -82,7 +82,7 @@ PaletteInfo parse_palette_file(const util::Path &file) {

if (version_no != 1) {
throw Error(MSG(err) << "Reading .opal file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Version "
<< version_no << " not supported");
}
Expand All @@ -105,7 +105,7 @@ PaletteInfo parse_palette_file(const util::Path &file) {
// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .opal file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Keyword "
<< args[0] << " is not defined");
}
Expand All @@ -120,7 +120,7 @@ PaletteInfo parse_palette_file(const util::Path &file) {
i += 1;
if (i >= lines.size()) {
throw Error(MSG(err) << "Reading .opal file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Matrix for keyword "
<< args[0] << " is malformed");
}
Expand Down
20 changes: 10 additions & 10 deletions libopenage/renderer/resources/parser/parse_sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ FrameData parse_frame(const std::vector<std::string> &args) {
return frame;
}

Animation2dInfo parse_sprite_file(const util::Path &file,
Animation2dInfo parse_sprite_file(const util::Path &path,
const std::shared_ptr<AssetCache> &cache) {
if (not file.is_file()) [[unlikely]] {
if (not path.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .sprite file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: File not found");
}

auto content = file.open();
auto lines = content.get_lines();
auto file = path.open();
auto lines = file.get_lines();

float scalefactor = 1.0;
std::vector<TextureData> textures;
Expand All @@ -162,7 +162,7 @@ Animation2dInfo parse_sprite_file(const util::Path &file,

if (version_no != 2) {
throw Error(MSG(err) << "Reading .sprite file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Version "
<< version_no << " not supported");
}
Expand Down Expand Up @@ -195,16 +195,16 @@ Animation2dInfo parse_sprite_file(const util::Path &file,
})};

for (auto line : lines) {
// Skip empty lines, lines with carriage returns, and comments
if (line.empty() || line.substr(0, 1) == "#" || line[0] == '\r') {
// Skip empty lines and comments
if (line.empty() || line.substr(0, 1) == "#") {
continue;
}
std::vector<std::string> args{util::split(line, ' ')};

// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .sprite file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Keyword "
<< args[0] << " is not defined");
}
Expand Down Expand Up @@ -317,7 +317,7 @@ Animation2dInfo parse_sprite_file(const util::Path &file,
// Parse textures
std::vector<std::shared_ptr<Texture2dInfo>> texture_infos;
for (auto texture : textures) {
util::Path texturepath = (file.get_parent() / texture.path);
util::Path texturepath = (path.get_parent() / texture.path);

if (cache && cache->check_texture_cache(texturepath)) {
// already loaded
Expand Down
22 changes: 11 additions & 11 deletions libopenage/renderer/resources/parser/parse_terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ TerrainFrameData parse_terrain_frame(const std::vector<std::string> &args) {
return frame;
}

TerrainInfo parse_terrain_file(const util::Path &file,
TerrainInfo parse_terrain_file(const util::Path &path,
const std::shared_ptr<AssetCache> &cache) {
if (not file.is_file()) [[unlikely]] {
if (not path.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .terrain file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: File not found");
}

auto content = file.open();
auto lines = content.get_lines();
auto file = path.open();
auto lines = file.get_lines();

float scalefactor = 1.0;
std::vector<TextureData> textures;
Expand All @@ -167,7 +167,7 @@ TerrainInfo parse_terrain_file(const util::Path &file,

if (version_no != 2) {
throw Error(MSG(err) << "Reading .terrain file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Version "
<< version_no << " not supported");
}
Expand Down Expand Up @@ -200,16 +200,16 @@ TerrainInfo parse_terrain_file(const util::Path &file,
})};

for (auto line : lines) {
// Skip empty lines, lines with carriage returns, and comments
if (line.empty() || line.substr(0, 1) == "#" || line[0] == '\r') {
// Skip empty lines and comments
if (line.empty() || line.substr(0, 1) == "#") {
continue;
}
std::vector<std::string> args{util::split(line, ' ')};

// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .terrain file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Keyword "
<< args[0] << " is not defined");
}
Expand Down Expand Up @@ -269,7 +269,7 @@ TerrainInfo parse_terrain_file(const util::Path &file,
// Parse textures
std::vector<std::shared_ptr<Texture2dInfo>> texture_infos;
for (auto texture : textures) {
util::Path texturepath = (file.get_parent() / texture.path);
util::Path texturepath = (path.get_parent() / texture.path);

if (cache && cache->check_texture_cache(texturepath)) {
// already loaded
Expand All @@ -287,7 +287,7 @@ TerrainInfo parse_terrain_file(const util::Path &file,

std::shared_ptr<BlendTableInfo> blendtable_info;
if (blendtable) {
util::Path tablepath = (file.get_parent() / blendtable.value().path);
util::Path tablepath = (path.get_parent() / blendtable.value().path);

if (cache && cache->check_bltable_cache(tablepath)) {
// already loaded
Expand Down
25 changes: 10 additions & 15 deletions libopenage/renderer/resources/parser/parse_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ std::string parse_imagefile(const std::vector<std::string> &args) {
// it should result in an error if wrongly used here.

// Call substr() to get rid of the quotes
// If the line ends in a carriage return, remove it as well
if (args[1][args[1].size() - 1] == '\r') {
return args[1].substr(1, args[1].size() - 3);
}

return args[1].substr(1, args[1].size() - 2);
}

Expand Down Expand Up @@ -144,15 +139,15 @@ SubtextureData parse_subtex(const std::vector<std::string> &args) {
return subtex;
}

Texture2dInfo parse_texture_file(const util::Path &file) {
if (not file.is_file()) [[unlikely]] {
Texture2dInfo parse_texture_file(const util::Path &path) {
if (not path.is_file()) [[unlikely]] {
throw Error(MSG(err) << "Reading .texture file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: File not found");
}

auto content = file.open();
auto lines = content.get_lines();
auto file = path.open();
auto lines = file.get_lines();

std::string imagefile;
SizeData size;
Expand All @@ -165,7 +160,7 @@ Texture2dInfo parse_texture_file(const util::Path &file) {

if (version_no != 1) {
throw Error(MSG(err) << "Reading .texture file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Version "
<< version_no << " not supported");
}
Expand All @@ -184,16 +179,16 @@ Texture2dInfo parse_texture_file(const util::Path &file) {
})};

for (auto line : lines) {
// Skip empty lines, lines with carriage returns, and comments
if (line.empty() || line.substr(0, 1) == "#" || line[0] == '\r') {
// Skip empty lines and comments
if (line.empty() || line.substr(0, 1) == "#") {
continue;
}
std::vector<std::string> args{util::split(line, ' ')};

// TODO: Avoid double lookup with keywordfuncs.find(args[0])
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
throw Error(MSG(err) << "Reading .texture file '"
<< file.get_name()
<< path.get_name()
<< "' failed. Reason: Keyword "
<< args[0] << " is not defined");
}
Expand All @@ -213,7 +208,7 @@ Texture2dInfo parse_texture_file(const util::Path &file) {
size.height);
}

auto imagepath = file.get_parent() / imagefile;
auto imagepath = path.get_parent() / imagefile;

auto align = guess_row_alignment(size.width);
return Texture2dInfo(size.width, size.height, pxformat.format, imagepath, align, std::move(subinfos));
Expand Down
Loading

0 comments on commit 641127f

Please sign in to comment.