Skip to content

Commit

Permalink
add notifications, add defaults, fun
Browse files Browse the repository at this point in the history
  • Loading branch information
factubsio committed Dec 22, 2021
1 parent d080c94 commit d6f51fb
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 31 deletions.
7 changes: 7 additions & 0 deletions BlueprintExplorer/BPFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public enum ChunkTypes
Blueprints,
Strings,
TypeNames,
ComponentNames,
Defaults,
}

public struct ChunkSubTypes
Expand Down Expand Up @@ -205,7 +207,12 @@ public ReadOnlyMemory<byte> OpenRaw(Chunk chunk)

public BinaryReader Open(Chunk chunk)
{
if (chunk == null)
return null;
var raw = OpenRaw(chunk);
if (raw.IsEmpty)
return null;

if (MemoryMarshal.TryGetArray(raw, out var array))
return new(new MemoryStream(array.Array, array.Offset, array.Count));
throw new Exception("WTF???");
Expand Down
54 changes: 48 additions & 6 deletions BlueprintExplorer/BlueprintControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class RowElement
public bool Visible = true;
public bool Collapsed = false;
public RowElement Parent;
public string TypeFull;
public string String;
public List<string> Lines;
public List<RowElement> Children = new();
Expand All @@ -93,6 +94,7 @@ public class RowElement
internal bool PreviewHover;
private string _Path;
public string Type;
internal string Default;

internal void AllChildren(Action<RowElement> p)
{
Expand Down Expand Up @@ -201,6 +203,7 @@ public BlueprintControl()
private void UpdateRowHoverColor()
{
RowHoverColor = new(ControlPaint.Dark(BackColor, -0.4f));
RowLineGuide = new(ControlPaint.Light(BackColor, 0.1f), 2);
RowPreviewHoverColor = new(ControlPaint.Dark(BackColor, -0.41f));
}

Expand Down Expand Up @@ -273,7 +276,6 @@ private void ValidateBlueprint()
int totalRows = 0;
if (blueprint != null)
{

Elements.Add(new ()
{
key = "Blueprint ID",
Expand Down Expand Up @@ -323,15 +325,23 @@ private void ValidateBlueprint()
Collapsed = totalRows != 0 && !Properties.Settings.Default.EagerExpand,
};

if (row.key == "$type" && row.Parent != null)
if (e.isObj && e.Node.TryGetProperty("$type", out var rawType))
{
var (typeGuid, typeName, _) = row.value.NewTypeStr();
var (typeGuid, typeName, typeNameFull) = rawType.NewTypeStr();
List<StyledString.StyleSpan> spans = new();
spans.Add(new(typeName + " ", StyleFlags.Bold));
spans.Add(new("typeId: " + typeGuid));
row.Parent.ValueStyled = new(spans);
row.Parent.Type = typeName;
row.ValueStyled = new(spans);
row.Type = typeName;
row.TypeFull = typeNameFull;
}

if (row.key == "$type" && row.Parent != null)
continue;

if (e.levelDelta == 0 && row.Parent != null)
{
row.Default = BlueprintDB.DefaultForField(row.Parent?.TypeFull, e.key);
}

if (row.String != null)
Expand Down Expand Up @@ -493,12 +503,28 @@ private void DrawElement(int row, DrawParams render)
}
if (elem.PrimaryRow == row)
{
float keyWidth = render.Graphics.MeasureString(elem.key, render.Bold).Width;
render.Graphics.DrawString(elem.key, render.Bold, new SolidBrush(ForeColor), new PointF(xOffset, 0));
float lineY = RowHeight / 2.0f;
render.Graphics.DrawLine(RowLineGuide, xOffset + keyWidth + 3, lineY, NameColumnWidth - 3, lineY);
if (elem.String == null)
{
bool empty = false;
float right = NameColumnWidth;
var brush = new SolidBrush(valueColor);
if (elem.ValueStyled == null)
render.Graphics.DrawString(elem.value + extra, valueFont, brush, new PointF(NameColumnWidth, 0));
{
var str = elem.value + extra;
if (str.Length > 0)
{
right += render.Graphics.MeasureString(str, valueFont).Width;
render.Graphics.DrawString(str, valueFont, brush, new PointF(NameColumnWidth, 0));
}
else
{
empty = true;
}
}
else
{
PointF p = new(NameColumnWidth, 0);
Expand All @@ -511,6 +537,21 @@ private void DrawElement(int row, DrawParams render)
render.Graphics.DrawString(span.Value, font, brush, p);
p.X += width;
}

right = p.X;
}


if (elem.Default != null)
{
if (!empty)
{
right += 64;
if (right < NameColumnWidth + 400)
right = NameColumnWidth + 400;
}

render.Graphics.DrawString("[default: " + elem.Default + "]", Font, Brushes.Gray, new PointF(right, 0));
}
}
}
Expand Down Expand Up @@ -740,6 +781,7 @@ protected override void OnPaint(PaintEventArgs e)

public Font LinkFont { get => linkFont ?? Font; set => linkFont = value; }
private SolidBrush RowHoverColor;
private Pen RowLineGuide;
private SolidBrush RowPreviewHoverColor;

public class DrawParams
Expand Down
106 changes: 84 additions & 22 deletions BlueprintExplorer/BlueprintDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ namespace BlueprintExplorer
{
public partial class BlueprintDB
{

#region DEV
bool generateOutput = false;
bool importNew = false;
bool forceLastKnown = false;
#endregion

private static BlueprintDB _Instance;
public static BlueprintDB Instance => _Instance ??= new();
public readonly Dictionary<string, string> Strings = new();
Expand Down Expand Up @@ -87,12 +94,13 @@ public int CompareTo(GameVersion other)

public List<GameVersion> Available = new() { };

private readonly GameVersion LastKnown = new(1, 1, 4, 'f', 0);
private readonly GameVersion LastKnown = new(1, 1, 6, 'e', 0);

private readonly string filenameRoot = "blueprints_raw";
private readonly string extension = "binz";

public string FileName => $"{filenameRoot}_{Latest}.{extension}";
private string FileNameFor(GameVersion version) => $"{filenameRoot}_{version}.{extension}";
public string FileName => FileNameFor(Latest);

public bool InCache => File.Exists(Path.Combine(CacheDir, FileName));

Expand All @@ -116,7 +124,6 @@ public GoingToLoad GetLoadType()

if (!AvailableDetected)
{
var last = Properties.Settings.Default.LastLoaded;
bool fromWeb = false;
if (Properties.Settings.Default.CheckForNewBP)
{
Expand Down Expand Up @@ -146,6 +153,7 @@ public GoingToLoad GetLoadType()

if (!fromWeb)
{
var last = Properties.Settings.Default.LastLoaded;
if (!string.IsNullOrWhiteSpace(last))
{
Console.WriteLine("setting available = last loaded");
Expand Down Expand Up @@ -182,14 +190,18 @@ public GoingToLoad GetLoadType()

}

#region DEV
bool generateOutput = false;
bool importNew = false;
bool forceLastKnown = false;
#endregion

public string[] ComponentTypeLookup;

private static Dictionary<string, Dictionary<string, string>> defaults = new();
public static string DefaultForField(string typename, string field)
{
if (typename == null || !defaults.TryGetValue(typename, out var map))
return null;
if (!map.TryGetValue(field, out var value))
value = null;
return value;
}

public class ConnectionProgress
{
public int Current;
Expand Down Expand Up @@ -228,6 +240,11 @@ public async Task<bool> TryConnect(ConnectionProgress progress)

using var bpDump = ZipFile.OpenRead(@"D:\WOTR-1.1-DEBUG\blueprints.zip");

if (File.Exists(@"D:\bp_defaults.json"))
{
defaults = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(File.ReadAllText(@"D:\bp_defaults.json"));
}

Dictionary<string, string> TypenameToGuid = new();

progress.EstimatedTotal = bpDump.Entries.Count(e => e.Name.EndsWith(".jbp"));
Expand Down Expand Up @@ -277,6 +294,7 @@ public async Task<bool> TryConnect(ConnectionProgress progress)

Console.WriteLine("COMPLETE, press a key");
Console.ReadKey();
Console.WriteLine("Continuing");
}
else
{
Expand Down Expand Up @@ -335,7 +353,9 @@ public async Task<bool> TryConnect(ConnectionProgress progress)

Console.WriteLine($"Reading {count} blueprints for Wrath: {header}");

foreach (var bundle in reader.Handle.GetChunks((ushort)ChunkTypes.Blueprints))
var blueprintBundles = reader.Handle.GetChunks((ushort)ChunkTypes.Blueprints);

foreach (var bundle in blueprintBundles)
{
var task = Task.Run<List<BlueprintHandle>>(() =>
{
Expand All @@ -360,7 +380,10 @@ public async Task<bool> TryConnect(ConnectionProgress progress)
}

byte[] guid_cache = new byte[16];
var refs = bundleContext.Open(bundle.ForSubType((ushort)ChunkSubTypes.Blueprints.References));
ushort refId = (ushort)ChunkSubTypes.Blueprints.References;
//if (header.Major == 1 && header.Minor == 1 && header.Patch < 6)
// refId = (ushort)ChunkSubTypes.Blueprints.Components;
var refs = bundleContext.Open(bundle.ForSubType(refId));

for (int i = 0; i < res.Count; i++)
{
Expand All @@ -375,6 +398,7 @@ public async Task<bool> TryConnect(ConnectionProgress progress)

return res;
});
task.Wait();
tasks.Add(task);
}

Expand All @@ -392,8 +416,30 @@ public async Task<bool> TryConnect(ConnectionProgress progress)
var strings = ctx.OpenRaw(reader.Get((ushort)ChunkTypes.Strings).Main);

var stringDictRaw = JsonSerializer.Deserialize<JsonElement>(strings.Span).GetProperty("strings");
foreach (var kv in stringDictRaw.EnumerateObject())
Strings[kv.Name] = kv.Value.GetString();
foreach (var kv in stringDictRaw.EnumerateObject())
{
Strings[kv.Name] = kv.Value.GetString();
}

var defs = ctx.Open(reader.Get((ushort)ChunkTypes.Defaults)?.Main);
if (defs != null)
{
int defCount = defs.ReadInt32();
for (int i = 0; i < defCount; i++)
{
string key = defs.ReadString();
int subCount = defs.ReadInt32();
var map = new Dictionary<string, string>();
for (int s = 0; s < subCount; s++)
{
string subKey = defs.ReadString();
string subVal = defs.ReadString();
map[subKey] = subVal;
}
defaults[key] = map;
}

}
});

float loadTime = watch.ElapsedMilliseconds;
Expand All @@ -420,6 +466,8 @@ public async Task<bool> TryConnect(ConnectionProgress progress)
#pragma warning disable CS0162 // Unreachable code detected
try
{
Console.WriteLine("Generating");
File.WriteAllLines(@"D:\bp_types.txt", GuidToFullTypeName.Select(kv => $"{kv.Key} {kv.Value}"));
WriteBlueprints();
}
catch (Exception ex)
Expand Down Expand Up @@ -467,15 +515,14 @@ private void WriteBlueprints()

Dictionary<string, UInt16> uniqueComponents = new();


using (var file = new BPFile.BPWriter("NEW_" + FileName))
using (var file = new BPFile.BPWriter("NEW_" + FileNameFor(LastKnown)))
{
using (var header = file.Begin((ushort)ChunkTypes.Header))
{
header.Stream.Write(1);
header.Stream.Write(1);
header.Stream.Write(4);
header.Stream.Write('d');
header.Stream.Write(LastKnown.Major);
header.Stream.Write(LastKnown.Minor);
header.Stream.Write(LastKnown.Patch);
header.Stream.Write(LastKnown.Suffix);
header.Stream.Write(cache.Count);
}

Expand All @@ -500,7 +547,7 @@ private void WriteBlueprints()
current.Stream.Write(c.Type);
current.Stream.Write(c.Raw);

var refs = current.GetStream((ushort)ChunkSubTypes.Blueprints.Components);
var refs = current.GetStream((ushort)ChunkSubTypes.Blueprints.References);
if (References.TryGetValue(Guid.Parse(c.GuidText), out var refList))
{
refs.Write(refList.Count);
Expand All @@ -517,7 +564,7 @@ private void WriteBlueprints()
refs.Write(0);
}

var comps = current.GetStream((ushort)ChunkSubTypes.Blueprints.References);
var comps = current.GetStream((ushort)ChunkSubTypes.Blueprints.Components);
comps.Write(components.Count);
foreach (var componentType in components)
{
Expand Down Expand Up @@ -547,7 +594,7 @@ private void WriteBlueprints()
}
}

using (var comps = file.Begin((ushort)ChunkTypes.TypeNames))
using (var comps = file.Begin((ushort)ChunkTypes.ComponentNames))
{
comps.Stream.Write(uniqueComponents.Count);
foreach (var kv in uniqueComponents)
Expand All @@ -557,6 +604,21 @@ private void WriteBlueprints()
}
}

using (var chunk = file.Begin((ushort)ChunkTypes.Defaults))
{
chunk.Stream.Write(defaults.Count);
foreach (var kv in defaults)
{
chunk.Stream.Write(kv.Key);
chunk.Stream.Write(kv.Value.Count);
foreach (var sub in kv.Value)
{
chunk.Stream.Write(sub.Key);
chunk.Stream.Write(sub.Value);
}
}
}

}
Console.WriteLine($"biggestString: {biggestString}, biggestStringZ: {biggestStringZ}, mostReferred: {mostReferred} ({biggestRefList})");
}
Expand Down
7 changes: 6 additions & 1 deletion BlueprintExplorer/BlueprintHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ public static (string Guid, string Name, string FullName) NewTypeStr(this string

public static (string Guid, string Name, string FullName) NewTypeStr(this JsonElement elem, bool strict = true)
{
return elem.Str("$type").NewTypeStr(strict);
if (elem.ValueKind == JsonValueKind.String)
return elem.GetString().NewTypeStr();
else if (elem.ValueKind == JsonValueKind.Object)
return elem.Str("$type").NewTypeStr(strict);
else
throw new Exception("invalid type query??");
}

public static bool True(this JsonElement elem, string child)
Expand Down
Loading

0 comments on commit d6f51fb

Please sign in to comment.