Skip to content

Commit

Permalink
Merge pull request #504 from damagex/master
Browse files Browse the repository at this point in the history
Extra element info in output + MinWait() decompile bug fixed
  • Loading branch information
CactusPuppy authored Oct 12, 2024
2 parents caa84d9 + cc7cb59 commit 2cb84e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,13 @@ public static class WorkshopFunctionDecompileHook
}).OnInterupt(() => Cap(decompiler, withBlock)).Get();
}},
{"Wait", (decompiler, function) => {
// Convert the Wait to a MinWait if the wait duration is less than or equal to the minimum.
if ((function.Values[0] is NumberExpression number && number.Value <= Constants.MINIMUM_WAIT) || (function.Values[0] is FunctionExpression durationFunc && durationFunc.Function.Name == "False"))
// Convert the Wait to a MinWait if the wait duration is less than or equal to the minimum and the behavior is set to Ignore Condition.
if (function.Values[1] is ConstantEnumeratorExpression enumerator &&
enumerator.Member == ElementRoot.Instance.GetEnumValueFromWorkshop("WaitBehavior", "Ignore Condition") &&
((function.Values[0] is NumberExpression number && number.Value <= Constants.MINIMUM_WAIT) ||
(function.Values[0] is FunctionExpression durationFunc && durationFunc.Function.Name == "False")))
{
decompiler.Append("MinWait(");

// Add wait behavior if it is not the default.
if (function.Values[1] is ConstantEnumeratorExpression enumerator && enumerator.Member != ElementRoot.Instance.GetEnumValueFromWorkshop("WaitBehavior", "Ignore Condition"))
enumerator.Decompile(decompiler);

// End function
decompiler.Append(")");
decompiler.Append("MinWait()");

// Finished
decompiler.EndAction();
Expand Down
36 changes: 28 additions & 8 deletions Deltinteger/Deltinteger/Elements/Rule.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Linq;

namespace Deltin.Deltinteger.Elements
{
public class Rule
Expand Down Expand Up @@ -43,6 +46,11 @@ public Rule(string name, string subroutine)

public void ToWorkshop(WorkshopBuilder builder)
{

// Element count comment.
if (builder.IncludeComments)
builder.AppendLine("// Rule Element Count: " + ElementCount());

if (Disabled)
{
builder.AppendKeyword("disabled")
Expand All @@ -51,7 +59,6 @@ public void ToWorkshop(WorkshopBuilder builder)
builder.AppendKeyword("rule")
.AppendLine("(\"" + Name + "\")")
.AppendLine("{")
.AppendLine()
.Indent()
.AppendKeywordLine("event")
.AppendLine("{")
Expand All @@ -78,10 +85,13 @@ public void ToWorkshop(WorkshopBuilder builder)
builder.Outdent()
.AppendLine("}");

if (Conditions?.Length > 0)
{
builder.AppendLine()
.AppendKeywordLine("conditions")
if (Conditions?.Length > 0) {
builder.AppendLine();

if (builder.IncludeComments)
builder.AppendLine("// Element Count: " + Conditions.Sum(x => x.ElementCount()) + ", Condition Count: " + Conditions.Length);

builder.AppendKeywordLine("conditions")
.AppendLine("{")
.Indent();

Expand All @@ -96,10 +106,20 @@ public void ToWorkshop(WorkshopBuilder builder)
{
builder.AppendLine();

// Action count comment.
// Action and element count comment.
if (builder.IncludeComments)
builder.AppendLine("// Action count: " + Actions.Length);

{
int largestCount = Actions.Max(x => x.ElementCount());
Element largestAction = Array.FindIndex(Actions, x => x.ElementCount() == largestCount);
int totalElementCount = Actions.Sum(x => x.ElementCount());

builder.AppendLine($"// Element Count: {totalElementCount}, Action Count: {Actions.Length}");
if (Actions.Length > 1) {
builder.AppendLine($"// Largest Action Index: {largestAction} using {largestCount} Elements");
}

}

builder.AppendKeywordLine("actions").AppendLine("{").Indent();
int resetIndentInCaseOfUnbalance = builder.GetCurrentIndent();

Expand Down

0 comments on commit 2cb84e3

Please sign in to comment.