|
| 1 | +using MegaCrit.Sts2.Core.Models; |
| 2 | +using MegaCrit.Sts2.Core.Saves.Runs; |
| 3 | +using STS2RitsuLib.Patching.Models; |
| 4 | +using STS2RitsuLib.Utils; |
| 5 | + |
| 6 | +namespace STS2RitsuLib.Interop.Patches |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Bridges <see cref="SavedAttachedState{TKey,TValue}" /> instances into <see cref="SavedProperties" /> |
| 10 | + /// serialization and deserialization. |
| 11 | + /// </summary> |
| 12 | + public static class SavedAttachedStatePatches |
| 13 | + { |
| 14 | + // ReSharper disable once InconsistentNaming |
| 15 | + private static void ExportAttachedStates(ref SavedProperties? __result, object model) |
| 16 | + { |
| 17 | + var states = SavedAttachedStateRegistry.GetStatesForModel(model); |
| 18 | + if (states.Count == 0) |
| 19 | + return; |
| 20 | + |
| 21 | + var props = __result ?? new SavedProperties(); |
| 22 | + var added = false; |
| 23 | + foreach (var state in states) |
| 24 | + { |
| 25 | + state.Export(model, props); |
| 26 | + added = true; |
| 27 | + } |
| 28 | + |
| 29 | + if (__result == null && added) |
| 30 | + __result = props; |
| 31 | + } |
| 32 | + |
| 33 | + // ReSharper disable once InconsistentNaming |
| 34 | + private static void ImportAttachedStates(SavedProperties __instance, object model) |
| 35 | + { |
| 36 | + foreach (var state in SavedAttachedStateRegistry.GetStatesForModel(model)) |
| 37 | + state.Import(model, __instance); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Exports registered saved attached states after vanilla model properties are serialized. |
| 42 | + /// </summary> |
| 43 | + public sealed class SavedPropertiesFromInternalPatch : IPatchMethod |
| 44 | + { |
| 45 | + /// <inheritdoc /> |
| 46 | + public static string PatchId => "ritsulib_saved_attached_state_SavedProperties_FromInternal"; |
| 47 | + |
| 48 | + /// <inheritdoc /> |
| 49 | + public static string Description => |
| 50 | + "Bridge SavedAttachedState through SavedProperties save -> SavedProperties.FromInternal(...)"; |
| 51 | + |
| 52 | + /// <inheritdoc /> |
| 53 | + public static bool IsCritical => false; |
| 54 | + |
| 55 | + /// <inheritdoc /> |
| 56 | + public static ModPatchTarget[] GetTargets() |
| 57 | + { |
| 58 | + return |
| 59 | + [ |
| 60 | + new(typeof(SavedProperties), nameof(SavedProperties.FromInternal), |
| 61 | + [typeof(object), typeof(ModelId)]), |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Exports registered saved attached states after vanilla model properties are serialized. |
| 67 | + /// </summary> |
| 68 | + // ReSharper disable once InconsistentNaming |
| 69 | + // ReSharper disable once UnusedParameter |
| 70 | + public static void Postfix(ref SavedProperties? __result, object model, ModelId? id) |
| 71 | + { |
| 72 | + ExportAttachedStates(ref __result, model); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Imports registered saved attached states after vanilla model properties are deserialized. |
| 78 | + /// </summary> |
| 79 | + public sealed class SavedPropertiesFillInternalPatch : IPatchMethod |
| 80 | + { |
| 81 | + /// <inheritdoc /> |
| 82 | + public static string PatchId => "ritsulib_saved_attached_state_SavedProperties_FillInternal"; |
| 83 | + |
| 84 | + /// <inheritdoc /> |
| 85 | + public static string Description => |
| 86 | + "Bridge SavedAttachedState through SavedProperties load -> SavedProperties.FillInternal(...)"; |
| 87 | + |
| 88 | + /// <inheritdoc /> |
| 89 | + public static bool IsCritical => false; |
| 90 | + |
| 91 | + /// <inheritdoc /> |
| 92 | + public static ModPatchTarget[] GetTargets() |
| 93 | + { |
| 94 | + return |
| 95 | + [ |
| 96 | + new(typeof(SavedProperties), nameof(SavedProperties.FillInternal), [typeof(object)]), |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Imports registered saved attached states after vanilla model properties are deserialized. |
| 102 | + /// </summary> |
| 103 | + // ReSharper disable once InconsistentNaming |
| 104 | + public static void Postfix(SavedProperties __instance, object model) |
| 105 | + { |
| 106 | + ImportAttachedStates(__instance, model); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments