inital SK upload
This commit is contained in:
parent
27d6e530e0
commit
5edb6915ed
125 changed files with 2659 additions and 107 deletions
130
src/RynthCore.DesktopLog/DesktopRollingLog.cs
Normal file
130
src/RynthCore.DesktopLog/DesktopRollingLog.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace RynthCore;
|
||||
|
||||
/// <summary>
|
||||
/// Desktop text logs with a fixed size cap and per-day roll retention. Active file lives on the user
|
||||
/// desktop as <c>{fileStem}.log</c>. When the active file reaches <see cref="MaxActiveFileBytes"/>,
|
||||
/// it is moved into <c>Desktop\RynthLogs\</c> with a time-stamped name, then a new active file is
|
||||
/// created. For each <paramref name="fileStem"/> and each calendar day, at most
|
||||
/// <see cref="MaxRolledFilesPerLocalDay"/> archived files are kept (oldest for that day deleted first).
|
||||
/// </summary>
|
||||
public static class DesktopRollingLog
|
||||
{
|
||||
// 10 MB active segment, up to 10 rolled segments per local calendar day.
|
||||
public const long MaxActiveFileBytes = 10L * 1024 * 1024;
|
||||
public const int MaxRolledFilesPerLocalDay = 10;
|
||||
|
||||
private static readonly string DesktopDir =
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
|
||||
|
||||
private static readonly string ArchiveDir = Path.Combine(DesktopDir, "RynthLogs");
|
||||
|
||||
/// <summary>Pre-defined stems for the three Rynth products (one file each on the desktop).</summary>
|
||||
public const string StemEngine = "RynthCore";
|
||||
public const string StemLauncher = "RynthCore-Launcher";
|
||||
public const string StemInjector = "RynthCore-Injector";
|
||||
|
||||
private static readonly object Gate = new();
|
||||
|
||||
/// <param name="fileStem">File name without extension, e.g. <see cref="StemEngine"/>.</param>
|
||||
public static void AppendLine(string fileStem, string line)
|
||||
{
|
||||
string stem = SanitizeStem(fileStem);
|
||||
string text = line ?? string.Empty;
|
||||
if (text.Length > 0 && text[^1] is not '\n' and not '\r')
|
||||
text += Environment.NewLine;
|
||||
|
||||
lock (Gate)
|
||||
{
|
||||
try
|
||||
{
|
||||
string active = Path.Combine(DesktopDir, stem + ".log");
|
||||
Directory.CreateDirectory(ArchiveDir);
|
||||
|
||||
if (File.Exists(active) && new FileInfo(active).Length + Encoding.UTF8.GetByteCount(text) > MaxActiveFileBytes)
|
||||
RotateToArchive(stem, active);
|
||||
|
||||
using var stream = new FileStream(active, FileMode.Append, FileAccess.Write, FileShare.Read);
|
||||
var bytes = Encoding.UTF8.GetBytes(text);
|
||||
stream.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Intentionally silent: logging must not crash host.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Moves current active file to RynthLogs; trims same-day archives for this stem, then the move.
|
||||
private static void RotateToArchive(string stem, string activePath)
|
||||
{
|
||||
string ymd = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
|
||||
string prefix = stem + "-roll-" + ymd + "-";
|
||||
|
||||
PruneOldestRolledForPrefix(prefix);
|
||||
|
||||
string destName = prefix + DateTime.Now.ToString("HHmmssfff", CultureInfo.InvariantCulture) + ".log";
|
||||
string destPath = Path.Combine(ArchiveDir, destName);
|
||||
if (File.Exists(destPath)) File.Delete(destPath);
|
||||
try
|
||||
{
|
||||
File.Move(activePath, destPath, overwrite: false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(destPath)) File.Delete(destPath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
|
||||
if (File.Exists(activePath)) File.Delete(activePath);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensures that after a new file with this prefix is created, at most MaxRolledFilesPerLocalDay rolled files for this prefix+day exist.
|
||||
private static void PruneOldestRolledForPrefix(string prefix)
|
||||
{
|
||||
if (!Directory.Exists(ArchiveDir)) return;
|
||||
|
||||
var todaysRolled = new DirectoryInfo(ArchiveDir)
|
||||
.GetFiles(prefix + "*.log", SearchOption.TopDirectoryOnly)
|
||||
.OrderBy(f => f.CreationTimeUtc)
|
||||
.ThenBy(f => f.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
|
||||
// Delete oldest rolled segments until a new one would not exceed the daily cap.
|
||||
while (todaysRolled.Count >= MaxRolledFilesPerLocalDay)
|
||||
{
|
||||
try
|
||||
{
|
||||
todaysRolled[0].Delete();
|
||||
todaysRolled.RemoveAt(0);
|
||||
}
|
||||
catch
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string SanitizeStem(string? stem)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(stem)) return "RynthLog";
|
||||
var invalid = Path.GetInvalidFileNameChars();
|
||||
var b = new StringBuilder(stem.Length);
|
||||
foreach (var ch in stem.Trim())
|
||||
{
|
||||
if (!invalid.Contains(ch) && ch != Path.DirectorySeparatorChar && ch != Path.AltDirectorySeparatorChar)
|
||||
b.Append(ch);
|
||||
}
|
||||
|
||||
string t = b.ToString();
|
||||
return t.Length == 0 ? "RynthLog" : t;
|
||||
}
|
||||
}
|
||||
105
src/RynthCore.DesktopLog/NetDesktopX64Prerequisite.cs
Normal file
105
src/RynthCore.DesktopLog/NetDesktopX64Prerequisite.cs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
// Detects the same "Suite tools" .NET 10+ x64 prerequisite as the Inno installer
|
||||
// (installer\RynthCore.iss, IsNetDesktop* helpers). If you change registry checks here,
|
||||
// update RynthCore.iss in lockstep and vice versa.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.Versioning;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace RynthCore;
|
||||
|
||||
/// <summary>
|
||||
/// Registry probe for the optional x64 framework-dependent tools (Loot Editor, Monster Editor).
|
||||
/// Must match <c>IsNetDesktop10PlusX64Present</c> and its helpers in <c>installer/RynthCore.iss</c>
|
||||
/// (Path A: <c>Microsoft.WindowsDesktop.App</c> shared folders; Path B: <c>sharedhost</c> Version;
|
||||
/// Path C: <c>InstalledManifests\x64</c> manifest-10. bands). Uses 64-bit registry view from an x86 process.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static class NetDesktopX64Prerequisite
|
||||
{
|
||||
// Same string as RynthCore.iss DotNet10DesktopDownloadUrl (x64 runtime hub; user picks Desktop / ASP.NET as needed).
|
||||
public const string DotNet10RuntimeDownloadUrl = "https://dotnet.microsoft.com/en-us/download/dotnet/10.0/runtime";
|
||||
|
||||
/// <summary>True if the x64 FDD tool prerequisite is met, or the OS is 32-bit (N/A, same as Inno skip).</summary>
|
||||
public static bool IsSatisfied()
|
||||
{
|
||||
if (!Environment.Is64BitOperatingSystem)
|
||||
return true;
|
||||
|
||||
return
|
||||
IsWindowsDesktopSharedFramework10Plus()
|
||||
|| IsDotNet10BandInInstalledManifestsX64()
|
||||
|| IsSharedHostVersion10PlusX64();
|
||||
}
|
||||
|
||||
// Path A: HKLM\SOFTWARE\dotnet\shared\Microsoft.WindowsDesktop.App\10.x.y
|
||||
private static bool IsWindowsDesktopSharedFramework10Plus()
|
||||
{
|
||||
using RegistryKey? base64 = OpenLocalMachine64();
|
||||
using RegistryKey? key = base64?.OpenSubKey(@"SOFTWARE\dotnet\shared\Microsoft.WindowsDesktop.App");
|
||||
if (key is null)
|
||||
return false;
|
||||
|
||||
foreach (string name in key.GetSubKeyNames())
|
||||
{
|
||||
if (ParseMajorFromVersionFolder(name) >= 10)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Path B: sharedhost Version 10+ under Setup\InstalledVersions\x64\sharedhost
|
||||
private static bool IsSharedHostVersion10PlusX64()
|
||||
{
|
||||
if (ReadSharedHostMajor(@"SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedhost") >= 10)
|
||||
return true;
|
||||
if (ReadSharedHostMajor(@"SOFTWARE\Microsoft\dotnet\Setup\InstalledVersions\x64\sharedhost") >= 10)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int ReadSharedHostMajor(string subKeyPath)
|
||||
{
|
||||
using RegistryKey? base64 = OpenLocalMachine64();
|
||||
using RegistryKey? key = base64?.OpenSubKey(subKeyPath);
|
||||
if (key is null)
|
||||
return 0;
|
||||
string? v = key.GetValue("Version") as string;
|
||||
if (string.IsNullOrWhiteSpace(v))
|
||||
return 0;
|
||||
return ParseMajorFromVersionFolder(v.Trim());
|
||||
}
|
||||
|
||||
// Path C: InstalledManifests x64, subkeys like ...Manifest-10.0.100
|
||||
private static bool IsDotNet10BandInInstalledManifestsX64()
|
||||
{
|
||||
using RegistryKey? base64 = OpenLocalMachine64();
|
||||
using RegistryKey? key = base64?.OpenSubKey(@"SOFTWARE\Microsoft\dotnet\InstalledManifests\x64");
|
||||
if (key is null)
|
||||
return false;
|
||||
|
||||
return key.GetSubKeyNames().Any(s => s != null
|
||||
&& s.IndexOf("Manifest-10.", StringComparison.OrdinalIgnoreCase) >= 0);
|
||||
}
|
||||
|
||||
private static RegistryKey? OpenLocalMachine64() =>
|
||||
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
|
||||
|
||||
/// <summary>First numeric segment of a "10.0.5" style folder or version string (Pascal: Copy before first '.').</summary>
|
||||
private static int ParseMajorFromVersionFolder(string name)
|
||||
{
|
||||
int dot = name.IndexOf('.');
|
||||
if (dot <= 0)
|
||||
return 0;
|
||||
return int.TryParse(
|
||||
name.AsSpan(0, dot),
|
||||
NumberStyles.Integer,
|
||||
CultureInfo.InvariantCulture,
|
||||
out int m)
|
||||
? m
|
||||
: 0;
|
||||
}
|
||||
}
|
||||
10
src/RynthCore.DesktopLog/RynthCore.DesktopLog.csproj
Normal file
10
src/RynthCore.DesktopLog/RynthCore.DesktopLog.csproj
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>RynthCore</RootNamespace>
|
||||
<AssemblyName>RynthCore.DesktopLog</AssemblyName>
|
||||
<Version>1.0.1</Version>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
Loading…
Reference in a new issue