Initial Commit
This commit is contained in:
111
Plugin.cs
Normal file
111
Plugin.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
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.0.0";
|
||||
public static new ManualLogSource Log;
|
||||
public static ConfigEntry<KeyCode> UpdateKey;
|
||||
private static List<DisplaySlot> m_CachedDisplaySlots = new List<DisplaySlot>(250);
|
||||
|
||||
public override void Load()
|
||||
{
|
||||
Log = base.Log;
|
||||
|
||||
Harmony harmony = new(PLUGIN_GUID);
|
||||
harmony.PatchAll();
|
||||
|
||||
UpdateKey = Config.Bind("Settings", "UpdateKey", KeyCode.F9);
|
||||
|
||||
SceneManager.sceneLoaded += (UnityAction<Scene, LoadSceneMode>) OnSceneLoaded;
|
||||
|
||||
Log.LogInfo($"Plugin {PLUGIN_GUID} is loaded!");
|
||||
}
|
||||
|
||||
public static void UpdatePrice()
|
||||
{
|
||||
List<Pricing> pricesSetByPlayer = Singleton<SaveManager>.Instance.Price.PricesSetByPlayer;
|
||||
foreach (Pricing pricing in pricesSetByPlayer)
|
||||
{
|
||||
pricing.Price = pricing.MarketPrice - 0.1f;
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user