Skip to content

Commit

Permalink
Merge pull request #1351 from zxopink/master
Browse files Browse the repository at this point in the history
Fixed functions duplications
  • Loading branch information
Grossley authored Jun 3, 2023
2 parents ca423dd + 0c27c83 commit 35eb010
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
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 @@ -101,7 +101,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

0 comments on commit 35eb010

Please sign in to comment.