Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of DucksEverywhere v1.0.4
DucksEverywhere.dll
Decompiled 2 months agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using Microsoft.CodeAnalysis; using Photon.Pun; using RepoAPI.Game; using RepoAPI.Items; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: IgnoresAccessChecksTo("")] [assembly: AssemblyCompany("Osmar Briones")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.4.0")] [assembly: AssemblyInformationalVersion("1.0.4+f6aaf67e6f589cbeb49bfd6c390c22b519ef7d9f")] [assembly: AssemblyProduct("DucksEverywhere")] [assembly: AssemblyTitle("DucksEverywhere")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("1.0.4.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [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; } } [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } public static class GameKeyEnumExtensions { public static string GetGameKey<T>(this T value) where T : Enum { return typeof(T).GetMember(value.ToString()).First().GetCustomAttribute<GameKeyAttribute>()?.Key ?? value.ToString(); } public static bool TryFromGameKey<T>(string key, out T result) where T : Enum { foreach (T item in Enum.GetValues(typeof(T)).Cast<T>()) { if (item.GetGameKey().Equals(key, StringComparison.OrdinalIgnoreCase)) { result = item; return true; } } result = default(T); return false; } public static string[] GetAllGameKeys<T>() where T : Enum { return (from T v in Enum.GetValues(typeof(T)) select v.GetGameKey()).ToArray(); } public static T FromGameKey<T>(string key) where T : Enum { if (TryFromGameKey<T>(key, out var result)) { return result; } throw new ArgumentException("Invalid game key '" + key + "' for enum " + typeof(T).Name); } } namespace RepoAPI.Game { [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] public sealed class GameKeyAttribute(string key) : Attribute() { public string Key { get; } = key; } } namespace RepoAPI.Items { public static class ItemProvider { public static bool TrySpawnRandomItem(Vector3 position, Quaternion rotation, out GameObject? spawned, float upwardOffset = 0.15f) { //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_0015: Unknown result type (might be due to invalid IL or missing references) spawned = null; string randomKey = ItemKeysProvider.GetRandomKey(); if (string.IsNullOrEmpty(randomKey)) { return false; } return TrySpawnByKey(randomKey, position, rotation, out spawned, upwardOffset); } public static bool TrySpawnByKey(string key, Vector3 position, Quaternion rotation, out GameObject? spawned, float upwardOffset = 0.15f) { //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Unknown result type (might be due to invalid IL or missing references) //IL_0040: Unknown result type (might be due to invalid IL or missing references) //IL_0045: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Unknown result type (might be due to invalid IL or missing references) //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_0059: Unknown result type (might be due to invalid IL or missing references) //IL_005a: Unknown result type (might be due to invalid IL or missing references) spawned = null; if (!SemiFunc.IsMasterClientOrSingleplayer()) { return false; } if (!StatsManager.instance.itemDictionary.TryGetValue(key, out var value) || (Object)(object)value == (Object)null || value.prefab == null) { return false; } Vector3 val = position + Vector3.up * upwardOffset; try { if (SemiFunc.IsMultiplayer()) { spawned = PhotonNetwork.InstantiateRoomObject(((PrefabRef<GameObject>)(object)value.prefab).ResourcePath, val, rotation, (byte)0, (object[])null); } else { spawned = Object.Instantiate<GameObject>(((PrefabRef<GameObject>)(object)value.prefab).Prefab, val, rotation); } } catch (Exception) { return false; } ApplySpawnImpulse(spawned); return true; } private static void ApplySpawnImpulse(GameObject? go) { //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_0021: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Unknown result type (might be due to invalid IL or missing references) Rigidbody val = default(Rigidbody); if (go != null && Object.op_Implicit((Object)(object)go) && go.TryGetComponent<Rigidbody>(ref val)) { val.AddForce(Random.insideUnitSphere * 1.25f + Vector3.up * 2f, (ForceMode)1); } } } public class ItemKeysProvider { private class WeightedKeySelector { private readonly List<(ItemName key, float threshold)> cumulativeWeights; private readonly float totalWeight; public Dictionary<ItemName, float> Weights { get; } public WeightedKeySelector(Dictionary<ItemName, float>? weights) { Weights = weights ?? new Dictionary<ItemName, float>(); cumulativeWeights = new List<(ItemName, float)>(Weights.Count); float num = 0f; foreach (KeyValuePair<ItemName, float> item in Weights.OrderBy((KeyValuePair<ItemName, float> x) => x.Key)) { if (!(item.Value <= 0f)) { num += item.Value; cumulativeWeights.Add((item.Key, num)); } } totalWeight = num; } public ItemName GetRandomKey(Random rng) { if (cumulativeWeights.Count == 0) { throw new InvalidOperationException("ItemKeysProvider weights have not been set."); } if (cumulativeWeights.Count == 1) { return cumulativeWeights[0].key; } float value = (float)(rng.NextDouble() * (double)totalWeight); int index = BinarySearchThreshold(value); return cumulativeWeights[index].key; } private int BinarySearchThreshold(float value) { int num = 0; int num2 = cumulativeWeights.Count - 1; while (num < num2) { int num3 = num + (num2 - num) / 2; if (value < cumulativeWeights[num3].threshold) { num2 = num3; } else { num = num3 + 1; } } return num; } } private static readonly Random random = new Random(); public static string[] AllKeys = GameKeyEnumExtensions.GetAllGameKeys<ItemName>(); private WeightedKeySelector selector = new WeightedKeySelector(weights); public ItemKeysProvider(Dictionary<ItemName, float>? weights) { } public Dictionary<ItemName, float> GetWeights() { return new Dictionary<ItemName, float>(selector.Weights); } public void SetWeights(Dictionary<ItemName, float>? weights) { selector = new WeightedKeySelector(weights); } public void SetWeight(ItemName itemName, float weight) { Dictionary<ItemName, float> dictionary = new Dictionary<ItemName, float>(selector.Weights); dictionary[itemName] = weight; selector = new WeightedKeySelector(dictionary); } public bool RemoveWeight(ItemName itemName) { Dictionary<ItemName, float> dictionary = new Dictionary<ItemName, float>(selector.Weights); if (!dictionary.Remove(itemName)) { return false; } selector = new WeightedKeySelector(dictionary); return true; } public static string? GetRandomKey() { if (AllKeys.Length == 0) { return null; } int num; lock (random) { num = random.Next(AllKeys.Length); } return AllKeys[num]; } public ItemName GetWeightedRandomKey() { lock (random) { return selector.GetRandomKey(random); } } } public enum ItemName { [GameKey("Item Cart Cannon")] CartCannon, [GameKey("Item Cart Laser")] CartLaser, [GameKey("Item Cart Medium")] CartMedium, [GameKey("Item Cart Small")] CartSmall, [GameKey("Item Drone Battery")] DroneBattery, [GameKey("Item Drone Feather")] DroneFeather, [GameKey("Item Drone Indestructible")] DroneIndestructible, [GameKey("Item Drone Torque")] DroneTorque, [GameKey("Item Drone Zero Gravity")] DroneZeroGravity, [GameKey("Item Duck Bucket")] DuckBucket, [GameKey("Item Extraction Tracker")] ExtractionTracker, [GameKey("Item Grenade Duct Taped")] GrenadeDuctTaped, [GameKey("Item Grenade Explosive")] GrenadeExplosive, [GameKey("Item Grenade Human")] GrenadeHuman, [GameKey("Item Grenade Shockwave")] GrenadeShockwave, [GameKey("Item Grenade Stun")] GrenadeStun, [GameKey("Item Gun Handgun")] GunHandgun, [GameKey("Item Gun Laser")] GunLaser, [GameKey("Item Gun Shockwave")] GunShockwave, [GameKey("Item Gun Shotgun")] GunShotgun, [GameKey("Item Gun Stun")] GunStun, [GameKey("Item Gun Tranq")] GunTranq, [GameKey("Item Health Pack Large")] HealthPackLarge, [GameKey("Item Health Pack Medium")] HealthPackMedium, [GameKey("Item Health Pack Small")] HealthPackSmall, [GameKey("Item Melee Baseball Bat")] MeleeBaseballBat, [GameKey("Item Melee Frying Pan")] MeleeFryingPan, [GameKey("Item Melee Inflatable Hammer")] MeleeInflatableHammer, [GameKey("Item Melee Sledge Hammer")] MeleeSledgeHammer, [GameKey("Item Melee Stun Baton")] MeleeStunBaton, [GameKey("Item Melee Sword")] MeleeSword, [GameKey("Item Mine Explosive")] MineExplosive, [GameKey("Item Mine Shockwave")] MineShockwave, [GameKey("Item Mine Stun")] MineStun, [GameKey("Item Orb Zero Gravity")] OrbZeroGravity, [GameKey("Item Phase Bridge")] PhaseBridge, [GameKey("Item Power Crystal")] PowerCrystal, [GameKey("Item Rubber Duck")] RubberDuck, [GameKey("Item Upgrade Death Head Battery")] UpgradeDeathHeadBattery, [GameKey("Item Upgrade Map Player Count")] UpgradeMapPlayerCount, [GameKey("Item Upgrade Player Crouch Rest")] UpgradePlayerCrouchRest, [GameKey("Item Upgrade Player Energy")] UpgradePlayerEnergy, [GameKey("Item Upgrade Player Extra Jump")] UpgradePlayerExtraJump, [GameKey("Item Upgrade Player Grab Range")] UpgradePlayerGrabRange, [GameKey("Item Upgrade Player Grab Strength")] UpgradePlayerGrabStrength, [GameKey("Item Upgrade Player Health")] UpgradePlayerHealth, [GameKey("Item Upgrade Player Sprint Speed")] UpgradePlayerSprintSpeed, [GameKey("Item Upgrade Player Tumble Climb")] UpgradePlayerTumbleClimb, [GameKey("Item Upgrade Player Tumble Launch")] UpgradePlayerTumbleLaunch, [GameKey("Item Upgrade Player Tumble Wings")] UpgradePlayerTumbleWings, [GameKey("Item Valuable Tracker")] ValuableTracker } } namespace DucksEverywhere { internal enum DuckSpawnMode { Both, TruckOnly, LevelOnly } internal sealed class ConfigurationController { private static ConfigFile? configFile; private static ConfigEntry<bool>? enabledEntry; private static ConfigEntry<DuckSpawnMode>? spawnModeEntry; private static ConfigEntry<int>? truckDuckCountEntry; private static ConfigEntry<int>? levelDuckCountEntry; internal static bool IsEnabled => enabledEntry?.Value ?? true; internal static DuckSpawnMode SpawnMode => spawnModeEntry?.Value ?? DuckSpawnMode.Both; internal static int TruckDuckCount => truckDuckCountEntry?.Value ?? 15; internal static int DuckCount => TruckDuckCount; internal static int LevelDuckCount => levelDuckCountEntry?.Value ?? 30; internal static void Initialize(ConfigFile config) { //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Expected O, but got Unknown //IL_0099: Unknown result type (might be due to invalid IL or missing references) //IL_00a3: Expected O, but got Unknown configFile = config; enabledEntry = configFile.Bind<bool>("General", "Enabled", true, "Enable or disable this mod."); spawnModeEntry = configFile.Bind<DuckSpawnMode>("General", "SpawnMode", DuckSpawnMode.Both, "Where rubber ducks should spawn: Both (truck and level), TruckOnly, or LevelOnly."); truckDuckCountEntry = configFile.Bind<int>("General", "TruckDuckCount", 15, new ConfigDescription("Number of rubber ducks to spawn in the truck on level start.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 50), Array.Empty<object>())); levelDuckCountEntry = configFile.Bind<int>("General", "LevelDuckCount", 30, new ConfigDescription("Number of rubber ducks to spawn across the facility rooms on level start.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>())); configFile.Save(); } internal static void Reload() { ConfigFile? obj = configFile; if (obj != null) { obj.Reload(); } ConfigFile? obj2 = configFile; if (obj2 != null) { obj2.Save(); } } } [BepInPlugin("com.osmar.DucksEverywhere", "DucksEverywhere", "1.0.4")] public class DucksEverywherePlugin : BaseUnityPlugin { public const string PluginGuid = "com.osmar.DucksEverywhere"; public const string PluginName = "DucksEverywhere"; public const string PluginVersion = "1.0.4"; internal static ManualLogSource Log { get; private set; } internal Harmony? Harmony { get; private set; } private void Awake() { //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_0044: Expected O, but got Unknown Log = ((BaseUnityPlugin)this).Logger; ((Component)this).gameObject.transform.parent = null; ((Object)((Component)this).gameObject).hideFlags = (HideFlags)61; ConfigurationController.Initialize(((BaseUnityPlugin)this).Config); Harmony = new Harmony("com.osmar.DucksEverywhere"); Harmony.PatchAll(); Log.LogInfo((object)"DucksEverywhere 1.0.4 loaded!"); } internal void Unpatch() { Harmony? harmony = Harmony; if (harmony != null) { harmony.UnpatchSelf(); } } } } namespace DucksEverywhere.Patches { [HarmonyPatch(typeof(RoundDirector), "StartRoundLogic")] internal static class RoundDirector_StartRoundLogic_Patch { [HarmonyPostfix] private static void Postfix() { if (ConfigurationController.IsEnabled && SemiFunc.RunIsLevel() && SemiFunc.IsMasterClientOrSingleplayer()) { DuckSpawnMode spawnMode = ConfigurationController.SpawnMode; if ((uint)spawnMode <= 1u) { SpawnTruckDucks(ConfigurationController.TruckDuckCount); } if ((spawnMode == DuckSpawnMode.Both || spawnMode == DuckSpawnMode.LevelOnly) ? true : false) { SpawnLevelDucks(ConfigurationController.LevelDuckCount); } } } private static void SpawnTruckDucks(int duckCount) { //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Unknown result type (might be due to invalid IL or missing references) //IL_0088: Unknown result type (might be due to invalid IL or missing references) //IL_0089: Unknown result type (might be due to invalid IL or missing references) //IL_008a: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_00aa: Unknown result type (might be due to invalid IL or missing references) //IL_00af: Unknown result type (might be due to invalid IL or missing references) //IL_00b8: Unknown result type (might be due to invalid IL or missing references) //IL_00ba: Unknown result type (might be due to invalid IL or missing references) TruckSafetySpawnPoint val = TruckSafetySpawnPoint.instance ?? Object.FindObjectOfType<TruckSafetySpawnPoint>(); if ((Object)(object)val == (Object)null) { DucksEverywherePlugin.Log.LogWarning((object)"TruckSafetySpawnPoint instance not found; skipping truck rubber duck spawning."); return; } Vector3 position = ((Component)val).transform.position; DucksEverywherePlugin.Log.LogInfo((object)$"Spawning {duckCount} rubber ducks in the truck..."); Vector3 val2 = default(Vector3); for (int i = 0; i < duckCount; i++) { ((Vector3)(ref val2))..ctor(Random.Range(-0.6f, 0.6f), 0.1f + (float)i * 0.05f, Random.Range(-0.6f, 0.6f)); Vector3 position2 = position + val2; Quaternion rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f); if (!ItemProvider.TrySpawnByKey(ItemName.RubberDuck.GetGameKey(), position2, rotation, out GameObject _)) { DucksEverywherePlugin.Log.LogWarning((object)$"Failed to spawn truck rubber duck index {i}."); } } DucksEverywherePlugin.Log.LogInfo((object)$"Finished spawning {duckCount} rubber ducks in the truck."); } private static void SpawnLevelDucks(int duckCount) { //IL_01a1: Unknown result type (might be due to invalid IL or missing references) //IL_01c9: Unknown result type (might be due to invalid IL or missing references) //IL_01ce: Unknown result type (might be due to invalid IL or missing references) //IL_01d3: Unknown result type (might be due to invalid IL or missing references) //IL_01ee: Unknown result type (might be due to invalid IL or missing references) //IL_01f3: Unknown result type (might be due to invalid IL or missing references) //IL_01fc: Unknown result type (might be due to invalid IL or missing references) //IL_01fe: Unknown result type (might be due to invalid IL or missing references) //IL_00a6: Unknown result type (might be due to invalid IL or missing references) //IL_00ab: Unknown result type (might be due to invalid IL or missing references) //IL_00b5: Unknown result type (might be due to invalid IL or missing references) //IL_00ba: Unknown result type (might be due to invalid IL or missing references) //IL_00bf: Unknown result type (might be due to invalid IL or missing references) //IL_0115: Unknown result type (might be due to invalid IL or missing references) //IL_011a: Unknown result type (might be due to invalid IL or missing references) //IL_0123: Unknown result type (might be due to invalid IL or missing references) //IL_0125: Unknown result type (might be due to invalid IL or missing references) //IL_00cb: Unknown result type (might be due to invalid IL or missing references) //IL_00f0: Unknown result type (might be due to invalid IL or missing references) //IL_00f5: Unknown result type (might be due to invalid IL or missing references) //IL_00fa: Unknown result type (might be due to invalid IL or missing references) if (duckCount <= 0) { return; } DucksEverywherePlugin.Log.LogInfo((object)$"Spawning {duckCount} rubber ducks across facility rooms..."); ValuableVolume[] array = Object.FindObjectsOfType<ValuableVolume>(false); GameObject spawned; if (array != null && array.Length != 0) { List<ValuableVolume> list = new List<ValuableVolume>(array); for (int num = list.Count - 1; num > 0; num--) { int num2 = Random.Range(0, num + 1); List<ValuableVolume> list2 = list; int index = num; int index2 = num2; ValuableVolume value = list[num2]; ValuableVolume value2 = list[num]; list2[index] = value; list[index2] = value2; } for (int i = 0; i < duckCount; i++) { Vector3 val = ((Component)list[i % list.Count]).transform.position + Vector3.up * 0.15f; if (i >= list.Count) { val += new Vector3(Random.Range(-0.2f, 0.2f), 0.05f, Random.Range(-0.2f, 0.2f)); } Quaternion rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f); if (!ItemProvider.TrySpawnByKey(ItemName.RubberDuck.GetGameKey(), val, rotation, out spawned)) { DucksEverywherePlugin.Log.LogWarning((object)$"Failed to spawn level rubber duck index {i}."); } } DucksEverywherePlugin.Log.LogInfo((object)$"Finished spawning {duckCount} rubber ducks across facility rooms."); return; } Module[] array2 = Object.FindObjectsOfType<Module>(); if (array2 != null && array2.Length != 0) { for (int j = 0; j < duckCount; j++) { Vector3 position = ((Component)array2[j % array2.Length]).transform.position + new Vector3(Random.Range(-1.5f, 1.5f), 0.5f, Random.Range(-1.5f, 1.5f)); Quaternion rotation2 = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f); if (!ItemProvider.TrySpawnByKey(ItemName.RubberDuck.GetGameKey(), position, rotation2, out spawned)) { DucksEverywherePlugin.Log.LogWarning((object)$"Failed to spawn fallback level rubber duck index {j}."); } } DucksEverywherePlugin.Log.LogInfo((object)$"Finished spawning {duckCount} fallback level rubber ducks across room modules."); } else { DucksEverywherePlugin.Log.LogWarning((object)"No ValuableVolume or Module instances found; skipping level rubber duck spawning."); } } } }