Skip to content

Commit 81a56ce

Browse files
committed
Make mods.txt parsing skip BOM; make .json only write if moddatavector is different from existing json
1 parent 4a5de8a commit 81a56ce

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

UE4SS/include/UE4SSProgram.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ namespace RC
215215

216216
GLZ_LOCAL_META(ModData, mod_name, mod_enabled);
217217
};
218+
std::vector<ModData> global_mod_data_vector;
219+
218220

219221
auto init() -> void;
220222
auto is_program_started() -> bool;

UE4SS/src/UE4SSProgram.cpp

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ namespace RC
384384
std::filesystem::path game_exe_path = exe_path_buffer;
385385
std::filesystem::path game_directory_path = game_exe_path.parent_path();
386386
m_legacy_root_directory = game_directory_path;
387-
387+
388388
m_working_directory = m_root_directory;
389389
m_mods_directory = m_working_directory / "Mods";
390390
m_game_executable_directory = game_directory_path;
@@ -1119,7 +1119,24 @@ namespace RC
11191119

11201120
auto UE4SSProgram::read_mods_json(std::string enabled_mods_file, std::vector<ModData>& mod_data_vector) -> void
11211121
{
1122-
auto ec = glz::read_file_json(mod_data_vector, enabled_mods_file, std::string{});
1122+
std::string buffer{};
1123+
glz::parse_error pe = glz::read_file_json(mod_data_vector, enabled_mods_file, buffer);
1124+
if (pe)
1125+
{
1126+
std::string descriptive_error = glz::format_error(pe, buffer);
1127+
1128+
size_t count = descriptive_error.size() + 1;
1129+
wchar_t* converted_method_name = new wchar_t[count];
1130+
1131+
size_t num_of_char_converted = 0;
1132+
mbstowcs_s(&num_of_char_converted, converted_method_name, count, descriptive_error.data(), count);
1133+
1134+
auto converted = File::StringViewType(converted_method_name);
1135+
1136+
delete[] converted_method_name;
1137+
1138+
Output::send<LogLevel::Error>(STR("{}\n\nError parsing mods.json file, please fix the file...\n"), converted);
1139+
}
11231140
}
11241141

11251142
auto UE4SSProgram::write_mods_json(std::string enabled_mods_file, std::vector<ModData>& mod_data_vector) -> void
@@ -1128,17 +1145,20 @@ namespace RC
11281145
glz::write<glz::opts{.prettify = true}>(mod_data_vector, mod_data_buffer);
11291146
glz::error_code ec = glz::buffer_to_file(mod_data_buffer, enabled_mods_file);
11301147
}
1131-
1148+
11321149
auto UE4SSProgram::convert_legacy_mods_file(StringType legacy_enabled_mods_file, std::vector<ModData>& mod_data_vector) -> void
11331150
{
11341151
Output::send(STR("Converting legacy mods.txt to mods.json...\n"));
1135-
1136-
// 'mods.txt' exists, lets parse it
1137-
std::wifstream mods_stream{legacy_enabled_mods_file};
1138-
1152+
std::wifstream mods_stream = File::open_file_skip_BOM(legacy_enabled_mods_file);
1153+
try {
1154+
mods_stream = File::open_file_skip_BOM(legacy_enabled_mods_file);
1155+
} catch (const std::exception& e) {
1156+
std::cerr << e.what() << std::endl;
1157+
}
11391158
std::wstring current_line;
11401159
while (std::getline(mods_stream, current_line))
11411160
{
1161+
11421162
// Don't parse any lines with ';'
11431163
if (current_line.find(L";") != current_line.npos)
11441164
{
@@ -1180,21 +1200,24 @@ namespace RC
11801200
ProfilerScope();
11811201
// Part #1: Start all mods that are enabled in mods.json.
11821202
std::filesystem::path mods_directory = UE4SSProgram::get_program().get_mods_directory();
1183-
StringType legacy_enabled_mods_file{mods_directory / "mods.txt"};
1203+
std::filesystem::path legacy_enabled_mods_file{mods_directory / "mods.txt"};
11841204
std::filesystem::path enabled_mods_file{mods_directory / "mods.json"};
11851205
std::vector<UE4SSProgram::ModData> mod_data_vector{};
1206+
UE4SSProgram& program = UE4SSProgram::get_program();
11861207

1187-
if (std::filesystem::exists(legacy_enabled_mods_file))
1188-
{
1189-
UE4SSProgram::convert_legacy_mods_file(legacy_enabled_mods_file, mod_data_vector);
1190-
}
11911208
if (std::filesystem::exists(enabled_mods_file))
11921209
{
11931210
UE4SSProgram::read_mods_json(enabled_mods_file.string(), mod_data_vector);
11941211
}
11951212

1213+
if (std::filesystem::exists(legacy_enabled_mods_file))
1214+
{
1215+
UE4SSProgram::convert_legacy_mods_file(legacy_enabled_mods_file, mod_data_vector);
1216+
}
1217+
11961218
UE4SSProgram::write_mods_json(enabled_mods_file.string(), mod_data_vector);
11971219

1220+
11981221
Output::send(STR("Starting mods (from mods.json load order)...\n"));
11991222
for (auto it = mod_data_vector.begin(); it != mod_data_vector.end(); ++it)
12001223
{

0 commit comments

Comments
 (0)