Skip to content

Commit

Permalink
RequieredCategory, RequieredID
Browse files Browse the repository at this point in the history
  • Loading branch information
Denadan committed Jul 28, 2018
1 parent 813a504 commit 8422aa1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
33 changes: 33 additions & 0 deletions source/Components/RequieredCategory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using BattleTech;

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

public void ValidateMech(Dictionary<MechValidationType, List<string>> errors, MechValidationLevel validationLevel, MechDef mechDef,
MechComponentRef componentRef)
{
if (string.IsNullOrEmpty(CategoryID))
return;
var category = Control.GetCategory(CategoryID);
if (mechDef.Inventory.Any(i => i.IsCategory(CategoryID)))
return;
errors[MechValidationType.InvalidInventorySlots].Add(string.IsNullOrEmpty(ErrorMessage) ?
$"{Def.Description.Name} requшere {category.displayName} installed" :
ErrorMessage);
}

public bool ValidateMechCanBeFielded(MechDef mechDef, MechComponentRef componentRef)
{
if (string.IsNullOrEmpty(CategoryID))
return true;
return mechDef.Inventory.Any(i => i.IsCategory(CategoryID));
}
}
}
38 changes: 38 additions & 0 deletions source/Components/RequieredID.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;
using BattleTech;

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

public void ValidateMech(Dictionary<MechValidationType, List<string>> errors, MechValidationLevel validationLevel, MechDef mechDef,
MechComponentRef componentRef)
{
if (string.IsNullOrEmpty(ComponentDefId))
return;
foreach (var cref in mechDef.Inventory)
{
if (cref.ComponentDefID == ComponentDefId)
return;
}
errors[MechValidationType.InvalidInventorySlots].Add(string.IsNullOrEmpty(ErrorMessage) ? $"{Def.Description.Name} missed requred components" : ErrorMessage);
}

public bool ValidateMechCanBeFielded(MechDef mechDef, MechComponentRef componentRef)
{
if (string.IsNullOrEmpty(ComponentDefId))
return true;
foreach (var cref in mechDef.Inventory)
{
if (cref.ComponentDefID == ComponentDefId)
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.6.0.1.0");
Logger.Log("Loaded CustomComponents v0.6.2.0.0");
#if CCDEBUG
Logger.LogDebug("Loading Categories");
#endif
Expand Down
2 changes: 2 additions & 0 deletions source/CustomComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
<Compile Include="CCLight\DataManagerPerfomance\DMPHelper.cs" />
<Compile Include="CCLight\Factories\IPostProcessor.cs" />
<Compile Include="CCLight\Factories\IPreProcessor.cs" />
<Compile Include="Components\RequieredCategory.cs" />
<Compile Include="Components\RequieredID.cs" />
<Compile Include="Components\Sorter.cs" />
<Compile Include="DropResults\IChange.cs" />
<Compile Include="DropResults\InvItem.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public static bool Prefix(InventoryItemElement theWidget, ListElementController_
}
catch (Exception e)
{
Control.Logger.LogError("Salvage coloring problem!", e);
//Control.Logger.LogError("Salvage coloring problem!", e);
return false;
}

return true;
Expand Down

0 comments on commit 8422aa1

Please sign in to comment.