Skip to content

Commit

Permalink
v1.1.6
Browse files Browse the repository at this point in the history
- Added compatibility for Prison Labor
  • Loading branch information
Jaxe-Dev committed Sep 30, 2018
1 parent 13a3b2a commit c7cba32
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 42 deletions.
2 changes: 1 addition & 1 deletion About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<name>Pawn Rules</name>
<author>Jaxe</author>
<targetVersion>0.19.0</targetVersion>
<description>Mod Version: 1.1.5\n\n\nPawn Rules is a mod that allows custom rules to be assigned individually to your colonists, animals, guests and prisoners.\n\nCurrently the following rules can be applied:\n\n- Disallow certain foods\n- Disallow bonding with certain animals\n- Disallow new romances\n- Disallow constructing items that have a quality level\n\nAny of these rules can be disabled and hidden from the rules window. Rules presets and defaults can be imported and exported between games.</description>
<description>Mod Version: 1.1.6\n\n\nPawn Rules is a mod that allows custom rules to be assigned individually to your colonists, animals, guests and prisoners.\n\nCurrently the following rules can be applied:\n\n- Disallow certain foods\n- Disallow bonding with certain animals\n- Disallow new romances\n- Disallow constructing items that have a quality level\n\nAny of these rules can be disabled and hidden from the rules window. Rules presets and defaults can be imported and exported between games.</description>
<url>https://github.com/Jaxe-Dev/PawnRules</url>
</ModMetaData>
2 changes: 1 addition & 1 deletion About/ModSync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ModSyncNinjaData>
<ID>59f538ed-f86d-4506-a4a5-7e9faaa37508</ID>
<ModName>Pawn Rules</ModName>
<Version>v1.1.5</Version>
<Version>v1.1.6</Version>
<SaveBreaking>False</SaveBreaking>
<Host name="Github">
<Owner>Jaxe-Dev</Owner>
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Pawn Rules
![](https://img.shields.io/badge/Mod_Version-1.1.5-blue.svg)
![](https://img.shields.io/badge/Built_for_RimWorld-B19-blue.svg)
![](https://img.shields.io/badge/Powered_by_Harmony-1.2.0.1-blue.svg)
![Mod Version](https://img.shields.io/badge/Mod_Version-1.1.6-blue.svg)
![RimWorld Version](https://img.shields.io/badge/Built_for_RimWorld-B19-blue.svg)
![Harmony Version](https://img.shields.io/badge/Powered_by_Harmony-1.2.0.1-blue.svg)\
![Steam Subscribers](https://img.shields.io/badge/dynamic/xml.svg?label=Steam+Subscribers&query=//table/tr[2]/td[1]&colorB=blue&url=https://steamcommunity.com/sharedfiles/filedetails/%3Fid=1499843448&suffix=+total)
![GitHub Downloads](https://img.shields.io/github/downloads/Jaxe-Dev/PawnRules/total.svg?colorB=blue&label=GitHub+Downloads)


[Link to Steam Workshop page](https://steamcommunity.com/sharedfiles/filedetails/?id=1499843448)\
[Link to Ludeon Forum thread](https://ludeon.com/forums/index.php?topic=43086.0)
Expand Down
68 changes: 68 additions & 0 deletions Source/Compatibility/PrisonLabor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Linq;
using System.Reflection;
using Harmony;
using PawnRules.Data;
using PawnRules.Patch;
using RimWorld;
using Verse;

namespace PawnRules.Compatibility
{
internal static class PrisonLabor
{
private const bool AllowHarvest = true;
private const bool ForceScanWholeMap = true;
public static void Setup()
{
var assembly = GetAssembly("PrisonLabor");
var foodTweaks = assembly?.GetType("FoodUtility_Tweak");
if (foodTweaks == null) { return; }

var tryFindBestFoodSourceFor = AccessTools.Method(foodTweaks, "TryFindBestFoodSourceFor");
var bestFoodInInventory = AccessTools.Method(foodTweaks, "BestFoodInInventory");
var bestFoodSourceOnMap = AccessTools.Method(foodTweaks, "BestFoodSourceOnMap");

if ((tryFindBestFoodSourceFor == null) || (bestFoodInInventory == null) || (bestFoodSourceOnMap == null)) { return; }

Patcher.Harmony.Patch(tryFindBestFoodSourceFor, new HarmonyMethod(typeof(PrisonLabor), nameof(TryFindBestFoodSourceFor)));
Patcher.Harmony.Patch(bestFoodInInventory, new HarmonyMethod(typeof(PrisonLabor), nameof(BestFoodInInventory)));
Patcher.Harmony.Patch(bestFoodSourceOnMap, new HarmonyMethod(typeof(PrisonLabor), nameof(BestFoodSourceOnMap)));

Mod.Warning("Compatibility patch added for Prison Labor");
}

private static bool TryFindBestFoodSourceFor(ref bool __result, Pawn getter, Pawn eater, bool desperate, out Thing foodSource, out ThingDef foodDef, bool canRefillDispenser = true, bool canUseInventory = true, bool allowForbidden = false, bool allowCorpse = true, bool allowSociallyImproper = false)
{
foodSource = null;
foodDef = null;

var restriction = Registry.GetRules(eater)?.GetRestriction(RestrictionType.Food);
if (eater.InMentalState || (restriction == null) || restriction.IsVoid) { return true; }

__result = FoodUtility.TryFindBestFoodSourceFor(getter, eater, desperate, out foodSource, out foodDef, canRefillDispenser, canUseInventory, allowForbidden, allowCorpse, allowSociallyImproper, AllowHarvest, ForceScanWholeMap);
return false;
}

private static bool BestFoodInInventory(ref Thing __result, Pawn holder, Pawn eater = null, FoodPreferability minFoodPref = FoodPreferability.NeverForNutrition, FoodPreferability maxFoodPref = FoodPreferability.MealLavish, float minStackNutrition = 0f, bool allowDrug = false)
{
if (eater == null) { eater = holder; }

var restriction = Registry.GetRules(eater)?.GetRestriction(RestrictionType.Food);
if (eater.InMentalState || (restriction == null) || restriction.IsVoid) { return true; }

__result = FoodUtility.BestFoodInInventory(holder, eater, minFoodPref, maxFoodPref, minStackNutrition, allowDrug);
return false;
}

private static bool BestFoodSourceOnMap(ref Thing __result, Pawn getter, Pawn eater, bool desperate, FoodPreferability maxPref = FoodPreferability.MealLavish, bool allowPlant = true, bool allowDrug = true, bool allowCorpse = true, bool allowDispenserFull = true, bool allowDispenserEmpty = true, bool allowForbidden = false, bool allowSociallyImproper = false)
{
var restriction = Registry.GetRules(eater)?.GetRestriction(RestrictionType.Food);
if (eater.InMentalState || (restriction == null) || restriction.IsVoid) { return true; }

__result = FoodUtility.BestFoodSourceOnMap(getter, eater, desperate, out var _, maxPref, allowPlant, allowDrug, allowCorpse, allowDispenserFull, allowDispenserEmpty, allowForbidden, allowSociallyImproper, AllowHarvest, ForceScanWholeMap);
return false;
}

private static Assembly GetAssembly(string name) => LoadedModManager.RunningMods.Select(mod => mod.assemblies.loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == name)).FirstOrDefault(assembly => assembly != null);
}
}
19 changes: 0 additions & 19 deletions Source/Controller.cs

This file was deleted.

12 changes: 10 additions & 2 deletions Source/Integration/RimHUD.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PawnRules.Data;
using PawnRules.Interface;
using Verse;

namespace PawnRules.Integration
Expand All @@ -11,10 +12,17 @@ public static string GetRulesInfo(Pawn pawn)
{
if (!Registry.IsActive) { return null; }

var rules = Registry.GetRules(pawn);
var rules = Registry.GetOrNewRules(pawn);
if (rules == null) { return null; }

return rules.IsPreset ? rules.Name : Lang.Get("Preset.Personalized");
return rules.IsPreset ? rules.IsVoid ? null : rules.Name : Lang.Get("Preset.Personalized");
}

public static void OpenRulesDialog(Pawn pawn)
{
if (!Registry.IsActive) { return; }

Dialog_Rules.Open(pawn);
}
}
}
11 changes: 9 additions & 2 deletions Source/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class Mod : Verse.Mod
{
public const string Id = "PawnRules";
public const string Name = "Pawn Rules";
public const string Version = "1.1.5";
public const string Version = "1.1.6";

public static readonly DirectoryInfo ConfigDirectory = new DirectoryInfo(Path.Combine(GenFilePaths.ConfigFolderPath, Id));

Expand All @@ -23,12 +23,13 @@ internal class Mod : Verse.Mod
public Mod(ModContentPack contentPack) : base(contentPack)
{
Instance = this;
Log("Loaded");

FirstTimeUser = !ConfigDirectory.Exists;
ConfigDirectory.Create();

if (!FirstTimeUser) { HugsLib.RegisterUpdateFeature(); }

Log("Initialized");
}

public static void Log(string message) => Verse.Log.Message(PrefixMessage(message));
Expand All @@ -54,6 +55,12 @@ public override void DoSettingsWindowContents(Rect inRect)
listing.End();
}

public static void LoadWorld()
{
AddonManager.AcceptingAddons = false;
Registry.Initialize();
}

internal class Exception : System.Exception
{
public Exception(string message) : base($"[{Name} v{Version} : EXCEPTION] {message}")
Expand Down
14 changes: 14 additions & 0 deletions Source/Patch/Patcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Reflection;
using Harmony;
using Verse;

namespace PawnRules.Patch
{
[StaticConstructorOnStartup]
internal static class Patcher
{
public static HarmonyInstance Harmony { get; } = HarmonyInstance.Create(Mod.Id);

static Patcher() => Harmony.PatchAll(Assembly.GetExecutingAssembly());
}
}
2 changes: 1 addition & 1 deletion Source/Patch/Verse_Game_FinalizeInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace PawnRules.Patch
[HarmonyPatch(typeof(Game), "FinalizeInit")]
internal static class Verse_Game_FinalizeInit
{
private static void Postfix() => Controller.LoadWorld();
private static void Postfix() => Mod.LoadWorld();
}
}
15 changes: 2 additions & 13 deletions Source/PawnRules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\Assemblies\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>default</LangVersion>
<DocumentationFile>..\Assemblies\PawnRules.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
Expand Down Expand Up @@ -56,7 +44,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Mod.cs" />
<Compile Include="Controller.cs" />
<Compile Include="Compatibility\PrisonLabor.cs" />
<Compile Include="Data\AddonManager.cs" />
<Compile Include="Data\AddonOption.cs" />
<Compile Include="Data\Binding.cs" />
Expand Down Expand Up @@ -86,6 +74,7 @@
<Compile Include="Interface\WindowPlus.cs" />
<Compile Include="Patch\Extensions.cs" />
<Compile Include="Patch\Access.cs" />
<Compile Include="Patch\Patcher.cs" />
<Compile Include="Patch\RimWorld_FoodUtility_BestFoodInInventory.cs" />
<Compile Include="Patch\RimWorld_FoodUtility_BestFoodSourceOnMap.cs" />
<Compile Include="Patch\RimWorld_FoodUtility_TryFindBestFoodSourceFor.cs" />
Expand Down

0 comments on commit c7cba32

Please sign in to comment.