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.

182 lines
8.0 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() => resetEvent.Set();
  54. internal static MethodInfo ImplInvokeChangedMethod = typeof(Impl).GetMethod(nameof(ImplInvokeChanged));
  55. public static void ImplInvokeChanged(IGeneratedStore s) => FindImpl(s).InvokeChanged();
  56. public void InvokeChanged() => generated.Changed();
  57. internal static MethodInfo ImplTakeReadMethod = typeof(Impl).GetMethod(nameof(ImplTakeRead));
  58. public static void ImplTakeRead(IGeneratedStore s) => FindImpl(s).TakeRead();
  59. public void TakeRead()
  60. {
  61. if (!WriteSyncObject.IsWriteLockHeld)
  62. WriteSyncObject.EnterReadLock();
  63. }
  64. internal static MethodInfo ImplReleaseReadMethod = typeof(Impl).GetMethod(nameof(ImplReleaseRead));
  65. public static void ImplReleaseRead(IGeneratedStore s) => FindImpl(s).ReleaseRead();
  66. public void ReleaseRead()
  67. {
  68. if (!WriteSyncObject.IsWriteLockHeld)
  69. WriteSyncObject.ExitReadLock();
  70. }
  71. internal static MethodInfo ImplTakeWriteMethod = typeof(Impl).GetMethod(nameof(ImplTakeWrite));
  72. public static void ImplTakeWrite(IGeneratedStore s) => FindImpl(s).TakeWrite();
  73. public void TakeWrite() => WriteSyncObject.EnterWriteLock();
  74. internal static MethodInfo ImplReleaseWriteMethod = typeof(Impl).GetMethod(nameof(ImplReleaseWrite));
  75. public static void ImplReleaseWrite(IGeneratedStore s) => FindImpl(s).ReleaseWrite();
  76. public void ReleaseWrite() => WriteSyncObject.ExitWriteLock();
  77. internal static MethodInfo ImplChangeTransactionMethod = typeof(Impl).GetMethod(nameof(ImplChangeTransaction));
  78. public static IDisposable ImplChangeTransaction(IGeneratedStore s, IDisposable nest) => FindImpl(s).ChangeTransaction(nest);
  79. // TODO: improve trasactionals so they don't always save in every case
  80. public IDisposable ChangeTransaction(IDisposable nest, bool takeWrite = true)
  81. => GetFreeTransaction().InitWith(this, !inChangeTransaction, nest, takeWrite && !WriteSyncObject.IsWriteLockHeld);
  82. private ChangeTransactionObj GetFreeTransaction()
  83. => freeTransactionObjs.Count > 0 ? freeTransactionObjs.Pop()
  84. : new ChangeTransactionObj();
  85. // TODO: maybe sometimes clean this?
  86. private static readonly Stack<ChangeTransactionObj> freeTransactionObjs = new Stack<ChangeTransactionObj>();
  87. private sealed class ChangeTransactionObj : IDisposable
  88. {
  89. private struct Data
  90. {
  91. public readonly Impl impl;
  92. public readonly bool owns;
  93. public readonly bool ownsWrite;
  94. public readonly IDisposable nested;
  95. public Data(Impl impl, bool owning, bool takeWrite, IDisposable nest)
  96. {
  97. this.impl = impl; owns = owning; ownsWrite = takeWrite; nested = nest;
  98. }
  99. }
  100. private Data data;
  101. public ChangeTransactionObj InitWith(Impl impl, bool owning, IDisposable nest, bool takeWrite)
  102. {
  103. data = new Data(impl, owning, takeWrite, nest);
  104. if (data.owns)
  105. impl.inChangeTransaction = true;
  106. if (data.ownsWrite)
  107. impl.TakeWrite();
  108. return this;
  109. }
  110. public void Dispose() => Dispose(true);
  111. private void Dispose(bool addToStore)
  112. {
  113. if (data.owns)
  114. {
  115. data.impl.inChangeTransaction = false;
  116. data.impl.InvokeChanged();
  117. }
  118. data.nested?.Dispose();
  119. if (data.ownsWrite)
  120. data.impl.ReleaseWrite();
  121. if (addToStore)
  122. freeTransactionObjs.Push(this);
  123. }
  124. ~ChangeTransactionObj() => Dispose(false);
  125. }
  126. public static Impl FindImpl(IGeneratedStore store)
  127. {
  128. while (store?.Parent != null) store = store.Parent; // walk to the top of the tree
  129. return store?.Impl;
  130. }
  131. internal static MethodInfo ImplReadFromMethod = typeof(Impl).GetMethod(nameof(ImplReadFrom));
  132. public static void ImplReadFrom(IGeneratedStore s, ConfigProvider provider) => FindImpl(s).ReadFrom(provider);
  133. public void ReadFrom(ConfigProvider provider)
  134. {
  135. var values = provider.Load();
  136. Logger.config.Debug("Generated impl ReadFrom");
  137. Logger.config.Debug($"Read {values}");
  138. generated.Deserialize(values);
  139. using var transaction = generated.ChangeTransaction();
  140. generated.OnReload();
  141. }
  142. internal static MethodInfo ImplWriteToMethod = typeof(Impl).GetMethod(nameof(ImplWriteTo));
  143. public static void ImplWriteTo(IGeneratedStore s, ConfigProvider provider) => FindImpl(s).WriteTo(provider);
  144. public void WriteTo(ConfigProvider provider)
  145. {
  146. var values = generated.Serialize();
  147. Logger.config.Debug("Generated impl WriteTo");
  148. Logger.config.Debug($"Serialized {values}");
  149. provider.Store(values);
  150. }
  151. }
  152. }
  153. }