Skip to content

Commit

Permalink
Fix bottom log texture + Native AOT support
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueStaggo committed Nov 26, 2024
1 parent 17a53c8 commit b707b0a
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 30 deletions.
4 changes: 3 additions & 1 deletion PDS/src/CompoundItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System.Reflection;
using System.Runtime.CompilerServices;

namespace PDS;

Expand Down Expand Up @@ -56,7 +57,8 @@ public CompoundItem Put(string key, object? value)

public object? Deserialize(Type type)
{
if (type.GetCustomAttribute<PdsAutoSerializableAttribute>() is null) return default;
if (!RuntimeFeature.IsDynamicCodeCompiled
|| type.GetCustomAttribute<PdsAutoSerializableAttribute>() is null) return default;

ConstructorInfo? constructor = type.GetConstructor([]);
if (constructor is null) return default;
Expand Down
32 changes: 30 additions & 2 deletions PDS/src/StructureItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace PDS;

Expand Down Expand Up @@ -32,6 +33,32 @@ public abstract class StructureItem
typeof(CompoundItem),
typeof(ListItem),
];

public static readonly Func<object>[] RegisteredConstructors =
[
() => new NumericItem<sbyte>(),
() => new ArrayItem<sbyte>(),
() => new NumericItem<byte>(),
() => new ArrayItem<byte>(),
() => new NumericItem<short>(),
() => new ArrayItem<short>(),
() => new NumericItem<ushort>(),
() => new ArrayItem<ushort>(),
() => new NumericItem<int>(),
() => new ArrayItem<int>(),
() => new NumericItem<uint>(),
() => new ArrayItem<uint>(),
() => new NumericItem<long>(),
() => new ArrayItem<long>(),
() => new NumericItem<ulong>(),
() => new ArrayItem<ulong>(),
() => new BoolItem(),
() => new NumericItem<float>(),
() => new NumericItem<double>(),
() => new StringItem(),
() => new CompoundItem(),
() => new ListItem(),
];

public static readonly HashSet<Type> SupportedTypes =
[
Expand Down Expand Up @@ -193,7 +220,8 @@ public static StructureItem Serialize(object obj)
return serializable.Serialize();
}

if (obj.GetType().GetCustomAttribute<PdsAutoSerializableAttribute>() is null)
if (!RuntimeFeature.IsDynamicCodeCompiled
|| obj.GetType().GetCustomAttribute<PdsAutoSerializableAttribute>() is null)
return EofItem.Instance;

CompoundItem compound = new();
Expand Down Expand Up @@ -228,7 +256,7 @@ public static StructureItem ReadItem(BinaryReader reader)
if (type == 0 || type > RegisteredTypes.Length)
return EofItem.Instance;

if (RegisteredTypes[type - 1].GetConstructor([])?.Invoke(null) is not StructureItem item)
if (RegisteredConstructors[type - 1].Invoke() is not StructureItem item)
return EofItem.Instance;

item.Read(reader);
Expand Down
3 changes: 1 addition & 2 deletions VoxelThing.Client/VoxelThing.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>false</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTK" Version="4.8.2" />
<PackageReference Include="StbImageSharp" Version="2.30.15" />
Expand Down
4 changes: 4 additions & 0 deletions VoxelThing.Client/src/Game.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
Expand Down Expand Up @@ -108,6 +109,9 @@ public Screen? CurrentScreen
Profile = ContextProfile.Core
})
{
if (!RuntimeFeature.IsDynamicCodeCompiled)
GL.LoadBindings(new GLFWBindingsContext());

if (SharedConstants.Debug)
Console.WriteLine("Running in a debug configuration! Expect much lower performance than release.");

Check warning on line 116 in VoxelThing.Client/src/Game.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected

Expand Down
14 changes: 12 additions & 2 deletions VoxelThing.Client/src/Rendering/Vertices/Bindings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL;
using VoxelThing.Game;

Expand Down Expand Up @@ -80,8 +82,16 @@ private void UploadIndices(bool dynamic)
if (DataSize == 0 || nextIndices is null || ebo == 0) return;

GL.BindBuffer(BufferTarget.ElementArrayBuffer, ebo);
GL.BufferData(BufferTarget.ElementArrayBuffer, nextIndices.Count * sizeof(int), nextIndices.GetInternalArray(),
dynamic ? BufferUsageHint.DynamicDraw : BufferUsageHint.StaticDraw);
if (RuntimeFeature.IsDynamicCodeCompiled)
GL.BufferData(BufferTarget.ElementArrayBuffer, nextIndices.Count * sizeof(int), nextIndices.GetInternalArray(),
dynamic ? BufferUsageHint.DynamicDraw : BufferUsageHint.StaticDraw);
else
unsafe
{
fixed (int* indices = CollectionsMarshal.AsSpan(nextIndices))
GL.BufferData(BufferTarget.ElementArrayBuffer, nextIndices.Count * sizeof(int), (IntPtr)indices,
dynamic ? BufferUsageHint.DynamicDraw : BufferUsageHint.StaticDraw);
}
}

public virtual void Clear()
Expand Down
13 changes: 12 additions & 1 deletion VoxelThing.Client/src/Rendering/Vertices/FloatBindings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL;
using VoxelThing.Game;

Expand Down Expand Up @@ -29,7 +31,16 @@ public FloatBindings Put(params float[] vertices)
}

protected override void UploadVertices(bool dynamic)
=> VertexLayout.BufferData(Vbo, nextData.Count * sizeof(float), nextData.GetInternalArray(), dynamic);
{
if (RuntimeFeature.IsDynamicCodeCompiled)
VertexLayout.BufferData(Vbo, nextData.Count * sizeof(float), nextData.GetInternalArray(), dynamic);
else
unsafe
{
fixed (float* data = CollectionsMarshal.AsSpan(nextData))
VertexLayout.BufferData(Vbo, nextData.Count * sizeof(float), (IntPtr)data, dynamic);
}
}

public override void Clear()
{
Expand Down
6 changes: 6 additions & 0 deletions VoxelThing.Client/src/Rendering/Vertices/VertexLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ public static void BufferData<T>(int vbo, int length, T[] data, bool dynamic)
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, length, data, dynamic ? BufferUsageHint.DynamicDraw : BufferUsageHint.StaticDraw);
}

public static void BufferData(int vbo, int length, IntPtr data, bool dynamic)
{
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, length, data, dynamic ? BufferUsageHint.DynamicDraw : BufferUsageHint.StaticDraw);
}
}
2 changes: 1 addition & 1 deletion VoxelThing.Game/src/Blocks/Texture/BlockTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public readonly record struct ColumnTexture(Vector2i Side, Vector2i TopBottom) :
public Vector2i Get(Direction face, IBlockAccess? blockAccess, int x, int y, int z)
=> face switch
{
Direction.Up | Direction.Down => TopBottom,
Direction.Up or Direction.Down => TopBottom,
_ => Side,
};
}
Expand Down
10 changes: 7 additions & 3 deletions VoxelThing.Game/src/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Runtime.CompilerServices;

namespace VoxelThing.Game;

Expand All @@ -10,9 +11,12 @@ public static class Extensions
// Only use this for sending vertex data or doing
// anything that doesn't require writing to the list.
public static T[] GetInternalArray<T>(this List<T> list)
=> (T[])list.GetType()
.GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance)! // Shouldn't be null ;)
.GetValue(list);
=> !RuntimeFeature.IsDynamicCodeCompiled
? list.ToArray()
: (T[])list.GetType()
.GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance)! // Shouldn't be null ;)
.GetValue(list);

#pragma warning restore CS8600
#pragma warning restore CS8603

Expand Down
2 changes: 1 addition & 1 deletion VoxelThing.Game/src/Worlds/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class World : IBlockAccess

public World(ISaveHandler saveHandler, WorldInfo? info = null)
{
info ??= saveHandler.LoadData("world")?.Deserialize<WorldInfo>() ?? new();
info ??= new WorldInfo().Deserialize(saveHandler.LoadData("world"));

SaveHandler = saveHandler;
Info = info;
Expand Down
33 changes: 16 additions & 17 deletions VoxelThing.Game/src/Worlds/WorldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@

namespace VoxelThing.Game.Worlds;

[PdsAutoSerializable]
public class WorldInfo
// [PdsAutoSerializable]
public class WorldInfo : IStructureItemSerializable
{
public string Name = "World";
public ulong Seed = new Random64().NextUInt64();

// public StructureItem Serialize() => new CompoundItem()
// .Put(nameof(Name), Name)
// .Put(nameof(Seed), Seed);
//
// public static WorldInfo Deserialize(StructureItem? item)
// {
// WorldInfo info = new();
// if (item is not CompoundItem compound)
// return info;
//
// info.Name = compound[nameof(Name)]?.TryStringValue ?? info.Name;
// info.Seed = compound[nameof(Seed)]?.TryULongValue ?? info.Seed;
//
// return info;
// }
public StructureItem Serialize() => new CompoundItem()
.Put(nameof(Name), Name)
.Put(nameof(Seed), Seed);

public WorldInfo Deserialize(StructureItem? item)
{
if (item is not CompoundItem compound)
return this;

Name = compound[nameof(Name)]?.TryStringValue ?? Name;
Seed = compound[nameof(Seed)]?.TryULongValue ?? Seed;

return this;
}
}

0 comments on commit b707b0a

Please sign in to comment.