Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. internal static class MenuExtensions
  2. {
  3. internal static DestT ReinterpretCast<DestT>(this object value)
  4. {
  5. return (DestT)value;
  6. }
  7.  
  8. internal static T GetValue<T>(this string id)
  9. {
  10. var targetMenus = AddonMenuList.Where(menu => menu[id] != null).ToList();
  11. if (targetMenus.Count == 0)
  12. {
  13. throw new Exception($"Menu id '{id}' wasn't found in the menu collection");
  14. }
  15. else if (targetMenus.Count > 1)
  16. {
  17. throw new Exception($"Menu id '{id}' was found multiple times across all menus");
  18. }
  19.  
  20. var targetMenu = targetMenus.First();
  21. var valueBase = targetMenu[id];
  22.  
  23. if (valueBase is Slider)
  24. {
  25. return valueBase.Cast<Slider>().CurrentValue.ReinterpretCast<T>();
  26. }
  27.  
  28. if (valueBase is CheckBox)
  29. {
  30. return valueBase.Cast<CheckBox>().CurrentValue.ReinterpretCast<T>();
  31. }
  32.  
  33. if (valueBase is KeyBind)
  34. {
  35. return valueBase.Cast<KeyBind>().CurrentValue.ReinterpretCast<T>();
  36. }
  37.  
  38. if (typeof(T) == typeof(string))
  39. {
  40. return valueBase.Cast<ComboBox>().SelectedText.ReinterpretCast<T>();
  41. }
  42. else if (typeof(T) == typeof(int))
  43. {
  44. return valueBase.Cast<ComboBox>().SelectedText.ReinterpretCast<T>();
  45. }
  46.  
  47. throw new NotImplementedException($"GetValue error in {nameof(MenuExtensions)}");
  48. }
  49.  
  50. internal static T GetValue<T>(this Menu m, string id)
  51. {
  52. var valueBase = m[id];
  53.  
  54. if (valueBase is Slider)
  55. {
  56. return valueBase.Cast<Slider>().CurrentValue.ReinterpretCast<T>();
  57. }
  58.  
  59. if (valueBase is CheckBox)
  60. {
  61. return valueBase.Cast<CheckBox>().CurrentValue.ReinterpretCast<T>();
  62. }
  63.  
  64. if (valueBase is KeyBind)
  65. {
  66. return valueBase.Cast<KeyBind>().CurrentValue.ReinterpretCast<T>();
  67. }
  68.  
  69. if (typeof(T) == typeof(string))
  70. {
  71. return valueBase.Cast<ComboBox>().SelectedText.ReinterpretCast<T>();
  72. }
  73. else if (typeof(T) == typeof(int))
  74. {
  75. return valueBase.Cast<ComboBox>().SelectedText.ReinterpretCast<T>();
  76. }
  77.  
  78. throw new NotImplementedException($"GetValue error in {nameof(MenuExtensions)}");
  79. }
  80.  
  81. internal static void SetValue<T>(this string id, T value)
  82. {
  83. var targetMenus = AddonMenuList.Where(menu => menu[id] != null).ToList();
  84. if (targetMenus.Count == 0)
  85. {
  86. throw new Exception($"Menu id '{id}' wasn't found in the menu collection");
  87. }
  88. else if (targetMenus.Count > 1)
  89. {
  90. throw new Exception($"Menu id '{id}' was found multiple times across all menus");
  91. }
  92.  
  93. var targetMenu = targetMenus.First();
  94. var valueBase = targetMenu[id];
  95.  
  96.  
  97. if (valueBase is Slider)
  98. {
  99. valueBase.Cast<Slider>().CurrentValue = value.ReinterpretCast<int>();
  100. }
  101.  
  102. if (valueBase is CheckBox)
  103. {
  104. valueBase.Cast<CheckBox>().CurrentValue = value.ReinterpretCast<bool>();
  105. }
  106.  
  107. if (valueBase is KeyBind)
  108. {
  109. valueBase.Cast<KeyBind>().CurrentValue = value.ReinterpretCast<bool>();
  110. }
  111.  
  112. if (valueBase is ComboBox)
  113. {
  114. var comboBox = valueBase as ComboBox;
  115. if (typeof(T) == typeof(string))
  116. {
  117. var str = value.ReinterpretCast<string>();
  118. int destIndex = -1;
  119.  
  120. for (int i = 0; i < 1000; i++)
  121. {
  122. if (str == comboBox[i])
  123. {
  124. destIndex = i;
  125. break;
  126. }
  127. }
  128.  
  129. comboBox.SelectedIndex = destIndex;
  130. }
  131. else if (typeof(T) == typeof(int))
  132. {
  133. comboBox.SelectedIndex = value.ReinterpretCast<int>();
  134. }
  135. }
  136. }
  137.  
  138. internal static void SetValue<T>(this Menu m, string id, T value)
  139. {
  140. var valueBase = m[id];
  141.  
  142. if (valueBase is Slider)
  143. {
  144. valueBase.Cast<Slider>().CurrentValue = value.ReinterpretCast<int>();
  145. }
  146.  
  147. if (valueBase is CheckBox)
  148. {
  149. valueBase.Cast<CheckBox>().CurrentValue = value.ReinterpretCast<bool>();
  150. }
  151.  
  152. if (valueBase is KeyBind)
  153. {
  154. valueBase.Cast<KeyBind>().CurrentValue = value.ReinterpretCast<bool>();
  155. }
  156.  
  157. if (valueBase is ComboBox)
  158. {
  159. var comboBox = valueBase as ComboBox;
  160. if (typeof(T) == typeof(string))
  161. {
  162. var str = value.ReinterpretCast<string>();
  163. int destIndex = -1;
  164.  
  165. for (int i = 0; i < 1000; i++)
  166. {
  167. if (str == comboBox[i])
  168. {
  169. destIndex = i;
  170. break;
  171. }
  172. }
  173.  
  174. comboBox.SelectedIndex = destIndex;
  175. }
  176. else if (typeof(T) == typeof(int))
  177. {
  178. comboBox.SelectedIndex = value.ReinterpretCast<int>();
  179. }
  180. }
  181. }
  182.  
  183. private static readonly List<Menu> AddonMenuList = new List<Menu>();
  184.  
  185. /// <summary>
  186. /// Adds all menus of their declaring type in the AddonMenuList
  187. /// </summary>
  188. /// <param name="c"></param>
  189. internal static void ClipMenus([CallerMemberName]string c = null)
  190. {
  191. var flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
  192. var staticType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(classCandidate =>
  193. {
  194. bool containsMethod = classCandidate.GetMethods(flags).Any(x => x.Name.Equals(c));
  195. bool containsMenuFields = classCandidate.GetFields(flags).Any(f => f.FieldType == typeof(Menu));
  196. bool containsMenuProps = classCandidate.GetProperties(flags).Any(p => p.PropertyType == typeof(Menu));
  197. return containsMethod && (containsMenuFields || containsMenuProps);
  198. });
  199.  
  200. if (staticType == null)
  201. {
  202. throw new NotImplementedException($"ClipMenus() error in {nameof(MenuExtensions)}");
  203. }
  204.  
  205.  
  206. var variables = staticType.GetFields(flags).Concat(staticType.GetProperties(flags).Cast<MemberInfo>());
  207. foreach (var info in variables)
  208. {
  209. if (info is FieldInfo)
  210. {
  211. var f = info as FieldInfo;
  212. var fType = f.FieldType;
  213. if (fType == typeof(Menu))
  214. {
  215. AddonMenuList.Add(f.GetValue(null) as Menu);
  216. }
  217. }
  218. else if (info is PropertyInfo)
  219. {
  220. var p = info as PropertyInfo;
  221. var pType = p.PropertyType;
  222. if (pType == typeof(Menu))
  223. {
  224. AddonMenuList.Add(p.GetValue(null) as Menu);
  225. }
  226. }
  227. }
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement