Skip to content

Commit

Permalink
0.2-beta
Browse files Browse the repository at this point in the history
Add EventBus approach
Add SettingsValueAttribute
Minor refactor
  • Loading branch information
decembrist-revolt authored and decembrist-revolt committed May 7, 2021
1 parent fdde581 commit 16de61d
Show file tree
Hide file tree
Showing 28 changed files with 728 additions and 142 deletions.
22 changes: 22 additions & 0 deletions Example/Assertions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Godot;

namespace Decembrist.Example
{
public static class Assertions
{
public static void AssertTrue(bool expression, string test)
{
if (expression)
{
GD.Print($"PASSED:{test}");
}
else
{
var message = $"FAILED:{test}";
GD.PrintErr(message);
throw new Exception(message);
}
}
}
}
59 changes: 59 additions & 0 deletions Example/DiTestNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using Godot;
using Decembrist.Di;
using Decembrist.Example.Service;
using Decembrist.Utils;
using static Decembrist.Example.Assertions;

namespace Decembrist.Example
{
public class DiTestNode : Node2D
{
[Inject] private IService _service1;

[Inject] private IService _service2;

[Inject] private PrototypeService1 _prototypeService1;

[Inject] private PrototypeService1 _prototypeService2;

[Inject] private InstanceService _instanceService;

[SettingsValue(DecembristSettings.EventBusEnabled)]
private bool _eventBusEnabled;

public override void _Ready()
{
// init dependencies
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
this.InjectAll();
watch.Stop();
GD.Print($"Injection time Time: {watch.ElapsedMilliseconds} ms");
GD.Print("Service assertions................................................");
AssertTrue(_service1 != null, "singleton service exists");
AssertTrue(this.Resolve<IService>() != null, "singleton service exists");
AssertTrue(_instanceService == InstanceService.Instance, "service is singleton");
AssertTrue(this.Resolve<IService>() == _service1, "service is singleton");
AssertTrue(_service1 == _service2, "service is singleton");
AssertTrue(this.Resolve<PrototypeService1>() != null, "prototype service exists");
AssertTrue(this.Resolve<PrototypeService1>() != _prototypeService1, "service is prototype");
AssertTrue(_prototypeService1 != _prototypeService2, "service is prototype");
AssertTrue(_prototypeService1.PrototypeService != _prototypeService2.PrototypeService,
"service.PrototypeService is prototype");
AssertTrue(_eventBusEnabled, "settings value check");
GD.Print("Service test......................................................");
ServiceEcho();
}

private void ServiceEcho()
{
GD.Print($"singleton1 says {_service1.GetString()}");
GD.Print($"singleton2 says {_service2.GetString()}");
GD.Print($"resolved singleton says {this.Resolve<IService>().GetString()}");
GD.Print($"prototype1 says {_prototypeService1.GetString()}");
GD.Print($"prototype2 says {_prototypeService2.GetString()}");
GD.Print($"resolved prototype says {this.Resolve<PrototypeService1>().GetString()}");
}
}
}
49 changes: 49 additions & 0 deletions Example/EventBusTest/Consumer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Decembrist.Events;
using Godot;

namespace Decembrist.Example.EventBusTest
{
public class Consumer : Node2D
{
public const string ConsumerAddress1 = "consumer-address1";
public const string ConsumerAddress2 = "consumer-address2";
public const string TestError = "test error";
public const int TestErrorCode = 2;

private EventBusSubscription _subscription1;
private EventBusSubscription _subscription2;

public override void _Ready()
{
var messageCount1 = 0;
_subscription1 = this.Consumer<int, int>(ConsumerAddress1, message =>
{
messageCount1++;
HandleMessage(_subscription1, message, messageCount1);
});
var messageCount2 = 0;
_subscription2 = this.Consumer<int, int>(ConsumerAddress2, message =>
{
messageCount2++;
HandleMessage(_subscription2, message, messageCount2);
});
}

private void HandleMessage(
EventBusSubscription eventBusSubscription,
ReplyEventMessage<int, int> message,
int messageCount)
{
Assertions.AssertTrue(!message.IsError(), "not error message");
if (messageCount > 5)
{
message.ErrorReply("test error", 2);
eventBusSubscription.Stop();
}
else
{
message.Reply(message.Content + 1);
}
}
}
}
56 changes: 56 additions & 0 deletions Example/EventBusTest/Producer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Decembrist.Events;
using Godot;
using static Decembrist.Example.Assertions;

namespace Decembrist.Example.EventBusTest
{
[Tool]
public class Producer : Node2D
{
private readonly List<int> _responses = new();

private string _errorMessage;

private int? _errorCode;

[Export] private string _messageAddress;

public override async void _Ready()
{
await Task.Delay(1000);
GD.Print($"EventBus test started for {_messageAddress}....................");
const int messageCount = 6;
ProduceMessages(messageCount);
await Task.Delay(1000);
AssertTrue(_responses.Count == 5, "all messages consumed");
for (var i = 1; i < messageCount - 1; i++)
{
AssertTrue(_responses[i - 1] == i + 1, "message response ok");
}

AssertTrue(_errorMessage == Consumer.TestError, "event bus error message ok");
AssertTrue(_errorCode == Consumer.TestErrorCode, "event bus error code ok");
GD.Print("EventBus test stopped...........................................");
}

private async void ProduceMessages(int messageCount)
{
for (var i = 1; i <= messageCount; i++)
{
try
{
var response = await this.SendMessageAsync<int, int>(_messageAddress, i);
_responses.Add(response);
}
catch (SendEventException ex)
{
_errorMessage = ex.Message;
_errorCode = ex.GetCode();
}
}
}
}
}
68 changes: 0 additions & 68 deletions Example/TestScene.cs

This file was deleted.

21 changes: 19 additions & 2 deletions Example/TestScene.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=4 format=2]

[ext_resource path="res://Example/TestScene.cs" type="Script" id=1]
[ext_resource path="res://Example/DiTestNode.cs" type="Script" id=1]
[ext_resource path="res://Example/EventBusTest/Producer.cs" type="Script" id=2]
[ext_resource path="res://Example/EventBusTest/Consumer.cs" type="Script" id=3]

[node name="Node2D" type="Node2D"]

[node name="DiTestNode" type="Node2D" parent="."]
script = ExtResource( 1 )

[node name="EventBusTestNode" type="Node2D" parent="."]

[node name="Producer1" type="Node2D" parent="EventBusTestNode"]
script = ExtResource( 2 )
_messageAddress = "consumer-address1"

[node name="Producer2" type="Node2D" parent="EventBusTestNode"]
script = ExtResource( 2 )
_messageAddress = "consumer-address2"

[node name="Consumer" type="Node2D" parent="EventBusTestNode"]
script = ExtResource( 3 )
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
**Godot Decembrist Plugin**
_Utils for godot development_
* (DI) Dependency Injection for Godot
* Util classes for _Buttons_, _Controls_, _Vectors_ etc
* Event driven development
* Util classes for _Buttons_, _Controls_, _Vectors_ etc

[Documentation](https://github.com/decembrist-revolt/godot-decembrist-plugin/wiki)
22 changes: 22 additions & 0 deletions addons/decembrist_plugin/Autoload/AutoloadUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Godot;

namespace Decembrist.Autoload
{
public static class AutoloadUtils
{
/// <summary>
/// Get DiService autoload instance
/// </summary>
/// <param name="node"></param>
/// <returns>DiService singleton instance</returns>
public static DiService GetDiService(this Node node) =>
node.GetNode<DecembristAutoload>("/root/DecembristAutoload").DiService;

/// <summary>
/// Get event bus singleton instance
/// </summary>
/// <returns>EventBus instance</returns>
public static EventBus GetEventBus(this Node node) =>
node.GetNode<DecembristAutoload>("/root/DecembristAutoload").EventBus;
}
}
40 changes: 40 additions & 0 deletions addons/decembrist_plugin/Autoload/DecembristAutoload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Decembrist.Di;
using Godot;

namespace Decembrist.Autoload
{
public class DecembristAutoload: Node
{
[SettingsValue(DecembristSettings.EventBusEnabled)]
private bool _eventBusEnabled;

public readonly DiService DiService;
private EventBus _eventBus;

public EventBus EventBus
{
get
{
if (!_eventBusEnabled)
{
throw new Exception("Event bus disabled");
}
return _eventBus;
}
}

public DecembristAutoload()
{
DiService = new DiService();
AddChild(DiService);
}

public override void _Ready()
{
this.InjectAll();
_eventBus = DiService.Resolve<EventBus>();
AddChild(_eventBus);
}
}
}
Loading

0 comments on commit 16de61d

Please sign in to comment.