Decompiled source of CustomPrice v1.0.2

CustomPrice.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CSync.Extensions;
using CSync.Lib;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("testCSync")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("testCSync")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("c3bcafbe-e9fd-4f0a-a184-996ed5269171")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
}
namespace CustomPrice
{
	public class CustomConfig : SyncedConfig2<CustomConfig>
	{
		private ManualLogSource logger;

		private ConfigFile cfg;

		private ConfigEntry<bool> Debug { get; set; }

		[field: SyncedEntryField]
		public SyncedEntry<int> StartCredit { get; private set; }

		[field: SyncedEntryField]
		public SyncedEntry<string> ItemsShopPrice { get; private set; }

		[field: SyncedEntryField]
		public SyncedEntry<string> VehiclesPrice { get; private set; }

		public CustomConfig(ConfigFile cfg, ManualLogSource logger)
			: base("luffyvanquish.CustomPrice")
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			//IL_0054: Expected O, but got Unknown
			this.logger = logger;
			this.cfg = cfg;
			Debug = cfg.Bind<bool>(new ConfigDefinition("Debug", "ShowDebug"), false, new ConfigDescription("Show debuging information in the console.", (AcceptableValueBase)null, Array.Empty<object>()));
			StartCredit = SyncedBindingExtensions.BindSyncedEntry<int>(cfg, "Start Credit", "StartCredit", 60, "Starting money.");
			ItemsShopPrice = SyncedBindingExtensions.BindSyncedEntry<string>(cfg, "Items", "ItemsPrice", "", "Price ofr each item");
			VehiclesPrice = SyncedBindingExtensions.BindSyncedEntry<string>(cfg, "Vehicles", "VehiclesPrice", "", "Price of each Vehicle");
			StartCredit.Changed += delegate(object sender, SyncedSettingChangedEventArgs<int> args)
			{
				logger.LogInfo((object)"StartCredit value : ");
				logger.LogInfo((object)$"The old value was {args.OldValue}");
				logger.LogInfo((object)$"The new value is {args.NewValue}");
			};
			ConfigManager.Register<CustomConfig>((SyncedConfig2<CustomConfig>)this);
		}

		public void ShowConsole(string text)
		{
			if (Debug.Value)
			{
				logger.LogMessage((object)text);
			}
		}

		public void ShowConsoleErr(string text)
		{
			if (Debug.Value)
			{
				logger.LogError((object)text);
			}
		}

		public void AddShopItems(SyncedEntry<string> entryCfg, string itemName, string itemPrice)
		{
			string localValue = itemName + ":" + itemPrice;
			string localValue2 = entryCfg.LocalValue;
			if (Utility.IsNullOrWhiteSpace(localValue2))
			{
				entryCfg.LocalValue = localValue;
			}
			else if (!localValue2.Contains(itemName + ":"))
			{
				Dictionary<string, int> dictionary = StringToDict(localValue2);
				dictionary.Add(itemName, int.Parse(itemPrice));
				entryCfg.LocalValue = DictToString(dictionary);
			}
		}

		public int GetShopItemPrice(SyncedEntry<string> entryCfg, string itemName)
		{
			Dictionary<string, int> dictionary = StringToDict(entryCfg.Value);
			return dictionary[itemName];
		}

		private Dictionary<string, int> StringToDict(string str)
		{
			Dictionary<string, int> dictionary = new Dictionary<string, int>();
			string[] array = str.Trim().Split(new char[1] { ',' });
			foreach (string text in array)
			{
				string[] array2 = text.Split(new char[1] { ':' });
				dictionary.Add(array2[0], int.Parse(array2[1]));
			}
			return dictionary;
		}

		private string DictToString<T, K>(Dictionary<K, T> dict)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (K key in dict.Keys)
			{
				K val = key;
				stringBuilder.Append(val?.ToString() + ":" + dict[key]?.ToString() + ",");
			}
			stringBuilder.Length--;
			return stringBuilder.ToString();
		}
	}
	internal class CustomPricePatch
	{
		private static readonly CustomConfig config = Plugin.Config;

		private static Item[] buyableItemsList;

		private static BuyableVehicle[] buyableVehicles;

		private static readonly SyncedEntry<string> itemCfg = config.ItemsShopPrice;

		private static readonly SyncedEntry<string> vehicleCfg = config.VehiclesPrice;

		[HarmonyPatch(typeof(Terminal), "SetItemSales")]
		[HarmonyPostfix]
		private static void ChangePrice(ref Item[] ___buyableItemsList, ref BuyableVehicle[] ___buyableVehicles)
		{
			try
			{
				if (___buyableItemsList != null)
				{
					config.ShowConsole("Loading the new price of " + ___buyableItemsList.Length + " shop items.");
					Item[] array = ___buyableItemsList;
					foreach (Item val in array)
					{
						string text = val.itemName.Trim();
						config.AddShopItems(itemCfg, text, val.creditsWorth.ToString());
						try
						{
							int shopItemPrice = config.GetShopItemPrice(itemCfg, text);
							config.ShowConsole("Set " + text + " price to " + shopItemPrice + "$");
							val.creditsWorth = shopItemPrice;
						}
						catch (KeyNotFoundException)
						{
							config.ShowConsole(text + " not found in config.");
						}
					}
					buyableItemsList = ___buyableItemsList;
				}
			}
			catch (Exception ex2)
			{
				config.ShowConsoleErr("Error during changing item's prices");
				config.ShowConsoleErr("CustomPrice - ChangePrice - buyableItemsList : ");
				config.ShowConsoleErr(ex2.ToString());
			}
			try
			{
				if (___buyableVehicles == null)
				{
					return;
				}
				config.ShowConsole("Loading the new price of " + ___buyableVehicles.Length + " vehicle.");
				BuyableVehicle[] array2 = ___buyableVehicles;
				foreach (BuyableVehicle val2 in array2)
				{
					string text2 = val2.vehicleDisplayName.Trim();
					config.AddShopItems(vehicleCfg, text2, val2.creditsWorth.ToString());
					try
					{
						int shopItemPrice2 = config.GetShopItemPrice(vehicleCfg, text2);
						config.ShowConsole("Set " + text2 + " price to " + shopItemPrice2 + "$");
						val2.creditsWorth = shopItemPrice2;
					}
					catch (KeyNotFoundException)
					{
						config.ShowConsole(text2 + " not found in config.");
					}
				}
				buyableVehicles = ___buyableVehicles;
			}
			catch (Exception ex4)
			{
				config.ShowConsoleErr("Error during changing vehicle's prices");
				config.ShowConsoleErr("CustomPrice - ChangePrice - buyableVehicles : ");
				config.ShowConsoleErr(ex4.ToString());
			}
		}

		[HarmonyPatch(typeof(Terminal), "LoadNewNodeIfAffordable")]
		[HarmonyPrefix]
		private static void ChangePriceAfford(TerminalNode node)
		{
			if ((Object)(object)node == (Object)null)
			{
				return;
			}
			try
			{
				if (node.buyVehicleIndex != -1)
				{
					int buyVehicleIndex = node.buyVehicleIndex;
					BuyableVehicle val = ((buyableVehicles.Count() >= buyVehicleIndex) ? buyableVehicles[buyVehicleIndex] : null);
					if (val == null)
					{
						return;
					}
					string vehicleDisplayName = val.vehicleDisplayName;
					config.ShowConsole("(" + buyVehicleIndex + ") " + vehicleDisplayName + " : " + node.itemCost);
					node.itemCost = config.GetShopItemPrice(vehicleCfg, vehicleDisplayName);
				}
			}
			catch (Exception ex)
			{
				config.ShowConsoleErr("Error during changing afford véhicle's prices");
				config.ShowConsoleErr("CustomPrice - ChangePriceAfford - buyVehicleIndex : ");
				config.ShowConsoleErr(ex.ToString());
			}
			try
			{
				if (node.buyItemIndex != -1)
				{
					int buyVehicleIndex = node.buyItemIndex;
					Item val2 = ((buyableItemsList.Count() >= buyVehicleIndex) ? buyableItemsList[buyVehicleIndex] : null);
					if (!((Object)(object)val2 == (Object)null))
					{
						string itemName = val2.itemName;
						config.ShowConsole("(" + buyVehicleIndex + ") " + itemName + " : " + node.itemCost);
						node.itemCost = config.GetShopItemPrice(itemCfg, itemName);
					}
				}
			}
			catch (Exception ex2)
			{
				config.ShowConsoleErr("Error during changing afford véhicle's prices");
				config.ShowConsoleErr("CustomPrice - ChangePriceAfford - buyItemIndex : ");
				config.ShowConsoleErr(ex2.ToString());
			}
		}
	}
	[HarmonyPatch(typeof(Terminal))]
	internal class StartingCreditPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void SetStartingCredit(ref int ___groupCredits)
		{
			CustomConfig config = Plugin.Config;
			int value = config.StartCredit.Value;
			config.ShowConsole("Start money set to " + value);
			___groupCredits = value;
		}
	}
	[BepInPlugin("luffyvanquish.CustomPrice", "CustomPrice", "1.0.3")]
	[BepInDependency("com.sigurd.csync", "5.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal const string modGUID = "luffyvanquish.CustomPrice";

		private const string modName = "CustomPrice";

		private const string modVersion = "1.0.3";

		private static readonly Harmony harmony = new Harmony("luffyvanquish.CustomPrice");

		internal static ManualLogSource Logger { get; private set; } = null;


		internal static CustomConfig Config { get; private set; } = null;


		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Config = new CustomConfig(((BaseUnityPlugin)this).Config, Logger);
			((SyncedConfig2<CustomConfig>)Config).InitialSyncCompleted += delegate
			{
				Logger.LogInfo((object)"Initial sync complete!");
			};
			harmony.PatchAll(typeof(StartingCreditPatch));
			harmony.PatchAll(typeof(CustomPricePatch));
			Logger.LogInfo((object)"luffyvanquish.CustomPrice Loaded.");
		}
	}
}