Skip to content

Commit

Permalink
Sync reading policies
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetrith committed Apr 11, 2024
1 parent 45db604 commit 824d2f4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Source/Client/Syncing/Dict/SyncDictRimWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Multiplayer.Common;
using RimWorld;
using RimWorld.Planet;
using RimWorld.Utility;
using Verse;
using Verse.AI;
using Verse.AI.Group;
Expand Down Expand Up @@ -93,6 +92,10 @@ public static class SyncDictRimWorld
(ByteWriter data, Pawn_FoodRestrictionTracker comp) => WriteSync(data, comp.pawn),
(ByteReader data) => ReadSync<Pawn>(data)?.foodRestriction
},
{
(ByteWriter data, Pawn_ReadingTracker comp) => WriteSync(data, comp.pawn),
(ByteReader data) => ReadSync<Pawn>(data)?.reading
},
{
(ByteWriter data, Pawn_TrainingTracker comp) => WriteSync(data, comp.pawn),
(ByteReader data) => ReadSync<Pawn>(data)?.training
Expand Down Expand Up @@ -806,6 +809,7 @@ public static class SyncDictRimWorld
{ (SyncWorker data, ref OutfitDatabase db) => db = Current.Game.outfitDatabase },
{ (SyncWorker data, ref DrugPolicyDatabase db) => db = Current.Game.drugPolicyDatabase },
{ (SyncWorker data, ref FoodRestrictionDatabase db) => db = Current.Game.foodRestrictionDatabase },
{ (SyncWorker data, ref ReadingPolicyDatabase db) => db = Current.Game.readingPolicyDatabase },

#endregion

Expand Down
20 changes: 20 additions & 0 deletions Source/Client/Syncing/Game/SyncMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static void Init()
SyncMethod.Register(typeof(Pawn_DrugPolicyTracker), nameof(Pawn_DrugPolicyTracker.CurrentPolicy)).CancelIfAnyArgNull();
SyncMethod.Register(typeof(Pawn_OutfitTracker), nameof(Pawn_OutfitTracker.CurrentApparelPolicy)).CancelIfAnyArgNull();
SyncMethod.Register(typeof(Pawn_FoodRestrictionTracker), nameof(Pawn_FoodRestrictionTracker.CurrentFoodPolicy)).CancelIfAnyArgNull();
SyncMethod.Register(typeof(Pawn_ReadingTracker), nameof(Pawn_ReadingTracker.CurrentPolicy)).CancelIfAnyArgNull();
SyncMethod.Register(typeof(Policy), nameof(Policy.RenamableLabel));
SyncMethod.Register(typeof(Pawn_PlayerSettings), nameof(Pawn_PlayerSettings.AreaRestrictionInPawnCurrentMap));
SyncMethod.Register(typeof(Pawn_PlayerSettings), nameof(Pawn_PlayerSettings.Master));
Expand Down Expand Up @@ -59,6 +60,8 @@ public static void Init()
SyncMethod.Register(typeof(OutfitDatabase), nameof(OutfitDatabase.TryDelete)).CancelIfAnyArgNull();
SyncMethod.Register(typeof(FoodRestrictionDatabase), nameof(FoodRestrictionDatabase.MakeNewFoodRestriction));
SyncMethod.Register(typeof(FoodRestrictionDatabase), nameof(FoodRestrictionDatabase.TryDelete)).CancelIfAnyArgNull();
SyncMethod.Register(typeof(ReadingPolicyDatabase), nameof(ReadingPolicyDatabase.MakeNewReadingPolicy));
SyncMethod.Register(typeof(ReadingPolicyDatabase), nameof(ReadingPolicyDatabase.TryDelete)).CancelIfAnyArgNull();

SyncMethod.Register(typeof(Building_Bed), nameof(Building_Bed.Medical));

Expand Down Expand Up @@ -373,6 +376,14 @@ static void MakeNewFood_Postfix(FoodPolicy __result)
dialog.SelectedPolicy = __result;
}

[MpPostfix(typeof(ReadingPolicyDatabase), nameof(ReadingPolicyDatabase.MakeNewReadingPolicy))]
static void MakeNewReading_Postfix(ReadingPolicy __result)
{
var dialog = GetDialogReadingPolicies();
if (__result != null && dialog != null && TickPatch.currentExecutingCmdIssuedBySelf)
dialog.SelectedPolicy = __result;
}

[MpPostfix(typeof(DrugPolicyDatabase), nameof(DrugPolicyDatabase.TryDelete))]
static void TryDeleteDrugPolicy_Postfix(DrugPolicy policy, AcceptanceReport __result)
{
Expand All @@ -397,9 +408,18 @@ static void TryDeleteFood_Postfix(FoodPolicy foodPolicy, AcceptanceReport __resu
dialog.SelectedPolicy = null;
}

[MpPostfix(typeof(ReadingPolicyDatabase), nameof(ReadingPolicyDatabase.TryDelete))]
static void TryDeleteReading_Postfix(ReadingPolicy policy, AcceptanceReport __result)
{
var dialog = GetDialogReadingPolicies();
if (__result.Accepted && dialog != null && dialog.SelectedPolicy == policy)
dialog.SelectedPolicy = null;
}

static Dialog_ManageDrugPolicies GetDialogDrugPolicies() => Find.WindowStack?.WindowOfType<Dialog_ManageDrugPolicies>();
static Dialog_ManageApparelPolicies GetDialogOutfits() => Find.WindowStack?.WindowOfType<Dialog_ManageApparelPolicies>();
static Dialog_ManageFoodPolicies GetDialogFood() => Find.WindowStack?.WindowOfType<Dialog_ManageFoodPolicies>();
static Dialog_ManageReadingPolicies GetDialogReadingPolicies() => Find.WindowStack?.WindowOfType<Dialog_ManageReadingPolicies>();

[MpPostfix(typeof(WITab_Caravan_Gear), nameof(WITab_Caravan_Gear.TryEquipDraggedItem))]
static void TryEquipDraggedItem_Postfix(WITab_Caravan_Gear __instance)
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/Syncing/Game/SyncThingFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static void AllowCategory_Helper(ThingFilterContext context, ThingCatego
categoryDef,
allow,
null,
Listing_TreeThingFilter
context.Filter.OnlySpecialFilters ? null : Listing_TreeThingFilter
.CalculateHiddenSpecialFilters(node, context.ParentFilter)
.ConcatIfNotNull(context.HiddenFilters)
);
Expand Down
12 changes: 12 additions & 0 deletions Source/Client/Syncing/Game/ThingFilterContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public record FoodRestrictionWrapper(FoodPolicy Policy) : ThingFilterContext
public override IEnumerable<SpecialThingFilterDef> HiddenFilters => SpecialThingFilterDefOf.AllowFresh.ToEnumerable();
}

public record ReadingPolicyDefFilterWrapper(ReadingPolicy Policy) : ThingFilterContext
{
public override ThingFilter Filter => Policy.defFilter;
public override ThingFilter ParentFilter => Dialog_ManageReadingPolicies.PolicyGlobalFilter;
}

public record ReadingPolicyEffectFilterWrapper(ReadingPolicy Policy) : ThingFilterContext
{
public override ThingFilter Filter => Policy.effectFilter;
public override ThingFilter ParentFilter => null;
}

public record PenAnimalsWrapper(CompAnimalPenMarker PenMarker) : ThingFilterContext
{
public override ThingFilter Filter => PenMarker.AnimalFilter;
Expand Down
35 changes: 31 additions & 4 deletions Source/Client/Syncing/Game/ThingFilterMarkers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public static ThingFilterContext DrawnThingFilter
}
}

#region ThingFilter Markers
[MpPrefix(typeof(ITab_Storage), "FillTab")]
static void TabStorageFillTab_Prefix(ITab_Storage __instance)
{
Expand Down Expand Up @@ -83,10 +82,38 @@ static void TabWindTurbineAutocutFillTab_Prefix(ITab_WindTurbineAutoCut __instan
[MpPostfix(typeof(ITab_WindTurbineAutoCut), nameof(ITab_WindTurbineAutoCut.FillTab))]
static void TabWindTurbineAutocutFillTab_Postfix(ITab_WindTurbineAutoCut __instance) => DrawnThingFilter = null;

[MpPrefix(typeof(ThingFilterUI), "DoThingFilterConfigWindow")]
[MpPrefix(typeof(ThingFilterUI), nameof(ThingFilterUI.DoThingFilterConfigWindow))]
static void ThingFilterUI_Prefix() => drawingThingFilter = true;

[MpPostfix(typeof(ThingFilterUI), "DoThingFilterConfigWindow")]
[MpPostfix(typeof(ThingFilterUI), nameof(ThingFilterUI.DoThingFilterConfigWindow))]
static void ThingFilterUI_Postfix() => drawingThingFilter = false;
#endregion

// Reading policies need special handling as they draw two ThingFilters
private static ReadingPolicy drawnReadingPolicy;

[MpPrefix(typeof(Dialog_ManageReadingPolicies), nameof(Dialog_ManageReadingPolicies.DoContentsRect))]
static void Dialog_ManageReadingPolicies_Prefix(Dialog_ManageReadingPolicies __instance) => drawnReadingPolicy = __instance.SelectedPolicy;

[MpPostfix(typeof(Dialog_ManageReadingPolicies), nameof(Dialog_ManageReadingPolicies.DoContentsRect))]
static void Dialog_ManageReadingPolicies_Postfix(Dialog_ManageReadingPolicies __instance) => drawnReadingPolicy = null;

[MpPrefix(typeof(ThingFilterUI), nameof(ThingFilterUI.DoThingFilterConfigWindow))]
static void ThingFilterUI_ReadingPolicy_Prefix(ThingFilter filter)
{
if (drawnReadingPolicy != null && drawnReadingPolicy.defFilter == filter)
DrawnThingFilter = new ReadingPolicyDefFilterWrapper(drawnReadingPolicy);

if (drawnReadingPolicy != null && drawnReadingPolicy.effectFilter == filter)
DrawnThingFilter = new ReadingPolicyEffectFilterWrapper(drawnReadingPolicy);
}

[MpPostfix(typeof(ThingFilterUI), nameof(ThingFilterUI.DoThingFilterConfigWindow))]
static void ThingFilterUI_ReadingPolicy_Postfix(ThingFilter filter)
{
if (drawnReadingPolicy != null && drawnReadingPolicy.defFilter == filter)
DrawnThingFilter = null;

if (drawnReadingPolicy != null && drawnReadingPolicy.effectFilter == filter)
DrawnThingFilter = null;
}
}
13 changes: 7 additions & 6 deletions Source/TestsOnMono/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ void Print()
// Test patching generic methods with Harmony
public static void Main(string[] args)
{
TestClassForPatches<SomeClass> test = new()
TestClassForPatches<SomeClassDerived> test = new()
{
field = new SomeClassDerived { a = 2 }
field = new SomeClassDerived { a = 2, b = 1 }
};

Console.WriteLine(test.GetField().GetA());
Console.WriteLine(test.GetField().b);

new Harmony("test").Patch(
typeof(TestClassForPatches<SomeClass>).GetMethod("GetField"),
postfix: new HarmonyMethod(typeof(Program).GetMethod("GenericPostfix"))
);

Console.WriteLine(test.GetField().GetA());
Console.WriteLine(test.GetField().b);
}

public static void GenericPostfix(ref object __result)
public static void GenericPostfix(ref SomeClass __result)
{
__result = new SomeClass2() { a = 3 };
__result = __result;
}

class TestClass<S>
Expand Down Expand Up @@ -92,6 +92,7 @@ public class SomeClass

public class SomeClassDerived : SomeClass
{
public int b;
}

public class SomeClass2
Expand Down

0 comments on commit 824d2f4

Please sign in to comment.