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

better profile mode with no dogcheck #1553

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions UndertaleModCli/Program.UMTLibInherited.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ public void ReassignGUIDs(string guid, uint objectIndex)

public uint ReduceCollisionValue(List<uint> possibleValues)
{
this.ScriptMessage("haha dumbass");
if (possibleValues.Count == 1)
{
if (possibleValues[0] != uint.MaxValue)
Expand Down
22 changes: 3 additions & 19 deletions UndertaleModCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,8 @@ private static int Dump(DumpOptions options)
return EXIT_FAILURE;
}

if (program.Data.IsYYC())
{
Console.WriteLine("The game was made with YYC (YoYo Compiler), which means that the code was compiled into the executable. " +
"There is thus no code to dump. Exiting.");
return EXIT_SUCCESS;
}

// If user provided code to dump, dump code
if ((options.Code?.Length > 0) && (program.Data.Code?.Count > 0))
if ((options.Code?.Length > 0) && (program.Data.Code.Count > 0))
{
// If user wanted to dump everything, do that, otherwise only dump what user provided
string[] codeArray;
Expand Down Expand Up @@ -579,17 +572,8 @@ private void CliQuickInfo()
Console.WriteLine($"{Data.Paths.Count} Paths, {Data.Scripts.Count} Scripts, {Data.Shaders.Count} Shaders");
Console.WriteLine($"{Data.Fonts.Count} Fonts, {Data.Timelines.Count} Timelines, {Data.GameObjects.Count} Game Objects");
Console.WriteLine($"{Data.Rooms.Count} Rooms, {Data.Extensions.Count} Extensions, {Data.TexturePageItems.Count} Texture Page Items");
if (!Data.IsYYC())
{
Console.WriteLine($"{Data.Code.Count} Code Entries, {Data.Variables.Count} Variables, {Data.Functions.Count} Functions");
Console.WriteLine($"{Data.CodeLocals.Count} Code locals, {Data.Strings.Count} Strings, {Data.EmbeddedTextures.Count} Embedded Textures");
}
else
{
Console.WriteLine("Unknown amount of Code entries and Code locals");
}
Console.WriteLine($"{Data.Strings.Count} Strings");
Console.WriteLine($"{Data.EmbeddedTextures.Count} Embedded Textures");
Console.WriteLine($"{Data.Code.Count} Code Entries, {Data.Variables.Count} Variables, {Data.Functions.Count} Functions");
Console.WriteLine($"{Data.CodeLocals.Count} Code locals, {Data.Strings.Count} Strings, {Data.EmbeddedTextures.Count} Embedded Textures");
Console.WriteLine($"{Data.EmbeddedAudio.Count} Embedded Audio");

if (IsInteractive) Pause();
Expand Down
95 changes: 57 additions & 38 deletions UndertaleModLib/Compiler/AssemblyWriter.cs

Large diffs are not rendered by default.

55 changes: 18 additions & 37 deletions UndertaleModLib/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;
using UndertaleModLib.Models;
using static UndertaleModLib.Compiler.Compiler.AssemblyWriter;
using AssetRefType = UndertaleModLib.Decompiler.Decompiler.ExpressionAssetRef.RefType;

namespace UndertaleModLib.Compiler
{
Expand All @@ -18,10 +17,10 @@ public class CompileContext
public bool ensureFunctionsDefined = true;
public bool ensureVariablesDefined = true;
public static bool GMS2_3;
public bool TypedAssetRefs => Data.IsVersionAtLeast(2023, 8);
public int LastCompiledArgumentCount = 0;
public Dictionary<string, string> LocalVars = new Dictionary<string, string>();
public Dictionary<string, string> GlobalVars = new Dictionary<string, string>();
//public Dictionary<Compiler.Parser.Statement, Dictionary<string, int>> LocalArgs = new();
public Dictionary<string, Dictionary<string, int>> Enums = new Dictionary<string, Dictionary<string, int>>();
public UndertaleCode OriginalCode;
public IList<UndertaleVariable> OriginalReferencedLocalVars;
Expand Down Expand Up @@ -82,21 +81,18 @@ public void Setup(bool redoAssets = false)
private void MakeAssetDictionary()
{
assetIds.Clear();
AddAssetsFromList(Data?.GameObjects, AssetRefType.Object);
AddAssetsFromList(Data?.Sprites, AssetRefType.Sprite);
AddAssetsFromList(Data?.Sounds, AssetRefType.Sound);
AddAssetsFromList(Data?.Backgrounds, AssetRefType.Background);
AddAssetsFromList(Data?.Paths, AssetRefType.Path);
AddAssetsFromList(Data?.Fonts, AssetRefType.Font);
AddAssetsFromList(Data?.Timelines, AssetRefType.Timeline);
AddAssetsFromList(Data?.GameObjects);
AddAssetsFromList(Data?.Sprites);
AddAssetsFromList(Data?.Sounds);
AddAssetsFromList(Data?.Backgrounds);
AddAssetsFromList(Data?.Paths);
AddAssetsFromList(Data?.Fonts);
AddAssetsFromList(Data?.Timelines);
if (!GMS2_3)
AddAssetsFromList(Data?.Scripts, AssetRefType.Object /* not actually used */);
AddAssetsFromList(Data?.Shaders, AssetRefType.Shader);
AddAssetsFromList(Data?.Rooms, AssetRefType.Room);
AddAssetsFromList(Data?.AudioGroups, AssetRefType.Sound /* apparently? */);
AddAssetsFromList(Data?.AnimationCurves, AssetRefType.AnimCurve);
AddAssetsFromList(Data?.Sequences, AssetRefType.Sequence);
AddAssetsFromList(Data?.ParticleSystems, AssetRefType.ParticleSystem);
AddAssetsFromList(Data?.Scripts);
AddAssetsFromList(Data?.Shaders);
AddAssetsFromList(Data?.Rooms);
AddAssetsFromList(Data?.AudioGroups);

scripts.Clear();
if (Data?.Scripts != null)
Expand All @@ -121,30 +117,15 @@ private void MakeAssetDictionary()
}
}

private void AddAssetsFromList<T>(IList<T> list, AssetRefType type) where T : UndertaleNamedResource
private void AddAssetsFromList<T>(IList<T> list) where T : UndertaleNamedResource
{
if (list == null)
return;
if (TypedAssetRefs)
for (int i = 0; i < list.Count; i++)
{
for (int i = 0; i < list.Count; i++)
{
string name = list[i].Name?.Content;
if (name != null)
{
// Typed asset refs pack their type into the ID
assetIds[name] = (i & 0xffffff) | (((int)type & 0x7f) << 24);
}
}
}
else
{
for (int i = 0; i < list.Count; i++)
{
string name = list[i].Name?.Content;
if (name != null)
assetIds[name] = i;
}
string name = list[i].Name?.Content;
if (name != null)
assetIds[name] = i;
}
}
}
Expand Down Expand Up @@ -216,4 +197,4 @@ public static CompileContext CompileGMLText(string input, CompileContext context
return context;
}
}
}
}
55 changes: 33 additions & 22 deletions UndertaleModLib/Compiler/Parser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -31,8 +32,7 @@ public enum Kind
Number,
String,
Constant,
Int64,
Reference
Int64
}

public ExpressionConstant(double val)
Expand Down Expand Up @@ -403,7 +403,8 @@ private static void ReportCodeError(string msg, Lexer.Token context, bool synchr
if (context.Location != null)
{
msg += string.Format(" around line {0}, column {1}", context.Location.Line, context.Location.Column);
} else if (context.Kind == TokenKind.EOF)
}
else if (context.Kind == TokenKind.EOF)
{
msg += " around EOF (end of file)";
}
Expand Down Expand Up @@ -439,6 +440,7 @@ public static Statement ParseTokens(CompileContext context, List<Lexer.Token> to
context.LocalVars["arguments"] = "arguments";
context.GlobalVars.Clear();
context.Enums.Clear();
//context.LocalArgs.Clear();
hasError = false;

// Ensuring an EOF exists
Expand All @@ -460,7 +462,7 @@ public static Statement ParseTokens(CompileContext context, List<Lexer.Token> to
else if (tokens[i].Kind == TokenKind.Identifier)
{
// Convert identifiers into their proper references, at least sort of.
if ((i != 0 && tokens[i - 1].Kind == TokenKind.Dot) ||
if ((i != 0 && tokens[i - 1].Kind == TokenKind.Dot) ||
!ResolveIdentifier(context, tokens[i].Content, out ExpressionConstant constant))
{
int ID = GetVariableID(context, tokens[i].Content, out _);
Expand All @@ -485,7 +487,8 @@ public static Statement ParseTokens(CompileContext context, List<Lexer.Token> to
try
{
val = Convert.ToInt64(t.Content.Substring(t.Content[0] == '$' ? 1 : 2), 16);
} catch (Exception)
}
catch (Exception)
{
ReportCodeError("Invalid hex literal.", t, false);
constant = new ExpressionConstant(0);
Expand Down Expand Up @@ -672,12 +675,16 @@ private static Statement ParseFunction(CompileContext context)
}

EnsureTokenKind(TokenKind.OpenParen);

var i = 0;
//Dictionary<string, int> argsDict = new();
while (remainingStageOne.Count > 0 && !hasError && !IsNextToken(TokenKind.EOF, TokenKind.CloseParen))
{
Statement expr = ParseExpression(context);
if (expr != null)
args.Children.Add(expr);
var token = EnsureTokenKind(TokenKind.ProcVariable);
if (token == null)
return null;
//argsDict.Add(token.Text, i);
args.Children.Add(new Statement(Statement.StatementKind.Token, token.Text));
i++;
if (!IsNextTokenDiscard(TokenKind.Comma))
{
if (!IsNextToken(TokenKind.CloseParen))
Expand All @@ -691,7 +698,9 @@ private static Statement ParseFunction(CompileContext context)

if (EnsureTokenKind(TokenKind.CloseParen) == null) return null;

result.Children.Add(ParseStatement(context));
result.Children.Add(ParseStatement(context)); // most likely parses the body.
//context.LocalArgs.Add(result, argsDict);

if (expressionMode)
return result;
else // Whatever you call non-anonymous definitions
Expand Down Expand Up @@ -1044,7 +1053,7 @@ private static Statement ParseFunctionCall(CompileContext context, bool expressi
if (EnsureTokenKind(TokenKind.CloseParen) == null) return null;

// Check for proper argument count, at least for builtins
if (context.BuiltInList.Functions.TryGetValue(s.Text, out FunctionInfo fi) &&
if (context.BuiltInList.Functions.TryGetValue(s.Text, out FunctionInfo fi) &&
fi.ArgumentCount != -1 && result.Children.Count != fi.ArgumentCount)
ReportCodeError(string.Format("Function {0} expects {1} arguments, got {2}.",
s.Text, fi.ArgumentCount, result.Children.Count)
Expand Down Expand Up @@ -1107,7 +1116,7 @@ private static Statement ParseOrOp(CompileContext context)
{
result.Children.Add(ParseAndOp(context));
}

return result;
}
else
Expand Down Expand Up @@ -1339,7 +1348,7 @@ private static Statement ParsePostAndRef(CompileContext context)
// Parse chain variable reference
Statement result = new Statement(Statement.StatementKind.ExprVariableRef, remainingStageOne.Peek().Token);
bool combine = false;
if (left.Kind != Statement.StatementKind.ExprConstant || left.Constant.kind == ExpressionConstant.Kind.Reference /* TODO: will this ever change? */)
if (left.Kind != Statement.StatementKind.ExprConstant)
result.Children.Add(left);
else
combine = true;
Expand Down Expand Up @@ -1385,7 +1394,7 @@ private static Statement ParseSingleVar(CompileContext context)
if (context.BuiltInList.Functions.ContainsKey(s.Text) || context.scripts.Contains(s.Text))
{
//ReportCodeError(string.Format("Variable name {0} cannot be used; a function or script already has the name.", s.Text), false);
return new Statement(Statement.StatementKind.ExprFuncName, s.Token) { ID = s.ID };
return new Statement(Statement.StatementKind.ExprFuncName, s.Token) { ID = s.ID };
}

Statement result = new Statement(Statement.StatementKind.ExprSingleVariable, s.Token);
Expand Down Expand Up @@ -1575,7 +1584,8 @@ public static Statement Optimize(CompileContext context, Statement s)
result.Children[i] = Optimize(context, result.Children[i]);
}
}
} else
}
else
result = new Statement(s);
Statement child0 = result.Children[0];

Expand Down Expand Up @@ -1619,7 +1629,8 @@ public static Statement Optimize(CompileContext context, Statement s)
accessorFunc.Children.Insert(0, left);
accessorFunc.Children.Add(Optimize(context, result.Children[2]));
return accessorFunc;
} else
}
else
{
// Not the final set function
Statement accessorFunc = new Statement(Statement.StatementKind.ExprFunctionCall, ai.RFunc);
Expand All @@ -1646,7 +1657,8 @@ public static Statement Optimize(CompileContext context, Statement s)
{
result.Children[i] = Optimize(context, result.Children[i]);
}
} else
}
else
{
for (int i = 0; i < result.Children.Count; i++)
{
Expand Down Expand Up @@ -1935,7 +1947,7 @@ public static Statement Optimize(CompileContext context, Statement s)
ReportCodeError("Case argument must be constant.", result.Token, false);
}
break;
// todo: parse enum references
// todo: parse enum references
}
return result;
}
Expand All @@ -1950,7 +1962,8 @@ private static AccessorInfo GetAccessorInfoFromStatement(CompileContext context,
ai = context.BuiltInList.Accessors1D[kind];
else
ReportCodeError("Accessor has incorrect number of arguments", s.Children[0].Token, false);
} else
}
else
{
if (context.BuiltInList.Accessors2D.ContainsKey(kind))
ai = context.BuiltInList.Accessors2D[kind];
Expand Down Expand Up @@ -2708,8 +2721,6 @@ private static bool ResolveIdentifier(CompileContext context, string identifier,
}
return false;
}
if (context.TypedAssetRefs)
constant.kind = ExpressionConstant.Kind.Reference;
constant.valueNumber = (double)index;
return true;
}
Expand Down Expand Up @@ -2745,4 +2756,4 @@ private static int GetVariableID(CompileContext context, string name, out bool i
}
}
}
}
}
13 changes: 2 additions & 11 deletions UndertaleModLib/Decompiler/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public static class Assembler
{ -7, "setstatic" },
{ -8, "savearef" },
{ -9, "restorearef" },
{ -10, "chknullish" },
{ -11, "pushref" }
{ -10, "chknullish" }
};
public static Dictionary<string, short> NameToBreakID = new Dictionary<string, short>()
{
Expand All @@ -37,8 +36,7 @@ public static class Assembler
{ "setstatic", -7 },
{ "savearef", -8 },
{ "restorearef", -9 },
{ "chknullish", -10 },
{ "pushref", -11 }
{ "chknullish", -10 }
};

// TODO: Improve the error messages
Expand Down Expand Up @@ -219,14 +217,7 @@ public static UndertaleInstruction AssembleOne(string source, IList<UndertaleFun

case UndertaleInstruction.InstructionType.BreakInstruction:
if (breakId != 0)
{
instr.Value = breakId;
if (breakId == -11) // pushref
{
// parse additional int argument
instr.IntArgument = Int32.Parse(line);
}
}
else
instr.Value = Int16.Parse(line);
line = "";
Expand Down
3 changes: 0 additions & 3 deletions UndertaleModLib/Decompiler/AssetTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public enum AssetIDType
GameObject, // or GameObjectInstance or InstanceType, these are all interchangable
Script,
Shader,
AnimCurve,
Sequence,
ParticleSystem,

EventType, // For event_perform

Expand Down
Loading