Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworking copying functions to only copy files for textures #264

Merged
merged 12 commits into from
Apr 20, 2024
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
</MarkerCategory>

<POIs>
<POI IconFile="my_texture.png" Type="mycategory" MapID="50" />
<POI IconFile="my_texture2.png" Type="mycategory" MapID="50" />
<POI IconFile="somedir/my_texture3.png" Type="mycategory" MapID="50" />
<Trail Texture="my_texture.png" Type="mycategory" MapID="50" />
<POI IconFile="texture_one.png" Type="mycategory" MapID="50" />
<POI IconFile="texture_two.png" Type="mycategory" MapID="50" />
<POI IconFile="somedir/texture_three.png" Type="mycategory" MapID="50" />
<Trail Texture="texture_one.png" Type="mycategory" MapID="50" />
</POIs>
</OverlayData>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full Diff
--- xml_converter/integration_tests/test_cases/texture/output_proto/markers.bin.textproto._old	2024-03-18 01:20:59.122077476 +0000
+++ xml_converter/integration_tests/test_cases/texture/output_proto/markers.bin.textproto._new	2024-03-18 01:20:59.130077442 +0000
@@ -21,11 +21,11 @@
 textures {
 }
 textures {
-  filepath: "my_texture.png"
+  filepath: "texture_one.png"
 }
 textures {
-  filepath: "my_texture2.png"
+  filepath: "texture_two.png"
 }
 textures {
-  filepath: "somedir/my_texture3.png"
+  filepath: "somedir/texture_three.png"
 }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full Diff
--- xml_converter/integration_tests/test_cases/texture/output_proto/markers.bin.textproto._old	2024-04-20 22:56:53.572116768 +0000
+++ xml_converter/integration_tests/test_cases/texture/output_proto/markers.bin.textproto._new	2024-04-20 22:56:53.580116633 +0000
@@ -21,11 +21,11 @@
 textures {
 }
 textures {
-  filepath: "my_texture.png"
+  filepath: "texture_one.png"
 }
 textures {
-  filepath: "my_texture2.png"
+  filepath: "texture_two.png"
 }
 textures {
-  filepath: "somedir/my_texture3.png"
+  filepath: "somedir/texture_three.png"
 }

Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
</MarkerCategory>

<POIs>
<POI Type="mycategory" IconFile="my_texture.png" MapID="50"/>
<POI Type="mycategory" IconFile="my_texture2.png" MapID="50"/>
<POI Type="mycategory" IconFile="somedir/my_texture3.png" MapID="50"/>
<Trail Type="mycategory" MapID="50" Texture="my_texture.png"/>
<POI Type="mycategory" IconFile="texture_one.png" MapID="50"/>
<POI Type="mycategory" IconFile="texture_two.png" MapID="50"/>
<POI Type="mycategory" IconFile="somedir/texture_three.png" MapID="50"/>
<Trail Type="mycategory" MapID="50" Texture="texture_one.png"/>
</POIs>
</OverlayData>
32 changes: 25 additions & 7 deletions xml_converter/src/attribute/image.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "image.hpp"

#include <iosfwd>
#include <filesystem>
#include <string>
#include <vector>

#include "../rapid_helpers.hpp"
#include "../rapidxml-1.13/rapidxml.hpp"
#include "waypoint.pb.h"
#include "../string_helper.hpp"

using namespace std;

Expand All @@ -21,8 +21,10 @@ void xml_attribute_to_image(
XMLReaderState* state,
Image* value,
bool* is_set) {
value->filename = get_attribute_value(input);
value->original_filepath = state->xml_filedir + "/" + value->filename;
Image image;
image.filename = get_attribute_value(input);
image.original_filepath = join_file_paths(state->marker_pack_root_directory, image.filename);
*value = image;
*is_set = true;
}

Expand All @@ -33,8 +35,16 @@ void xml_attribute_to_image(
////////////////////////////////////////////////////////////////////////////////
string image_to_xml_attribute(
const string& attribute_name,
XMLWriterState*,
XMLWriterState* state,
const Image* value) {
if (filesystem::exists(filesystem::path(value->original_filepath))) {
filesystem::path output_path = filesystem::path(state->marker_pack_root_directory) / value->filename;
filesystem::create_directories(output_path.parent_path());
filesystem::copy_file(filesystem::path(value->original_filepath), output_path, filesystem::copy_options::overwrite_existing);
}
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
else {
cout << "Warning: File path " << value->original_filepath << " not found." << endl;
}
return " " + attribute_name + "=\"" + value->filename + "\"";
}

Expand All @@ -48,9 +58,9 @@ void proto_to_image(
ProtoReaderState* state,
Image* value,
bool* is_set) {
// TODO: this is broken until we load the string index into the proto read state
Image image;
// image.path = input.path();
image.filename = state->textures[input].filepath();
image.original_filepath = join_file_paths(state->marker_pack_root_directory, image.filename);
*value = image;
*is_set = true;
}
Expand All @@ -75,6 +85,14 @@ void image_to_proto(
texture_index = state->textures.size();
state->texture_path_to_textures_index[value.original_filepath] = texture_index;
state->textures.push_back(&value);
if (filesystem::exists(filesystem::path(value.original_filepath))) {
filesystem::path output_path = filesystem::path(state->marker_pack_root_directory) / value.filename;
filesystem::create_directories(output_path.parent_path());
filesystem::copy_file(filesystem::path(value.original_filepath), output_path, filesystem::copy_options::overwrite_existing);
}
else {
cout << "Warning: File path " << value.original_filepath << " not found." << endl;
}
}

setter(texture_index);
Expand Down
6 changes: 3 additions & 3 deletions xml_converter/src/attribute/trail_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void xml_attribute_to_trail_data(
bool* is_map_id_set) {
TrailData trail_data;
string trail_data_relative_path = get_attribute_value(input);
if (state->xml_filedir == "") {
if (state->marker_pack_root_directory == "") {
throw "Error: Marker pack base directory is an empty string";
}
if (trail_data_relative_path == "") {
Expand All @@ -41,7 +41,7 @@ void xml_attribute_to_trail_data(
}

ifstream trail_data_file;
string trail_path = state->xml_filedir + "/" + trail_data_relative_path;
string trail_path = join_file_paths(state->marker_pack_root_directory, trail_data_relative_path);
trail_data_file.open(trail_path, ios::in | ios::binary);
if (!trail_data_file.good()) {
errors->push_back(new XMLAttributeValueError("No trail file found at " + trail_path, input));
Expand Down Expand Up @@ -132,7 +132,7 @@ string trail_data_to_xml_attribute(
}

string trail_file_name = long_to_hex_string(djb2_hash(byte_array, byte_array_size)) + ".trl";
string trail_file_path = join_file_paths(state->filedir, trail_file_name);
string trail_file_path = join_file_paths(state->marker_pack_root_directory, trail_file_name);

ofstream trail_data_file(trail_file_path, ios::binary);

Expand Down
51 changes: 31 additions & 20 deletions xml_converter/src/packaging_protobin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "packaging_protobin.hpp"

#include <filesystem>
#include <fstream>
#include <map>
#include <set>
Expand All @@ -8,6 +9,7 @@

#include "category_gen.hpp"
#include "parseable.hpp"
#include "state_structs/proto_writer_state.hpp"
#include "string_helper.hpp"
#include "string_hierarchy.hpp"
#include "waypoint.pb.h"
Expand All @@ -25,47 +27,51 @@ void parse_waypoint_categories(
string full_category_name,
::waypoint::Category proto_category,
map<string, Category>* marker_categories,
vector<Parseable*>* parsed_pois) {
vector<Parseable*>* parsed_pois,
ProtoReaderState* state) {
full_category_name += proto_category.name();
Category* this_category = &(*marker_categories)[full_category_name];

ProtoReaderState state;

this_category->parse_protobuf(proto_category, &state);
this_category->parse_protobuf(proto_category, state);

for (int i = 0; i < proto_category.icon_size(); i++) {
Icon* icon = new Icon();
icon->parse_protobuf(proto_category.icon(i), &state);
icon->parse_protobuf(proto_category.icon(i), state);
// TODO: The field category in Icon is being deprciated
// This overwrites any icon.category with its position in the heirarchy
icon->category.category = full_category_name;
parsed_pois->push_back(icon);
}
for (int i = 0; i < proto_category.trail_size(); i++) {
Trail* trail = new Trail();
trail->parse_protobuf(proto_category.trail(i), &state);
trail->parse_protobuf(proto_category.trail(i), state);
// TODO: The field category in Trail is being deprciated
// This overwrites any trail.category with its position in the heirarchy
trail->category.category = full_category_name;
parsed_pois->push_back(trail);
}

for (int i = 0; i < proto_category.children_size(); i++) {
parse_waypoint_categories(full_category_name + ".", proto_category.children(i), &(this_category->children), parsed_pois);
parse_waypoint_categories(full_category_name + ".", proto_category.children(i), &(this_category->children), parsed_pois, state);
}
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////
void read_protobuf_file(string proto_filepath, map<string, Category>* marker_categories, vector<Parseable*>* parsed_pois) {
void read_protobuf_file(string proto_filepath, const string marker_pack_root_directory, map<string, Category>* marker_categories, vector<Parseable*>* parsed_pois) {
fstream infile;
waypoint::Waypoint proto_message;
ProtoReaderState state;
state.marker_pack_root_directory = marker_pack_root_directory;

infile.open(proto_filepath, ios::in | ios::binary);
proto_message.ParseFromIstream(&infile);

state.textures = proto_message.textures();

for (int i = 0; i < proto_message.category_size(); i++) {
parse_waypoint_categories("", proto_message.category(i), marker_categories, parsed_pois);
parse_waypoint_categories("", proto_message.category(i), marker_categories, parsed_pois, &state);
}
}

Expand Down Expand Up @@ -163,12 +169,11 @@ void _write_protobuf_file(
const string& filepath,
const StringHierarchy& category_filter,
const map<string, Category>* marker_categories,
const std::map<string, std::vector<Parseable*>>& category_to_pois) {
const std::map<string, std::vector<Parseable*>>& category_to_pois,
ProtoWriterState* state) {
ofstream outfile;
outfile.open(filepath, ios::out | ios::binary);

ProtoWriterState state;

if (!outfile.is_open()) {
cout << "Unable to open " << filepath << endl;
}
Expand All @@ -185,25 +190,27 @@ void _write_protobuf_file(
category_filter,
category_to_pois,
&category_vector,
&state);
state);

if (maybe_category.is_category) {
output_message.add_category()->MergeFrom(maybe_category.category);
}
}

proto_post_processing(&state, &output_message);
proto_post_processing(state, &output_message);

output_message.SerializeToOstream(&outfile);
outfile.close();
}

void write_protobuf_file(
const string& filepath,
const string& marker_pack_root_directory,
const StringHierarchy& category_filter,
const map<string, Category>* marker_categories,
const vector<Parseable*>* parsed_pois) {
std::map<string, std::vector<Parseable*>> category_to_pois;
ProtoWriterState state;
state.marker_pack_root_directory = marker_pack_root_directory;

for (size_t i = 0; i < parsed_pois->size(); i++) {
Parseable* parsed_poi = (*parsed_pois)[i];
Expand All @@ -221,19 +228,22 @@ void write_protobuf_file(
}

_write_protobuf_file(
join_file_paths(filepath, "markers.bin"),
join_file_paths(state.marker_pack_root_directory, "markers.bin"),
category_filter,
marker_categories,
category_to_pois);
category_to_pois,
&state);
}

// Write protobuf per map id
void write_protobuf_file_per_map_id(
const string& proto_directory,
const string& marker_pack_root_directory,
const StringHierarchy& category_filter,
const map<string, Category>* marker_categories,
const vector<Parseable*>* parsed_pois) {
std::map<int, std::map<string, std::vector<Parseable*>>> mapid_to_category_to_pois;
ProtoWriterState state;
state.marker_pack_root_directory = marker_pack_root_directory;

for (size_t i = 0; i < parsed_pois->size(); i++) {
Parseable* parsed_poi = (*parsed_pois)[i];
Expand All @@ -251,12 +261,13 @@ void write_protobuf_file_per_map_id(
}

for (auto iterator = mapid_to_category_to_pois.begin(); iterator != mapid_to_category_to_pois.end(); iterator++) {
string output_filepath = join_file_paths(proto_directory, to_string(iterator->first) + ".data");
string output_filepath = join_file_paths(state.marker_pack_root_directory, to_string(iterator->first) + ".bin");

_write_protobuf_file(
output_filepath,
category_filter,
marker_categories,
iterator->second);
iterator->second,
&state);
}
}
5 changes: 3 additions & 2 deletions xml_converter/src/packaging_protobin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@

void read_protobuf_file(
std::string proto_filepath,
const std::string marker_pack_root_directory,
std::map<std::string, Category>* marker_categories,
std::vector<Parseable*>* parsed_pois);

void write_protobuf_file(
const std::string& proto_directory,
const std::string& marker_pack_root_directory,
const StringHierarchy& category_filter,
const std::map<std::string, Category>* marker_categories,
const std::vector<Parseable*>* parsed_pois);

void write_protobuf_file_per_map_id(
const std::string& proto_directory,
const std::string& marker_pack_root_directory,
const StringHierarchy& category_filter,
const std::map<std::string, Category>* marker_categories,
const std::vector<Parseable*>* parsed_pois);
Loading
Loading