Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up version flags #1047

Merged
merged 19 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion UndertaleModCli/Program.UMTLibInherited.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ void ImportCode(string codeName, string gmlCode, bool isGML = true, bool doParse
methodNumberStr = afterPrefix.Substring(afterPrefix.LastIndexOf("_Collision_") + s2.Length, afterPrefix.Length - (afterPrefix.LastIndexOf("_Collision_") + s2.Length));
methodName = "Collision";
// GMS 2.3+ use the object name for the one colliding, which is rather useful.
if (Data.GMS2_3)
if (Data.IsVersionAtLeast(2, 3))
{
if (Data.GameObjects.ByName(methodNumberStr) != null)
{
Expand Down
50 changes: 27 additions & 23 deletions UndertaleModLib/Compiler/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public List<UndertaleInstruction> Finish()
for (int i = 1; i < locals.Locals.Count; i++)
{
string localName = locals.Locals[i].Name.Content;
if (compileContext.Data?.GMS2_3 != true)
if (CompileContext.GMS2_3 != true)
locals.Locals[i].Index = (uint)i;
if (!compileContext.LocalVars.ContainsKey(localName))
{
Expand All @@ -142,7 +142,7 @@ bool hasLocal(string name)
string name = l.Key;
if (!hasLocal(name))
{
if (variables != null && compileContext?.Data.GMS2_3 == true)
if (variables != null && CompileContext.GMS2_3 == true)
{
UndertaleVariable def = variables.DefineLocal(compileContext.OriginalReferencedLocalVars, 0, name, compileContext.Data.Strings, compileContext.Data);
if (def != null)
Expand Down Expand Up @@ -192,7 +192,7 @@ bool hasLocal(string name)

if (patch.VarType == VariableType.Normal)
patch.Target.TypeInst = InstanceType.Local;
else if (compileContext.Data.GMS2_3)
else if (CompileContext.GMS2_3)
patch.InstType = InstanceType.Self;
}
}
Expand All @@ -217,7 +217,7 @@ bool hasLocal(string name)
// 2.3 variable fix
// Definitely needs at least some change when ++/-- support is added,
// since that does use instance type global
if (compileContext.Data.GMS2_3 &&
if (CompileContext.GMS2_3 &&
patch.VarType == VariableType.Array &&
realInstType == InstanceType.Global)
realInstType = InstanceType.Self;
Expand Down Expand Up @@ -252,7 +252,7 @@ bool hasLocal(string name)
{
patch.Target.ArgumentsCount = (ushort)patch.ArgCount;
def = compileContext.Data.Functions.ByName(patch.Name);
if (compileContext.Data.GMS2_3)
if (CompileContext.GMS2_3)
{
if (def != null && def.Autogenerated)
def = null;
Expand Down Expand Up @@ -870,7 +870,7 @@ private static void AssembleStatement(CodeWriter cw, Parser.Statement s, int rem
Patch endPatch = Patch.Start();
Patch popEnvPatch = Patch.Start();
// Hacky override for @@Other@@ and @@This@@ usage- will likely expand to whatever other cases it turns out the compiler uses.
if (cw.compileContext.Data.GMS2_3 &&
if (CompileContext.GMS2_3 &&
s.Children[0].Kind == Parser.Statement.StatementKind.ExprConstant &&
((InstanceType)s.Children[0].Constant.valueNumber).In(InstanceType.Other, InstanceType.Self))
{
Expand All @@ -887,7 +887,7 @@ private static void AssembleStatement(CodeWriter cw, Parser.Statement s, int rem
var type = cw.typeStack.Pop();
if (type != DataType.Int32)
{
if (cw.compileContext.Data.GMS2_3 && type == DataType.Variable)
if (CompileContext.GMS2_3 && type == DataType.Variable)
cw.Emit(Opcode.PushI, DataType.Int16).Value = (short)-9; // stacktop conversion
else
cw.Emit(Opcode.Conv, type, DataType.Int32);
Expand Down Expand Up @@ -1016,7 +1016,11 @@ private static void AssembleStatement(CodeWriter cw, Parser.Statement s, int rem
else
{
// Returns nothing, basically the same as exit
if (!(cw.compileContext.Data.GMS2_3 && remaining == 1))
// TODO: I'm pretty sure the "remaining" part is actually just because the decompiler keeps adding
// returns and it's necessary to preserve 1:1 compilation
// But this workaround causes issue https://github.com/krzys-h/UndertaleModTool/issues/900
// So it would be fixed by cutting the "remaining" check here and removing the extra from decompilation.
if (!(CompileContext.GMS2_3 && remaining == 1))
AssembleExit(cw);
}
break;
Expand Down Expand Up @@ -1655,14 +1659,14 @@ private static void AssembleVariablePush(CodeWriter cw, Parser.Statement e, out
}

// Special array access- instance type needs to be pushed beforehand
if (cw.compileContext.Data.GMS2_3 && (cw.compileContext.BuiltInList.GlobalArray.ContainsKey(e.Children[0].Text) || cw.compileContext.BuiltInList.GlobalNotArray.ContainsKey(e.Children[0].Text)))
if (CompileContext.GMS2_3 && (cw.compileContext.BuiltInList.GlobalArray.ContainsKey(e.Children[0].Text) || cw.compileContext.BuiltInList.GlobalNotArray.ContainsKey(e.Children[0].Text)))
cw.Emit(Opcode.PushI, DataType.Int16).Value = (short)(e.Children[0].Text == "argument" ? InstanceType.Arg : InstanceType.Builtin); // hack x2
else
cw.Emit(Opcode.PushI, DataType.Int16).Value = (short)e.Children[0].ID;
// Pushing array (incl. 2D) but not popping
AssembleArrayPush(cw, e.Children[0], !duplicate);

if (cw.compileContext.Data.GMS2_3 && e.Children[0].Children.Count > 2)
if (CompileContext.GMS2_3 && e.Children[0].Children.Count > 2)
{
for (int i = 2; i < e.Children[0].Children.Count; i++)
{
Expand All @@ -1672,7 +1676,7 @@ private static void AssembleVariablePush(CodeWriter cw, Parser.Statement e, out
}
if (duplicate)
{
if (cw.compileContext.Data.GMS2_3 && e.Children[0].Children.Count != 1)
if (CompileContext.GMS2_3 && e.Children[0].Children.Count != 1)
{
cw.Emit(Opcode.Dup, DataType.Int32).Extra = 4;
cw.Emit(Opcode.Break, DataType.Int16).Value = (short)-8; // savearef
Expand All @@ -1685,7 +1689,7 @@ private static void AssembleVariablePush(CodeWriter cw, Parser.Statement e, out
cw.Emit(Opcode.Dup, DataType.Int32).Extra = 1;
}
}
if (cw.compileContext.Data.GMS2_3 && e.Children[0].Children.Count > 1)
if (CompileContext.GMS2_3 && e.Children[0].Children.Count > 1)
{
cw.Emit(Opcode.Break, DataType.Int16).Value = (short)-2; // pushaf
}
Expand Down Expand Up @@ -1713,7 +1717,7 @@ private static void AssembleVariablePush(CodeWriter cw, Parser.Statement e, out
case -1:
if (cw.compileContext.BuiltInList.GlobalArray.ContainsKey(name) || cw.compileContext.BuiltInList.GlobalNotArray.ContainsKey(name))
{
if (cw.compileContext.Data.GMS2_3 &&
if (CompileContext.GMS2_3 &&
name.In(
"argument0", "argument1", "argument2", "argument3",
"argument4", "argument5", "argument6", "argument7",
Expand All @@ -1736,7 +1740,7 @@ private static void AssembleVariablePush(CodeWriter cw, Parser.Statement e, out
{
Target = cw.EmitRef(useNoSpecificType ? Opcode.Push : Opcode.PushBltn, DataType.Variable),
Name = name,
InstType = (cw.compileContext.Data.GMS2_3 && !useNoSpecificType) ? InstanceType.Builtin : InstanceType.Self,
InstType = (CompileContext.GMS2_3 && !useNoSpecificType) ? InstanceType.Builtin : InstanceType.Self,
VarType = VariableType.Normal
});
}
Expand Down Expand Up @@ -1785,7 +1789,7 @@ private static void AssembleVariablePush(CodeWriter cw, Parser.Statement e, out
else
{
AssembleExpression(cw, e.Children[0]);
if (cw.compileContext.Data.GMS2_3 && cw.typeStack.Peek() == DataType.Variable)
if (CompileContext.GMS2_3 && cw.typeStack.Peek() == DataType.Variable)
{
cw.typeStack.Pop();
cw.Emit(Opcode.PushI, DataType.Int16).Value = (short)-9; // stacktop conversion
Expand Down Expand Up @@ -1827,7 +1831,7 @@ private static void AssembleVariablePush(CodeWriter cw, Parser.Statement e, out
{
if (duplicate && next + 1 >= e.Children.Count)
{
cw.Emit(Opcode.Dup, DataType.Int32).Extra = (byte)(cw.compileContext.Data.GMS2_3 ? 4 : 0);
cw.Emit(Opcode.Dup, DataType.Int32).Extra = (byte)(CompileContext.GMS2_3 ? 4 : 0);
}
cw.varPatches.Add(new VariablePatch()
{
Expand Down Expand Up @@ -1891,7 +1895,7 @@ private static void AssembleArrayPush(CodeWriter cw, Parser.Statement a, bool ar
// 2D index
if (a.Children.Count != 1)
{
if (!cw.compileContext.Data.GMS2_3)
if (!CompileContext.GMS2_3)
{
// These instructions are hardcoded. Honestly it seems pretty
// inefficient because these could be easily combined into
Expand Down Expand Up @@ -1926,7 +1930,7 @@ private static void AssembleArrayPush(CodeWriter cw, Parser.Statement a, bool ar
cw.typeStack.Push(DataType.Int32);
}

if (!cw.compileContext.Data.GMS2_3)
if (!CompileContext.GMS2_3)
{
cw.Emit(Opcode.Break, DataType.Int16).Value = (short)-1; // chkindex
cw.Emit(Opcode.Add, DataType.Int32, DataType.Int32);
Expand Down Expand Up @@ -1964,7 +1968,7 @@ private static void AssembleStoreVariable(CodeWriter cw, Parser.Statement s, Dat
if (!skip)
{
// Convert to variable in 2.3
if (cw.compileContext.Data.GMS2_3 && typeToStore != DataType.Variable)
if (CompileContext.GMS2_3 && typeToStore != DataType.Variable)
{
cw.Emit(Opcode.Conv, typeToStore, DataType.Variable);
typeToStore = DataType.Variable;
Expand All @@ -1973,7 +1977,7 @@ private static void AssembleStoreVariable(CodeWriter cw, Parser.Statement s, Dat
AssembleArrayPush(cw, s.Children[0]);
}

if (cw.compileContext.Data.GMS2_3 && s.Children[0].Children.Count != 1)
if (CompileContext.GMS2_3 && s.Children[0].Children.Count != 1)
{
if (duplicate)
{
Expand Down Expand Up @@ -2009,7 +2013,7 @@ private static void AssembleStoreVariable(CodeWriter cw, Parser.Statement s, Dat
int id = s.Children[0].ID;
if (id >= 100000)
id -= 100000;
if (cw.compileContext.Data.GMS2_3 && (cw.compileContext.BuiltInList.GlobalArray.ContainsKey(s.Children[0].Text) || cw.compileContext.BuiltInList.GlobalNotArray.ContainsKey(s.Children[0].Text)))
if (CompileContext.GMS2_3 && (cw.compileContext.BuiltInList.GlobalArray.ContainsKey(s.Children[0].Text) || cw.compileContext.BuiltInList.GlobalNotArray.ContainsKey(s.Children[0].Text)))
{
if (s.Children[0].Text.In(
"argument0", "argument1", "argument2", "argument3",
Expand Down Expand Up @@ -2052,13 +2056,13 @@ private static void AssembleStoreVariable(CodeWriter cw, Parser.Statement s, Dat
if (!skip)
{
// Convert to variable in 2.3
if (cw.compileContext.Data.GMS2_3 && typeToStore != DataType.Variable && s.Children.Last().Children.Count != 0)
if (CompileContext.GMS2_3 && typeToStore != DataType.Variable && s.Children.Last().Children.Count != 0)
{
cw.Emit(Opcode.Conv, typeToStore, DataType.Variable);
typeToStore = DataType.Variable;
}
AssembleExpression(cw, s.Children[0]);
if (cw.compileContext.Data.GMS2_3 && cw.typeStack.Peek() == DataType.Variable)
if (CompileContext.GMS2_3 && cw.typeStack.Peek() == DataType.Variable)
{
cw.typeStack.Pop();
cw.Emit(Opcode.PushI, DataType.Int16).Value = (short)-9; // stacktop conversion
Expand Down
4 changes: 2 additions & 2 deletions UndertaleModLib/Compiler/BuiltinList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ public void Initialize(UndertaleData data)
Functions["array_equals"] = new FunctionInfo(this, 2);
Functions["array_create"] = new FunctionInfo(this, -1);
Functions["array_copy"] = new FunctionInfo(this, 5);
if (data?.GMS2_3 == true)
if (data?.IsVersionAtLeast(2, 3) == true)
{
Functions["method"] = new FunctionInfo(this, 2);
Functions["method_get_self"] = new FunctionInfo(this, 1);
Expand Down Expand Up @@ -2090,7 +2090,7 @@ public void Initialize(UndertaleData data)
Functions["buffer_async_group_begin"] = new FunctionInfo(this, 1);
Functions["buffer_async_group_end"] = new FunctionInfo(this, 0);
Functions["buffer_async_group_option"] = new FunctionInfo(this, 2);
Functions["buffer_get_surface"] = new FunctionInfo(this, (data.GMS2_3_1 ? 3 : 5)); // be more robust here
Functions["buffer_get_surface"] = new FunctionInfo(this, (data.IsVersionAtLeast(2, 3, 1) ? 3 : 5)); // be more robust here
Functions["buffer_set_surface"] = new FunctionInfo(this, 5);
Functions["buffer_set_network_safe"] = new FunctionInfo(this, 2);
Functions["buffer_create_from_vertex_buffer"] = new FunctionInfo(this, 3);
Expand Down
3 changes: 2 additions & 1 deletion UndertaleModLib/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class CompileContext
public Dictionary<string, VariableInfo> userDefinedVariables = new Dictionary<string, VariableInfo>();
public bool ensureFunctionsDefined = true;
public bool ensureVariablesDefined = true;
public static bool GMS2_3;
public int LastCompiledArgumentCount = 0;
public Dictionary<string, string> LocalVars = new Dictionary<string, string>();
public Dictionary<string, string> GlobalVars = new Dictionary<string, string>();
Expand Down Expand Up @@ -86,7 +87,7 @@ private void MakeAssetDictionary()
AddAssetsFromList(Data?.Paths);
AddAssetsFromList(Data?.Fonts);
AddAssetsFromList(Data?.Timelines);
if (!(Data?.GMS2_3 ?? false))
if (!GMS2_3)
AddAssetsFromList(Data?.Scripts);
AddAssetsFromList(Data?.Shaders);
AddAssetsFromList(Data?.Rooms);
Expand Down
2 changes: 1 addition & 1 deletion UndertaleModLib/Compiler/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ private static Token ReadIdentifier(CodeReader cr)
"return" => new Token(Token.TokenKind.KeywordReturn, cr.GetPositionInfo(index)),
"default" => new Token(Token.TokenKind.KeywordDefault, cr.GetPositionInfo(index)),
"struct" => new Token(Token.TokenKind.KeywordStruct, cr.GetPositionInfo(index)),
"function" when cr.compileContext.Data.GMS2_3 => new Token(Token.TokenKind.KeywordFunction, cr.GetPositionInfo(index)),
"function" when CompileContext.GMS2_3 => new Token(Token.TokenKind.KeywordFunction, cr.GetPositionInfo(index)),
"for" => new Token(Token.TokenKind.KeywordFor, cr.GetPositionInfo(index)),
"case" => new Token(Token.TokenKind.KeywordCase, cr.GetPositionInfo(index)),
"switch" => new Token(Token.TokenKind.KeywordSwitch, cr.GetPositionInfo(index)),
Expand Down
Loading