Compare commits
7 Commits
499f875903
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
| 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 : EngineTypeYamlSerializerBase<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})"));
|
||||
}
|
||||
}
|
||||
@@ -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)!;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user