Skip to content

Commit

Permalink
Added mod requirement checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Wheeler committed Aug 15, 2020
1 parent 50c7b70 commit d8f7d19
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
79 changes: 79 additions & 0 deletions Assets/Scripts/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Assets.Scripts.Craft.Parts.Modifiers;
using Assets.Scripts.Vizzy;
using ModApi.Craft;
using ModApi.Craft.Program;
using ModApi.Craft.Program.Instructions;
using ModApi.Mods;
using ModApi.Ui;
using ModApi.Ui.Events;
Expand Down Expand Up @@ -109,6 +112,10 @@ private Mod() {
/// <value>The singleton instance of the mod object.</value>
public static Mod Instance { get; } = GetModInstance<Mod>();

public override Boolean IsModRequiredForCraft(CraftData craft) {
return base.IsModRequiredForCraft(craft) || DoesCraftUseVizzyGL(craft);
}

protected override void OnModInitialized() {
var ui = ModApi.Common.Game.Instance.UserInterface;

Expand All @@ -117,6 +124,78 @@ protected override void OnModInitialized() {
base.OnModInitialized();
}

private static Boolean DoesCraftUseVizzyGL(CraftData craft) {
Debug.Log($"Checking craft {craft.Name} for VizzyGL usage.");
if (craft.Assembly != null) {
foreach (var part in craft.Assembly.Parts) {
var flightProgramData = part.GetModifier<FlightProgramData>();

if (flightProgramData?.FlightProgramXml != null) {
try {
var flightProgram = flightProgramData.Script.FlightProgram ??
new ProgramSerializer().DeserializeFlightProgram(flightProgramData.FlightProgramXml);
if (ContainsVizzyGL(flightProgram.RootInstructions) ||
ContainsVizzyGL(flightProgram.RootExpressions) ||
ContainsVizzyGL(flightProgram.CustomExpressions) ||
ContainsVizzyGL(flightProgram.CustomInstructions)) {
Debug.Log($"Flight Program on part {part.Name} contains VizzyGL.");
return true;
} else {
Debug.Log($"Flight Program on part {part.Name} does not contain VizzyGL.");
}
} catch (Exception ex) {
Debug.LogWarning($"Unable to deserialize Flight Program: {flightProgramData.FlightProgramXml}");
Debug.LogError(ex);
}
}
}
} else {
Debug.LogWarning("Unable to check craft for VizzyGL because Assembly is null.");
}

return false;
}

private static Boolean ContainsVizzyGL(IEnumerable<ProgramInstruction> instructions) {
var instructionStack = new Stack<ProgramInstruction>(instructions);
while (instructionStack.Count > 0) {
var instruction = instructionStack.Pop();
if (instruction is IVizzyGLProgramNode) {
return true;
}

if (instruction.Expressions != null && ContainsVizzyGL(instruction.Expressions)) {
return true;
}

if (instruction.Next != null) {
instructionStack.Push(instruction.Next);
}

if (instruction.SupportsChildren) {
instructionStack.Push(instruction.FirstChild);
}
}

return false;
}

private static Boolean ContainsVizzyGL(IEnumerable<ProgramExpression> expressions) {
var expressionStack = new Stack<ProgramExpression>(expressions);
while (expressionStack.Count > 0) {
var expression = expressionStack.Pop();
if (expression is IVizzyGLProgramNode) {
return true;
} else if (expression.Expressions != null) {
foreach (var child in expression.Expressions.Reverse()) {
expressionStack.Push(child);
}
}
}

return false;
}

private void UiOnUserInterfaceLoading(object sender, UserInterfaceLoadingEventArgs e) {
var vizzyUI = e.XmlLayout.GameObject.GetComponent<VizzyUIController>();
if (e.UserInterfaceId == UserInterfaceIds.Vizzy) {
Expand Down
4 changes: 4 additions & 0 deletions Assets/Scripts/Vizzy/IVizzyGLProgramNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Assets.Scripts.Vizzy {
public interface IVizzyGLProgramNode {
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Vizzy/IVizzyGLProgramNode.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Scripts/Vizzy/VizzyGLInstructionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnityEngine;

namespace Assets.Scripts.Vizzy {
public abstract class VizzyGLInstructionBase : ProgramInstruction {
public abstract class VizzyGLInstructionBase : ProgramInstruction, IVizzyGLProgramNode {
protected IVizzyGLContext DrawingContext { get; private set; }

public override ProgramInstruction Execute(IThreadContext context) {
Expand Down

0 comments on commit d8f7d19

Please sign in to comment.