Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal static class MenuExtensions
- {
- internal static DestT ReinterpretCast<DestT>(this object value)
- {
- return (DestT)value;
- }
- internal static T GetValue<T>(this string id)
- {
- var targetMenus = AddonMenuList.Where(menu => menu[id] != null).ToList();
- if (targetMenus.Count == 0)
- {
- throw new Exception($"Menu id '{id}' wasn't found in the menu collection");
- }
- else if (targetMenus.Count > 1)
- {
- throw new Exception($"Menu id '{id}' was found multiple times across all menus");
- }
- var targetMenu = targetMenus.First();
- var valueBase = targetMenu[id];
- if (valueBase is Slider)
- {
- return valueBase.Cast<Slider>().CurrentValue.ReinterpretCast<T>();
- }
- if (valueBase is CheckBox)
- {
- return valueBase.Cast<CheckBox>().CurrentValue.ReinterpretCast<T>();
- }
- if (valueBase is KeyBind)
- {
- return valueBase.Cast<KeyBind>().CurrentValue.ReinterpretCast<T>();
- }
- if (typeof(T) == typeof(string))
- {
- return valueBase.Cast<ComboBox>().SelectedText.ReinterpretCast<T>();
- }
- else if (typeof(T) == typeof(int))
- {
- return valueBase.Cast<ComboBox>().SelectedText.ReinterpretCast<T>();
- }
- throw new NotImplementedException($"GetValue error in {nameof(MenuExtensions)}");
- }
- internal static T GetValue<T>(this Menu m, string id)
- {
- var valueBase = m[id];
- if (valueBase is Slider)
- {
- return valueBase.Cast<Slider>().CurrentValue.ReinterpretCast<T>();
- }
- if (valueBase is CheckBox)
- {
- return valueBase.Cast<CheckBox>().CurrentValue.ReinterpretCast<T>();
- }
- if (valueBase is KeyBind)
- {
- return valueBase.Cast<KeyBind>().CurrentValue.ReinterpretCast<T>();
- }
- if (typeof(T) == typeof(string))
- {
- return valueBase.Cast<ComboBox>().SelectedText.ReinterpretCast<T>();
- }
- else if (typeof(T) == typeof(int))
- {
- return valueBase.Cast<ComboBox>().SelectedText.ReinterpretCast<T>();
- }
- throw new NotImplementedException($"GetValue error in {nameof(MenuExtensions)}");
- }
- internal static void SetValue<T>(this string id, T value)
- {
- var targetMenus = AddonMenuList.Where(menu => menu[id] != null).ToList();
- if (targetMenus.Count == 0)
- {
- throw new Exception($"Menu id '{id}' wasn't found in the menu collection");
- }
- else if (targetMenus.Count > 1)
- {
- throw new Exception($"Menu id '{id}' was found multiple times across all menus");
- }
- var targetMenu = targetMenus.First();
- var valueBase = targetMenu[id];
- if (valueBase is Slider)
- {
- valueBase.Cast<Slider>().CurrentValue = value.ReinterpretCast<int>();
- }
- if (valueBase is CheckBox)
- {
- valueBase.Cast<CheckBox>().CurrentValue = value.ReinterpretCast<bool>();
- }
- if (valueBase is KeyBind)
- {
- valueBase.Cast<KeyBind>().CurrentValue = value.ReinterpretCast<bool>();
- }
- if (valueBase is ComboBox)
- {
- var comboBox = valueBase as ComboBox;
- if (typeof(T) == typeof(string))
- {
- var str = value.ReinterpretCast<string>();
- int destIndex = -1;
- for (int i = 0; i < 1000; i++)
- {
- if (str == comboBox[i])
- {
- destIndex = i;
- break;
- }
- }
- comboBox.SelectedIndex = destIndex;
- }
- else if (typeof(T) == typeof(int))
- {
- comboBox.SelectedIndex = value.ReinterpretCast<int>();
- }
- }
- }
- internal static void SetValue<T>(this Menu m, string id, T value)
- {
- var valueBase = m[id];
- if (valueBase is Slider)
- {
- valueBase.Cast<Slider>().CurrentValue = value.ReinterpretCast<int>();
- }
- if (valueBase is CheckBox)
- {
- valueBase.Cast<CheckBox>().CurrentValue = value.ReinterpretCast<bool>();
- }
- if (valueBase is KeyBind)
- {
- valueBase.Cast<KeyBind>().CurrentValue = value.ReinterpretCast<bool>();
- }
- if (valueBase is ComboBox)
- {
- var comboBox = valueBase as ComboBox;
- if (typeof(T) == typeof(string))
- {
- var str = value.ReinterpretCast<string>();
- int destIndex = -1;
- for (int i = 0; i < 1000; i++)
- {
- if (str == comboBox[i])
- {
- destIndex = i;
- break;
- }
- }
- comboBox.SelectedIndex = destIndex;
- }
- else if (typeof(T) == typeof(int))
- {
- comboBox.SelectedIndex = value.ReinterpretCast<int>();
- }
- }
- }
- private static readonly List<Menu> AddonMenuList = new List<Menu>();
- /// <summary>
- /// Adds all menus of their declaring type in the AddonMenuList
- /// </summary>
- /// <param name="c"></param>
- internal static void ClipMenus([CallerMemberName]string c = null)
- {
- var flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
- var staticType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(classCandidate =>
- {
- bool containsMethod = classCandidate.GetMethods(flags).Any(x => x.Name.Equals(c));
- bool containsMenuFields = classCandidate.GetFields(flags).Any(f => f.FieldType == typeof(Menu));
- bool containsMenuProps = classCandidate.GetProperties(flags).Any(p => p.PropertyType == typeof(Menu));
- return containsMethod && (containsMenuFields || containsMenuProps);
- });
- if (staticType == null)
- {
- throw new NotImplementedException($"ClipMenus() error in {nameof(MenuExtensions)}");
- }
- var variables = staticType.GetFields(flags).Concat(staticType.GetProperties(flags).Cast<MemberInfo>());
- foreach (var info in variables)
- {
- if (info is FieldInfo)
- {
- var f = info as FieldInfo;
- var fType = f.FieldType;
- if (fType == typeof(Menu))
- {
- AddonMenuList.Add(f.GetValue(null) as Menu);
- }
- }
- else if (info is PropertyInfo)
- {
- var p = info as PropertyInfo;
- var pType = p.PropertyType;
- if (pType == typeof(Menu))
- {
- AddonMenuList.Add(p.GetValue(null) as Menu);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement