// // Copyright 2022 alterNERDtive. // // This file is part of YAVAPF. // // YAVAPF is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // YAVAPF is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with YAVAPF. If not, see <https://www.gnu.org/licenses/>. // using System; using System.IO; using VoiceAttack; namespace alterNERDtive.Yavapf { /// /// Extends the class with generic /// methods for getting and setting VoiceAttack variables. /// public static class VoiceAttackInitProxyExtension { /// /// Gets the value of a variable from VoiceAttack. /// /// Valid varible types are , , /// , and . /// /// The type of the variable. /// The object. /// The name of the variable. /// The value of the variable. Can be null. /// Thrown when the variable is of an invalid type. public static T? Get(this VoiceAttackInitProxyClass vaProxy, string name) { dynamic? ret = typeof(T) switch { Type boolType when boolType == typeof(bool) => vaProxy.GetBoolean(name), Type dateType when dateType == typeof(DateTime) => vaProxy.GetDate(name), Type decType when decType == typeof(decimal) => vaProxy.GetDecimal(name), Type intType when intType == typeof(int) => vaProxy.GetInt(name), Type stringType when stringType == typeof(string) => vaProxy.GetText(name), _ => throw new InvalidDataException($"Cannot get variable '{name}': invalid type '{typeof(T).Name}'."), }; return ret; } /// /// Sets a variable for use in VoiceAttack. /// /// Valid varible types are , , /// , and . /// /// The type of the variable. /// The object. /// The name of the variable. /// The value of the variable. Can not be null. /// Thrown when the variable is of an invalid type. public static void Set(this VoiceAttackInitProxyClass vaProxy, string name, T? value) { if (value == null) { vaProxy.Unset(name); } else { switch (value) { case bool b: vaProxy.SetBoolean(name, b); break; case DateTime d: vaProxy.SetDate(name, d); break; case decimal d: vaProxy.SetDecimal(name, d); break; case int i: vaProxy.SetInt(name, i); break; case string s: vaProxy.SetText(name, s); break; default: throw new InvalidDataException($"Cannot set variable '{name}': invalid type '{typeof(T).Name}'."); } } } /// /// Unsets a variable for use in VoiceAttack (= sets it to null). /// /// Valid varible types are , , /// , and . /// /// The type of the variable. /// The object. /// The name of the variable. /// Thrown when the variable is of an invalid type. public static void Unset(this VoiceAttackInitProxyClass vaProxy, string name) { switch (typeof(T)) { case Type boolType when boolType == typeof(bool): vaProxy.SetBoolean(name, null); break; case Type dateType when dateType == typeof(DateTime): vaProxy.SetDate(name, null); break; case Type decType when decType == typeof(decimal): vaProxy.SetDecimal(name, null); break; case Type intType when intType == typeof(int): vaProxy.SetInt(name, null); break; case Type stringType when stringType == typeof(string): vaProxy.SetText(name, null); break; default: throw new InvalidDataException($"Cannot set variable '{name}': invalid type '{typeof(T).Name}'."); } } } }