Skip to content

Commit c2a276e

Browse files
committed
Use different cfg folder
1 parent 78eeb1a commit c2a276e

File tree

2 files changed

+91
-7
lines changed

2 files changed

+91
-7
lines changed

src/client/component/game_data.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <std_include.hpp>
2+
#include "loader/component_loader.hpp"
3+
4+
#include "game/game.hpp"
5+
6+
#include "console.hpp"
7+
#include "filesystem.hpp"
8+
#include "mods.hpp"
9+
#include "mod_stats.hpp"
10+
#include "command.hpp"
11+
12+
#include <utils/hook.hpp>
13+
#include <utils/io.hpp>
14+
#include <utils/string.hpp>
15+
#include <utils/concurrency.hpp>
16+
#include <utils/thread.hpp>
17+
#include <utils/properties.hpp>
18+
19+
#define PLAYERS_FOLDER "players2/"
20+
#define DEFAULT_PLAYERS_FOLDER PLAYERS_FOLDER "default"
21+
#define H2_MOD_PLAYERS_FOLDER PLAYERS_FOLDER "h2-mod"
22+
23+
namespace game_data
24+
{
25+
namespace
26+
{
27+
std::optional<std::string> find_bnet_player_folder()
28+
{
29+
const auto dirs = utils::io::list_files(PLAYERS_FOLDER);
30+
for (const auto& dir : dirs)
31+
{
32+
if (dir == DEFAULT_PLAYERS_FOLDER || dir == H2_MOD_PLAYERS_FOLDER)
33+
{
34+
continue;
35+
}
36+
37+
const auto cfg_file = std::format("{}\\config.cfg", dir);
38+
if (utils::io::file_exists(cfg_file))
39+
{
40+
return dir;
41+
}
42+
}
43+
44+
return {};
45+
}
46+
47+
void initialize_players_folder()
48+
{
49+
if (utils::io::directory_exists(H2_MOD_PLAYERS_FOLDER))
50+
{
51+
return;
52+
}
53+
54+
if (utils::io::directory_exists(DEFAULT_PLAYERS_FOLDER))
55+
{
56+
utils::io::copy_folder(DEFAULT_PLAYERS_FOLDER, H2_MOD_PLAYERS_FOLDER);
57+
return;
58+
}
59+
60+
const auto bnet_folder = find_bnet_player_folder();
61+
if (!bnet_folder.has_value())
62+
{
63+
return;
64+
}
65+
66+
utils::io::copy_folder(bnet_folder.value(), H2_MOD_PLAYERS_FOLDER);
67+
}
68+
}
69+
70+
class component final : public component_interface
71+
{
72+
public:
73+
void post_unpack() override
74+
{
75+
initialize_players_folder();
76+
utils::hook::inject(0x14059D64E + 3, H2_MOD_PLAYERS_FOLDER);
77+
}
78+
};
79+
}
80+
81+
REGISTER_COMPONENT(game_data::component)

src/client/component/mods.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <utils/concurrency.hpp>
1919

2020
#define MOD_FOLDER "mods"
21-
#define MOD_STATS_FOLDER "players2/modstats"
21+
#define OLD_MOD_STATS_FOLDER "players2/modstats"
2222

2323
namespace mods
2424
{
@@ -258,15 +258,13 @@ namespace mods
258258

259259
std::unordered_map<std::string, std::string> info;
260260
const auto data = utils::io::read_file(info_file);
261-
try
262-
{
263-
return {nlohmann::json::parse(data)};
264-
}
265-
catch (const std::exception&)
261+
const auto parsed = nlohmann::json::parse(data, {}, false);
262+
if (parsed.is_discarded())
266263
{
264+
return {};
267265
}
268266

269-
return {};
267+
return {parsed};
270268
}
271269

272270
void load(const std::string& path)
@@ -312,6 +310,11 @@ namespace mods
312310
class component final : public component_interface
313311
{
314312
public:
313+
void post_start() override
314+
{
315+
utils::io::remove_directory(OLD_MOD_STATS_FOLDER);
316+
}
317+
315318
void post_unpack() override
316319
{
317320
if (!utils::io::directory_exists(MOD_FOLDER))

0 commit comments

Comments
 (0)