@@ -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