116 lines
4.2 KiB
C#
116 lines
4.2 KiB
C#
using BepInEx;
|
|
using BepInEx.Configuration;
|
|
using BepInEx.Logging;
|
|
using BepInEx.Unity.IL2CPP;
|
|
using HarmonyLib;
|
|
using Il2CppInterop.Runtime.Injection;
|
|
using Il2CppSystem.Collections.Generic;
|
|
using MyBox;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.SceneManagement;
|
|
using KeyCode = BepInEx.Unity.IL2CPP.UnityEngine.KeyCode;
|
|
|
|
namespace AutoPriceUpdater;
|
|
|
|
[BepInPlugin(PLUGIN_GUID, PLUGIN_NAME, PLUGIN_VERSION)]
|
|
public class Plugin : BasePlugin
|
|
{
|
|
public const string PLUGIN_GUID = "de.mpburgmann.AutoPriceUpdater";
|
|
public const string PLUGIN_NAME = "Auto Price Updater";
|
|
public const string PLUGIN_VERSION = "1.1.0";
|
|
public static new ManualLogSource Log;
|
|
private static ConfigEntry<bool> EnableAutoUpdate;
|
|
private static ConfigEntry<float> AmountToAdd;
|
|
public static ConfigEntry<KeyCode> UpdateKey;
|
|
private static List<DisplaySlot> m_CachedDisplaySlots = new List<DisplaySlot>(250);
|
|
|
|
public override void Load()
|
|
{
|
|
Log = base.Log;
|
|
Log.LogInfo($"Loading {PLUGIN_NAME} v{PLUGIN_VERSION}");
|
|
|
|
Harmony harmony = new(PLUGIN_GUID);
|
|
harmony.PatchAll();
|
|
|
|
EnableAutoUpdate = Config.Bind("Settings", "Enabled", true, "If enabled, prices will be automatically updated at the start of each day.");
|
|
UpdateKey = Config.Bind("Settings", "UpdateKey", KeyCode.F9, "Key to manually update prices. Not working when Enabled is false.");
|
|
AmountToAdd = Config.Bind("Price Settings", "Amount To Add", 0.1f, "Amount to add to market price. Can be negative.");
|
|
|
|
SceneManager.sceneLoaded += (UnityAction<Scene, LoadSceneMode>) OnSceneLoaded;
|
|
Log.LogInfo($"{PLUGIN_NAME} loaded successfully");
|
|
}
|
|
|
|
public static void UpdatePrice()
|
|
{
|
|
if(!EnableAutoUpdate.Value) return;
|
|
List<Pricing> pricesSetByPlayer = Singleton<SaveManager>.Instance.Price.PricesSetByPlayer;
|
|
foreach (Pricing pricing in pricesSetByPlayer)
|
|
{
|
|
pricing.Price = pricing.MarketPrice + AmountToAdd.Value;
|
|
Log.LogInfo($"Updated price for product ID {pricing.ProductID} to {pricing.Price}");
|
|
if(Singleton<DisplayManager>.Instance.GetDisplaySlots(pricing.ProductID, false, m_CachedDisplaySlots) > 0)
|
|
{
|
|
foreach (DisplaySlot slot in m_CachedDisplaySlots)
|
|
{
|
|
slot.PricingChanged(pricing.ProductID);
|
|
}
|
|
}
|
|
}
|
|
Singleton<SFXManager>.Instance.PlayCheckoutWarningSFX(Singleton<PlayerInstance>.Instance.transform.position);
|
|
}
|
|
|
|
public static void SetMissingPrices()
|
|
{
|
|
List<ProductSO> products = Singleton<IDManager>.Instance.Products;
|
|
List<Pricing> pricesSetByPlayer = Singleton<SaveManager>.Instance.Price.PricesSetByPlayer;
|
|
foreach (ProductSO product in products)
|
|
{
|
|
bool priceExists = false;
|
|
foreach (Pricing pricing in pricesSetByPlayer)
|
|
{
|
|
if (pricing.ProductID == product.ID)
|
|
{
|
|
priceExists = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!priceExists)
|
|
{
|
|
pricesSetByPlayer.Add(new Pricing(product.ID, product.BasePrice));
|
|
Log.LogInfo($"Set missing price for product ID {product.ID} to base price {product.BasePrice}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
if (scene.name == "Main Scene")
|
|
{
|
|
ClassInjector.RegisterTypeInIl2Cpp<Behaviour>();
|
|
GameObject gameObject = new("AutoPriceUpdaterBehaviour");
|
|
Object.DontDestroyOnLoad(gameObject);
|
|
gameObject.AddComponent<Behaviour>();
|
|
}
|
|
|
|
if(scene.name == "Main Menu")
|
|
{
|
|
Object gameObject = GameObject.Find("AutoPriceUpdaterBehaviour");
|
|
if(gameObject != null)
|
|
{
|
|
Object.Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(DayCycleManager), "StartNextDay")]
|
|
private static class DayCycleManager_StartNextDay_Patch
|
|
{
|
|
[HarmonyPostfix]
|
|
private static void Postfix()
|
|
{
|
|
Log.LogInfo("StartNextDay triggered");
|
|
UpdatePrice();
|
|
}
|
|
}
|
|
} |