Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Wincent01 committed Feb 8, 2020
2 parents 727a764 + 6e6ade7 commit 863112b
Show file tree
Hide file tree
Showing 53 changed files with 1,703 additions and 116 deletions.
19 changes: 10 additions & 9 deletions InfectedRose.Core/Extensions/BitReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System;
using System.Numerics;
using RakDotNet.IO;

Expand All @@ -20,14 +20,6 @@ public static string ReadNiString(this BitReader @this, bool wide = false, bool
return new string(str);
}

public static void Write<T>(this BitWriter @this, ICollection<T> collection) where T : struct
{
foreach (var value in collection)
{
@this.Write(value);
}
}

public static Quaternion ReadNiQuaternion(this BitReader @this)
{
return new Quaternion
Expand All @@ -50,5 +42,14 @@ public static byte[] ReadBuffer(this BitReader @this, uint length)

return buffer;
}

public static T Read<T>(this BitReader @this) where T : IDeserializable
{
var instance = Activator.CreateInstance<T>();

instance.Deserialize(@this);

return instance;
}
}
}
14 changes: 14 additions & 0 deletions InfectedRose.Core/Extensions/BitWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Numerics;
using RakDotNet.IO;

Expand Down Expand Up @@ -43,5 +44,18 @@ public static void Write(this BitWriter @this, byte[] buffer)
@this.Write(value);
}
}

public static void Write<T>(this BitWriter @this, ICollection<T> collection) where T : struct
{
foreach (var value in collection)
{
@this.Write(value);
}
}

public static void Write(this BitWriter @this, ISerializable serializable)
{
serializable.Serialize(@this);
}
}
}
25 changes: 25 additions & 0 deletions InfectedRose.Database/Concepts/Generic/LwoObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace InfectedRose.Database.Concepts.Generic
{
public static class LwoObjectExtensions
{
public static T GetComponent<T>(this LwoObject @this) where T : class
{
var name = typeof(T).Name;

foreach (var component in @this)
{
if (component.Row?.Table?.Name == default) continue;

if ($"{component.Row.Table.Name}Table" != name) continue;

var instance = Activator.CreateInstance(typeof(T), component.Row) as T;

return instance;
}

return default;
}
}
}
5 changes: 2 additions & 3 deletions InfectedRose.Database/Concepts/LwoObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using InfectedRose.Database.Generic;

namespace InfectedRose.Database.Concepts
{
Expand All @@ -27,7 +26,7 @@ private List<LwoComponent> Components

foreach (var entry in Database["ComponentsRegistry"].Where(r => r.Key == Row.Key))
{
var type = (ComponentId) entry.Value<int>("component_type");
var type = (ComponentId) entry["component_type"].Value;

var component = new LwoComponent
{
Expand All @@ -40,7 +39,7 @@ private List<LwoComponent> Components

var componentTable = Database[$"{type}"];

var id = entry.Value<int>("component_id");
var id = (int) entry["component_id"].Value;

if (id == 0 || componentTable == default) continue;

Expand Down
30 changes: 18 additions & 12 deletions InfectedRose.Database/FdbRowInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@ internal class FdbRowInfo : DatabaseData

public override void Deserialize(BitReader reader)
{
using (var s = new DatabaseScope(reader, true))
// Super hacky way of getting around stackoverflow
Task.Run(() =>
{
if (s)
lock (reader)
{
DataHeader = new FdbRowDataHeader();

DataHeader.Deserialize(reader);
using var s = new DatabaseScope(reader, true);

if (s)
{
DataHeader = new FdbRowDataHeader();

DataHeader.Deserialize(reader);
}
}
}

using (var s = new DatabaseScope(reader, true))
{
if (!s) return;
using (var s = new DatabaseScope(reader, true))
{
if (!s) return;

Linked = new FdbRowInfo();
Linked = new FdbRowInfo();

Linked.Deserialize(reader);
}
Linked.Deserialize(reader);
}
}).Wait();
}

public override void Compile(HashMap map)
Expand Down
54 changes: 28 additions & 26 deletions InfectedRose.Database/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal Table(FdbColumnHeader info, FdbRowBucket data, AccessDatabase database)

internal AccessDatabase Database { get; }

public uint Buckets => (uint) Data.RowHeader.RowInfos.Length;

public string Name
{
get => Info.TableName;
Expand Down Expand Up @@ -278,10 +280,7 @@ public Column Create(object key, object values)
return new Column(column, this);
}

public void Recalculate()
{
Recalculate(1);
}
public void Recalculate() => Recalculate(1);

public void Recalculate(int bucketSize)
{
Expand All @@ -300,9 +299,9 @@ public void Recalculate(int bucketSize)
taken.Add(row.Key);
}

var buckets = FdbRowBucket.NextPowerOf2(bucketSize == -1 ? taken.Count : bucketSize);
var buckets = FdbRowBucket.NextPowerOf2(bucketSize == default ? taken.Count : bucketSize);

var final = new FdbRowInfo[buckets];
var hierarchy = new Dictionary<uint, List<FdbRowInfo>>();

var data = this.ToArray();

Expand All @@ -314,36 +313,39 @@ public void Recalculate(int bucketSize)

var key = index % buckets;

FdbRowInfo bucket;

try
if (hierarchy.TryGetValue(key, out var list))
{
bucket = final[key];
list.Add(column.Data);
}
catch (Exception e)
else
{
Console.WriteLine(
$"\n\"[{column[0].Type}] {column[0].Value}\" [{index}] -> {key} / {buckets}\n{e}\n");

Console.ReadLine();

throw;
hierarchy[key] = new List<FdbRowInfo>
{
column.Data
};
}
}

var final = new FdbRowInfo[buckets];

if (bucket != default)
{
var linked = bucket;
foreach (var (key, values) in hierarchy)
{
var root = values[0];

while (linked.Linked != default) linked = linked.Linked;
var current = root;

linked.Linked = column.Data;
}
else
values.RemoveAt(0);

foreach (var value in values)
{
final[index % buckets] = column.Data;
current.Linked = value;

current = value;
}
}

final[key] = root;
}

Data.RowHeader.RowInfos = final.ToArray();
}

Expand Down
45 changes: 45 additions & 0 deletions InfectedRose.Examples/AccessDatabaseCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.IO;
using Eto.Forms;

namespace InfectedRose.Examples
{
public class AccessDatabaseCommand : Command
{
public string Macro = $"{Application.Instance.CommonModifier} + {Keys.O}";

private AccessEditor Editor { get; }

public AccessDatabaseCommand(AccessEditor editor)
{
Editor = editor;

MenuText = "Open...";

ToolBarText = "Open a Database";

ToolTip = "Open a Database of Access type";

Shortcut = Application.Instance.CommonModifier | Keys.O;
}

protected override void OnExecuted(EventArgs e)
{
base.OnExecuted(e);

var dialog = new OpenFileDialog();

dialog.Filters.Add(new FileFilter("*.fdb", ".fdb"));
dialog.Filters.Add(new FileFilter("*"));

dialog.ShowDialog(Editor);

Console.WriteLine(dialog.FileName);

if (File.Exists(dialog.FileName))
{
Editor.OpenDatabase(dialog.FileName);
}
}
}
}
Loading

0 comments on commit 863112b

Please sign in to comment.