public async void UpdatePacFromGfwList(Configuration config) { Logging.Info($@"Checking GFWList from {GfwlistUrl}"); try { var userAgent = config.ProxyUserAgent; var proxy = CreateProxy(config); var content = await AutoGetAsync(GfwlistUrl, proxy, userAgent, config.ConnectTimeout * 1000, TimeSpan.FromMinutes(1).TotalMilliseconds); File.WriteAllText(Utils.GetTempPath(PACServer.gfwlist_FILE), content, Encoding.UTF8); var pacFileChanged = MergeAndWritePacFile(content); UpdateCompleted?.Invoke(this, new ResultEventArgs(pacFileChanged, PacType.GfwList)); } catch (Exception ex) { Error?.Invoke(this, new ErrorEventArgs(ex)); } }
public void DownloadFileAsync(string url, bool blProxy, int downloadTimeout) { try { Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().enableSecurityProtocolTls13); UpdateCompleted?.Invoke(this, new ResultEventArgs(false, ResUI.Downloading)); var client = new HttpClient(new WebRequestHandler() { Proxy = GetWebProxy(blProxy) }); var progress = new Progress <double>(); progress.ProgressChanged += (sender, value) => { if (UpdateCompleted != null) { string msg = $"...{value}%"; UpdateCompleted(this, new ResultEventArgs(value > 100 ? true : false, msg)); } }; var cancellationToken = new CancellationTokenSource(); _ = HttpClientHelper.GetInstance().DownloadFileAsync(client, url, Utils.GetPath(Utils.GetDownloadFileName(url)), progress, cancellationToken.Token); } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); if (ex.InnerException != null) { Error?.Invoke(this, new ErrorEventArgs(ex.InnerException)); } } }
public async Task <int> DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout) { try { Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().enableSecurityProtocolTls13); UpdateCompleted?.Invoke(this, new ResultEventArgs(false, ResUI.Speedtesting)); var client = new HttpClient(new WebRequestHandler() { Proxy = webProxy }); var progress = new Progress <string>(); progress.ProgressChanged += (sender, value) => { if (UpdateCompleted != null) { string msg = $"{value} M/s".PadLeft(9, ' '); UpdateCompleted(this, new ResultEventArgs(false, msg)); } }; var cancellationToken = new CancellationTokenSource(); cancellationToken.CancelAfter(downloadTimeout * 1000); await HttpClientHelper.GetInstance().DownloadDataAsync4Speed(client, url, progress, cancellationToken.Token); } catch (Exception ex) { //Utils.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); if (ex.InnerException != null) { Error?.Invoke(this, new ErrorEventArgs(ex.InnerException)); } } return(0); }
private void Ws_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { try { if (e.Error == null || Utils.IsNullOrEmpty(e.Error.ToString())) { string source = e.Result; UpdateCompleted?.Invoke(this, new ResultEventArgs(true, source)); } else { throw e.Error; } } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); Error?.Invoke(this, new ErrorEventArgs(ex)); } }
public static void UpdatePACFromGeosite(Configuration config) { string geositeUrl = GEOSITE_URL; string group = config.geositeGroup; bool blacklist = config.geositeBlacklistMode; if (!string.IsNullOrWhiteSpace(config.geositeUrl)) { logger.Info("Found custom Geosite URL in config file"); geositeUrl = config.geositeUrl; } logger.Info($"Checking Geosite from {geositeUrl}"); WebClient http = new WebClient(); if (config.enabled) { http.Proxy = new WebProxy( config.isIPv6Enabled ? $"[{IPAddress.IPv6Loopback}]" : IPAddress.Loopback.ToString(), config.localPort); } http.DownloadDataCompleted += (o, e) => { try { File.WriteAllBytes(DATABASE_PATH, e.Result); LoadGeositeList(); bool pacFileChanged = MergeAndWritePACFile(group, blacklist); UpdateCompleted?.Invoke(null, new GeositeResultEventArgs(pacFileChanged)); } catch (Exception ex) { Error?.Invoke(null, new ErrorEventArgs(ex)); } }; http.DownloadDataAsync(new Uri(geositeUrl)); }
protected void OnUpdateCompleted() { UpdateCompleted?.Invoke(); }
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); }
/// <summary> /// Raises the event. /// </summary> /// <param name="e"> /// The parameter provided to the event handler. /// </param> /// <remarks> /// <para> /// If your application is running in WPF, this event /// will be raised on the UI thread automatically. /// </para><para> /// If your application is running in Windows Forms, /// this event will be raised on a background thread. /// If you also set DataPortal.SynchronizationObject /// to a Windows Forms form or control, then the event /// will be raised on the UI thread automatically. /// </para><para> /// In any other environment (such as ASP.NET), this /// event will be raised on a background thread. /// </para> /// </remarks> protected virtual void OnUpdateCompleted(DataPortalResult <T> e) { UpdateCompleted?.Invoke(this, e); }
public static void Update([NotNull] this DataService dataService, [NotNull] string fileName, [NotNull] string siteTimestamp, long siteFileSize, [NotNull] string action, [NotNull] UpdateCompleted callback) { Assert.ArgumentNotNull(dataService, nameof(dataService)); Assert.ArgumentNotNull(fileName, nameof(fileName)); Assert.ArgumentNotNull(siteTimestamp, nameof(siteTimestamp)); Assert.ArgumentNotNull(action, nameof(action)); Assert.ArgumentNotNull(callback, nameof(callback)); ExecuteCompleted c = delegate(string response, ExecuteResult executeResult) { byte[] file; if (!DataService.HandleExecute(response, executeResult)) { file = new byte[0]; callback("failed", ref file, 0, 0); return; } var root = Parse(response); if (root == null) { file = new byte[0]; callback("failed", ref file, 0, 0); return; } file = Convert.FromBase64String(root.Value); var result = root.GetAttributeValue("status"); long serverTimestamp; if (!long.TryParse(root.GetAttributeValue("timestamp"), out serverTimestamp)) { serverTimestamp = 0; } long serverFileSize; if (!long.TryParse(root.GetAttributeValue("filesize"), out serverFileSize)) { serverFileSize = 0; } callback(result, ref file, serverTimestamp, serverFileSize); }; dataService.ExecuteAsync("Sitecore.Rocks.Server.Requests.Projects.Update", c, fileName, siteTimestamp, siteFileSize, action); }
public static async Task UpdatePACFromGeosite() { string geositeUrl = GEOSITE_URL; string geositeSha256sumUrl = GEOSITE_SHA256SUM_URL; SHA256 mySHA256 = SHA256.Create(); var config = Program.MainController.GetCurrentConfiguration(); string group = config.geositeGroup; bool blacklist = config.geositeBlacklistMode; if (!string.IsNullOrWhiteSpace(config.geositeUrl)) { logger.Info("Found custom Geosite URL in config file"); geositeUrl = config.geositeUrl; } logger.Info($"Checking Geosite from {geositeUrl}"); // use System.Net.Http.HttpClient to download GeoSite db. // NASTY workaround: new HttpClient every update // because we can't change proxy on existing socketsHttpHandler instance httpClientHandler = new HttpClientHandler(); httpClient = new HttpClient(httpClientHandler); if (config.enabled) { httpClientHandler.Proxy = new WebProxy( config.isIPv6Enabled ? $"[{IPAddress.IPv6Loopback}]" : IPAddress.Loopback.ToString(), config.localPort); } try { // download checksum first var geositeSha256sum = await httpClient.GetStringAsync(geositeSha256sumUrl); geositeSha256sum = geositeSha256sum.Substring(0, 64).ToUpper(); logger.Info($"Got Sha256sum: {geositeSha256sum}"); // compare downloaded checksum with local geositeDB byte[] localDBHashBytes = mySHA256.ComputeHash(geositeDB); string localDBHash = BitConverter.ToString(localDBHashBytes).Replace("-", String.Empty); logger.Info($"Local Sha256sum: {localDBHash}"); // if already latest if (geositeSha256sum == localDBHash) { logger.Info("Local GeoSite DB is up to date."); return; } // not latest. download new DB var downloadedBytes = await httpClient.GetByteArrayAsync(geositeUrl); // verify sha256sum byte[] downloadedDBHashBytes = mySHA256.ComputeHash(downloadedBytes); string downloadedDBHash = BitConverter.ToString(downloadedDBHashBytes).Replace("-", String.Empty); logger.Info($"Actual Sha256sum: {downloadedDBHash}"); if (geositeSha256sum != downloadedDBHash) { logger.Info("Sha256sum Verification: FAILED. Downloaded GeoSite DB is corrupted. Aborting the update."); throw new Exception("Sha256sum mismatch"); } else { logger.Info("Sha256sum Verification: PASSED. Applying to local GeoSite DB."); } // write to geosite file using (FileStream geositeFileStream = File.Create(DATABASE_PATH)) await geositeFileStream.WriteAsync(downloadedBytes, 0, downloadedBytes.Length); // update stuff geositeDB = downloadedBytes; LoadGeositeList(); bool pacFileChanged = MergeAndWritePACFile(group, blacklist); UpdateCompleted?.Invoke(null, new GeositeResultEventArgs(pacFileChanged)); } catch (Exception ex) { Error?.Invoke(null, new ErrorEventArgs(ex)); } finally { if (httpClientHandler != null) { httpClientHandler.Dispose(); httpClientHandler = null; } if (httpClient != null) { httpClient.Dispose(); httpClient = null; } } }
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); }
private void OnMonitorUpdateCompleted(object sender, EventArgs eventArgs) { Application.Current.Dispatcher.Invoke(() => { UpdateCompleted?.Invoke(this, EventArgs.Empty); }); }
private void OnUpdateCompleted() { UpdateCompleted?.Invoke(this, EventArgs.Empty); }
protected virtual void OnUpdateCompleted() { UpdateCompleted?.Invoke(this, EventArgs.Empty); }
public async Task UpdateAllDataServices() { Debug.WriteLine($"Updating all data services in mode: {currentUpdateMode}"); try { IWalkingDataService walkingDataService = CrossServiceContainer.WalkingDataService; if (!walkingDataService.GetIsConnectedToOSService()) { walkingDataService.ConnectToOSService(); return; } TodaysWalkingModel todaysWalking = null; if (walkingDataService != null) { await walkingDataService.RequestUpdate(); todaysWalking = walkingDataService.GetTodaysHistory(); } IGoalService goalService = GoalService.Current; if (goalService != null && todaysWalking != null) { if (await goalService.CheckIfGoalCompleted(todaysWalking)) { if (!App.IsForeground && Settings.WillSendNotifications) { var id = (Config.NotificationIdGoal + (int)Settings.CurrentGoal); CrossLocalNotifications.Current.Show(AppText.notification_title, AppText.notification_goal_reached, id); Analytics.TrackEvent(TrackingEvents.BackgroundEvents.NotificationSent); } } } IAchievementService achievementService = AchievementService.Current; if (achievementService != null && todaysWalking != null) { //await achievementService.CheckAllAchievementsCriteria(todaysWalking); await achievementService.CheckTieredAchievementsProgress(todaysWalking); } var lastWalkingDateEventDate = Settings.LastWalkingDataEventDate; if (lastWalkingDateEventDate.AddDays(1) < DateTime.Today) { IStorageService storageService = StorageService.Current; if (storageService != null) { var days = await storageService.GetWalkingDaysSinceInclusive(lastWalkingDateEventDate.AddDays(1)); var today = DateTime.Today; var daysToTrack = days.Where(d => Settings.LastWalkingDataEventDate < d.Day && d.Day < today); foreach (var d in daysToTrack) { Analytics.TrackEvent(TrackingEvents.BriskWalking, new TrackingEvents.WalkingDataArgs(d.Day.DayOfWeek, d.MinutesBriskWalking)); Analytics.TrackEvent(TrackingEvents.RegularWalking, new TrackingEvents.WalkingDataArgs(d.Day.DayOfWeek, d.MinutesRegularWalking)); Analytics.TrackEvent(TrackingEvents.WalkingRaw, new TrackingEvents.WalkingRawDataArgs(d.Day.Date, d.MinutesBriskWalking, d.MinutesRegularWalking, d.MinutesUnknownWalking, Settings.UserHeight)); } //Last date stored is always today - 1 Settings.LastWalkingDataEventDate = today.AddDays(-1); // log history var all = await storageService.GetWalkingDaysSinceInclusive(DateTime.MinValue); Analytics.TrackEvent(TrackingEvents.DailyHistory, new TrackingEvents.WalkingDataArgs(all)); } } UpdateCompleted?.Invoke(); } catch (Exception e) { #if DEBUG Device.BeginInvokeOnMainThread(() => throw e); #else Crashes.TrackError(e); #endif } }
public async Task UpdatePACFromGeosite(PACSettings pACSettings) { string geositeUrl = GEOSITE_URL; string geositeSha256sumUrl = GEOSITE_SHA256SUM_URL; SHA256 mySHA256 = SHA256.Create(); bool blacklist = pACSettings.PACDefaultToDirect; var httpClient = Locator.Current.GetService <HttpClient>(); if (!string.IsNullOrWhiteSpace(pACSettings.CustomGeositeUrl)) { this.Log().Info("Found custom Geosite URL in config file"); geositeUrl = pACSettings.CustomGeositeUrl; } this.Log().Info($"Checking Geosite from {geositeUrl}"); try { // download checksum first var geositeSha256sum = await httpClient.GetStringAsync(geositeSha256sumUrl); geositeSha256sum = geositeSha256sum.Substring(0, 64).ToUpper(); this.Log().Info($"Got Sha256sum: {geositeSha256sum}"); // compare downloaded checksum with local geositeDB byte[] localDBHashBytes = mySHA256.ComputeHash(geositeDB); string localDBHash = BitConverter.ToString(localDBHashBytes).Replace("-", String.Empty); this.Log().Info($"Local Sha256sum: {localDBHash}"); // if already latest if (geositeSha256sum == localDBHash) { this.Log().Info("Local GeoSite DB is up to date."); return; } // not latest. download new DB var downloadedBytes = await httpClient.GetByteArrayAsync(geositeUrl); // verify sha256sum byte[] downloadedDBHashBytes = mySHA256.ComputeHash(downloadedBytes); string downloadedDBHash = BitConverter.ToString(downloadedDBHashBytes).Replace("-", String.Empty); this.Log().Info($"Actual Sha256sum: {downloadedDBHash}"); if (geositeSha256sum != downloadedDBHash) { this.Log().Info("Sha256sum Verification: FAILED. Downloaded GeoSite DB is corrupted. Aborting the update."); throw new Exception("Sha256sum mismatch"); } else { this.Log().Info("Sha256sum Verification: PASSED. Applying to local GeoSite DB."); } // write to geosite file using (FileStream geositeFileStream = File.Create(DATABASE_PATH)) await geositeFileStream.WriteAsync(downloadedBytes, 0, downloadedBytes.Length); // update stuff geositeDB = downloadedBytes; LoadGeositeList(); bool pacFileChanged = MergeAndWritePACFile(pACSettings.GeositeDirectGroups, pACSettings.GeositeProxiedGroups, blacklist); UpdateCompleted?.Invoke(null, new GeositeResultEventArgs(pacFileChanged)); } catch (Exception e) { this.Log().Error(e, "An error occurred while updating PAC."); } }
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); }
public void OnUpdateCompleted(bool success, string queryString) { UpdateCompleted?.Invoke(this, new UpdateCompletedEventArgs { Success = success, QueryString = queryString }); }
public override void Update(ProcessedEventHandler callback) { Assert.ArgumentNotNull(callback, nameof(callback)); var action = string.Empty; switch (ConflictResolution) { case ConflictResolution.NotResolved: callback(this, new ProcessedEventArgs(Resources.ProjectFileItem_Update_skipped, Resources.ProjectFileItem_Update_Conflict_not_resolved)); return; case ConflictResolution.UseLocalVersion: callback(this, new ProcessedEventArgs(Resources.ProjectFileItem_Update_skipped, Resources.ProjectFileItem_Update_Use_Commit_command_to_overwrite_server_version_with_local_version)); return; case ConflictResolution.UseServerVersion: action += Resources.ProjectFileItem_Update__revert; break; } UpdateCompleted process = delegate(string result, ref byte[] file, long serverTimestamp, long serverFileSize) { var processed = new ProcessedEventArgs(result); switch (result) { case "ok": if ((IsModified || IsAdded) && action.IndexOf(Resources.ProjectFileItem_Update_revert, StringComparison.InvariantCultureIgnoreCase) < 0) { ServerTimestamp = serverTimestamp; ServerFileSize = serverFileSize; ConflictResolution = ConflictResolution.NotResolved; processed.Text = Resources.ProjectFileItem_Update_conflict; processed.Comment = Resources.ProjectFileItem_Update_Local_file_has_been_modified; } else { processed.Text = Resources.ProjectFileItem_Update_updated; WriteFile(ref file, serverTimestamp, serverFileSize, processed); ConflictResolution = ConflictResolution.None; } break; case "deleted": processed.Text = Resources.ProjectFileItem_Update_deleted; DeleteFile(processed, serverTimestamp, serverFileSize); break; case "conflict": ServerTimestamp = serverTimestamp; ServerFileSize = serverFileSize; ConflictResolution = ConflictResolution.NotResolved; processed.Text = Resources.ProjectFileItem_Update_conflict; break; case "unchanged": processed.Ignore = true; break; } callback(this, processed); }; Project.Site.DataService.Update(File, ServerTimestamp.ToString(CultureInfo.InvariantCulture), ServerFileSize, action, process); }
public static async Task UpdatePACFromGeosite() { var geositeUrl = GEOSITE_URL; var geositeSha256sumUrl = GEOSITE_SHA256SUM_URL; var geositeVerifySha256 = true; var geositeSha256sum = ""; var mySHA256 = SHA256.Create(); var config = Program.MainController.GetCurrentConfiguration(); var blacklist = config.geositePreferDirect; var httpClient = Program.MainController.GetHttpClient(); if (!string.IsNullOrWhiteSpace(config.geositeUrl)) { logger.Info("Found custom Geosite URL in config file"); geositeUrl = config.geositeUrl; geositeSha256sumUrl = config.geositeSha256sumUrl; if (string.IsNullOrWhiteSpace(geositeSha256sumUrl)) { geositeVerifySha256 = false; logger.Info("Geosite SHA256 verification is disabled."); } } logger.Info($"Checking Geosite from {geositeUrl}"); try { // Use sha256sum to check if local database is already latest. if (geositeVerifySha256) { // download checksum first geositeSha256sum = await httpClient.GetStringAsync(geositeSha256sumUrl); geositeSha256sum = geositeSha256sum.Substring(0, 64).ToUpper(); logger.Info($"Got Sha256sum: {geositeSha256sum}"); // compare downloaded checksum with local geositeDB byte[] localDBHashBytes = mySHA256.ComputeHash(geositeDB); string localDBHash = BitConverter.ToString(localDBHashBytes).Replace("-", String.Empty); logger.Info($"Local Sha256sum: {localDBHash}"); // if already latest if (geositeSha256sum == localDBHash) { logger.Info("Local GeoSite DB is up to date."); return; } } // not latest. download new DB var downloadedBytes = await httpClient.GetByteArrayAsync(geositeUrl); // verify sha256sum if (geositeVerifySha256) { byte[] downloadedDBHashBytes = mySHA256.ComputeHash(downloadedBytes); string downloadedDBHash = BitConverter.ToString(downloadedDBHashBytes).Replace("-", String.Empty); logger.Info($"Actual Sha256sum: {downloadedDBHash}"); if (geositeSha256sum != downloadedDBHash) { logger.Info("Sha256sum Verification: FAILED. Downloaded GeoSite DB is corrupted. Aborting the update."); throw new Exception("Sha256sum mismatch"); } else { logger.Info("Sha256sum Verification: PASSED. Applying to local GeoSite DB."); } } // write to geosite file using (FileStream geositeFileStream = File.Create(DATABASE_PATH)) await geositeFileStream.WriteAsync(downloadedBytes, 0, downloadedBytes.Length); // update stuff geositeDB = downloadedBytes; LoadGeositeList(); bool pacFileChanged = MergeAndWritePACFile(config.geositeDirectGroups, config.geositeProxiedGroups, blacklist); UpdateCompleted?.Invoke(null, new GeositeResultEventArgs(pacFileChanged)); } catch (Exception ex) { Error?.Invoke(null, new ErrorEventArgs(ex)); } }