Skip to content

Commit 7345ff7

Browse files
fix script stopping
1 parent f1b532a commit 7345ff7

11 files changed

Lines changed: 39 additions & 59 deletions

File tree

Code/ContextSystem/BaseContexts/LoopContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ protected override IEnumerator<float> RunChildren()
4848
{
4949
foreach (var coro in Children
5050
.TakeWhile(_ => !ReceivedBreak)
51-
.TakeWhile(_ => Script.IsRunning)
5251
.Select(child => child.ExecuteBaseContext()))
5352
{
5453
while (coro.MoveNext())

Code/ContextSystem/BaseContexts/StatementContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ protected virtual void OnReceivedControlMessageFromChild(ParentContextControlMes
2222
protected virtual IEnumerator<float> RunChildren()
2323
{
2424
foreach (var coro in Children
25-
.TakeWhile(_ => Script.IsRunning)
2625
.Select(child => child.ExecuteBaseContext()))
2726
{
2827
while (coro.MoveNext())

Code/ContextSystem/Contexts/Control/ElifStatement.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,17 @@ protected override IEnumerator<float> Execute()
8282
var coro = enumerator();
8383
while (coro.MoveNext())
8484
{
85-
if (!Script.IsRunning)
86-
{
87-
yield break;
88-
}
89-
9085
yield return coro.Current;
9186
}
9287

9388
yield break;
9489
}
9590

9691
foreach (var coro in Children
97-
.TakeWhile(_ => Script.IsRunning)
9892
.Select(child => child.ExecuteBaseContext()))
9993
{
10094
while (coro.MoveNext())
10195
{
102-
if (!Script.IsRunning)
103-
{
104-
yield break;
105-
}
106-
10796
yield return coro.Current;
10897
}
10998
}

Code/ContextSystem/Contexts/Control/StopKeyword.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using SER.Code.ContextSystem.Interfaces;
44
using SER.Code.ContextSystem.Structures;
55
using SER.Code.Helpers.ResultSystem;
6+
using SER.Code.ScriptSystem.Structures;
67
using SER.Code.TokenSystem.Tokens;
78

89
namespace SER.Code.ContextSystem.Contexts.Control;
@@ -35,6 +36,6 @@ public override Result VerifyCurrentState()
3536

3637
protected override void Execute()
3738
{
38-
Script.Stop(true);
39+
throw new StopScript();
3940
}
4041
}

Code/ContextSystem/Contexts/FunctionDefinitionContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,12 @@ protected override void OnReceivedControlMessageFromChild(ParentContextControlMe
137137
protected override IEnumerator<float> Execute()
138138
{
139139
foreach (var coro in Children
140-
.TakeWhile(_ => Script.IsRunning)
141140
.Select(child => child.ExecuteBaseContext())
142141
)
143142
{
144143
while (coro.MoveNext())
145144
{
146-
if (!Script.IsRunning || _end)
145+
if (_end)
147146
{
148147
goto Exit;
149148
}

Code/ContextSystem/Extensions/BaseContextExtensions.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@ public static IEnumerator<float> ExecuteBaseContext(this Context context)
1818

1919
case YieldingContext yieldingContext:
2020
var coro = yieldingContext.Run();
21-
while (coro.MoveNext())
22-
{
23-
if (!context.Script.IsRunning)
24-
{
25-
yield break;
26-
}
27-
28-
yield return coro.Current;
29-
}
30-
21+
while (coro.MoveNext()) yield return coro.Current;
3122
yield break;
3223

3324
default:

Code/Helpers/BetterCoros.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using MEC;
22
using SER.Code.Exceptions;
33
using SER.Code.Extensions;
4+
using SER.Code.Plugin;
45
using SER.Code.ScriptSystem;
6+
using SER.Code.ScriptSystem.Structures;
57

68
namespace SER.Code.Helpers;
79

@@ -31,10 +33,19 @@ private static IEnumerator<float> Wrapper(
3133
{
3234
while (true)
3335
{
36+
if (MainPlugin.Instance.Config?.SafeScripts is true)
37+
{
38+
yield return Timing.WaitForOneFrame;
39+
}
40+
3441
try
3542
{
3643
if (!routine.MoveNext()) goto End;
3744
}
45+
catch (StopScript)
46+
{
47+
goto End;
48+
}
3849
catch (ScriptCompileError compErr)
3950
{
4051
onException?.Invoke(compErr);
@@ -59,6 +70,11 @@ private static IEnumerator<float> Wrapper(
5970
scr.Error($"Coroutine failed with {ex.GetType().AccurateName}: {ex.Message}\n{ex.StackTrace}");
6071
goto End;
6172
}
73+
74+
if (scr.Killed)
75+
{
76+
goto End;
77+
}
6278

6379
yield return routine.Current;
6480
}

Code/MethodSystem/Methods/ScriptMethods/RunScriptAndWaitMethod.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using SER.Code.ArgumentSystem.Arguments;
44
using SER.Code.ArgumentSystem.BaseArguments;
55
using SER.Code.MethodSystem.BaseMethods.Yielding;
6-
using SER.Code.ScriptSystem;
76
using SER.Code.ScriptSystem.Structures;
87
using SER.Code.VariableSystem.Bases;
98

@@ -33,8 +32,7 @@ public override IEnumerator<float> Execute()
3332
script.AddLocalVariables(variables);
3433
script.Run(RunReason.Script, Script);
3534

36-
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
37-
while (script.IsRunning)
35+
while (ScriptSystem.Script.RunningScripts.Contains(script))
3836
{
3937
yield return Timing.WaitForOneFrame;
4038
}

Code/MethodSystem/Methods/ScriptMethods/StopScriptMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public class StopScriptMethod : SynchronousMethod
1717

1818
public override void Execute()
1919
{
20-
Args.GetRunningScript("running script").Stop();
20+
Args.GetRunningScript("running script").ExternalStop();
2121
}
2222
}

Code/ScriptSystem/Script.cs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public required ScriptExecutor Executor
5151
public Line[] Lines = [];
5252
public Context[] Contexts = [];
5353

54+
public bool Killed { get; private set; }
55+
5456
public Script? Caller { get; private set; }
5557

5658
public Profile? Profile { get; private set; }
5759

5860
public RunReason RunReason { get; private set; }
5961

6062
public uint CurrentLine { get; set; } = 0;
61-
62-
public bool IsRunning => RunningScripts.Contains(this);
6363

6464
private static readonly List<Script> RunningScriptsList = [];
6565
public static readonly ReadOnlyCollection<Script> RunningScripts = RunningScriptsList.AsReadOnly();
@@ -69,9 +69,7 @@ public required ScriptExecutor Executor
6969

7070
public DateTime StartTime { get; private set; }
7171

72-
public TimeSpan TimeRunning => IsRunning ? DateTime.Now - StartTime : TimeSpan.Zero;
73-
74-
private CoroutineHandle _scriptCoroutine;
72+
public TimeSpan TimeRunning => StartTime == DateTime.MinValue ? TimeSpan.Zero : DateTime.Now - StartTime;
7573

7674
private bool? _isEventAllowed;
7775

@@ -91,7 +89,6 @@ public void Warn(string message)
9189
public void Error(string message)
9290
{
9391
Executor.Error(message, this);
94-
Stop();
9592
}
9693

9794
public static TryGet<Script> CreateByScriptName(string dirtyName, ScriptExecutor? executor)
@@ -129,19 +126,24 @@ public static int StopAll()
129126
var count = RunningScripts.Count;
130127
foreach (var script in new List<Script>(RunningScripts))
131128
{
132-
script.Stop();
129+
script.ExternalStop();
133130
}
134131

135132
return count;
136133
}
134+
135+
public void ExternalStop()
136+
{
137+
Killed = true;
138+
}
137139

138140
public static int StopByName(string name)
139141
{
140142
var matches = new List<Script>(RunningScripts)
141143
.Where(scr => string.Equals(scr.Name, name, StringComparison.CurrentCultureIgnoreCase))
142144
.ToArray();
143145

144-
matches.ForEachItem(scr => scr.Stop());
146+
matches.ForEachItem(scr => scr.ExternalStop());
145147
return matches.Length;
146148
}
147149

@@ -189,22 +191,15 @@ public void Run(RunReason reason = RunReason.Unknown, Script? caller = null)
189191

190192
RunningScriptsList.Add(this);
191193
//Profile = new Profile(this);
192-
_scriptCoroutine = InternalExecute().Run(
193-
this,
194-
_ => _scriptCoroutine.Kill()
195-
//() => Profile.LogResults()
194+
InternalExecute().Run(
195+
this,
196+
null,
197+
() => RunningScriptsList.Remove(this)
196198
);
197199

198200
return _isEventAllowed;
199201
}
200202

201-
public void Stop(bool silent = false)
202-
{
203-
RunningScriptsList.Remove(this);
204-
_scriptCoroutine.Kill();
205-
if (!silent) Logger.Info($"Script {Name} was stopped");
206-
}
207-
208203
public void SendControlMessage(ScriptControlMessage msg)
209204
{
210205
if (msg == ScriptControlMessage.EventNotAllowed)
@@ -314,19 +309,9 @@ private IEnumerator<float> InternalExecute()
314309

315310
foreach (var context in Contexts)
316311
{
317-
if (!IsRunning)
318-
{
319-
break;
320-
}
321-
322312
var handle = context.ExecuteBaseContext();
323313
while (handle.MoveNext())
324314
{
325-
if (!IsRunning)
326-
{
327-
break;
328-
}
329-
330315
yield return handle.Current;
331316
}
332317
}

0 commit comments

Comments
 (0)