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.

198 lines
8.5 KiB

  1. using IPA.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Threading;
  6. using System.Runtime.CompilerServices;
  7. using System.ComponentModel;
  8. using IPA.Config.Data;
  9. #if NET3
  10. using Net3_Proxy;
  11. using Array = Net3_Proxy.Array;
  12. #endif
  13. [assembly: InternalsVisibleTo(IPA.Config.Stores.GeneratedStore.AssemblyVisibilityTarget)]
  14. namespace IPA.Config.Stores
  15. {
  16. internal static partial class GeneratedStoreImpl
  17. {
  18. internal interface IGeneratedStore
  19. {
  20. Type Type { get; }
  21. IGeneratedStore Parent { get; }
  22. Impl Impl { get; }
  23. void OnReload();
  24. void Changed();
  25. IDisposable ChangeTransaction();
  26. Value Serialize();
  27. void Deserialize(Value val);
  28. }
  29. internal interface IGeneratedStore<T> : IGeneratedStore where T : class
  30. {
  31. void CopyFrom(T source, bool useLock);
  32. }
  33. internal interface IGeneratedPropertyChanged : INotifyPropertyChanged
  34. {
  35. PropertyChangedEventHandler PropertyChangedEvent { get; }
  36. }
  37. internal class Impl : IConfigStore
  38. {
  39. private readonly IGeneratedStore generated;
  40. private bool inChangeTransaction = false;
  41. //private bool changedInTransaction = false;
  42. internal static ConstructorInfo Ctor = typeof(Impl).GetConstructor(new[] { typeof(IGeneratedStore) });
  43. public Impl(IGeneratedStore store) => generated = store;
  44. private readonly AutoResetEvent resetEvent = new AutoResetEvent(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 ReaderWriterLockSlim();
  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, !inChangeTransaction, nest, takeWrite && !WriteSyncObject.IsWriteLockHeld);
  93. private 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 Stack<ChangeTransactionObj>();
  98. private sealed class ChangeTransactionObj : IDisposable
  99. {
  100. private struct Data
  101. {
  102. public readonly Impl impl;
  103. public readonly bool owns;
  104. public readonly bool ownsWrite;
  105. public readonly IDisposable nested;
  106. public Data(Impl impl, bool owning, bool takeWrite, IDisposable nest)
  107. {
  108. this.impl = impl; owns = owning; ownsWrite = takeWrite; nested = nest;
  109. }
  110. }
  111. private Data data;
  112. public ChangeTransactionObj InitWith(Impl impl, bool owning, IDisposable nest, bool takeWrite)
  113. {
  114. data = new Data(impl, owning, takeWrite, nest);
  115. if (data.owns)
  116. impl.inChangeTransaction = true;
  117. if (data.ownsWrite)
  118. impl.TakeWrite();
  119. return this;
  120. }
  121. public void Dispose() => Dispose(true);
  122. private void Dispose(bool addToStore)
  123. {
  124. if (data.owns)
  125. {
  126. data.impl.inChangeTransaction = false;
  127. data.impl.InvokeChanged();
  128. }
  129. data.nested?.Dispose();
  130. try
  131. {
  132. if (data.ownsWrite)
  133. data.impl.ReleaseWrite();
  134. }
  135. catch
  136. {
  137. }
  138. if (addToStore)
  139. freeTransactionObjs.Push(this);
  140. }
  141. ~ChangeTransactionObj() => Dispose(false);
  142. }
  143. public static Impl FindImpl(IGeneratedStore store)
  144. {
  145. while (store?.Parent != null) store = store.Parent; // walk to the top of the tree
  146. return store?.Impl;
  147. }
  148. internal static MethodInfo ImplReadFromMethod = typeof(Impl).GetMethod(nameof(ImplReadFrom));
  149. public static void ImplReadFrom(IGeneratedStore s, ConfigProvider provider) => FindImpl(s).ReadFrom(provider);
  150. public void ReadFrom(ConfigProvider provider)
  151. {
  152. var values = provider.Load();
  153. Logger.config.Debug("Generated impl ReadFrom");
  154. Logger.config.Debug($"Read {values}");
  155. generated.Deserialize(values);
  156. using var transaction = generated.ChangeTransaction();
  157. generated.OnReload();
  158. }
  159. internal static MethodInfo ImplWriteToMethod = typeof(Impl).GetMethod(nameof(ImplWriteTo));
  160. public static void ImplWriteTo(IGeneratedStore s, ConfigProvider provider) => FindImpl(s).WriteTo(provider);
  161. public void WriteTo(ConfigProvider provider)
  162. {
  163. var values = generated.Serialize();
  164. Logger.config.Debug("Generated impl WriteTo");
  165. Logger.config.Debug($"Serialized {values}");
  166. provider.Store(values);
  167. }
  168. }
  169. }
  170. }