dolphin/Source/Core/Common/NandPaths.cpp
Léo Lam 64101137cd Remove pre-generated SYSCONF
Dolphin is able to generate one with all correct default settings, so
we don't need to ship with a pre-generated SYSCONF and worry about
syncing default settings.

Additionally, this commit changes SysConf to work with session SYSCONFs
so that Dolphin is able to generate a default one even for Movie/TAS.
Which SYSCONF needs to be touched is explicitly specified to avoid
confusion about which file SysConf is managing.

(Another notable change is that the Wii root functions are moved into
Core to prevent Common from depending on Core.)
2017-01-12 21:30:11 +01:00

141 lines
4.4 KiB
C++

// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <unordered_set>
#include <utility>
#include "Common/CommonFuncs.h"
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
#include "Common/StringUtil.h"
namespace Common
{
std::string RootUserPath(FromWhichRoot from)
{
int idx = from == FROM_CONFIGURED_ROOT ? D_WIIROOT_IDX : D_SESSION_WIIROOT_IDX;
return File::GetUserPath(idx);
}
std::string GetTicketFileName(u64 _titleID, FromWhichRoot from)
{
return StringFromFormat("%s/ticket/%08x/%08x.tik", RootUserPath(from).c_str(),
(u32)(_titleID >> 32), (u32)_titleID);
}
std::string GetTitleDataPath(u64 _titleID, FromWhichRoot from)
{
return StringFromFormat("%s/title/%08x/%08x/data/", RootUserPath(from).c_str(),
(u32)(_titleID >> 32), (u32)_titleID);
}
std::string GetTMDFileName(u64 _titleID, FromWhichRoot from)
{
return GetTitleContentPath(_titleID, from) + "title.tmd";
}
std::string GetTitleContentPath(u64 _titleID, FromWhichRoot from)
{
return StringFromFormat("%s/title/%08x/%08x/content/", RootUserPath(from).c_str(),
(u32)(_titleID >> 32), (u32)_titleID);
}
bool CheckTitleTMD(u64 _titleID, FromWhichRoot from)
{
const std::string TitlePath = GetTMDFileName(_titleID, from);
if (File::Exists(TitlePath))
{
File::IOFile pTMDFile(TitlePath, "rb");
u64 TitleID = 0;
pTMDFile.Seek(0x18C, SEEK_SET);
if (pTMDFile.ReadArray(&TitleID, 1) && _titleID == Common::swap64(TitleID))
return true;
}
INFO_LOG(DISCIO, "Invalid or no tmd for title %08x %08x", (u32)(_titleID >> 32),
(u32)(_titleID & 0xFFFFFFFF));
return false;
}
bool CheckTitleTIK(u64 _titleID, FromWhichRoot from)
{
const std::string ticketFileName = Common::GetTicketFileName(_titleID, from);
if (File::Exists(ticketFileName))
{
File::IOFile pTIKFile(ticketFileName, "rb");
u64 TitleID = 0;
pTIKFile.Seek(0x1dC, SEEK_SET);
if (pTIKFile.ReadArray(&TitleID, 1) && _titleID == Common::swap64(TitleID))
return true;
}
INFO_LOG(DISCIO, "Invalid or no tik for title %08x %08x", (u32)(_titleID >> 32),
(u32)(_titleID & 0xFFFFFFFF));
return false;
}
std::string EscapeFileName(const std::string& filename)
{
// Prevent paths from containing special names like ., .., ..., ...., and so on
if (std::all_of(filename.begin(), filename.end(), [](char c) { return c == '.'; }))
return ReplaceAll(filename, ".", "__2e__");
// Escape all double underscores since we will use double underscores for our escape sequences
std::string filename_with_escaped_double_underscores = ReplaceAll(filename, "__", "__5f____5f__");
// Escape all other characters that need to be escaped
static const std::unordered_set<char> chars_to_replace = {'\"', '*', '/', ':', '<',
'>', '?', '\\', '|', '\x7f'};
std::string result;
result.reserve(filename_with_escaped_double_underscores.size());
for (char c : filename_with_escaped_double_underscores)
{
if ((c >= 0 && c <= 0x1F) || chars_to_replace.find(c) != chars_to_replace.end())
result.append(StringFromFormat("__%02x__", c));
else
result.push_back(c);
}
return result;
}
std::string EscapePath(const std::string& path)
{
std::vector<std::string> split_strings;
SplitString(path, '/', split_strings);
std::vector<std::string> escaped_split_strings;
escaped_split_strings.reserve(split_strings.size());
for (const std::string& split_string : split_strings)
escaped_split_strings.push_back(EscapeFileName(split_string));
return JoinStrings(escaped_split_strings, "/");
}
std::string UnescapeFileName(const std::string& filename)
{
std::string result = filename;
size_t pos = 0;
// Replace escape sequences of the format "__3f__" with the ASCII
// character defined by the escape sequence's two hex digits.
while ((pos = result.find("__", pos)) != std::string::npos)
{
u32 character;
if (pos + 6 <= result.size() && result[pos + 4] == '_' && result[pos + 5] == '_')
if (AsciiToHex(result.substr(pos + 2, 2), character))
result.replace(pos, 6, {static_cast<char>(character)});
++pos;
}
return result;
}
}