Skip to content

Commit

Permalink
Using compressed json
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Dec 21, 2023
1 parent 92e90b9 commit 0629669
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
21 changes: 19 additions & 2 deletions src/BUTR.CrashReport.Bannerlord.Parser/CrashReportParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;

namespace BUTR.CrashReport.Bannerlord.Parser;

Expand Down Expand Up @@ -74,7 +77,20 @@ private static HtmlDocument Create(ref string content)
public static string? ParseHtmlJson(string content)
{
var document = Create(ref content);
return document.DocumentNode.SelectSingleNode("descendant::div[@id=\"json-model-data\"]")?.InnerText;
var gzipBase64Json = document.DocumentNode.SelectSingleNode("descendant::div[@id=\"json-model-data\"]")?.InnerText;
if (string.IsNullOrEmpty(gzipBase64Json))
return null;

return DecompressJson(gzipBase64Json!);
}

private static string DecompressJson(string gzipBase64Json)
{
var compressedStream = new MemoryStream(Convert.FromBase64String(gzipBase64Json));
using var decompressorStream = new DeflateStream(compressedStream, CompressionMode.Decompress);
using var decompressedStream = new MemoryStream();
decompressorStream.CopyTo(decompressedStream);
return Encoding.UTF8.GetString(decompressedStream.ToArray());
}

/// <summary>
Expand All @@ -93,7 +109,8 @@ public static bool TryParse(string content, out byte version, out CrashReportMod
case >= 13:
{
crashReportModel = null;
crashReportJson = document.DocumentNode.SelectSingleNode("descendant::div[@id=\"json-model-data\"]")?.InnerText;
var gzipBase64Json = document.DocumentNode.SelectSingleNode("descendant::div[@id=\"json-model-data\"]")?.InnerText;
crashReportJson = DecompressJson(gzipBase64Json!);
return true;
}
default:
Expand Down
51 changes: 37 additions & 14 deletions src/BUTR.CrashReport.Bannerlord.Source/CrashReportHtmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
// SOFTWARE.
#endregion

using System.IO.Compression;

#if !BUTRCRASHREPORT_DISABLE || BUTRCRASHREPORT_ENABLE_HTML_RENDERER
#nullable enable
#if !BUTRCRASHREPORT_ENABLEWARNINGS
Expand Down Expand Up @@ -63,8 +65,8 @@ internal static class CrashReportHtmlRenderer
private static readonly string SaveFileButtonTag = "<!-- SAVE FILE BUTTON -->";
private static readonly string ScreenshotTag = "<!-- SCREENSHOT -->";
private static readonly string ScreenshotButtonTag = "<!-- SCREENSHOT BUTTON -->";
private static readonly string DecompressScriptTag = "<!-- DECOMPRESS SCRIPT -->";
private static readonly string JsonModelDataTag = "<!-- JSON MODEL -->";
private static readonly string JsonModelTag = "<!-- JSON MODEL -->";
private static readonly string JsonModelButtonTag = "<!-- JSON MODEL BUTTON -->";

#pragma warning disable format // @formatter:off
private static readonly string Scripts = """
Expand Down Expand Up @@ -162,11 +164,29 @@ function screenshot(element) {
document.getElementById("screenshot").src = "data:image/jpeg;charset=utf-8;base64," + base64;
document.getElementById("screenshot").parentElement.style.display = "block";
}
function jsonmodel(element) {
var base64 = document.getElementById("json-model-data").innerText.trim();
//var binData = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
var binData = new Uint8Array(
atob(base64)
.split("")
.map(function (x) {
return x.charCodeAt(0);
})
);
var result = window.pako.inflate(binData);
var a = document.createElement("a");
var blob = new Blob([result]);
a.href = window.URL.createObjectURL(blob);
a.download = "crashreport.json";
a.click();
}
</script>
""";
#pragma warning disable format // @formatter:on

public static string AddData(string htmlReport, string crashReportJson, string? gZipBase64MiniDump = null, string? gZipBase64SaveFile = null, string? base64Screenshot = null)
public static string AddData(string htmlReport, string gzipBase64CrashReportJson, string? gZipBase64MiniDump = null, string? gZipBase64SaveFile = null, string? base64Screenshot = null)
{
var IncludeMiniDump = !string.IsNullOrEmpty(gZipBase64MiniDump);
var IncludeSaveFile = !string.IsNullOrEmpty(gZipBase64SaveFile);
Expand Down Expand Up @@ -213,16 +233,17 @@ public static string AddData(string htmlReport, string crashReportJson, string?
<![endif]>
""");
}

htmlReport = htmlReport
.Replace(CrashReportHtmlRenderer.JsonModelTag, gzipBase64CrashReportJson)
.Replace(CrashReportHtmlRenderer.JsonModelButtonTag, """
if (IncludeMiniDump || IncludeSaveFile)
{
htmlReport = htmlReport.Replace(CrashReportHtmlRenderer.DecompressScriptTag, @"
<![if !IE]>
<script src=""https://cdn.jsdelivr.net/pako/1.0.3/pako_inflate.min.js""></script>
<![endif]>");
}

htmlReport = htmlReport.Replace(CrashReportHtmlRenderer.JsonModelDataTag, crashReportJson);
<br/>
<br/>
<button onclick='jsonmodel(this)'>Get Json</button>
<![endif]>
""");

return htmlReport;
}
Expand Down Expand Up @@ -347,7 +368,7 @@ Most likely this error was caused by a custom installed module.
<option value='0.9em'>Medium</option>
<option value='0.8em'>Small</option>
</select>
{{MiniDumpButtonTag}} {{SaveFileButtonTag}} {{ScreenshotButtonTag}}
{{JsonModelButtonTag}} {{MiniDumpButtonTag}} {{SaveFileButtonTag}} {{ScreenshotButtonTag}}
</div>
</td>
</tr>
Expand Down Expand Up @@ -428,10 +449,12 @@ Most likely this error was caused by a custom installed module.
<div class='root-container' style='display: none;'>
<h2><a href='javascript:;' class="headers" onclick='showHideById(this, "json-model-data")'>+ Json Model Data</a></h2>
<div id='json-model-data' class='headers-container'>
{{JsonModelDataTag}}
{{JsonModelTag}}
</div>
</div>
{{DecompressScriptTag}}
<![if !IE]>
<script src="https://cdn.jsdelivr.net/pako/1.0.3/pako_inflate.min.js"></script>
<![endif]>
{{Scripts}}
</body>
</html>
Expand Down
1 change: 0 additions & 1 deletion src/BUTR.CrashReport.Decompilers/ILSpy/CSharpLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public static CSharpDecompiler CreateDecompiler(PEFile module, DecompilerSetting
{
var resolver = new UniversalAssemblyResolver(null, false, module.DetectTargetFrameworkId(), module.DetectRuntimePack());
var decompiler = new CSharpDecompiler(module, resolver, settings) { CancellationToken = ct };

while (decompiler.AstTransforms.Count >= _transformCount)
decompiler.AstTransforms.RemoveAt(decompiler.AstTransforms.Count - 1);
decompiler.AstTransforms.Add(new EscapeInvalidIdentifiers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public static string[] DecompileCSharpCode(MethodBase? method)

var output = new PlainTextOutput2();
var language = new CSharpLanguage();
language.DecompileMethod(method2, output, new DecompilerSettings(LanguageVersion.Preview));
language.DecompileMethod(method2, output, new DecompilerSettings(LanguageVersion.Preview)
{
AggressiveInlining = true,
});

return output.ToString()!.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
}
Expand Down

0 comments on commit 0629669

Please sign in to comment.