using CommandLine; using LANCommander.Launcher.Data; using LANCommander.Launcher.Models; using LANCommander.SDK; using LANCommander.SDK.Enums; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LANCommander.Launcher.Services.Extensions { public static class ServiceCollectionExtensions { public static IServiceCollection AddLANCommander(this IServiceCollection services) { var settings = SettingService.GetSettings(); services.AddDbContext(); #region Register Client var client = new SDK.Client(settings.Authentication.ServerAddress, settings.Games.DefaultInstallDirectory); client.UseToken(new SDK.Models.AuthToken { AccessToken = settings.Authentication.AccessToken, RefreshToken = settings.Authentication.RefreshToken, }); client.Scripts.Debug = settings.Debug.EnableScriptDebugging; client.Scripts.ExternalScriptRunner += Scripts_ExternalScriptRunner; services.AddSingleton(client); services.AddSingleton(); #endregion services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); return services; } private static async Task Scripts_ExternalScriptRunner(SDK.PowerShell.PowerShellScript script) { if (script.RunAsAdmin) { var manifest = script.Variables.GetValue("GameManifest"); var options = new RunScriptCommandLineOptions { InstallDirectory = script.Variables.GetValue("InstallDirectory"), GameId = manifest.Id, Type = script.Type }; if (script.Type == ScriptType.KeyChange) options.AllocatedKey = script.Variables.GetValue("AllocatedKey"); if (script.Type == ScriptType.NameChange) { options.OldPlayerAlias = script.Variables.GetValue("OldPlayerAlias"); options.NewPlayerAlias = script.Variables.GetValue("NewPlayerAlias"); } var arguments = Parser.Default.FormatCommandLine(options); var path = Process.GetCurrentProcess().MainModule.FileName; var process = new Process(); process.StartInfo.FileName = path; process.StartInfo.Verb = "runas"; process.StartInfo.UseShellExecute = true; process.StartInfo.WorkingDirectory = script.WorkingDirectory; process.StartInfo.Arguments = arguments; process.Start(); await process.WaitForExitAsync(); return true; } return false; } } }