Skip to content

Commit

Permalink
Extra element info in output + MinWait() decompile bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
damagex committed Jul 28, 2024
1 parent caa84d9 commit 8062ccb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 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
40 changes: 30 additions & 10 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,12 +59,11 @@ public void ToWorkshop(WorkshopBuilder builder)
builder.AppendKeyword("rule")
.AppendLine("(\"" + Name + "\")")
.AppendLine("{")
.AppendLine()
.Indent()
.AppendKeywordLine("event")
.AppendLine("{")
.Indent();

ElementRoot.Instance.GetEnumValue("Event", RuleEvent.ToString()).ToWorkshop(builder, ToWorkshopContext.Other);
builder.Append(";").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,16 +106,26 @@ 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();

foreach (var action in Actions)
action.ToWorkshop(builder, ToWorkshopContext.Action);

builder.SetCurrentIndent(resetIndentInCaseOfUnbalance);
builder.Outdent().AppendLine("}");
}
Expand Down

0 comments on commit 8062ccb

Please sign in to comment.