From 19de47bacf740b923782e812d236ba5da69c1130 Mon Sep 17 00:00:00 2001 From: Cinka Date: Sun, 19 Jan 2025 22:52:29 +0300 Subject: [PATCH] - tweak: error cleanup --- .../Views/ServerEntryView.axaml.cs | 50 +++++-------------- Nebula.Shared/FileApis/AssemblyApi.cs | 5 +- Nebula.Shared/FileApis/FileApi.cs | 5 +- Nebula.Shared/FileApis/HashApi.cs | 5 +- Nebula.Shared/Services/DebugService.cs | 23 +-------- Nebula.Shared/Services/EngineService.cs | 6 +-- Nebula.Shared/Services/FileService.cs | 2 +- Nebula.Shared/Services/RestService.cs | 2 +- Nebula.Shared/Utils/Manifest.cs | 2 +- 9 files changed, 29 insertions(+), 71 deletions(-) diff --git a/Nebula.Launcher/Views/ServerEntryView.axaml.cs b/Nebula.Launcher/Views/ServerEntryView.axaml.cs index 683464a..7c13209 100644 --- a/Nebula.Launcher/Views/ServerEntryView.axaml.cs +++ b/Nebula.Launcher/Views/ServerEntryView.axaml.cs @@ -1,7 +1,5 @@ using System; -using Avalonia; using Avalonia.Controls; -using Avalonia.Markup.Xaml; using Avalonia.Threading; using Nebula.Launcher.ViewModels; @@ -9,11 +7,17 @@ namespace Nebula.Launcher.Views; public partial class ServerEntryView : UserControl { - private DispatcherTimer _scrollTimer; - private bool _scrollingDown = true; + private readonly TimeSpan _interval = TimeSpan.FromMilliseconds(25); + private readonly DispatcherTimer _scrollTimer; + private TimeSpan _currTime = TimeSpan.Zero; public ServerEntryView() { + _scrollTimer = new DispatcherTimer + { + Interval = _interval + }; + InitializeComponent(); StartAutoScrolling(); } @@ -25,45 +29,17 @@ public partial class ServerEntryView : UserControl private void StartAutoScrolling() { - _scrollTimer = new DispatcherTimer - { - Interval = TimeSpan.FromMilliseconds(50) // Adjust the interval for scroll speed - }; _scrollTimer.Tick += AutoScroll; _scrollTimer.Start(); } private void AutoScroll(object? sender, EventArgs e) { - // Get the current offset and extent - var currentOffset = AutoScrollViewer.Offset.X; var maxOffset = AutoScrollViewer.Extent.Width - AutoScrollViewer.Viewport.Width; - - if (_scrollingDown) - { - // Scroll down - if (currentOffset < maxOffset) - { - AutoScrollViewer.Offset = new Avalonia.Vector(currentOffset + 2, AutoScrollViewer.Offset.Y); // Adjust speed - } - else - { - // Reverse direction when reaching the bottom - _scrollingDown = false; - } - } - else - { - // Scroll up - if (currentOffset > 0) - { - AutoScrollViewer.Offset = new Avalonia.Vector(currentOffset - 2, AutoScrollViewer.Offset.Y); // Adjust speed - } - else - { - // Reverse direction when reaching the top - _scrollingDown = true; - } - } + var value = (Math.Sin(_currTime.TotalSeconds / 2) + 1) * (maxOffset / 2) ; + + AutoScrollViewer.Offset = new Avalonia.Vector(value, AutoScrollViewer.Offset.Y); + _currTime += _interval; + if (_currTime > TimeSpan.FromSeconds(10000)) _currTime = TimeSpan.Zero; } } \ No newline at end of file diff --git a/Nebula.Shared/FileApis/AssemblyApi.cs b/Nebula.Shared/FileApis/AssemblyApi.cs index 2b9d48c..44e6ce0 100644 --- a/Nebula.Shared/FileApis/AssemblyApi.cs +++ b/Nebula.Shared/FileApis/AssemblyApi.cs @@ -1,4 +1,5 @@ -using Robust.LoaderApi; +using System.Diagnostics.CodeAnalysis; +using Robust.LoaderApi; namespace Nebula.Shared.FileApis; @@ -11,7 +12,7 @@ public class AssemblyApi : IFileApi _root = root; } - public bool TryOpen(string path, out Stream? stream) + public bool TryOpen(string path,[NotNullWhen(true)] out Stream? stream) { return _root.TryOpen(path, out stream); } diff --git a/Nebula.Shared/FileApis/FileApi.cs b/Nebula.Shared/FileApis/FileApi.cs index 68137f7..ee11c84 100644 --- a/Nebula.Shared/FileApis/FileApi.cs +++ b/Nebula.Shared/FileApis/FileApi.cs @@ -1,4 +1,5 @@ -using Nebula.Shared.FileApis.Interfaces; +using System.Diagnostics.CodeAnalysis; +using Nebula.Shared.FileApis.Interfaces; namespace Nebula.Shared.FileApis; @@ -11,7 +12,7 @@ public sealed class FileApi : IReadWriteFileApi RootPath = rootPath; } - public bool TryOpen(string path, out Stream? stream) + public bool TryOpen(string path,[NotNullWhen(true)] out Stream? stream) { var fullPath = Path.Join(RootPath, path); if (File.Exists(fullPath)) diff --git a/Nebula.Shared/FileApis/HashApi.cs b/Nebula.Shared/FileApis/HashApi.cs index 321a6e4..929e313 100644 --- a/Nebula.Shared/FileApis/HashApi.cs +++ b/Nebula.Shared/FileApis/HashApi.cs @@ -1,4 +1,5 @@ -using Nebula.Shared.Models; +using System.Diagnostics.CodeAnalysis; +using Nebula.Shared.Models; using Robust.LoaderApi; namespace Nebula.Shared.FileApis; @@ -15,7 +16,7 @@ public class HashApi : IFileApi foreach (var item in manifest) Manifest.TryAdd(item.Path, item); } - public bool TryOpen(string path, out Stream? stream) + public bool TryOpen(string path,[NotNullWhen(true)] out Stream? stream) { if (path[0] == '/') path = path.Substring(1); diff --git a/Nebula.Shared/Services/DebugService.cs b/Nebula.Shared/Services/DebugService.cs index bf22b17..742dfa3 100644 --- a/Nebula.Shared/Services/DebugService.cs +++ b/Nebula.Shared/Services/DebugService.cs @@ -5,30 +5,16 @@ namespace Nebula.Shared.Services; [ServiceRegister] public class DebugService : IDisposable { - private static string LogPath = Path.Combine(FileService.RootPath, "log"); - public DateTime LogDate = DateTime.Now; public ILogger Logger; - private FileStream LogStream; - private StreamWriter LogWriter; public DebugService(ILogger logger) { Logger = logger; - - //if (!Directory.Exists(LogPath)) - // Directory.CreateDirectory(LogPath); - - //var filename = String.Format("{0:yyyy-MM-dd}.txt", DateTime.Now); - - //LogStream = File.Open(Path.Combine(LogPath, filename), - // FileMode.Append, FileAccess.Write); - //LogWriter = new StreamWriter(LogStream); } public void Dispose() { - LogWriter.Dispose(); - LogStream.Dispose(); + } public void Debug(string message) @@ -56,13 +42,6 @@ public class DebugService : IDisposable private void Log(LoggerCategory category, string message) { Logger.Log(category, message); - //SaveToLog(category, message); - } - - private void SaveToLog(LoggerCategory category, string message) - { - LogWriter.WriteLine($"[{category}] {message}"); - LogWriter.Flush(); } } diff --git a/Nebula.Shared/Services/EngineService.cs b/Nebula.Shared/Services/EngineService.cs index 0f9427a..273561c 100644 --- a/Nebula.Shared/Services/EngineService.cs +++ b/Nebula.Shared/Services/EngineService.cs @@ -84,7 +84,7 @@ public sealed class EngineService var api = _fileService.OpenZip(version, _fileService.EngineFileApi); if (api != null) return _assemblyService.Mount(api); } - catch (Exception e) + catch (Exception) { _fileService.EngineFileApi.Remove(version); throw; @@ -164,9 +164,9 @@ public sealed class EngineService try { - return _assemblyService.Mount(_fileService.OpenZip(fileName, _fileService.EngineFileApi)); + return _assemblyService.Mount(_fileService.OpenZip(fileName, _fileService.EngineFileApi) ?? throw new InvalidOperationException($"{fileName} is not exist!")); } - catch (Exception e) + catch (Exception) { _fileService.EngineFileApi.Remove(fileName); throw; diff --git a/Nebula.Shared/Services/FileService.cs b/Nebula.Shared/Services/FileService.cs index 6d34fbf..3a5a34d 100644 --- a/Nebula.Shared/Services/FileService.cs +++ b/Nebula.Shared/Services/FileService.cs @@ -65,7 +65,7 @@ public class FileService if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) prefix = "Space Station 14.app/Contents/Resources/"; return new ZipFileApi(zipArchive, prefix); } - catch (Exception e) + catch (Exception) { zipStream?.Dispose(); throw; diff --git a/Nebula.Shared/Services/RestService.cs b/Nebula.Shared/Services/RestService.cs index 77cc549..1546278 100644 --- a/Nebula.Shared/Services/RestService.cs +++ b/Nebula.Shared/Services/RestService.cs @@ -58,7 +58,7 @@ public class RestService return await ReadResult(response, cancellationToken); } - private async Task> ReadResult(HttpResponseMessage response, CancellationToken cancellationToken) + private async Task> ReadResult(HttpResponseMessage response, CancellationToken cancellationToken) where T : notnull { var content = await response.Content.ReadAsStringAsync(cancellationToken); diff --git a/Nebula.Shared/Utils/Manifest.cs b/Nebula.Shared/Utils/Manifest.cs index 91f3048..72c35e2 100644 --- a/Nebula.Shared/Utils/Manifest.cs +++ b/Nebula.Shared/Utils/Manifest.cs @@ -87,7 +87,7 @@ public class ManifestReader : StreamReader private void ReadManifestVersion() { - ManifestVersion = ReadLine(); + ManifestVersion = ReadLine() ?? throw new InvalidOperationException("File is empty!"); } public RobustManifestItem? ReadItem()