Skip to content

Commit

Permalink
MechLabFix: Add logging; Priority bump; Recover direct inv changes
Browse files Browse the repository at this point in the history
  • Loading branch information
m22spencer committed Jun 7, 2019
1 parent ce84f79 commit 2e62b30
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 23 deletions.
2 changes: 1 addition & 1 deletion source/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.*")]
99 changes: 77 additions & 22 deletions source/PatchMechlabLimitItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class MechlabFix : Feature {
"OnAddItem".Pre<MechLabInventoryWidget>();
"OnRemoveItem".Pre<MechLabInventoryWidget>();
"OnItemGrab".Pre<MechLabInventoryWidget>();
"ApplyFiltering".Pre<MechLabInventoryWidget>();
"ApplyFiltering".Pre<MechLabInventoryWidget>("ApplyFiltering_Pre", Priority.First);
"MechCanEquipItem".Pre<MechLabPanel>();
"ApplySorting".Pre<MechLabInventoryWidget>();
"ApplySorting".Pre<MechLabInventoryWidget>("ApplySorting_Pre", Priority.First);

// Fix some annoying seemingly vanilla log spam
"OnDestroy".Pre<InventoryItemElement_NotListView>(iel => { if(iel.iconMech != null) iel.iconMech.sprite = null;
Expand Down Expand Up @@ -417,7 +417,31 @@ public ListElementController_BASE_NotListView FetchItem(MechComponentRef mcr)
tmpctl.componentDef = def;
break;
}
var yes = filter.Execute(Enumerable.Repeat(tmpctl, 1)).Any();
Func<string> 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<float>() + "\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<string>());
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();
Expand All @@ -433,10 +457,10 @@ public ListElementController_BASE_NotListView FetchItem(MechComponentRef mcr)
sw.Start();
var tmp = inventoryWidget.localInventory;
var iw = iieTmp;
inventoryWidget.localInventory = Enumerable.Repeat(iw, 1).ToList();

// Filter items once using the faster code, then again to handle mods.
var okItems = Filter(items).Where(lec => {
inventoryWidget.localInventory = Enumerable.Repeat(iw, 1).ToList();
var cref = GetRef(lec);
lec.ItemWidget = iw;
iw.ComponentRef = cref;
Expand All @@ -451,7 +475,30 @@ public ListElementController_BASE_NotListView FetchItem(MechComponentRef mcr)
filterGuard = true;
// Let the main game or any mods filter if needed
// filter guard is to prevent us from infinitely recursing here, as this is also our triggering patch.
inventoryWidget.ApplyFiltering(false);
Trap(() => { 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<string>());
o += "JSON: " + json;
LogError($"FilterSummary: \n{o}\n\n");
return 0;
});;
filterGuard = false;
lec.ItemWidget = null;
var yes = iw.gameObject.activeSelf == true;
Expand Down Expand Up @@ -522,18 +569,37 @@ public ListElementController_BASE_NotListView FetchItem(MechComponentRef mcr)
}
if (Spam) LogSpam(string.Format("[LimitItems] Refresh(F): {0} {1} {2} {3}", index, filteredInventory.Count, itemLimit, new Traverse(inventoryWidget).Field("scrollbarArea").GetValue<UnityEngine.UI.ScrollRect>().verticalNormalizedPosition));

var toShow = filteredInventory.Skip(index).Take(itemLimit).ToList();

var icc = ielCache.ToList();

Func<ListElementController_BASE_NotListView,string> pp = lec => {
return string.Format( "[id:{0},damage:{1},quantity:{2},id:{3}]"
Func<ListElementController_BASE_NotListView, string> 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<string>();
Expand All @@ -551,17 +617,6 @@ public ListElementController_BASE_NotListView FetchItem(MechComponentRef mcr)
});
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;
Expand Down

0 comments on commit 2e62b30

Please sign in to comment.