Skip to content

Commit 641127f

Browse files
authored
Merge pull request #1667 from heinezen/fix/newline_split
Remove string splits with `\r` from renderer resource parsers
2 parents 99e355e + f4ff6cd commit 641127f

File tree

10 files changed

+99
-102
lines changed

10 files changed

+99
-102
lines changed

libopenage/renderer/resources/parser/common.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ TextureData parse_texture(const std::vector<std::string> &args) {
2020
texture.texture_id = std::stoul(args[1]);
2121

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

3125
return texture;
3226
}

libopenage/renderer/resources/parser/parse_blendmask.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2023-2024 the openage authors. See copying.md for legal info.
22

33
#include "parse_blendmask.h"
44

@@ -40,7 +40,7 @@ blending_mask parse_mask(const std::vector<std::string> &args) {
4040
if (args[0].starts_with("0b")) {
4141
// discard prefix because std::stoul doesn't understand binary prefixes
4242
std::string strip = args[0].substr(2, args[2].size() - 1);
43-
dir = std::stoul(args[0], nullptr, 2);
43+
dir = std::stoul(strip, nullptr, 2);
4444
}
4545
else {
4646
dir = std::stoul(args[0]);
@@ -58,16 +58,16 @@ blending_mask parse_mask(const std::vector<std::string> &args) {
5858
return mask;
5959
}
6060

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

69-
auto content = file.open();
70-
auto lines = content.get_lines();
69+
auto file = path.open();
70+
auto lines = file.get_lines();
7171

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

8080
if (version_no != 2) {
8181
throw Error(MSG(err) << "Reading .blmask file '"
82-
<< file.get_name()
82+
<< path.get_name()
8383
<< "' failed. Reason: Version "
8484
<< version_no << " not supported");
8585
}
@@ -104,7 +104,7 @@ BlendPatternInfo parse_blendmask_file(const util::Path &file,
104104
// TODO: Avoid double lookup with keywordfuncs.find(args[0])
105105
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
106106
throw Error(MSG(err) << "Reading .blmask file '"
107-
<< file.get_name()
107+
<< path.get_name()
108108
<< "' failed. Reason: Keyword "
109109
<< args[0] << " is not defined");
110110
}
@@ -128,7 +128,7 @@ BlendPatternInfo parse_blendmask_file(const util::Path &file,
128128
// Parse textures
129129
std::vector<std::shared_ptr<Texture2dInfo>> texture_infos;
130130
for (auto texture : textures) {
131-
util::Path texturepath = (file.get_parent() / texture.path);
131+
util::Path texturepath = (path.get_parent() / texture.path);
132132

133133
if (cache && cache->check_texture_cache(texturepath)) {
134134
// already loaded

libopenage/renderer/resources/parser/parse_blendtable.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2023-2024 the openage authors. See copying.md for legal info.
22

33
#include "parse_blendtable.h"
44

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

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

78-
auto content = file.open();
79-
auto lines = content.get_lines();
78+
auto file = path.open();
79+
auto lines = file.get_lines();
8080

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

8888
if (version_no != 1) {
8989
throw Error(MSG(err) << "Reading .bltable file '"
90-
<< file.get_name()
90+
<< path.get_name()
9191
<< "' failed. Reason: Version "
9292
<< version_no << " not supported");
9393
}
@@ -110,7 +110,7 @@ BlendTableInfo parse_blendtable_file(const util::Path &file,
110110
// TODO: Avoid double lookup with keywordfuncs.find(args[0])
111111
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
112112
throw Error(MSG(err) << "Reading .bltable file '"
113-
<< file.get_name()
113+
<< path.get_name()
114114
<< "' failed. Reason: Keyword "
115115
<< args[0] << " is not defined");
116116
}
@@ -125,7 +125,7 @@ BlendTableInfo parse_blendtable_file(const util::Path &file,
125125
i += 1;
126126
if (i >= lines.size()) {
127127
throw Error(MSG(err) << "Reading .bltable file '"
128-
<< file.get_name()
128+
<< path.get_name()
129129
<< "' failed. Reason: Matrix for keyword "
130130
<< args[0] << " is malformed");
131131
}
@@ -142,7 +142,7 @@ BlendTableInfo parse_blendtable_file(const util::Path &file,
142142

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

147147
if (cache && cache->check_blpattern_cache(maskpath)) {
148148
// already loaded

libopenage/renderer/resources/parser/parse_palette.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2023-2024 the openage authors. See copying.md for legal info.
22

33
#include "parse_palette.h"
44

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

66-
PaletteInfo parse_palette_file(const util::Path &file) {
67-
if (not file.is_file()) [[unlikely]] {
66+
PaletteInfo parse_palette_file(const util::Path &path) {
67+
if (not path.is_file()) [[unlikely]] {
6868
throw Error(MSG(err) << "Reading .opal file '"
69-
<< file.get_name()
69+
<< path.get_name()
7070
<< "' failed. Reason: File not found");
7171
}
7272

73-
auto content = file.open();
74-
auto lines = content.get_lines();
73+
auto file = path.open();
74+
auto lines = file.get_lines();
7575

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

8383
if (version_no != 1) {
8484
throw Error(MSG(err) << "Reading .opal file '"
85-
<< file.get_name()
85+
<< path.get_name()
8686
<< "' failed. Reason: Version "
8787
<< version_no << " not supported");
8888
}
@@ -105,7 +105,7 @@ PaletteInfo parse_palette_file(const util::Path &file) {
105105
// TODO: Avoid double lookup with keywordfuncs.find(args[0])
106106
if (not keywordfuncs.contains(args[0])) [[unlikely]] {
107107
throw Error(MSG(err) << "Reading .opal file '"
108-
<< file.get_name()
108+
<< path.get_name()
109109
<< "' failed. Reason: Keyword "
110110
<< args[0] << " is not defined");
111111
}
@@ -120,7 +120,7 @@ PaletteInfo parse_palette_file(const util::Path &file) {
120120
i += 1;
121121
if (i >= lines.size()) {
122122
throw Error(MSG(err) << "Reading .opal file '"
123-
<< file.get_name()
123+
<< path.get_name()
124124
<< "' failed. Reason: Matrix for keyword "
125125
<< args[0] << " is malformed");
126126
}

libopenage/renderer/resources/parser/parse_sprite.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,16 @@ FrameData parse_frame(const std::vector<std::string> &args) {
134134
return frame;
135135
}
136136

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

145-
auto content = file.open();
146-
auto lines = content.get_lines();
145+
auto file = path.open();
146+
auto lines = file.get_lines();
147147

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

163163
if (version_no != 2) {
164164
throw Error(MSG(err) << "Reading .sprite file '"
165-
<< file.get_name()
165+
<< path.get_name()
166166
<< "' failed. Reason: Version "
167167
<< version_no << " not supported");
168168
}
@@ -195,16 +195,16 @@ Animation2dInfo parse_sprite_file(const util::Path &file,
195195
})};
196196

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

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

322322
if (cache && cache->check_texture_cache(texturepath)) {
323323
// already loaded

libopenage/renderer/resources/parser/parse_terrain.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,16 @@ TerrainFrameData parse_terrain_frame(const std::vector<std::string> &args) {
139139
return frame;
140140
}
141141

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

150-
auto content = file.open();
151-
auto lines = content.get_lines();
150+
auto file = path.open();
151+
auto lines = file.get_lines();
152152

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

168168
if (version_no != 2) {
169169
throw Error(MSG(err) << "Reading .terrain file '"
170-
<< file.get_name()
170+
<< path.get_name()
171171
<< "' failed. Reason: Version "
172172
<< version_no << " not supported");
173173
}
@@ -200,16 +200,16 @@ TerrainInfo parse_terrain_file(const util::Path &file,
200200
})};
201201

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

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

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

288288
std::shared_ptr<BlendTableInfo> blendtable_info;
289289
if (blendtable) {
290-
util::Path tablepath = (file.get_parent() / blendtable.value().path);
290+
util::Path tablepath = (path.get_parent() / blendtable.value().path);
291291

292292
if (cache && cache->check_bltable_cache(tablepath)) {
293293
// already loaded

libopenage/renderer/resources/parser/parse_texture.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ std::string parse_imagefile(const std::vector<std::string> &args) {
5656
// it should result in an error if wrongly used here.
5757

5858
// Call substr() to get rid of the quotes
59-
// If the line ends in a carriage return, remove it as well
60-
if (args[1][args[1].size() - 1] == '\r') {
61-
return args[1].substr(1, args[1].size() - 3);
62-
}
63-
6459
return args[1].substr(1, args[1].size() - 2);
6560
}
6661

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

147-
Texture2dInfo parse_texture_file(const util::Path &file) {
148-
if (not file.is_file()) [[unlikely]] {
142+
Texture2dInfo parse_texture_file(const util::Path &path) {
143+
if (not path.is_file()) [[unlikely]] {
149144
throw Error(MSG(err) << "Reading .texture file '"
150-
<< file.get_name()
145+
<< path.get_name()
151146
<< "' failed. Reason: File not found");
152147
}
153148

154-
auto content = file.open();
155-
auto lines = content.get_lines();
149+
auto file = path.open();
150+
auto lines = file.get_lines();
156151

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

166161
if (version_no != 1) {
167162
throw Error(MSG(err) << "Reading .texture file '"
168-
<< file.get_name()
163+
<< path.get_name()
169164
<< "' failed. Reason: Version "
170165
<< version_no << " not supported");
171166
}
@@ -184,16 +179,16 @@ Texture2dInfo parse_texture_file(const util::Path &file) {
184179
})};
185180

186181
for (auto line : lines) {
187-
// Skip empty lines, lines with carriage returns, and comments
188-
if (line.empty() || line.substr(0, 1) == "#" || line[0] == '\r') {
182+
// Skip empty lines and comments
183+
if (line.empty() || line.substr(0, 1) == "#") {
189184
continue;
190185
}
191186
std::vector<std::string> args{util::split(line, ' ')};
192187

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

216-
auto imagepath = file.get_parent() / imagefile;
211+
auto imagepath = path.get_parent() / imagefile;
217212

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

0 commit comments

Comments
 (0)