Skip to content

Commit

Permalink
RequieredCategories
Browse files Browse the repository at this point in the history
  • Loading branch information
Denadan committed Sep 10, 2018
1 parent d2c52e9 commit 4ede6fd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
42 changes: 42 additions & 0 deletions source/Components/RequieredCategories.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Linq;
using BattleTech;

namespace CustomComponents
{
[CustomComponent("RequiredCategories")]
public class RequiredCategories : SimpleCustomComponent, IMechValidate
{
public string[] Categories { get; set; }
public string ErrorMessage { get; set; }

public void ValidateMech(Dictionary<MechValidationType, List<Localize.Text>> errors, MechValidationLevel validationLevel, MechDef mechDef,
MechComponentRef componentRef)
{
if (Categories == null || Categories.Length == 0)
return;

foreach (var category in Categories)
{
if (mechDef.Inventory.Any(i => i.IsCategory(category)))
return;
}

errors[MechValidationType.InvalidInventorySlots].Add(new Localize.Text(string.IsNullOrEmpty(ErrorMessage) ?
$"{Def.Description.Name} requires additional modules to work" :
ErrorMessage));
}

public bool ValidateMechCanBeFielded(MechDef mechDef, MechComponentRef componentRef)
{
if (Categories == null || Categories.Length == 0)
return true;
foreach (var category in Categories)
{
if (mechDef.Inventory.Any(i => i.IsCategory(category)))
return true;
}
return false;
}
}
}
2 changes: 1 addition & 1 deletion source/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void Init(string directory, string settingsJSON)
Validator.RegisterMechValidator(CategoryController.ValidateMech, CategoryController.ValidateMechCanBeFielded);


Logger.Log("Loaded CustomComponents v0.7.0.3.0");
Logger.Log("Loaded CustomComponents v0.7.1.0.0");
#if CCDEBUG
Logger.LogDebug("Loading Categories");
#endif
Expand Down
1 change: 1 addition & 0 deletions source/CustomComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="CCLight\DataManagerPerfomance\DMPHelper.cs" />
<Compile Include="CCLight\Factories\IPreProcessor.cs" />
<Compile Include="CCLight\Factories\SimpleCustom.cs" />
<Compile Include="Components\RequieredCategories.cs" />
<Compile Include="Components\RequieredCategory.cs" />
<Compile Include="Components\RequieredID.cs" />
<Compile Include="Components\Sorter.cs" />
Expand Down
28 changes: 14 additions & 14 deletions source/Helpers/HarmonyNestedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ namespace CustomComponents
/// <summary>
/// mark a patch for private nested class
/// </summary>
public class HarmonyNestedAttribute : HarmonyPatch
{
public HarmonyNestedAttribute(Type baseType, string nestedType, string method, Type[] parameters = null)
: base(null, method, null)
{
this.info.declaringType = baseType.GetNestedType(nestedType, BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic);
this.info.argumentTypes = parameters;
this.info.methodName = method;
//public class HarmonyNestedAttribute : HarmonyPatch
//{
// public HarmonyNestedAttribute(Type baseType, string nestedType, string method, Type[] parameters = null)
// : base(null, method, null)
// {
// this.info.declaringType = baseType.GetNestedType(nestedType, BindingFlags.Static |
// BindingFlags.Instance |
// BindingFlags.Public |
// BindingFlags.NonPublic);
// this.info.argumentTypes = parameters;
// this.info.methodName = method;

Control.Logger.LogDebug($"Type: {this.info.declaringType}\tMethod: {this.info.methodName}");
}
}
// Control.Logger.LogDebug($"Type: {this.info.declaringType}\tMethod: {this.info.methodName}");
// }
//}
}

0 comments on commit 4ede6fd

Please sign in to comment.