2024-05-21 20:51:09 -05:00
|
|
|
|
using LANCommander.Client.Data;
|
|
|
|
|
|
using LANCommander.Client.Data.Models;
|
2024-06-10 23:29:19 -05:00
|
|
|
|
using LANCommander.Client.Models;
|
2024-05-28 00:54:09 -05:00
|
|
|
|
using LANCommander.SDK;
|
|
|
|
|
|
using LANCommander.SDK.Helpers;
|
2024-06-12 01:51:14 -05:00
|
|
|
|
using LANCommander.SDK.PowerShell;
|
2024-05-21 20:51:09 -05:00
|
|
|
|
using System;
|
2024-05-20 20:26:15 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.Client.Services
|
|
|
|
|
|
{
|
2024-05-21 20:51:09 -05:00
|
|
|
|
public class GameService : BaseDatabaseService<Game>
|
2024-05-20 20:26:15 -05:00
|
|
|
|
{
|
2024-05-28 00:54:09 -05:00
|
|
|
|
private readonly SDK.Client Client;
|
2024-06-12 01:51:14 -05:00
|
|
|
|
private Settings Settings { get; set; }
|
2024-05-28 00:54:09 -05:00
|
|
|
|
|
|
|
|
|
|
public GameService(DatabaseContext dbContext, SDK.Client client) : base(dbContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
Client = client;
|
2024-06-12 01:51:14 -05:00
|
|
|
|
Settings = SettingService.GetSettings();
|
2024-05-28 00:54:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<SDK.Models.Action>> GetActionsAsync(Game game)
|
|
|
|
|
|
{
|
|
|
|
|
|
var actions = new List<SDK.Models.Action>();
|
|
|
|
|
|
var manifests = GetGameManifests(game);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var manifest in manifests.Where(m => m != null && m.Actions != null))
|
|
|
|
|
|
{
|
|
|
|
|
|
actions.AddRange(manifest.Actions.OrderBy(a => a.SortOrder).ToList());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check for an active connection to the server
|
|
|
|
|
|
if (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
var remoteGame = await Client.Games.GetAsync(game.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (remoteGame != null && remoteGame.Servers != null)
|
|
|
|
|
|
actions.AddRange(remoteGame.Servers.Where(s => s.Actions != null).SelectMany(s => s.Actions));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<SDK.GameManifest> GetGameManifests(Game game)
|
2024-05-21 20:51:09 -05:00
|
|
|
|
{
|
2024-05-28 00:54:09 -05:00
|
|
|
|
var manifests = new List<GameManifest>();
|
|
|
|
|
|
|
|
|
|
|
|
var mainManifest = ManifestHelper.Read(game.InstallDirectory, game.Id);
|
|
|
|
|
|
|
|
|
|
|
|
manifests.Add(mainManifest);
|
|
|
|
|
|
|
|
|
|
|
|
if (mainManifest.DependentGames != null)
|
|
|
|
|
|
foreach (var dependentGameId in mainManifest.DependentGames)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var dependentGameManifest = ManifestHelper.Read(game.InstallDirectory, dependentGameId);
|
|
|
|
|
|
|
|
|
|
|
|
if (dependentGameManifest.Type == SDK.Enums.GameType.Expansion || dependentGameManifest.Type == SDK.Enums.GameType.Mod)
|
|
|
|
|
|
manifests.Add(dependentGameManifest);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Logger?.Error(ex, $"Could not load manifest from dependent game {dependentGameId}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return manifests;
|
2024-05-21 20:51:09 -05:00
|
|
|
|
}
|
2024-06-10 23:29:19 -05:00
|
|
|
|
|
|
|
|
|
|
public async Task Uninstall(Game game)
|
|
|
|
|
|
{
|
2024-06-12 01:51:14 -05:00
|
|
|
|
Settings = SettingService.GetSettings();
|
|
|
|
|
|
|
|
|
|
|
|
if (game.DependentGames != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var dependentGame in game.DependentGames.Where(g => g.Installed))
|
|
|
|
|
|
{
|
|
|
|
|
|
await Uninstall(dependentGame);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-10 23:29:19 -05:00
|
|
|
|
await Task.Run(() => Client.Games.Uninstall(game.InstallDirectory, game.Id));
|
|
|
|
|
|
|
2024-06-12 01:51:14 -05:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
RunUninstallScript(game);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var metadataPath = SDK.GameService.GetMetadataDirectoryPath(game.InstallDirectory, game.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(metadataPath))
|
|
|
|
|
|
Directory.Delete(metadataPath, true);
|
|
|
|
|
|
|
|
|
|
|
|
DirectoryHelper.DeleteEmptyDirectories(game.InstallDirectory);
|
|
|
|
|
|
|
2024-06-10 23:29:19 -05:00
|
|
|
|
game.InstallDirectory = null;
|
|
|
|
|
|
game.Installed = false;
|
|
|
|
|
|
game.InstalledVersion = null;
|
|
|
|
|
|
|
|
|
|
|
|
await Update(game);
|
|
|
|
|
|
}
|
2024-06-12 01:51:14 -05:00
|
|
|
|
|
|
|
|
|
|
private int RunUninstallScript(Game game)
|
|
|
|
|
|
{
|
|
|
|
|
|
var manifest = ManifestHelper.Read(game.InstallDirectory, game.Id);
|
|
|
|
|
|
var path = ScriptHelper.GetScriptFilePath(game.InstallDirectory, game.Id, SDK.Enums.ScriptType.Uninstall);
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
var script = new PowerShellScript();
|
|
|
|
|
|
|
|
|
|
|
|
script.AddVariable("InstallDirectory", game.InstallDirectory);
|
|
|
|
|
|
script.AddVariable("GameManifest", manifest);
|
|
|
|
|
|
script.AddVariable("DefaultInstallDirectory", Settings.Games.DefaultInstallDirectory);
|
|
|
|
|
|
script.AddVariable("ServerAddress", Settings.Authentication.ServerAddress);
|
|
|
|
|
|
|
|
|
|
|
|
script.UseFile(path);
|
|
|
|
|
|
|
|
|
|
|
|
if (Settings.Debug.EnableScriptDebugging)
|
|
|
|
|
|
script.EnableDebug();
|
|
|
|
|
|
|
|
|
|
|
|
return script.Execute();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2024-05-20 20:26:15 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|