Skip to content

Commit

Permalink
Added Destroy interface method for mixin VMs. Added destroy callsite …
Browse files Browse the repository at this point in the history
…for gauntlet party screen
  • Loading branch information
tbeswick96 authored and shdwp committed Apr 14, 2020
1 parent 02186da commit 465b3fb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
8 changes: 7 additions & 1 deletion UIExtenderLib/ViewModel/BaseViewModelMixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace UIExtenderLib.ViewModel
public interface IViewModelMixin
{
void Refresh();
void Destroy();
}

/**
Expand All @@ -32,6 +33,11 @@ public BaseViewModelMixin(T vm)
* Called when view model is refreshed
*/
public virtual void Refresh() { }

/**
* Called when view model is destroyed
*/
public virtual void Destroy() { }

/**
* Helper method to get private value from _vm
Expand All @@ -53,4 +59,4 @@ protected void SetPrivate<V>(string name, V value)
field.SetValue(_vm, value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,25 @@ public static void Prefix()
component.RefreshMixinsForTypes(new[] {typeof(PartyVM)});
}
}
}

// OnDeactivate - runs on party screen destroy
[HarmonyPatch]
public static class GauntletPartyScreenOnDeactivateCallsite
{
public static MethodBase TargetMethod()
{
Type type = typeof(GauntletPartyScreen);
MethodInfo interfaceMethod = type.GetInterface(nameof(IGameStateListener)).GetMethod("OnDeactivate");
if (interfaceMethod == null) return null;

InterfaceMapping map = type.GetInterfaceMap(interfaceMethod.DeclaringType ?? throw new NullReferenceException("Cannot find GauntletPartyScreen IGameStateListener.OnDeactivate method"));
return map.TargetMethods[Array.IndexOf(map.InterfaceMethods, interfaceMethod)];
}

public static void Prefix()
{
ViewModelComponent component = UIExtenderLibModule.SharedInstance.ViewModelComponent;
component.DestroyMixinsForTypes(new[] {typeof(PartyVM)});
}
}
}
13 changes: 12 additions & 1 deletion UIExtenderLibModule/ViewModel/ViewModelComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ internal void RefreshMixinsForTypes(Type[] types)
}
}

/**
* Call `Destroy` on mixins for view model types.
*/
internal void DestroyMixinsForTypes(IEnumerable<Type> types)
{
foreach (KeyValuePair<object, IViewModelMixin> pair in _mixinInstanceCache.Where(kv => types.Contains(kv.Key.GetType().BaseType)))
{
pair.Value.Destroy();
}
}

/**
* Generate type in the shared _assemblyBuilder for view model type
*/
Expand Down Expand Up @@ -205,4 +216,4 @@ internal void SaveRuntimeImages()
_assemblyBuilder.Save("UIExtender_VMGeneratedAssemblyModule.runtime_dll");
}
}
}
}

0 comments on commit 465b3fb

Please sign in to comment.