Skip to content

Commit

Permalink
prerelease cleaning and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Denadan committed Jun 30, 2018
1 parent 9a703df commit c06c9db
Show file tree
Hide file tree
Showing 20 changed files with 139 additions and 40 deletions.
4 changes: 4 additions & 0 deletions Settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"LoadDefaultValidators": "True",
"logLevel": "Debug"
}
6 changes: 6 additions & 0 deletions source/ColorChanger/IColorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace CustomComponents
{
/// <summary>
/// component has specific color
/// </summary>
interface IColorComponent
{
/// <summary>
/// color of component
/// </summary>
UIColor Color { get; }
}
}
2 changes: 1 addition & 1 deletion source/ColorChanger/MechComponentRef_GetUIColor_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CustomComponents
{
[HarmonyPatch(typeof(MechComponentRef), "GetUIColor")]
public static class MechComponentRef_GetUIColor
internal static class MechComponentRef_GetUIColor
{

[HarmonyPostfix]
Expand Down
23 changes: 19 additions & 4 deletions source/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@
using System.Collections.Generic;
using BattleTech.Data;
using System.Text.RegularExpressions;
using BattleTech;

namespace CustomComponents
{
public static class Control
{
internal static Mod mod;
public static Mod mod;
public static CustomCompoentSettings settings = new CustomCompoentSettings();

public static void Init(string directory, string settingsJSON)
{
mod = new Mod(directory);

try
{
// mod.LoadSettings(settings);
mod.LoadSettings(settings);

var harmony = HarmonyInstance.Create("io.github.denadan.CustomComponents");
harmony.PatchAll(Assembly.GetExecutingAssembly());
RegisterCustomTypes(Assembly.GetExecutingAssembly());

Validator.RegisterValidator(WeighLimitedController.ValidateMech);
Validator.RegisterAddValidator(typeof(IWeightLimited), WeighLimitedController.ValidateAdd);
if (settings.LoadDefaultValidators)
{
Validator.RegisterValidator(WeighLimitedController.ValidateMech);
Validator.RegisterAddValidator(typeof(IWeightLimited), WeighLimitedController.ValidateAdd);
}

// logging output can be found under BATTLETECH\BattleTech_Data\output_log.txt
// or also under yourmod/log.txt
Expand All @@ -49,6 +56,11 @@ internal static ICustomComponent CreateNew(string custom_type)

private static Dictionary<string, CustomComponentDescriptor> descriptors = new Dictionary<string, CustomComponentDescriptor>();


/// <summary>
/// register custom types for your assamble
/// </summary>
/// <param name="assembly"></param>
public static void RegisterCustomTypes(Assembly assembly)
{
var desc_list = from type in assembly.GetTypes()
Expand Down Expand Up @@ -81,7 +93,8 @@ public static bool LoaderPatch<T>(DataManager.ResourceLoadRequest<T> loader,
return true;

string custom_type = custom.Result("$1");
Control.mod.Logger.Log("Loading custom: " + custom_type);

Control.mod.Logger.LogDebug("Loading custom: " + custom_type);

var custom_obj = Control.CreateNew(custom_type) as ICustomComponent;
if (custom_obj == null || !(custom_obj is T))
Expand All @@ -97,6 +110,8 @@ public static bool LoaderPatch<T>(DataManager.ResourceLoadRequest<T> loader,

resource = custom_obj as T;
Traverse.Create(loader).Method("TryLoadDependencies", resource).GetValue();
if(custom_obj is MechComponentDef)
Control.mod.Logger.LogDebug("Loaded: " + (custom_obj as MechComponentDef).Description.Id);

return false;
}
Expand Down
12 changes: 12 additions & 0 deletions source/CustomCompoentSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using DynModLib;

namespace CustomComponents
{
public class CustomCompoentSettings : ModSettings
{
public bool LoadDefaultValidators = true;
}
}
13 changes: 9 additions & 4 deletions source/CustomComponent.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
using BattleTech;
using System;
using System;
using System.Reflection;

namespace CustomComponents
{

/// <summary>
/// basic interface for custom component
/// </summary>
public interface ICustomComponent
{
string CustomType { get; }
void FromJson(string json);
string ToJson();
}

/// <summary>
/// mark class as custom component
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class CustomAttribute : Attribute
{
public string CustomType { get; set; }
Expand All @@ -22,7 +27,7 @@ public CustomAttribute(string CustomType)
}
}

public class CustomComponentDescriptor
internal class CustomComponentDescriptor
{
public string CustomName { get; private set; }
public Type ActualType { get; private set; }
Expand Down
1 change: 1 addition & 0 deletions source/CustomComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="ColorChanger\IColorComponent.cs" />
<Compile Include="ColorChanger\MechComponentRef_GetUIColor_Patch.cs" />
<Compile Include="Control.cs" />
<Compile Include="CustomCompoentSettings.cs" />
<Compile Include="CustomComponent.cs" />
<Compile Include="CustomTypes\CustomAmmunitionBoxDef.cs" />
<Compile Include="CustomTypes\CustomJumpJetDef.cs" />
Expand Down
4 changes: 4 additions & 0 deletions source/CustomTypes/CustomAmmunitionBoxDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

namespace CustomComponents
{
/// <summary>
/// Custom ammobox definition
/// </summary>
/// <typeparam name="T">Your type</typeparam>
public class CustomAmmunitionBoxDef<T> : BattleTech.AmmunitionBoxDef, ICustomComponent
where T : CustomAmmunitionBoxDef<T>
{
Expand Down
6 changes: 4 additions & 2 deletions source/HarmonyNested/HarmonyNestedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

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)
{
Expand All @@ -19,7 +21,7 @@ public HarmonyNestedAttribute(Type baseType, string nestedType, string method, T
this.info.parameter = parameters;
this.info.methodName = method;

Control.mod.Logger.Log(string.Format("Type: {0}\tMethod: {1}",
Control.mod.Logger.LogDebug(string.Format("Type: {0}\tMethod: {1}",
this.info.originalType, this.info.methodName));
}
}
Expand Down
1 change: 0 additions & 1 deletion source/Test/TestUpgradeDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class TestUpgradeDef : CustomUpgradeDef<TestUpgradeDef>, IColorComponent

public TestUpgradeDef()
{

}
}

Expand Down
4 changes: 2 additions & 2 deletions source/TypePatch/AmmoBoxPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CustomComponents
{
[HarmonyNested(typeof(DataManager), "AmmunitionBoxDefLoadRequest", "OnLoadedWithJSON")]
public static class DataManager_AmmunitionBoxDef_Patch
internal static class DataManager_AmmunitionBoxDef_Patch
{
public static bool Prefix(DataManager.ResourceLoadRequest<AmmunitionBoxDef> __instance,
string json, ref AmmunitionBoxDef ___resource)
Expand All @@ -17,7 +17,7 @@ public static bool Prefix(DataManager.ResourceLoadRequest<AmmunitionBoxDef> __in
}

[HarmonyPatch(typeof(AmmunitionBoxDef), "ToJSON")]
public static class AmmunitionBoxDef_ToJSON_Patch
internal static class AmmunitionBoxDef_ToJSON_Patch
{
public static bool Prefix(AmmunitionBoxDef __instance, ref string __result)
{
Expand Down
4 changes: 2 additions & 2 deletions source/TypePatch/HeatSinkPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CustomComponents
{
[HarmonyNested(typeof(DataManager), "HeatSinkDefLoadRequest", "OnLoadedWithJSON")]
public static class DataManager_HeatSink_Patch
internal static class DataManager_HeatSink_Patch
{
public static bool Prefix(DataManager.ResourceLoadRequest<HeatSinkDef> __instance,
string json, ref HeatSinkDef ___resource)
Expand All @@ -17,7 +17,7 @@ public static bool Prefix(DataManager.ResourceLoadRequest<HeatSinkDef> __instanc
}

[HarmonyPatch(typeof(HeatSinkDef), "ToJSON")]
public static class HeatSinkDef_ToJSON_Patch
internal static class HeatSinkDef_ToJSON_Patch
{
public static bool Prefix(HeatSinkDef __instance, ref string __result)
{
Expand Down
4 changes: 2 additions & 2 deletions source/TypePatch/JumpJetPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CustomComponents
{
[HarmonyNested(typeof(DataManager), "JumpJetDefLoadRequest", "OnLoadedWithJSON")]
public static class DataManager_JumpJet_Patch
internal static class DataManager_JumpJet_Patch
{
public static bool Prefix(DataManager.ResourceLoadRequest<JumpJetDef> __instance,
string json, ref JumpJetDef ___resource)
Expand All @@ -17,7 +17,7 @@ public static bool Prefix(DataManager.ResourceLoadRequest<JumpJetDef> __instance
}

[HarmonyPatch(typeof(JumpJetDef), "ToJSON")]
public static class JumpJetDef_ToJSON_Patch
internal static class JumpJetDef_ToJSON_Patch
{
public static bool Prefix(JumpJetDef __instance, ref string __result)
{
Expand Down
4 changes: 2 additions & 2 deletions source/TypePatch/UpgradePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CustomComponents
{
[HarmonyNested(typeof(DataManager), "UpgradeDefLoadRequest", "OnLoadedWithJSON")]
public static class DataManager_Upgrade_Patch
internal static class DataManager_Upgrade_Patch
{
public static bool Prefix(DataManager.ResourceLoadRequest<UpgradeDef> __instance,
string json, ref UpgradeDef ___resource)
Expand All @@ -17,7 +17,7 @@ public static bool Prefix(DataManager.ResourceLoadRequest<UpgradeDef> __instance
}

[HarmonyPatch(typeof(UpgradeDef), "ToJSON")]
public static class UpgradeDef_ToJSON_Patch
internal static class UpgradeDef_ToJSON_Patch
{
public static bool Prefix(UpgradeDef __instance, ref string __result)
{
Expand Down
4 changes: 2 additions & 2 deletions source/TypePatch/WeaponDefPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CustomComponents
{
[HarmonyNested(typeof(DataManager), "WeaponDefLoadRequest", "OnLoadedWithJSON")]
public static class DataManager_WeaponDef_Patch
internal static class DataManager_WeaponDef_Patch
{
public static bool Prefix(DataManager.ResourceLoadRequest<WeaponDef> __instance,
string json, ref WeaponDef ___resource)
Expand All @@ -17,7 +17,7 @@ public static bool Prefix(DataManager.ResourceLoadRequest<WeaponDef> __instance,
}

[HarmonyPatch(typeof(WeaponDef), "ToJSON")]
public static class WeaponDef_ToJSON_Patch
internal static class WeaponDef_ToJSON_Patch
{
public static bool Prefix(WeaponDef __instance, ref string __result)
{
Expand Down
21 changes: 13 additions & 8 deletions source/Validators/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CustomComponents
{
public delegate bool ValidateAddDelegate(MechComponentDef component, MechLabLocationWidget widget,
bool current_result, ref string errorMessage, MechLabPanel mechlab);

public delegate void ValidateMechDelegate(Dictionary<MechValidationType, List<string>> errors,
MechValidationLevel validationLevel, MechDef mechDef);

/// <summary>
/// Static class to make validation
/// </summary>
public static class Validator
{
static Dictionary<Type, ValidateAddDelegate> add_validators = new Dictionary<Type, ValidateAddDelegate>();
static List<ValidateMechDelegate> validators = new List<ValidateMechDelegate>();

/// <summary>
/// register new AddValidator
/// </summary>
/// <param name="type"></param>
/// <param name="validator"></param>
public static void RegisterAddValidator(Type type, ValidateAddDelegate validator)
{
add_validators.Add(type, validator);
}

/// <summary>
/// register new mech validator
/// </summary>
/// <param name="validator"></param>
public static void RegisterValidator(ValidateMechDelegate validator)
{
validators.Add(validator);
Expand Down Expand Up @@ -71,7 +76,7 @@ MechLabPanel ___mechLab
}

[HarmonyPatch(typeof(MechValidationRules), "ValidateMechDef")]
public static class MechValidationRulesValidate_ValidateMech_Patch
internal static class MechValidationRulesValidate_ValidateMech_Patch
{
public static void Postfix(Dictionary<MechValidationType, List<string>> __result,
MechValidationLevel validationLevel, MechDef mechDef)
Expand Down
43 changes: 42 additions & 1 deletion source/Validators/ValidatorInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,56 @@

namespace CustomComponents
{
/// <summary>
/// component check if it can be added to this location
/// </summary>
public interface IValidateAdd
{
/// <summary>
/// validation check
/// </summary>
/// <param name="widget">location, where check</param>
/// <param name="current_result">result of previous checks</param>
/// <param name="errorMessage">message, that show as warning if cannot add item</param>
/// <param name="mechlab"></param>
/// <returns></returns>
bool ValidateAdd(MechLabLocationWidget widget, bool current_result, ref string errorMessage,
MechLabPanel mechlab);
}

/// <summary>
/// component need to validate mech state
/// </summary>
public interface IMechValidate
{
/// <summary>
/// validate mech
/// </summary>
/// <param name="errors">list of errors</param>
/// <param name="validationLevel">level of validation</param>
/// <param name="mechDef">mech to check</param>
void ValidateMech(Dictionary<MechValidationType, List<string>> errors,
MechValidationLevel validationLevel, MechDef mechDef);
}

/// <summary>
/// delegate for "component can be added" validation
/// </summary>
/// <param name="component">component to validate</param>
/// <param name="widget">location to add</param>
/// <param name="current_result">resulr of previous сруслы</param>
/// <param name="errorMessage">error message</param>
/// <param name="mechlab"></param>
/// <returns></returns>
public delegate bool ValidateAddDelegate(MechComponentDef component, MechLabLocationWidget widget,
bool current_result, ref string errorMessage, MechLabPanel mechlab);

/// <summary>
/// delegate for mech valudation
/// </summary>
/// <param name="errors">list of errors</param>
/// <param name="validationLevel">level of validation</param>
/// <param name="mechDef">mech to validate</param>
public delegate void ValidateMechDelegate(Dictionary<MechValidationType, List<string>> errors,
MechValidationLevel validationLevel, MechDef mechDef);

}
Loading

0 comments on commit c06c9db

Please sign in to comment.