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

Correct function definitions and 2.3 FancyRoomSelect #921

Merged
merged 5 commits into from
Jun 11, 2022
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
22 changes: 20 additions & 2 deletions UndertaleModLib/Compiler/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,19 @@ private static Statement ParseFunction(CompileContext context)
{
Statement result = new Statement(Statement.StatementKind.FunctionDef, EnsureTokenKind(TokenKind.KeywordFunction).Token);
Statement args = new Statement();
bool expression = true;
Statement destination = null;

if (GetNextTokenKind() == TokenKind.ProcFunction)
{
expression = false;
Statement s = remainingStageOne.Dequeue();
destination = new Statement(Statement.StatementKind.ExprFuncName, s.Token) { ID = s.ID };
}

EnsureTokenKind(TokenKind.OpenParen);

while (remainingStageOne.Count > 0 && !hasError && !IsNextToken(TokenKind.EOF) && !IsNextToken(TokenKind.CloseParen))
while (remainingStageOne.Count > 0 && !hasError && !IsNextToken(TokenKind.EOF, TokenKind.CloseParen))
{
Statement expr = ParseExpression(context);
if (expr != null)
Expand All @@ -682,7 +691,16 @@ private static Statement ParseFunction(CompileContext context)
if (EnsureTokenKind(TokenKind.CloseParen) == null) return null;

result.Children.Add(ParseStatement(context));
return result;
if (expression)
return result;
else // Whatever you call non-anonymous definitions
{
Statement trueResult = new Statement(Statement.StatementKind.Assign, new Lexer.Token(TokenKind.Assign));
trueResult.Children.Add(destination);
trueResult.Children.Add(new Statement(Statement.StatementKind.Token, trueResult.Token));
trueResult.Children.Add(result);
return trueResult;
}
}

private static Statement ParseFor(CompileContext context)
Expand Down
18 changes: 17 additions & 1 deletion UndertaleModLib/Decompiler/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,14 @@ public override string ToString(DecompileContext context)
}
}

// Someone enlighten me on structs, I'm steering clear for now.
// And find the "right" way to do this.
if (Value is FunctionDefinition functionVal && functionVal.Subtype != FunctionDefinition.FunctionType.Struct)
{
functionVal.IsStatement = true;
return functionVal.ToString(context);
}

string varPrefix = (HasVarKeyword ? "var " : "");

// Check for possible ++, --, or operation equal (for single vars)
Expand Down Expand Up @@ -1306,6 +1314,7 @@ public enum FunctionType
public UndertaleCode FunctionBodyCodeEntry { get; private set; }
public Block FunctionBodyEntryBlock { get; private set; }
public FunctionType Subtype { get; private set; }
public bool IsStatement = false; // I know it's an expression, yes. But I'm not duplicating the rest.

internal List<Expression> Arguments;

Expand Down Expand Up @@ -1354,12 +1363,19 @@ public override string ToString(DecompileContext context)
FunctionDefinition def;
var oldDecompilingStruct = context.DecompilingStruct;
var oldReplacements = context.ArgumentReplacements;

if (Subtype == FunctionType.Struct)
context.DecompilingStruct = true;
else
{
context.DecompilingStruct = false;
sb.Append("function(");
sb.Append("function");
if (IsStatement)
{
sb.Append(" ");
sb.Append((context.Statements[0].Last() as AssignmentStatement).Destination.Var.Name.Content);
}
sb.Append("(");
for (int i = 0; i < FunctionBodyCodeEntry.ArgumentsCount; ++i)
{
if (i != 0)
Expand Down
2 changes: 1 addition & 1 deletion UndertaleModLib/Models/UndertaleRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ protected void OnPropertyChanged([CallerMemberName] string name = null)
// GMS 2022.1+
public bool EffectEnabled { get; set; }
public UndertaleString EffectType { get; set; }
public UndertaleSimpleList<EffectProperty> EffectProperties { get; set; }
public UndertaleSimpleList<EffectProperty> EffectProperties { get; set; } = new();

public void UpdateParentRoom()
{
Expand Down
38 changes: 21 additions & 17 deletions UndertaleModTool/Scripts/CommunityScripts/FancyRoomSelect.csx
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
//Originally created by _creepersbane#2074
//Adapted to work with all games by Grossley
//Adapted to GMS 2.3+ by Space Core/Jacky720

EnsureDataLoaded();

if (Data?.GeneralInfo?.DisplayName?.Content.ToLower() == "deltarune chapter 1 & 2")
{
ScriptError("Error 0: Incompatible with the new Deltarune Chapter 1 & 2 demo");
return;
}
else if (Data?.GeneralInfo?.DisplayName?.Content.ToLower() == "deltarune chapter 1&2")
{
ScriptError("Error 1: Incompatible with the new Deltarune Chapter 1 & 2 demo");
return;
}


var obj = Data.GameObjects.ByName("obj_roomselector");

if(obj == null) {
Expand Down Expand Up @@ -127,14 +116,19 @@ update = 1
scale = 1
fnt = -4
len = 0
ss = -4
for (i = room_first; i <= room_last; i++)
ss = -4"
+ (!Data.GMS2_3
? "for (i = room_first; i <= room_last; i++)"
: "for (i = 0; room_exists(i); i++)")
+ @"
{
if room_exists(i)
room_names[i] = (((room_get_name(i) + "" ("") + string(i)) + "")"")
else
room_names[i] = -4
}
// for 2.3
myroom_last = i
ww = -1
hh = -1
xx = 0
Expand Down Expand Up @@ -164,8 +158,11 @@ if selector_active
positions[0] = -1
roomid = -1
if (dest_room != """" && dest_room == string_digits(dest_room))
roomid = real(dest_room)
for (i = room_first; i <= room_last; i++)
roomid = real(dest_room)"
+ (!Data.GMS2_3
? "for (i = room_first; i <= room_last; i++)"
: "for (i = 0; i < myroom_last; i++)")
+ @"
{
if is_string(room_names[i])
{
Expand Down Expand Up @@ -410,8 +407,15 @@ selector_active = 0
exiting = 0
", Data);

string version;
if (Data.GMS2_3)
version = "2.3";
else if (gms2)
version = "2";
else
version = "1";

ScriptMessage("Successfully applied for Gamemaker " + (gms2 ? 2 : 1).ToString() + @"
ScriptMessage(@$"Successfully applied for Gamemaker {version}
Controls:
- F3: open/close menu
- Escape: close menu
Expand Down