Compare commits
17 Commits
499f875903
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
| 105b87da3a | |||
| 51534606c8 | |||
| 1418927c32 | |||
| 35c7eb9578 | |||
| 6ca3f22b17 | |||
| 7ae8b4feb0 | |||
| e84c6edce1 | |||
| 4326d5615e | |||
| fa514531bf | |||
| 1d3dd8b046 | |||
| 785fee9b6b | |||
| 9f54f89f6d | |||
| aadc87d78a | |||
| d653774357 | |||
| 45bd505da7 | |||
| 3b1c291588 | |||
| 32a7e9be24 |
50
Engine.Core/Config/BasicConfiguration.cs
Normal file
50
Engine.Core/Config/BasicConfiguration.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Engine.Core.Config;
|
||||
|
||||
public class BasicConfiguration : IConfiguration
|
||||
{
|
||||
public Event<IConfiguration, IConfiguration.ConfigUpdateArguments> OnAdded { get; } = new();
|
||||
public Event<IConfiguration, IConfiguration.ConfigUpdateArguments> OnSet { get; } = new();
|
||||
public Event<IConfiguration, IConfiguration.ConfigUpdateArguments> OnRemoved { get; } = new();
|
||||
|
||||
private readonly Dictionary<string, object?> values = [];
|
||||
|
||||
public IReadOnlyDictionary<string, object?> Values => values;
|
||||
|
||||
public T? Get<T>(string key, T? defaultValue = default)
|
||||
{
|
||||
if (!values.TryGetValue(key, out object? value))
|
||||
return defaultValue;
|
||||
|
||||
if (value is T castedObject)
|
||||
return castedObject;
|
||||
|
||||
try { return (T?)System.Convert.ChangeType(value, typeof(T)); } catch { }
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public object? Get(string key)
|
||||
{
|
||||
values.TryGetValue(key, out object? value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public bool Has(string key) => values.ContainsKey(key);
|
||||
|
||||
public void Remove<T>(string key)
|
||||
{
|
||||
if (values.Remove(key))
|
||||
OnRemoved.Invoke(this, new(key));
|
||||
}
|
||||
|
||||
public void Set<T>(string key, T value)
|
||||
{
|
||||
if (!values.TryAdd(key, value))
|
||||
values[key] = value;
|
||||
else
|
||||
OnAdded.Invoke(this, new(key));
|
||||
OnSet.Invoke(this, new(key));
|
||||
}
|
||||
}
|
||||
8
Engine.Core/Config/ConfigurationExtensions.cs
Normal file
8
Engine.Core/Config/ConfigurationExtensions.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Engine.Core.Exceptions;
|
||||
|
||||
namespace Engine.Core.Config;
|
||||
|
||||
public static class ConfigurationExtensions
|
||||
{
|
||||
public static T GetRequired<T>(this IConfiguration configuration, string key) => configuration.Get<T>(key) ?? throw new NotFoundException($"Type of {typeof(T).FullName} with the key {key} was not present in the {configuration.GetType().FullName}");
|
||||
}
|
||||
23
Engine.Core/Config/IConfiguration.cs
Normal file
23
Engine.Core/Config/IConfiguration.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Engine.Core.Config;
|
||||
|
||||
public interface IConfiguration
|
||||
{
|
||||
static IConfiguration System { get; set; } = new SystemConfiguration();
|
||||
static IConfiguration Shared { get; set; } = new BasicConfiguration();
|
||||
|
||||
Event<IConfiguration, ConfigUpdateArguments> OnAdded { get; }
|
||||
Event<IConfiguration, ConfigUpdateArguments> OnSet { get; }
|
||||
Event<IConfiguration, ConfigUpdateArguments> OnRemoved { get; }
|
||||
|
||||
IReadOnlyDictionary<string, object?> Values { get; }
|
||||
|
||||
bool Has(string key);
|
||||
object? Get(string key);
|
||||
T? Get<T>(string key, T? defaultValue = default);
|
||||
void Set<T>(string key, T value);
|
||||
void Remove<T>(string key);
|
||||
|
||||
readonly record struct ConfigUpdateArguments(string Key);
|
||||
}
|
||||
11
Engine.Core/Config/SystemConfiguration.cs
Normal file
11
Engine.Core/Config/SystemConfiguration.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Engine.Core.Config;
|
||||
|
||||
public class SystemConfiguration : BasicConfiguration, IConfiguration
|
||||
{
|
||||
public SystemConfiguration()
|
||||
{
|
||||
foreach (System.Collections.DictionaryEntry entry in System.Environment.GetEnvironmentVariables())
|
||||
if (entry is { Key: string key, Value: not null })
|
||||
Set(key, entry.Value);
|
||||
}
|
||||
}
|
||||
12
Engine.Core/Extensions/IdentifiableExtensions.cs
Normal file
12
Engine.Core/Extensions/IdentifiableExtensions.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Engine.Core;
|
||||
|
||||
public static class IdentifiableExtensions
|
||||
{
|
||||
public static bool IsIdentical(this IIdentifiable? left, IIdentifiable? right)
|
||||
{
|
||||
if (left == null || right == null)
|
||||
return false;
|
||||
|
||||
return left?.Id?.CompareTo(right?.Id) == 0;
|
||||
}
|
||||
}
|
||||
@@ -4,15 +4,15 @@ namespace Engine.Core.Serialization;
|
||||
|
||||
public interface ISerializer
|
||||
{
|
||||
object Deserialize(string configuration);
|
||||
object Deserialize(string configuration, Type type);
|
||||
T Deserialize<T>(string configuration);
|
||||
object Deserialize(string content);
|
||||
object Deserialize(string content, Type type);
|
||||
T Deserialize<T>(string content);
|
||||
|
||||
string Serialize(object instance);
|
||||
|
||||
ProgressiveTask<object> DeserializeAsync(string configuration);
|
||||
ProgressiveTask<object> DeserializeAsync(string configuration, Type type);
|
||||
ProgressiveTask<T> DeserializeAsync<T>(string configuration);
|
||||
ProgressiveTask<object> DeserializeAsync(string content);
|
||||
ProgressiveTask<object> DeserializeAsync(string content, Type type);
|
||||
ProgressiveTask<T> DeserializeAsync<T>(string content);
|
||||
|
||||
ProgressiveTask<string> SerializeAsync(object instance);
|
||||
}
|
||||
|
||||
@@ -44,6 +44,17 @@ public class UpdateManager : Behaviour, IEnterUniverse, IExitUniverse
|
||||
universe.OnPostUpdate.RemoveListener(OnPostUpdate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call the <see cref="IFirstFrameUpdate"/> early if it's in queue to be called by this the <see cref="UpdateManager"/>.
|
||||
/// It will not be called in the next natural cycle.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance that will be called now rather than later.</param>
|
||||
public void CallFirstActiveFrameImmediately(IFirstFrameUpdate instance)
|
||||
{
|
||||
if (toCallFirstFrameUpdates.Remove(instance))
|
||||
instance.FirstActiveFrame();
|
||||
}
|
||||
|
||||
private void OnFirstUpdate(IUniverse sender, IUniverse.UpdateArguments args)
|
||||
{
|
||||
for (int i = toCallFirstFrameUpdates.Count - 1; i >= 0; i--)
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Engine.Core;
|
||||
|
||||
public class WaitForSecondsYield(float seconds) : ICoroutineYield
|
||||
{
|
||||
private readonly DateTime triggerTime = DateTime.UtcNow.AddSeconds(seconds);
|
||||
|
||||
public bool Yield() => DateTime.UtcNow < triggerTime;
|
||||
}
|
||||
35
Engine.Core/Systems/Yields/WaitForTaskYield.cs
Normal file
35
Engine.Core/Systems/Yields/WaitForTaskYield.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using static Engine.Core.WaitForTaskYield;
|
||||
|
||||
namespace Engine.Core;
|
||||
|
||||
public class WaitForTaskYield(Task task, TaskCompletionStatus completionStatus = TaskCompletionStatus.Either) : ICoroutineYield
|
||||
{
|
||||
public bool Yield()
|
||||
{
|
||||
switch (completionStatus)
|
||||
{
|
||||
case TaskCompletionStatus.Successful:
|
||||
if (task.IsCanceled)
|
||||
throw new("Task has been canceled.");
|
||||
if (task.IsFaulted)
|
||||
throw new("Task has faulted.");
|
||||
return task.IsCompletedSuccessfully;
|
||||
|
||||
case TaskCompletionStatus.Failed:
|
||||
if (task.IsCompletedSuccessfully)
|
||||
throw new("Task was completed successfully.");
|
||||
return task.IsFaulted;
|
||||
}
|
||||
|
||||
return task.IsCompleted;
|
||||
}
|
||||
|
||||
public enum TaskCompletionStatus
|
||||
{
|
||||
Either,
|
||||
Successful,
|
||||
Failed
|
||||
}
|
||||
}
|
||||
14
Engine.Core/Systems/Yields/WaitForTimeYield.cs
Normal file
14
Engine.Core/Systems/Yields/WaitForTimeYield.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace Engine.Core;
|
||||
|
||||
public class WaitForTimeYield(float seconds = 0f, float milliseconds = 0f, float minutes = 0f, float hours = 0f) : ICoroutineYield
|
||||
{
|
||||
private readonly DateTime triggerTime = DateTime.UtcNow
|
||||
.AddHours(hours)
|
||||
.AddMinutes(minutes)
|
||||
.AddSeconds(seconds)
|
||||
.AddMilliseconds(milliseconds);
|
||||
|
||||
public bool Yield() => DateTime.UtcNow < triggerTime;
|
||||
}
|
||||
@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class BehaviourControllerConverter : EngineTypeYamlSerializerBase<IBehaviourController>
|
||||
public class BehaviourControllerConverter : EngineTypeYamlConverterBase<IBehaviourController>
|
||||
{
|
||||
private const string BEHAVIOURS_SCALAR_NAME = "Behaviours";
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class BehaviourConverter : EngineTypeYamlSerializerBase<IBehaviour>
|
||||
public class BehaviourConverter : EngineTypeYamlConverterBase<IBehaviour>
|
||||
{
|
||||
public override IBehaviour? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public abstract class EngineTypeYamlSerializerBase<T> : IEngineTypeYamlConverter
|
||||
public abstract class EngineTypeYamlConverterBase<T> : IEngineTypeYamlConverter
|
||||
{
|
||||
protected const string SERIALIZED_SCALAR_NAME = "Properties";
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class AABB2DConverter : EngineTypeYamlSerializerBase<AABB2D>
|
||||
public class AABB2DConverter : EngineTypeYamlConverterBase<AABB2D>
|
||||
{
|
||||
public override AABB2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class AABB3DConverter : EngineTypeYamlSerializerBase<AABB3D>
|
||||
public class AABB3DConverter : EngineTypeYamlConverterBase<AABB3D>
|
||||
{
|
||||
public override AABB3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class CircleConverter : EngineTypeYamlSerializerBase<Circle>
|
||||
public class CircleConverter : EngineTypeYamlConverterBase<Circle>
|
||||
{
|
||||
public override Circle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class ColorHSVAConverter : EngineTypeYamlSerializerBase<ColorHSVA>
|
||||
public class ColorHSVAConverter : EngineTypeYamlConverterBase<ColorHSVA>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorHSVA).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class ColorHSVConverter : EngineTypeYamlSerializerBase<ColorHSV>
|
||||
public class ColorHSVConverter : EngineTypeYamlConverterBase<ColorHSV>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorHSV).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class ColorRGBAConverter : EngineTypeYamlSerializerBase<ColorRGBA>
|
||||
public class ColorRGBAConverter : EngineTypeYamlConverterBase<ColorRGBA>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorRGBA).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class ColorRGBConverter : EngineTypeYamlSerializerBase<ColorRGB>
|
||||
public class ColorRGBConverter : EngineTypeYamlConverterBase<ColorRGB>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(ColorRGB).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Line2DConverter : EngineTypeYamlSerializerBase<Line2D>
|
||||
public class Line2DConverter : EngineTypeYamlConverterBase<Line2D>
|
||||
{
|
||||
public override Line2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Line2DEquationConverter : EngineTypeYamlSerializerBase<Line2DEquation>
|
||||
public class Line2DEquationConverter : EngineTypeYamlConverterBase<Line2DEquation>
|
||||
{
|
||||
public override Line2DEquation Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Line3DConverter : EngineTypeYamlSerializerBase<Line3D>
|
||||
public class Line3DConverter : EngineTypeYamlConverterBase<Line3D>
|
||||
{
|
||||
public override Line3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Matrix4x4Converter : EngineTypeYamlConverterBase<Matrix4x4>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Matrix4x4).Length + 1;
|
||||
|
||||
public override Matrix4x4 Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
string value = parser.Consume<Scalar>().Value;
|
||||
string insideParenthesis = value[SUBSTRING_START_LENGTH..^1];
|
||||
string[] values = insideParenthesis.Split(", ");
|
||||
return new Matrix4x4(
|
||||
float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]), float.Parse(values[3]),
|
||||
float.Parse(values[4]), float.Parse(values[5]), float.Parse(values[6]), float.Parse(values[7]),
|
||||
float.Parse(values[8]), float.Parse(values[9]), float.Parse(values[10]), float.Parse(values[11]),
|
||||
float.Parse(values[12]), float.Parse(values[13]), float.Parse(values[14]), float.Parse(values[15])
|
||||
);
|
||||
}
|
||||
|
||||
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
|
||||
{
|
||||
Matrix4x4 m = (Matrix4x4)value!;
|
||||
emitter.Emit(new Scalar($"{nameof(Matrix4x4)}({m.M11}, {m.M12}, {m.M13}, {m.M14},{m.M21}, {m.M22}, {m.M23}, {m.M24},{m.M31}, {m.M32}, {m.M33}, {m.M34},{m.M41}, {m.M42}, {m.M43}, {m.M44})"));
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Projection1DConverter : EngineTypeYamlSerializerBase<Projection1D>
|
||||
public class Projection1DConverter : EngineTypeYamlConverterBase<Projection1D>
|
||||
{
|
||||
public override Projection1D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class QuaternionConverter : EngineTypeYamlSerializerBase<Quaternion>
|
||||
public class QuaternionConverter : EngineTypeYamlConverterBase<Quaternion>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Quaternion).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Ray2DConverter : EngineTypeYamlSerializerBase<Ray2D>
|
||||
public class Ray2DConverter : EngineTypeYamlConverterBase<Ray2D>
|
||||
{
|
||||
public override Ray2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Ray3DConverter : EngineTypeYamlSerializerBase<Ray3D>
|
||||
public class Ray3DConverter : EngineTypeYamlConverterBase<Ray3D>
|
||||
{
|
||||
public override Ray3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Shape2DConverter : EngineTypeYamlSerializerBase<Shape2D>
|
||||
public class Shape2DConverter : EngineTypeYamlConverterBase<Shape2D>
|
||||
{
|
||||
public override Shape2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Sphere3DConverter : EngineTypeYamlSerializerBase<Sphere3D>
|
||||
public class Sphere3DConverter : EngineTypeYamlConverterBase<Sphere3D>
|
||||
{
|
||||
public override Sphere3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class TriangleConverter : EngineTypeYamlSerializerBase<Triangle>
|
||||
public class TriangleConverter : EngineTypeYamlConverterBase<Triangle>
|
||||
{
|
||||
public override Triangle Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Vector2DConverter : EngineTypeYamlSerializerBase<Vector2D>
|
||||
public class Vector2DConverter : EngineTypeYamlConverterBase<Vector2D>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector2D).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Vector2DIntConverter : EngineTypeYamlSerializerBase<Vector2DInt>
|
||||
public class Vector2DIntConverter : EngineTypeYamlConverterBase<Vector2DInt>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector2DInt).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Vector3DConverter : EngineTypeYamlSerializerBase<Vector3D>
|
||||
public class Vector3DConverter : EngineTypeYamlConverterBase<Vector3D>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector3D).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Vector3DIntConverter : EngineTypeYamlSerializerBase<Vector3DInt>
|
||||
public class Vector3DIntConverter : EngineTypeYamlConverterBase<Vector3DInt>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector3DInt).Length + 1;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class Vector4DConverter : EngineTypeYamlSerializerBase<Vector4D>
|
||||
public class Vector4DConverter : EngineTypeYamlConverterBase<Vector4D>
|
||||
{
|
||||
private static readonly int SUBSTRING_START_LENGTH = nameof(Vector4D).Length + 1;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class SerializedClassConverter : EngineTypeYamlSerializerBase<SerializedClass>
|
||||
public class SerializedClassConverter : EngineTypeYamlConverterBase<SerializedClass>
|
||||
{
|
||||
public override SerializedClass? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class StateEnableConverter : EngineTypeYamlSerializerBase<IStateEnable>
|
||||
public class StateEnableConverter : EngineTypeYamlConverterBase<IStateEnable>
|
||||
{
|
||||
public override IStateEnable? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -9,7 +9,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class TypeContainerConverter : EngineTypeYamlSerializerBase<TypeContainer>
|
||||
public class TypeContainerConverter : EngineTypeYamlConverterBase<TypeContainer>
|
||||
{
|
||||
public override TypeContainer Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class UniverseConverter : EngineTypeYamlSerializerBase<IUniverse>
|
||||
public class UniverseConverter : EngineTypeYamlConverterBase<IUniverse>
|
||||
{
|
||||
public override IUniverse? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class UniverseObjectSerializer : EngineTypeYamlSerializerBase<IUniverseObject>
|
||||
public class UniverseObjectConverter : EngineTypeYamlConverterBase<IUniverseObject>
|
||||
{
|
||||
public override IUniverseObject? Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
using Engine.Core.Config;
|
||||
|
||||
namespace Engine.Serializers.Yaml;
|
||||
|
||||
public class YamlConfiguration : BasicConfiguration
|
||||
{
|
||||
public readonly string FilePath;
|
||||
|
||||
private readonly YamlSerializer yamlSerializer = new();
|
||||
|
||||
public YamlConfiguration(string filePath)
|
||||
{
|
||||
if (!filePath.EndsWith(".yaml"))
|
||||
filePath += ".yaml";
|
||||
|
||||
FilePath = filePath;
|
||||
|
||||
bool isRelativePath = Path.GetFullPath(filePath).CompareTo(filePath) != 0;
|
||||
|
||||
if (isRelativePath)
|
||||
FilePath = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath));
|
||||
|
||||
if (Path.GetDirectoryName(FilePath) is string directoryPath)
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
|
||||
if (!File.Exists(FilePath))
|
||||
return;
|
||||
|
||||
string yamlFileText = File.ReadAllText(FilePath);
|
||||
Dictionary<string, string> valuePairs = yamlSerializer.Deserialize<Dictionary<string, string>>(yamlFileText);
|
||||
|
||||
foreach ((string key, string value) in valuePairs)
|
||||
Set(key, value);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
File.WriteAllText(FilePath, yamlSerializer.Serialize(Values));
|
||||
}
|
||||
}
|
||||
@@ -62,65 +62,65 @@ public class YamlSerializer : Core.Serialization.ISerializer
|
||||
}
|
||||
}
|
||||
|
||||
public object Deserialize(string configuration)
|
||||
public object Deserialize(string content)
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
identifiableRegistry.Reset();
|
||||
object result = deserializer.Deserialize(configuration)!;
|
||||
object result = deserializer.Deserialize(content)!;
|
||||
identifiableRegistry.AssignAll();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public object Deserialize(string configuration, Type type)
|
||||
public object Deserialize(string content, Type type)
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
identifiableRegistry.Reset();
|
||||
object result = deserializer.Deserialize(configuration, type)!;
|
||||
object result = deserializer.Deserialize(content, type)!;
|
||||
identifiableRegistry.AssignAll();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public T Deserialize<T>(string configuration)
|
||||
public T Deserialize<T>(string content)
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
identifiableRegistry.Reset();
|
||||
T result = deserializer.Deserialize<T>(configuration);
|
||||
T result = deserializer.Deserialize<T>(content);
|
||||
identifiableRegistry.AssignAll();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public ProgressiveTask<object> DeserializeAsync(string configuration)
|
||||
public ProgressiveTask<object> DeserializeAsync(string content)
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
progressionTracker.Reset();
|
||||
Task<object> task = Task.Run(() => Deserialize(configuration));
|
||||
Task<object> task = Task.Run(() => Deserialize(content));
|
||||
return new ProgressiveTask<object>(progressionTracker, task);
|
||||
}
|
||||
}
|
||||
|
||||
public ProgressiveTask<object> DeserializeAsync(string configuration, Type type)
|
||||
public ProgressiveTask<object> DeserializeAsync(string content, Type type)
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
progressionTracker.Reset();
|
||||
Task<object> task = Task.Run(() => Deserialize(configuration, type));
|
||||
Task<object> task = Task.Run(() => Deserialize(content, type));
|
||||
return new ProgressiveTask<object>(progressionTracker, task);
|
||||
}
|
||||
}
|
||||
|
||||
public ProgressiveTask<T> DeserializeAsync<T>(string configuration)
|
||||
public ProgressiveTask<T> DeserializeAsync<T>(string content)
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
progressionTracker.Reset();
|
||||
Task<T> task = Task.Run(() => Deserialize<T>(configuration));
|
||||
Task<T> task = Task.Run(() => Deserialize<T>(content));
|
||||
return new ProgressiveTask<T>(progressionTracker, task);
|
||||
}
|
||||
}
|
||||
@@ -135,8 +135,8 @@ public class YamlSerializer : Core.Serialization.ISerializer
|
||||
}
|
||||
}
|
||||
|
||||
internal object InternalDeserialize(string configuration, Type type)
|
||||
internal object InternalDeserialize(string content, Type type)
|
||||
{
|
||||
return deserializer.Deserialize(configuration, type)!;
|
||||
return deserializer.Deserialize(content, type)!;
|
||||
}
|
||||
}
|
||||
|
||||
40
Engine.Systems/Network/CommonNetworkBehaviour.cs
Normal file
40
Engine.Systems/Network/CommonNetworkBehaviour.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Systems.Network;
|
||||
|
||||
/// <summary>
|
||||
/// Basic network behaviour that supports both client & server behaviour. Finds both
|
||||
/// the <see cref="INetworkCommunicatorClient"/> and the <see cref="INetworkCommunicatorServer"/>
|
||||
/// in the universe in it's first active frame. Recommended to use <see cref="ClientBehaviour"/> or <see cref="ServerBehaviour"/>
|
||||
/// <br/>
|
||||
/// Disclaimer: It implements <see cref="IFirstFrameUpdate"/> and <see cref="ILastFrameUpdate"/> in virtual methods.
|
||||
/// </summary>
|
||||
public class CommonNetworkBehaviour : Behaviour, IFirstFrameUpdate, ILastFrameUpdate
|
||||
{
|
||||
protected INetworkCommunicatorServer? server = null!;
|
||||
protected INetworkCommunicatorClient? client = null!;
|
||||
|
||||
protected bool IsServer { get; private set; } = false;
|
||||
protected bool IsClient { get; private set; } = false;
|
||||
|
||||
public INetworkCommunicatorServer Server => server ?? throw new Core.Exceptions.NotFoundException($"Universe does not contain a {nameof(INetworkCommunicatorServer)}");
|
||||
public INetworkCommunicatorClient Client => client ?? throw new Core.Exceptions.NotFoundException($"Universe does not contain a {nameof(INetworkCommunicatorClient)}");
|
||||
|
||||
public virtual void FirstActiveFrame()
|
||||
{
|
||||
client = Universe.FindBehaviour<INetworkCommunicatorClient>();
|
||||
server = Universe.FindBehaviour<INetworkCommunicatorServer>();
|
||||
|
||||
IsServer = server is not null;
|
||||
IsClient = client is not null;
|
||||
}
|
||||
|
||||
public virtual void LastActiveFrame()
|
||||
{
|
||||
client = null!;
|
||||
server = null!;
|
||||
|
||||
IsServer = false;
|
||||
IsClient = false;
|
||||
}
|
||||
}
|
||||
13
Engine.Systems/Network/Extensions/CommunicatorExtensions.cs
Normal file
13
Engine.Systems/Network/Extensions/CommunicatorExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Engine.Systems.Network;
|
||||
|
||||
public static class CommunicatorExtensions
|
||||
{
|
||||
public static void SendToClients<T>(this INetworkCommunicatorServer server, IEnumerable<IConnection> connections, T packet, PacketDelivery packetDelivery = PacketDelivery.ReliableInOrder) where T : class, new()
|
||||
{
|
||||
foreach (IConnection connection in connections)
|
||||
server.SendToClient(connection, packet, packetDelivery);
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,16 @@ internal class Tween : ITween
|
||||
field = value;
|
||||
switch (value)
|
||||
{
|
||||
case TweenState.Completed: OnCompleted?.Invoke(this); OnEnded?.Invoke(this); break;
|
||||
case TweenState.Cancelled: OnCancelled?.Invoke(this); OnEnded?.Invoke(this); break;
|
||||
case TweenState.Completed:
|
||||
OnCompleted?.Invoke(this);
|
||||
if (State == TweenState.Completed)
|
||||
OnEnded?.Invoke(this);
|
||||
break;
|
||||
case TweenState.Cancelled:
|
||||
OnCancelled?.Invoke(this);
|
||||
if (State == TweenState.Cancelled)
|
||||
OnEnded?.Invoke(this);
|
||||
break;
|
||||
case TweenState.Paused: OnPaused?.Invoke(this); break;
|
||||
case TweenState.Playing:
|
||||
if (previousState == TweenState.Idle)
|
||||
|
||||
@@ -4,35 +4,48 @@ namespace Engine.Systems.Tween;
|
||||
|
||||
public static class TweenExtensions
|
||||
{
|
||||
private static readonly System.Collections.Generic.Dictionary<ITween, int> loopDictionary = [];
|
||||
|
||||
public static ITween Loop(this ITween tween, int count)
|
||||
{
|
||||
Tween tweenConcrete = (Tween)tween;
|
||||
int counter = count;
|
||||
if (!loopDictionary.TryAdd(tween, count))
|
||||
throw new($"Tween already has a loop in progress.");
|
||||
|
||||
tweenConcrete.OnCompleted.AddListener(_ =>
|
||||
{
|
||||
if (counter-- <= 0)
|
||||
return;
|
||||
|
||||
tweenConcrete.Reset();
|
||||
tweenConcrete.State = TweenState.Playing;
|
||||
});
|
||||
tween.OnCompleted.AddListener(looperDelegate);
|
||||
tween.OnEnded.AddListener(looperEndDelegate);
|
||||
|
||||
return tween;
|
||||
}
|
||||
|
||||
private static readonly Core.Event<ITween>.EventHandler looperEndDelegate = sender => loopDictionary.Remove(sender);
|
||||
private static readonly Core.Event<ITween>.EventHandler looperDelegate = sender =>
|
||||
{
|
||||
int counter = loopDictionary[sender] = loopDictionary[sender] - 1;
|
||||
|
||||
if (counter <= 0)
|
||||
{
|
||||
loopDictionary.Remove(sender);
|
||||
return;
|
||||
}
|
||||
|
||||
Tween tweenConcrete = (Tween)sender;
|
||||
tweenConcrete.Reset();
|
||||
tweenConcrete.State = TweenState.Playing;
|
||||
};
|
||||
|
||||
public static ITween LoopInfinitely(this ITween tween)
|
||||
{
|
||||
Tween tweenConcrete = (Tween)tween;
|
||||
tweenConcrete.OnCompleted.AddListener(_ =>
|
||||
{
|
||||
tweenConcrete.Reset();
|
||||
tweenConcrete.State = TweenState.Playing;
|
||||
});
|
||||
|
||||
tween.OnCompleted.AddListener(repeaterDelegate);
|
||||
return tween;
|
||||
}
|
||||
|
||||
private static readonly Core.Event<ITween>.EventHandler repeaterDelegate = sender =>
|
||||
{
|
||||
Tween tweenConcrete = (Tween)sender;
|
||||
tweenConcrete.Reset();
|
||||
tweenConcrete.State = TweenState.Playing;
|
||||
};
|
||||
|
||||
public static ITween Ease(this ITween tween, IEasing easing)
|
||||
{
|
||||
Tween tweenConcrete = (Tween)tween;
|
||||
|
||||
@@ -2,4 +2,4 @@ using Engine.Core;
|
||||
|
||||
namespace Engine.Systems.Tween;
|
||||
|
||||
public class WaitWhileTweenActiveCoroutineYield(ITween tween) : WaitUntilYield(() => tween.State.CheckFlag(TweenState.Completed | TweenState.Cancelled));
|
||||
public class WaitForTweenDoneCoroutineYield(ITween tween) : WaitUntilYield(() => tween.State.CheckFlag(TweenState.Completed | TweenState.Cancelled));
|
||||
|
||||
Reference in New Issue
Block a user