public CloudCommand( IConfig config, ISavegameBackupService savegameBackupService, ArkContextManager contextManager) { _config = config; _savegameBackupService = savegameBackupService; _contextManager = contextManager; }
public bool Update(bool manualUpdate, IConfig fullconfig, ISavegameBackupService savegameBackupService, IProgress <string> progress, CancellationToken ct) { ArkClusterDataUpdateResult result = null; var st = Stopwatch.StartNew(); try { progress.Report($"Cluster ({Config.Key}): Update started ({DateTime.Now:HH:mm:ss.ffff})"); result = Update(ct, false, fullconfig.AnonymizeWebApiData ? _anonymizeData : null); if (result?.Success == true) { progress.Report($"Cluster ({Config.Key}): Update finished in {st.ElapsedMilliseconds:N0} ms"); IsInitialized = true; LastUpdate = DateTime.Now; } if (result?.Cancelled == true) { progress.Report($"Cluster ({Config.Key}): Update was cancelled after {result.Elapsed.TotalMilliseconds:N0} ms"); } } catch (Exception ex) { Logging.LogException($"Failed to update cluster ({Config.Key})", ex, GetType(), LogLevel.ERROR, ExceptionLevel.Ignored); progress.Report($"Cluster ({Config.Key}): Update failed after {st.ElapsedMilliseconds:N0} ms"); } finally { try { var servers = _contextManager.GetServersInCluster(Config.Key); if (servers != null) { foreach (var serverContext in servers) { var success = serverContext.ApplyPreviousUpdate(); if (success == true) { serverContext.OnGameDataUpdated(); } } } } catch (Exception ex) { Logging.LogException($"Failed to apply updates to servers in cluster ({Config.Key})", ex, GetType(), LogLevel.ERROR, ExceptionLevel.Ignored); progress.Report($"Cluster ({Config.Key}): Failed to apply updates to servers in cluster"); } UpdateCompleted?.Invoke(this, result?.Success ?? false, result?.Cancelled ?? false); } return(result?.Success ?? false); }
public ArkContextManager(IConfig config, IProgress <string> progress, ISavegameBackupService savegameBackupService) { _config = config; _progress = progress; _savegameBackupService = savegameBackupService; _updateQueue = new BlockingCollection <Tuple <IArkUpdateableContext, bool> >(); _cts = new CancellationTokenSource(); _proc = Task.Run(() => _updateManagerRun(_cts.Token)); }
public RconCommand( ILifetimeScope scope, IConfig config, IConstants constants, EfDatabaseContextFactory databaseContextFactory, ISavedState savedstate, IArkServerService arkServerService, ISavegameBackupService savegameBackupService, ArkContextManager contextManager, ScheduledTasksManager scheduledTasksManager) { _config = config; _contextManager = contextManager; }
public AdminCommand( ILifetimeScope scope, IArkContext context, IConfig config, IConstants constants, EfDatabaseContextFactory databaseContextFactory, ISavedState savedstate, IArkServerService arkServerService, ISavegameBackupService savegameBackupService, ArkContextManager contextManager, ScheduledTasksManager scheduledTasksManager) { _context = context; _config = config; _constants = constants; _databaseContextFactory = databaseContextFactory; _savedstate = savedstate; _arkServerService = arkServerService; _savegameBackupService = savegameBackupService; _contextManager = contextManager; _scheduledTasksManager = scheduledTasksManager; }
public bool Update(bool manualUpdate, IConfig fullconfig, ISavegameBackupService savegameBackupService, IProgress <string> progress, CancellationToken ct) { //backup this savegame if (!manualUpdate) { SavegameBackupResult bresult = null; try { if (fullconfig.Backups.Enabled) { bresult = savegameBackupService.CreateBackup(Config, _contextManager?.GetCluster(Config.ClusterKey)?.Config); if (bresult != null && bresult.ArchivePaths != null) { progress.Report($@"Server ({Config.Key}): Backup successfull ({string.Join(", ", bresult.ArchivePaths.Select(x => $@"""{x}"""))})!"); } else { progress.Report($"Server ({Config.Key}): Backup failed..."); } } } catch (Exception ex) { Logging.LogException($"Server ({Config.Key}): Backup failed", ex, typeof(ArkServerContext), LogLevel.ERROR, ExceptionLevel.Ignored); } BackupCompleted?.Invoke(this, fullconfig.Backups.Enabled, bresult); } //todo: temp copy all ArkGameDataUpdateResult result = null; var st = Stopwatch.StartNew(); try { progress.Report($"Server ({Config.Key}): Update started ({DateTime.Now:HH:mm:ss.ffff})"); result = Update(ct, _savedState.PlayerLastActive.Where(x => x.ServerKey != null && x.ServerKey.Equals(Config.Key, StringComparison.OrdinalIgnoreCase)).Select(x => new ArkPlayerExternal { Id = x.Id, SteamId = x.SteamId, TribeId = x.TribeId, LastActiveTime = x.LastActiveTime, Name = x.Name, CharacterName = x.CharacterName }) .ToArray(), _clusterContext != null, fullconfig.AnonymizeWebApiData ? _anonymizeData : null); //update and defer apply new data until cluster is updated if (result?.Success == true) { progress.Report($"Server ({Config.Key}): Update finished in {st.ElapsedMilliseconds:N0} ms"); IsInitialized = true; LastUpdate = DateTime.Now; } if (result?.Cancelled == true) { progress.Report($"Server ({Config.Key}): Update was cancelled after {st.ElapsedMilliseconds:N0} ms"); } } catch (Exception ex) { Logging.LogException($"Failed to update server ({Config.Key})", ex, typeof(ArkServerContext), LogLevel.ERROR, ExceptionLevel.Ignored); progress.Report($"Server ({Config.Key}): Update failed after {st.ElapsedMilliseconds:N0} ms"); } finally { UpdateCompleted?.Invoke(this, result?.Success ?? false, result?.Cancelled ?? false); if (result?.Success == true && result?.Cancelled == false && _clusterContext == null) { OnGameDataUpdated(); } } return(result?.Success ?? false); }
public bool Update(bool manualUpdate, IConfig fullconfig, ISavegameBackupService savegameBackupService, IProgress <string> progress, CancellationToken ct) { //backup this savegame if (!manualUpdate) { SavegameBackupResult result = null; try { if (fullconfig.BackupsEnabled) { result = savegameBackupService.CreateBackup(Config, _contextManager?.GetCluster(Config.Cluster)?.Config); if (result != null && result.ArchivePaths != null) { progress.Report($@"Server ({Config.Key}): Backup successfull ({(string.Join(", ", result.ArchivePaths.Select(x => $@"""{x}""")))})!"); } else { progress.Report($"Server ({Config.Key}): Backup failed..."); } } } catch (Exception ex) { Logging.LogException($"Server ({Config.Key}): Backup failed", ex, typeof(ArkServerContext), LogLevel.ERROR, ExceptionLevel.Ignored); } BackupCompleted?.Invoke(this, fullconfig.BackupsEnabled, result); } //todo: temp copy all var copy = true; var success = false; var cancelled = false; var tmppaths = new List <string>(); var gid = Guid.NewGuid().ToString(); var tempFileOutputDirPath = Path.Combine(fullconfig.TempFileOutputDirPath, gid); ArkSavegame save = null; var st = Stopwatch.StartNew(); try { progress.Report($"Server ({Config.Key}): Update started ({DateTime.Now:HH:mm:ss.ffff})"); var directoryPath = Path.GetDirectoryName(Config.SaveFilePath); if (copy) { //todo: if it exists get a new path if (!Directory.Exists(tempFileOutputDirPath)) { Directory.CreateDirectory(tempFileOutputDirPath); } } if (copy) { var saveFilePath = Path.Combine(tempFileOutputDirPath, Path.GetFileName(Config.SaveFilePath)); tmppaths.Add(saveFilePath); File.Copy(Config.SaveFilePath, saveFilePath); save = new ArkSavegame(saveFilePath); } else { save = new ArkSavegame(Config.SaveFilePath); } save.LoadEverything(); ct.ThrowIfCancellationRequested(); ArkSavegameToolkitNet.ArkTribe[] tribes = null; if (copy) { var tribePaths = new List <string>(); foreach (var tp in Directory.GetFiles(directoryPath, "*.arktribe", SearchOption.TopDirectoryOnly)) { var tribePath = Path.Combine(tempFileOutputDirPath, Path.GetFileName(tp)); tribePaths.Add(tp); tmppaths.Add(tribePath); File.Copy(tp, tribePath); } tribes = tribePaths.Select(x => new ArkSavegameToolkitNet.ArkTribe(x)).ToArray(); } else { tribes = Directory.GetFiles(directoryPath, "*.arktribe", SearchOption.TopDirectoryOnly).Select(x => new ArkSavegameToolkitNet.ArkTribe(x)).ToArray(); } ct.ThrowIfCancellationRequested(); ArkProfile[] profiles = null; if (copy) { var profilePaths = new List <string>(); foreach (var pp in Directory.GetFiles(directoryPath, "*.arkprofile", SearchOption.TopDirectoryOnly)) { var profilePath = Path.Combine(tempFileOutputDirPath, Path.GetFileName(pp)); profilePaths.Add(pp); tmppaths.Add(profilePath); File.Copy(pp, profilePath); } profiles = profilePaths.Select(x => new ArkProfile(x)).ToArray(); } else { profiles = Directory.GetFiles(directoryPath, "*.arkprofile", SearchOption.TopDirectoryOnly).Select(x => new ArkProfile(x)).ToArray(); } ct.ThrowIfCancellationRequested(); var _myCharacterStatusComponent = ArkName.Create("MyCharacterStatusComponent"); var statusComponents = save.Objects.Where(x => x.IsDinoStatusComponent).ToDictionary(x => x.Index, x => x); var tamed = save.Objects.Where(x => x.IsTamedCreature).Select(x => { GameObject status = null; statusComponents.TryGetValue(x.GetPropertyValue <ObjectReference>(_myCharacterStatusComponent).ObjectId, out status); return(x.AsTamedCreature(status, null, save.SaveState)); }).ToArray(); var wild = save.Objects.Where(x => x.IsWildCreature).Select(x => { GameObject status = null; statusComponents.TryGetValue(x.GetPropertyValue <ObjectReference>(_myCharacterStatusComponent).ObjectId, out status); return(x.AsWildCreature(status, null, save.SaveState)); }).ToArray(); var _myData = ArkName.Create("MyData"); var _playerDataID = ArkName.Create("PlayerDataID"); var _linkedPlayerDataID = ArkName.Create("LinkedPlayerDataID"); var playerdict = save.Objects.Where(x => x.IsPlayerCharacter).ToLookup(x => x.GetPropertyValue <ulong>(_linkedPlayerDataID), x => x); var duplicates = playerdict.Where(x => x.Count() > 1).ToArray(); var players = profiles.Select(x => { var mydata = x.GetPropertyValue <StructPropertyList>(_myData); var playerId = mydata.GetPropertyValue <ulong>(_playerDataID); var player = playerdict[playerId]?.FirstOrDefault(); return(x.Profile.AsPlayer(player, x.SaveTime, save.SaveState)); }).ToArray(); SaveState = save.SaveState; TamedCreatures = tamed; WildCreatures = wild; Players = players; Tribes = tribes.Select(x => x.Tribe.AsTribe(x.SaveTime)).ToArray(); Items = save.Objects.Where(x => x.IsItem).Select(x => x.AsItem(save.SaveState)).ToArray(); Structures = save.Objects.Where(x => x.IsStructure).Select(x => x.AsStructure(save.SaveState)).ToArray(); progress.Report($"Server ({Config.Key}): Update finished in {st.ElapsedMilliseconds:N0} ms"); IsInitialized = true; LastUpdate = DateTime.Now; success = true; } catch (OperationCanceledException) { progress.Report($"Server ({Config.Key}): Update was cancelled after {st.ElapsedMilliseconds:N0} ms"); cancelled = true; } catch (Exception ex) { Logging.LogException($"Failed to update server ({Config.Key})", ex, typeof(ArkServerContext), LogLevel.ERROR, ExceptionLevel.Ignored); progress.Report($"Server ({Config.Key}): Update failed after {st.ElapsedMilliseconds:N0} ms"); } finally { save?.Dispose(); if (copy) { try { foreach (var path in tmppaths) { File.Delete(path); } Directory.Delete(tempFileOutputDirPath); } catch { /* ignore exception */ } } UpdateCompleted?.Invoke(this, success, cancelled); } GC.Collect(); return(success); }
public bool Update(bool manualUpdate, IConfig fullconfig, ISavegameBackupService savegameBackupService, IProgress <string> progress, CancellationToken ct) { //todo: temp copy all var copy = true; var success = false; var cancelled = false; var tmppaths = new List <string>(); var gid = Guid.NewGuid().ToString(); var tempFileOutputDirPath = Path.Combine(fullconfig.TempFileOutputDirPath, gid); var st = Stopwatch.StartNew(); try { progress.Report($"Cluster ({Config.Key}): Update started ({DateTime.Now:HH:mm:ss.ffff})"); if (copy) { //todo: if it exists get a new path if (!Directory.Exists(tempFileOutputDirPath)) { Directory.CreateDirectory(tempFileOutputDirPath); } } ArkSavegameToolkitNet.ArkCloudInventory[] cloudInventories = null; if (copy) { var cloudInventoryPaths = new List <string>(); foreach (var ci in Directory.GetFiles(Config.SavePath, "*", SearchOption.TopDirectoryOnly)) { var cloudInventoryPath = Path.Combine(tempFileOutputDirPath, Path.GetFileName(ci)); cloudInventoryPaths.Add(ci); tmppaths.Add(cloudInventoryPath); File.Copy(ci, cloudInventoryPath); } cloudInventories = cloudInventoryPaths.Select(x => new ArkSavegameToolkitNet.ArkCloudInventory(x)).ToArray(); } else { cloudInventories = Directory.GetFiles(Config.SavePath, "*", SearchOption.TopDirectoryOnly).Select(x => new ArkSavegameToolkitNet.ArkCloudInventory(x)).ToArray(); } CloudInventories = cloudInventories.Where(x => x.InventoryData != null).Select(x => x.InventoryData.AsCloudInventory(x.SteamId)).ToArray(); progress.Report($"Cluster ({Config.Key}): Update finished in {st.ElapsedMilliseconds:N0} ms"); IsInitialized = true; LastUpdate = DateTime.Now; success = true; } catch (OperationCanceledException) { progress.Report($"Cluster ({Config.Key}): Update was cancelled after {st.ElapsedMilliseconds:N0} ms"); cancelled = true; } catch (Exception ex) { Logging.LogException($"Failed to update cluster ({Config.Key})", ex, this.GetType(), LogLevel.ERROR, ExceptionLevel.Ignored); progress.Report($"Cluster ({Config.Key}): Update failed after {st.ElapsedMilliseconds:N0} ms"); } finally { if (copy) { try { foreach (var path in tmppaths) { File.Delete(path); } Directory.Delete(tempFileOutputDirPath); } catch { /* ignore exception */ } } UpdateCompleted?.Invoke(this, success, cancelled); } GC.Collect(); return(success); }