You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
8.3 KiB

  1. #nullable enable
  2. using IPA.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using System.Threading;
  7. using System.Runtime.CompilerServices;
  8. using System.ComponentModel;
  9. using IPA.Config.Data;
  10. #if NET3
  11. using Net3_Proxy;
  12. using Array = Net3_Proxy.Array;
  13. #endif
  14. [assembly: InternalsVisibleTo(IPA.Config.Stores.GeneratedStore.AssemblyVisibilityTarget)]
  15. namespace IPA.Config.Stores
  16. {
  17. internal static partial class GeneratedStoreImpl
  18. {
  19. internal interface IGeneratedStore
  20. {
  21. Type Type { get; }
  22. IGeneratedStore Parent { get; }
  23. Impl Impl { get; }
  24. void OnReload();
  25. void Changed();
  26. IDisposable ChangeTransaction();
  27. Value Serialize();
  28. void Deserialize(Value val);
  29. }
  30. internal interface IGeneratedStore<T> : IGeneratedStore where T : class
  31. {
  32. void CopyFrom(T source, bool useLock);
  33. }
  34. internal interface IGeneratedPropertyChanged : INotifyPropertyChanged
  35. {
  36. PropertyChangedEventHandler PropertyChangedEvent { get; }
  37. }
  38. internal class Impl : IConfigStore
  39. {
  40. private readonly IGeneratedStore generated;
  41. private long enteredTransactions;
  42. internal static ConstructorInfo Ctor = typeof(Impl).GetConstructor(new[] { typeof(IGeneratedStore) });
  43. public Impl(IGeneratedStore store) => generated = store;
  44. private readonly AutoResetEvent resetEvent = new(false);
  45. public WaitHandle SyncObject => resetEvent;
  46. public static WaitHandle? ImplGetSyncObject(IGeneratedStore s) => FindImpl(s)?.SyncObject;
  47. internal static MethodInfo ImplGetSyncObjectMethod = typeof(Impl).GetMethod(nameof(ImplGetSyncObject));
  48. public ReaderWriterLockSlim WriteSyncObject { get; } = new();
  49. public static ReaderWriterLockSlim? ImplGetWriteSyncObject(IGeneratedStore s) => FindImpl(s)?.WriteSyncObject;
  50. internal static MethodInfo ImplGetWriteSyncObjectMethod = typeof(Impl).GetMethod(nameof(ImplGetWriteSyncObject));
  51. internal static MethodInfo ImplSignalChangedMethod = typeof(Impl).GetMethod(nameof(ImplSignalChanged));
  52. public static void ImplSignalChanged(IGeneratedStore s) => FindImpl(s)?.SignalChanged();
  53. public void SignalChanged()
  54. {
  55. try
  56. {
  57. _ = resetEvent.Set();
  58. }
  59. catch (ObjectDisposedException e)
  60. {
  61. Logger.config.Error($"ObjectDisposedException while signalling a change for generated store {generated?.GetType()}");
  62. Logger.config.Error(e);
  63. }
  64. }
  65. internal static MethodInfo ImplInvokeChangedMethod = typeof(Impl).GetMethod(nameof(ImplInvokeChanged));
  66. public static void ImplInvokeChanged(IGeneratedStore s) => FindImpl(s)?.InvokeChanged();
  67. public void InvokeChanged() => generated.Changed();
  68. internal static MethodInfo ImplTakeReadMethod = typeof(Impl).GetMethod(nameof(ImplTakeRead));
  69. public static void ImplTakeRead(IGeneratedStore s) => FindImpl(s)?.TakeRead();
  70. public void TakeRead()
  71. {
  72. if (!WriteSyncObject.IsWriteLockHeld)
  73. WriteSyncObject.EnterReadLock();
  74. }
  75. internal static MethodInfo ImplReleaseReadMethod = typeof(Impl).GetMethod(nameof(ImplReleaseRead));
  76. public static void ImplReleaseRead(IGeneratedStore s) => FindImpl(s)?.ReleaseRead();
  77. public void ReleaseRead()
  78. {
  79. if (!WriteSyncObject.IsWriteLockHeld)
  80. WriteSyncObject.ExitReadLock();
  81. }
  82. internal static MethodInfo ImplTakeWriteMethod = typeof(Impl).GetMethod(nameof(ImplTakeWrite));
  83. public static void ImplTakeWrite(IGeneratedStore s) => FindImpl(s)?.TakeWrite();
  84. public void TakeWrite() => WriteSyncObject.EnterWriteLock();
  85. internal static MethodInfo ImplReleaseWriteMethod = typeof(Impl).GetMethod(nameof(ImplReleaseWrite));
  86. public static void ImplReleaseWrite(IGeneratedStore s) => FindImpl(s)?.ReleaseWrite();
  87. public void ReleaseWrite() => WriteSyncObject.ExitWriteLock();
  88. internal static MethodInfo ImplChangeTransactionMethod = typeof(Impl).GetMethod(nameof(ImplChangeTransaction));
  89. public static IDisposable? ImplChangeTransaction(IGeneratedStore s, IDisposable nest) => FindImpl(s)?.ChangeTransaction(nest);
  90. // TODO: improve trasactionals so they don't always save in every case
  91. public IDisposable ChangeTransaction(IDisposable nest, bool takeWrite = true)
  92. => GetFreeTransaction().InitWith(this, nest, takeWrite && !WriteSyncObject.IsWriteLockHeld);
  93. private static ChangeTransactionObj GetFreeTransaction()
  94. => freeTransactionObjs.Count > 0 ? freeTransactionObjs.Pop()
  95. : new ChangeTransactionObj();
  96. // TODO: maybe sometimes clean this?
  97. private static readonly Stack<ChangeTransactionObj> freeTransactionObjs = new();
  98. private sealed class ChangeTransactionObj : IDisposable
  99. {
  100. private struct Data
  101. {
  102. public readonly Impl impl;
  103. public readonly bool ownsWrite;
  104. public readonly IDisposable nested;
  105. public Data(Impl impl, bool takeWrite, IDisposable nest)
  106. {
  107. this.impl = impl; ownsWrite = takeWrite; nested = nest;
  108. }
  109. }
  110. private Data data;
  111. public ChangeTransactionObj InitWith(Impl impl, IDisposable nest, bool takeWrite)
  112. {
  113. data = new Data(impl, takeWrite, nest);
  114. _ = Interlocked.Increment(ref impl.enteredTransactions);
  115. if (data.ownsWrite)
  116. impl.TakeWrite();
  117. return this;
  118. }
  119. public void Dispose() => Dispose(true);
  120. private void Dispose(bool addToStore)
  121. {
  122. if (data.impl != null && Interlocked.Decrement(ref data.impl.enteredTransactions) == 0)
  123. {
  124. data.impl.InvokeChanged();
  125. }
  126. data.nested?.Dispose();
  127. try
  128. {
  129. if (data.ownsWrite)
  130. data.impl?.ReleaseWrite();
  131. }
  132. catch
  133. {
  134. }
  135. data = default;
  136. if (addToStore)
  137. freeTransactionObjs.Push(this);
  138. }
  139. ~ChangeTransactionObj() => Dispose(false);
  140. }
  141. public static Impl? FindImpl(IGeneratedStore store)
  142. {
  143. while (store?.Parent != null) store = store.Parent; // walk to the top of the tree
  144. return store?.Impl;
  145. }
  146. internal static MethodInfo ImplReadFromMethod = typeof(Impl).GetMethod(nameof(ImplReadFrom));
  147. public static void ImplReadFrom(IGeneratedStore s, ConfigProvider provider) => FindImpl(s)?.ReadFrom(provider);
  148. public void ReadFrom(ConfigProvider provider)
  149. {
  150. Logger.config.Debug($"Generated impl ReadFrom {generated.GetType()}");
  151. var values = provider.Load();
  152. //Logger.config.Debug($"Read {values}");
  153. generated.Deserialize(values);
  154. using var transaction = generated.ChangeTransaction();
  155. generated.OnReload();
  156. }
  157. internal static MethodInfo ImplWriteToMethod = typeof(Impl).GetMethod(nameof(ImplWriteTo));
  158. public static void ImplWriteTo(IGeneratedStore s, ConfigProvider provider) => FindImpl(s)?.WriteTo(provider);
  159. public void WriteTo(ConfigProvider provider)
  160. {
  161. Logger.config.Debug($"Generated impl WriteTo {generated.GetType()}");
  162. var values = generated.Serialize();
  163. //Logger.config.Debug($"Serialized {values}");
  164. provider.Store(values);
  165. }
  166. }
  167. }
  168. }