mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Serialize Mesh and MeshRenderer in scene + fixed Light serialization
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
using GlitchyEngine.Core;
|
||||
using System;
|
||||
using Bon;
|
||||
using Bon.Integrated;
|
||||
using System.Reflection;
|
||||
|
||||
namespace GlitchyEngine.Content;
|
||||
|
||||
[BonTarget]
|
||||
class Asset : RefCounter
|
||||
{
|
||||
private append String _identifier;
|
||||
@@ -28,10 +32,51 @@ class Asset : RefCounter
|
||||
/// Gets the content manager that manages this asset; or null if this asset isn't managed.
|
||||
public IContentManager ContentManager => _contentManager;
|
||||
|
||||
static this
|
||||
{
|
||||
gBonEnv.typeHandlers.Add(typeof(Asset),
|
||||
((.)new => AssetSerialize, new => AssetDeserialize));
|
||||
}
|
||||
|
||||
protected ~this()
|
||||
{
|
||||
// TODO: crash when _contentManager is deleted first...
|
||||
// TODO: unregister from content manager
|
||||
_contentManager?.UnmanageAsset(this);
|
||||
}
|
||||
|
||||
static void AssetSerialize(BonWriter writer, ValueView value, BonEnvironment environment)
|
||||
{
|
||||
Log.EngineLogger.Assert(value.type == typeof(Asset));
|
||||
|
||||
let identifier = value.Get<Asset>().Identifier;
|
||||
writer.String(identifier);
|
||||
}
|
||||
|
||||
static Result<void> AssetDeserialize(BonReader reader, ValueView value, BonEnvironment environment)//, DeserializeFieldState state)
|
||||
{
|
||||
Log.EngineLogger.Assert(value.type == typeof(Asset));
|
||||
|
||||
String identifier = scope .();
|
||||
|
||||
Deserialize.String!(reader, ref identifier, environment);
|
||||
|
||||
Asset asset = Application.Get().ContentManager.LoadAsset(identifier);
|
||||
|
||||
if (asset != null)
|
||||
{
|
||||
Asset oldAsset = value.Get<Asset>();
|
||||
oldAsset.ReleaseRef();
|
||||
|
||||
value.Assign(asset);
|
||||
return .Ok;
|
||||
}
|
||||
else
|
||||
{
|
||||
Deserialize.Error!("Invalid resource path", reader, value.type);
|
||||
}
|
||||
}
|
||||
|
||||
//gBonEnv.typeHandlers.Add(typeof(Resource<>),
|
||||
// ((.)new => ResourceSerialize, (.)new => ResourceDeserialize));
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
using Bon;
|
||||
using System;
|
||||
|
||||
namespace GlitchyEngine.Math
|
||||
{
|
||||
/// Represents a four component color with 8 bit per channel
|
||||
[CRepr]
|
||||
[BonTarget]
|
||||
public struct Color
|
||||
{
|
||||
// from DirectXColors.h
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Bon;
|
||||
using System;
|
||||
|
||||
using internal GlitchyEngine.Math;
|
||||
@@ -6,6 +7,7 @@ namespace GlitchyEngine.Math
|
||||
{
|
||||
/// Represents an RGB color.
|
||||
[CRepr]
|
||||
[BonTarget]
|
||||
public struct ColorRGB
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -46,7 +46,20 @@ namespace GlitchyEngine.World
|
||||
[Component("Sprite Renderer")]
|
||||
struct SpriterRendererComponent : IDisposableComponent
|
||||
{
|
||||
public Texture2D Sprite = null;
|
||||
private Texture2D _sprite = null;
|
||||
|
||||
public Texture2D Sprite
|
||||
{
|
||||
get => _sprite;
|
||||
set mut
|
||||
{
|
||||
if (_sprite == value)
|
||||
return;
|
||||
|
||||
SetReference!(_sprite, value);
|
||||
}
|
||||
}
|
||||
|
||||
public ColorRGBA Color = .White;
|
||||
public Vector4 UvTransform = .(0, 0, 1, 1);
|
||||
|
||||
@@ -63,7 +76,7 @@ namespace GlitchyEngine.World
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Sprite?.ReleaseRef();
|
||||
_sprite?.ReleaseRef();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace GlitchyEngine.World
|
||||
{
|
||||
Serialize.Value(writer, "Color", component.Color);
|
||||
Serialize.Value(writer, "IsCircle", component.IsCircle);
|
||||
Serialize.Value(writer, "Sprite", component.Sprite);
|
||||
Serialize.Value(writer, "Sprite", component.Sprite?.Identifier);
|
||||
Serialize.Value(writer, "UvTransform", component.UvTransform);
|
||||
});
|
||||
|
||||
@@ -169,6 +169,16 @@ namespace GlitchyEngine.World
|
||||
Serialize.Value(writer, "Restitution", component.Restitution);
|
||||
Serialize.Value(writer, "RestitutionThreshold", component.RestitutionThreshold);
|
||||
});
|
||||
|
||||
SerializeComponent<MeshComponent>(writer, entity, "MeshComponent", scope (component) =>
|
||||
{
|
||||
Serialize.Value(writer, "Mesh", component.Mesh.Identifier);
|
||||
});
|
||||
|
||||
SerializeComponent<MeshRendererComponent>(writer, entity, "MeshRendererComponent", scope (component) =>
|
||||
{
|
||||
Serialize.Value(writer, "Material", component.Material.Identifier);
|
||||
});
|
||||
}
|
||||
|
||||
writer.EntryEnd();
|
||||
@@ -260,6 +270,21 @@ namespace GlitchyEngine.World
|
||||
|
||||
private Result<void> DeserializeEntity(BonReader reader)
|
||||
{
|
||||
mixin DeserializeAsset<T>(StringView identifier) where T : Asset
|
||||
{
|
||||
Asset asset = null;
|
||||
|
||||
Try!(Deserialize.Value(reader, identifier, out asset));
|
||||
|
||||
if (asset != null && !(asset is T))
|
||||
{
|
||||
Log.EngineLogger.Error($"Asset {asset.Identifier} is not a {nameof(T)}.");
|
||||
return .Err;
|
||||
}
|
||||
|
||||
(T)asset
|
||||
}
|
||||
|
||||
Try!(reader.ObjectBlock());
|
||||
|
||||
Deserialize.Value<uint64>(reader, "Id", let uuid);
|
||||
@@ -287,40 +312,22 @@ namespace GlitchyEngine.World
|
||||
|
||||
delete name;
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
case "NameComponent":
|
||||
Try!(DeserializeComponent<DebugNameComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
String name;
|
||||
|
||||
Deserialize.Value(reader, "Name", out name);
|
||||
|
||||
component.SetName(name);
|
||||
|
||||
delete name;
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
case "SpriterRendererComponent":
|
||||
Try!(DeserializeComponent<SpriterRendererComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
// TODO: Texture
|
||||
|
||||
Try!(Deserialize.Value(reader, "Color", out component.Color));
|
||||
reader.EntryEnd();
|
||||
Try!(Deserialize.Value(reader, "IsCircle", out component.IsCircle));
|
||||
reader.EntryEnd();
|
||||
|
||||
String spriteName;
|
||||
Try!(Deserialize.Value(reader, "Sprite", out spriteName));
|
||||
|
||||
if (spriteName != null)
|
||||
component.Sprite = Content.LoadAsset<Texture2D>(spriteName);
|
||||
|
||||
delete spriteName;
|
||||
|
||||
using (Texture2D sprite = DeserializeAsset!<Texture2D>("Sprite"))
|
||||
{
|
||||
component.Sprite = (Texture2D)sprite;
|
||||
}
|
||||
reader.EntryEnd();
|
||||
|
||||
Try!(Deserialize.Value(reader, "UvTransform", out component.UvTransform));
|
||||
|
||||
return .Ok;
|
||||
@@ -407,7 +414,7 @@ namespace GlitchyEngine.World
|
||||
case "LightComponent":
|
||||
Try!(DeserializeComponent<LightComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
SceneLight light = component.SceneLight;
|
||||
ref SceneLight light = ref component.SceneLight;
|
||||
|
||||
Deserialize.Value(reader, "LightType", out light.[Friend]_type);
|
||||
reader.EntryEnd();
|
||||
@@ -464,7 +471,26 @@ namespace GlitchyEngine.World
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
|
||||
case "MeshComponent":
|
||||
Try!(DeserializeComponent<MeshComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
using (GeometryBinding mesh = DeserializeAsset!<GeometryBinding>("Mesh"))
|
||||
{
|
||||
component.Mesh = mesh;
|
||||
}
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
case "MeshRendererComponent":
|
||||
Try!(DeserializeComponent<MeshRendererComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
using (Material material = DeserializeAsset!<Material>("Material"))
|
||||
{
|
||||
component.Material = material;
|
||||
}
|
||||
|
||||
return .Ok;
|
||||
}));
|
||||
default:
|
||||
Log.EngineLogger.AssertDebug(false, "Unknown component type");
|
||||
//return .Err;
|
||||
|
||||
Reference in New Issue
Block a user