Skip to content

Commit 15adbaa

Browse files
Merge pull request #10 from CoderGamester/develop
Release 0.5.2
2 parents 7675691 + f36fe90 commit 15adbaa

File tree

6 files changed

+84
-20
lines changed

6 files changed

+84
-20
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ All notable changes to this package will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.5.2] - 2020-09-27
8+
9+
- Added *IStatechartEvent* data to the logs
10+
- Added logs to all possible trigger cases
11+
- Added logs to the *ITaskWaitState* and *IWaitState* waiting call method
12+
- Added exception catch to the *ITaskWaitState* and *IWaitState* waiting call method
13+
- Added the possibility to enable logs individually to each *IState*
14+
715
## [0.5.1] - 2020-09-24
816

917
**Fixed**
10-
- Fixed *ITaskWaitState* execution crash
18+
- Fixed *ITaskWaitState* execution crash
1119

1220
## [0.5.0] - 2020-09-24
1321

Runtime/IState.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ namespace GameLovers.Statechart
1010
/// </summary>
1111
public interface IState
1212
{
13+
/// <summary>
14+
/// Allows to show logs in the console to help debugging possible errors in this specific state
15+
/// </summary>
16+
bool LogsEnabled { get; set; }
1317
}
1418

1519
#region Compositions

Runtime/Internal/StateInternal.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ internal abstract class StateInternal : IStateInternal
5252

5353
private static uint _idRef;
5454

55+
/// <inheritdoc />
56+
public bool LogsEnabled { get; set; }
5557
/// <inheritdoc />
5658
public uint Id { get; }
5759
/// <inheritdoc />
@@ -61,6 +63,8 @@ internal abstract class StateInternal : IStateInternal
6163
/// <inheritdoc />
6264
public string CreationStackTrace { get; }
6365

66+
protected bool IsStateLogsEnabled => LogsEnabled || _stateFactory.Data.Statechart.LogsEnabled;
67+
6468
protected StateInternal(string name, IStateFactoryInternal stateFactory)
6569
{
6670
Id = ++_idRef;
@@ -80,25 +84,36 @@ public IStateInternal Trigger(IStatechartEvent statechartEvent)
8084

8185
if (transition == null)
8286
{
87+
if (IsStateLogsEnabled)
88+
{
89+
Debug.Log($"'{statechartEvent?.Name}' does not trigger any transition in state '{Name}'");
90+
}
91+
8392
return null;
8493
}
8594

8695
var nextState = transition.TargetState;
8796

8897
if (Equals(nextState))
8998
{
99+
if (IsStateLogsEnabled)
100+
{
101+
Debug.Log($"'{statechartEvent?.Name}' event triggers the transition in state'{Name}' to" +
102+
$"itself because it's of type {GetType().UnderlyingSystemType.Name}");
103+
}
104+
90105
return nextState;
91106
}
92107

93-
if (transition.TargetState == null)
108+
if (nextState == null)
94109
{
95-
TriggerTransition(transition);
110+
TriggerTransition(transition, statechartEvent?.Name);
96111

97112
return null;
98113
}
99114

100115
TriggerExit();
101-
TriggerTransition(transition);
116+
TriggerTransition(transition, statechartEvent?.Name);
102117
TriggerEnter(nextState);
103118

104119
return nextState;
@@ -141,9 +156,9 @@ private void TriggerEnter(IStateInternal state)
141156
{
142157
try
143158
{
144-
if (_stateFactory.Data.Statechart.LogsEnabled)
159+
if (IsStateLogsEnabled)
145160
{
146-
Debug.LogFormat("Entering '{0}'", state.Name);
161+
Debug.Log($"Entering '{state.Name}'");
147162
}
148163
state.Enter();
149164
}
@@ -157,9 +172,9 @@ private void TriggerExit()
157172
{
158173
try
159174
{
160-
if(_stateFactory.Data.Statechart.LogsEnabled)
175+
if(IsStateLogsEnabled)
161176
{
162-
Debug.LogFormat("Exiting '{0}'", Name);
177+
Debug.LogFormat($"Exiting '{Name}'");
163178
}
164179
Exit();
165180
}
@@ -169,14 +184,17 @@ private void TriggerExit()
169184
}
170185
}
171186

172-
private void TriggerTransition(ITransitionInternal transition)
187+
private void TriggerTransition(ITransitionInternal transition, string eventName)
173188
{
174189
try
175190
{
176-
if (_stateFactory.Data.Statechart.LogsEnabled)
191+
if (IsStateLogsEnabled)
177192
{
178-
Debug.Log($"Transition '{Name}' -> '{transition.TargetState?.Name}'");
193+
var targetState = transition.TargetState?.Name ?? "only invokes OnTransition() without moving to new state";
194+
195+
Debug.Log($"'{eventName}' event triggers the transition '{Name}' -> '{targetState}'");
179196
}
197+
180198
transition.TriggerTransition();
181199
}
182200
catch (Exception e)

Runtime/Internal/TaskWaitState.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4+
using UnityEngine;
45

56
// ReSharper disable CheckNamespace
67

@@ -119,20 +120,34 @@ protected override ITransitionInternal OnTrigger(IStatechartEvent statechartEven
119120

120121
if (!_triggered)
121122
{
122-
InnerTaskAwait(_taskAwaitAction);
123123
_triggered = true;
124+
InnerTaskAwait(statechartEvent?.Name);
124125
}
125126

126127
return _completed ? _transition : null;
127128
}
128129

129-
private async void InnerTaskAwait(Func<Task> taskAwaitAction)
130+
private async void InnerTaskAwait(string eventName)
130131
{
131132
var currentExecution = _executionCount;
132133

133134
_executionCount++;
134-
135-
await taskAwaitAction();
135+
136+
try
137+
{
138+
if (IsStateLogsEnabled)
139+
{
140+
Debug.Log($"'{eventName}' event triggers the task wait method '{_taskAwaitAction.Method.Name}'" +
141+
$"from the object {_taskAwaitAction.Target} in the state {Name}");
142+
}
143+
144+
await _taskAwaitAction();
145+
}
146+
catch (Exception e)
147+
{
148+
throw new Exception($"Exception in the state '{Name}', when calling the task wait action {_taskAwaitAction.Method.Name}" +
149+
$"from the object {_taskAwaitAction.Target}.\n" + CreationStackTrace, e);
150+
}
136151

137152
_completed = true;
138153

Runtime/Internal/WaitState.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using GameLovers.Statechart;
4+
using UnityEngine;
45

56
// ReSharper disable CheckNamespace
67

@@ -129,13 +130,31 @@ protected override ITransitionInternal OnTrigger(IStatechartEvent statechartEven
129130
if (!_triggered)
130131
{
131132
_triggered = true;
132-
_waitingActivity.ExecutionCount = _executionCount;
133-
_executionCount++;
134-
135-
_waitAction(_waitingActivity);
133+
InnerWait(statechartEvent?.Name);
136134
}
137135

138136
return _waitingActivity.IsCompleted && _waitingActivity.ExecutionCount == _executionCount - 1 ? _transition : null;
139137
}
138+
139+
private void InnerWait(string eventName)
140+
{
141+
_waitingActivity.ExecutionCount = _executionCount;
142+
_executionCount++;
143+
144+
try
145+
{
146+
if (IsStateLogsEnabled)
147+
{
148+
Debug.Log($"'{eventName}' event triggers the wait method '{_waitAction.Method.Name}'" +
149+
$"from the object {_waitAction.Target} in the state {Name}");
150+
}
151+
_waitAction(_waitingActivity);
152+
}
153+
catch(Exception e)
154+
{
155+
throw new Exception($"Exception in the state '{Name}', when calling the wait action {_waitAction.Method.Name}" +
156+
$"from the object {_waitAction.Target}.\n" + CreationStackTrace, e);
157+
}
158+
}
140159
}
141160
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.gamelovers.statechart",
33
"displayName": "Statechart",
4-
"version": "0.5.1",
4+
"version": "0.5.2",
55
"unity": "2019.3",
66
"description": "This package allows the use of Statecharts (Hierarchichal State Machine) within an Unity project.\n\nThe primary feature of Statecharts is that states can be organized in a hierarchy.\nA Statecharts is a state machine where each state in the state machine may define its own subordinate state machines, called substates.\nThose states can again define substates.\n\nFor more information: https://statecharts.github.io/what-is-a-statechart.html",
77
"type": "library",

0 commit comments

Comments
 (0)