8. 代码附录 - 8.1 OpenQuantOutside

优质
小牛编辑
123浏览
2023-12-01
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Threading;
  7. using Microsoft.Win32;
  8. using System.Linq;
  9. namespace SmartQuant
  10. {
  11. public class OpenQuantOutside
  12. {
  13. private static readonly string SmartQuantPath;
  14. private static string GetSmartQuantPath()
  15. {
  16. var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C224DA18-4901-433D-BD94-82D28B640B2C}");
  17. if (key != null) {
  18. var names = new List<string>(key.GetSubKeyNames());
  19. names.Sort();
  20. return key.GetValue("InstallLocation").ToString();
  21. }
  22. return Directory.GetParent(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)).FullName;
  23. }
  24. private static Assembly DomainOnAssemblyResolve(object sender, ResolveEventArgs args)
  25. {
  26. var assemblyName = new AssemblyName(args.Name);
  27. var path = Path.Combine(SmartQuantPath, assemblyName.Name + ".dll");
  28. if (File.Exists(path)) {
  29. return Assembly.LoadFile(path);
  30. }
  31. Console.WriteLine(@"Not Found: " + assemblyName.Name);
  32. return null;
  33. }
  34. private static void OpenFileServer()
  35. {
  36. var file = Path.Combine(SmartQuantPath, "FileServer.exe");
  37. if (Process.GetProcessesByName("FileServer").Length == 0) {
  38. try {
  39. Process.Start(new ProcessStartInfo {
  40. UseShellExecute = false,
  41. FileName = file,
  42. WindowStyle = ProcessWindowStyle.Hidden,
  43. CreateNoWindow = true,
  44. Arguments = "-auto"
  45. });
  46. new EventWaitHandle(false, EventResetMode.AutoReset, "DataFileServerHandle").WaitOne();
  47. }
  48. catch (Exception ex) {
  49. Console.WriteLine(string.Concat("Framework::Init Can not start ", file, " ", ex));
  50. }
  51. }
  52. }
  53. static OpenQuantOutside()
  54. {
  55. SmartQuantPath = GetSmartQuantPath();
  56. AppDomain.CurrentDomain.AssemblyResolve += DomainOnAssemblyResolve;
  57. }
  58. public static void Init()
  59. {
  60. OpenFileServer();
  61. }
  62. private static void Resubscribe(Strategy root, FieldInfo subscriptionListfield, IDataProvider provider)
  63. {
  64. foreach (var child in root.Strategies) {
  65. var subscriptionList = (SubscriptionList)subscriptionListfield.GetValue(child);
  66. if (subscriptionList != null) {
  67. var items = subscriptionList.ToList();
  68. foreach (var item in items) {
  69. subscriptionList.Remove(item);
  70. subscriptionList.Add(item.Instrument, provider);
  71. }
  72. }
  73. if (child.Strategies.Count > 0) Resubscribe(child, subscriptionListfield, provider);
  74. }
  75. }
  76. public static void Resubscribe(Strategy root)
  77. {
  78. Resubscribe(root, null);
  79. }
  80. public static void Resubscribe(Strategy root, IDataProvider provider)
  81. {
  82. var fields = typeof(Strategy).GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
  83. FieldInfo subscriptionListField = null;
  84. FieldInfo providerField = null;
  85. foreach (var field in fields) {
  86. if (field.FieldType.Name == "SubscriptionList") {
  87. subscriptionListField = field;
  88. }
  89. if (field.FieldType.Name == "IDataProvider") {
  90. providerField = field;
  91. }
  92. }
  93. if (subscriptionListField == null)
  94. return;
  95. if (provider == null && providerField == null) {
  96. return;
  97. }
  98. Resubscribe(root, subscriptionListField, provider ?? (IDataProvider)providerField.GetValue(root));
  99. }
  100. }
  101. }