Skip to content

Commit

Permalink
Added OS information
Browse files Browse the repository at this point in the history
Added native modules information
Using SequenceEqual for lists
Fixed Windows/ProgramData/Program Files anonymization
  • Loading branch information
Aragas committed Sep 21, 2024
1 parent 6df686b commit 5610748
Show file tree
Hide file tree
Showing 50 changed files with 793 additions and 43 deletions.
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!--Development Variables-->
<PropertyGroup>
<GITHUB_RUN_NUMBER Condition="$(GITHUB_RUN_NUMBER) == ''">7</GITHUB_RUN_NUMBER>
<Version>13.0.0.$(GITHUB_RUN_NUMBER)</Version>
<Version>14.0.0.$(GITHUB_RUN_NUMBER)</Version>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>

Expand Down
5 changes: 4 additions & 1 deletion src/BUTR.CrashReport.Bannerlord.Parser/CrashReportParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ private static CrashReportModel ParseLegacyHtmlInternal(byte version, HtmlDocume
LauncherType = launcherType,
LauncherVersion = launcherVersion,
Runtime = runtime,
OperatingSystemType = OperatingSystemType.Unknown,
OperatingSystemVersion = null,
AdditionalMetadata = new List<MetadataModel>
{
new() { Key = "LauncherExVersion", Value = launcherexVersion },
Expand All @@ -257,6 +259,7 @@ private static CrashReportModel ParseLegacyHtmlInternal(byte version, HtmlDocume
InvolvedModules = involvedModules,
EnhancedStacktrace = enhancedStacktrace,
Assemblies = assemblies,
NativeModules = Array.Empty<NativeAssemblyModel>(),
HarmonyPatches = harmonyPatches,
//MonoModDetours = Array.Empty<MonoModDetoursModel>(),
LoaderPlugins = Array.Empty<LoaderPluginModel>(),
Expand Down Expand Up @@ -483,7 +486,7 @@ private static AssemblyModel ParseAssembly(HtmlNode node, ModuleModel[] modules)
ModuleId = module?.Id,
LoaderPluginId = null,
CultureName = null,
Architecture = splt[2],
Architecture = (AssemblyArchitectureType) Enum.Parse(typeof(AssemblyArchitectureType), splt[2], true),
Hash = isDynamic || isEmpty ? string.Empty : splt[3],
AnonymizedPath = isDynamic ? "DYNAMIC" : isEmpty ? "EMPTY" : Anonymizer.AnonymizePath(splt[4]),

Expand Down
146 changes: 146 additions & 0 deletions src/BUTR.CrashReport.Bannerlord.Source/CrashReportCreatorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ namespace BUTR.CrashReport.Bannerlord
using global::System.IO;
using global::System.Linq;
using global::System.Reflection;
using global::System.Runtime.InteropServices;
using global::System.Xml.Linq;

internal class CrashReportInfoHelper :
IAssemblyUtilities,
Expand Down Expand Up @@ -127,10 +129,154 @@ public virtual CrashReportMetadataModel GetCrashReportMetadataModel(CrashReportI
LauncherVersion = GetLauncherVersion(crashReport),

Runtime = null,

OperatingSystemType = GetOperatingSystemType(),
OperatingSystemVersion = GetOSVersion(),

AdditionalMetadata = additionalMetdata,
};
}

private OperatingSystemType GetOperatingSystemType()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return OperatingSystemType.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return OperatingSystemType.Linux;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return OperatingSystemType.MacOS;
return OperatingSystemType.Unknown;
}

private string? GetOSVersion()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return GetOSVersionWindows();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return GetOSVersionLinux();
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return GetOSXVersion();
return null;
}


[DllImport("ntdll.dll", SetLastError = true)]
private static extern int RtlGetVersion(out RTL_OSVERSIONINFOEX lpVersionInformation);
[StructLayout(LayoutKind.Sequential)]
private struct RTL_OSVERSIONINFOEX
{
internal uint dwOSVersionInfoSize;
internal uint dwMajorVersion;
internal uint dwMinorVersion;
internal uint dwBuildNumber;
internal uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
internal string szCSDVersion;
}
private string GetOSVersionWindows()
{
var osName = "Windows";
var osVersion = string.Empty;

var osvi = new RTL_OSVERSIONINFOEX();
osvi.dwOSVersionInfoSize = (uint) Marshal.SizeOf(osvi);
if (RtlGetVersion(out osvi) == 0)
osVersion = $"{osvi.dwMajorVersion}.{osvi.dwMinorVersion}.{osvi.dwBuildNumber}";

return $"{osName} {osVersion}";
}

private static string GetOSVersionLinux()
{
var osName = string.Empty;
var osVersion = string.Empty;

var filePath = string.Empty;

try
{
if (File.Exists("/usr/lib/os-release"))
filePath = "/usr/lib/os-release";
}
catch { /* ignored */ }

try
{
if (File.Exists("/etc/os-release"))
filePath = "/etc/os-release";
}
catch { /* ignored */ }

try
{
if (File.Exists("/etc/lsb-release"))
filePath = "/etc/lsb-release";
}
catch { /* ignored */ }

if (string.IsNullOrEmpty(filePath))
return "UNKNOWN LINUX";

try
{
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (var reader = new StreamReader(fileStream))
{
string? line;
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("NAME="))
osName = line.Split('=')[1].Trim('"');
if (line.StartsWith("DISTRIB_ID="))
osName = line.Split('=')[1].Trim('"');

if (line.StartsWith("VERSION_ID="))
osVersion = line.Split('=')[1].Trim('"');
if (line.StartsWith("DISTRIB_RELEASE="))
osVersion = line.Split('=')[1].Trim('"');
}
}
}
}
catch
{
return "UNKNOWN LINUX";
}

return $"{osName} {osVersion}";
}

private static string? GetOSXVersion()
{
try
{
if (!File.Exists("/System/Library/CoreServices/SystemVersion.plist"))
return null;
}
catch { /* ignored */ }

try
{
using (var fileStream = new FileStream("/System/Library/CoreServices/SystemVersion.plist", FileMode.Open, FileAccess.Read))
{
using (var reader = new StreamReader(fileStream))
{
var systemVersionFile = XDocument.Load(reader);
var parsedSystemVersionFile = systemVersionFile.Descendants("dict")
.SelectMany(d => d.Elements("key").Zip(d.Elements().Where(e => e.Name != "key"), (k, v) => new { Key = k, Value = v }))
.ToDictionary(i => i.Key.Value, i => i.Value.Value);
var productName = parsedSystemVersionFile.ContainsKey("ProductName") ? parsedSystemVersionFile["ProductName"] : null;
var productVersion = parsedSystemVersionFile.ContainsKey("ProductVersion") ? parsedSystemVersionFile["ProductVersion"] : null;
return $"{productName} {productVersion}";
}
}
}
catch
{
return null;
}
}


public virtual IEnumerable<Assembly> Assemblies() => AccessTools2.AllAssemblies();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Iced" Version="1.21.0" Alias="iced" Private="false" />
<PackageReference Include="AsmResolver.DotNet.Dynamic" Version="6.0.0-beta.1" />
<PackageReference Include="ICSharpCode.Decompiler" Version="9.0.0.7618-preview1" />
<PackageReference Include="ICSharpCode.Decompiler" Version="9.0.0.7660-preview2" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/BUTR.CrashReport.Decompilers/Utils/MethodCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;

using TypeAttributes = AsmResolver.PE.DotNet.Metadata.Tables.TypeAttributes;

namespace BUTR.CrashReport.Decompilers.Utils;
Expand Down
4 changes: 3 additions & 1 deletion src/BUTR.CrashReport.Models/AssemblyIdModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public bool Equals(AssemblyIdModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name && Version == other.Version && PublicKeyToken == other.PublicKeyToken;
return Name == other.Name &&
Version == other.Version &&
PublicKeyToken == other.PublicKeyToken;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public bool Equals(AssemblyImportedReferenceModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name && Version == other.Version && Culture == other.Culture && PublicKeyToken == other.PublicKeyToken;
return Name == other.Name &&
Version == other.Version &&
Culture == other.Culture &&
PublicKeyToken == other.PublicKeyToken;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public bool Equals(AssemblyImportedTypeReferenceModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name && Namespace == other.Namespace && FullName == other.FullName;
return Name == other.Name &&
Namespace == other.Namespace &&
FullName == other.FullName;
}

/// <inheritdoc />
Expand Down
15 changes: 13 additions & 2 deletions src/BUTR.CrashReport.Models/AssemblyModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;

namespace BUTR.CrashReport.Models;

Expand Down Expand Up @@ -34,7 +35,7 @@ public sealed record AssemblyModel
/// <inheritdoc cref="System.Reflection.AssemblyName.ProcessorArchitecture"/>
/// </summary>
/// <returns><inheritdoc cref="System.Reflection.AssemblyName.ProcessorArchitecture"/></returns>
public required string Architecture { get; set; }
public required AssemblyArchitectureType Architecture { get; set; }

/// <summary>
/// The hash of the assembly.
Expand Down Expand Up @@ -72,7 +73,17 @@ public bool Equals(AssemblyModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return ModuleId == other.ModuleId && LoaderPluginId == other.LoaderPluginId && Id.Equals(other.Id) && CultureName == other.CultureName && Architecture == other.Architecture && Hash == other.Hash && AnonymizedPath == other.AnonymizedPath && Type == other.Type && ImportedTypeReferences.Equals(other.ImportedTypeReferences) && ImportedAssemblyReferences.Equals(other.ImportedAssemblyReferences) && AdditionalMetadata.Equals(other.AdditionalMetadata);
return ModuleId == other.ModuleId &&
LoaderPluginId == other.LoaderPluginId &&
Id.Equals(other.Id) &&
CultureName == other.CultureName &&
Architecture == other.Architecture &&
Hash == other.Hash &&
AnonymizedPath == other.AnonymizedPath &&
Type == other.Type &&
ImportedTypeReferences.SequenceEqual(other.ImportedTypeReferences) &&
ImportedAssemblyReferences.SequenceEqual(other.ImportedAssemblyReferences) &&
AdditionalMetadata.SequenceEqual(other.AdditionalMetadata);
}

/// <inheritdoc />
Expand Down
3 changes: 2 additions & 1 deletion src/BUTR.CrashReport.Models/CapabilityModuleOrPluginModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public bool Equals(CapabilityModuleOrPluginModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name && Description == other.Description;
return Name == other.Name &&
Description == other.Description;
}

/// <inheritdoc />
Expand Down
20 changes: 19 additions & 1 deletion src/BUTR.CrashReport.Models/CrashReportMetadataModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;

namespace BUTR.CrashReport.Models;

Expand Down Expand Up @@ -42,6 +43,16 @@ public sealed record CrashReportMetadataModel
/// </summary>
public required string? Runtime { get; set; }

/// <summary>
/// The operating system that was used to launch the game.
/// </summary>
public required OperatingSystemType OperatingSystemType { get; set; }

/// <summary>
/// The operating system that was used to launch the game.
/// </summary>
public required string? OperatingSystemVersion { get; set; }

/// <summary>
/// <inheritdoc cref="CrashReportModel.AdditionalMetadata"/>
/// </summary>
Expand All @@ -53,7 +64,14 @@ public bool Equals(CrashReportMetadataModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return GameName == other.GameName && GameVersion == other.GameVersion && LoaderPluginProviderName == other.LoaderPluginProviderName && LoaderPluginProviderVersion == other.LoaderPluginProviderVersion && LauncherType == other.LauncherType && LauncherVersion == other.LauncherVersion && Runtime == other.Runtime && AdditionalMetadata.Equals(other.AdditionalMetadata);
return GameName == other.GameName &&
GameVersion == other.GameVersion &&
LoaderPluginProviderName == other.LoaderPluginProviderName &&
LoaderPluginProviderVersion == other.LoaderPluginProviderVersion &&
LauncherType == other.LauncherType &&
LauncherVersion == other.LauncherVersion &&
Runtime == other.Runtime &&
AdditionalMetadata.SequenceEqual(other.AdditionalMetadata);
}

/// <inheritdoc />
Expand Down
19 changes: 18 additions & 1 deletion src/BUTR.CrashReport.Models/CrashReportModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace BUTR.CrashReport.Models;

Expand Down Expand Up @@ -48,6 +49,11 @@ public sealed record CrashReportModel
/// </summary>
public required IList<AssemblyModel> Assemblies { get; set; } = new List<AssemblyModel>();

/// <summary>
/// The list of native modules that are present.
/// </summary>
public required IList<NativeAssemblyModel> NativeModules { get; set; } = new List<NativeAssemblyModel>();


/*
/// <summary>
Expand Down Expand Up @@ -83,7 +89,18 @@ public bool Equals(CrashReportModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Id.Equals(other.Id) && Version == other.Version && Exception.Equals(other.Exception) && Metadata.Equals(other.Metadata) && Modules.Equals(other.Modules) && InvolvedModules.Equals(other.InvolvedModules) && EnhancedStacktrace.Equals(other.EnhancedStacktrace) && Assemblies.Equals(other.Assemblies) && HarmonyPatches.Equals(other.HarmonyPatches) && LoaderPlugins.Equals(other.LoaderPlugins) && InvolvedLoaderPlugins.Equals(other.InvolvedLoaderPlugins) && AdditionalMetadata.Equals(other.AdditionalMetadata);
return Id.Equals(other.Id) &&
Version == other.Version &&
Exception.Equals(other.Exception) &&
Metadata.Equals(other.Metadata) &&
Modules.SequenceEqual(other.Modules) &&
InvolvedModules.SequenceEqual(other.InvolvedModules) &&
EnhancedStacktrace.SequenceEqual(other.EnhancedStacktrace) &&
Assemblies.SequenceEqual(other.Assemblies) &&
HarmonyPatches.SequenceEqual(other.HarmonyPatches) &&
LoaderPlugins.SequenceEqual(other.LoaderPlugins) &&
InvolvedLoaderPlugins.SequenceEqual(other.InvolvedLoaderPlugins) &&
AdditionalMetadata.SequenceEqual(other.AdditionalMetadata);
}

/// <inheritdoc />
Expand Down
7 changes: 6 additions & 1 deletion src/BUTR.CrashReport.Models/DependencyMetadataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ public bool Equals(DependencyMetadataModel? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return ModuleOrPluginId == other.ModuleOrPluginId && Type == other.Type && IsOptional == other.IsOptional && Version == other.Version && VersionRange == other.VersionRange && AdditionalMetadata.Equals(other.AdditionalMetadata);
return ModuleOrPluginId == other.ModuleOrPluginId &&
Type == other.Type &&
IsOptional == other.IsOptional &&
Version == other.Version &&
VersionRange == other.VersionRange &&
AdditionalMetadata.Equals(other.AdditionalMetadata);
}

/// <inheritdoc />
Expand Down
Loading

0 comments on commit 5610748

Please sign in to comment.