ryujinx/Ryujinx.HLE/HOS/Services/Mii/DatabaseImpl.cs

328 lines
9.1 KiB
C#
Raw Normal View History

Update to LibHac 0.13.1 (#2328) Update the LibHac dependency to version 0.13.1. This brings a ton of improvements and changes such as: - Refactor `FsSrv` to match the official refactoring done in FS. - Change how the `Horizon` and `HorizonClient` classes are handled. Each client created represents a different process with its own process ID and client state. - Add FS access control to handle permissions for FS service method calls. - Add FS program registry to keep track of the program ID, location and permissions of each process. - Add FS program index map info manager to track the program IDs and indexes of multi-application programs. - Add all FS IPC interfaces. - Rewrite `Fs.Fsa` code to be more accurate. - Rewrite a lot of `FsSrv` code to be more accurate. - Extend directory save data to store `SaveDataExtraData` - Extend directory save data to lock the save directory to allow only one accessor at a time. - Improve waiting and retrying when encountering access issues in `LocalFileSystem` and `DirectorySaveDataFileSystem`. - More `IFileSystemProxy` methods should work now. - Probably a bunch more stuff. On the Ryujinx side: - Forward most `IFileSystemProxy` methods to LibHac. - Register programs and program index map info when launching an application. - Remove hacks and workarounds for missing LibHac functionality. - Recreate missing save data extra data found on emulator startup. - Create system save data that wasn't indexed correctly on an older LibHac version. `FsSrv` now enforces access control for each process. When a process tries to open a save data file system, FS reads the save's extra data to determine who the save owner is and if the caller has permission to open the save data. Previously-created save data did not have extra data created when the save was created. With access control checks in place, this means that processes with no permissions (most games) wouldn't be able to access their own save data. The extra data can be partially created from data in the save data indexer, which should be enough for access control purposes.
2021-07-13 08:19:28 +00:00
using LibHac;
using Ryujinx.HLE.HOS.Services.Mii.Types;
using System;
namespace Ryujinx.HLE.HOS.Services.Mii
{
class DatabaseImpl
{
private static DatabaseImpl _instance;
public static DatabaseImpl Instance
{
get
{
if (_instance == null)
{
_instance = new DatabaseImpl();
}
return _instance;
}
}
private UtilityImpl _utilityImpl;
private MiiDatabaseManager _miiDatabase;
private bool _isBroken;
public DatabaseImpl()
{
_utilityImpl = new UtilityImpl();
_miiDatabase = new MiiDatabaseManager();
}
public bool IsUpdated(DatabaseSessionMetadata metadata, SourceFlag flag)
{
if (flag.HasFlag(SourceFlag.Database))
{
return _miiDatabase.IsUpdated(metadata);
}
return false;
}
public bool IsBrokenDatabaseWithClearFlag()
{
bool result = _isBroken;
if (_isBroken)
{
_isBroken = false;
Format(new DatabaseSessionMetadata(0, new SpecialMiiKeyCode()));
}
return result;
}
public bool IsFullDatabase()
{
return _miiDatabase.IsFullDatabase();
}
private ResultCode GetDefault<T>(SourceFlag flag, ref int count, Span<T> elements) where T : struct, IElement
{
if (!flag.HasFlag(SourceFlag.Default))
{
return ResultCode.Success;
}
for (uint i = 0; i < DefaultMii.TableLength; i++)
{
if (count >= elements.Length)
{
return ResultCode.BufferTooSmall;
}
elements[count] = default;
elements[count].SetFromStoreData(StoreData.BuildDefault(_utilityImpl, i));
elements[count].SetSource(Source.Default);
count++;
}
return ResultCode.Success;
}
public ResultCode UpdateLatest<T>(DatabaseSessionMetadata metadata, IStoredData<T> oldMiiData, SourceFlag flag, IStoredData<T> newMiiData) where T : unmanaged
{
if (!flag.HasFlag(SourceFlag.Database))
{
return ResultCode.NotFound;
}
if (metadata.IsInterfaceVersionSupported(1) && !oldMiiData.IsValid())
{
return oldMiiData.InvalidData;
}
ResultCode result = _miiDatabase.FindIndex(metadata, out int index, oldMiiData.CreateId);
if (result == ResultCode.Success)
{
_miiDatabase.Get(metadata, index, out StoreData storeData);
if (storeData.Type != oldMiiData.Type)
{
return ResultCode.NotFound;
}
newMiiData.SetFromStoreData(storeData);
if (oldMiiData == newMiiData)
{
return ResultCode.NotUpdated;
}
}
return result;
}
public ResultCode Get<T>(DatabaseSessionMetadata metadata, SourceFlag flag, out int count, Span<T> elements) where T : struct, IElement
{
count = 0;
if (!flag.HasFlag(SourceFlag.Database))
{
return GetDefault(flag, ref count, elements);
}
int databaseCount = _miiDatabase.GetCount(metadata);
for (int i = 0; i < databaseCount; i++)
{
if (count >= elements.Length)
{
return ResultCode.BufferTooSmall;
}
_miiDatabase.Get(metadata, i, out StoreData storeData);
elements[count] = default;
elements[count].SetFromStoreData(storeData);
elements[count].SetSource(Source.Database);
count++;
}
return GetDefault(flag, ref count, elements);
}
Update to LibHac 0.13.1 (#2328) Update the LibHac dependency to version 0.13.1. This brings a ton of improvements and changes such as: - Refactor `FsSrv` to match the official refactoring done in FS. - Change how the `Horizon` and `HorizonClient` classes are handled. Each client created represents a different process with its own process ID and client state. - Add FS access control to handle permissions for FS service method calls. - Add FS program registry to keep track of the program ID, location and permissions of each process. - Add FS program index map info manager to track the program IDs and indexes of multi-application programs. - Add all FS IPC interfaces. - Rewrite `Fs.Fsa` code to be more accurate. - Rewrite a lot of `FsSrv` code to be more accurate. - Extend directory save data to store `SaveDataExtraData` - Extend directory save data to lock the save directory to allow only one accessor at a time. - Improve waiting and retrying when encountering access issues in `LocalFileSystem` and `DirectorySaveDataFileSystem`. - More `IFileSystemProxy` methods should work now. - Probably a bunch more stuff. On the Ryujinx side: - Forward most `IFileSystemProxy` methods to LibHac. - Register programs and program index map info when launching an application. - Remove hacks and workarounds for missing LibHac functionality. - Recreate missing save data extra data found on emulator startup. - Create system save data that wasn't indexed correctly on an older LibHac version. `FsSrv` now enforces access control for each process. When a process tries to open a save data file system, FS reads the save's extra data to determine who the save owner is and if the caller has permission to open the save data. Previously-created save data did not have extra data created when the save was created. With access control checks in place, this means that processes with no permissions (most games) wouldn't be able to access their own save data. The extra data can be partially created from data in the save data indexer, which should be enough for access control purposes.
2021-07-13 08:19:28 +00:00
public ResultCode InitializeDatabase(HorizonClient horizonClient)
{
Update to LibHac 0.13.1 (#2328) Update the LibHac dependency to version 0.13.1. This brings a ton of improvements and changes such as: - Refactor `FsSrv` to match the official refactoring done in FS. - Change how the `Horizon` and `HorizonClient` classes are handled. Each client created represents a different process with its own process ID and client state. - Add FS access control to handle permissions for FS service method calls. - Add FS program registry to keep track of the program ID, location and permissions of each process. - Add FS program index map info manager to track the program IDs and indexes of multi-application programs. - Add all FS IPC interfaces. - Rewrite `Fs.Fsa` code to be more accurate. - Rewrite a lot of `FsSrv` code to be more accurate. - Extend directory save data to store `SaveDataExtraData` - Extend directory save data to lock the save directory to allow only one accessor at a time. - Improve waiting and retrying when encountering access issues in `LocalFileSystem` and `DirectorySaveDataFileSystem`. - More `IFileSystemProxy` methods should work now. - Probably a bunch more stuff. On the Ryujinx side: - Forward most `IFileSystemProxy` methods to LibHac. - Register programs and program index map info when launching an application. - Remove hacks and workarounds for missing LibHac functionality. - Recreate missing save data extra data found on emulator startup. - Create system save data that wasn't indexed correctly on an older LibHac version. `FsSrv` now enforces access control for each process. When a process tries to open a save data file system, FS reads the save's extra data to determine who the save owner is and if the caller has permission to open the save data. Previously-created save data did not have extra data created when the save was created. With access control checks in place, this means that processes with no permissions (most games) wouldn't be able to access their own save data. The extra data can be partially created from data in the save data indexer, which should be enough for access control purposes.
2021-07-13 08:19:28 +00:00
_miiDatabase.InitializeDatabase(horizonClient);
_miiDatabase.LoadFromFile(out _isBroken);
// Nintendo ignore any error code from before
return ResultCode.Success;
}
public DatabaseSessionMetadata CreateSessionMetadata(SpecialMiiKeyCode miiKeyCode)
{
return _miiDatabase.CreateSessionMetadata(miiKeyCode);
}
public void SetInterfaceVersion(DatabaseSessionMetadata metadata, uint interfaceVersion)
{
_miiDatabase.SetInterfaceVersion(metadata, interfaceVersion);
}
public void Format(DatabaseSessionMetadata metadata)
{
_miiDatabase.FormatDatabase(metadata);
_miiDatabase.SaveDatabase();
}
public ResultCode DestroyFile(DatabaseSessionMetadata metadata)
{
_isBroken = true;
return _miiDatabase.DestroyFile(metadata);
}
public void BuildDefault(uint index, out CharInfo charInfo)
{
StoreData storeData = StoreData.BuildDefault(_utilityImpl, index);
charInfo = default;
charInfo.SetFromStoreData(storeData);
}
public void BuildRandom(Age age, Gender gender, Race race, out CharInfo charInfo)
{
StoreData storeData = StoreData.BuildRandom(_utilityImpl, age, gender, race);
charInfo = default;
charInfo.SetFromStoreData(storeData);
}
public ResultCode DeleteFile()
{
return _miiDatabase.DeleteFile();
}
public ResultCode ConvertCoreDataToCharInfo(CoreData coreData, out CharInfo charInfo)
{
charInfo = new CharInfo();
if (!coreData.IsValid())
{
return ResultCode.InvalidCoreData;
}
StoreData storeData = StoreData.BuildFromCoreData(_utilityImpl, coreData);
if (!storeData.CoreData.Nickname.IsValidForFontRegion(storeData.CoreData.FontRegion))
{
storeData.CoreData.Nickname = Nickname.Question;
storeData.UpdateCrc();
}
charInfo.SetFromStoreData(storeData);
return ResultCode.Success;
}
public int FindIndex(CreateId createId, bool isSpecial)
{
if (_miiDatabase.FindIndex(out int index, createId, isSpecial) == ResultCode.Success)
{
return index;
}
return -1;
}
public uint GetCount(DatabaseSessionMetadata metadata, SourceFlag flag)
{
int count = 0;
if (flag.HasFlag(SourceFlag.Default))
{
count += DefaultMii.TableLength;
}
if (flag.HasFlag(SourceFlag.Database))
{
count += _miiDatabase.GetCount(metadata);
}
return (uint)count;
}
public ResultCode Move(DatabaseSessionMetadata metadata, int index, CreateId createId)
{
ResultCode result = _miiDatabase.Move(metadata, index, createId);
if (result == ResultCode.Success)
{
result = _miiDatabase.SaveDatabase();
}
return result;
}
public ResultCode Delete(DatabaseSessionMetadata metadata, CreateId createId)
{
ResultCode result = _miiDatabase.Delete(metadata, createId);
if (result == ResultCode.Success)
{
result = _miiDatabase.SaveDatabase();
}
return result;
}
public ResultCode AddOrReplace(DatabaseSessionMetadata metadata, StoreData storeData)
{
ResultCode result = _miiDatabase.AddOrReplace(metadata, storeData);
if (result == ResultCode.Success)
{
result = _miiDatabase.SaveDatabase();
}
return result;
}
public ResultCode ConvertCharInfoToCoreData(CharInfo charInfo, out CoreData coreData)
{
coreData = new CoreData();
if (charInfo.IsValid())
{
return ResultCode.InvalidCharInfo;
}
coreData.SetFromCharInfo(charInfo);
if (!coreData.Nickname.IsValidForFontRegion(coreData.FontRegion))
{
coreData.Nickname = Nickname.Question;
}
return ResultCode.Success;
}
public ResultCode GetIndex(DatabaseSessionMetadata metadata, CharInfo charInfo, out int index)
{
if (!charInfo.IsValid())
{
index = -1;
return ResultCode.InvalidCharInfo;
}
if (_miiDatabase.FindIndex(out index, charInfo.CreateId, metadata.MiiKeyCode.IsEnabledSpecialMii()) != ResultCode.Success)
{
return ResultCode.NotFound;
}
return ResultCode.Success;
}
}
}