예제 #1
0
        /// <summary>
        /// Invoke the whole backup process - get sources, discover files, copy to destination
        /// </summary>
        static public void InvokeBackup()
        {
            try
            {
                BackupStarted?.Invoke();

                //while (!cts.IsCancellationRequested)
                //{
                //    Thread.Sleep(1000);
                //    Debug.WriteLine("WORKING");
                //}
                //Debug.WriteLine("COMPLETE");
                //isWorking = false;
                //return;

                var sources = GetSources();
                var files   = DiscoverFiles(sources);
                SaveDiscoveredFiles(files);
                CopyFilesAsync();
                UpdateTimestamp(sources);

                BackupCompleted?.Invoke();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                BackupError?.Invoke(ex.Message);
            }
        }
예제 #2
0
 /// <summary>
 /// SQLs the backup on complete.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="ServerMessageEventArgs"/> instance containing the event data.</param>
 private static void SqlBackupOnComplete(object sender, ServerMessageEventArgs e)
 {
     if (e.Error != null)
     {
         Log.NewLog(LogType.Info, e.Error.Message);
         BackupCompleted?.Invoke(null, e);
     }
 }
예제 #3
0
        /// <summary>
        /// Invoke backup of the discovered files list, possibly modified by user
        /// </summary>
        static public void InvokeBackupFromUser()
        {
            try
            {
                BackupStarted?.Invoke();

                CopyFiles();

                UpdateTimestamp(GetSources());

                BackupCompleted?.Invoke();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                BackupError?.Invoke(ex.Message);
            }
        }
예제 #4
0
 private void Context_BackupCompleted(ArkServerContext sender, bool backupsEnabled, Services.Data.SavegameBackupResult result)
 {
     BackupCompleted?.Invoke(sender, backupsEnabled, result);
 }
예제 #5
0
 protected void OnBackupCompleted(int serverHostID)
 {
     BackupCompleted?.Invoke(serverHostID);
 }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }