Refactoring HOS folder structure (#771)

* Refactoring HOS folder structure

Refactoring HOS folder structure:

- Added some subfolders when needed (Following structure decided in private).
- Added some `Types` folders when needed.
- Little cleanup here and there.
- Add services placeholders for every HOS services (close #766 and #753).

* Remove Types namespaces
This commit is contained in:
Ac_K 2019-09-19 02:45:11 +02:00 committed by jduncanator
parent 4af3101b22
commit a0720b5681
393 changed files with 2540 additions and 1299 deletions

View file

@ -0,0 +1,144 @@
using LibHac;
using LibHac.Fs;
using LibHac.Fs.NcaUtils;
using Ryujinx.Common;
using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.Utilities;
using System.IO;
namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
{
static class FileSystemProxyHelper
{
public static ResultCode LoadSaveDataFileSystem(ServiceCtx context, bool readOnly, out IFileSystem loadedFileSystem)
{
loadedFileSystem = null;
SaveSpaceId saveSpaceId = (SaveSpaceId)context.RequestData.ReadInt64();
ulong titleId = context.RequestData.ReadUInt64();
UInt128 userId = context.RequestData.ReadStruct<UInt128>();
long saveId = context.RequestData.ReadInt64();
SaveDataType saveDataType = (SaveDataType)context.RequestData.ReadByte();
SaveInfo saveInfo = new SaveInfo(titleId, saveId, saveDataType, saveSpaceId, userId);
string savePath = context.Device.FileSystem.GetSavePath(context, saveInfo);
try
{
LocalFileSystem fileSystem = new LocalFileSystem(savePath);
LibHac.Fs.IFileSystem saveFileSystem = new DirectorySaveDataFileSystem(fileSystem);
if (readOnly)
{
saveFileSystem = new ReadOnlyFileSystem(saveFileSystem);
}
loadedFileSystem = new IFileSystem(saveFileSystem);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
public static ResultCode OpenNsp(ServiceCtx context, string pfsPath, out IFileSystem openedFileSystem)
{
openedFileSystem = null;
try
{
LocalStorage storage = new LocalStorage(pfsPath, FileAccess.Read, FileMode.Open);
PartitionFileSystem nsp = new PartitionFileSystem(storage);
ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
openedFileSystem = new IFileSystem(nsp);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
public static ResultCode OpenNcaFs(ServiceCtx context, string ncaPath, LibHac.Fs.IStorage ncaStorage, out IFileSystem openedFileSystem)
{
openedFileSystem = null;
try
{
Nca nca = new Nca(context.Device.System.KeySet, ncaStorage);
if (!nca.SectionExists(NcaSectionType.Data))
{
return ResultCode.PartitionNotFound;
}
LibHac.Fs.IFileSystem fileSystem = nca.OpenFileSystem(NcaSectionType.Data, context.Device.System.FsIntegrityCheckLevel);
openedFileSystem = new IFileSystem(fileSystem);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
public static ResultCode OpenFileSystemFromInternalFile(ServiceCtx context, string fullPath, out IFileSystem openedFileSystem)
{
openedFileSystem = null;
DirectoryInfo archivePath = new DirectoryInfo(fullPath).Parent;
while (string.IsNullOrWhiteSpace(archivePath.Extension))
{
archivePath = archivePath.Parent;
}
if (archivePath.Extension == ".nsp" && File.Exists(archivePath.FullName))
{
FileStream pfsFile = new FileStream(
archivePath.FullName.TrimEnd(Path.DirectorySeparatorChar),
FileMode.Open,
FileAccess.Read);
try
{
PartitionFileSystem nsp = new PartitionFileSystem(pfsFile.AsStorage());
ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\');
if (nsp.FileExists(filename))
{
return OpenNcaFs(context, fullPath, nsp.OpenFile(filename, OpenMode.Read).AsStorage(), out openedFileSystem);
}
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
}
return ResultCode.PathDoesNotExist;
}
public static void ImportTitleKeysFromNsp(LibHac.Fs.IFileSystem nsp, Keyset keySet)
{
foreach (DirectoryEntry ticketEntry in nsp.EnumerateEntries("*.tik"))
{
Ticket ticket = new Ticket(nsp.OpenFile(ticketEntry.FullPath, OpenMode.Read).AsStream());
if (!keySet.TitleKeys.ContainsKey(ticket.RightsId))
{
keySet.TitleKeys.Add(ticket.RightsId, ticket.GetTitleKey(keySet));
}
}
}
}
}

View file

@ -0,0 +1,84 @@
using LibHac;
using System.Collections.Generic;
using System.Text;
namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
{
class IDirectory : IpcService
{
private const int DirectoryEntrySize = 0x310;
private IEnumerator<LibHac.Fs.DirectoryEntry> _enumerator;
private LibHac.Fs.IDirectory _baseDirectory;
public IDirectory(LibHac.Fs.IDirectory directory)
{
_baseDirectory = directory;
_enumerator = directory.Read().GetEnumerator();
}
[Command(0)]
// Read() -> (u64 count, buffer<nn::fssrv::sf::IDirectoryEntry, 6, 0> entries)
public ResultCode Read(ServiceCtx context)
{
long bufferPosition = context.Request.ReceiveBuff[0].Position;
long bufferLen = context.Request.ReceiveBuff[0].Size;
int maxReadCount = (int)(bufferLen / DirectoryEntrySize);
int readCount = 0;
try
{
while (readCount < maxReadCount && _enumerator.MoveNext())
{
long position = bufferPosition + readCount * DirectoryEntrySize;
WriteDirectoryEntry(context, position, _enumerator.Current);
readCount++;
}
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
context.ResponseData.Write((long)readCount);
return ResultCode.Success;
}
private void WriteDirectoryEntry(ServiceCtx context, long position, LibHac.Fs.DirectoryEntry entry)
{
for (int offset = 0; offset < 0x300; offset += 8)
{
context.Memory.WriteInt64(position + offset, 0);
}
byte[] nameBuffer = Encoding.UTF8.GetBytes(entry.Name);
context.Memory.WriteBytes(position, nameBuffer);
context.Memory.WriteInt32(position + 0x300, (int)entry.Attributes);
context.Memory.WriteInt32(position + 0x304, (byte)entry.Type);
context.Memory.WriteInt64(position + 0x308, entry.Size);
}
[Command(1)]
// GetEntryCount() -> u64
public ResultCode GetEntryCount(ServiceCtx context)
{
try
{
context.ResponseData.Write((long)_baseDirectory.GetEntryCount());
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
}
}

View file

@ -0,0 +1,136 @@
using LibHac;
using LibHac.Fs;
using System;
namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
{
class IFile : IpcService, IDisposable
{
private LibHac.Fs.IFile _baseFile;
public IFile(LibHac.Fs.IFile baseFile)
{
_baseFile = baseFile;
}
[Command(0)]
// Read(u32 readOption, u64 offset, u64 size) -> (u64 out_size, buffer<u8, 0x46, 0> out_buf)
public ResultCode Read(ServiceCtx context)
{
long position = context.Request.ReceiveBuff[0].Position;
ReadOption readOption = (ReadOption)context.RequestData.ReadInt32();
context.RequestData.BaseStream.Position += 4;
long offset = context.RequestData.ReadInt64();
long size = context.RequestData.ReadInt64();
byte[] data = new byte[size];
int readSize;
try
{
readSize = _baseFile.Read(data, offset, readOption);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
context.Memory.WriteBytes(position, data);
context.ResponseData.Write((long)readSize);
return ResultCode.Success;
}
[Command(1)]
// Write(u32 writeOption, u64 offset, u64 size, buffer<u8, 0x45, 0>)
public ResultCode Write(ServiceCtx context)
{
long position = context.Request.SendBuff[0].Position;
WriteOption writeOption = (WriteOption)context.RequestData.ReadInt32();
context.RequestData.BaseStream.Position += 4;
long offset = context.RequestData.ReadInt64();
long size = context.RequestData.ReadInt64();
byte[] data = context.Memory.ReadBytes(position, size);
try
{
_baseFile.Write(data, offset, writeOption);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(2)]
// Flush()
public ResultCode Flush(ServiceCtx context)
{
try
{
_baseFile.Flush();
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(3)]
// SetSize(u64 size)
public ResultCode SetSize(ServiceCtx context)
{
try
{
long size = context.RequestData.ReadInt64();
_baseFile.SetSize(size);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(4)]
// GetSize() -> u64 fileSize
public ResultCode GetSize(ServiceCtx context)
{
try
{
context.ResponseData.Write(_baseFile.GetSize());
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_baseFile?.Dispose();
}
}
}
}

View file

@ -0,0 +1,324 @@
using LibHac;
using LibHac.Fs;
using static Ryujinx.HLE.Utilities.StringUtils;
namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
{
class IFileSystem : IpcService
{
private LibHac.Fs.IFileSystem _fileSystem;
public IFileSystem(LibHac.Fs.IFileSystem provider)
{
_fileSystem = provider;
}
[Command(0)]
// CreateFile(u32 createOption, u64 size, buffer<bytes<0x301>, 0x19, 0x301> path)
public ResultCode CreateFile(ServiceCtx context)
{
string name = ReadUtf8String(context);
CreateFileOptions createOption = (CreateFileOptions)context.RequestData.ReadInt32();
context.RequestData.BaseStream.Position += 4;
long size = context.RequestData.ReadInt64();
try
{
_fileSystem.CreateFile(name, size, createOption);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(1)]
// DeleteFile(buffer<bytes<0x301>, 0x19, 0x301> path)
public ResultCode DeleteFile(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
_fileSystem.DeleteFile(name);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(2)]
// CreateDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
public ResultCode CreateDirectory(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
_fileSystem.CreateDirectory(name);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(3)]
// DeleteDirectory(buffer<bytes<0x301>, 0x19, 0x301> path)
public ResultCode DeleteDirectory(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
_fileSystem.DeleteDirectory(name);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(4)]
// DeleteDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
public ResultCode DeleteDirectoryRecursively(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
_fileSystem.DeleteDirectoryRecursively(name);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(5)]
// RenameFile(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
public ResultCode RenameFile(ServiceCtx context)
{
string oldName = ReadUtf8String(context, 0);
string newName = ReadUtf8String(context, 1);
try
{
_fileSystem.RenameFile(oldName, newName);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(6)]
// RenameDirectory(buffer<bytes<0x301>, 0x19, 0x301> oldPath, buffer<bytes<0x301>, 0x19, 0x301> newPath)
public ResultCode RenameDirectory(ServiceCtx context)
{
string oldName = ReadUtf8String(context, 0);
string newName = ReadUtf8String(context, 1);
try
{
_fileSystem.RenameDirectory(oldName, newName);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(7)]
// GetEntryType(buffer<bytes<0x301>, 0x19, 0x301> path) -> nn::fssrv::sf::DirectoryEntryType
public ResultCode GetEntryType(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
DirectoryEntryType entryType = _fileSystem.GetEntryType(name);
if (entryType == DirectoryEntryType.Directory || entryType == DirectoryEntryType.File)
{
context.ResponseData.Write((int)entryType);
}
else
{
return ResultCode.PathDoesNotExist;
}
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(8)]
// OpenFile(u32 mode, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IFile> file
public ResultCode OpenFile(ServiceCtx context)
{
OpenMode mode = (OpenMode)context.RequestData.ReadInt32();
string name = ReadUtf8String(context);
try
{
LibHac.Fs.IFile file = _fileSystem.OpenFile(name, mode);
IFile fileInterface = new IFile(file);
MakeObject(context, fileInterface);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(9)]
// OpenDirectory(u32 filter_flags, buffer<bytes<0x301>, 0x19, 0x301> path) -> object<nn::fssrv::sf::IDirectory> directory
public ResultCode OpenDirectory(ServiceCtx context)
{
OpenDirectoryMode mode = (OpenDirectoryMode)context.RequestData.ReadInt32();
string name = ReadUtf8String(context);
try
{
LibHac.Fs.IDirectory dir = _fileSystem.OpenDirectory(name, mode);
IDirectory dirInterface = new IDirectory(dir);
MakeObject(context, dirInterface);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(10)]
// Commit()
public ResultCode Commit(ServiceCtx context)
{
try
{
_fileSystem.Commit();
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(11)]
// GetFreeSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalFreeSpace
public ResultCode GetFreeSpaceSize(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
context.ResponseData.Write(_fileSystem.GetFreeSpaceSize(name));
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(12)]
// GetTotalSpaceSize(buffer<bytes<0x301>, 0x19, 0x301> path) -> u64 totalSize
public ResultCode GetTotalSpaceSize(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
context.ResponseData.Write(_fileSystem.GetTotalSpaceSize(name));
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(13)]
// CleanDirectoryRecursively(buffer<bytes<0x301>, 0x19, 0x301> path)
public ResultCode CleanDirectoryRecursively(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
_fileSystem.CleanDirectoryRecursively(name);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
[Command(14)]
// GetFileTimeStampRaw(buffer<bytes<0x301>, 0x19, 0x301> path) -> bytes<0x20> timestamp
public ResultCode GetFileTimeStampRaw(ServiceCtx context)
{
string name = ReadUtf8String(context);
try
{
FileTimeStampRaw timestamp = _fileSystem.GetFileTimeStampRaw(name);
context.ResponseData.Write(timestamp.Created);
context.ResponseData.Write(timestamp.Modified);
context.ResponseData.Write(timestamp.Accessed);
byte[] data = new byte[8];
// is valid?
data[0] = 1;
context.ResponseData.Write(data);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
}
}

View file

@ -0,0 +1,65 @@
using LibHac;
using Ryujinx.HLE.HOS.Ipc;
namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy
{
class IStorage : IpcService
{
private LibHac.Fs.IStorage _baseStorage;
public IStorage(LibHac.Fs.IStorage baseStorage)
{
_baseStorage = baseStorage;
}
[Command(0)]
// Read(u64 offset, u64 length) -> buffer<u8, 0x46, 0> buffer
public ResultCode Read(ServiceCtx context)
{
long offset = context.RequestData.ReadInt64();
long size = context.RequestData.ReadInt64();
if (context.Request.ReceiveBuff.Count > 0)
{
IpcBuffDesc buffDesc = context.Request.ReceiveBuff[0];
// Use smaller length to avoid overflows.
if (size > buffDesc.Size)
{
size = buffDesc.Size;
}
byte[] data = new byte[size];
try
{
_baseStorage.Read(data, offset);
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
context.Memory.WriteBytes(buffDesc.Position, data);
}
return ResultCode.Success;
}
[Command(4)]
// GetSize() -> u64 size
public ResultCode GetSize(ServiceCtx context)
{
try
{
context.ResponseData.Write(_baseStorage.GetSize());
}
catch (HorizonResultException ex)
{
return (ResultCode)ex.ResultValue.Value;
}
return ResultCode.Success;
}
}
}