Merge pull request #1556 from comex/project-moration

Rudimentary version of Wii IPC determinism.  Ported from my old udpnet branch.
This commit is contained in:
comex
2015-06-02 18:38:19 -04:00
43 changed files with 417 additions and 483 deletions

View File

@ -2,101 +2,60 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#if !__clang__ && __GNUC__ == 4 && __GNUC_MINOR__ < 9
#error <regex> is broken in GCC < 4.9; please upgrade
#endif
#include <algorithm>
#include <cstring>
#include <functional>
#include <regex>
#include "Common/CommonPaths.h"
#include "Common/FileSearch.h"
#include "Common/StringUtil.h"
#include "Common/FileUtil.h"
#ifndef _WIN32
#include <dirent.h>
#else
#include <windows.h>
#endif
CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, const CFileSearch::XStringVector& _rDirectories)
static std::vector<std::string> FileSearchWithTest(const std::vector<std::string>& directories, bool recursive, std::function<bool(const File::FSTEntry &)> callback)
{
// Reverse the loop order for speed?
for (auto& _rSearchString : _rSearchStrings)
std::vector<std::string> result;
for (const std::string& directory : directories)
{
for (auto& _rDirectory : _rDirectories)
{
FindFiles(_rSearchString, _rDirectory);
}
File::FSTEntry top = File::ScanDirectoryTree(directory, recursive);
std::function<void(File::FSTEntry&)> DoEntry;
DoEntry = [&](File::FSTEntry& entry) {
if (callback(entry))
result.push_back(entry.physicalName);
for (auto& child : entry.children)
DoEntry(child);
};
DoEntry(top);
}
// remove duplicates
std::sort(result.begin(), result.end());
result.erase(std::unique(result.begin(), result.end()), result.end());
return result;
}
void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath)
std::vector<std::string> DoFileSearch(const std::vector<std::string>& globs, const std::vector<std::string>& directories, bool recursive)
{
std::string GCMSearchPath;
BuildCompleteFilename(GCMSearchPath, _strPath, _searchString);
#ifdef _WIN32
WIN32_FIND_DATA findData;
HANDLE FindFirst = FindFirstFile(UTF8ToTStr(GCMSearchPath).c_str(), &findData);
if (FindFirst != INVALID_HANDLE_VALUE)
std::string regex_str = "^(";
for (const auto& str : globs)
{
bool bkeepLooping = true;
while (bkeepLooping)
{
if (findData.cFileName[0] != '.')
{
std::string strFilename;
BuildCompleteFilename(strFilename, _strPath, TStrToUTF8(findData.cFileName));
m_FileNames.push_back(strFilename);
}
bkeepLooping = FindNextFile(FindFirst, &findData) ? true : false;
}
if (regex_str.size() != 2)
regex_str += "|";
// convert glob to regex
regex_str += std::regex_replace(std::regex_replace(str, std::regex("\\."), "\\."), std::regex("\\*"), ".*");
}
FindClose(FindFirst);
#else
// TODO: super lame/broken
auto end_match(_searchString);
// assuming we have a "*.blah"-like pattern
if (!end_match.empty() && end_match[0] == '*')
end_match.erase(0, 1);
// ugly
if (end_match == ".*")
end_match.clear();
DIR* dir = opendir(_strPath.c_str());
if (!dir)
return;
while (auto const dp = readdir(dir))
{
std::string found(dp->d_name);
if ((found != ".") && (found != "..") &&
(found.size() >= end_match.size()) &&
std::equal(end_match.rbegin(), end_match.rend(), found.rbegin()))
{
std::string full_name;
if (_strPath.c_str()[_strPath.size() - 1] == DIR_SEP_CHR)
full_name = _strPath + found;
else
full_name = _strPath + DIR_SEP + found;
m_FileNames.push_back(full_name);
}
}
closedir(dir);
#endif
regex_str += ")$";
std::regex regex(regex_str);
return FileSearchWithTest(directories, recursive, [&](const File::FSTEntry& entry) {
return std::regex_match(entry.virtualName, regex);
});
}
const CFileSearch::XStringVector& CFileSearch::GetFileNames() const
std::vector<std::string> FindSubdirectories(const std::vector<std::string>& directories, bool recursive)
{
return m_FileNames;
return FileSearchWithTest(directories, true, [&](const File::FSTEntry& entry) {
return entry.isDirectory;
});
}

View File

@ -7,18 +7,5 @@
#include <string>
#include <vector>
class CFileSearch
{
public:
typedef std::vector<std::string>XStringVector;
CFileSearch(const XStringVector& _rSearchStrings, const XStringVector& _rDirectories);
const XStringVector& GetFileNames() const;
private:
void FindFiles(const std::string& _searchString, const std::string& _strPath);
XStringVector m_FileNames;
};
std::vector<std::string> DoFileSearch(const std::vector<std::string>& globs, const std::vector<std::string>& directories, bool recursive = false);
std::vector<std::string> FindSubdirectories(const std::vector<std::string>& directories, bool recursive);

View File

@ -20,6 +20,7 @@
#include <commdlg.h> // for GetSaveFileName
#include <direct.h> // getcwd
#include <io.h>
#include <objbase.h> // guid stuff
#include <shellapi.h>
#include <windows.h>
#else
@ -453,11 +454,14 @@ bool CreateEmptyFile(const std::string &filename)
// Scans the directory tree gets, starting from _Directory and adds the
// results into parentEntry. Returns the number of files+directories found
u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry)
FSTEntry ScanDirectoryTree(const std::string &directory, bool recursive)
{
INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory.c_str());
// How many files + directories we found
u32 foundEntries = 0;
FSTEntry parent_entry;
parent_entry.physicalName = directory;
parent_entry.isDirectory = true;
parent_entry.size = 0;
#ifdef _WIN32
// Find the first file in the directory.
WIN32_FIND_DATA ffd;
@ -466,59 +470,55 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry)
if (hFind == INVALID_HANDLE_VALUE)
{
FindClose(hFind);
return foundEntries;
return parent_entry;
}
// Windows loop
do
{
FSTEntry entry;
const std::string virtualName(TStrToUTF8(ffd.cFileName));
const std::string virtual_name(TStrToUTF8(ffd.cFileName));
#else
struct dirent dirent, *result = nullptr;
DIR *dirp = opendir(directory.c_str());
if (!dirp)
return 0;
return parent_entry;
// non Windows loop
while (!readdir_r(dirp, &dirent, &result) && result)
{
FSTEntry entry;
const std::string virtualName(result->d_name);
#endif
// check for "." and ".."
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
((virtualName[0] == '.') && (virtualName[1] == '.') &&
(virtualName[2] == '\0')))
continue;
entry.virtualName = virtualName;
entry.physicalName = directory;
entry.physicalName += DIR_SEP + entry.virtualName;
if (IsDirectory(entry.physicalName))
// non Windows loop
while (!readdir_r(dirp, &dirent, &result) && result)
{
entry.isDirectory = true;
// is a directory, lets go inside
entry.size = ScanDirectoryTree(entry.physicalName, entry);
foundEntries += (u32)entry.size;
}
else
{ // is a file
entry.isDirectory = false;
entry.size = GetSize(entry.physicalName.c_str());
}
++foundEntries;
// Push into the tree
parentEntry.children.push_back(entry);
#ifdef _WIN32
} while (FindNextFile(hFind, &ffd) != 0);
const std::string virtual_name(result->d_name);
#endif
if (virtual_name == "." || virtual_name == "..")
continue;
auto physical_name = directory + DIR_SEP + virtual_name;
FSTEntry entry;
entry.isDirectory = IsDirectory(physical_name);
if (entry.isDirectory)
{
if (recursive)
entry = ScanDirectoryTree(physical_name, true);
else
entry.size = 0;
}
else
{
entry.size = GetSize(physical_name);
}
entry.virtualName = virtual_name;
entry.physicalName = physical_name;
++parent_entry.size;
// Push into the tree
parent_entry.children.push_back(entry);
#ifdef _WIN32
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
#else
}
closedir(dirp);
#endif
// Return number of entries found.
return foundEntries;
return parent_entry;
}
@ -628,14 +628,11 @@ void CopyDir(const std::string &source_path, const std::string &dest_path)
if (virtualName == "." || virtualName == "..")
continue;
std::string source, dest;
source = source_path + virtualName;
dest = dest_path + virtualName;
std::string source = source_path + DIR_SEP + virtualName;
std::string dest = dest_path + DIR_SEP + virtualName;
if (IsDirectory(source))
{
source += '/';
dest += '/';
if (!File::Exists(dest)) File::CreateFullPath(dest);
if (!File::Exists(dest)) File::CreateFullPath(dest + DIR_SEP);
CopyDir(source, dest);
}
else if (!File::Exists(dest)) File::Copy(source, dest);
@ -670,6 +667,32 @@ bool SetCurrentDir(const std::string &directory)
return __chdir(directory.c_str()) == 0;
}
std::string CreateTempDir()
{
#ifdef _WIN32
TCHAR temp[MAX_PATH];
if (!GetTempPath(MAX_PATH, temp))
return "";
GUID guid;
CoCreateGuid(&guid);
TCHAR tguid[40];
StringFromGUID2(guid, tguid, 39);
tguid[39] = 0;
std::string dir = TStrToUTF8(temp) + "/" + TStrToUTF8(tguid);
if (!CreateDir(dir))
return "";
dir = ReplaceAll(dir, "\\", DIR_SEP);
return dir;
#else
const char* base = getenv("TMPDIR") ?: "/tmp";
std::string path = std::string(base) + "/DolphinWii.XXXXXX";
if (!mkdtemp(&path[0]))
return "";
return path;
#endif
}
std::string GetTempFilenameForAtomicWrite(const std::string &path)
{
std::string abs = path;
@ -738,17 +761,9 @@ static void RebuildUserDirectories(unsigned int dir_index)
{
switch (dir_index)
{
case D_WIIROOT_IDX:
s_user_paths[D_WIIUSER_IDX] = s_user_paths[D_WIIROOT_IDX] + DIR_SEP;
s_user_paths[D_WIISYSCONF_IDX] = s_user_paths[D_WIIUSER_IDX] + WII_SYSCONF_DIR + DIR_SEP;
s_user_paths[D_WIIWC24_IDX] = s_user_paths[D_WIIUSER_IDX] + WII_WC24CONF_DIR DIR_SEP;
s_user_paths[F_WIISYSCONF_IDX] = s_user_paths[D_WIISYSCONF_IDX] + WII_SYSCONF;
break;
case D_USER_IDX:
s_user_paths[D_GCUSER_IDX] = s_user_paths[D_USER_IDX] + GC_USER_DIR DIR_SEP;
s_user_paths[D_WIIROOT_IDX] = s_user_paths[D_USER_IDX] + WII_USER_DIR;
s_user_paths[D_WIIUSER_IDX] = s_user_paths[D_WIIROOT_IDX] + DIR_SEP;
s_user_paths[D_CONFIG_IDX] = s_user_paths[D_USER_IDX] + CONFIG_DIR DIR_SEP;
s_user_paths[D_GAMESETTINGS_IDX] = s_user_paths[D_USER_IDX] + GAMESETTINGS_DIR DIR_SEP;
s_user_paths[D_MAPS_IDX] = s_user_paths[D_USER_IDX] + MAPS_DIR DIR_SEP;
@ -766,14 +781,11 @@ static void RebuildUserDirectories(unsigned int dir_index)
s_user_paths[D_DUMPDSP_IDX] = s_user_paths[D_DUMP_IDX] + DUMP_DSP_DIR DIR_SEP;
s_user_paths[D_LOGS_IDX] = s_user_paths[D_USER_IDX] + LOGS_DIR DIR_SEP;
s_user_paths[D_MAILLOGS_IDX] = s_user_paths[D_LOGS_IDX] + MAIL_LOGS_DIR DIR_SEP;
s_user_paths[D_WIISYSCONF_IDX] = s_user_paths[D_WIIUSER_IDX] + WII_SYSCONF_DIR DIR_SEP;
s_user_paths[D_WIIWC24_IDX] = s_user_paths[D_WIIUSER_IDX] + WII_WC24CONF_DIR DIR_SEP;
s_user_paths[D_THEMES_IDX] = s_user_paths[D_USER_IDX] + THEMES_DIR DIR_SEP;
s_user_paths[F_DOLPHINCONFIG_IDX] = s_user_paths[D_CONFIG_IDX] + DOLPHIN_CONFIG;
s_user_paths[F_DEBUGGERCONFIG_IDX] = s_user_paths[D_CONFIG_IDX] + DEBUGGER_CONFIG;
s_user_paths[F_LOGGERCONFIG_IDX] = s_user_paths[D_CONFIG_IDX] + LOGGER_CONFIG;
s_user_paths[F_MAINLOG_IDX] = s_user_paths[D_LOGS_IDX] + MAIN_LOG;
s_user_paths[F_WIISYSCONF_IDX] = s_user_paths[D_WIISYSCONF_IDX] + WII_SYSCONF;
s_user_paths[F_RAMDUMP_IDX] = s_user_paths[D_DUMP_IDX] + RAM_DUMP;
s_user_paths[F_ARAMDUMP_IDX] = s_user_paths[D_DUMP_IDX] + ARAM_DUMP;
s_user_paths[F_FAKEVMEMDUMP_IDX] = s_user_paths[D_DUMP_IDX] + FAKEVMEM_DUMP;

View File

@ -20,8 +20,8 @@
enum {
D_USER_IDX,
D_GCUSER_IDX,
D_WIIROOT_IDX,
D_WIIUSER_IDX,
D_WIIROOT_IDX, // always points to User/Wii or global user-configured directory
D_SESSION_WIIROOT_IDX, // may point to minimal temporary directory for determinism
D_CONFIG_IDX, // global settings
D_GAMESETTINGS_IDX, // user-specified settings which override both the global and the default settings (per game)
D_MAPS_IDX,
@ -39,14 +39,11 @@ enum {
D_LOAD_IDX,
D_LOGS_IDX,
D_MAILLOGS_IDX,
D_WIISYSCONF_IDX,
D_WIIWC24_IDX,
D_THEMES_IDX,
F_DOLPHINCONFIG_IDX,
F_DEBUGGERCONFIG_IDX,
F_LOGGERCONFIG_IDX,
F_MAINLOG_IDX,
F_WIISYSCONF_IDX,
F_RAMDUMP_IDX,
F_ARAMDUMP_IDX,
F_FAKEVMEMDUMP_IDX,
@ -107,9 +104,8 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename);
// creates an empty file filename, returns true on success
bool CreateEmptyFile(const std::string &filename);
// Scans the directory tree gets, starting from _Directory and adds the
// results into parentEntry. Returns the number of files+directories found
u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry);
// Recursive or non-recursive list of files under directory.
FSTEntry ScanDirectoryTree(const std::string &directory, bool recursive);
// deletes the given directory and anything under it. Returns true on success.
bool DeleteDirRecursively(const std::string &directory);
@ -123,6 +119,9 @@ void CopyDir(const std::string &source_path, const std::string &dest_path);
// Set the current directory to given directory
bool SetCurrentDir(const std::string &directory);
// Creates and returns the path to a new temporary directory.
std::string CreateTempDir();
// Get a filename that can hopefully be atomically renamed to the given path.
std::string GetTempFilenameForAtomicWrite(const std::string &path);

View File

@ -7,6 +7,7 @@
#include <string>
#include <utility>
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/NandPaths.h"
@ -15,17 +16,55 @@
namespace Common
{
static std::string s_temp_wii_root;
void InitializeWiiRoot(bool use_dummy)
{
ShutdownWiiRoot();
if (use_dummy)
{
s_temp_wii_root = File::CreateTempDir();
if (s_temp_wii_root.empty())
{
ERROR_LOG(WII_IPC_FILEIO, "Could not create temporary directory");
return;
}
File::CopyDir(File::GetSysDirectory() + WII_USER_DIR, s_temp_wii_root);
WARN_LOG(WII_IPC_FILEIO, "Using temporary directory %s for minimal Wii FS", s_temp_wii_root.c_str());
static bool s_registered;
if (!s_registered)
{
s_registered = true;
atexit(ShutdownWiiRoot);
}
File::SetUserPath(D_SESSION_WIIROOT_IDX, s_temp_wii_root);
}
else
{
File::SetUserPath(D_SESSION_WIIROOT_IDX, File::GetUserPath(D_WIIROOT_IDX));
}
}
void ShutdownWiiRoot()
{
if (!s_temp_wii_root.empty())
{
File::DeleteDirRecursively(s_temp_wii_root);
s_temp_wii_root.clear();
}
}
std::string GetTicketFileName(u64 _titleID)
{
return StringFromFormat("%sticket/%08x/%08x.tik",
File::GetUserPath(D_WIIUSER_IDX).c_str(),
return StringFromFormat("%s/ticket/%08x/%08x.tik",
File::GetUserPath(D_SESSION_WIIROOT_IDX).c_str(),
(u32)(_titleID >> 32), (u32)_titleID);
}
std::string GetTitleDataPath(u64 _titleID)
{
return StringFromFormat("%stitle/%08x/%08x/data/",
File::GetUserPath(D_WIIUSER_IDX).c_str(),
return StringFromFormat("%s/title/%08x/%08x/data/",
File::GetUserPath(D_SESSION_WIIROOT_IDX).c_str(),
(u32)(_titleID >> 32), (u32)_titleID);
}
@ -35,8 +74,8 @@ std::string GetTMDFileName(u64 _titleID)
}
std::string GetTitleContentPath(u64 _titleID)
{
return StringFromFormat("%stitle/%08x/%08x/content/",
File::GetUserPath(D_WIIUSER_IDX).c_str(),
return StringFromFormat("%s/title/%08x/%08x/content/",
File::GetUserPath(D_SESSION_WIIROOT_IDX).c_str(),
(u32)(_titleID >> 32), (u32)_titleID);
}
@ -89,8 +128,7 @@ void ReadReplacements(replace_v& replacements)
{
replacements.clear();
const std::string replace_fname = "/sys/replace";
std::string filename(File::GetUserPath(D_WIIROOT_IDX));
filename += replace_fname;
std::string filename = File::GetUserPath(D_SESSION_WIIROOT_IDX) + replace_fname;
if (!File::Exists(filename))
CreateReplacementFile(filename);

View File

@ -18,6 +18,9 @@ namespace Common
typedef std::pair<char, std::string> replace_t;
typedef std::vector<replace_t> replace_v;
void InitializeWiiRoot(bool use_temporary);
void ShutdownWiiRoot();
std::string GetTicketFileName(u64 _titleID);
std::string GetTMDFileName(u64 _titleID);
std::string GetTitleDataPath(u64 _titleID);

View File

@ -8,6 +8,7 @@
#include <string>
#include <vector>
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/SysConf.h"
@ -15,8 +16,7 @@
SysConf::SysConf()
: m_IsValid(false)
{
m_FilenameDefault = File::GetUserPath(F_WIISYSCONF_IDX);
m_IsValid = LoadFromFile(m_FilenameDefault);
UpdateLocation();
}
SysConf::~SysConf()
@ -38,6 +38,10 @@ void SysConf::Clear()
bool SysConf::LoadFromFile(const std::string& filename)
{
if (m_IsValid)
Clear();
m_IsValid = false;
// Basic check
if (!File::Exists(filename))
{
@ -67,6 +71,7 @@ bool SysConf::LoadFromFile(const std::string& filename)
if (LoadFromFileInternal(f.ReleaseHandle()))
{
m_Filename = filename;
m_IsValid = true;
return true;
}
}
@ -107,6 +112,7 @@ bool SysConf::LoadFromFileInternal(FILE *fh)
f.ReadArray(curEntry.name, curEntry.nameLength);
curEntry.name[curEntry.nameLength] = '\0';
// Get length of data
curEntry.data = nullptr;
curEntry.dataLength = 0;
switch (curEntry.type)
{
@ -362,6 +368,7 @@ void SysConf::GenerateSysConf()
g.WriteBytes("SCed", 4);
m_Filename = m_FilenameDefault;
m_IsValid = true;
}
bool SysConf::SaveToFile(const std::string& filename)
@ -409,17 +416,17 @@ void SysConf::UpdateLocation()
// Clear the old filename and set the default filename to the new user path
// So that it can be generated if the file does not exist in the new location
m_Filename.clear();
m_FilenameDefault = File::GetUserPath(F_WIISYSCONF_IDX);
// Note: We don't use the dummy Wii root here (if in use) because this is
// all tied up with the configuration code. In the future this should
// probably just be synced with the other settings.
m_FilenameDefault = File::GetUserPath(D_WIIROOT_IDX) + DIR_SEP WII_SYSCONF_DIR DIR_SEP WII_SYSCONF;
Reload();
}
bool SysConf::Reload()
{
if (m_IsValid)
Clear();
std::string& filename = m_Filename.empty() ? m_FilenameDefault : m_Filename;
m_IsValid = LoadFromFile(filename);
LoadFromFile(filename);
return m_IsValid;
}