2020-10-05 21:25:39 +02:00
|
|
|
|
using LibHac.Common.Keys;
|
|
|
|
|
using LibHac.Fs;
|
2020-06-10 07:55:59 +02:00
|
|
|
|
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;
|
2022-04-23 21:38:10 +02:00
|
|
|
|
using LibHac.FsSystem;
|
2022-05-01 08:58:19 +02:00
|
|
|
|
using LibHac.Ncm;
|
2021-12-19 09:35:45 +01:00
|
|
|
|
using LibHac.Tools.Fs;
|
2020-02-14 01:21:24 +01:00
|
|
|
|
|
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
|
2020-02-14 01:21:24 +01:00
|
|
|
|
{
|
2022-05-01 08:58:19 +02:00
|
|
|
|
private static HorizonServerSet CreateHorizonImpl(bool sdCardInserted = true,
|
|
|
|
|
AccessControlBits.Bits fsAcBits = AccessControlBits.Bits.Debug, ProgramLocation programLocation = default)
|
2020-02-14 01:21:24 +01:00
|
|
|
|
{
|
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();
|
2021-05-17 07:52:27 +02:00
|
|
|
|
|
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);
|
2021-05-17 07:52:27 +02:00
|
|
|
|
|
2022-05-01 08:58:19 +02:00
|
|
|
|
hos.InitialProcessClient = hos.Server.CreatePrivilegedHorizonClient();
|
2021-05-17 07:52:27 +02:00
|
|
|
|
|
2022-04-23 21:38:10 +02:00
|
|
|
|
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);
|
2020-02-14 01:21:24 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
defaultObjects.SdCard.SetSdCardInsertionStatus(sdCardInserted);
|
2020-02-14 01:21:24 +01:00
|
|
|
|
|
2021-11-14 20:08:57 +01:00
|
|
|
|
var config = new FileSystemServerConfig();
|
|
|
|
|
config.FsCreators = defaultObjects.FsCreators;
|
|
|
|
|
config.DeviceOperator = defaultObjects.DeviceOperator;
|
|
|
|
|
config.ExternalKeySet = new ExternalKeySet();
|
2022-04-23 21:38:10 +02:00
|
|
|
|
config.RandomGenerator = randomGenerator;
|
2020-02-14 01:21:24 +01:00
|
|
|
|
|
2022-05-01 08:58:19 +02:00
|
|
|
|
FileSystemServerInitializer.InitializeWithConfig(hos.FsProcessClient, hos.FsServer, config);
|
|
|
|
|
hos.FsServer.SetDebugFlagEnabled(true);
|
2020-02-14 01:21:24 +01:00
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-08-23 23:37:08 +02:00
|
|
|
|
|
2022-05-01 08:58:19 +02:00
|
|
|
|
return hos;
|
2021-11-14 20:08:57 +01:00
|
|
|
|
}
|
2020-02-14 01:21:24 +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-05-31 01:29:42 +02: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);
|
2020-02-14 01:21:24 +01:00
|
|
|
|
}
|
2021-12-19 09:35:45 +01:00
|
|
|
|
}
|