Skip to content

Commit 0bc1a2a

Browse files
committed
Updates
1 parent 844f65b commit 0bc1a2a

17 files changed

Lines changed: 644 additions & 232 deletions

File tree

GS2Engine.TestApp/Program.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
using System.Reflection;
2-
using GS2Engine;
3-
using GS2Engine.GS2.Script;
4-
1+
using System.Reflection;
2+
using GS2Engine;
3+
using GS2Engine.Extensions;
4+
using GS2Engine.GS2.Script;
5+
using GS2Engine.Models;
6+
using static GS2Engine.GS2.Script.Script;
7+
58
internal class Program
69
{
710
private static async Task Main(string[] args)
811
{
912
Tools.DEBUG_ON = true;
13+
/*
1014
HashSet<Script> scripts = new();
1115
foreach (string file in Directory.GetFiles($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}{Path.DirectorySeparatorChar}scripts"))
1216
{
1317
Console.WriteLine($"File: {file}");
14-
scripts.Add(new(file, null, null, null));
18+
scripts.Add(new Script(file, null, null, null));
1519
}
1620
1721
while (true)
@@ -20,5 +24,24 @@ private static async Task Main(string[] args)
2024
2125
Thread.Sleep(10);
2226
}
27+
*/
28+
string path = $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}{Path.DirectorySeparatorChar}scripts/test.gs2bc";
29+
Dictionary<string, Command> commands = new() {
30+
{
31+
"echo",
32+
delegate (ScriptMachine machine, IStackEntry[]? args)
33+
{
34+
if (args?.Length > 0)
35+
Console.WriteLine(machine.GetEntry(args[0]).GetValue() ?? "");
36+
return 0.ToStackEntry();
37+
}
38+
}
39+
};
40+
Script script = new(path, null, null, commands);
41+
42+
await script.Call("myFunction", new object[] { "test", 1, true });
43+
await script.Call("myFunction", new object[] { "test", 1, false });
44+
await script.Call("myFunction2", new object[] { "test", -0xFF, false, 1.345, true });
45+
2346
}
2447
}
412 Bytes
Binary file not shown.

GS2Engine/Enums/StackEntryType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public enum StackEntryType
99
Array,
1010
ArrayStart,
1111
Player,
12-
Function
12+
Function,
1313
}
1414
}

GS2Engine/Extensions/StackEntryExtensions.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace GS2Engine.Extensions
1010
public static class StackEntryExtensions
1111
{
1212
public static StackEntry ToStackEntry(this object stackObject, bool isVariable = false) =>
13-
new(isVariable?StackEntryType.Variable:GetStackEntryType(stackObject), FixStackValue(stackObject));
13+
new(isVariable ? StackEntryType.Variable : GetStackEntryType(stackObject), FixStackValue(stackObject));
1414

1515
private static object FixStackValue(object stackObject)
1616
{
17-
return stackObject switch
17+
return stackObject switch
1818
{
1919
string => (TString)stackObject.ToString(),
2020
TString => stackObject,
@@ -54,17 +54,22 @@ private static StackEntryType GetStackEntryType(object stackObject)
5454
Type stackType = stackObject.GetType();
5555
if (stackType == typeof(TString))
5656
return StackEntryType.String;
57-
57+
5858
if (stackType == typeof(Script.Command))
5959
return StackEntryType.Function;
60-
61-
if (stackType.GetInterfaces().Any(x => x.Name.Equals("IGuiControl", StringComparison.CurrentCultureIgnoreCase)))
60+
61+
if (stackType.GetInterfaces()
62+
.Any(x => x.Name.Equals("IGuiControl", StringComparison.CurrentCultureIgnoreCase)))
6263
return StackEntryType.Array;
63-
64+
65+
if (stackType == typeof(VariableCollection))
66+
return StackEntryType.Array;
67+
6468
if (stackObject is float)
6569
return StackEntryType.Number;
6670

67-
if (stackType.IsGenericType && stackType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>)))
71+
if (stackType.IsGenericType &&
72+
stackType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>)))
6873
return StackEntryType.Array;
6974

7075
throw new ArgumentOutOfRangeException();

GS2Engine/GS2/ByteCode/Opcode.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public enum Opcode
6666
OP_BWO = 76, // PUSH (S(1) | S(0))
6767
OP_BWA = 77, // PUSH (S(1) & S(0))
6868

69-
OP_IN_RANGE = 80,
70-
OP_IN_OBJ = 81,
71-
OP_OBJ_INDEX = 82,
72-
OP_OBJ_TYPE = 83, // gets the type of the var (float 0, string 1, object 2, array 3)
73-
69+
OP_IN_RANGE = 80,
70+
OP_IN_OBJ = 81,
71+
OP_OBJ_INDEX = 82,
72+
OP_OBJ_TYPE = 83, // gets the type of the var (float 0, string 1, object 2, array 3)
73+
7474
OP_FORMAT = 84,
7575
OP_INT = 85,
7676
OP_ABS = 86,

GS2Engine/GS2/Script/Script.cs

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

Comments
 (0)