forked from ZeraGmbH/Blockly.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptCommon.cs
More file actions
168 lines (137 loc) · 5.09 KB
/
ScriptCommon.cs
File metadata and controls
168 lines (137 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using BlocklyNet.Scripting.Engine;
using BlocklyNet.Scripting.Logging;
namespace BlocklyNet.Scripting;
/// <summary>
/// Describes an active script.
/// </summary>
/// <typeparam name="TOption">Option class to use.</typeparam>
/// <typeparam name="TLogType">Type of each log entry.</typeparam>
/// <typeparam name="TModifierType">Implementation class of log modifiers.</typeparam>
public abstract class Script<TOption, TLogType, TModifierType> : Script, IScriptInstance<TLogType>, IScript<TLogType>
where TOption : StartScriptOptions
where TLogType : ScriptLoggingResult, new()
where TModifierType : ScriptLogModifier
{
private readonly TOption? _options;
/// <summary>
/// Must only be created inside this assembly.
/// </summary>
/// <param name="options">Options to use.</param>
public Script(TOption? options)
{
_options = options;
ResultForLogging = CreateResultForLogging();
}
/// <inheritdoc/>
public override TOption? Options => _options;
/// <inheritdoc/>
public TLogType ResultForLogging { get; set; }
/// <summary>
/// Create a brand new empty log entry.
/// </summary>
/// <returns>Empty log entry.</returns>
protected virtual TLogType CreateResultForLogging() => new();
/// <summary>
/// Identifier of the last log created in no-log
/// operation mode - pure imaginary and more or
/// less useless.
/// </summary>
private string? _lastLogId;
/// <summary>
/// Internal reset of state.
/// </summary>
protected virtual Task ReinitializeAsync()
{
ResultForLogging = CreateResultForLogging();
_ActiveGroups.Clear();
_Modifiers.Clear();
_lastLogId = null;
return Task.CompletedTask;
}
/// <summary>
/// All side-effects applied to the log entry during
/// script execution - not used in no-log base
/// operation mode.
/// </summary>
private readonly List<TModifierType> _Modifiers = [];
/// <summary>
/// Register a side-effect.
/// </summary>
/// <param name="modifier">Side effect to apply to the
/// log entry and remember for replay.</param>
protected Task RegisterModifierAsync(TModifierType modifier)
{
// Remember for later replay and execute now.
_Modifiers.Add(modifier);
return modifier.ApplyAsync(this, true);
}
/// <summary>
/// Wrapper class for replay information on side-effects
/// on the log entry.
/// </summary>
private class CustomGroupInformation
{
/// <summary>
/// Number of modfiers prior to these ones.
/// </summary>
public int ModifierIndex { get; set; }
/// <summary>
/// List of modifiers for a single execution group.
/// </summary>
public List<TModifierType> Modifiers { get; set; } = [];
}
/// <summary>
/// Replay information for each execution group.
/// </summary>
private readonly Stack<CustomGroupInformation> _ActiveGroups = [];
/// <summary>
/// Decode list of modifiers on this script.
/// </summary>
/// <param name="status">Some status information.</param>
/// <returns>List of modifiers.</returns>
protected IEnumerable<TModifierType> GetModifiers<TChild>(GroupStatus<TChild> status) where TChild : GroupStatus<TChild>
=> BlockyUtils.Decompress<CustomGroupInformation>(status.CustomizerBlob)?.Modifiers ?? Enumerable.Empty<TModifierType>();
/// <inheritdoc/>
public override async Task BeginGroupExecutionAsync(GroupStatus status, bool repeat)
{
if (repeat)
foreach (var modifier in GetModifiers(status))
await modifier.ApplyAsync(this, false);
else
_ActiveGroups.Push(new() { ModifierIndex = _Modifiers.Count });
}
/// <inheritdoc/>
public override Task EndGroupExecutionAsync(GroupStatus status)
{
/* Get the side effects create by this group. */
var info = _ActiveGroups.Pop();
info.Modifiers.AddRange(_Modifiers.Skip(info.ModifierIndex));
/* Serialize in group. */
status.CustomizerBlob = BlockyUtils.Compress(info);
return Task.CompletedTask;
}
/// <inheritdoc/>
public override void SetGroups(ScriptGroupStatus? status)
=> ResultForLogging.GroupsFinished = status;
/// <inheritdoc/>
public virtual Task SetResultAsync(ScriptExecutionResultTypes result)
{
/* In no-log mode just update the entry. */
ResultForLogging.Result = result;
return Task.CompletedTask;
}
/// <inheritdoc/>
public virtual Task<string> WriteToLogAsync()
{
/* In no-log mode just generate and keep a unique identifier for the log entry. */
if (string.IsNullOrEmpty(_lastLogId)) _lastLogId = Guid.NewGuid().ToString();
return Task.FromResult(_lastLogId);
}
/// <inheritdoc/>
public virtual Task RegisterChildAsync(string id)
{
/* In no-log mode just remember the log entry in the hierarchy of nested scripts. */
ResultForLogging.Children.Add(id);
return Task.CompletedTask;
}
}