| 1 |
#include <wx/dir.h> |
| 2 |
#include <wx/filefn.h> |
| 3 |
#include <wx/sstream.h> |
| 4 |
#include <wx/wfstream.h> |
| 5 |
|
| 6 |
#include "generated/configure_launcher.h" |
| 7 |
#include "apis/FlagListManager.h" |
| 8 |
#include "global/Compatibility.h" |
| 9 |
#include "global/ProfileKeys.h" |
| 10 |
|
| 11 |
#if IS_WIN32 |
| 12 |
#include "RegistryKeys.h" |
| 13 |
|
| 14 |
#define WIN32_LEAN_AND_MEAN |
| 15 |
#include <Windows.h> |
| 16 |
|
| 17 |
extern HKEY GetRegistryKeyname(wxString& keyNameOut); |
| 18 |
|
| 19 |
bool enumRegistryValue(HKEY parent, DWORD index, wxString& valueNameOut, wxString& valueOut) { |
| 20 |
valueNameOut.Clear(); |
| 21 |
valueOut.Clear(); |
| 22 |
|
| 23 |
WCHAR valueNameBuffer[512]; |
| 24 |
DWORD valueNameBufferSize = static_cast<DWORD>(sizeof(valueNameBuffer)); |
| 25 |
|
| 26 |
BYTE valueBuffer[512]; |
| 27 |
DWORD valueBufferSize = static_cast<DWORD>(sizeof(valueBuffer)); |
| 28 |
|
| 29 |
DWORD type; |
| 30 |
|
| 31 |
LONG ret = RegEnumValueW(parent, index, valueNameBuffer, &valueNameBufferSize, NULL, &type, valueBuffer, &valueBufferSize); |
| 32 |
|
| 33 |
if (ret != ERROR_SUCCESS) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
valueNameOut = wxString(valueNameBuffer, valueNameBufferSize); |
| 38 |
|
| 39 |
switch (type) { |
| 40 |
case REG_BINARY: |
| 41 |
valueOut = wxString::From8BitData(reinterpret_cast<char*>(valueBuffer), valueBufferSize); |
| 42 |
break; |
| 43 |
case REG_DWORD: |
| 44 |
{ |
| 45 |
ULONG val = *reinterpret_cast<ULONG*>(valueBuffer); // I feel dirty... |
| 46 |
valueOut << val; |
| 47 |
break; |
| 48 |
} |
| 49 |
case REG_QWORD: |
| 50 |
{ |
| 51 |
ULONGLONG val = *reinterpret_cast<ULONGLONG*>(valueBuffer); // I feel dirty... |
| 52 |
valueOut << val; |
| 53 |
break; |
| 54 |
} |
| 55 |
case REG_SZ: |
| 56 |
valueOut = wxString(reinterpret_cast<wchar_t*>(valueBuffer), (valueBufferSize / sizeof(WCHAR)) - 1); |
| 57 |
break; |
| 58 |
|
| 59 |
default: |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
void copyValuesIntoConfig(HKEY key, wxFileConfig& config, const wxString& sectionName) { |
| 67 |
config.SetPath(sectionName); |
| 68 |
|
| 69 |
for (DWORD i = 0; ; ++i) { |
| 70 |
wxString name; |
| 71 |
wxString value; |
| 72 |
if (!enumRegistryValue(key, i, name, value)) |
| 73 |
{ |
| 74 |
break; |
| 75 |
} |
| 76 |
config.Write(name, value); |
| 77 |
} |
| 78 |
} |
| 79 |
#endif |
| 80 |
|
| 81 |
#define FSO_CONFIG_FILENAME _T("fs2_open.ini") |
| 82 |
|
| 83 |
extern wxFileName GetPlatformDefaultConfigFilePathNew(); |
| 84 |
extern wxFileName GetPlatformDefaultConfigFilePathOld(); |
| 85 |
|
| 86 |
namespace Compatibility |
| 87 |
{ |
| 88 |
bool SynchronizeOldPilots(ProMan* profileManager) |
| 89 |
{ |
| 90 |
if (!(FlagListManager::GetFlagListManager()->GetBuildCaps() & FlagListManager::BUILD_CAPS_SDL)) |
| 91 |
{ |
| 92 |
// Nothing to do, we have an old build |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
wxLogStatus(_T("Synchronizing old pilot files...")); |
| 97 |
wxFileName oldConfigFolder; |
| 98 |
#if IS_WIN32 |
| 99 |
wxString rootPath; |
| 100 |
if (!profileManager->ProfileRead(PRO_CFG_TC_ROOT_FOLDER, &rootPath)) |
| 101 |
{ |
| 102 |
wxLogWarning(_T("No TC root folder in configuration!")); |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
oldConfigFolder.AssignDir(rootPath, wxPATH_NATIVE); |
| 107 |
#else |
| 108 |
oldConfigFolder.Assign(GetPlatformDefaultConfigFilePathOld()); |
| 109 |
#endif |
| 110 |
|
| 111 |
oldConfigFolder.AppendDir(_T("data")); |
| 112 |
oldConfigFolder.AppendDir(_T("players")); |
| 113 |
|
| 114 |
wxFileName newConfigFolder(GetPlatformDefaultConfigFilePathNew()); |
| 115 |
newConfigFolder.AppendDir(_T("data")); |
| 116 |
newConfigFolder.AppendDir(_T("players")); |
| 117 |
|
| 118 |
if (!oldConfigFolder.DirExists()) |
| 119 |
{ |
| 120 |
// No old config folder so there are no pilot files to copy |
| 121 |
wxLogStatus(_T(" Old pilot directory does not exist, nothing to be done.")); |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
// Heuristic to determine if the files were copied before |
| 126 |
if (newConfigFolder.DirExists()) |
| 127 |
{ |
| 128 |
// new players directory already exists so it was already used before |
| 129 |
// Don't try to copy in this case |
| 130 |
wxLogStatus(_T(" New pilot directory already exists, was probably copied before.")); |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
if (!newConfigFolder.Mkdir(0777, wxPATH_MKDIR_FULL)) |
| 135 |
{ |
| 136 |
wxLogError(_T("Failed to create directory '%s'"), newConfigFolder.GetFullPath().c_str()); |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
// If we are here we need to copy the old pilot files |
| 141 |
wxDir oldDir(oldConfigFolder.GetFullPath()); |
| 142 |
|
| 143 |
wxString pilotFile; |
| 144 |
bool cont = oldDir.GetFirst(&pilotFile, wxEmptyString, wxDIR_FILES); |
| 145 |
|
| 146 |
wxLogStatus(_T("Copying pilot files from '%s' to '%s'."), oldConfigFolder.GetFullPath().c_str(), newConfigFolder.GetFullPath().c_str()); |
| 147 |
while (cont) |
| 148 |
{ |
| 149 |
wxFileName pilotFileName; |
| 150 |
pilotFileName.Assign(oldConfigFolder.GetFullPath(), pilotFile); |
| 151 |
|
| 152 |
wxFileName newPilotFile; |
| 153 |
newPilotFile.Assign(newConfigFolder.GetFullPath(), pilotFile); |
| 154 |
|
| 155 |
wxLogStatus(_T(" Copying '%s' to '%s'"), pilotFileName.GetFullPath().c_str(), newPilotFile.GetFullPath().c_str()); |
| 156 |
if (!wxCopyFile(pilotFileName.GetFullPath(), newPilotFile.GetFullPath())) |
| 157 |
{ |
| 158 |
wxLogError(_T("Failed to copy pilot file '%s'!"), pilotFileName.GetFullPath().c_str()); |
| 159 |
} |
| 160 |
|
| 161 |
cont = oldDir.GetNext(&pilotFile); |
| 162 |
} |
| 163 |
|
| 164 |
wxLogStatus(_T(" Done copying pilot files.")); |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
bool MigrateOldConfig() |
| 169 |
{ |
| 170 |
if (!(FlagListManager::GetFlagListManager()->GetBuildCaps() & FlagListManager::BUILD_CAPS_SDL)) |
| 171 |
{ |
| 172 |
// Nothing to do, we have an old build |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
wxFileName newName = GetPlatformDefaultConfigFilePathNew(); |
| 177 |
newName.SetFullName(FSO_CONFIG_FILENAME); |
| 178 |
|
| 179 |
if (wxFile::Exists(newName.GetFullPath())) { |
| 180 |
// New file already exists, nothing to do here |
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
wxLogStatus(_T("Migrating old configuration...")); |
| 185 |
#if IS_WIN32 |
| 186 |
// On Windows this is implemented by iterating through the registry and copying the values to the config file |
| 187 |
wxString keyName; |
| 188 |
HKEY useKey = GetRegistryKeyname(keyName); |
| 189 |
|
| 190 |
LONG ret = ERROR_SUCCESS; |
| 191 |
HKEY regHandle = 0; |
| 192 |
ret = RegOpenKeyExW(useKey, |
| 193 |
keyName.wc_str(), |
| 194 |
0, |
| 195 |
KEY_READ, |
| 196 |
®Handle |
| 197 |
); |
| 198 |
if (ret != ERROR_SUCCESS) { |
| 199 |
// Key does not exists or some other error, assume it doesn't exist |
| 200 |
return true; // No old config, nothing to do here |
| 201 |
} |
| 202 |
|
| 203 |
wxStringInputStream configBlankInputStream(_T("")); |
| 204 |
|
| 205 |
wxFileConfig outConfig(configBlankInputStream, wxMBConvUTF8()); |
| 206 |
|
| 207 |
copyValuesIntoConfig(regHandle, outConfig, REG_KEY_DEFAULT_FOLDER_CFG); |
| 208 |
|
| 209 |
for (DWORD i = 0;; ++i) { |
| 210 |
WCHAR subkey[255]; |
| 211 |
DWORD subkeyLen = 255; |
| 212 |
|
| 213 |
LONG res = RegEnumKeyExW(regHandle, i, subkey, &subkeyLen, NULL, NULL, NULL, NULL); |
| 214 |
if (res != ERROR_SUCCESS) |
| 215 |
break; |
| 216 |
|
| 217 |
HKEY handle; |
| 218 |
res = RegOpenKeyExW(regHandle, subkey, 0, KEY_READ, &handle); |
| 219 |
if (res != ERROR_SUCCESS) { |
| 220 |
continue; |
| 221 |
} |
| 222 |
wxString sectionName(subkey, subkeyLen); |
| 223 |
|
| 224 |
sectionName = wxString::FromAscii("/") + sectionName; |
| 225 |
|
| 226 |
copyValuesIntoConfig(handle, outConfig, sectionName); |
| 227 |
RegCloseKey(handle); |
| 228 |
} |
| 229 |
|
| 230 |
RegCloseKey(regHandle); |
| 231 |
|
| 232 |
wxLogStatus(_T("Writing fs2_open.ini to %s"), newName.GetFullPath().c_str()); |
| 233 |
wxFFileOutputStream outFileStream(newName.GetFullPath()); |
| 234 |
|
| 235 |
outConfig.Save(outFileStream, wxMBConvUTF8()); |
| 236 |
#else |
| 237 |
wxFileName oldName = GetPlatformDefaultConfigFilePathOld(); |
| 238 |
oldName.SetFullName(FSO_CONFIG_FILENAME); |
| 239 |
|
| 240 |
if (!wxFile::Exists(oldName.GetFullPath())) { |
| 241 |
// Old file does not exist, probably first run. |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
if (!wxCopyFile(oldName.GetFullPath(), newName.GetFullPath())) { |
| 246 |
wxLogError(_T("Failed to copy old configuration file to new location!")); |
| 247 |
return false; |
| 248 |
} |
| 249 |
#endif |
| 250 |
|
| 251 |
wxLogStatus(_T("Migration finished.")); |
| 252 |
return true; |
| 253 |
} |
| 254 |
} |