Decompiled source of CustomPrice v1.0.1

CustomPrice.dll

Decompiled 5 hours 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 System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CSync.Extensions;
using CSync.Lib;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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"), true, 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 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;

		[HarmonyPatch(typeof(Terminal), "SetItemSales")]
		[HarmonyPriority(0)]
		[HarmonyPostfix]
		private static void ChangePrice(ref Item[] ___buyableItemsList, ref BuyableVehicle[] ___buyableVehicles)
		{
			if (___buyableItemsList != null)
			{
				config.ShowConsole("Loading the new price of " + ___buyableItemsList.Length + " shop items.");
				SyncedEntry<string> itemsShopPrice = config.ItemsShopPrice;
				Item[] array = ___buyableItemsList;
				foreach (Item val in array)
				{
					string text = val.itemName.Trim();
					config.AddShopItems(itemsShopPrice, text, val.creditsWorth.ToString());
					try
					{
						int shopItemPrice = config.GetShopItemPrice(itemsShopPrice, text);
						config.ShowConsole("Set " + text + " price to " + shopItemPrice + "$");
						val.creditsWorth = shopItemPrice;
					}
					catch (KeyNotFoundException)
					{
						config.ShowConsole(text + " not found in config.");
					}
				}
			}
			if (___buyableVehicles == null)
			{
				return;
			}
			config.ShowConsole("Loading the new price of " + ___buyableVehicles.Length + " vehicle.");
			SyncedEntry<string> vehiclesPrice = config.VehiclesPrice;
			BuyableVehicle[] array2 = ___buyableVehicles;
			foreach (BuyableVehicle val2 in array2)
			{
				string text2 = val2.vehicleDisplayName.Trim();
				config.AddShopItems(vehiclesPrice, text2, val2.creditsWorth.ToString());
				try
				{
					int shopItemPrice2 = config.GetShopItemPrice(vehiclesPrice, text2);
					config.ShowConsole("Set " + text2 + " price to " + shopItemPrice2 + "$");
					val2.creditsWorth = shopItemPrice2;
				}
				catch (KeyNotFoundException)
				{
					config.ShowConsole(text2 + " not found in config.");
				}
			}
		}
	}
	[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", "0.0.0.1")]
	[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 = "0.0.0.1";

		private static 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.");
		}
	}
}