@@ -13,10 +13,10 @@ 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 readonly Dictionary < string , VariableCollection > Objects = new ( ) ;
19- public readonly VariableCollection Variables = new ( ) ;
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 VariableCollection GlobalVariables = new ( ) ;
2020
2121 private ScriptCom [ ] _bytecode = Array . Empty < ScriptCom > ( ) ;
2222
@@ -25,7 +25,7 @@ public Script(TString bytecodeFile, IDictionary<string, VariableCollection>? obj
2525 Name = Path . GetFileNameWithoutExtension ( bytecodeFile ) ;
2626 File = bytecodeFile ;
2727 Machine = new ( this ) ;
28- setStream ( ReadAllBytes ( bytecodeFile ) ) ;
28+ SetStream ( ReadAllBytes ( bytecodeFile ) ) ;
2929
3030 Init ( objects , variables , functions ) ;
3131 }
@@ -34,25 +34,25 @@ public void UpdateFromFile(string scriptFile, IDictionary<string, VariableCollec
3434 {
3535 Name = Path . GetFileNameWithoutExtension ( scriptFile ) ;
3636 File = scriptFile ;
37- setStream ( ReadAllBytes ( scriptFile ) ) ;
37+ SetStream ( ReadAllBytes ( scriptFile ) ) ;
3838
3939 Init ( objects , variables , functions ) ;
4040 }
4141
4242 public void UpdateFromByteCode ( byte [ ] byteCode , IDictionary < string , VariableCollection > ? objects , VariableCollection ? variables , Dictionary < string , Command > ? functions )
4343 {
44- setStream ( byteCode ) ;
44+ SetStream ( byteCode ) ;
4545
4646 Init ( objects , variables , functions ) ;
4747 }
4848
4949 private void Init ( IDictionary < string , VariableCollection > ? objects , VariableCollection ? variables , Dictionary < string , Command > ? functions )
5050 {
5151 if ( objects != null )
52- foreach ( KeyValuePair < string , VariableCollection > obj in objects )
53- Objects . Add ( obj . Key , obj . Value ) ;
52+ GlobalObjects = objects ;
5453
55- Variables . AddOrUpdate ( variables ) ;
54+ if ( variables != null )
55+ GlobalVariables = variables ;
5656
5757 if ( functions != null )
5858 foreach ( KeyValuePair < string , Command > obj in functions )
@@ -70,10 +70,10 @@ private void Init(IDictionary<string, VariableCollection>? objects, VariableColl
7070 public delegate IStackEntry Command ( ScriptMachine machine , IStackEntry [ ] ? args ) ;
7171 private void Reset ( )
7272 {
73- Machine ? . Reset ( ) ;
74- Variables . Clear ( ) ;
73+ Machine . Reset ( ) ;
74+ GlobalVariables . Clear ( ) ;
7575 Functions . Clear ( ) ;
76- Objects . Clear ( ) ;
76+ GlobalObjects . Clear ( ) ;
7777 ExternalFunctions . Clear ( ) ;
7878 _bytecode = Array . Empty < ScriptCom > ( ) ;
7979 }
@@ -90,10 +90,11 @@ private void Reset()
9090 public Dictionary < string , Command > ExternalFunctions { get ; } = new ( ) ;
9191
9292
93- private void setStream ( TString bytecodeParam )
93+ private void SetStream ( TString bytecodeParam )
9494 {
9595 int oIndex = 0 ;
9696
97+ CheckHeader ( bytecodeParam ) ;
9798
9899 Reset ( ) ;
99100 bytecodeParam . setRead ( 0 ) ;
@@ -265,6 +266,8 @@ private void setStream(TString bytecodeParam)
265266 Tools . DebugLine ( "Bytecode done" ) ;
266267 break ;
267268 }
269+ default :
270+ throw new ArgumentOutOfRangeException ( ) ;
268271 }
269272 }
270273
@@ -273,6 +276,43 @@ private void setStream(TString bytecodeParam)
273276 onScriptUpdated ( ) ;
274277 }
275278
279+ private static void CheckHeader ( TString bytecodeParam )
280+ {
281+ byte isPacket = bytecodeParam . readChar ( ) ;
282+ if ( isPacket != 0xAC ) return ;
283+
284+ Tools . DebugLine ( "GServer packet header included" ) ;
285+ ushort infoSectionLength = ( ushort ) bytecodeParam . readShort ( ) ;
286+ Tools . DebugLine ( "Length of information section: $infoSectionLength" ) ;
287+
288+ TString infoSection = bytecodeParam . readChars ( infoSectionLength ) ;
289+
290+ string [ ] data = infoSection . ToString ( ) . Split ( ',' ) ;
291+
292+ string target = data [ 0 ] ;
293+ string name = data [ 1 ] ;
294+
295+ Tools . DebugLine ( $ "Code target: { target } ") ;
296+ Tools . DebugLine ( $ "Target name: { name } ") ;
297+
298+ int . TryParse ( data [ 2 ] , out int saveScriptToFileInt ) ;
299+ string saveScriptToFile = saveScriptToFileInt == 1 ? "Yes" : "No" ;
300+
301+ Tools . DebugLine ( $ "Save script to file: { saveScriptToFile } ") ;
302+
303+ /*
304+ infoSection = data[3];
305+ int keys = 0;
306+ while (infoSection.bytesLeft() > 0)
307+ {
308+ uint key = infoSection.readGInt5().toUInt();
309+ //scriptKeys[keys] = CString(key);
310+ Tools.DebugLine($"Key({keys}): {key}");
311+ keys++;
312+ }
313+ */
314+ }
315+
276316 private void addFunction ( TString functionName , int pos , bool isPublic ) =>
277317 Functions . Add ( functionName . ToString ( ) . ToLower ( ) , new ( ) { BytecodePosition = pos , IsPublic = isPublic } ) ;
278318
@@ -369,10 +409,10 @@ public async Task<IStackEntry> RunEvents()
369409
370410 public void AddObjectReference ( string objectType , VariableCollection obj )
371411 {
372- if ( Objects . ContainsKey ( objectType ) )
373- Objects [ objectType ] = obj ;
412+ if ( GlobalObjects . ContainsKey ( objectType ) )
413+ GlobalObjects [ objectType ] = obj ;
374414 else
375- Objects . Add ( objectType , obj ) ;
415+ GlobalObjects . Add ( objectType , obj ) ;
376416 }
377417 }
378418}
0 commit comments