-
Notifications
You must be signed in to change notification settings - Fork 9
/
TMM.cpp
173 lines (146 loc) · 3.93 KB
/
TMM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// TMM.cpp : Defines the entry point for the application.
//
#include "TMM.h"
#include "UI/ModWindow.h"
#include "UI/RootDirWindow.h"
#include <wx/stdpaths.h>
#include <wx/fileconf.h>
wxIMPLEMENT_APP(TMM);
const char* ConfigFile = "Settings.ini";
const char* ConfigFileRootKey = "RootDir";
const char* ConfigFileWaitForTeraKey = "WaitForTera";
const char* GameConfigFilePath = "ModList.tmm";
const char* CompositeMapperFile = "CompositePackageMapper.dat";
const char* CompositeMapperBackupFile = "CompositePackageMapper.clean";
const char* CookedPcDir = "CookedPC";
const char* ModsStorageDir = "CookedPC";
wxString GetConfigPath()
{
wxString path = wxStandardPaths::Get().GetUserLocalDataDir() + wxFILE_SEP_PATH;
if (!wxDirExists(path))
{
wxMkDir(path);
}
path += ConfigFile;
return path;
}
void TMM::LoadAppConfig()
{
wxString path = GetConfigPath();
wxFileConfig cfg(wxEmptyString, wxEmptyString, path);
cfg.SetPath(path);
RootDir = cfg.Read(ConfigFileRootKey, wxEmptyString).ToStdWstring();
WaitForTera = cfg.ReadBool(ConfigFileWaitForTeraKey, false);
}
void TMM::SaveAppConfig()
{
wxString path = GetConfigPath();
wxFileConfig cfg(wxEmptyString, wxEmptyString, path);
cfg.SetPath(path);
wxString tmpPath = RootDir.wstring();
cfg.Write(ConfigFileRootKey, tmpPath);
cfg.Write(ConfigFileWaitForTeraKey, WaitForTera);
}
void TMM::LoadGameConfig()
{
if (!std::filesystem::exists(GameConfigPath))
{
SaveGameConfig();
}
std::ifstream s(GameConfigPath, std::ios::binary | std::ios::in);
s >> GameConfig;
}
void TMM::SaveGameConfig()
{
std::ofstream s(GameConfigPath, std::ios::binary | std::ios::out);
s << GameConfig;
}
void TMM::ChangeRootDir(const std::filesystem::path& newRootDir)
{
RootDir = newRootDir;
if (!SetupPaths())
{
ExitMainLoop();
return;
}
SaveAppConfig();
GameConfig.Mods.clear();
LoadGameConfig();
ModWindow* mainWindow = new ModWindow(nullptr, GameConfig.Mods);
mainWindow->Show();
}
void TMM::UpdateModsList(const std::vector<ModEntry> modData)
{
GameConfig.Mods = modData;
SaveGameConfig();
}
bool TMM::BackupCompositeMapperFile()
{
if (!std::filesystem::exists(CompositeMapperPath))
{
return false;
}
std::error_code err;
if (!std::filesystem::exists(BackupCompositeMapperPath))
{
return std::filesystem::copy_file(CompositeMapperPath, BackupCompositeMapperPath, err);
}
else
{
if (!std::filesystem::remove(BackupCompositeMapperPath, err))
{
return false;
}
return std::filesystem::copy_file(CompositeMapperPath, BackupCompositeMapperPath, err);
}
return true;
}
int TMM::OnRun()
{
LoadAppConfig();
if (!SetupPaths())
{
return 0;
}
SaveAppConfig();
LoadGameConfig();
ModWindow* mainWindow = new ModWindow(nullptr, GameConfig.Mods);
mainWindow->Show();
return wxApp::OnRun();
}
bool TMM::SetupPaths()
{
if (RootDir.empty() || !std::filesystem::exists(RootDir))
{
RootDirWindow rooWin(nullptr, RootDir.wstring());
if (rooWin.ShowModal() == wxID_OK)
{
RootDir = rooWin.GetPath().ToStdWstring();
}
if (RootDir.empty() || !std::filesystem::exists(RootDir))
{
return false;
}
}
CompositeMapperPath = RootDir / CookedPcDir / CompositeMapperFile;
while (!std::filesystem::exists(CompositeMapperPath))
{
wxMessageBox(_("Couldn't find \"S1Game\\CookedPC\\CompositePackageMapper.dat\" file."), _("Error!"), wxICON_ERROR);
RootDirWindow rooWin(nullptr, RootDir.wstring());
if (rooWin.ShowModal() != wxID_OK)
{
return false;
}
RootDir = rooWin.GetPath().ToStdWstring();
if (RootDir.empty() || !std::filesystem::exists(RootDir))
{
return false;
}
CompositeMapperPath = RootDir / CookedPcDir / CompositeMapperFile;
}
ClientDir = RootDir.parent_path();
ModsDir = RootDir / ModsStorageDir;
BackupCompositeMapperPath = ModsDir / CompositeMapperBackupFile;
GameConfigPath = ModsDir / GameConfigFilePath;
return true;
}