Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PlannedParenthood] Remove Harmony Patchall #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 91 additions & 104 deletions PlannedParenthood/CodePatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,136 +54,123 @@ private static List<string> GetSpouseNames()
return names;
}

[HarmonyPatch(typeof(GameLocation), nameof(GameLocation.answerDialogueAction))]
public class GameLocation_answerDialogueAction_Patch
public static bool GameLocation_answerDialogueAction_Prefix(GameLocation __instance, ref string questionAndAnswer, ref bool __result)
{
public static bool Prefix(GameLocation __instance, ref string questionAndAnswer, ref bool __result)
if (!Config.ModEnabled)
return true;
partnerName = null;
if (questionAndAnswer == "Sleep_Baby")
{
if (!Config.ModEnabled)
return true;
partnerName = null;
if (questionAndAnswer == "Sleep_Baby")
{
SMonitor.Log("Starting baby dialogue");
SMonitor.Log("Starting baby dialogue");

CreateNameListQuestion(__instance);
CreateNameListQuestion(__instance);

__result = true;
return false;
__result = true;
return false;
}
else if (questionAndAnswer.StartsWith("SleepBaby_"))
{
string name = questionAndAnswer.Substring("SleepBaby_".Length);
if (name == "prev_page")
{
namePage--;
CreateNameListQuestion(__instance);
}
else if (questionAndAnswer.StartsWith("SleepBaby_"))
else if (name == "next_page")
{
string name = questionAndAnswer.Substring("SleepBaby_".Length);
if (name == "prev_page")
{
namePage--;
CreateNameListQuestion(__instance);
}
else if (name == "next_page")
{
namePage++;
CreateNameListQuestion(__instance);
}
else
{
partnerName = name;
SMonitor.Log($"Trying to make baby with {partnerName}");
questionAndAnswer = "Sleep_Yes";
return true;
}
__result = true;
return false;
namePage++;
CreateNameListQuestion(__instance);
}
return true;
else
{
partnerName = name;
SMonitor.Log($"Trying to make baby with {partnerName}");
questionAndAnswer = "Sleep_Yes";
return true;
}
__result = true;
return false;
}
private static void CreateNameListQuestion(GameLocation __instance)
{
var names = GetSpouseNames();
return true;
}
private static void CreateNameListQuestion(GameLocation __instance)
{
var names = GetSpouseNames();

int totalNames = names.Count;
int totalNames = names.Count;

names = names.Skip(namePage * Config.NamesPerPage).Take(Config.NamesPerPage).ToList();
names = names.Skip(namePage * Config.NamesPerPage).Take(Config.NamesPerPage).ToList();

List<Response> responses = new List<Response>();
List<Response> responses = new List<Response>();

if (namePage > 0)
responses.Add(new Response("prev_page", "..."));
foreach (var name in names)
{
responses.Add(new Response(name, name));
}
if (Config.NamesPerPage * (namePage + 1) < totalNames)
responses.Add(new Response("next_page", "..."));

__instance.createQuestionDialogue(SHelper.Translation.Get("which-npc"), responses.ToArray(), "SleepBaby");
if (namePage > 0)
responses.Add(new Response("prev_page", "..."));
foreach (var name in names)
{
responses.Add(new Response(name, name));
}
}
if (Config.NamesPerPage * (namePage + 1) < totalNames)
responses.Add(new Response("next_page", "..."));

[HarmonyPatch(typeof(GameLocation), nameof(GameLocation.createQuestionDialogue), new Type[] { typeof(string), typeof(Response[]), typeof(string), typeof(Object) })]
public class GameLocation_createQuestionDialogue_Patch
__instance.createQuestionDialogue(SHelper.Translation.Get("which-npc"), responses.ToArray(), "SleepBaby");
}

public static void GameLocation_createQuestionDialogue_Prefix(GameLocation __instance, ref string question, ref Response[] answerChoices, string dialogKey)
{
public static void Prefix(GameLocation __instance, ref string question, ref Response[] answerChoices, string dialogKey)
{
if (!Config.ModEnabled || dialogKey != "Sleep")
return;
SMonitor.Log($"Showing sleep confirmation");
if (!Config.ModEnabled || dialogKey != "Sleep")
return;
SMonitor.Log($"Showing sleep confirmation");

var fh = Utility.getHomeOfFarmer(Game1.player);
var fh = Utility.getHomeOfFarmer(Game1.player);

if (fh.upgradeLevel < 2)
{
SMonitor.Log($"Cannot ask about pregnancy; farmhouse level {fh.upgradeLevel}", LogLevel.Debug);
return;
}
if (fh.cribStyle.Value <= 0)
{
SMonitor.Log($"Cannot ask about pregnancy; no crib", LogLevel.Debug);
return;
}

partnerName = null;
if (fh.upgradeLevel < 2)
{
SMonitor.Log($"Cannot ask about pregnancy; farmhouse level {fh.upgradeLevel}", LogLevel.Debug);
return;
}
if (fh.cribStyle.Value <= 0)
{
SMonitor.Log($"Cannot ask about pregnancy; no crib", LogLevel.Debug);
return;
}

var names = GetSpouseNames();
if (!names.Any())
{
SMonitor.Log($"Cannot ask about pregnancy; no applicable spouses", LogLevel.Debug);
return;
}
partnerName = null;

var list = answerChoices.ToList();
list.Add(new Response("Baby", SHelper.Translation.Get("make-baby")));
answerChoices = list.ToArray();
var names = GetSpouseNames();
if (!names.Any())
{
SMonitor.Log($"Cannot ask about pregnancy; no applicable spouses", LogLevel.Debug);
return;
}

var list = answerChoices.ToList();
list.Add(new Response("Baby", SHelper.Translation.Get("make-baby")));
answerChoices = list.ToArray();
}
[HarmonyPatch(typeof(Utility), nameof(Utility.pickPersonalFarmEvent))]
public class Utility_pickPersonalFarmEvent_Patch
{

public static bool Prefix(ref FarmEvent __result)
public static bool Utility_pickPersonalFarmEvent_Prefix(ref FarmEvent __result)
{
if (!Config.ModEnabled || partnerName is null)
return true;
if (Game1.weddingToday)
{
if (!Config.ModEnabled || partnerName is null)
return true;
if (Game1.weddingToday)
{
__result = null;
return false;
}
if (freeLoveAPI is not null)
freeLoveAPI.SetLastPregnantSpouse(partnerName);

if (Game1.player.friendshipData.ContainsKey(partnerName))
{
SMonitor.Log($"creating NPC pregnancy event with {partnerName}");
__result = new QuestionEvent(1);
}
else if(Game1.getAllFarmers().ToList().Exists(f => f.Name == partnerName))
{
SMonitor.Log($"creating PC pregnancy event with {partnerName}");
__result = new QuestionEvent(3);
}
__result = null;
return false;
}
if (freeLoveAPI is not null)
freeLoveAPI.SetLastPregnantSpouse(partnerName);

if (Game1.player.friendshipData.ContainsKey(partnerName))
{
SMonitor.Log($"creating NPC pregnancy event with {partnerName}");
__result = new QuestionEvent(1);
}
else if(Game1.getAllFarmers().ToList().Exists(f => f.Name == partnerName))
{
SMonitor.Log($"creating PC pregnancy event with {partnerName}");
__result = new QuestionEvent(3);
}
return false;
}
}
}
21 changes: 18 additions & 3 deletions PlannedParenthood/ModEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using HarmonyLib;
using StardewModdingAPI;
using StardewValley;
using System;

namespace PlannedParenthood
{
Expand Down Expand Up @@ -30,11 +32,24 @@ public override void Entry(IModHelper helper)
SMonitor = Monitor;
SHelper = helper;

helper.Events.GameLoop.GameLaunched += GameLoop_GameLaunched;

var harmony = new Harmony(ModManifest.UniqueID);
harmony.PatchAll();

harmony.Patch(
original: AccessTools.Method(typeof(GameLocation), nameof(GameLocation.answerDialogueAction)),
prefix: new HarmonyMethod(typeof(ModEntry), nameof(GameLocation_answerDialogueAction_Prefix))
);

harmony.Patch(
original: AccessTools.Method(typeof(GameLocation), nameof(GameLocation.createQuestionDialogue), new Type[] { typeof(string), typeof(Response[]), typeof(string), typeof(StardewValley.Object) }),
prefix: new HarmonyMethod(typeof(ModEntry), nameof(GameLocation_createQuestionDialogue_Prefix))
);

harmony.Patch(
original: AccessTools.Method(typeof(Utility), nameof(Utility.pickPersonalFarmEvent)),
prefix: new HarmonyMethod(typeof(ModEntry), nameof(Utility_pickPersonalFarmEvent_Prefix))
);

helper.Events.GameLoop.GameLaunched += GameLoop_GameLaunched;
}
public override object GetApi()
{
Expand Down
2 changes: 1 addition & 1 deletion PlannedParenthood/PlannedParenthood.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.0.0</Version>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<EnableHarmony>true</EnableHarmony>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions PlannedParenthood/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"Name": "Planned Parenthood",
"Author": "aedenthorn",
"Version": "0.2.5",
"Version": "0.2.6",
"Description": "Planned Parenthood.",
"UniqueID": "aedenthorn.PlannedParenthood",
"EntryDll": "PlannedParenthood.dll",
"MinimumApiVersion": "3.8.0",
"MinimumApiVersion": "4.0.0",
"ModUpdater": {
"Repository": "StardewValleyMods",
"User": "aedenthorn",
Expand Down