Skip to content

Commit

Permalink
Switch save game to json
Browse files Browse the repository at this point in the history
  • Loading branch information
royfalk committed Nov 30, 2024
1 parent a86672e commit fe477bd
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 157 deletions.
23 changes: 0 additions & 23 deletions engine/src/cmd/csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,29 +198,6 @@ CSVTable::CSVTable(const string &data, const string &root) {
}

CSVTable::CSVTable(VSFileSystem::VSFile &f, const string &root) {
/*if (f.GetFilename() == "units_description.csv" ||
f.GetFilename() == "master_part_list.csv") {
} else if (f.GetFilename() == "units.csv") {
VSFileSystem::VSFile jsonFile;
VSFileSystem::VSError err = jsonFile.OpenReadOnly("units.json", VSFileSystem::UnitFile);
if (err <= VSFileSystem::Ok) {
UnitJSONFactory::ParseJSON(jsonFile);
}
jsonFile.Close();
}*/

if (f.GetFilename() == "units_description.csv" ||
f.GetFilename() == "master_part_list.csv") {
// Not the CSV file we expect. Will crash unit_csv_factory
} /*else if (f.GetFilename() != "units.csv") {
// Open a saved game.
std::cerr << "Parsing " << f.GetFilename() << std::endl;
//abort();
UnitCSVFactory factory;
factory.ParseCSV(f, true);
f.Begin();
}*/

std::string data = f.ReadFull();
this->rootdir = root;
Init(data);
Expand Down
31 changes: 29 additions & 2 deletions engine/src/cmd/unit_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,30 @@ extern void pushMesh(std::vector<Mesh *> &mesh,
void addShieldMesh(Unit::XML *xml, const char *filename, const float scale, int faction, class Flightgroup *fg);
void addRapidMesh(Unit::XML *xml, const char *filename, const float scale, int faction, class Flightgroup *fg);

// TODO: This is a terrible kludge. Replace with boost::json
std::string MapToJson(std::map<std::string, std::string> unit) {
std::string json_string = "[\n\t{\n";

int len = unit.size();
int i = 0;

for (auto const& pair : unit) {
std::cout << pair.first << " = " << pair.second << std::endl;
boost::format new_line;
if(i < len-1) {
new_line = boost::format("\t\t\"%1%\": \"%2%\",\n") % pair.first % pair.second;
} else {
new_line = boost::format("\t\t\"%1%\": \"%2%\"\n") % pair.first % pair.second;
}

i++;
json_string += new_line.str();
}

json_string += "\t}\n]\n";

return json_string;
}

void AddMeshes(std::vector<Mesh *> &xmeshes,
float &randomstartframe,
Expand Down Expand Up @@ -1088,12 +1111,14 @@ void Unit::WriteUnit(const char *modifications) {
std::string savedir = modifications;
VSFileSystem::CreateDirectoryHome(VSFileSystem::savedunitpath + "/" + savedir);
VSFileSystem::VSFile f;
VSFileSystem::VSError err = f.OpenCreateWrite(savedir + "/" + name + ".csv", VSFileSystem::UnitFile);
VSFileSystem::VSError err = f.OpenCreateWrite(savedir + "/" + name + ".json", VSFileSystem::UnitFile);
if (err > VSFileSystem::Ok) {
VS_LOG(error, (boost::format("!!! ERROR : Writing saved unit file : %1%") % f.GetFullPath().c_str()));
return;
}
std::string towrite = WriteUnitString();

std::map<std::string, std::string> map = UnitToMap();
std::string towrite = MapToJson(map);
f.Write(towrite.c_str(), towrite.length());
f.Close();
}
Expand Down Expand Up @@ -1356,6 +1381,8 @@ const std::map<std::string, std::string> Unit::UnitToMap() {

return unit;
}


string Unit::WriteUnitString() {
std::map<std::string, std::string> unit = UnitToMap();
return writeCSV(unit);
Expand Down
100 changes: 0 additions & 100 deletions engine/src/cmd/unit_csv_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,106 +35,6 @@ std::map<std::string, std::map<std::string, std::string>> UnitCSVFactory::units;
// This is probably unique enough to ensure no collision
std::string UnitCSVFactory::DEFAULT_ERROR_VALUE = "UnitCSVFactory::_GetVariable DEFAULT_ERROR_VALUE";

void ExtractColumns(std::string &line) {
std::string data(line);
std::string delimiter = ",";
size_t pos = 0;
std::string token;

while ((pos = data.find(delimiter)) != std::string::npos) {
token = data.substr(0, pos);
data.erase(0, pos + delimiter.length());
}
}

int line_num = 1;

/**
* @brief ProcessLine is a slightly complicated CSV parsing, as it
* needs to account for quotes and commas within these quotes.
* This code won't work if there are quotes within quotes.
* @param line - the input string
*/
std::vector<std::string> ProcessLine(std::string &line) {
std::string data(line);
std::vector<std::string> cells;

std::string token;

size_t comma_index = data.find(",");
size_t quote_index = data.find("\"");

while (comma_index != std::string::npos) {
// Start quote
if (quote_index < comma_index) {
// End quote
quote_index = data.find("\"", quote_index + 1);
// Comma after quote
comma_index = data.find(",", quote_index);
}

token = data.substr(0, comma_index);
data.erase(0, comma_index + 1);

// If token starts and ends with a quote, remove them
if (token[0] == '"' && token[token.size() - 1] == '"') {
token = token.substr(1, token.size() - 2);
}

cells.push_back(token);

comma_index = data.find(",");
quote_index = data.find("\"");
}

token = data.substr(0, comma_index);
cells.push_back(token);

return cells;
}

void UnitCSVFactory::ParseCSV(std::string data, std::string root, bool saved_game) {
std::vector<std::string> columns;
std::string delimiter = "\n";
size_t pos = 0;
std::string token;
bool first_line = true;

line_num = 1;

// Add newline to end of file, so last line will be processed.
if (!data.empty() && data.back() != '\n') {
data.append("\n");
}

while ((pos = data.find(delimiter)) != std::string::npos) {
token = data.substr(0, pos);
if (first_line) {
columns = ProcessLine(token);

first_line = false;
} else {

std::vector<std::string> line = ProcessLine(token);
std::map<std::string, std::string> unit_attributes;

for (unsigned int i = 1; i < columns.size(); i++) {
unit_attributes[columns[i]] = line[i];
}

// Add root
unit_attributes["root"] = root;

std::string key = (saved_game ? "player_ship" : line[0]);

if(!key.empty()) {
UnitCSVFactory::units[key] = unit_attributes;
}
}
data.erase(0, pos + delimiter.length());
}
}


// TODO: place this somewhere else with similar code from unit_csv
std::string GetUnitKeyFromNameAndFaction(const std::string unit_name, const std::string unit_faction) {
Expand Down
2 changes: 0 additions & 2 deletions engine/src/cmd/unit_csv_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ class UnitCSVFactory {
friend class UnitJSONFactory;
friend class UnitOptimizeFactory;
public:
static void ParseCSV(std::string data, std::string root, bool saved_game);

template<class T>
static inline T GetVariable(std::string unit_key, std::string const &attribute_key, T default_value) = delete;
static bool HasVariable(std::string unit_key, std::string const &attribute_key) {
Expand Down
6 changes: 3 additions & 3 deletions engine/src/cmd/unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include "resource/resource.h"
#include "base_util.h"
#include "unit_csv_factory.h"
#include "unit_json_factory.h"
#include "savegame.h"
#include "manifest.h"

Expand Down Expand Up @@ -448,10 +449,9 @@ void Unit::Init(const char *filename,
//Try to open save
if (filename[0]) {
VSFile unitTab;
VSError taberr = unitTab.OpenReadOnly(filepath + ".csv", UnitSaveFile);
VSError taberr = unitTab.OpenReadOnly(filepath + ".json", UnitSaveFile);
if (taberr <= Ok) {
std::string data = unitTab.ReadFull();
UnitCSVFactory::ParseCSV(data, unitTab.GetRoot(), true);
UnitJSONFactory::ParseJSON(unitTab, true);
unitTab.Close();
saved_game = true;
}
Expand Down
13 changes: 9 additions & 4 deletions engine/src/cmd/unit_json_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "json.h"


void UnitJSONFactory::ParseJSON(VSFileSystem::VSFile &file) {
void UnitJSONFactory::ParseJSON(VSFileSystem::VSFile &file, bool player_ship) {
const std::string json_text = file.ReadFull();

std::vector<std::string> units = json::parsing::parse_array(json_text.c_str());
Expand All @@ -58,9 +58,14 @@ void UnitJSONFactory::ParseJSON(VSFileSystem::VSFile &file) {
// Add root
unit_attributes["root"] = file.GetRoot();

std::string unit_key = unit.get("Key");
std::string stripped_unit_key = unit_key.substr(1, unit_key.size() - 2);


UnitCSVFactory::units[stripped_unit_key] = unit_attributes;
if(player_ship) {
UnitCSVFactory::units["player_ship"] = unit_attributes;
} else {
std::string unit_key = unit.get("Key");
std::string stripped_unit_key = unit_key.substr(1, unit_key.size() - 2);
UnitCSVFactory::units[stripped_unit_key] = unit_attributes;
}
}
}
2 changes: 1 addition & 1 deletion engine/src/cmd/unit_json_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class UnitJSONFactory {
static std::string DEFAULT_ERROR_VALUE;

public:
static void ParseJSON(VSFileSystem::VSFile &file);
static void ParseJSON(VSFileSystem::VSFile &file, bool player_ship = false);
};
#endif //VEGA_STRIKE_ENGINE_CMD_UNIT_JSON_FACTORY_H
1 change: 0 additions & 1 deletion engine/src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ void vs_options::init() {
universe_path = vs_config->getVariable("data", "universe_path", "universe");
sectors = vs_config->getVariable("data", "sectors", "sectors");
techniquesBasePath = vs_config->getVariable("data", "techniques", "techniques");
unitCSV = vs_config->getVariable("data", "UnitCSV", "units.csv");
modUnitCSV = vs_config->getVariable("data", "ModUnitCSV", "");
cockpits = vs_config->getVariable("data", "cockpits", "cockpits");
animations = vs_config->getVariable("data", "animations", "animations");
Expand Down
4 changes: 2 additions & 2 deletions engine/src/savegame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,10 @@ void CopySavedShips(std::string filename, int player_num, const std::vector<std:
srcnam = dstnam;
dstnam = tmp;
}
VSError e = src.OpenReadOnly(srcnam + "/" + starships[i] + ".csv", UnitSaveFile);
VSError e = src.OpenReadOnly(srcnam + "/" + starships[i] + ".json", UnitSaveFile);
if (e <= Ok) {
VSFileSystem::CreateDirectoryHome(VSFileSystem::savedunitpath + "/" + dstnam);
VSError f = dst.OpenCreateWrite(dstnam + "/" + starships[i] + ".csv", UnitFile);
VSError f = dst.OpenCreateWrite(dstnam + "/" + starships[i] + ".json", UnitFile);
if (f <= Ok) {
string srcdata = src.ReadFull();
dst.Write(srcdata);
Expand Down
19 changes: 0 additions & 19 deletions engine/src/universe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,29 +281,10 @@ void InitUnitTables() {
VSFileSystem::VSError err = jsonFile.OpenReadOnly("units.json", VSFileSystem::UnitFile);
if (err <= VSFileSystem::Ok) {
UnitJSONFactory::ParseJSON(jsonFile);

} else {
// Try units.csv
VSFileSystem::VSFile csvFile;
VSFileSystem::VSError err = csvFile.OpenReadOnly("units.csv", VSFileSystem::UnitFile);
if (err <= VSFileSystem::Ok) {
std::string data = csvFile.ReadFull();
UnitCSVFactory::ParseCSV(data, csvFile.GetRoot(), true);
} else {
std::cerr << "Unable to open units file. Aborting.\n";
abort();
}
csvFile.Close();
}

jsonFile.Close();

// TODO: where did this code snippet come from???
/*if (f.GetFilename() == "units_description.csv" ||
f.GetFilename() == "master_part_list.csv") {
// TODO: handle master_part_list
*/

// Really New Init
VSFileSystem::VSFile newJsonFile;
err = newJsonFile.OpenReadOnly("ships.json", VSFileSystem::UnitFile);
Expand Down

0 comments on commit fe477bd

Please sign in to comment.