Skip to content

Commit

Permalink
Fix library implementation + add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rockymoto517 committed Aug 5, 2022
1 parent 9cebc1c commit 517541d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 53 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Converter.exe
folder/*
Ino_Glitch/*
template/*
alt_templates/*
recursive_tests/*
recursive_tests/*
Include/*
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.5)

project(BeardlibConverter VERSION 1.1 LANGUAGES CXX)
project(BeardlibConverter VERSION 1.3 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ Make sure that the output folder hasn't been created, the tool will create the f
## CMake

1. Create a build directory
2. cmake ../ and build
3. Move the executable to the parent folder
2. cd into it
3. cmake ../ && make (if you're using mingw, use cmake ../ -G "MinGW Makefiles" && make)
4. Move the executable to the parent folder

### To convert an entire folder

1. Create a new folder and drop the songs you want to convert inside there
2. Write: Converter -r folder_with_multiple_folders output_folder_name
2. I've included a recursive_tests folder to use.
3. Write: Converter -r recursive_tests output_folder

#### Troubleshooting
You'll get assertion errors if the track.txt is written in improper json, so if you get an error, try validating the json
You'll get errors if the track.txt is written in improper json.
The program may not be able to find all folders when doing a recursive conversion if the folder names have spaces or special characters.
77 changes: 37 additions & 40 deletions src/Converter.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
#include <filesystem>
#include <sstream>
#include <regex>
#include <cstdio>
#include <algorithm>
#include <rapidjson/filereadstream.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include "Converter.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/writer.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"

namespace fs = std::filesystem;
namespace rj = rapidjson;

Converter::Converter(const std::string ost, const std::string dst, const char* _SEPARATOR):
original(ost), destination(dst), SEPARATOR(_SEPARATOR) {
bool success = readJson(original);
if (trackExists()) {
if (trackExists() && success) { //Make sure the track.txt is actually read
name = track["name"].GetString();
id = track["id"].GetString();
}
}

void Converter::copyDir(const bool is_recursive) {
try {
if (is_recursive) {
if (is_recursive) { //Create the output folder for the song folders
std::string pre_destination = destination.substr(0, destination.find(SEPARATOR));
fs::create_directory(pre_destination);
}
Expand All @@ -37,18 +37,18 @@ void Converter::copyDir(const bool is_recursive) {
}

bool Converter::readJson(const std::string folder) {
try {
try { //Open the track.txt and parse it as a file stream
FILE* in = fopen(folder.c_str(), "rb");
if (in == 0) {
std::cout << "File could not be read.\n";
return false;
}
else {
char buffer[65536];
rj::FileReadStream stream(in, buffer, sizeof(buffer));
char buffer[65536];
rj::FileReadStream stream(in, buffer, sizeof(buffer));

track.ParseStream(stream);
}
track.ParseStream(stream);
} //Close the C style file
fclose(in);
return true;
}
Expand All @@ -61,18 +61,18 @@ bool Converter::readJson(const std::string folder) {

void Converter::locRewrite(const std::string file) {
try{
std::ifstream readfile(file);
std::ifstream readfile(file); //Read the en.txt with an input stream
std::string fstring;
std::ostringstream sstr;

sstr << readfile.rdbuf();
sstr << readfile.rdbuf(); //Parse the file into a string stream
fstring = sstr.str();
readfile.close();

fstring = std::regex_replace(fstring, std::regex("sample_id"), id);
fstring = std::regex_replace(fstring, std::regex("sample_name"), name);

std::ofstream outfile(file);
std::ofstream outfile(file); //Output the new en.txt file with an output stream
outfile << fstring;
outfile.close();
}
Expand All @@ -84,18 +84,18 @@ void Converter::locRewrite(const std::string file) {

void Converter::altSoundsRewrite(const std::string file, const std::vector<bool> alts) {
try {
std::ifstream readfile(file);
std::ifstream readfile(file); //Read the main.xml with an input stream
std::string fstring;
std::ostringstream sstr;

sstr << readfile.rdbuf();
sstr << readfile.rdbuf(); //Parse xml into string stream then to string
readfile.close();
fstring = sstr.str();

//Adding the alt parts before editing
std::size_t index = 0;
for (size_t i = 0; i < alts.size(); i++) {
if (alts[i]) {
if (alts[i]) { //Replace event tags with nested track tags for alt tracks
if (i == 0) {
std::ifstream replacementFile("alt_templates/alt_setup.xml");
std::ostringstream rstream;
Expand Down Expand Up @@ -139,8 +139,7 @@ void Converter::altSoundsRewrite(const std::string file, const std::vector<bool>
std::string xmltag = m.value["file"].GetString();
std::string part = m.name.GetString();


if (m.value.HasMember("start_file")) {
if (m.value.HasMember("start_file")) { //Add startfiles
assert(m.value.IsObject());
xmltag += "\" start_source=\"";
xmltag += m.value["start_file"].GetString();
Expand All @@ -153,7 +152,7 @@ void Converter::altSoundsRewrite(const std::string file, const std::vector<bool>
else
fstring = std::regex_replace(fstring, std::regex("assaultPart"), xmltag);
}
else {
else { //Add tracks
if (part == "setup")
fstring = std::regex_replace(fstring, std::regex("stealthPart"), xmltag);
else if (part == "control")
Expand All @@ -163,7 +162,7 @@ void Converter::altSoundsRewrite(const std::string file, const std::vector<bool>
else
fstring = std::regex_replace(fstring, std::regex("assaultPart"), xmltag);
}
if (m.value.HasMember("alt")) {
if (m.value.HasMember("alt")) { //Add alt tracks
std::string alttag = m.value["alt"].GetString();
if (part == "setup" && alts[0]) {
fstring = std::regex_replace(fstring, std::regex("altStealthPart"), alttag);
Expand Down Expand Up @@ -193,7 +192,7 @@ void Converter::altSoundsRewrite(const std::string file, const std::vector<bool>

std::vector<bool> Converter::checkAlts() {
std::vector<bool> alts;
for (auto& itr : track["events"].GetObject()) {
for (auto& itr : track["events"].GetObject()) { //Finding alts in track.txt
if (itr.value.HasMember("alt"))
alts.push_back(true);
else
Expand All @@ -203,24 +202,22 @@ std::vector<bool> Converter::checkAlts() {
}

void Converter::copySongs(const std::string folder, const std::string dst) {
std::vector<std::string> names;
std::string current_title;
std::vector<std::string> names; //Cache tracks

for (auto& m : track["events"].GetObject()) {
assert(m.value.IsObject());
if (m.value.HasMember("alt"))
if (!std::count(names.begin(), names.end(), m.value["alt"].GetString()))
if (m.value.HasMember("alt")) //Add alt tracks
if (!std::count(names.begin(), names.end(), m.value["alt"].GetString())) //Make sure it doesn't get added twice
names.push_back(m.value["alt"].GetString());

if (m.value.HasMember("start_file"))
if (m.value.HasMember("start_file")) //Add start tracks
if (!std::count(names.begin(), names.end(), m.value["start_file"].GetString()))
names.push_back(m.value["start_file"].GetString());

if (!std::count(names.begin(), names.end(), m.value["file"].GetString()))
if (!std::count(names.begin(), names.end(), m.value["file"].GetString())) //Add normal tracks
names.push_back(m.value["file"].GetString());
}

for(size_t i = 0; i < names.size(); i++) {
for(size_t i = 0; i < names.size(); i++) { //Build paths to tracks
std::string srcs(SOURCE_DIR);
srcs += folder;
srcs += SEPARATOR;
Expand All @@ -231,26 +228,26 @@ void Converter::copySongs(const std::string folder, const std::string dst) {
dsts += "sounds";
dsts += SEPARATOR;
dsts += names[i];
fs::copy(srcs, dsts);
fs::copy(srcs, dsts); //Copy tracks
}

std::string _remove_file = dst;
_remove_file += SEPARATOR +
"sounds" +
SEPARATOR +
".gitkeep";
fs::remove(_remove_file);
fs::remove(_remove_file); //Remove empty file (used so github creates a proper folder)
}

void Converter::callEdits(const std::string in, const std::string out, const bool is_recursive) {
std::string loc = out +
SEPARATOR +
"loc" +
SEPARATOR +
"en.txt";
"en.txt"; //Path to en.txt
std::string mxml = out +
SEPARATOR +
"main.xml";
"main.xml"; //Path to main.xml

if (!is_recursive) std::cout << "Copying directory...\n";
copyDir(is_recursive);
Expand Down
2 changes: 1 addition & 1 deletion src/Converter.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <string>
#include <vector>
#include "rapidjson/document.h"
#include <rapidjson/document.h>

class Converter {
private:
Expand Down
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <iostream>
#include <string>
#include <filesystem>
#include <regex>
#include <iostream>
#include <memory>
#include "rapidjson/document.h"
#include <regex>
#include <string>
#include <rapidjson/document.h>
#include "Converter.h"

#ifdef _WIN32
Expand Down

0 comments on commit 517541d

Please sign in to comment.