@@ -13,14 +13,20 @@ namespace GS2Engine.GS2.Script
1313{
1414 public class Script
1515 {
16- private readonly List < TString > _strings = new ( ) ;
17- public readonly Dictionary < string , FunctionParams > Functions = new ( ) ;
18- public IDictionary < string , VariableCollection > GlobalObjects = new Dictionary < string , VariableCollection > ( ) ;
19- public static VariableCollection GlobalVariables = new ( ) ;
16+ public delegate IStackEntry Command ( ScriptMachine machine , IStackEntry [ ] ? args ) ;
2017
18+ public static VariableCollection GlobalVariables = new ( ) ;
19+ private readonly List < TString > _strings = new ( ) ;
20+ public readonly Dictionary < string , FunctionParams > Functions = new ( ) ;
2121 private ScriptCom [ ] _bytecode = Array . Empty < ScriptCom > ( ) ;
22-
23- public Script ( TString bytecodeFile , IDictionary < string , VariableCollection > ? objects , VariableCollection ? variables , Dictionary < string , Command > ? functions )
22+ public IDictionary < string , VariableCollection > GlobalObjects = new Dictionary < string , VariableCollection > ( ) ;
23+
24+ public Script (
25+ TString bytecodeFile ,
26+ IDictionary < string , VariableCollection > ? objects ,
27+ VariableCollection ? variables ,
28+ Dictionary < string , Command > ? functions
29+ )
2430 {
2531 Name = Path . GetFileNameWithoutExtension ( bytecodeFile ) ;
2632 File = bytecodeFile ;
@@ -29,24 +35,49 @@ public Script(TString bytecodeFile, IDictionary<string, VariableCollection>? obj
2935
3036 Init ( objects , variables , functions ) ;
3137 }
32-
33- public void UpdateFromFile ( string scriptFile , IDictionary < string , VariableCollection > ? objects , VariableCollection ? variables , Dictionary < string , Command > ? functions )
38+
39+ private int BytecodeLength => _bytecode . Length ;
40+
41+ public TString Name { get ; set ; }
42+ public TString File { get ; set ; }
43+ private int Gs1Flags { get ; set ; }
44+ public ScriptMachine Machine { get ; }
45+ private DateTime ? Timer { get ; set ; }
46+
47+ public ScriptCom [ ] Bytecode => _bytecode ;
48+ public Dictionary < string , Command > ExternalFunctions { get ; } = new ( ) ;
49+
50+ public void UpdateFromFile (
51+ string scriptFile ,
52+ IDictionary < string , VariableCollection > ? objects ,
53+ VariableCollection ? variables ,
54+ Dictionary < string , Command > ? functions
55+ )
3456 {
3557 Name = Path . GetFileNameWithoutExtension ( scriptFile ) ;
3658 File = scriptFile ;
3759 SetStream ( ReadAllBytes ( scriptFile ) ) ;
38-
60+
3961 Init ( objects , variables , functions ) ;
4062 }
4163
42- public void UpdateFromByteCode ( byte [ ] byteCode , IDictionary < string , VariableCollection > ? objects , VariableCollection ? variables , Dictionary < string , Command > ? functions )
64+ public void UpdateFromByteCode (
65+ byte [ ] byteCode ,
66+ IDictionary < string , VariableCollection > ? objects ,
67+ VariableCollection ? variables ,
68+ Dictionary < string , Command > ? functions
69+ )
4370 {
4471 SetStream ( byteCode ) ;
4572
4673 Init ( objects , variables , functions ) ;
4774 }
4875
49- private void Init ( IDictionary < string , VariableCollection > ? objects , VariableCollection ? variables , Dictionary < string , Command > ? functions )
76+ private void Init (
77+ IDictionary < string , VariableCollection > ? objects ,
78+ VariableCollection ? variables ,
79+ Dictionary < string , Command > ? functions
80+ )
5081 {
5182 if ( objects != null )
5283 GlobalObjects = objects ;
@@ -56,39 +87,31 @@ private void Init(IDictionary<string, VariableCollection>? objects, VariableColl
5687
5788 if ( functions != null )
5889 foreach ( KeyValuePair < string , Command > obj in functions )
59- ExternalFunctions . Add ( obj . Key , obj . Value ) ;
90+ ExternalFunctions ? . Add ( obj . Key , obj . Value ) ;
6091
61- ExternalFunctions . Add ( "settimer" , delegate ( ScriptMachine machine , IStackEntry [ ] ? args )
62- {
63- if ( args ? . Length > 0 )
64- SetTimer ( ( double ) ( machine . GetEntry ( args [ 0 ] ) . GetValue ( ) ?? 0 ) ) ;
65- return 0 . ToStackEntry ( ) ;
66- } ) ;
92+ ExternalFunctions ? . Add (
93+ "settimer" ,
94+ delegate ( ScriptMachine machine , IStackEntry [ ] ? args )
95+ {
96+ if ( args ? . Length > 0 )
97+ SetTimer ( ( double ) ( machine . GetEntry ( args [ 0 ] ) . GetValue ( ) ?? 0 ) ) ;
98+ return 0 . ToStackEntry ( ) ;
99+ }
100+ ) ;
67101
68102 Execute ( "onCreated" ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
69103 }
70- public delegate IStackEntry Command ( ScriptMachine machine , IStackEntry [ ] ? args ) ;
104+
71105 private void Reset ( )
72106 {
73107 Machine . Reset ( ) ;
74108 //GlobalVariables.Clear();
75109 Functions . Clear ( ) ;
76110 GlobalObjects . Clear ( ) ;
77- ExternalFunctions . Clear ( ) ;
111+ ExternalFunctions ? . Clear ( ) ;
78112 _bytecode = Array . Empty < ScriptCom > ( ) ;
79113 }
80114
81- private int BytecodeLength => _bytecode . Length ;
82-
83- public TString Name { get ; set ; }
84- public TString File { get ; set ; }
85- private int Gs1Flags { get ; set ; }
86- public ScriptMachine Machine { get ; }
87- private DateTime ? Timer { get ; set ; }
88-
89- public ScriptCom [ ] Bytecode => _bytecode ;
90- public Dictionary < string , Command > ExternalFunctions { get ; } = new ( ) ;
91-
92115
93116 private void SetStream ( TString bytecodeParam )
94117 {
@@ -217,7 +240,7 @@ private void SetStream(TString bytecodeParam)
217240 }
218241 case 0xF3 :
219242 {
220- byte varIndex = segmentSection . readChar ( ) ;
243+ sbyte varIndex = ( sbyte ) segmentSection . readChar ( ) ;
221244 op . Value = varIndex ;
222245 Tools . Debug ( $ " - double({ op . Value } ) (byte)\n ") ;
223246 break ;
@@ -326,11 +349,20 @@ private static void onScriptUpdated()
326349
327350 private static void optimizeByteCode ( )
328351 {
329-
330352 }
331353
332- private async Task < IStackEntry > Execute ( string functionName , Stack < IStackEntry > ? parameters = null ) =>
333- await Machine . Execute ( functionName , parameters ) ;
354+ private async Task < IStackEntry > Execute ( string functionName , Stack < IStackEntry > ? parameters = null )
355+ {
356+ try
357+ {
358+ return await Machine . Execute ( functionName , parameters ) ;
359+ }
360+ catch ( Exception e )
361+ {
362+ Tools . DebugLine ( e . Message ) ;
363+ return 0 . ToStackEntry ( ) ;
364+ }
365+ }
334366
335367 /// <summary>
336368 /// Function -> Call Event for Object
@@ -394,14 +426,14 @@ public async Task<IStackEntry> TriggerEvent(string eventName)
394426 }
395427
396428 private void SetTimer ( double value ) => Timer = DateTime . UtcNow . AddSeconds ( value ) ;
397-
429+
398430
399431 public async Task < IStackEntry > RunEvents ( )
400432 {
401433 if ( Timer <= DateTime . UtcNow )
402434 {
403435 Timer = null ;
404- return await Execute ( "onTimeout" ) ;
436+ return await Execute ( "onTimeout" ) . ConfigureAwait ( false ) ;
405437 }
406438
407439 return 0 . ToStackEntry ( ) ;
0 commit comments