From 2e62b3023ad123af1a03d01a45ce2b9b3cff5e47 Mon Sep 17 00:00:00 2001 From: Matthew Spencer Date: Fri, 7 Jun 2019 01:28:09 -0500 Subject: [PATCH] MechLabFix: Add logging; Priority bump; Recover direct inv changes --- source/AssemblyInfo.cs | 2 +- source/PatchMechlabLimitItems.cs | 99 +++++++++++++++++++++++++------- 2 files changed, 78 insertions(+), 23 deletions(-) diff --git a/source/AssemblyInfo.cs b/source/AssemblyInfo.cs index 1525371..3dfb3b1 100644 --- a/source/AssemblyInfo.cs +++ b/source/AssemblyInfo.cs @@ -31,4 +31,4 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.9.3.*")] +[assembly: AssemblyVersion("2.9.14.*")] diff --git a/source/PatchMechlabLimitItems.cs b/source/PatchMechlabLimitItems.cs index 4fc67a3..6a9b154 100644 --- a/source/PatchMechlabLimitItems.cs +++ b/source/PatchMechlabLimitItems.cs @@ -19,9 +19,9 @@ public void Activate() { "OnAddItem".Pre(); "OnRemoveItem".Pre(); "OnItemGrab".Pre(); - "ApplyFiltering".Pre(); + "ApplyFiltering".Pre("ApplyFiltering_Pre", Priority.First); "MechCanEquipItem".Pre(); - "ApplySorting".Pre(); + "ApplySorting".Pre("ApplySorting_Pre", Priority.First); // Fix some annoying seemingly vanilla log spam "OnDestroy".Pre(iel => { if(iel.iconMech != null) iel.iconMech.sprite = null; @@ -417,7 +417,31 @@ public List Filter(List Summary = () => + { + var o = ""; + o += "filteringWeapons? " + f("filteringWeapons") + "\n"; + o += "filterEnabledWeaponBallistic? " + f("filterEnabledWeaponBallistic") + "\n"; + o += "filterEnabledWeaponEnergy? " + f("filterEnabledWeaponEnergy") + "\n"; + o += "filterEnabledWeaponMissile? " + f("filterEnabledWeaponMissile") + "\n"; + o += "filterEnabledWeaponSmall? " + f("filterEnabledWeaponSmall") + "\n"; + o += "filteringEquipment? " + f("filteringEquipment") + "\n"; + o += "filterEnabledHeatsink? " + f("filterEnabledHeatsink") + "\n"; + o += "filterEnabledJumpjet? " + f("filterEnabledJumpjet") + "\n"; + o += "mechTonnage? " + iw.Field("mechTonnage").GetValue() + "\n"; + o += "filterEnabledUpgrade? " + f("filterEnabledUpgrade") + "\n"; + o += $"weaponDef? {tmpctl.weaponDef}\n"; + o += $"ammoboxDef? {tmpctl.ammoBoxDef}\n"; + o += $"componentDef? {tmpctl.componentDef}\n"; + o += $"ComponentDefType? {tmpctl.componentDef?.ComponentType}\n"; + o += $"componentDefCSType? {tmpctl.componentDef?.GetType()?.FullName}\n"; + var json = Trap(() => new Traverse(tmpctl.componentDef).Method("ToJSON").GetValue()); + o += "JSON: " + json; + return o; + }; + + var yes = Trap(() => filter.Execute(Enumerable.Repeat(tmpctl, 1)).Any() + ,() => { LogError($"Filtering failed\n{Summary()}\n\n"); return false; }); if (!yes) LogDebug(string.Format("[Filter] Removing :id {0} :componentType {1} :quantity {2}", def.Description.Id, def.ComponentType, item.quantity)); return yes; }).ToList(); @@ -433,10 +457,10 @@ public List FilterUsingHBSCode(List { + inventoryWidget.localInventory = Enumerable.Repeat(iw, 1).ToList(); var cref = GetRef(lec); lec.ItemWidget = iw; iw.ComponentRef = cref; @@ -451,7 +475,30 @@ public List FilterUsingHBSCode(List { inventoryWidget.ApplyFiltering(false); return 0; } + , () => + { + // We don't display bad items + iw.gameObject.SetActive(false); + + var fst = inventoryWidget.localInventory.Count > 0 ? inventoryWidget.localInventory[0] : null; + var o = ""; + o += $"Widget? {fst != null}\n"; + o += $"Controller? {fst?.controller != null}\n"; + o += $"ComponentDef? {fst?.controller?.componentDef != null}\n"; + o += $"ComponentDefType? {fst?.controller?.componentDef?.ComponentType}\n"; + o += $"componentDefCSType? {fst?.controller?.componentDef?.GetType()?.FullName}\n"; + o += $"ComponentRef? {fst?.ComponentRef != null}\n"; + o += $"ComponentRefDef? {fst?.ComponentRef?.Def != null}\n"; + o += $"ComponentRefType? {fst?.ComponentRef?.Def?.ComponentType}\n"; + o += $"componentRefCSType? {fst?.ComponentRef?.Def?.GetType()?.FullName}\n"; + + var def = (fst?.controller?.componentDef) ?? (fst?.ComponentRef?.Def); + var json = Trap(() => new Traverse(def).Method("ToJSON").GetValue()); + o += "JSON: " + json; + LogError($"FilterSummary: \n{o}\n\n"); + return 0; + });; filterGuard = false; lec.ItemWidget = null; var yes = iw.gameObject.activeSelf == true; @@ -522,18 +569,37 @@ public void Refresh(bool wantClobber = true) { } if (Spam) LogSpam(string.Format("[LimitItems] Refresh(F): {0} {1} {2} {3}", index, filteredInventory.Count, itemLimit, new Traverse(inventoryWidget).Field("scrollbarArea").GetValue().verticalNormalizedPosition)); - var toShow = filteredInventory.Skip(index).Take(itemLimit).ToList(); - - var icc = ielCache.ToList(); - - Func pp = lec => { - return string.Format( "[id:{0},damage:{1},quantity:{2},id:{3}]" + Func pp = lec => { + return string.Format("[id:{0},damage:{1},quantity:{2},id:{3}]" , GetRef(lec).ComponentDefID , GetRef(lec).DamageLevel , lec.quantity , lec.GetId()); }; + var iw_corrupted_add = inventoryWidget.localInventory.Where(x => !ielCache.Contains(x)).ToList(); + if (iw_corrupted_add.Count > 0) + { + LogError("inventoryWidget has been corrupted, items were added directly: " + string.Join(", ", iw_corrupted_add.Select(c => c.controller).Select(pp).ToArray())); + } + var iw_corrupted_remove = ielCache.Where(x => !inventoryWidget.localInventory.Contains(x)).ToList(); + if (iw_corrupted_remove.Count > 0) + { + LogError("inventoryWidget has been corrupted, iel elements were removed."); + } + + if (iw_corrupted_add.Any() || iw_corrupted_remove.Any()) + { + LogWarning("Restoring to last good state. Duplication or item loss may occur."); + inventoryWidget.localInventory = ielCache.ToArray().ToList(); + } + + var toShow = filteredInventory.Skip(index).Take(itemLimit).ToList(); + + var icc = ielCache.ToList(); + + + if (Spam) LogSpam("[LimitItems] Showing: " + string.Join(", ", toShow.Select(pp).ToArray())); var details = new List(); @@ -551,17 +617,6 @@ public void Refresh(bool wantClobber = true) { }); icc.ForEach(unused => unused.gameObject.SetActive(false)); - var iw_corrupted_add = inventoryWidget.localInventory.Where(x => !ielCache.Contains(x)).ToList(); - if (iw_corrupted_add.Count > 0) { - LogError("inventoryWidget has been corrupted, items were added: " + string.Join(", ", iw_corrupted_add.Select(c => c.controller).Select(pp).ToArray())); - instance.ExitMechLab(); - } - var iw_corrupted_remove = ielCache.Where(x => !inventoryWidget.localInventory.Contains(x)).ToList(); - if (iw_corrupted_remove.Count > 0) { - LogError("inventoryWidget has been corrupted, items were removed"); - instance.ExitMechLab(); - } - var listElemSize = 64.0f; var spacerTotal = 16.0f; // IEL elements are 64 tall, but have a total of 80 pixels between each when considering spacing. var spacerHalf = spacerTotal * .5f;