Skip to content

Commit

Permalink
Merge pull request #213 from anno-mods/devel/dependency_injection
Browse files Browse the repository at this point in the history
What did I get myself into...
  • Loading branch information
taubenangriff authored Jun 18, 2023
2 parents bd061d2 + 3af1642 commit 37ef709
Show file tree
Hide file tree
Showing 179 changed files with 3,367 additions and 1,964 deletions.
4 changes: 1 addition & 3 deletions ModManager/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Imya.UI"
xmlns:converters="clr-namespace:Imya.UI.ValueConverters"

StartupUri="MainWindow.xaml">
xmlns:converters="clr-namespace:Imya.UI.ValueConverters">
<Application.Resources>

<ResourceDictionary x:Key="Colors">
Expand Down
209 changes: 188 additions & 21 deletions ModManager/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,213 @@
using System.Windows;
using Imya.Utils;
using Imya.Models;
using Imya.UI.Properties;
using Imya.Enums;
using Imya.UI.Utils;
using System.Threading.Tasks;
using Imya.Models.Options;
using Imya.Models.Attributes;
using Imya.UI.Models;
using Imya.Validation;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Imya.Services.Interfaces;
using Imya.Services;
using Imya.Models.Cache;
using Imya.GithubIntegration;
using System;
using Imya.Utils;
using Imya.Texts;
using Imya.UI.Views;
using Imya.GithubIntegration.StaticData;
using Imya.UI.Models;
using Imya.Models.Options;
using Imya.Models.GameLauncher;
using Imya.Models.Installation.Interfaces;
using Imya.Models.Installation;
using Imya.Models.Mods;
using System.Windows.Forms;
using Imya.Models.Attributes.Interfaces;
using Imya.Models.Attributes.Factories;
using Imya.Models.ModTweaker.DataModel.Storage;
using Imya.Models.ModTweaker.IO;
using Imya.UI.Components;
using Imya.Models.ModMetadata;
using Octokit;
using Imya.GithubIntegration.JsonData;
using Imya.GithubIntegration.RepositoryInformation;
using Imya.UI.ValueConverters;
using Anno.Utils;

namespace Imya.UI
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
public partial class App : System.Windows.Application
{
private ModCollectionHooks _hooks;
public static IHost AppHost { get; private set; }

public App()
{
// load localized text first
var text = TextManager.Instance;
text.LoadLanguageFile(Settings.Default.LanguageFilePath);
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
//services
services.AddSingleton<ITextManager, TextManager>();
services.AddSingleton<IGameSetupService, GameSetupService>();
var gameSetup = services.BuildServiceProvider().GetRequiredService<IGameSetupService>();
gameSetup.SetGamePath(Settings.Default.GameRootPath, true);
gameSetup.SetModDirectoryName(Settings.Default.ModDirectoryName);

services.AddSingleton<IImyaSetupService, ImyaSetupService>();
services.AddTransient<ICyclicDependencyAttributeFactory, CyclicDependencyAttributeFactory>();
services.AddTransient<IMissingModinfoAttributeFactory, MissingModinfoAttributeFactory>();
services.AddTransient<IModCompabilityAttributeFactory, ModCompabilityAttributeFactory>();
services.AddTransient<IModDependencyIssueAttributeFactory, ModDependencyIssueAttributeFactory>();
services.AddTransient<IModReplacedByAttributeFactory, ModReplacedByAttributeFactory>();
services.AddTransient<IModStatusAttributeFactory, ModStatusAttributeFactory>();
services.AddTransient<IRemovedFolderAttributeFactory, RemovedFolderAttributeFactory>();
services.AddTransient<ITweakedAttributeFactory, TweakedAttributeFactory>();
services.AddTransient<IContentInSubfolderAttributeFactory, ContentInSubfolderAttributeFactory>();
services.AddTransient<IModAccessIssueAttributeFactory, ModAccessIssueAttributeFactory>();

services.AddSingleton<LocalizedModinfoFactory>();
services.AddSingleton<IModFactory, ModFactory>();
services.AddSingleton<IModCollectionFactory, ModCollectionFactory>();
services.AddTransient<ModTweaksStorageModelLoader>();
services.AddSingleton<ITweakRepository, TweakRepository>();
services.AddSingleton<ModTweaksLoader>();
services.AddSingleton<ModTweaksExporter>();
services.AddSingleton<ModCollectionHooks>(serviceProvider => new ModCollectionHooks());

services.AddSingleton<CyclicDependencyAttributeFactory>();

services.AddSingleton<IInstallationService, InstallationService>();
services.AddSingleton<IAppSettings, AppSettings>();
services.AddSingleton<ITweakService, TweakService>();

//github integration
var githubClient = new GitHubClient(new ProductHeaderValue("iModYourAnno"));
services.AddSingleton<Octokit.IGitHubClient, Octokit.GitHubClient>(x => githubClient);
services.AddTransient<IReadmeStrategy, StaticFilenameReadmeStrategy>();
services.AddTransient<IReleaseAssetStrategy, StaticNameReleaseAssetStrategy>();
services.AddTransient<IModImageStrategy, StaticFilepathImageStrategy>();
services.AddSingleton<IAuthenticator, DeviceFlowAuthenticator>();

//tweaks

//game launcher
services.AddSingleton<IGameLauncherFactory, GameLauncherFactory>();
services.AddSingleton<SteamGameLauncher>(serviceProvider => new SteamGameLauncher(
serviceProvider.GetRequiredService<IGameSetupService>()));
services.AddSingleton<StandardGameLauncher>(serviceProvider => new StandardGameLauncher(
serviceProvider.GetRequiredService<IGameSetupService>()));
//hooks
services.AddSingleton<CyclicDependencyValidator>(serviceProvider => new CyclicDependencyValidator(
serviceProvider.GetRequiredService<CyclicDependencyAttributeFactory>()));
services.AddSingleton<ModCompatibilityValidator>();
services.AddSingleton<ModContentValidator>();
services.AddSingleton<ModDependencyValidator>();
services.AddSingleton<ModReplacementValidator>();
services.AddSingleton<RemovedModValidator>();
services.AddSingleton<TweakValidator>();

//caching
services.AddScoped<ICache<GithubRepoInfo, String>, TimedCache<GithubRepoInfo, String>>();

//installation
services.AddScoped<IModInstallationOptions, ModInstallationOptions>();
services.AddScoped<IGithubDownloaderOptions, GithubDownloaderOptions>();
services.AddScoped<IModloaderInstallationOptions, ModloaderInstallationOptions>();
services.AddScoped<IGithubInstallationBuilderFactory, GithubInstallationBuilderFactory>();
services.AddScoped<IZipInstallationBuilderFactory, ZipInstallationBuilderFactory>();

services.AddScoped<IRepositoryProvider, RepositoryProvider>();
services.AddSingleton<GithubInstallationBuilder>(serviceProvider => new GithubInstallationBuilder(
serviceProvider.GetRequiredService<IGameSetupService>(),
serviceProvider.GetRequiredService<IImyaSetupService>(),
serviceProvider.GetRequiredService<ITextManager>(),
serviceProvider.GetRequiredService<IReleaseAssetStrategy>(),
serviceProvider.GetRequiredService<IModImageStrategy>()));
services.AddSingleton<ZipInstallationBuilder>(serviceProvider => new ZipInstallationBuilder(
serviceProvider.GetRequiredService<IGameSetupService>(),
serviceProvider.GetRequiredService<IImyaSetupService>(),
serviceProvider.GetRequiredService<ITextManager>()));

services.AddSingleton<IProfilesService, ProfilesService>();

var gameSetup = GameSetupManager.Instance;
//gameSetup.SetDownloadDirectory(Settings.Default.DownloadDir);
//application
services.AddTransient<ModList>();
services.AddTransient<ModTweaker>();
services.AddTransient<Dashboard>();
services.AddTransient<ConsoleLog>();
services.AddTransient<ModDescriptionDisplay>();
services.AddSingleton<ModActivationView>();
services.AddSingleton<GithubBrowserView>();
services.AddSingleton<InstallationView>();
services.AddSingleton<ModinfoCreatorView>();
services.AddSingleton<ModTweakerView>();
services.AddSingleton<SettingsView>();
services.AddSingleton<PopupCreator>();
services.AddSingleton<MainWindow>();
services.AddSingleton<IMainViewController, MainViewController>();
services.AddSingleton<IAuthenticationController, AuthenticationController>();

services.AddTransient<DlcTextConverter>();
services.AddTransient<FilenameValidationConverter>();
services.AddSingleton<SelfUpdater>();
})
.Build();


var gameSetup = AppHost.Services.GetRequiredService<IGameSetupService>();
gameSetup.SetGamePath(Settings.Default.GameRootPath, true);
gameSetup.SetModDirectoryName(Settings.Default.ModDirectoryName);
var appSettings = new AppSettings();

GithubClientProvider.Authenticator = new DeviceFlowAuthenticator();
var factory = AppHost.Services.GetRequiredService<IModCollectionFactory>();
var collection = factory.Get(gameSetup.GetModDirectory(), normalize: true, loadImages: true);
var imyaSetup = AppHost.Services.GetRequiredService<IImyaSetupService>();
imyaSetup.GlobalModCollection = collection;

//subscribe the global mod collection to the gamesetup
var textManager = AppHost.Services.GetRequiredService<ITextManager>();
textManager.LoadLanguageFile(Settings.Default.LanguageFilePath);
//check if this can be moved to OnStartup
var globalMods = AppHost.Services.GetRequiredService<IImyaSetupService>().GlobalModCollection;
globalMods.Hooks.AddHook(AppHost.Services.GetRequiredService<ModContentValidator>());
globalMods.Hooks.AddHook(AppHost.Services.GetRequiredService<ModCompatibilityValidator>());
globalMods.Hooks.AddHook(AppHost.Services.GetRequiredService<CyclicDependencyValidator>());
globalMods.Hooks.AddHook(AppHost.Services.GetRequiredService<ModDependencyValidator>());
globalMods.Hooks.AddHook(AppHost.Services.GetRequiredService<ModReplacementValidator>());
globalMods.Hooks.AddHook(AppHost.Services.GetRequiredService<RemovedModValidator>());
globalMods.Hooks.AddHook(AppHost.Services.GetRequiredService<TweakValidator>());


}

protected override async void OnStartup(StartupEventArgs e)
{
var globalMods = AppHost.Services.GetRequiredService<IImyaSetupService>().GlobalModCollection;
await globalMods.LoadModsAsync();

var appSettings = AppHost.Services.GetRequiredService<IAppSettings>();
appSettings.Initialize();
var installationService = AppHost.Services.GetRequiredService<IInstallationService>();

if (appSettings.UseRateLimiting)
installationService.DownloadConfig.MaximumBytesPerSecond = appSettings.DownloadRateLimit;

await AppHost.StartAsync();

// init global mods
ModCollection.Global = new ModCollection(gameSetup.GetModDirectory(), normalize: true, loadImages: true);
_hooks = new(ModCollection.Global);
Task.Run(() => ModCollection.Global.LoadModsAsync());
//hacky converters with dependencyinjection....
Resources.Add("DlcTextConverter", AppHost.Services.GetRequiredService<DlcTextConverter>());
Resources.Add("FilenameValidationConverter", AppHost.Services.GetRequiredService<FilenameValidationConverter>());

if(AppSettings.Instance.UseRateLimiting)
InstallationManager.Instance.DownloadConfig.MaximumBytesPerSecond = AppSettings.Instance.DownloadRateLimit;
var startupForm = AppHost.Services.GetRequiredService<MainWindow>();
startupForm.Show();

base.OnStartup(e);
}

protected override async void OnExit(ExitEventArgs e)
{
await AppHost.StopAsync();
base.OnExit(e);
}
}
}
19 changes: 2 additions & 17 deletions ModManager/Controls/FancyToggle.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Imya.Models;
using Imya.Texts;
using Imya.Utils;
using System;
using System.Windows;
Expand Down Expand Up @@ -51,23 +52,7 @@ public FancyToggle()
InitializeComponent();

OnTextBlock.SizeChanged += OnTextBlock_SizeChanged;
OffTextBlock.SizeChanged += OnTextBlock_SizeChanged;

Binding onBinding = new()
{
Source = TextManager.Instance.GetText("CONTROLS_TOGGLE_DEFAULT_ONTEXT") as LocalizedText,
Path = new PropertyPath("Text"),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
SetBinding(OnTextProperty, onBinding);

Binding offBinding = new()
{
Source = TextManager.Instance.GetText("CONTROLS_TOGGLE_DEFAULT_OFFTEXT") as LocalizedText,
Path = new PropertyPath("Text"),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
SetBinding(OffTextProperty, offBinding);
OffTextBlock.SizeChanged += OnTextBlock_SizeChanged;
}

private void OnTextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
Expand Down
14 changes: 14 additions & 0 deletions ModManager/IMainViewController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Windows.Controls;

namespace Imya.UI
{
public interface IMainViewController
{
public delegate void ViewChangedEventHandler(View view);
public event ViewChangedEventHandler ViewChanged;

void SetView(View view);
void GoToLastView();
View GetView();
}
}
Loading

0 comments on commit 37ef709

Please sign in to comment.