Skip to content

Commit 2113f38

Browse files
committed
Minor refactoring
1 parent 02eeae6 commit 2113f38

7 files changed

Lines changed: 81 additions & 30 deletions

File tree

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ bin/
22
obj/
33
/packages/
44
riderModule.iml
5-
/_ReSharper.Caches/
5+
/_ReSharper.Caches/
6+
*.user
7+
.idea/
8+
.vs/
9+
Testing/

GS2Engine/GS2/Script/Script.cs

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

GS2Engine/GS2/Script/ScriptMachine.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public async Task<IStackEntry> Execute(string functionName, Stack<IStackEntry>?
165165
IStackEntry ret = 0.ToStackEntry();
166166
if (stack.Count > 0)
167167
ret = stack.Pop();
168-
Tools.DebugLine(JsonConvert.SerializeObject(_script.Variables.GetDictionary(), Formatting.Indented));
168+
Tools.DebugLine(JsonConvert.SerializeObject(_script.GlobalVariables.GetDictionary(), Formatting.Indented));
169169
return ret;
170170
case Opcode.OP_SLEEP:
171171
double sleep = getEntryValue<double>(stack.Pop());
@@ -297,7 +297,7 @@ public async Task<IStackEntry> Execute(string functionName, Stack<IStackEntry>?
297297
}
298298
else if (!_useTemp)
299299
{
300-
_script.Variables.AddOrUpdate((variable.GetValue() ?? "").ToString().ToLower(), val);
300+
_script.GlobalVariables.AddOrUpdate((variable.GetValue() ?? "").ToString().ToLower(), val);
301301
}
302302
else
303303
{
@@ -487,7 +487,7 @@ public async Task<IStackEntry> Execute(string functionName, Stack<IStackEntry>?
487487
case Opcode.OP_ARRAY:
488488
double arrayIndex = getEntryValue<double>(stack.Pop());
489489
object? array = getEntryValue<object>(stack.Pop());
490-
490+
491491
if (array?.GetType() == typeof(List<string>))
492492
{
493493
stack.Push(((List<string>?)array)?[(int)arrayIndex].ToStackEntry() ?? new object().ToStackEntry());
@@ -501,7 +501,6 @@ public async Task<IStackEntry> Execute(string functionName, Stack<IStackEntry>?
501501
stack.Push(((List<object>?)array)?[(int)arrayIndex].ToStackEntry() ?? new object().ToStackEntry());
502502
}
503503

504-
505504
break;
506505
case Opcode.OP_ARRAY_ASSIGN:
507506
break;
@@ -532,15 +531,15 @@ public async Task<IStackEntry> Execute(string functionName, Stack<IStackEntry>?
532531
case Opcode.OP_FOREACH:
533532
break;
534533
case Opcode.OP_THIS:
535-
stack.Push(new StackEntry(StackEntryType.Array, _script.Variables));
534+
stack.Push(new StackEntry(StackEntryType.Array, _script.GlobalVariables));
536535
break;
537536
case Opcode.OP_THISO:
538537
break;
539538
case Opcode.OP_PLAYER:
540539
//_script.Objects[p]
541540
stack.Push(
542-
_script.Objects.ContainsKey("player")
543-
? new(Player, _script.Objects["player"])
541+
_script.GlobalObjects.ContainsKey("player")
542+
? new(Player, _script.GlobalObjects["player"])
544543
: 0.ToStackEntry()
545544
);
546545
break;
@@ -572,8 +571,8 @@ when _tempVariables.ContainsVariable(stackEntry.GetValue<TString>()?.ToLower() ?
572571
_useTemp = false;
573572
return _tempVariables.GetVariable(stackEntry.GetValue<TString>()?.ToLower() ?? string.Empty);
574573
case Variable
575-
when _script.Variables.ContainsVariable(stackEntry.GetValue<TString>()?.ToLower() ?? string.Empty):
576-
return _script.Variables[stackEntry.GetValue<TString>()?.ToLower() ?? string.Empty];
574+
when _script.GlobalVariables.ContainsVariable(stackEntry.GetValue<TString>()?.ToLower() ?? string.Empty):
575+
return _script.GlobalVariables[stackEntry.GetValue<TString>()?.ToLower() ?? string.Empty];
577576
default:
578577
return stackEntry;
579578
}

GS2Engine/Models/Control.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GS2Engine.Models
2+
{
3+
public class Control
4+
{
5+
6+
}
7+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace GS2Engine
1+
namespace GS2Engine.Models
22
{
33
public struct FunctionParams
44
{

GS2Engine/TString.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45
using System.Text;
@@ -41,7 +42,7 @@ private void AddBuffer(string input, int length = 0)
4142
}
4243
}
4344

44-
private void AddBuffer(byte[] input, int start, int length = 0)
45+
private void AddBuffer(IReadOnlyList<byte> input, int start, int length = 0)
4546
{
4647
Array.Resize(ref buffer, stringLength + length);
4748
for (int i = start; i < start + length; i++)

GS2Engine/Tools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private static string ReplaceMetaCharsMatch(Match m)
156156

157157
#endregion
158158

159-
public static bool DEBUG_ON = false;
159+
public static bool DEBUG_ON { get; set; } = false;
160160

161161
public static void Debug(string? text)
162162
{

0 commit comments

Comments
 (0)