Make some stuff safer maybe.

This commit is contained in:
Ottermandias 2024-03-20 22:38:39 +01:00
parent fda77b49cd
commit 978f41a4d9
3 changed files with 67 additions and 56 deletions

View file

@ -123,24 +123,24 @@ public class MemoryMappedBuffer : IDisposable
protected static int WriteString(string text, Span<byte> span) protected static int WriteString(string text, Span<byte> span)
{ {
var bytes = Encoding.UTF8.GetBytes(text); var bytes = Encoding.UTF8.GetBytes(text);
var length = bytes.Length + 1; var source = (Span<byte>)bytes;
var length = source.Length + 1;
if (length > span.Length) if (length > span.Length)
throw new Exception($"String {text} is too long to write into span."); source = source[..(span.Length - 1)];
source.CopyTo(span);
bytes.CopyTo(span);
span[bytes.Length] = 0; span[bytes.Length] = 0;
return length; return source.Length + 1;
} }
protected static int WriteSpan(ReadOnlySpan<byte> input, Span<byte> span) protected static int WriteSpan(ReadOnlySpan<byte> input, Span<byte> span)
{ {
var length = input.Length + 1; var length = input.Length + 1;
if (length > span.Length) if (length > span.Length)
throw new Exception("Byte array is too long to write into span."); input = input[..(span.Length - 1)];
input.CopyTo(span); input.CopyTo(span);
span[input.Length] = 0; span[input.Length] = 0;
return length; return input.Length + 1;
} }
protected Span<byte> GetLine(int i) protected Span<byte> GetLine(int i)
@ -150,7 +150,7 @@ public class MemoryMappedBuffer : IDisposable
lock (_header) lock (_header)
{ {
var lineIdx = CurrentLinePosition + i & _lineMask; var lineIdx = (CurrentLinePosition + i) & _lineMask;
if (lineIdx > CurrentLineCount) if (lineIdx > CurrentLineCount)
return null; return null;
@ -169,7 +169,7 @@ public class MemoryMappedBuffer : IDisposable
{ {
var currentLinePos = CurrentLinePosition; var currentLinePos = CurrentLinePosition;
view = _lines[currentLinePos]!; view = _lines[currentLinePos]!;
CurrentLinePosition = currentLinePos + 1 & _lineMask; CurrentLinePosition = (currentLinePos + 1) & _lineMask;
} }
else else
{ {

View file

@ -14,9 +14,18 @@ public class CrashHandler
{ {
using var reader = new GameEventLogReader(); using var reader = new GameEventLogReader();
var parent = Process.GetProcessById(pid); var parent = Process.GetProcessById(pid);
using var handle = parent.SafeHandle;
parent.WaitForExit(); parent.WaitForExit();
var exitCode = parent.ExitCode; int exitCode;
try
{
exitCode = parent.ExitCode;
}
catch (Exception ex)
{
exitCode = -1;
}
var obj = reader.Dump("Crash", pid, exitCode, args[2], args[3]); var obj = reader.Dump("Crash", pid, exitCode, args[2], args[3]);
using var fs = File.Open(args[0], FileMode.Create); using var fs = File.Open(args[0], FileMode.Create);
using var w = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true }); using var w = new Utf8JsonWriter(fs, new JsonWriterOptions { Indented = true });

View file

@ -21,7 +21,15 @@ public class ValidityChecker : IService
public readonly string Version; public readonly string Version;
public readonly string CommitHash; public readonly string CommitHash;
public readonly string GameVersion;
public unsafe string GameVersion
{
get
{
var framework = Framework.Instance();
return framework == null ? string.Empty : framework->GameVersion[0];
}
}
public ValidityChecker(DalamudPluginInterface pi) public ValidityChecker(DalamudPluginInterface pi)
{ {
@ -32,12 +40,8 @@ public class ValidityChecker : IService
var assembly = GetType().Assembly; var assembly = GetType().Assembly;
Version = assembly.GetName().Version?.ToString() ?? string.Empty; Version = assembly.GetName().Version?.ToString() ?? string.Empty;
CommitHash = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown"; CommitHash = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown";
GameVersion = GetGameVersion();
} }
private static unsafe string GetGameVersion()
=> Framework.Instance()->GameVersion[0];
public void LogExceptions() public void LogExceptions()
{ {
if (ImcExceptions.Count > 0) if (ImcExceptions.Count > 0)
@ -49,16 +53,16 @@ public class ValidityChecker : IService
private static bool CheckDevPluginPenumbra(DalamudPluginInterface pi) private static bool CheckDevPluginPenumbra(DalamudPluginInterface pi)
{ {
#if !DEBUG #if !DEBUG
var path = Path.Combine( pi.DalamudAssetDirectory.Parent?.FullName ?? "INVALIDPATH", "devPlugins", "Penumbra" ); var path = Path.Combine(pi.DalamudAssetDirectory.Parent?.FullName ?? "INVALIDPATH", "devPlugins", "Penumbra");
var dir = new DirectoryInfo( path ); var dir = new DirectoryInfo(path);
try try
{ {
return dir.Exists && dir.EnumerateFiles( "*.dll", SearchOption.AllDirectories ).Any(); return dir.Exists && dir.EnumerateFiles("*.dll", SearchOption.AllDirectories).Any();
} }
catch( Exception e ) catch (Exception e)
{ {
Penumbra.Log.Error( $"Could not check for dev plugin Penumbra:\n{e}" ); Penumbra.Log.Error($"Could not check for dev plugin Penumbra:\n{e}");
return true; return true;
} }
#else #else
@ -71,11 +75,9 @@ public class ValidityChecker : IService
{ {
#if !DEBUG #if !DEBUG
var checkedDirectory = pi.AssemblyLocation.Directory?.Parent?.Parent?.Name; var checkedDirectory = pi.AssemblyLocation.Directory?.Parent?.Parent?.Name;
var ret = checkedDirectory?.Equals( "installedPlugins", StringComparison.OrdinalIgnoreCase ) ?? false; var ret = checkedDirectory?.Equals("installedPlugins", StringComparison.OrdinalIgnoreCase) ?? false;
if( !ret ) if (!ret)
{ Penumbra.Log.Error($"Penumbra is not correctly installed. Application loaded from \"{pi.AssemblyLocation.Directory!.FullName}\".");
Penumbra.Log.Error( $"Penumbra is not correctly installed. Application loaded from \"{pi.AssemblyLocation.Directory!.FullName}\"." );
}
return !ret; return !ret;
#else #else