Merge pull request #685 from lodle/MiscFixes

Misc fixes
master
Mark Chandler 2013-10-29 16:21:12 -07:00
commit e8240b30e2
6 changed files with 219 additions and 56 deletions

View File

@ -26,46 +26,83 @@ END_EVENT_TABLE()
#define STEAMPATH "HKEY_CURRENT_USER\\Software\\Valve\\Steam\\SteamPath"
#define NOSTEAM "Steam Not Installed"
int GetSteamUsers(wxChoice *cbBox)
const char* g_szIgnoredFolders[] =
{
if (!cbBox)
return 0;
"common",
"sourcemods",
"media",
"downloading",
NULL
};
cbBox->Clear();
int GetSteamUsers(std::vector<gcString> &vUsers)
{
std::string steampath = UTIL::OS::getConfigValue(STEAMPATH);
if (steampath.size() == 0)
{
cbBox->Append(wxT(NOSTEAM));
return 0;
}
gcString searchPath("{0}\\steamapps\\", steampath);
std::vector<UTIL::FS::Path> fileList;
UTIL::FS::getAllFolders(UTIL::FS::Path(searchPath, "", false), fileList);
if (fileList.empty())
{
cbBox->Append(wxT(NOSTEAM));
return 0;
}
int num = 0;
for (size_t x=0; x<fileList.size(); x++)
for (size_t x = 0; x < fileList.size(); x++)
{
if (fileList[x].getLastFolder() == "common" || fileList[x].getLastFolder() == "SourceMods" || fileList[x].getLastFolder() == "media")
gcString strFolder(fileList[x].getLastFolder());
std::transform(strFolder.begin(), strFolder.end(), strFolder.begin(), ::tolower);
int y = 0;
while (g_szIgnoredFolders[y])
{
if (strFolder == g_szIgnoredFolders[y])
break;
++y;
}
if (g_szIgnoredFolders[y])
continue;
cbBox->Append(fileList[x].getLastFolder());
vUsers.push_back(fileList[x].getLastFolder());
num++;
}
return num;
}
SteamUserDialog::SteamUserDialog( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : gcDialog( parent, id, title, pos, size, style )
int GetSteamUsers(wxChoice *cbBox)
{
if (!cbBox)
return 0;
cbBox->Clear();
std::vector<gcString> vUsers;
int nCount = GetSteamUsers(vUsers);
if (nCount == 0)
{
cbBox->Append(wxT(NOSTEAM));
return 0;
}
else
{
std::for_each(vUsers.begin(), vUsers.end(), [cbBox](gcString &path) -> void {
cbBox->Append(path);
});
}
return nCount;
}
SteamUserDialog::SteamUserDialog(wxWindow* parent)
: gcDialog(parent, wxID_ANY, wxT("Select Steam Account"), wxDefaultPosition, wxSize(300, 250), wxCAPTION | wxSTAY_ON_TOP | wxTAB_TRAVERSAL)
{
this->SetSizeHints( wxDefaultSize, wxDefaultSize );

View File

@ -30,7 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
class SteamUserDialog : public gcDialog
{
public:
SteamUserDialog( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("Select Steam Account"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(250,190) , long style = wxCAPTION|wxSTAY_ON_TOP|wxTAB_TRAVERSAL );
SteamUserDialog(wxWindow* parent);
~SteamUserDialog();
const char* getSteamUser(){return m_szSteamUser.c_str();}

View File

@ -172,22 +172,12 @@ std::vector<MapElementI*> DesuraJSSettings::getLanguages()
return ret;
}
extern int GetSteamUsers(std::vector<gcString> &vUsers);
std::vector<gcString> DesuraJSSettings::getSteamNames()
{
std::vector<gcString> ret;
gcString searchPath("{0}{1}steamapps{1}", UTIL::OS::getConfigValue(STEAMPATH), DIRS_STR);
std::vector<UTIL::FS::Path> fileList;
UTIL::FS::getAllFolders(UTIL::FS::Path(searchPath, "", false), fileList);
for (size_t x=0; x<fileList.size(); x++)
{
if (fileList[x].getLastFolder() == "common" || fileList[x].getLastFolder() == "SourceMods" || fileList[x].getLastFolder() == "media")
continue;
ret.push_back(fileList[x].getLastFolder());
}
GetSteamUsers(ret);
if (ret.size() == 0)
ret.push_back(NOSTEAM);

View File

@ -75,6 +75,7 @@ MCFManager* g_pMCFManager = NULL;
void InitMCFManager(const char* appDataPath, const char* mcfDataPath)
{
g_pMCFManager = new MCFManager(appDataPath, mcfDataPath);
g_pMCFManager->init();
}
void DelMCFManager()
@ -92,11 +93,14 @@ MCFManager* GetMCFManager()
MCFManager::MCFManager(const char* appDataPath, const char* mcfDataPath)
: m_szAppDataPath(appDataPath)
, m_szMCFSavePath(mcfDataPath)
{
createMcfDbTables(appDataPath);
m_szAppDataPath = appDataPath;
m_szMCFSavePath = mcfDataPath;
}
void MCFManager::init()
{
createMcfDbTables(m_szAppDataPath.c_str());
migrateOldFiles();
}
@ -107,20 +111,25 @@ public:
MCFBuild build;
MCFBranch branch;
gcString path;
gcString newPath;
};
void MCFManager::migrateOldFiles()
void MCFManager::getListOfBadMcfPaths(const gcString &szItemDb, std::vector<MigrateInfo> &delList, std::vector<MigrateInfo> &updateList)
{
gcString szItemDb = getMcfDb(m_szAppDataPath.c_str());
std::string res;
std::vector<MigrateInfo> delList;
std::vector<MigrateInfo> updateList;
try
{
sqlite3x::sqlite3_connection db(szItemDb.c_str());
getListOfBadMcfPaths(db, delList, updateList);
}
catch (std::exception &)
{
}
}
void MCFManager::getListOfBadMcfPaths(sqlite3x::sqlite3_connection &db, std::vector<MigrateInfo> &delList, std::vector<MigrateInfo> &updateList)
{
try
{
sqlite3x::sqlite3_command cmd(db, "SELECT * FROM mcfitem");
sqlite3x::sqlite3_reader reader = cmd.executereader();
@ -135,7 +144,14 @@ void MCFManager::migrateOldFiles()
mi.id = DesuraId(reader.getint64(0));
mi.build = MCFBuild::BuildFromInt(reader.getint(1));
mi.branch = MCFBranch::BranchFromInt(reader.getint(3));
gcString newPath = generatePath(mi.id, mi.branch, mi.build, false);
if (newPath == path)
continue;
mi.path = path;
mi.newPath = newPath;
uint32 flags = reader.getint(4);
@ -149,6 +165,18 @@ void MCFManager::migrateOldFiles()
catch (std::exception &)
{
}
}
void MCFManager::migrateOldFiles()
{
gcString szItemDb = getMcfDb(m_szAppDataPath.c_str());
std::string res;
std::vector<MigrateInfo> delList;
std::vector<MigrateInfo> updateList;
getListOfBadMcfPaths(szItemDb, delList, updateList);
for (size_t x=0; x<delList.size(); x++)
{
@ -158,9 +186,9 @@ void MCFManager::migrateOldFiles()
for (size_t x=0; x<updateList.size(); x++)
{
gcString newPath = generatePath(updateList[x].id, updateList[x].branch, updateList[x].build, false);
gcString newPath = updateList[x].newPath;
UTIL::FS::recMakeFolder(newPath.c_str());
UTIL::FS::recMakeFolder(UTIL::FS::PathWithFile(newPath));
UTIL::FS::moveFile(updateList[x].path.c_str(), newPath.c_str());
delMcfPath(updateList[x].id, updateList[x].branch, updateList[x].build);
@ -515,21 +543,12 @@ void MCFManager::getMCFFiles(std::vector<mcfInfo*> &validFiles, DesuraId id)
}
}
gcString MCFManager::generatePath(DesuraId id, MCFBranch branch, MCFBuild build, bool isUnAuthed)
{
#ifdef WIN32
gcString fileName("{2}b{0}_m{1}.mcf", branch, build, DIRS_STR);
#else
gcString fileName("b{0}_m{1}.mcf", branch, build);
#endif
if (isUnAuthed)
#ifdef WIN32
fileName = gcString("{2}b{0}_m{1}_unauthed.mcf", branch, build, DIRS_STR);
#else
fileName = gcString("b{0}_m{1}_unauthed.mcf", branch, build);
#endif
return gcString("{0}{1}{2}", m_szMCFSavePath, DIRS_STR, id.getFolderPathExtension(fileName.c_str()));
}
@ -579,6 +598,7 @@ void MCFManager::validateMcf(const char* mcf)
DesuraId id = mcfHead->getDesuraId();
MCFBuild build = mcfHead->getBuild();
MCFBranch branch = mcfHead->getBranch();
bool isPatch = strstr(mcf, "patch")?true:false;
bool isUnAuthed = strstr(mcf, "unauth")?true:false;
@ -593,7 +613,8 @@ void MCFManager::validateMcf(const char* mcf)
cmd.bind(1, (long long int)id.toInt64());
cmd.bind(2, (int)build);
cmd.bind(3, UTIL::OS::getRelativePath(mcf));
cmd.bind(4, (int)flags);
cmd.bind(4, (int)branch);
cmd.bind(5, (int)flags);
cmd.executenonquery();
}
@ -608,3 +629,99 @@ gcString MCFManager::getMcfSavePath()
}
}
#ifdef WITH_GTEST
#include <gtest/gtest.h>
namespace UnitTest
{
class MCFManagerFixture : public ::testing::Test
{
public:
MCFManagerFixture()
: m_MCFManager("appdata", "mcfroot")
{
}
gcString generatePath(DesuraId id, MCFBranch branch, MCFBuild build, bool isUnAuthed)
{
return m_MCFManager.generatePath(id, branch, build, isUnAuthed);
}
void getListOfBadMcfPaths(sqlite3x::sqlite3_connection &db, std::vector<UserCore::MigrateInfo> &delList, std::vector<UserCore::MigrateInfo> &updateList)
{
m_MCFManager.getListOfBadMcfPaths(db, delList, updateList);
}
UserCore::MCFManager m_MCFManager;
};
TEST_F(MCFManagerFixture, generatePath_authed)
{
DesuraId id("1", "games");
gcString strPath = generatePath(id, MCFBranch::BranchFromInt(2), MCFBuild::BuildFromInt(3), false);
#ifdef WIN32
ASSERT_STREQ("mcfroot\\games\\1\\b2_m3.mcf", strPath.c_str());
#else
ASSERT_STREQ("mcfroot/games/1/b2_m3.mcf", strPath.c_str());
#endif
}
TEST_F(MCFManagerFixture, generatePath_unauthed)
{
DesuraId id("1", "games");
gcString strPath = generatePath(id, MCFBranch::BranchFromInt(2), MCFBuild::BuildFromInt(3), true);
#ifdef WIN32
ASSERT_STREQ("mcfroot\\games\\1\\b2_m3_unauthed.mcf", strPath.c_str());
#else
ASSERT_STREQ("mcfroot/games/1/b2_m3_unauthed.mcf", strPath.c_str());
#endif
}
TEST_F(MCFManagerFixture, getListOfBadMcfPaths)
{
sqlite3x::sqlite3_connection db(":memory:");
db.executenonquery(CREATE_MCFITEM);
auto insertItem = [&db](DesuraId id, gcString strPath, bool bUnAuthed) -> void
{
try
{
sqlite3x::sqlite3_command cmd(db, "INSERT INTO mcfitem VALUES (?,?,?,?,?);");
cmd.bind(1, (long long int)id.toInt64());
cmd.bind(2, 0);
cmd.bind(3, UTIL::FS::PathWithFile(strPath).getFullPath());
cmd.bind(4, 0);
cmd.bind(5, bUnAuthed ? FLAG_UNAUTHED : 0);
cmd.executenonquery();
}
catch (std::exception &)
{
}
};
insertItem(DesuraId("51", "games"), "appdata\\test1.mcf", true);
insertItem(DesuraId("62", "games"), "appdata\\test2.mcf", false);
insertItem(DesuraId("73", "games"), "appdata\\test3.mcf", false);
insertItem(DesuraId("84", "games"), "mcfroot\\test4.mcf", false);
std::vector<UserCore::MigrateInfo> delList;
std::vector<UserCore::MigrateInfo> updateList;
getListOfBadMcfPaths(db, delList, updateList);
ASSERT_EQ(1, delList.size());
ASSERT_EQ(DesuraId("51", "games"), delList[0].id);
ASSERT_EQ(2, updateList.size());
ASSERT_EQ(DesuraId("62", "games"), updateList[0].id);
ASSERT_EQ(DesuraId("73", "games"), updateList[1].id);
}
}
#endif

View File

@ -37,6 +37,16 @@ public:
MCFBranch branch;
};
namespace UnitTest
{
class MCFManagerFixture;
}
namespace sqlite3x
{
class sqlite3_connection;
}
namespace UserCore
{
@ -53,6 +63,8 @@ public:
MCFBranch branch;
};
class MigrateInfo;
class MCFManager
{
public:
@ -73,7 +85,11 @@ public:
gcString getMcfSavePath();
void init();
protected:
friend class UnitTest::MCFManagerFixture;
void properDelMcfBackup(DesuraId gid, DesuraId mid);
void scanForMcf();
@ -88,9 +104,12 @@ protected:
void migrateOldFiles();
void getListOfBadMcfPaths(const gcString &szItemDb, std::vector<MigrateInfo> &delList, std::vector<MigrateInfo> &updateList);
void getListOfBadMcfPaths(sqlite3x::sqlite3_connection &db, std::vector<MigrateInfo> &delList, std::vector<MigrateInfo> &updateList);
private:
gcString m_szAppDataPath;
gcString m_szMCFSavePath;
const gcString m_szAppDataPath;
const gcString m_szMCFSavePath;
};

View File

@ -162,7 +162,7 @@ std::wstring getCachePath(std::wstring extra)
return UTIL::STRING::toWStr(cachePath) + extra;
#else
return getAppDataPath(std::wstring(L"cache/") + extra);
return getAppDataPath(std::wstring(L"cache\\") + extra);
#endif
}