Skip to content

Commit

Permalink
code cleanup and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yudonglin committed Feb 5, 2024
1 parent 79b41b9 commit 3f61b80
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 149 deletions.
2 changes: 1 addition & 1 deletion vns-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ project(vns)
file(GLOB SOURCES "libs/*/*.h" "libs/*/*.cpp" "libs/*/*.hpp")

# Add source to this project's executable.
add_executable(vns ${SOURCES} "vns.cpp" "vns.h" "processor.cpp" "naming.cpp" "naming.h" "content.h" "content.cpp" "contentNext.h" "contentNext.cpp" "processor.h" "functions.h" "tests.h" "tests.cpp" "functions.cpp" "compiler.h" "compiler.cpp" "version.h" "contentManager.h" "contentManager.cpp" "decompiler.cpp" "decompiler.h")
add_executable(vns ${SOURCES} "vns.cpp" "vns.h" "scriptProcessor.cpp" "naming.cpp" "naming.h" "content.h" "content.cpp" "contentNext.h" "contentNext.cpp" "scriptProcessor.h" "functions.h" "tests.h" "tests.cpp" "functions.cpp" "compiler.h" "compiler.cpp" "version.h" "contentManager.h" "contentManager.cpp" "decompiler.cpp" "decompiler.h")

if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET vns PROPERTY CXX_STANDARD 20)
Expand Down
28 changes: 12 additions & 16 deletions vns-cpp/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@
#include "compiler.h"
#include <fstream>
#include <iostream>
#include "processor.h"
#include "scriptProcessor.h"

// get the info of compiler
std::unordered_map<std::string, int> Compiler::get_compiler_info()
{
return {
{"version", VERSION},
return {{"version", VERSION},
{"reversion", REVISION},
{"patch", PATCH},
{"compiledAt", static_cast<int>(std::time(nullptr))}
};
{"compiledAt", static_cast<int>(std::time(nullptr))}};
}

// load data from file directly
std::unordered_map<std::string, std::any> Compiler::load(const std::filesystem::path &path)
{
Processor processor;
ScriptProcessor processor;
processor.process(path);
return {
{"dialogs", processor.get_output()},
return {{"dialogs", processor.get_output()},
{"compiler", get_compiler_info()},
{"id", processor.get_id()},
{"language", processor.get_language()}
};
{"language", processor.get_language()}};
}

// load data in the form of json string from file directly
Expand All @@ -38,13 +34,13 @@ std::string Compiler::load_as_string(const std::filesystem::path &path)
// load data in the form of json data from file directly
nlohmann::json Compiler::load_as_json(const std::filesystem::path &path)
{
Processor _processor;
_processor.process(path);
ScriptProcessor processor;
processor.process(path);
nlohmann::json json_data;
json_data["dialogs"] = _processor.get_output_as_json();
json_data["dialogs"] = processor.get_output_as_json();
json_data["compiler"] = get_compiler_info();
json_data["id"] = _processor.get_id();
json_data["language"] = _processor.get_language();
json_data["id"] = processor.get_id();
json_data["language"] = processor.get_language();
return json_data;
}

Expand All @@ -58,7 +54,7 @@ void Compiler::compile(const std::filesystem::path &path, const std::filesystem:
{
for (const auto &entry: std::filesystem::directory_iterator(path))
{
if (entry.path().extension() == Processor::SCRIPTS_FILE_EXTENSION)
if (entry.path().extension() == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
{
compile(entry.path());
}
Expand Down
2 changes: 2 additions & 0 deletions vns-cpp/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define COMPILER_H

#include <filesystem>
#include <unordered_map>
#include <any>
#include "libs/nlohmann/json.hpp"

class Compiler
Expand Down
41 changes: 23 additions & 18 deletions vns-cpp/content.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "content.h"


Content::Content(const ContentDataType &data, const std::string &content_id)
{
id = content_id;
Expand All @@ -10,40 +9,46 @@ Content::Content(const ContentDataType &data, const std::string &content_id)
contents = cast<std::vector<std::string>>(data, "contents", {});
narrator = cast<std::string>(data, "narrator", "");
previous = cast<std::string>(data, "previous", "");
next = ContentNext(
cast<std::unordered_map<
std::string, ContentNextValueType>>(data, "next", {{"type", "default"},
{"target", ""}}));
next = ContentNext(cast<std::unordered_map<std::string, ContentNextValueType>>(data, "next", {{"type", "default"},
{"target", ""}}));
comments = cast<std::vector<std::string>>(data, "comments", {});
}

bool Content::has_next() const
{ return !next.is_null(); }
{
return !next.is_null();
}

ContentDataType Content::to_map() const
{
return {
{"background_image", background_image},
return {{"background_image", background_image},
{"background_music", background_music},
{"character_images", character_images},
{"contents", contents},
{"previous", previous},
{"narrator", narrator},
{"next", next.to_map()},
{"comments", comments}
};
{"comments", comments}};
}

nlohmann::json Content::to_json() const
{
nlohmann::json json_data;
if (!background_image.empty()) json_data["background_image"] = background_image;
if (!background_music.empty()) json_data["background_music"] = background_music;
if (!character_images.empty()) json_data["character_images"] = character_images;
if (!contents.empty()) json_data["contents"] = contents;
if (!previous.empty()) json_data["previous"] = previous;
if (!narrator.empty()) json_data["narrator"] = narrator;
json_data["next"] = next.to_json();
if (!comments.empty()) json_data["comments"] = comments;
if (!background_image.empty())
json_data["background_image"] = background_image;
if (!background_music.empty())
json_data["background_music"] = background_music;
if (!character_images.empty())
json_data["character_images"] = character_images;
if (!contents.empty())
json_data["contents"] = contents;
if (!previous.empty())
json_data["previous"] = previous;
if (!narrator.empty())
json_data["narrator"] = narrator;
if (nlohmann::json next_in_json = next.to_json(); !next_in_json.empty())
json_data["next"] = next_in_json;
if (!comments.empty())
json_data["comments"] = comments;
return json_data;
}
7 changes: 2 additions & 5 deletions vns-cpp/content.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include <vector>
#include "contentNext.h"

using ContentValueType = std::variant<std::string, std::vector<std::string>, std::unordered_map<
std::string, ContentNextValueType>>;
using ContentDataType = std::unordered_map<std::string, ContentValueType>;
using ContentDataType = std::unordered_map<std::string, std::variant<std::string, std::vector<std::string>, std::unordered_map<std::string, ContentNextValueType>>>;

using SectionDataType = std::unordered_map<std::string, ContentDataType>;

Expand Down Expand Up @@ -38,8 +36,7 @@ struct Content
std::vector<std::string> comments;
std::string id;

template<typename T>
static T cast(const ContentDataType &data, const std::string &k, T default_v)
template<typename T> static T cast(const ContentDataType &data, const std::string &k, T default_v)
{
const auto it = data.find(k);
if (it != data.end())
Expand Down
33 changes: 24 additions & 9 deletions vns-cpp/contentNext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,42 @@ nlohmann::json ContentNext::to_json() const
{
json_data["type"] = type_;
if (has_multi_targets())
{ json_data["target"] = get_targets(); }
else
{ json_data["target"] = get_target(); }
{
json_data["target"] = get_targets();
} else
{
json_data["target"] = get_target();
}
}
return json_data;
}

bool ContentNext::has_single_target() const
{ return std::holds_alternative<std::string>(target_); }
{
return std::holds_alternative<std::string>(target_);
}

bool ContentNext::has_multi_targets() const
{ return !has_single_target(); }
{
return !has_single_target();
}

std::string ContentNext::get_type() const
{ return type_; }
{
return type_;
}

std::string ContentNext::get_target() const
{ return std::get<std::string>(target_); }
{
return std::get<std::string>(target_);
}

MultiTargetsType ContentNext::get_targets() const
{ return std::get<MultiTargetsType>(target_); }
{
return std::get<MultiTargetsType>(target_);
}

bool ContentNext::is_null() const
{ return has_single_target() ? get_target().empty() : get_targets().empty(); }
{
return has_single_target() ? get_target().empty() : get_targets().empty();
}
12 changes: 7 additions & 5 deletions vns-cpp/contentNext.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <string>
#include <variant>
#include <vector>

#include "libs/nlohmann/json.hpp"

using MultiTargetsType = std::vector<std::unordered_map<std::string, std::string> >;
Expand All @@ -19,15 +18,18 @@ class ContentNext
{
public:
ContentNext(std::string type, ContentNextValueType target) : type_(std::move(type)), target_(std::move(target))
{}
{
}

explicit ContentNext(const std::unordered_map<std::string, ContentNextValueType> &data) : ContentNext(
std::get<std::string>(data.contains("type") ? data.at("type") : "default"),
data.contains("type") ? std::get<std::string>(data.at("type")) : "default",
data.contains("target") ? data.at("target") : "")
{}
{
}

ContentNext() : ContentNext("default", "")
{}
{
}

[[nodiscard]] std::string get_type() const;

Expand Down
54 changes: 25 additions & 29 deletions vns-cpp/decompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ std::string Decompiler::to_str_in_case_null(const std::string &text)
return text.empty() ? "null" : text;
}

void
Decompiler::decompile(const std::unordered_map<std::string, std::any> &data, const std::filesystem::path &out_path)
void Decompiler::decompile(const std::unordered_map<std::string, std::any> &data, const std::filesystem::path &out_path)
{
// If data is empty, return directly
if (data.empty())
Expand All @@ -28,12 +27,11 @@ Decompiler::decompile(const std::unordered_map<std::string, std::any> &data, con
// add comment for Fundamental parameters
result_ss << "# Fundamental parameters\n";
// create id line
result_ss << TAG_STARTS << "id" << TAG_ENDS << to_str_in_case_null(std::any_cast<std::string>(data.at("id"))) <<
"\n";
// create language line
result_ss << TAG_STARTS << "language" << TAG_ENDS << to_str_in_case_null(
std::any_cast<std::string>(data.at("language")))
result_ss << TAG_STARTS << "id" << TAG_ENDS << to_str_in_case_null(std::any_cast<std::string>(data.at("id")))
<< "\n";
// create language line
result_ss << TAG_STARTS << "language" << TAG_ENDS
<< to_str_in_case_null(std::any_cast<std::string>(data.at("language"))) << "\n";

// Iterate through all sections
for (const std::string &k: data | std::views::keys)
Expand All @@ -50,50 +48,48 @@ Decompiler::decompile(const std::unordered_map<std::string, std::any> &data, con
const ContentDataType &current_dialog = content_manager.get_content();
// Process comments
if (const auto &comments = std::get<std::vector<std::string>>(
current_dialog.at("comments")); !comments.
empty())
current_dialog.at("comments")); !comments.empty())
{
result_ss << "\n";
for (const std::string &note: comments)
result_ss << "// " << note << "\n";
}
// Write speaker's name
result_ss << (content_manager.get_current()->narrator.empty()
? "null"
: content_manager.get_current()->narrator) << ":\n";
result_ss << (content_manager.get_current()->narrator.empty() ? "null"
: content_manager.get_current()->narrator)
<< ":\n";
// Write dialogue
for (const auto &sentence: std::get<std::vector<std::string>>(current_dialog.at("contents")))
result_ss << "- " << sentence << "\n";

// If the following content has changed, write it
// Write background
if (content_manager.get_previous() == nullptr || content_manager.get_current()->background_image !=
content_manager.get_previous()->background_image)
if (content_manager.get_previous() == nullptr ||
content_manager.get_current()->background_image != content_manager.get_previous()->background_image)
{
if (content_manager.get_previous() == nullptr || content_manager.get_previous()->next.get_type() !=
"scene")
result_ss << TAG_STARTS << "bgi" << TAG_ENDS << to_str_in_case_null(
content_manager.get_current()->background_image) << "\n";
if (content_manager.get_previous() == nullptr ||
content_manager.get_previous()->next.get_type() != "scene")
result_ss << TAG_STARTS << "bgi" << TAG_ENDS
<< to_str_in_case_null(content_manager.get_current()->background_image) << "\n";
else
result_ss << TAG_STARTS << "scene" << TAG_ENDS << to_str_in_case_null(
content_manager.get_current()->background_image) << "\n";
result_ss << TAG_STARTS << "scene" << TAG_ENDS
<< to_str_in_case_null(content_manager.get_current()->background_image) << "\n";
}
// Write background music
if (content_manager.get_previous() == nullptr || content_manager.get_current()->background_music !=
content_manager.get_previous()->background_music)
if (content_manager.get_previous() == nullptr ||
content_manager.get_current()->background_music != content_manager.get_previous()->background_music)
{
result_ss << TAG_STARTS << "bgm" << TAG_ENDS << to_str_in_case_null(
content_manager.get_current()->background_music) << "\n";
result_ss << TAG_STARTS << "bgm" << TAG_ENDS
<< to_str_in_case_null(content_manager.get_current()->background_music) << "\n";
}
// Write current character images
if (content_manager.get_previous() == nullptr || content_manager.get_current()->character_images !=
content_manager.
get_previous()->character_images)
if (content_manager.get_previous() == nullptr ||
content_manager.get_current()->character_images != content_manager.get_previous()->character_images)
{
if (content_manager.get_current()->character_images.empty())
result_ss << TAG_STARTS << "hide" << TAG_ENDS << "*\n";
else if (content_manager.get_previous() == nullptr || content_manager.get_previous()->character_images.
empty())
else if (content_manager.get_previous() == nullptr ||
content_manager.get_previous()->character_images.empty())
{
result_ss << TAG_STARTS << "display" << TAG_ENDS;
for (const std::string &character_name: content_manager.get_current()->character_images)
Expand Down
5 changes: 2 additions & 3 deletions vns-cpp/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

bool ichar_equals(const char a, const char b)
{
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
return std::tolower(static_cast<unsigned char>(a)) == std::tolower(static_cast<unsigned char>(b));
}

bool iequals(std::string_view lhs, std::string_view rhs)
Expand Down Expand Up @@ -44,7 +43,7 @@ std::string to_lower(const std::string &input)

for (char &c: result)
{
c = std::tolower(c);
c = static_cast<char>(std::tolower(c));
}

return result;
Expand Down
Loading

0 comments on commit 3f61b80

Please sign in to comment.