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.
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.
14 changes: 8 additions & 6 deletions xml_converter/src/attribute/image.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "image.hpp"

#include <iosfwd>
#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 +20,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->xml_filedir, image.filename);
*value = image;
*is_set = true;
}

Expand All @@ -35,6 +36,7 @@ string image_to_xml_attribute(
const string& attribute_name,
XMLWriterState* state,
const Image* value) {
state->textures.push_back(value);
return " " + attribute_name + "=\"" + value->filename + "\"";
}

Expand All @@ -48,9 +50,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->proto_filedir, image.filename);
*value = image;
*is_set = true;
}
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 (string(state->xml_filedir) == "") {
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 = string(state->xml_filedir) + "/" + 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->xml_filedir, trail_file_name);

ofstream trail_data_file(trail_file_path, ios::binary);

Expand Down
63 changes: 42 additions & 21 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,11 +9,13 @@

#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"

using namespace std;
namespace fs = std::filesystem;
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// SERIALIZE ///////////////////////////////////
Expand All @@ -25,47 +28,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 proto_filedir, map<string, Category>* marker_categories, vector<Parseable*>* parsed_pois) {
fstream infile;
waypoint::Waypoint proto_message;
ProtoReaderState state;
state.proto_filedir = proto_filedir.c_str();

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 @@ -154,7 +161,16 @@ void proto_post_processing(ProtoWriterState* state, waypoint::Waypoint* proto) {

for (size_t i = 1; i < state->textures.size(); i++) {
waypoint::TextureData* texture_data = proto->add_textures();
texture_data->set_filepath(state->textures[i]->filename);
const Image* image = state->textures[i];
texture_data->set_filepath(image->filename);
if (fs::exists(fs::path(state->textures[i]->original_filepath))) {
fs::path output_path = fs::path(state->proto_filedir) / image->filename;
fs::create_directories(output_path.parent_path());
fs::copy_file(fs::path(image->original_filepath), output_path, fs::copy_options::update_existing);
}
else {
cout << "Warning: File path " << state->textures[i]->original_filepath << " not found." << endl;
}
}
}
}
Expand All @@ -163,12 +179,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 +200,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 proto_filedir,
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
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.proto_filedir = proto_filedir.c_str();

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

_write_protobuf_file(
join_file_paths(filepath, "markers.bin"),
join_file_paths(state.proto_filedir, "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 proto_filedir,
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.proto_filedir = proto_filedir.c_str();

for (size_t i = 0; i < parsed_pois->size(); i++) {
Parseable* parsed_poi = (*parsed_pois)[i];
Expand All @@ -251,12 +271,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.proto_filedir, 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 proto_filedir,
std::map<std::string, Category>* marker_categories,
std::vector<Parseable*>* parsed_pois);

void write_protobuf_file(
const std::string& proto_directory,
const std::string proto_filedir,
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
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 proto_filedir,
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
const StringHierarchy& category_filter,
const std::map<std::string, Category>* marker_categories,
const std::vector<Parseable*>* parsed_pois);
Loading