using LANCommander.Server.Data; using LANCommander.Server.Data.Models; using LANCommander.Server.Extensions; using LANCommander.SDK.Helpers; using System.Diagnostics; using System.IO.Compression; using LANCommander.SDK.Enums; namespace LANCommander.Server.Services { public class ServerService : BaseDatabaseService { private readonly GameService GameService; public ServerService(DatabaseContext dbContext, IHttpContextAccessor httpContextAccessor, GameService gameService) : base(dbContext, httpContextAccessor) { GameService = gameService; } public async Task Import(Guid objectKey) { var settings = SettingService.GetSettings(); var importArchivePath = ArchiveService.GetArchiveFileLocation(objectKey.ToString()); using (var importArchive = ZipFile.OpenRead(importArchivePath)) { var manifest = ManifestHelper.Deserialize(await importArchive.ReadAllTextAsync(ManifestHelper.ManifestFilename)); var server = await Get(manifest.Id); var exists = server != null; if (!exists) server = new Data.Models.Server() { Id = manifest.Id, }; server.Name = manifest.Name; server.Autostart = manifest.Autostart; server.AutostartMethod = (ServerAutostartMethod)(int)manifest.AutostartMethod; server.AutostartDelay = manifest.AutostartDelay; server.WorkingDirectory = Path.Combine(settings.Servers.StoragePath, server.Name.SanitizeFilename()); server.Path = manifest.Path.Replace(manifest.WorkingDirectory, server.WorkingDirectory); server.Arguments = manifest.Arguments.Replace(manifest.WorkingDirectory, server.WorkingDirectory); server.UseShellExecute = manifest.UseShellExecute; server.ProcessTerminationMethod = manifest.ProcessTerminationMethod; server.OnStartScriptPath = manifest.OnStartScriptPath; server.OnStopScriptPath = manifest.OnStopScriptPath; server.Port = manifest.Port; if (manifest.Game.Id != Guid.Empty) { var game = await GameService.Get(manifest.Game.Id); if (game != null) server.Game = game; } #region Consoles if (server.ServerConsoles == null) server.ServerConsoles = new List(); foreach (var serverConsole in server.ServerConsoles) { var manifestConsole = manifest.ServerConsoles.FirstOrDefault(c => c.Id == serverConsole.Id); if (manifestConsole != null) { serverConsole.Name = manifestConsole.Name; serverConsole.Type = (ServerConsoleType)(int)manifestConsole.Type; serverConsole.Path = manifestConsole.Path; serverConsole.Host = manifestConsole.Host; serverConsole.Port = manifestConsole.Port; // serverConsole.Password = manifestConsole.Password; } else server.ServerConsoles.Remove(serverConsole); } if (manifest.ServerConsoles != null) { foreach (var manifestConsole in manifest.ServerConsoles.Where(mc => !server.ServerConsoles.Any(c => c.Id != mc.Id))) { server.ServerConsoles.Add(new ServerConsole() { Id = manifestConsole.Id, Name = manifestConsole.Name, Type = (ServerConsoleType)(int)manifestConsole.Type, Path = manifestConsole.Path, Host = manifestConsole.Host, Port = manifestConsole.Port, // Password = manifestConsole.Password }); } } #endregion #region HTTP Paths if (server.HttpPaths == null) server.HttpPaths = new List(); foreach (var httpPath in server.HttpPaths) { var manifestHttpPath = manifest.HttpPaths.FirstOrDefault(p => p.Id == httpPath.Id); if (manifestHttpPath != null) { httpPath.LocalPath = manifestHttpPath.LocalPath.Replace(manifest.WorkingDirectory, server.WorkingDirectory); httpPath.Path = manifestHttpPath.Path; } else server.HttpPaths.Remove(httpPath); } if (manifest.HttpPaths != null) { foreach (var manifestPath in manifest.HttpPaths.Where(mp => !server.HttpPaths.Any(p => p.Id != mp.Id))) { server.HttpPaths.Add(new ServerHttpPath() { Id = manifestPath.Id, Path = manifestPath.Path, LocalPath = manifestPath.LocalPath }); } } #endregion #region Scripts if (server.Scripts == null) server.Scripts = new List