-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v2.3.2] Compatibility with TerminalFormatter's modified store, among…
… other things. - Added `TerminalFormatter` as a soft dependency. - _I think?_ - Sales for rotating items should now display with `TerminalFormatter` installed. - Added transpiler for `Store.GetNodeText()` to display discounted prices and amounts in the store page whenever an item is on sale. - Temporary fix until proper compatibility can be made. - `relativeScroll` now unpatches itself if `TerminalFormatter` is installed, since it already includes it. - Patching is done upon loading the main menu for the first time.
- Loading branch information
1 parent
7fed1f6
commit 8c1b01d
Showing
8 changed files
with
138 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
StoreRotationConfig/Compatibility/TerminalFormatterCompatibility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using HarmonyLib; | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
using System.Runtime.CompilerServices; | ||
using TerminalFormatter.Nodes; | ||
using static StoreRotationConfig.Patches.TerminalItemSalesPatches; | ||
|
||
namespace StoreRotationConfig.Compatibility | ||
{ | ||
/// <summary> | ||
/// Class handling compatibility with 'TerminalFormatter'. | ||
/// </summary> | ||
[HarmonyPatch] | ||
internal static class TerminalFormatterCompatibility | ||
{ | ||
/// <summary> | ||
/// Whether 'TerminalFormatter' is present in the BepInEx Chainloader or not. | ||
/// </summary> | ||
public static bool Enabled | ||
{ | ||
get | ||
{ | ||
_enabled ??= BepInEx.Bootstrap.Chainloader.PluginInfos.ContainsKey("TerminalFormatter"); | ||
|
||
return (bool)_enabled; | ||
} | ||
} | ||
private static bool? _enabled; | ||
|
||
/// <summary> | ||
/// Whether patches for 'TerminalFormatter' compatibility have already been applied or not. | ||
/// </summary> | ||
public static bool Patched { get; internal set; } = false; | ||
|
||
/// <summary> | ||
/// Replaces parameter with delegate call to display item discounts and their modified prices. | ||
/// </summary> | ||
/// ... (TerminalFormatter.Nodes.Store:286) | ||
/// consoleTable.AddRow(new object[] | ||
/// { | ||
/// text7, | ||
/// string.Format("${0}", | ||
/// // terminalNode2.itemCost), | ||
/// -> call(terminalNode2)), | ||
/// "" | ||
/// }); | ||
/// <param name="instructions">Iterator with original IL instructions.</param> | ||
/// <returns>Iterator with modified IL instructions.</returns> | ||
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] | ||
[HarmonyPatch(typeof(Store), nameof(Store.GetNodeText))] | ||
[HarmonyTranspiler] | ||
private static IEnumerable<CodeInstruction> GetNodeTextTranspiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
return new CodeMatcher(instructions).MatchForward(false, | ||
new CodeMatch(OpCodes.Ldstr, "[DECORATIONS]")) | ||
.MatchForward(false, | ||
new(OpCodes.Ldfld, AccessTools.Field(typeof(TerminalNode), nameof(TerminalNode.itemCost))), | ||
new(OpCodes.Box, operand: typeof(int))) | ||
.SetInstructionAndAdvance(Transpilers.EmitDelegate((TerminalNode item) => | ||
{ | ||
// Return full cost if 'salesChance' is disabled OR the 'RotationSales' dictionary doesn't contain a discount for the item about to be displayed. | ||
if (Plugin.Settings.SALE_CHANCE == 0 || (RotationSales != null && !RotationSales.ContainsKey(item))) | ||
{ | ||
return $"{item.itemCost}"; | ||
} | ||
|
||
Plugin.StaticLogger.LogDebug($"Appending {RotationSales[item]} to {item.creatureName}..."); | ||
|
||
// Obtain discounted item price. | ||
int discountedPrice = item.itemCost - (int)(item.itemCost * (RotationSales[item] / 100f)); | ||
|
||
// Return string containing the discounted price and discount amount to display in the store page. | ||
return $"{discountedPrice} ({RotationSales[item]}% OFF!)"; | ||
})) | ||
.SetOperandAndAdvance(typeof(string)) | ||
.InstructionEnumeration(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters