Compare commits
9 Commits
main
...
feat/gameo
Author | SHA1 | Date | |
---|---|---|---|
1df5eee59d | |||
0148afea52 | |||
4faac1b5ca | |||
4c04e3d02f | |||
2f6fecdd78 | |||
7e48eb8f12 | |||
86b9206515 | |||
0708ba89cc | |||
361a7c53b9 |
@ -1,20 +1,35 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core.Factory;
|
namespace Syntriax.Engine.Core.Factory;
|
||||||
|
|
||||||
public static class TypeFactory
|
public static class TypeFactory
|
||||||
{
|
{
|
||||||
public static T Get<T>(params object?[]? args) where T : class
|
public static T Get<T>(params object?[]? args) where T : class
|
||||||
|
=> Get<T>(typeof(T), args);
|
||||||
|
|
||||||
|
public static Type Get(string typeName)
|
||||||
|
{
|
||||||
|
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
|
||||||
|
if (type.FullName?.Equals(typeName, StringComparison.OrdinalIgnoreCase) ?? false)
|
||||||
|
return type;
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T Get<T>(string typeName, params object?[]? args) where T : class
|
||||||
|
=> Get<T>(Get(typeName), args);
|
||||||
|
|
||||||
|
public static T Get<T>(Type type, params object?[]? args) where T : class
|
||||||
{
|
{
|
||||||
T? result;
|
T? result;
|
||||||
|
|
||||||
if (args is not null && args.Length != 0)
|
if (args is not null && args.Length != 0)
|
||||||
result = Activator.CreateInstance(typeof(T), args) as T;
|
result = Activator.CreateInstance(type, args) as T;
|
||||||
else
|
else
|
||||||
result = Activator.CreateInstance(typeof(T)) as T;
|
result = Activator.CreateInstance(type) as T;
|
||||||
|
|
||||||
if (result is null)
|
if (result is null)
|
||||||
throw new Exception($"{typeof(T).Name} of type {typeof(T).Name} could not be created.");
|
throw new Exception($"{type.Name} could not be created or casted to {typeof(T).Name}.");
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
8
Engine.Serialization/DTOs/BehaviourControllerDTO.cs
Normal file
8
Engine.Serialization/DTOs/BehaviourControllerDTO.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Engine.Serialization;
|
||||||
|
|
||||||
|
public class BehaviourControllerDto : ClassInterchangeableDto
|
||||||
|
{
|
||||||
|
public Dictionary<string, object?> Behaviours { get; set; } = [];
|
||||||
|
}
|
6
Engine.Serialization/DTOs/ClassInterchangeableDto.cs
Normal file
6
Engine.Serialization/DTOs/ClassInterchangeableDto.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace Engine.Serialization;
|
||||||
|
|
||||||
|
public class ClassInterchangeableDto : EntityDto
|
||||||
|
{
|
||||||
|
public string ClassType { get; set; } = null!;
|
||||||
|
}
|
8
Engine.Serialization/DTOs/EntityDto.cs
Normal file
8
Engine.Serialization/DTOs/EntityDto.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Engine.Serialization;
|
||||||
|
|
||||||
|
public class EntityDto
|
||||||
|
{
|
||||||
|
public Dictionary<string, object?> Fields { get; set; } = [];
|
||||||
|
}
|
10
Engine.Serialization/DTOs/GameObjectDTO.cs
Normal file
10
Engine.Serialization/DTOs/GameObjectDTO.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace Engine.Serialization;
|
||||||
|
|
||||||
|
public class GameObjectDto : ClassInterchangeableDto
|
||||||
|
{
|
||||||
|
string Id { get; set; } = null!;
|
||||||
|
string Name { get; set; } = null!;
|
||||||
|
TransformDto Transform { get; set; } = null!;
|
||||||
|
BehaviourControllerDto BehaviourController { get; set; } = null!;
|
||||||
|
StateEnableDto StateEnable { get; set; } = null!;
|
||||||
|
}
|
7
Engine.Serialization/DTOs/StateEnableDto.cs
Normal file
7
Engine.Serialization/DTOs/StateEnableDto.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace Engine.Serialization;
|
||||||
|
|
||||||
|
public class StateEnableDto : ClassInterchangeableDto
|
||||||
|
{
|
||||||
|
public bool Enabled { get; set; } = false;
|
||||||
|
|
||||||
|
}
|
11
Engine.Serialization/DTOs/TransformDTO.cs
Normal file
11
Engine.Serialization/DTOs/TransformDTO.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using Syntriax.Engine.Core;
|
||||||
|
|
||||||
|
namespace Engine.Serialization;
|
||||||
|
|
||||||
|
public class TransformDto : ClassInterchangeableDto
|
||||||
|
{
|
||||||
|
string? ParentId { get; set; } = null;
|
||||||
|
Vector2D Position { get; set; } = Vector2D.Zero;
|
||||||
|
Vector2D Scale { get; set; } = Vector2D.Zero;
|
||||||
|
float Rotation { get; set; } = 0f;
|
||||||
|
}
|
17
Engine.Serialization/Engine.Serialization.csproj
Normal file
17
Engine.Serialization/Engine.Serialization.csproj
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>disable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="YamlDotNet" Version="15.1.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Engine.Core\Engine.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
85
Engine.Serialization/Serialization.cs
Normal file
85
Engine.Serialization/Serialization.cs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using Syntriax.Engine.Core;
|
||||||
|
using Syntriax.Engine.Core.Abstract;
|
||||||
|
using Syntriax.Engine.Core.Factory;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
|
namespace Engine.Serialization;
|
||||||
|
|
||||||
|
public static class Serialization
|
||||||
|
{
|
||||||
|
private static readonly ISerializer defaultSerializer = new SerializerBuilder().;
|
||||||
|
|
||||||
|
public static string Serialize(object @object)
|
||||||
|
{
|
||||||
|
EntityDto dto = new();
|
||||||
|
BindingFlags bindingFlags = BindingFlags.Public |
|
||||||
|
BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance;
|
||||||
|
|
||||||
|
Type type = @object.GetType();
|
||||||
|
FieldInfo[] fieldInfos = type.GetFields(bindingFlags);
|
||||||
|
List<(string Name, object?)> list = fieldInfos.Where(f => f.FieldType.IsValueType || f.FieldType == typeof(string)).Select(f => (f.Name, f.GetValue(@object))).ToList();
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// public static string SerializeGameObject(IGameObject gameObject) => Serialize(gameObject, defaultSerializer);
|
||||||
|
// public static T DeserializeGameObject<T>(StreamReader reader) where T : class, IGameObject
|
||||||
|
// {
|
||||||
|
// Dictionary<string, object?> gameObjectDTO = Deserialize<Dictionary<string, object?>>(reader, defaultSerializer);
|
||||||
|
// return CreateGameObject<T>(gameObjectDTO);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// private static T CreateGameObject<T>(Dictionary<string, object?> gameObject) where T : class, IGameObject
|
||||||
|
// {
|
||||||
|
// Type gameObjectType = gameObject.TryGetValue("ClassType", out var goType) ? TypeFactory.Get(goType?.ToString() ?? throw new Exception()) : typeof(GameObject);
|
||||||
|
// Type stateEnableType = gameObject.TryGetValue("StateEnable.ClassType", out var seType) ? TypeFactory.Get(seType?.ToString() ?? throw new Exception()) : typeof(Transform);
|
||||||
|
// Type transformType = gameObject.TryGetValue("Transform.ClassType", out var tType) ? TypeFactory.Get(tType?.ToString() ?? throw new Exception()) : typeof(Transform);
|
||||||
|
// Type behaviourControllerType = gameObject.TryGetValue("BehaviourController.ClassType", out var bcType) ? TypeFactory.Get(bcType?.ToString() ?? throw new Exception()) : typeof(Transform);
|
||||||
|
|
||||||
|
// ITransform transform = TypeFactory.Get<ITransform>(transformType);
|
||||||
|
// IStateEnable stateEnable = TypeFactory.Get<IStateEnable>(stateEnableType);
|
||||||
|
// IBehaviourController behaviourController = TypeFactory.Get<IBehaviourController>(behaviourControllerType);
|
||||||
|
// T t = new GameObjectFactory().Instantiate<T>(transform, behaviourController, stateEnable, gameObjectType);
|
||||||
|
|
||||||
|
// Dictionary<string, object?>? behaviours = gameObject["BehaviourController"] as Dictionary<string, object?> ?? throw new Exception();
|
||||||
|
|
||||||
|
// foreach ((var key, var value) in behaviours)
|
||||||
|
// {
|
||||||
|
// Dictionary<string, object?> values = value as Dictionary<string, object?> ?? throw new Exception();
|
||||||
|
|
||||||
|
// IBehaviour behaviour = TypeFactory.Get<IBehaviour>(values["ClassType"]);
|
||||||
|
// behaviourController.AddBehaviour(behaviour);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return t;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public static string SerializeGameManager(IGameManager gameManager) => Serialize(gameManager, defaultSerializer);
|
||||||
|
// public static T DeserializeGameManager<T>(StreamReader reader) where T : class, IGameManager
|
||||||
|
// {
|
||||||
|
// GameManagerDTO gameManagerDto = Deserialize<GameManagerDTO>(reader, defaultSerializer);
|
||||||
|
|
||||||
|
// Type gameManagerType = (gameManagerDto.ClassType is not null) ? TypeFactory.Get(gameManagerDto.ClassType) : typeof(GameManager);
|
||||||
|
// T gameManager = TypeFactory.Get<T>(gameManagerType);
|
||||||
|
|
||||||
|
// foreach (var gameObjectDto in gameManagerDto.GameObjects)
|
||||||
|
// gameManager.RegisterGameObject(CreateGameObject<IGameObject>(gameObjectDto));
|
||||||
|
|
||||||
|
// return gameManager;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public static string Serialize<T>(T @object) => Serialize(@object, defaultSerializer);
|
||||||
|
// public static string Serialize<T>(T @object, ISerializer serializer) => serializer.Serialize(@object);
|
||||||
|
|
||||||
|
// public static T Deserialize<T>(string serializedString) => Deserialize<T>(serializedString, defaultSerializer);
|
||||||
|
// public static T Deserialize<T>(string serializedString, ISerializer serializer) => serializer.Deserialize<T>(serializedString);
|
||||||
|
|
||||||
|
// public static T Deserialize<T>(StreamReader reader) => Deserialize<T>(reader, defaultSerializer);
|
||||||
|
// public static T Deserialize<T>(StreamReader reader, ISerializer serializer) => serializer.Deserialize<T>(reader);
|
||||||
|
}
|
48
Engine.Serialization/Vector2DYamlConverter.cs
Normal file
48
Engine.Serialization/Vector2DYamlConverter.cs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
using Syntriax.Engine.Core;
|
||||||
|
|
||||||
|
using YamlDotNet.Core;
|
||||||
|
using YamlDotNet.Core.Events;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
|
namespace Engine.Serialization;
|
||||||
|
|
||||||
|
internal class Vector2DYamlConverter : IYamlTypeConverter
|
||||||
|
{
|
||||||
|
public bool Accepts(Type type) => type == typeof(Vector2D);
|
||||||
|
|
||||||
|
public object? ReadYaml(IParser parser, Type type)
|
||||||
|
{
|
||||||
|
if (parser.Current is not MappingStart)
|
||||||
|
throw new InvalidOperationException("Expected MappingStart");
|
||||||
|
|
||||||
|
parser.MoveNext();
|
||||||
|
float x = 0.0f;
|
||||||
|
float y = 0.0f;
|
||||||
|
|
||||||
|
while (parser.Current != null && parser.Current is not MappingEnd)
|
||||||
|
{
|
||||||
|
var propertyName = ((Scalar)parser.Current).Value;
|
||||||
|
parser.MoveNext();
|
||||||
|
switch (propertyName)
|
||||||
|
{
|
||||||
|
case nameof(Vector2D.X): x = float.Parse(((Scalar)parser.Current).Value); break;
|
||||||
|
case nameof(Vector2D.Y): y = float.Parse(((Scalar)parser.Current).Value); break;
|
||||||
|
}
|
||||||
|
parser.MoveNext();
|
||||||
|
}
|
||||||
|
return new Vector2D(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||||
|
{
|
||||||
|
var vector = (Vector2D)(value ?? throw new Exception());
|
||||||
|
emitter.Emit(new MappingStart());
|
||||||
|
emitter.Emit(new Scalar(nameof(Vector2D.X)));
|
||||||
|
emitter.Emit(new Scalar(vector.X.ToString()));
|
||||||
|
emitter.Emit(new Scalar(nameof(Vector2D.Y)));
|
||||||
|
emitter.Emit(new Scalar(vector.Y.ToString()));
|
||||||
|
emitter.Emit(new MappingEnd());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user