using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Wabbajack.App.Models;
using Wabbajack.Compiler;
using Wabbajack.Downloaders;
using Wabbajack.Downloaders.GameFile;
using Wabbajack.DTOs;
using Wabbajack.DTOs.Logins;
using Wabbajack.Installer;
using Wabbajack.Networking.Discord;
using Wabbajack.Networking.Http;
using Wabbajack.Networking.Http.Interfaces;
using Wabbajack.Networking.NexusApi;
using Wabbajack.Networking.Steam;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Wabbajack.RateLimiter;
using Wabbajack.Services.OSIntegrated.Services;
using Wabbajack.Services.OSIntegrated.TokenProviders;
using Wabbajack.VFS;
using Client = Wabbajack.Networking.WabbajackClientApi.Client;
namespace Wabbajack.Services.OSIntegrated;
public static class ServiceExtensions
{
///
/// Adds variants of services that integrate into global OS services. These are not testing
/// variants or services that require Environment variables. These are the "full fat" services.
///
///
public static IServiceCollection AddOSIntegrated(this IServiceCollection service,
Action? cfn = null)
{
var options = new OSIntegratedOptions();
cfn?.Invoke(options);
service.AddTransient(s =>
new TemporaryFileManager(KnownFolders.EntryPoint.Combine("temp", Guid.NewGuid().ToString())));
service.AddSingleton(s => options.UseLocalCache
? new FileHashCache(s.GetService()!.CreateFile().Path,
s.GetService>()!)
: new FileHashCache(KnownFolders.AppDataLocal.Combine("Wabbajack", "GlobalHashCache.sqlite"),
s.GetService>()!));
service.AddSingleton(s => options.UseLocalCache
? new VFSCache(s.GetService()!.CreateFile().Path)
: new VFSCache(KnownFolders.EntryPoint.Combine("GlobalVFSCache3.sqlite")));
service.AddSingleton(s => options.UseLocalCache
? new BinaryPatchCache(s.GetService()!.CreateFile().Path)
: new BinaryPatchCache(KnownFolders.EntryPoint.Combine("patchCache.sqlite")));
service.AddSingleton(new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount});
Func> GetSettings(IServiceProvider provider, string name)
{
return async () =>
{
var s = await provider.GetService()!.GetSettings(name);
return ((int) s.MaxTasks, s.MaxThroughput);
};
}
// Settings
service.AddSingleton(s => new Configuration
{
EncryptedDataLocation = KnownFolders.WabbajackAppLocal.Combine("encrypted"),
ModListsDownloadLocation = KnownFolders.EntryPoint.Combine("downloaded_mod_lists"),
SavedSettingsLocation = KnownFolders.WabbajackAppLocal.Combine("saved_settings"),
LogLocation = KnownFolders.EntryPoint.Combine("logs"),
ImageCacheLocation = KnownFolders.WabbajackAppLocal.Combine("image_cache")
});
service.AddSingleton();
service.AddSingleton();
// Resources
service.AddAllSingleton>(s =>
new Resource("Downloads", GetSettings(s, "Downloads")));
service.AddAllSingleton>(s => new Resource("Web Requests", GetSettings(s, "Web Requests")));
service.AddAllSingleton>(s => new Resource("VFS", GetSettings(s, "VFS")));
service.AddAllSingleton>(s =>
new Resource("File Hashing", GetSettings(s, "File Hashing")));
service.AddAllSingleton>(s =>
new Resource("File Extractor", GetSettings(s, "File Extractor")));
service.AddAllSingleton>(s =>
new Resource("Compiler", GetSettings(s, "Compiler")));
service.AddSingleton();
service.AddScoped();
service.AddSingleton();
service.AddSingleton();
// Networking
service.AddSingleton();
service.AddAllSingleton();
service.AddSteam();
service.AddSingleton();
service.AddSingleton();
// Token Providers
service.AddAllSingleton, NexusApiTokenProvider>();
service
.AddAllSingleton, EncryptedJsonTokenProvider,
LoversLabTokenProvider>();
service
.AddAllSingleton, EncryptedJsonTokenProvider,
VectorPlexusTokenProvider>();
service
.AddAllSingleton, EncryptedJsonTokenProvider,
SteamTokenProvider>();
service.AddAllSingleton, WabbajackApiTokenProvider>();
service
.AddAllSingleton>,
EncryptedJsonTokenProvider>, DiscordTokenProvider>();
service.AddAllSingleton();
service.AddDownloadDispatcher();
if (options.UseStubbedGameFolders)
service.AddAllSingleton();
else
service.AddAllSingleton();
// Installer/Compiler Configuration
service.AddScoped();
service.AddScoped();
service.AddScoped();
service.AddScoped();
// Application Info
var version =
$"{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Patch}{ThisAssembly.Git.SemVer.DashLabel}";
service.AddSingleton(s => new ApplicationInfo
{
ApplicationSlug = "Wabbajack",
ApplicationName = Environment.ProcessPath?.ToAbsolutePath().FileName.ToString() ?? "Wabbajack",
ApplicationSha = ThisAssembly.Git.Sha,
Platform = RuntimeInformation.ProcessArchitecture.ToString(),
OperatingSystemDescription = RuntimeInformation.OSDescription,
RuntimeIdentifier = RuntimeInformation.RuntimeIdentifier,
OSVersion = Environment.OSVersion.VersionString,
Version = version
});
return service;
}
public class OSIntegratedOptions
{
public bool UseLocalCache { get; set; } = false;
public bool UseStubbedGameFolders { get; set; } = false;
}
}