Skip to content

Commit

Permalink
Merge pull request #39 from KSP2Community/dev
Browse files Browse the repository at this point in the history
0.11.0 - Add slotted mixins and an error counter
  • Loading branch information
cheese3660 authored Feb 17, 2024
2 parents f285e61 + 87166ac commit 40515c0
Show file tree
Hide file tree
Showing 24 changed files with 2,701 additions and 2,178 deletions.
2 changes: 1 addition & 1 deletion plugin_template/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Patch Manager",
"description": "A mod for generic patching needs similar to KSP 1's Module Manager.",
"source": "https://github.com/KSP2Community/PatchManager",
"version": "0.10.1",
"version": "0.11.0",
"version_check": "https://raw.githubusercontent.com/KSP2Community/PatchManager/main/plugin_template/swinfo.json",
"ksp2_version": {
"min": "0.2.0",
Expand Down
3 changes: 3 additions & 0 deletions src/PatchManager.Core/Assets/PatchingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal static class PatchingManager
private static Dictionary<string, List<(string name, string text)>> _createdAssets = new();

internal static int TotalPatchCount;
internal static int TotalErrorCount;

public static void GenerateUniverse(HashSet<string> singleFileModIds)
{
var loadedPlugins = PluginList.AllEnabledAndActivePlugins.Select(x => x.Guid).ToList();
Expand Down Expand Up @@ -87,6 +89,7 @@ private static string PatchJson(string label, string assetName, string text)
}
catch (Exception e)
{
TotalErrorCount += 1;
Console.WriteLine($"Patch of {label}:{assetName} errored due to: {e}");
text = backup;
}
Expand Down
1 change: 1 addition & 0 deletions src/PatchManager.Core/CoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public override VisualElement GetDetails()
{
text.text += "Total amount of patches: Unknown (loaded from cache)\n";
}
text.text += $"Total amount of errors: {PatchingManager.TotalErrorCount}\n";

text.text += "Patched labels:";
foreach (var label in PatchingManager.Universe.LoadedLabels)
Expand Down
2 changes: 2 additions & 0 deletions src/PatchManager.Core/Patches/Runtime/LoadingBarPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static bool ShuffleLoadingTip(ref LoadingBar __instance)
if (!InjectPatchManagerTips)
return true;
__instance.tipsText.text = $"Patch Manager: {PatchingManager.TotalPatchCount} patches";
if (PatchingManager.TotalErrorCount > 0)
__instance.tipsText.text += $", {PatchingManager.TotalErrorCount} errors";

return false;
}
Expand Down
15 changes: 14 additions & 1 deletion src/PatchManager.SassyPatching/Execution/Environment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using JetBrains.Annotations;
using PatchManager.SassyPatching.Nodes;
using PatchManager.SassyPatching.Nodes.Statements.SelectionLevel;

namespace PatchManager.SassyPatching.Execution;

Expand All @@ -20,6 +22,16 @@ public class Environment
/// </summary>
public Dictionary<string, DataValue> ScopedValues;


[CanBeNull] private List<Node> _slotActions;

[CanBeNull]
public List<Node> SlotActions
{
get => _slotActions ?? Parent?.SlotActions;
set => _slotActions = value;
}

/// <summary>
/// Creates a new environment
/// </summary>
Expand Down Expand Up @@ -76,7 +88,8 @@ public Environment Snapshot()
var parent = Parent?.Snapshot();
return new Environment(GlobalEnvironment, parent)
{
ScopedValues = scopedValues
ScopedValues = scopedValues,
_slotActions = _slotActions
};
}
}
86 changes: 46 additions & 40 deletions src/PatchManager.SassyPatching/Execution/PatchMixin.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JetBrains.Annotations;
using PatchManager.SassyPatching.Exceptions;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.Nodes;
using PatchManager.SassyPatching.Nodes.Statements.SelectionLevel;
using PatchManager.SassyPatching.Nodes.Statements.TopLevel;

Expand All @@ -17,48 +18,10 @@ public PatchMixin(Mixin mixin)

public void Include(Environment env, List<PatchArgument> arguments, ISelectable selectable, [CanBeNull] IModifiable modifiable)
{
Environment subEnvironment = new Environment(env.GlobalEnvironment, env);
var subEnvironment = new Environment(env.GlobalEnvironment, env);
foreach (var arg in Function.Arguments)
{
// As per usual we consume
bool foundPositional = false;
DataValue argument = null;
int removalIndex = -1;
for (int i = 0; i < arguments.Count; i++)
{
if (!foundPositional && arguments[i].ArgumentName == null)
{
foundPositional = true;
removalIndex = i;
argument = arguments[i].ArgumentDataValue;
}

if (arguments[i].ArgumentName != arg.Name) continue;
removalIndex = i;
argument = arguments[i].ArgumentDataValue;
break;
}

if (removalIndex >= 0)
{
arguments.RemoveAt(removalIndex);
}
if (argument == null)
{
if (arg.Value != null)
{
subEnvironment[arg.Name] = arg.Value.Compute(subEnvironment);
}
else
{
throw new InvocationException($"No value found for argument: {arg.Name}");
}
}
else
{
// args.Add(CheckParameter(parameter, argument));
subEnvironment[arg.Name] = argument;
}
ConsumeMixinArgument(arguments, arg, subEnvironment);
}

if (arguments.Count > 0)
Expand All @@ -78,4 +41,47 @@ public void Include(Environment env, List<PatchArgument> arguments, ISelectable
}

}

private static void ConsumeMixinArgument(List<PatchArgument> arguments, Argument arg, Environment subEnvironment)
{
// As per usual we consume
var foundPositional = false;
DataValue argument = null;
var removalIndex = -1;
for (var i = 0; i < arguments.Count; i++)
{
if (!foundPositional && arguments[i].ArgumentName == null)
{
foundPositional = true;
removalIndex = i;
argument = arguments[i].ArgumentDataValue;
}

if (arguments[i].ArgumentName != arg.Name) continue;
removalIndex = i;
argument = arguments[i].ArgumentDataValue;
break;
}

if (removalIndex >= 0)
{
arguments.RemoveAt(removalIndex);
}
if (argument == null)
{
if (arg.Value != null)
{
subEnvironment[arg.Name] = arg.Value.Compute(subEnvironment);
}
else
{
throw new InvocationException($"No value found for argument: {arg.Name}");
}
}
else
{
// args.Add(CheckParameter(parameter, argument));
subEnvironment[arg.Name] = argument;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using PatchManager.SassyPatching.Exceptions;
using PatchManager.SassyPatching.Interfaces;
using Environment = PatchManager.SassyPatching.Execution.Environment;

namespace PatchManager.SassyPatching.Nodes.Statements.SelectionLevel;

/// <summary>
/// Represents a mixin block include (i.e. slotting actions into a mixin)
/// </summary>
public class MixinBlockInclude : Node, ISelectionAction
{
/// <summary>
/// The name of the mixin being included
/// </summary>
public readonly string MixinName;
/// <summary>
/// The list of arguments to the mixin being included
/// </summary>
public readonly List<CallArgument> Arguments;

/// <summary>
/// The actions to be slotted into the mixin
/// </summary>
public readonly List<Node> SlotActions;

internal MixinBlockInclude(Coordinate c, string mixinName, List<CallArgument> arguments, List<Node> slotActions) : base(c)
{
MixinName = mixinName;
Arguments = arguments;
SlotActions = slotActions;
}

/// <inheritdoc />
public override void ExecuteIn(Environment environment)
{
}

/// <inheritdoc />
public void ExecuteOn(Environment environment, ISelectable selectable, IModifiable modifiable)
{
if (environment.GlobalEnvironment.AllMixins.TryGetValue(MixinName, out var mixin))
{
try
{
var subEnv = new Environment(environment.GlobalEnvironment, environment)
{
SlotActions = SlotActions
};
mixin.Include(subEnv,Arguments.Select(x => x.Compute(environment)).ToList(),selectable,modifiable);
}
catch (InvocationException e)
{
throw new InterpreterException(Coordinate, e.ToString());
}
}
else
{
throw new InterpreterException(Coordinate, $"{MixinName} is not a valid mixin");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using PatchManager.SassyPatching.Exceptions;
using PatchManager.SassyPatching.Interfaces;
using Environment = PatchManager.SassyPatching.Execution.Environment;

namespace PatchManager.SassyPatching.Nodes.Statements.SelectionLevel;

public class MixinSlot(Coordinate c) : Node(c), ISelectionAction
{
public override void ExecuteIn(Environment environment)
{
}

public void ExecuteOn(Environment environment, ISelectable selectable, IModifiable modifiable)
{
var actions = environment.SlotActions;
if (actions == null)
{
throw new InterpreterException(Coordinate, "Attempting to insert into a mixin slot without there being any actions passed for this purpose");
}

foreach (var action in actions)
{
if (action is ISelectionAction selectionAction)
{
selectionAction.ExecuteOn(environment, selectable, modifiable);
}
else
{
action.ExecuteIn(environment);
}
}
}
}
Loading

0 comments on commit 40515c0

Please sign in to comment.