Skip to content

Commit

Permalink
Merge pull request #17 from KSP2Community/dev
Browse files Browse the repository at this point in the history
0.4.0
  • Loading branch information
cheese3660 authored Nov 11, 2023
2 parents 67dae4a + da66559 commit 16a09b1
Show file tree
Hide file tree
Showing 37 changed files with 2,103 additions and 830 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.3.2",
"version": "0.4.0",
"version_check": "https://raw.githubusercontent.com/KSP2Community/PatchManager/main/plugin_template/swinfo.json",
"ksp2_version": {
"min": "0.1.5",
Expand Down
24 changes: 21 additions & 3 deletions src/PatchManager.Parts/Selectables/ModuleSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ public sealed class ModuleSelectable : BaseSelectable
{
public readonly JToken SerializedData;
public readonly PartSelectable Selectable;
private Dictionary<string, int> _dataIndices;

/// <inheritdoc />
public ModuleSelectable(JToken token, PartSelectable selectable)
{
SerializedData = token;
Selectable = selectable;
_dataIndices = new Dictionary<string, int>();
ElementType = ((string)token["Name"]).Replace("PartComponent", "");
Name = ElementType;
Classes = new();
Children = new();
// Now we go down the list in the data type
var data = (JArray)token["ModuleData"];
var index = 0;
foreach (var moduleData in data)
{
Classes.Add((string)moduleData["Name"]);
_dataIndices[moduleData["Name"].Value<string>()] = index++;
Classes.Add(moduleData["Name"].Value<string>());
// Where we are going to have to add children ree
// TODO: Add a specialization for ModuleEngine
Children.Add(GetSelectable((JObject)moduleData));
Expand All @@ -39,14 +43,14 @@ public ModuleSelectable(JToken token, PartSelectable selectable)

private ISelectable GetSelectable(JObject moduleData)
{
var type = Type.GetType((string)moduleData["DataType"]);
var type = Type.GetType(moduleData["DataType"].Value<string>());
if (type != null && PartsUtilities.ModuleDataAdapters.TryGetValue(type, out var adapterType))
{
return (ISelectable)Activator.CreateInstance(type, moduleData, this);
}
else
{
return new JTokenSelectable(Selectable.SetModified, moduleData["DataObject"], (string)moduleData["Name"]);
return new JTokenSelectable(Selectable.SetModified, moduleData["DataObject"], moduleData["Name"].Value<string>());
}
}

Expand All @@ -59,8 +63,22 @@ private ISelectable GetSelectable(JObject moduleData)
/// <inheritdoc />
public override List<string> Classes { get; }

/// <inheritdoc />
public override bool MatchesClass(string @class, out DataValue classValue) {

if (_dataIndices.TryGetValue(@class.Replace("PartComponent", ""), out var index))
{
classValue = DataValue.FromJToken(SerializedData["ModuleData"][index]["DataObject"]);
return true;
}
classValue = null;
return false;
}

/// <inheritdoc />
public override string ElementType { get; }



/// <inheritdoc />
public override bool IsSameAs(ISelectable other) =>
Expand Down
37 changes: 35 additions & 2 deletions src/PatchManager.Parts/Selectables/PartSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.Selectables;
using UnityEngine;

namespace PatchManager.Parts.Selectables;

Expand Down Expand Up @@ -38,11 +39,13 @@ public void SetDeleted()
internal readonly JObject JObject;
private static readonly Regex Sanitizer = new("[^a-zA-Z0-9 -_]");
private static string Sanitize(string str) => Sanitizer.Replace(str, "");

private readonly Dictionary<string, int> _moduleIndices;
private static JArray SerializedPartModules;
internal PartSelectable(string data)
{
_originalData = data;
JObject = JObject.Parse(data);
_moduleIndices = new Dictionary<string, int>();
Classes = new();
Children = new();
var partData = JObject["data"];
Expand All @@ -53,10 +56,14 @@ internal PartSelectable(string data)
}

var serializedPartModules = partData["serializedPartModules"];
SerializedPartModules = serializedPartModules as JArray;
var index = 0;
foreach (var module in serializedPartModules)
{
// var moduleData = module["ModuleData"];
Classes.Add(((string)module["Name"]).Replace("PartComponent", ""));
_moduleIndices[module["Name"].Value<string>().Replace("PartComponent", "")] = index++;
Classes.Add(module["Name"].Value<string>().Replace("PartComponent", ""));
Classes.Add(module["Name"].Value<string>());
// Classes.Add((string)moduleData["name"]);
Children.Add(new ModuleSelectable(module,this));
}
Expand All @@ -81,6 +88,32 @@ internal PartSelectable(string data)
/// <inheritdoc />
public override List<string> Classes { get; }

private DataValue ConcatenateModuleData(int index)
{
DataValue value = new DataValue(DataValue.DataType.Dictionary, new Dictionary<string, DataValue>());
foreach (var data in SerializedPartModules[index]["ModuleData"])
{
var dataObject = DataValue.FromJToken(data["DataObject"]);
foreach (var kv in dataObject.Dictionary.Where(kv => kv.Key != "$type"))
{
value.Dictionary[kv.Key] = kv.Value;
}
}
return value;
}

/// <inheritdoc />
public override bool MatchesClass(string @class, out DataValue classValue)
{
if (_moduleIndices.TryGetValue(@class.Replace("PartComponent", ""), out var index))
{
classValue = ConcatenateModuleData(index);
return true;
}
classValue = null;
return false;
}

/// <inheritdoc />
public override string ElementType => "parts_data";

Expand Down
17 changes: 17 additions & 0 deletions src/PatchManager.Parts/Selectables/ResourceContainersSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ public sealed class ResourceContainersSelectable : BaseSelectable
{
private readonly JArray _containers;
private PartSelectable _selectable;
private Dictionary<string, int> _resourceIndices;

internal ResourceContainersSelectable(JArray containers, PartSelectable selectable)
{
_containers = containers;
_resourceIndices = new Dictionary<string, int>();
Children = new();
ElementType = "resourceContainers";
Classes = new();
Name = "resourceContainers";
_selectable = selectable;
var index = 0;
foreach (var container in containers)
{
_resourceIndices[(container as JObject)?["name"].Value<string>() ?? string.Empty] = index++;
Classes.Add((container as JObject)?["name"].Value<string>());
Children.Add(new ResourceContainerSelectable(container as JObject, selectable));
}
}
Expand All @@ -35,6 +40,18 @@ internal ResourceContainersSelectable(JArray containers, PartSelectable selectab
/// <inheritdoc />
public override List<string> Classes { get; }

public override bool MatchesClass(string @class, out DataValue classValue)
{
classValue = null;
if (_resourceIndices.TryGetValue(@class, out var index))
{
classValue = DataValue.FromJToken(_containers[index]);
return true;
}

return false;
}

/// <inheritdoc />
public override string ElementType { get; }

Expand Down
25 changes: 24 additions & 1 deletion src/PatchManager.Resources/Selectables/RecipeSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public sealed class RecipeSelectable : BaseSelectable
private readonly string _originalData;
internal readonly JObject JObject;
internal readonly JArray Ingredients;

internal readonly Dictionary<string, int> IngredientIndices;

/// <summary>
/// Marks this resource selectable as having been modified any level down
/// </summary>
Expand All @@ -34,18 +35,22 @@ public void SetDeleted()
SetModified();
_deleted = true;
}

internal RecipeSelectable(string data)
{
_originalData = data;
JObject = JObject.Parse(data);
IngredientIndices = new Dictionary<string, int>();
Classes = new() { "recipe" };
Children = new();
var resourceData = JObject["recipeData"];
Name = (string)resourceData["name"];
Ingredients = (JArray)resourceData["ingredients"];
ElementType = "recipe";
var index = 0;
foreach (var ingredient in Ingredients)
{
IngredientIndices[ingredient["name"].Value<string>()] = index++;
Classes.Add(ingredient["name"].Value<string>());
Children.Add(
new JTokenSelectable(SetModified, ingredient, tok => tok["name"].Value<string>(), "ingredient"));
Expand All @@ -61,6 +66,24 @@ internal RecipeSelectable(string data)
/// <inheritdoc />
public override List<string> Classes { get; }

public override bool MatchesClass(string @class, out DataValue classValue)
{
classValue = null;
if (@class == "recipe")
{
classValue = DataValue.FromJToken(JObject["recipeData"]);
return true;
}
if (IngredientIndices.TryGetValue(@class, out var index))
{
classValue = DataValue.FromJToken(Ingredients[index]);
return true;
}

return false;

}

/// <inheritdoc />
public override string ElementType { get; }

Expand Down
11 changes: 11 additions & 0 deletions src/PatchManager.Resources/Selectables/ResourceSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ internal ResourceSelectable(string data)
/// <inheritdoc />
public override List<string> Classes { get; }

public override bool MatchesClass(string @class, out DataValue classValue)
{
classValue = null;
if (@class == "resource")
{
classValue = DataValue.FromJToken(JObject["data"]);
return true;
}
return false;
}

/// <inheritdoc />
public override string ElementType { get; }

Expand Down
20 changes: 2 additions & 18 deletions src/PatchManager.SassyPatching/BaseSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,6 @@ namespace PatchManager.SassyPatching;
[PublicAPI]
public abstract class BaseSelectable : ISelectable
{
/// <inheritdoc />
public List<ISelectable> SelectByName(string name) =>
Children.Where(child => child.MatchesName(name)).ToList();

/// <inheritdoc />
public List<ISelectable> SelectByClass(string @class)=>
Children.Where(child => child.MatchesClass(@class)).ToList();

/// <inheritdoc />
public List<ISelectable> SelectWithoutClass(string @class) =>
Children.Where(child => !child.MatchesClass(@class)).ToList();

/// <inheritdoc />
public List<ISelectable> SelectWithoutName(string name) =>
Children.Where(child => !child.MatchesName(name)).ToList();

/// <inheritdoc />
public List<ISelectable> SelectByElement(string element) => Children.Where(child => child.MatchesElement(element)).ToList();



Expand Down Expand Up @@ -54,6 +36,8 @@ public List<ISelectable> SelectWithoutName(string name) =>
/// <inheritdoc />
public bool MatchesClass(string @class) => Classes.Contains(@class);

public abstract bool MatchesClass(string @class, out DataValue classValue);

/// <summary>
/// The type of this selectable element (usually corresponds to the field name it is defined as, or the module type)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PatchManager.SassyPatching.Interfaces;

namespace PatchManager.SassyPatching.Execution;

/// <summary>
/// Used to store information in a selectable, for stuff like class capture selectors and the like
/// Makes stuff a lot more complex, but meh
/// </summary>
public class SelectableWithEnvironment
{
/// <summary>
/// The selectable being selected against
/// </summary>
public ISelectable Selectable;
/// <summary>
/// The environment being used
/// </summary>
public Environment Environment;
}
40 changes: 7 additions & 33 deletions src/PatchManager.SassyPatching/Interfaces/ISelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,11 @@
/// </summary>
public interface ISelectable
{
/// <summary>
/// Select from children by name pattern
/// </summary>
/// <param name="name">The name pattern being matched against</param>
/// <returns>All children that match the name pattern</returns>
public List<ISelectable> SelectByName(string name);
/// <summary>
/// Select from children by class
/// </summary>
/// <param name="class">The class being selected by</param>
/// <returns>All children that have the class</returns>
public List<ISelectable> SelectByClass(string @class);
/// <summary>
/// Select from children by class
/// </summary>
/// <param name="class">The class being selected by</param>
/// <returns>All children that don't have the class</returns>
public List<ISelectable> SelectWithoutClass(string @class);

/// <summary>
/// Select from children by name pattern
/// </summary>
/// <param name="name">The name pattern being matched against</param>
/// <returns>All children that don't match the name pattern</returns>
public List<ISelectable> SelectWithoutName(string name);
/// <summary>
/// Select from children by element
/// </summary>
/// <param name="element">The element being selected by</param>
/// <returns>All children that are the element</returns>
public List<ISelectable> SelectByElement(string element);

/// <summary>
/// Select all children
/// </summary>
/// <returns>All children</returns>
public List<ISelectable> SelectEverything();


/// <summary>
/// Test if this selectable matches a name pattern
Expand All @@ -56,6 +23,13 @@ public interface ISelectable
/// <param name="class">The class</param>
/// <returns>True if it has the class</returns>
public bool MatchesClass(string @class);

/// <summary>
/// Get the value for a class
/// </summary>
public bool MatchesClass(string @class, out DataValue classValue);


/// <summary>
/// Test if this selectable is an element
/// </summary>
Expand Down
Loading

0 comments on commit 16a09b1

Please sign in to comment.