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

Fixed functions duplications #1351

Merged
merged 6 commits into from
Jun 3, 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
7 changes: 7 additions & 0 deletions UndertaleModLib/Compiler/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ bool hasLocal(string name)
NameStringID = childNameIndex,
Autogenerated = true
};

compileContext.Data.Functions.Add(childFunction);

compileContext.Data.KnownSubFunctions.Add(patch.Name, childFunction);
Expand Down Expand Up @@ -1275,6 +1276,12 @@ private static void AssembleExpression(CodeWriter cw, Parser.Statement e, Parser
endPatch.Add(cw.Emit(Opcode.B));
// we're accessing a subfunction here, so build the cache if needed
Decompiler.Decompiler.BuildSubFunctionCache(cw.compileContext.Data);

//Attempt to find the function before rushing to create a new one
var func = cw.compileContext.Data.Functions.FirstOrDefault(f => f.Name.Content == "gml_Script_" + funcDefName.Text);
if (func != null)
cw.compileContext.Data.KnownSubFunctions.TryAdd(funcDefName.Text, func);

if (cw.compileContext.Data.KnownSubFunctions.ContainsKey(funcDefName.Text))
{
string subFunctionName = cw.compileContext.Data.KnownSubFunctions[funcDefName.Text].Name.Content;
Expand Down
27 changes: 24 additions & 3 deletions UndertaleModLib/Decompiler/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,21 @@ public override string ToString(DecompileContext context)
if (kvp.Key != null)
sb.Append(kvp.Key);
else
sb.Append((context.Statements[0].Last() as AssignmentStatement).Destination.Var.Name.Content);
{
//Attempt to find function names before going with the last functions' name
bool gotFuncName = false;
if (Function.Name.Content.StartsWith("gml_Script_"))
{
string funcName = Function.Name.Content.Substring("gml_Script_".Length);
if (context.Statements[0].Any(x => x is AssignmentStatement && (x as AssignmentStatement).Destination.Var.Name.Content == funcName))
{
sb.Append(funcName);
gotFuncName = true;
}
}
if(!gotFuncName)
sb.Append((context.Statements[0].Last() as AssignmentStatement).Destination.Var.Name.Content);
}
}
sb.Append("(");
for (int i = 0; i < FunctionBodyCodeEntry.ArgumentsCount; ++i)
Expand Down Expand Up @@ -3848,6 +3862,15 @@ public static void BuildSubFunctionCache(UndertaleData data)
data.KnownSubFunctions = new Dictionary<string, UndertaleFunction>();
GlobalDecompileContext globalDecompileContext = new GlobalDecompileContext(data, false);

foreach (var func in data.Functions)
{
if (func.Name.Content.StartsWith("gml_Script_"))
{
var funcName = func.Name.Content.Substring("gml_Script_".Length);
data.KnownSubFunctions.TryAdd(funcName, func);
}
}

Parallel.ForEach(data.GlobalInitScripts, globalScript =>
{
UndertaleCode scriptCode = globalScript.Code;
Expand Down Expand Up @@ -3875,10 +3898,8 @@ public static void BuildSubFunctionCache(UndertaleData data)
{
Debug.WriteLine(e.ToString());
}

processingCodeList.Remove(scriptCode.Name.Content, out _);
});

elapsedSec = 3 * 60;
});

Expand Down
2 changes: 1 addition & 1 deletion UndertaleModLib/UndertaleDataExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static UndertaleString MakeString(this IList<UndertaleString> list, strin
public static UndertaleFunction EnsureDefined(this IList<UndertaleFunction> list, string name, IList<UndertaleString> strg, bool fast = false)
{
UndertaleFunction func = fast ? null : list.ByName(name);
if (func == null)
if (func == null)
{
var str = strg.MakeString(name, out int id);
func = new UndertaleFunction()
Expand Down