Decompiled source of OrderHelper v1.0.0

BepInEx/plugins/OldMarket.OrderHelper.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("OldMarket.SalesHistoryMod")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OldMarket.SalesHistoryMod")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("dd15dedd-4baa-4882-ae3d-09b9317ec1d3")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace OldMarket.SalesHistoryMod;

[BepInPlugin("oldmarket.orderhelper", "Old Market Order Helper", "1.0.0")]
public class SalesHistoryPlugin : BaseUnityPlugin
{
	public const string PluginGuid = "oldmarket.orderhelper";

	public const string PluginName = "Old Market Order Helper";

	public const string PluginVersion = "1.0.0";

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		new Harmony("oldmarket.orderhelper").PatchAll();
	}
}
internal static class SalesHistoryManager
{
	private static readonly Dictionary<long, int> TodaySoldItemCounts = new Dictionary<long, int>();

	public static void ResetForNewDay()
	{
		TodaySoldItemCounts.Clear();
	}

	public static void RegisterSale(long itemId, int itemCount = 1)
	{
		if (itemId != 0L && itemCount > 0)
		{
			if (TodaySoldItemCounts.TryGetValue(itemId, out var value))
			{
				TodaySoldItemCounts[itemId] = value + itemCount;
			}
			else
			{
				TodaySoldItemCounts[itemId] = itemCount;
			}
		}
	}

	public static int GetSoldItems(long itemId)
	{
		if (!TodaySoldItemCounts.TryGetValue(itemId, out var value))
		{
			return 0;
		}
		return value;
	}

	public static int GetSoldItems(int productId)
	{
		return GetSoldItems((long)productId);
	}
}
internal static class InventoryHelper
{
	private static readonly Dictionary<long, int> InventoryItemCounts = new Dictionary<long, int>();

	public static void RecalculateInventory()
	{
		InventoryItemCounts.Clear();
		Item[] array;
		try
		{
			array = Object.FindObjectsOfType<Item>();
		}
		catch (Exception ex)
		{
			ManualLogSource log = SalesHistoryPlugin.Log;
			if (log != null)
			{
				log.LogError((object)("[SalesHistory.Inventory] FindObjectsOfType<Item>() に失敗しました: " + ex));
			}
			return;
		}
		Item[] array2 = array;
		foreach (Item val in array2)
		{
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			ItemSO itemSO = val.itemSO;
			ProductSO val2 = (ProductSO)(object)((itemSO is ProductSO) ? itemSO : null);
			if ((Object)(object)val2 == (Object)null)
			{
				continue;
			}
			int value;
			try
			{
				value = val.amount.Value;
			}
			catch (Exception ex2)
			{
				ManualLogSource log2 = SalesHistoryPlugin.Log;
				if (log2 != null)
				{
					log2.LogError((object)("[SalesHistory.Inventory] item.amount.Value の取得に失敗しました: " + ex2));
				}
				continue;
			}
			if (value <= 0)
			{
				continue;
			}
			long id = ((ItemSO)val2).id;
			if (id != 0L)
			{
				if (InventoryItemCounts.TryGetValue(id, out var value2))
				{
					InventoryItemCounts[id] = value2 + value;
				}
				else
				{
					InventoryItemCounts[id] = value;
				}
			}
		}
	}

	public static int GetInventoryItemCount(long productId)
	{
		if (!InventoryItemCounts.TryGetValue(productId, out var value))
		{
			return 0;
		}
		return value;
	}

	public static int GetInventoryItemCount(int productId)
	{
		return GetInventoryItemCount((long)productId);
	}
}
internal static class BoxFormatter
{
	public static string FormatBoxQuantity(double boxCount)
	{
		if (boxCount <= 0.0001)
		{
			return "0";
		}
		double num = Math.Round(boxCount, 1);
		if (Math.Abs(num - Math.Round(num)) < 0.0001)
		{
			return ((int)Math.Round(num)).ToString();
		}
		return num.ToString("0.0");
	}
}
[HarmonyPatch(typeof(GameManager))]
internal static class GameManager_LogSale_Patch
{
	[HarmonyPostfix]
	[HarmonyPatch("LogSale")]
	private static void Postfix(long itemId, int cost, int sellingPrice)
	{
		SalesHistoryManager.RegisterSale(itemId);
	}
}
[HarmonyPatch(typeof(GameManager))]
internal static class GameManager_OnDayChanged_Patch
{
	[HarmonyPostfix]
	[HarmonyPatch("OnDayChanged")]
	private static void Postfix(GameManager __instance, int previous, int current)
	{
		SalesHistoryManager.ResetForNewDay();
	}
}
[HarmonyPatch(typeof(UIManager))]
internal static class UIManager_UpdateDockOrdersPanel_InventoryPatch
{
	[HarmonyPrefix]
	[HarmonyPatch("UpdateDockOrdersPanel")]
	private static void Prefix()
	{
		InventoryHelper.RecalculateInventory();
	}
}
[HarmonyPatch(typeof(DockOrderTile))]
internal static class DockOrderTile_SalesInfoPatch
{
	[HarmonyPostfix]
	[HarmonyPatch("SetData")]
	private static void Postfix(DockOrderTile __instance, ProductSO productSO, int amount)
	{
		if ((Object)(object)__instance == (Object)null || (Object)(object)productSO == (Object)null || (Object)(object)__instance.textSubtitle == (Object)null)
		{
			return;
		}
		long id = ((ItemSO)productSO).id;
		if (id == 0L)
		{
			return;
		}
		string text = ((TMP_Text)__instance.textSubtitle).text ?? string.Empty;
		string newValue = "<color=yellow>";
		GameManager instance = GameManager.Instance;
		if ((Object)(object)instance != (Object)null)
		{
			EventSO currentEvent = instance.GetCurrentEvent();
			if ((Object)(object)currentEvent != (Object)null && currentEvent.products != null && currentEvent.products.Contains(productSO))
			{
				float priceMultiplier = currentEvent.priceMultiplier;
				if (priceMultiplier > 1.001f)
				{
					newValue = "<color=#FF6666>";
				}
				else if (priceMultiplier < 0.999f)
				{
					newValue = "<color=#66AAFF>";
				}
			}
		}
		if (!string.IsNullOrEmpty(text) && text.Contains("<color=yellow>"))
		{
			text = text.Replace("<color=yellow>", newValue);
		}
		int num = ((ItemSO)productSO).amount;
		if (num <= 0)
		{
			num = 1;
		}
		int soldItems = SalesHistoryManager.GetSoldItems(id);
		int inventoryItemCount = InventoryHelper.GetInventoryItemCount(id);
		double boxCount = (double)soldItems / (double)num;
		double boxCount2 = (double)inventoryItemCount / (double)num;
		string arg = BoxFormatter.FormatBoxQuantity(boxCount);
		string arg2 = BoxFormatter.FormatBoxQuantity(boxCount2);
		if (soldItems <= 0 && inventoryItemCount <= 0)
		{
			((TMP_Text)__instance.textSubtitle).text = text;
			return;
		}
		string text2 = $"Sales: {arg}Box / Stock: {arg2}Box({inventoryItemCount}pcs)";
		string text3 = "<size=90%><color=#DDDDDD>" + text2 + "</color></size>";
		if (string.IsNullOrEmpty(text))
		{
			((TMP_Text)__instance.textSubtitle).text = text3;
		}
		else
		{
			((TMP_Text)__instance.textSubtitle).text = text + "\n" + text3;
		}
	}
}