feat(context): add some safety checks

This commit is contained in:
Anna 2021-04-30 15:17:44 -04:00
parent 88161ee8f2
commit 35e76ad570

View File

@ -48,8 +48,6 @@ namespace XivCommon.Functions.ContextMenu {
/// </summary> /// </summary>
private const int InventoryMenuActionsOffset = 0x558; private const int InventoryMenuActionsOffset = 0x558;
private const int AgentAddonOffset = 0x20;
private const int ActorIdOffset = 0xEF0; private const int ActorIdOffset = 0xEF0;
private const int ContentIdLowerOffset = 0xEE0; private const int ContentIdLowerOffset = 0xEE0;
private const int TextPointerOffset = 0xE08; private const int TextPointerOffset = 0xE08;
@ -250,6 +248,7 @@ namespace XivCommon.Functions.ContextMenu {
if (inventory) { if (inventory) {
var info = GetInventoryAgentInfo(agent); var info = GetInventoryAgentInfo(agent);
var args = new InventoryContextMenuOpenArgs( var args = new InventoryContextMenuOpenArgs(
addon, addon,
agent, agent,
@ -259,6 +258,7 @@ namespace XivCommon.Functions.ContextMenu {
info.itemHq info.itemHq
); );
args.Items.AddRange(nativeItems); args.Items.AddRange(nativeItems);
try { try {
this.OpenInventoryContextMenu?.Invoke(args); this.OpenInventoryContextMenu?.Invoke(args);
} catch (Exception ex) { } catch (Exception ex) {
@ -266,9 +266,13 @@ namespace XivCommon.Functions.ContextMenu {
goto Original; goto Original;
} }
// remove any NormalContextMenuItems that may have been added - these will crash the game
args.Items.RemoveAll(item => item is NormalContextMenuItem);
this.Items.AddRange(args.Items); this.Items.AddRange(args.Items);
} else { } else {
var info = GetAgentInfo(agent); var info = GetAgentInfo(agent);
var args = new ContextMenuOpenArgs( var args = new ContextMenuOpenArgs(
addon, addon,
agent, agent,
@ -279,6 +283,7 @@ namespace XivCommon.Functions.ContextMenu {
info.actorWorld info.actorWorld
); );
args.Items.AddRange(nativeItems); args.Items.AddRange(nativeItems);
try { try {
this.OpenContextMenu?.Invoke(args); this.OpenContextMenu?.Invoke(args);
} catch (Exception ex) { } catch (Exception ex) {
@ -286,6 +291,9 @@ namespace XivCommon.Functions.ContextMenu {
goto Original; goto Original;
} }
// remove any InventoryContextMenuItems that may have been added - these will crash the game
args.Items.RemoveAll(item => item is InventoryContextMenuItem);
this.Items.AddRange(args.Items); this.Items.AddRange(args.Items);
} }
@ -360,45 +368,52 @@ namespace XivCommon.Functions.ContextMenu {
} }
var item = this.Items[index]; var item = this.Items[index];
// a custom item is being clicked switch (item) {
if (item is CustomContextMenuItem<ContextMenuItemSelectedDelegate> custom) { // a custom item is being clicked
var (_, agent) = this.GetContextMenuAgent(); case NormalContextMenuItem custom: {
var addonName = this.GetParentAddonName(addon); var (_, agent) = this.GetContextMenuAgent();
var info = GetAgentInfo(agent); var addonName = this.GetParentAddonName(addon);
var info = GetAgentInfo(agent);
var args = new ContextMenuItemSelectedArgs( var args = new ContextMenuItemSelectedArgs(
addon, addon,
agent, agent,
addonName, addonName,
info.actorId, info.actorId,
info.contentIdLower, info.contentIdLower,
info.text, info.text,
info.actorWorld info.actorWorld
); );
try { try {
custom.Action(args); custom.Action(args);
} catch (Exception ex) { } catch (Exception ex) {
Logger.LogError(ex, "Exception in custom context menu item"); Logger.LogError(ex, "Exception in custom context menu item");
}
break;
} }
} else if (item is CustomContextMenuItem<InventoryContextMenuItemSelectedDelegate> invCustom) { case InventoryContextMenuItem custom: {
var (_, agent) = this.GetContextMenuAgent(); var (_, agent) = this.GetContextMenuAgent();
var addonName = this.GetParentAddonName(addon); var addonName = this.GetParentAddonName(addon);
var info = GetInventoryAgentInfo(agent); var info = GetInventoryAgentInfo(agent);
var args = new InventoryContextMenuItemSelectedArgs( var args = new InventoryContextMenuItemSelectedArgs(
addon, addon,
agent, agent,
addonName, addonName,
info.itemId, info.itemId,
info.itemAmount, info.itemAmount,
info.itemHq info.itemHq
); );
try { try {
invCustom.Action(args); custom.Action(args);
} catch (Exception ex) { } catch (Exception ex) {
Logger.LogError(ex, "Exception in custom context menu item"); Logger.LogError(ex, "Exception in custom context menu item");
}
break;
} }
} }