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 TeamCartShrink v1.0.2
TeamCartShrink.dll
Decompiled 9 minutes agousing System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Text; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using Microsoft.CodeAnalysis; using REPOLib.Modules; using UnityEngine; using UnityEngine.Events; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("TeamCartShrink")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.0.2.0")] [assembly: AssemblyInformationalVersion("1.0.2")] [assembly: AssemblyProduct("TeamCartShrink")] [assembly: AssemblyTitle("TeamCartShrink")] [assembly: AssemblyVersion("1.0.2.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [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; } } } namespace TeamCartShrink { public class CartShrinker : MonoBehaviour { private class Tracked { public Vector3 OriginalScale; public float LastSeenInCart; } private static readonly FieldRef<PhysGrabCart, Transform> InCartRef = AccessTools.FieldRefAccess<PhysGrabCart, Transform>("inCart"); private static readonly FieldInfo AllDictsField = AccessTools.Field(typeof(StatsManager), "dictionaryOfDictionaries"); private bool _errorLogged; private const float ScanInterval = 0.2f; private const float CartRefreshInterval = 2f; private const float LeaveGraceTime = 0.6f; private readonly Dictionary<PhysGrabObject, Tracked> _tracked = new Dictionary<PhysGrabObject, Tracked>(); private readonly HashSet<PhysGrabObject> _foundThisScan = new HashSet<PhysGrabObject>(); private readonly List<PhysGrabObject> _toRemove = new List<PhysGrabObject>(); private PhysGrabCart[] _carts = (PhysGrabCart[])(object)new PhysGrabCart[0]; private readonly Collider[] _overlap = (Collider[])(object)new Collider[256]; private float _scanTimer; private float _cartTimer; private float _targetFactor = 1f; private int _grabLayer = -1; private static IDictionary<string, Dictionary<string, int>> AllDicts(StatsManager stats) { return AllDictsField?.GetValue(stats) as IDictionary<string, Dictionary<string, int>>; } public static int TeamLevel() { if (Plugin.Upgrade == null) { return Plugin.DebugForceLevel?.Value ?? 0; } int num = 0; foreach (int value2 in Plugin.Upgrade.PlayerDictionary.Values) { num += value2; } int num2 = 0; if ((Object)(object)StatsManager.instance != (Object)null && AllDicts(StatsManager.instance) != null && AllDicts(StatsManager.instance).TryGetValue("playerUpgradeTeamCartShrink", out var value) && value != Plugin.Upgrade.PlayerDictionary) { foreach (int value3 in value.Values) { num2 += value3; } } return Mathf.Max(Mathf.Max(num, num2), Plugin.DebugForceLevel.Value); } public static float FactorForLevel(int level) { if (level <= 0) { return 1f; } return Mathf.Max(Plugin.MinScale.Value, 1f - Plugin.ShrinkPerLevel.Value * (float)level); } private void Update() { try { Tick(); } catch (Exception ex) { if (!_errorLogged) { _errorLogged = true; Plugin.Log.LogError((object)("CartShrinker error (logged once): " + ex)); } } } private void Tick() { if (_grabLayer < 0) { _grabLayer = LayerMask.NameToLayer("PhysGrabObject"); } _cartTimer -= Time.deltaTime; if (_cartTimer <= 0f) { _cartTimer = 2f; _carts = Object.FindObjectsByType<PhysGrabCart>((FindObjectsSortMode)0); } _scanTimer -= Time.deltaTime; if (_scanTimer <= 0f) { _scanTimer = 0.2f; _targetFactor = FactorForLevel(TeamLevel()); Scan(); } Animate(); } private void Scan() { //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_006e: Unknown result type (might be due to invalid IL or missing references) //IL_0078: Unknown result type (might be due to invalid IL or missing references) //IL_0085: Unknown result type (might be due to invalid IL or missing references) //IL_010f: Unknown result type (might be due to invalid IL or missing references) //IL_0114: Unknown result type (might be due to invalid IL or missing references) _foundThisScan.Clear(); if (_targetFactor >= 1f && _tracked.Count == 0) { return; } float time = Time.time; PhysGrabCart[] carts = _carts; foreach (PhysGrabCart val in carts) { if ((Object)(object)val == (Object)null) { continue; } Transform val2 = InCartRef.Invoke(val); if ((Object)(object)val2 == (Object)null) { continue; } int num = Physics.OverlapBoxNonAlloc(val2.position, val2.localScale / 2f, _overlap, val2.rotation); for (int j = 0; j < num; j++) { Collider val3 = _overlap[j]; if ((Object)(object)val3 == (Object)null || ((Component)val3).gameObject.layer != _grabLayer) { continue; } PhysGrabObject componentInParent = ((Component)val3).GetComponentInParent<PhysGrabObject>(); if (!((Object)(object)componentInParent == (Object)null) && _foundThisScan.Add(componentInParent) && IsEligible(componentInParent, val)) { if (!_tracked.TryGetValue(componentInParent, out var value)) { value = new Tracked { OriginalScale = ((Component)componentInParent).transform.localScale }; _tracked[componentInParent] = value; } value.LastSeenInCart = time; } } } } private static bool IsEligible(PhysGrabObject obj, PhysGrabCart ownerCart) { if ((Object)(object)((Component)obj).gameObject == (Object)(object)((Component)ownerCart).gameObject) { return false; } if ((Object)(object)((Component)obj).GetComponent<PhysGrabCart>() != (Object)null) { return false; } if ((Object)(object)((Component)obj).GetComponent<PlayerTumble>() != (Object)null || (Object)(object)((Component)obj).GetComponent<EnemyRigidbody>() != (Object)null) { return false; } if ((Object)(object)((Component)obj).GetComponent<ValuableObject>() != (Object)null) { return Plugin.ShrinkValuables.Value; } if ((Object)(object)((Component)obj).GetComponent<ItemAttributes>() != (Object)null) { return Plugin.ShrinkItems.Value; } return false; } private void Animate() { //IL_00a1: Unknown result type (might be due to invalid IL or missing references) //IL_00ac: Unknown result type (might be due to invalid IL or missing references) //IL_0098: Unknown result type (might be due to invalid IL or missing references) //IL_00b1: Unknown result type (might be due to invalid IL or missing references) //IL_00bb: Unknown result type (might be due to invalid IL or missing references) //IL_00c0: Unknown result type (might be due to invalid IL or missing references) //IL_00c3: Unknown result type (might be due to invalid IL or missing references) //IL_00c8: Unknown result type (might be due to invalid IL or missing references) //IL_00ca: Unknown result type (might be due to invalid IL or missing references) //IL_00cc: Unknown result type (might be due to invalid IL or missing references) //IL_00ce: Unknown result type (might be due to invalid IL or missing references) //IL_00d3: Unknown result type (might be due to invalid IL or missing references) //IL_00e7: Unknown result type (might be due to invalid IL or missing references) //IL_00e3: Unknown result type (might be due to invalid IL or missing references) //IL_00e5: 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_00f4: Unknown result type (might be due to invalid IL or missing references) if (_tracked.Count == 0) { return; } float time = Time.time; float num = 1f - Mathf.Exp((0f - Plugin.ShrinkSpeed.Value) * Time.deltaTime); _toRemove.Clear(); foreach (KeyValuePair<PhysGrabObject, Tracked> item in _tracked) { PhysGrabObject key = item.Key; Tracked value = item.Value; if ((Object)(object)key == (Object)null) { _toRemove.Add(key); continue; } bool num2 = time - value.LastSeenInCart < 0.6f; Vector3 val = (num2 ? (value.OriginalScale * _targetFactor) : value.OriginalScale); Transform transform = ((Component)key).transform; Vector3 val2 = Vector3.Lerp(transform.localScale, val, num); Vector3 val3 = val2 - val; if (((Vector3)(ref val3)).sqrMagnitude < 1E-06f) { val2 = val; } transform.localScale = val2; if (!num2 && val2 == value.OriginalScale) { _toRemove.Add(key); } } foreach (PhysGrabObject item2 in _toRemove) { _tracked.Remove(item2); } } } public class CartShrinkUpgradeItem : MonoBehaviour { private static readonly FieldRef<ItemToggle, int> TogglePhotonId = AccessTools.FieldRefAccess<ItemToggle, int>("playerTogglePhotonID"); private static readonly FieldRef<PlayerAvatar, bool> IsLocal = AccessTools.FieldRefAccess<PlayerAvatar, bool>("isLocal"); private ItemToggle _toggle; private void Awake() { //IL_0029: Unknown result type (might be due to invalid IL or missing references) //IL_0033: Expected O, but got Unknown _toggle = ((Component)this).GetComponent<ItemToggle>(); ItemUpgrade component = ((Component)this).GetComponent<ItemUpgrade>(); if ((Object)(object)component != (Object)null) { component.upgradeEvent.AddListener(new UnityAction(OnUpgrade)); } } private void OnUpgrade() { if (!((Object)(object)_toggle == (Object)null) && Plugin.Upgrade != null) { PlayerAvatar val = SemiFunc.PlayerAvatarGetFromPhotonID(TogglePhotonId.Invoke(_toggle)); if (!((Object)(object)val == (Object)null) && IsLocal.Invoke(val)) { int num = Plugin.Upgrade.AddLevel(val, 1); Plugin.Log.LogInfo((object)$"Team Cart Shrink cumpărat. Nivelul echipei: {CartShrinker.TeamLevel()} (tu: {num})."); } } } } [BepInPlugin("Luffi.TeamCartShrink", "TeamCartShrink", "1.0.2")] [BepInDependency(/*Could not decode attribute arguments.*/)] public class Plugin : BaseUnityPlugin { [HarmonyPatch(typeof(StatsManager), "RunStartStats")] internal static class StatsManagerStartPatch { [HarmonyPostfix] private static void Postfix(StatsManager __instance) { if ((Object)(object)ShrinkItem != (Object)null) { return; } try { CreateUpgradeItem(__instance); } catch (Exception ex) { Log.LogError((object)("Failed to create the shop item: " + ex)); } } } public const string Guid = "Luffi.TeamCartShrink"; public const string ModName = "TeamCartShrink"; public const string ModVersion = "1.0.2"; public const string UpgradeId = "TeamCartShrink"; public const string ItemAssetName = "Item Upgrade Team Cart Shrink"; internal static ManualLogSource Log; internal static PlayerUpgrade Upgrade; internal static Item ShrinkItem; internal static ConfigEntry<float> ShrinkPerLevel; internal static ConfigEntry<float> MinScale; internal static ConfigEntry<bool> ShrinkValuables; internal static ConfigEntry<bool> ShrinkItems; internal static ConfigEntry<float> ShrinkSpeed; internal static ConfigEntry<int> DebugForceLevel; private void Awake() { //IL_0039: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Expected O, but got Unknown //IL_0076: Unknown result type (might be due to invalid IL or missing references) //IL_0080: Expected O, but got Unknown //IL_010c: Unknown result type (might be due to invalid IL or missing references) //IL_0116: Expected O, but got Unknown //IL_0178: Unknown result type (might be due to invalid IL or missing references) //IL_0191: Unknown result type (might be due to invalid IL or missing references) //IL_0196: Unknown result type (might be due to invalid IL or missing references) //IL_019e: Unknown result type (might be due to invalid IL or missing references) //IL_01a4: Expected O, but got Unknown Log = ((BaseUnityPlugin)this).Logger; ShrinkPerLevel = ((BaseUnityPlugin)this).Config.Bind<float>("Shrink", "ShrinkPerLevel", 0.2f, new ConfigDescription("How much smaller objects get per team upgrade level (0.2 = -20% per level).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 0.9f), Array.Empty<object>())); MinScale = ((BaseUnityPlugin)this).Config.Bind<float>("Shrink", "MinScale", 0.35f, new ConfigDescription("Smallest size allowed (0.35 = 35% of original size). 1 = never shrink.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.05f, 1f), Array.Empty<object>())); ShrinkValuables = ((BaseUnityPlugin)this).Config.Bind<bool>("Shrink", "ShrinkValuables", true, "Shrink valuables."); ShrinkItems = ((BaseUnityPlugin)this).Config.Bind<bool>("Shrink", "ShrinkItems", true, "Shrink shop items too (other carts are never shrunk)."); ShrinkSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Shrink", "ShrinkSpeed", 8f, "Shrink/grow animation speed."); DebugForceLevel = ((BaseUnityPlugin)this).Config.Bind<int>("Debug", "DebugForceLevel", 0, new ConfigDescription("TESTING ONLY: if above 0, acts as if the team owns this many levels without buying. Set back to 0 after testing.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 20), Array.Empty<object>())); if (DebugForceLevel.Value > 0) { Log.LogWarning((object)$"DebugForceLevel = {DebugForceLevel.Value}: upgrade forced on (test mode)."); } Upgrade = Upgrades.RegisterUpgrade("TeamCartShrink", (Item)null, (Action<PlayerAvatar, int>)null, (Action<PlayerAvatar, int>)null); if (Upgrade == null) { Log.LogError((object)"REPOLib could not register the upgrade."); } new Harmony("Luffi.TeamCartShrink").PatchAll(typeof(StatsManagerStartPatch)); GameObject val = new GameObject("TeamCartShrink_Runner") { hideFlags = (HideFlags)61 }; Object.DontDestroyOnLoad((Object)val); val.AddComponent<CartShrinker>(); Log.LogInfo((object)"TeamCartShrink 1.0.2 loaded."); } private static void CreateUpgradeItem(StatsManager stats) { //IL_01a3: Unknown result type (might be due to invalid IL or missing references) //IL_01aa: Expected O, but got Unknown //IL_024e: Unknown result type (might be due to invalid IL or missing references) //IL_0258: Expected O, but got Unknown //IL_02ec: Unknown result type (might be due to invalid IL or missing references) //IL_02fb: Unknown result type (might be due to invalid IL or missing references) List<Item> list = stats.itemDictionary.Values.Where((Item i) => (Object)(object)i != (Object)null).ToList(); List<Item> source = list.Where((Item i) => ((int)i.itemType == 3 || (int)i.itemType == 4) && i.prefab != null && ((PrefabRef<GameObject>)(object)i.prefab).IsValid() && ((Object)i).name.StartsWith("Item Upgrade Player")).ToList(); Log.LogInfo((object)($"Items in game: {list.Count}. Vanilla player upgrades found: " + string.Join(", ", source.Select((Item i) => ((Object)i).name)))); Item val = ((IEnumerable<Item>)source).FirstOrDefault((Func<Item, bool>)((Item i) => ((Object)i).name.Contains("Strength"))) ?? source.FirstOrDefault(); if ((Object)(object)val == (Object)null) { Log.LogError((object)("No vanilla player upgrade found to use as a model. Shop item not created. All items: " + string.Join(", ", list.Select((Item i) => $"{((Object)i).name} [{i.itemType}]")))); return; } GameObject val2 = null; try { val2 = ((PrefabRef<GameObject>)(object)val.prefab).Prefab; } catch { } if ((Object)(object)val2 == (Object)null && (Object)(object)((PrefabRef<GameObject>)(object)val.prefab).Bundle == (Object)null) { val2 = Resources.Load<GameObject>(((PrefabRef<GameObject>)(object)val.prefab).ResourcePath); } if ((Object)(object)val2 == (Object)null) { Log.LogError((object)("Could not load prefab \"" + ((PrefabRef<GameObject>)(object)val.prefab).ResourcePath + "\".")); return; } GameObject val3 = new GameObject("TeamCartShrink_Templates"); val3.SetActive(false); ((Object)val3).hideFlags = (HideFlags)61; Object.DontDestroyOnLoad((Object)(object)val3); GameObject val4 = Object.Instantiate<GameObject>(val2, val3.transform); ((Object)val4).name = "Item Upgrade Team Cart Shrink"; MonoBehaviour[] components = val4.GetComponents<MonoBehaviour>(); foreach (MonoBehaviour val5 in components) { if ((Object)(object)val5 != (Object)null && ((object)val5).GetType().Name.StartsWith("ItemUpgradePlayer")) { Object.DestroyImmediate((Object)(object)val5); } } ItemUpgrade component = val4.GetComponent<ItemUpgrade>(); if ((Object)(object)component == (Object)null) { Log.LogError((object)"Model prefab has no ItemUpgrade component."); return; } component.upgradeEvent = new UnityEvent(); val4.AddComponent<CartShrinkUpgradeItem>(); Item val6 = Object.Instantiate<Item>(val); ((Object)val6).name = "Item Upgrade Team Cart Shrink"; val6.itemName = "Team Cart Shrink Upgrade"; val6.itemNameLocalized = null; val6.description = "Shrinks objects placed in any cart, for the whole team."; ItemAttributes component2 = val4.GetComponent<ItemAttributes>(); component2.item = val6; ShopVisual.Dump(val4); Texture2D val7 = ShopVisual.LoadIcon(); ShopVisual.Apply(val4, val7, ((Object)val).name); if ((Object)(object)val7 != (Object)null) { component2.icon = Sprite.Create(val7, new Rect(0f, 0f, (float)((Texture)val7).width, (float)((Texture)val7).height), new Vector2(0.5f, 0.5f)); } if (Items.RegisterItem(component2) == null) { Log.LogError((object)"REPOLib could not register the shop item."); return; } ShrinkItem = val6; Log.LogInfo((object)("Shop item created from \"" + ((Object)val).name + "\".")); } } internal static class ShopVisual { private const string IconFile = "shop_icon.png"; public static Texture2D LoadIcon() { //IL_004b: Unknown result type (might be due to invalid IL or missing references) //IL_0051: Expected O, but got Unknown string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "", "shop_icon.png"); if (!File.Exists(text)) { Plugin.Log.LogWarning((object)("shop_icon.png not found next to the DLL (" + text + ").")); return null; } Texture2D val = new Texture2D(2, 2, (TextureFormat)4, true); if (!ImageConversion.LoadImage(val, File.ReadAllBytes(text))) { Plugin.Log.LogWarning((object)"Could not read shop_icon.png."); return null; } ((Object)val).name = "TeamCartShrink_ShopIcon"; ((Texture)val).wrapMode = (TextureWrapMode)1; Object.DontDestroyOnLoad((Object)(object)val); return val; } public static void Dump(GameObject root) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine("[Visual dump] " + ((Object)root).name); Renderer[] componentsInChildren = root.GetComponentsInChildren<Renderer>(true); foreach (Renderer val in componentsInChildren) { stringBuilder.AppendLine($" Renderer {PathOf(((Component)val).transform, root.transform)} ({((object)val).GetType().Name}, enabled={val.enabled})"); Material[] sharedMaterials = val.sharedMaterials; foreach (Material val2 in sharedMaterials) { if ((Object)(object)val2 == (Object)null) { stringBuilder.AppendLine(" Material <null>"); continue; } stringBuilder.AppendLine(" Material \"" + ((Object)val2).name + "\" shader=\"" + (((Object)(object)val2.shader != (Object)null) ? ((Object)val2.shader).name : "?") + "\""); string[] texturePropertyNames = val2.GetTexturePropertyNames(); foreach (string text in texturePropertyNames) { Texture texture = val2.GetTexture(text); if ((Object)(object)texture != (Object)null) { stringBuilder.AppendLine($" {text} = \"{((Object)texture).name}\" {texture.width}x{texture.height}"); } } } } Plugin.Log.LogInfo((object)stringBuilder.ToString()); } public static bool Apply(GameObject root, Texture2D icon, string baseItemName) { //IL_00de: Unknown result type (might be due to invalid IL or missing references) //IL_00e3: Unknown result type (might be due to invalid IL or missing references) //IL_00fb: Expected O, but got Unknown if ((Object)(object)icon == (Object)null) { return false; } string[] hintWords = baseItemName.Replace("Item Upgrade Player", "").Trim().ToLowerInvariant() .Split(' '); bool flag = false; Renderer[] componentsInChildren = root.GetComponentsInChildren<Renderer>(true); foreach (Renderer val in componentsInChildren) { Material[] array = val.sharedMaterials; bool flag2 = false; for (int j = 0; j < array.Length; j++) { Material val2 = array[j]; if ((Object)(object)val2 == (Object)null) { continue; } string[] texturePropertyNames = val2.GetTexturePropertyNames(); foreach (string text in texturePropertyNames) { Texture texture = val2.GetTexture(text); if (!((Object)(object)texture == (Object)null) && LooksLikeUpgradeIcon(((Object)texture).name, ((Object)val2).name, hintWords)) { if (!flag2) { array = (Material[])array.Clone(); flag2 = true; } if ((Object)(object)array[j] == (Object)(object)val2) { array[j] = new Material(val2) { name = ((Object)val2).name + " (TeamCartShrink)" }; } array[j].SetTexture(text, (Texture)(object)icon); Plugin.Log.LogInfo((object)("Shop look: replaced " + text + " \"" + ((Object)texture).name + "\" on " + PathOf(((Component)val).transform, root.transform) + ".")); flag = true; } } } if (flag2) { val.sharedMaterials = array; } } if (!flag) { Plugin.Log.LogWarning((object)"Shop look: no upgrade icon texture recognized; the item keeps the vanilla look. See [Visual dump] above."); } return flag; } private static bool LooksLikeUpgradeIcon(string texName, string matName, string[] hintWords) { string text = (texName + " " + matName).ToLowerInvariant(); if (text.Contains("icon")) { return true; } foreach (string text2 in hintWords) { if (text2.Length >= 4 && text.Contains(text2)) { return true; } } return false; } private static string PathOf(Transform t, Transform root) { string text = ((Object)t).name; while ((Object)(object)t.parent != (Object)null && (Object)(object)t != (Object)(object)root) { t = t.parent; text = ((Object)t).name + "/" + text; } return text; } } }