Refactored code

master
Mohammad Shoaib Khan 2023-02-22 17:03:33 +05:30
parent 31f3144bfd
commit cf0e46ecdd
No known key found for this signature in database
GPG Key ID: D8040D966320B620
2 changed files with 64 additions and 31 deletions

View File

@ -48,7 +48,7 @@ namespace stardew_access
harmony.Patch( harmony.Patch(
original: AccessTools.Method(typeof(CharacterCustomization), nameof(CharacterCustomization.draw), new Type[] { typeof(SpriteBatch) }), original: AccessTools.Method(typeof(CharacterCustomization), nameof(CharacterCustomization.draw), new Type[] { typeof(SpriteBatch) }),
postfix: new HarmonyMethod(typeof(CharacterCustomizationPatches), nameof(CharacterCustomizationPatches.CharacterCustomizationMenuPatch)) postfix: new HarmonyMethod(typeof(CharacterCustomizationMenuPatch), nameof(CharacterCustomizationMenuPatch.DrawPatch))
); );
harmony.Patch( harmony.Patch(

View File

@ -1,8 +1,10 @@
using StardewValley; using StardewValley;
using StardewValley.Menus; using StardewValley.Menus;
namespace stardew_access.Patches { namespace stardew_access.Patches
internal class CharacterCustomizationPatches { {
internal class CharacterCustomizationMenuPatch
{
private static bool isRunning = false; private static bool isRunning = false;
private static int saveGameIndex = -1; private static int saveGameIndex = -1;
public static string characterCreationMenuQueryKey = " "; public static string characterCreationMenuQueryKey = " ";
@ -28,7 +30,7 @@ namespace stardew_access.Patches {
public static bool characterDesignToggleShouldSpeak = true; public static bool characterDesignToggleShouldSpeak = true;
public static ClickableComponent? currentComponent = null; public static ClickableComponent? currentComponent = null;
internal static void CharacterCustomizationMenuPatch(CharacterCustomization __instance, bool ___skipIntro, internal static void DrawPatch(CharacterCustomization __instance, bool ___skipIntro,
ClickableComponent ___startingCabinsLabel, ClickableComponent ___difficultyModifierLabel, TextBox ___nameBox, ClickableComponent ___startingCabinsLabel, ClickableComponent ___difficultyModifierLabel, TextBox ___nameBox,
TextBox ___farmnameBox, TextBox ___favThingBox) TextBox ___farmnameBox, TextBox ___favThingBox)
{ {
@ -49,7 +51,7 @@ namespace stardew_access.Patches {
if (MainClass.Config.CharacterCreationMenuNextKey.JustPressed() && !isRunning) if (MainClass.Config.CharacterCreationMenuNextKey.JustPressed() && !isRunning)
{ {
isRunning = true; isRunning = true;
itemsToSpeak =CycleThroughItems(true, __instance, ___skipIntro, ___startingCabinsLabel, ___difficultyModifierLabel, ___nameBox, ___farmnameBox, ___favThingBox); itemsToSpeak = CycleThroughItems(true, __instance, ___skipIntro, ___startingCabinsLabel, ___difficultyModifierLabel, ___nameBox, ___farmnameBox, ___favThingBox);
if (itemsToSpeak != "") if (itemsToSpeak != "")
toSpeak = $"{itemsToSpeak} \n {toSpeak}"; toSpeak = $"{itemsToSpeak} \n {toSpeak}";
Task.Delay(200).ContinueWith(_ => { isRunning = false; }); Task.Delay(200).ContinueWith(_ => { isRunning = false; });
@ -97,7 +99,9 @@ namespace stardew_access.Patches {
if (characterDesignToggle) if (characterDesignToggle)
{ {
displayState = "shown"; displayState = "shown";
} else { }
else
{
displayState = "hidden"; displayState = "hidden";
} }
toSpeak = $"Character design controls {displayState}. \n {toSpeak}"; toSpeak = $"Character design controls {displayState}. \n {toSpeak}";
@ -185,7 +189,9 @@ namespace stardew_access.Patches {
prevEyeColorHue = currentEyeColorHue; prevEyeColorHue = currentEyeColorHue;
if (currentEyeColorHue != "") if (currentEyeColorHue != "")
toSpeak = $"{toSpeak} \n Hue: {currentEyeColorHue}"; toSpeak = $"{toSpeak} \n Hue: {currentEyeColorHue}";
} else { }
else
{
prevEyeColorHue = ""; prevEyeColorHue = "";
} }
} }
@ -197,7 +203,9 @@ namespace stardew_access.Patches {
prevEyeColorSaturation = currentEyeColorSaturation; prevEyeColorSaturation = currentEyeColorSaturation;
if (currentEyeColorSaturation != "") if (currentEyeColorSaturation != "")
toSpeak = $"{toSpeak} \n Saturation: {currentEyeColorSaturation}"; toSpeak = $"{toSpeak} \n Saturation: {currentEyeColorSaturation}";
} else { }
else
{
prevEyeColorSaturation = ""; prevEyeColorSaturation = "";
} }
} }
@ -209,7 +217,9 @@ namespace stardew_access.Patches {
prevEyeColorValue = currentEyeColorValue; prevEyeColorValue = currentEyeColorValue;
if (currentEyeColorValue != "") if (currentEyeColorValue != "")
toSpeak = $"{toSpeak} \n Value: {currentEyeColorValue}"; toSpeak = $"{toSpeak} \n Value: {currentEyeColorValue}";
} else { }
else
{
prevEyeColorValue = ""; prevEyeColorValue = "";
} }
} }
@ -231,7 +241,9 @@ namespace stardew_access.Patches {
prevHairColorHue = currentHairColorHue; prevHairColorHue = currentHairColorHue;
if (currentHairColorHue != "") if (currentHairColorHue != "")
toSpeak = $"{toSpeak} \n Hue: {currentHairColorHue}"; toSpeak = $"{toSpeak} \n Hue: {currentHairColorHue}";
} else { }
else
{
prevHairColorHue = ""; prevHairColorHue = "";
} }
} }
@ -243,7 +255,9 @@ namespace stardew_access.Patches {
prevHairColorSaturation = currentHairColorSaturation; prevHairColorSaturation = currentHairColorSaturation;
if (currentHairColorSaturation != "") if (currentHairColorSaturation != "")
toSpeak = $"{toSpeak} \n Saturation: {currentHairColorSaturation}"; toSpeak = $"{toSpeak} \n Saturation: {currentHairColorSaturation}";
} else { }
else
{
prevHairColorSaturation = ""; prevHairColorSaturation = "";
} }
} }
@ -255,7 +269,9 @@ namespace stardew_access.Patches {
prevHairColorValue = currentHairColorValue; prevHairColorValue = currentHairColorValue;
if (currentHairColorValue != "") if (currentHairColorValue != "")
toSpeak = $"{toSpeak} \n Value: {currentHairColorValue}"; toSpeak = $"{toSpeak} \n Value: {currentHairColorValue}";
} else { }
else
{
prevHairColorValue = ""; prevHairColorValue = "";
} }
} }
@ -277,7 +293,9 @@ namespace stardew_access.Patches {
prevPantsColorHue = currentPantsColorHue; prevPantsColorHue = currentPantsColorHue;
if (currentPantsColorHue != "") if (currentPantsColorHue != "")
toSpeak = $"{toSpeak} \n Hue: {currentPantsColorHue}"; toSpeak = $"{toSpeak} \n Hue: {currentPantsColorHue}";
} else { }
else
{
prevPantsColorHue = ""; prevPantsColorHue = "";
} }
} }
@ -289,7 +307,9 @@ namespace stardew_access.Patches {
prevPantsColorSaturation = currentPantsColorSaturation; prevPantsColorSaturation = currentPantsColorSaturation;
if (currentPantsColorSaturation != "") if (currentPantsColorSaturation != "")
toSpeak = $"{toSpeak} \n Saturation: {currentPantsColorSaturation}"; toSpeak = $"{toSpeak} \n Saturation: {currentPantsColorSaturation}";
} else { }
else
{
prevPantsColorSaturation = ""; prevPantsColorSaturation = "";
} }
} }
@ -301,7 +321,9 @@ namespace stardew_access.Patches {
prevPantsColorValue = currentPantsColorValue; prevPantsColorValue = currentPantsColorValue;
if (currentPantsColorValue != "") if (currentPantsColorValue != "")
toSpeak = $"{toSpeak} \n Value: {currentPantsColorValue}"; toSpeak = $"{toSpeak} \n Value: {currentPantsColorValue}";
} else { }
else
{
prevPantsColorValue = ""; prevPantsColorValue = "";
} }
} }
@ -320,13 +342,12 @@ namespace stardew_access.Patches {
if (prevPetName != currentPetName) if (prevPetName != currentPetName)
{ {
prevPetName = currentPetName; prevPetName = currentPetName;
if (currentPetName != "") if (currentPetName != "")
toSpeak = $"{toSpeak} \n Current Pet: {currentPetName}"; toSpeak = $"{toSpeak} \n Current Pet: {currentPetName}";
} }
return toSpeak.Trim(); return toSpeak.Trim();
} }
private static string CycleThroughItems(bool increase, CharacterCustomization __instance, bool ___skipIntro, private static string CycleThroughItems(bool increase, CharacterCustomization __instance, bool ___skipIntro,
ClickableComponent ___startingCabinsLabel, ClickableComponent ___difficultyModifierLabel, TextBox ___nameBox, ClickableComponent ___startingCabinsLabel, ClickableComponent ___difficultyModifierLabel, TextBox ___nameBox,
TextBox ___farmnameBox, TextBox ___favThingBox) TextBox ___farmnameBox, TextBox ___favThingBox)
@ -344,7 +365,9 @@ namespace stardew_access.Patches {
if (___nameBox.Text != "") if (___nameBox.Text != "")
{ {
postText = $": {___nameBox.Text}"; postText = $": {___nameBox.Text}";
} else { }
else
{
postText = " Text Box"; postText = " Text Box";
} }
buttons.Add(__instance.nameBoxCC, $"Farmer's Name{postText}"); buttons.Add(__instance.nameBoxCC, $"Farmer's Name{postText}");
@ -355,7 +378,9 @@ namespace stardew_access.Patches {
if (___farmnameBox.Text != "") if (___farmnameBox.Text != "")
{ {
postText = $": {___farmnameBox.Text}"; postText = $": {___farmnameBox.Text}";
} else { }
else
{
postText = " Text Box"; postText = " Text Box";
} }
buttons.Add(__instance.farmnameBoxCC, $"Farm's Name{postText}"); buttons.Add(__instance.farmnameBoxCC, $"Farm's Name{postText}");
@ -366,7 +391,9 @@ namespace stardew_access.Patches {
if (___favThingBox.Text != "") if (___favThingBox.Text != "")
{ {
postText = $": {___favThingBox.Text}"; postText = $": {___favThingBox.Text}";
} else { }
else
{
postText = " Text Box"; postText = " Text Box";
} }
buttons.Add(__instance.favThingBoxCC, $"Favourite Thing{postText}"); buttons.Add(__instance.favThingBoxCC, $"Favourite Thing{postText}");
@ -386,10 +413,10 @@ namespace stardew_access.Patches {
// Controls to rotate the farmer (Potentially useful for low vision players) are first if they're available. // Controls to rotate the farmer (Potentially useful for low vision players) are first if they're available.
// They also appear above the gender buttons, so we handle them separately here. // They also appear above the gender buttons, so we handle them separately here.
if (characterDesignToggle && new[] {__instance.leftSelectionButtons.Count, __instance.rightSelectionButtons.Count }.All(c => c >= 0)) // both have Count > 0 if (characterDesignToggle && new[] { __instance.leftSelectionButtons.Count, __instance.rightSelectionButtons.Count }.All(c => c >= 0)) // both have Count > 0
{ {
if (new[] {__instance.leftSelectionButtons[DesignControlsIndex].visible, __instance.rightSelectionButtons[DesignControlsIndex].visible }.All(v => v == true) // both visible if (new[] { __instance.leftSelectionButtons[DesignControlsIndex].visible, __instance.rightSelectionButtons[DesignControlsIndex].visible }.All(v => v == true) // both visible
&& new[] {__instance.leftSelectionButtons[DesignControlsIndex].name, __instance.rightSelectionButtons[DesignControlsIndex].name }.All(n => n == "Direction")) // both named "Direction" && new[] { __instance.leftSelectionButtons[DesignControlsIndex].name, __instance.rightSelectionButtons[DesignControlsIndex].name }.All(n => n == "Direction")) // both named "Direction"
{ {
buttons.Add(__instance.leftSelectionButtons[DesignControlsIndex], "Rotate Left Button"); buttons.Add(__instance.leftSelectionButtons[DesignControlsIndex], "Rotate Left Button");
buttons.Add(__instance.rightSelectionButtons[DesignControlsIndex], "Rotate Right Button"); buttons.Add(__instance.rightSelectionButtons[DesignControlsIndex], "Rotate Right Button");
@ -403,9 +430,9 @@ namespace stardew_access.Patches {
buttons.Add(__instance.genderButtons[1], ((!Game1.player.IsMale) ? "Selected " : "") + "Gender: Female Button"); buttons.Add(__instance.genderButtons[1], ((!Game1.player.IsMale) ? "Selected " : "") + "Gender: Female Button");
} }
if (characterDesignToggle&& new[] {__instance.leftSelectionButtons.Count, __instance.rightSelectionButtons.Count }.All(c => c >= DesignControlsIndex) && new[] {__instance.leftSelectionButtons[DesignControlsIndex].visible, __instance.rightSelectionButtons[DesignControlsIndex].visible }.All(v => v == true)) if (characterDesignToggle && new[] { __instance.leftSelectionButtons.Count, __instance.rightSelectionButtons.Count }.All(c => c >= DesignControlsIndex) && new[] { __instance.leftSelectionButtons[DesignControlsIndex].visible, __instance.rightSelectionButtons[DesignControlsIndex].visible }.All(v => v == true))
{ {
while(DesignControlsIndex < __instance.leftSelectionButtons.Count) while (DesignControlsIndex < __instance.leftSelectionButtons.Count)
{ {
ClickableComponent left = __instance.leftSelectionButtons[DesignControlsIndex]; ClickableComponent left = __instance.leftSelectionButtons[DesignControlsIndex];
ClickableComponent right = __instance.rightSelectionButtons[DesignControlsIndex]; ClickableComponent right = __instance.rightSelectionButtons[DesignControlsIndex];
@ -630,16 +657,18 @@ namespace stardew_access.Patches {
break; break;
} }
return sb; return sb;
} else { }
else
{
return null; return null;
} }
} }
private static void AdjustCurrentSlider(bool increase, CharacterCustomization __instance, int amount=1) private static void AdjustCurrentSlider(bool increase, CharacterCustomization __instance, int amount = 1)
{ {
if (currentComponent != null && currentComponent.myID >= 522 && currentComponent.myID <= 530) if (currentComponent != null && currentComponent.myID >= 522 && currentComponent.myID <= 530)
{ {
SliderBar sb = getCurrentSliderBar(currentComponent.myID, __instance) !; SliderBar sb = getCurrentSliderBar(currentComponent.myID, __instance)!;
if (sb != null) if (sb != null)
{ {
double step = ((double)sb.bounds.Width / 100d); // size of 1% change in slider value double step = ((double)sb.bounds.Width / 100d); // size of 1% change in slider value
@ -650,7 +679,9 @@ namespace stardew_access.Patches {
{ {
value = Math.Min(value + amount, 99d); value = Math.Min(value + amount, 99d);
x = Math.Min(Math.Ceiling((value * step)), (double)sb.bounds.Width); x = Math.Min(Math.Ceiling((value * step)), (double)sb.bounds.Width);
} else { }
else
{
value = Math.Max(value - amount, 0d); value = Math.Max(value - amount, 0d);
x = Math.Max(Math.Ceiling((value * step)), 0d); x = Math.Max(Math.Ceiling((value * step)), 0d);
} }
@ -796,7 +827,9 @@ namespace stardew_access.Patches {
if (currentComponent != null && currentComponent.name == "Pet") if (currentComponent != null && currentComponent.name == "Pet")
{ {
return ((Game1.player.catPerson) ? "Cat" : "Dog") + " Breed: " + Game1.player.whichPetBreed; return ((Game1.player.catPerson) ? "Cat" : "Dog") + " Breed: " + Game1.player.whichPetBreed;
} else { }
else
{
return ""; return "";
} }
} }