LibHac/tests/LibHac.Tests/Fs/FileSystemClientTests/FileSystemServerFactory.cs

94 lines
3.2 KiB
C#
Raw Normal View History

2020-10-05 21:25:39 +02:00
using LibHac.Common.Keys;
using LibHac.Fs;
using LibHac.Fs.Fsa;
2020-08-07 20:45:11 +02:00
using LibHac.FsSrv;
2022-05-01 08:58:19 +02:00
using LibHac.FsSrv.Impl;
using LibHac.FsSystem;
2022-05-01 08:58:19 +02:00
using LibHac.Ncm;
using LibHac.Tools.Fs;
2021-11-14 20:08:57 +01:00
namespace LibHac.Tests.Fs.FileSystemClientTests;
2022-05-01 08:58:19 +02:00
public class HorizonServerSet
{
public Horizon Server { get; set; }
public HorizonClient FsProcessClient { get; set; }
public HorizonClient InitialProcessClient { get; set; }
public HorizonClient Client { get; set; }
public FileSystemServer FsServer { get; set; }
public IFileSystem RootFileSystem { get; set; }
}
2021-11-14 20:08:57 +01:00
public static class FileSystemServerFactory
{
2022-05-01 08:58:19 +02:00
private static HorizonServerSet CreateHorizonImpl(bool sdCardInserted = true,
AccessControlBits.Bits fsAcBits = AccessControlBits.Bits.Debug, ProgramLocation programLocation = default)
{
2022-05-01 08:58:19 +02:00
var hos = new HorizonServerSet();
hos.RootFileSystem = new InMemoryFileSystem();
2021-11-14 20:08:57 +01:00
var keySet = new KeySet();
2022-05-01 08:58:19 +02:00
hos.Server = new Horizon(new HorizonConfiguration());
hos.FsProcessClient = hos.Server.CreatePrivilegedHorizonClient();
hos.FsServer = new FileSystemServer(hos.FsProcessClient);
2022-05-01 08:58:19 +02:00
hos.InitialProcessClient = hos.Server.CreatePrivilegedHorizonClient();
var random = new Random(12345);
RandomDataGenerator randomGenerator = buffer => random.NextBytes(buffer);
2022-05-01 08:58:19 +02:00
var defaultObjects =
DefaultFsServerObjects.GetDefaultEmulatedCreators(hos.RootFileSystem, keySet, hos.FsServer,
randomGenerator);
2021-11-14 20:08:57 +01:00
defaultObjects.SdCard.SetSdCardInsertionStatus(sdCardInserted);
2021-11-14 20:08:57 +01:00
var config = new FileSystemServerConfig();
config.FsCreators = defaultObjects.FsCreators;
config.DeviceOperator = defaultObjects.DeviceOperator;
config.ExternalKeySet = new ExternalKeySet();
config.RandomGenerator = randomGenerator;
2022-05-01 08:58:19 +02:00
FileSystemServerInitializer.InitializeWithConfig(hos.FsProcessClient, hos.FsServer, config);
hos.FsServer.SetDebugFlagEnabled(true);
2022-05-01 08:58:19 +02:00
if (programLocation.ProgramId == ProgramId.InvalidId)
{
hos.Client = hos.Server.CreateHorizonClient();
}
else
{
hos.Client = hos.Server.CreateHorizonClient(programLocation, fsAcBits);
}
2022-05-01 08:58:19 +02:00
return hos;
2021-11-14 20:08:57 +01:00
}
2021-11-14 20:08:57 +01:00
public static FileSystemClient CreateClient(bool sdCardInserted)
{
2022-05-01 08:58:19 +02:00
HorizonServerSet hos = CreateHorizonImpl(sdCardInserted: sdCardInserted);
return hos.InitialProcessClient.Fs;
2021-11-14 20:08:57 +01:00
}
2020-04-03 06:57:08 +02:00
2021-11-14 20:08:57 +01:00
public static FileSystemClient CreateClient(out IFileSystem rootFs)
{
2022-05-01 08:58:19 +02:00
HorizonServerSet hos = CreateHorizonImpl(sdCardInserted: true);
rootFs = hos.RootFileSystem;
return hos.InitialProcessClient.Fs;
2021-11-14 20:08:57 +01:00
}
2021-11-14 20:08:57 +01:00
public static Horizon CreateHorizonServer()
{
2022-05-01 08:58:19 +02:00
return CreateHorizonImpl(sdCardInserted: true).Server;
}
public static HorizonServerSet CreateHorizon(ProgramId programId = default, bool sdCardInserted = true,
AccessControlBits.Bits fsAcBits = AccessControlBits.Bits.Debug)
{
var programLocation = new ProgramLocation(programId, StorageId.BuiltInUser);
return CreateHorizonImpl(sdCardInserted, fsAcBits, programLocation);
}
}