Skip to content

Commit

Permalink
Sounds are now reloadable and configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestionableM committed Jul 14, 2023
1 parent 5b92edc commit f09296c
Show file tree
Hide file tree
Showing 18 changed files with 48,484 additions and 38 deletions.
18 changes: 18 additions & 0 deletions Code/DirectoryManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ namespace SM
return *reinterpret_cast<DirectoryManager**>(std::uintptr_t(GetModuleHandle(NULL)) + 0x12A78F0);
}

inline bool get_replacement(const std::string& key, std::string& replacement)
{
auto v_iter = content_key_to_path_list.find(key);
if (v_iter == content_key_to_path_list.end())
return false;

replacement = v_iter->second;
return true;
}

inline bool replace_path_r(std::string& path)
{
if (path.empty() || path[0] != L'$')
Expand Down Expand Up @@ -46,6 +56,14 @@ namespace SM
return v_dir_mgr->replace_path_r(path);
}

inline static bool GetReplacement(const std::string& key, std::string& replacement)
{
SM::DirectoryManager* v_dir_mgr = SM::DirectoryManager::GetInstance();
if (!v_dir_mgr) return false;

return v_dir_mgr->get_replacement(key, replacement);
}

private:
DirectoryManager() = delete;
DirectoryManager(const DirectoryManager&) = delete;
Expand Down
40 changes: 39 additions & 1 deletion Code/Utils/File.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include <filesystem>
#include <fstream>
#include <string>

namespace File
{
bool Exists(const std::string& path)
inline bool Exists(const std::string& path)
{
namespace fs = std::filesystem;

Expand All @@ -14,4 +15,41 @@ namespace File

return !v_ec && v_exists;
}

inline bool ReadToString(const std::wstring& path, std::string& r_output)
{
std::ifstream input_file(path, std::ios::binary);
if (!input_file.is_open()) return false;

input_file.seekg(0, std::ios::end);
r_output.resize(input_file.tellg());
input_file.seekg(0, std::ios::beg);

input_file.read(r_output.data(), r_output.size());
input_file.close();

return true;
}

inline bool ReadToStringED(const std::wstring& path, std::string& r_output)
{
std::ifstream v_input_file(path, std::ios::binary);
if (!v_input_file.is_open()) return false;

//Check the first 3 bytes of the file
char v_encoding_buffer;
v_input_file.read(&v_encoding_buffer, 1);

const bool v_guess_has_encoding = (v_encoding_buffer < 0);
const std::size_t v_file_offset = v_guess_has_encoding ? 3 : 0;

v_input_file.seekg(0, std::ios::end);
r_output.resize(static_cast<std::size_t>(v_input_file.tellg()) - v_file_offset);
v_input_file.seekg(v_file_offset, std::ios::beg);

v_input_file.read(r_output.data(), r_output.size());
v_input_file.close();

return true;
}
}
257 changes: 257 additions & 0 deletions Code/Utils/Json.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
#include "Json.hpp"

#include "Utils\Console.hpp"
#include "Utils\File.hpp"

void JsonReader::RemoveComments(std::string& json_string)
{
std::string v_output;
v_output.reserve(json_string.size());

const char* const v_data_beg = json_string.data();
const char* const v_data_end = v_data_beg + json_string.size();

const char* v_data = v_data_beg;

std::size_t v_data_ptr = 0;
while (v_data != v_data_end)
{
switch (*v_data)
{
case '\"':
{
v_data = strchr(v_data + 1, '\"');
if (!v_data) goto smc_escape_loop;

break;
}
case '/':
{
const char* v_last_char = v_data++;
if (v_data == v_data_end)
goto smc_escape_loop;

switch (*v_data)
{
case '/':
{
v_output.append(json_string.begin() + v_data_ptr, json_string.begin() + (v_last_char - v_data_beg));

v_data = strchr(v_data, '\n');
if (!v_data) goto smc_escape_loop;

v_data_ptr = v_data - v_data_beg;
continue;
}
case '*':
{
v_output.append(json_string.begin() + v_data_ptr, json_string.begin() + (v_last_char - v_data_beg));

v_data = strstr(v_data, "*/");
if (!v_data) goto smc_escape_loop;

v_data_ptr = (v_data += 2) - v_data_beg;
continue;
}
default:
break;
}

break;
}
default:
break;
}
/*if (*v_data == '\"')
{
v_data = strchr(v_data + 1, '\"');
if (!v_data) break;
}
else if (*v_data == '/')
{
const char* v_last_char = v_data++;
if (v_data == v_data_end)
{
break;
}
else if (*v_data == '/')
{
v_output.append(json_string.begin() + v_data_ptr, json_string.begin() + (v_last_char - v_data_beg));
v_data = strchr(v_data, '\n');
if (!v_data) break;
v_data_ptr = v_data - v_data_beg;
continue;
}
else if (*v_data == '*')
{
v_output.append(json_string.begin() + v_data_ptr, json_string.begin() + (v_last_char - v_data_beg));
v_data = strstr(v_data, "* /");
if (!v_data) break;
v_data_ptr = (v_data += 2) - v_data_beg;
continue;
}
}*/

v_data++;
}

smc_escape_loop:

if (v_data)
{
const std::size_t v_ptr_diff = v_data - v_data_beg;
const std::size_t v_diff_test = v_ptr_diff - v_data_ptr;
if (v_diff_test != json_string.size())
{
v_output.append(json_string.begin() + v_data_ptr, json_string.begin() + v_ptr_diff);
json_string = std::move(v_output);
}
}
//else
//{
// v_output.append(json_string.substr(v_data_ptr));
//}
}

bool JsonReader::LoadParseSimdjson(const std::wstring& path, simdjson::dom::document& v_doc)
{
try
{
std::string v_json_str;
if (!File::ReadToString(path, v_json_str))
return false;

simdjson::dom::parser v_parser;
v_parser.parse_into_document(v_doc, v_json_str);

return true;
}
#if defined(_DEBUG) || defined(DEBUG)
catch (const simdjson::simdjson_error& v_err)
{
DebugErrorL("Couldn't parse: ", path, "\nError: ", v_err.what());
}
#else
catch (...) {}
#endif

return false;
}

bool JsonReader::LoadParseSimdjsonC(const std::wstring& path, simdjson::dom::document& v_doc, const simdjson::dom::element_type& type_check)
{
try
{
std::string v_json_str;
if (!File::ReadToStringED(path, v_json_str))
return false;

simdjson::dom::parser v_parser;
v_parser.parse_into_document(v_doc, v_json_str);

const auto v_root = v_doc.root();
if (v_root.type() != type_check)
{
DebugErrorL("Mismatching root json type!\nFile: ", path);
return false;
}

return true;
}
#if defined(_DEBUG) || defined(DEBUG)
catch (const simdjson::simdjson_error& v_err)
{
DebugErrorL("Couldn't parse: ", path, "\nError: ", v_err.what());
}
#else
catch (...) {}
#endif

return false;
}

bool JsonReader::LoadParseSimdjsonComments(const std::wstring& path, simdjson::dom::document& v_doc)
{
try
{
std::string v_json_str;
if (!File::ReadToString(path, v_json_str))
return false;

JsonReader::RemoveComments(v_json_str);

simdjson::dom::parser v_parser;
v_parser.parse_into_document(v_doc, v_json_str);

return true;
}
#if defined(_DEBUG) || defined(DEBUG)
catch (const simdjson::simdjson_error& v_err)
{
DebugErrorL("Couldn't parse: ", path, "\nError: ", v_err.what());
}
#else
catch (...) {}
#endif

return false;
}

bool JsonReader::LoadParseSimdjsonCommentsC(const std::wstring& path, simdjson::dom::document& v_doc, const simdjson::dom::element_type& type_check)
{
try
{
std::string v_json_str;
if (!File::ReadToString(path, v_json_str))
return false;

JsonReader::RemoveComments(v_json_str);

simdjson::dom::parser v_parser;
v_parser.parse_into_document(v_doc, v_json_str);

const auto v_root = v_doc.root();
if (v_root.type() != type_check)
{
DebugErrorL("Mismatching root json type!\nFile: ", path);
return false;
}

return true;
}
#if defined(_DEBUG) || defined(DEBUG)
catch (const simdjson::simdjson_error& v_err)
{
DebugErrorL("Couldn't parse: ", path, "\nError: ", v_err.what());
}
#else
catch (...) {}
#endif

return false;
}

bool JsonReader::ParseSimdjsonString(const std::string& json_str, simdjson::dom::document& v_doc)
{
try
{
simdjson::dom::parser v_parser;
v_parser.parse_into_document(v_doc, json_str);

return true;
}
#if defined(_DEBUG) || defined(DEBUG)
catch (const simdjson::simdjson_error& v_err)
{
DebugErrorL("Couldn't parse a json string. Error: ", v_err.what());
}
#else
catch (...) {}
#endif

return false;
}
Loading

0 comments on commit f09296c

Please sign in to comment.