/// <summary> /// Ordinals for properties defined on base class take precedence /// </summary> /// <returns></returns> protected IEnumerable <PropertyInfo> getHelpMethodsSorted(ParamsObject inputParams) { //Sort List <PropertyInfo> helpMethods = inputParams.GetType().GetProperties() .Where(p => p.PropertyType.IsAssignableFrom( typeof(String)) && p.GetCustomAttribute <HelpTextAttribute>() != null) .OrderBy(s => s.GetCustomAttribute <HelpTextAttribute>().Ordinal) .ToList(); //base class props don't move in ordinal IEnumerable <PropertyInfo> baseClassMethods = inputParams.GetType().BaseType.GetProperties() .Where(p => p.PropertyType.IsAssignableFrom( typeof(String)) && p.GetCustomAttribute <HelpTextAttribute>() != null); foreach (PropertyInfo pi in baseClassMethods) { //find pi in helpMethods //remove it, then place it in its correct position HelpTextAttribute attr = pi.GetCustomAttribute <HelpTextAttribute>(); PropertyInfo helpMethodPi = helpMethods.Where(p => p.Name == pi.Name).FirstOrDefault(); bool success = helpMethods.Remove(helpMethodPi); helpMethods.Insert(attr.Ordinal, helpMethodPi); } return(helpMethods); }
public SwitchParser(ITypeParser typeParser, ParamsObject paramsObject) { _typeParser = typeParser; _paramsObject = paramsObject; GetSwitchOptions(); GetAvailableSwitchProperties(); }
public void TestKeyValueOnlyParsing_Bad() { ParamsObject _paramObj = DynamicParamsCreator .Create() .OverrideTypeParsers(() => new TypeParserContainer(false, new KeyValueParser())) .AddSwitch <KeyValuePair <string, int> >("NameAge") .AddSwitch <string>("FirstName") .FinishBuilding("/NameAge:Yizzy:30"); TypeParserContainer _container = _paramObj.GetPropertyValue <TypeParserContainer>("TypeParser"); Assert.IsTrue(_container.TypeParsers.Count() == 1, "More than 1 type parser should not have existed"); bool _parseErr = false; try { _paramObj.CheckParams(); } catch { _parseErr = true; } Assert.IsTrue(_parseErr, "String and int parsers were not explicated, so this should have failed"); KeyValuePair <string, int> _namgeAge = _paramObj.GetPropertyValue <KeyValuePair <string, int> >("NameAge"); Assert.IsNull(_namgeAge.Key, "Name key should have been null"); Assert.IsTrue(_namgeAge.Value == 0, "Age value should have been null"); }
public void TestKeyValueOnlyParsing_Good() { ParamsObject _paramObj = DynamicParamsCreator .Create() .OverrideTypeParsers(() => new TypeParserContainer(false, new KeyValueParser(), new ObjectParser())) .AddSwitch <KeyValuePair <string, int> >("NameAge") .AddSwitch <string>("FirstName") .FinishBuilding("/NameAge:Yizzy:30"); TypeParserContainer _container = _paramObj.GetPropertyValue <TypeParserContainer>("TypeParser"); Assert.IsTrue(_container.TypeParsers.Count() == 2, "Only 2 type parsers should have existed"); bool _parseErr = false; try { _paramObj.CheckParams(); } catch { _parseErr = true; } Assert.IsFalse(_parseErr, "String and int parsers were explicated, so this should NOT have failed"); KeyValuePair <string, int>?_namgeAge = _paramObj.GetPropertyValue <KeyValuePair <string, int> >("NameAge"); Assert.IsNotNull(_namgeAge); Assert.IsTrue(_namgeAge.Value.Key == "Yizzy"); Assert.IsTrue(_namgeAge.Value.Value == 30); }
public static Exception AssertCheckParams(ParamsObject paramObj, string errMsg = "", bool shouldFail = false, string expctdErrMsg = "") { bool _hasExpctdErr = !string.IsNullOrWhiteSpace(expctdErrMsg); string _expctdErrMsg = expctdErrMsg; string _errMsg = shouldFail ? "Parsing should have failed." : "Parsing failed."; if (errMsg.Length > 0) { _errMsg += $" {errMsg}."; } Exception _ex = null; try { paramObj.CheckParams(); } catch (Exception ex) { _ex = ex; if (!shouldFail) { _errMsg += $" Ex: {ex.Message}"; } else if (_hasExpctdErr) { _errMsg += $" Expected err '{removePeriod(expctdErrMsg)}' does not match actual err '{removePeriod(ex.Message)}'."; } } Assert.IsTrue((shouldFail && (_hasExpctdErr && _ex != null && removePeriod(_ex.Message).ToLower().Trim() == removePeriod(expctdErrMsg).ToLower().Trim()) || (!_hasExpctdErr && _ex != null) ) || (!shouldFail && _ex == null), _errMsg); return(_ex); }
public void TestAllDefaultParsing_Not_AllValuesSet() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <string[]>("Nums") .AddSwitch <bool>("IsItTrue") .AddSwitch <KeyValuePair <string, int> >("NameAge") .AddSwitch <MyColors>("Color") .AddSwitch <int>("Height") .AddSwitch <SecureString>("pw") .FinishBuilding("/IsItTrue:true", "/NameAge:yiz:30", "/Color:Blue", "/pw:passw0rd"); bool _parseErr = false; try { _paramObj.CheckParams(); } catch { _parseErr = true; } Assert.IsFalse(_parseErr, "Check params failed"); Assert.IsTrue(_paramObj.GetPropertyValue <string[]>("Nums") == null); Assert.IsTrue(_paramObj.GetPropertyValue <bool>("IsItTrue") == true); Assert.IsTrue(_paramObj.GetPropertyValue <KeyValuePair <string, int> >("NameAge").Value == 30); Assert.IsTrue(_paramObj.GetPropertyValue <MyColors>("Color") == MyColors.Blue); Assert.IsTrue(_paramObj.GetPropertyValue <int>("Height") == 0); Assert.IsTrue(_paramObj.GetPropertyValue <SecureString>("pw").Length == 8); }
public override bool Resolve(ParamsObject requestParams) { List <Vector2> worldPositions = requestParams.GetAllOfType <Vector2>(); Vector3Int firstElementCellPosition = grid.WorldToCell(worldPositions[0]); return(worldPositions.All(worldPosition => grid.WorldToCell(worldPosition) == firstElementCellPosition)); }
private void OnReload(ParamsObject paramsObj) { if (CurrentAmmo != MaxAmmo && !reloading) { reloading = true; eventManager.TriggerEvent(GunEvents.OnReloadStart); } }
public void Test_TypeTypeParser_FriendName_Bad() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <Type>("PersonType") .FinishBuilding("/PersonType:NotReal"); Assert.IsTrue(_paramObj.GetPropertyValue <Type>("PersonType") == null); }
protected override Rigidbody2D FireProjectile(Vector2 target) { LastFired = base.FireProjectile(target); ParamsObject paramsObj = new ParamsObject(7);//<-Sound level paramsObj.Vector2 = playerRigidbody.position; GameObjectEventManager.TriggerRadiusEvent(PlayerRadiusEvents.OnPlayerMakeNoise, transform.position, 10, LayerMask.GetMask("Enemies"), paramsObj); return(LastFired); }
public void Test_TypeTypeParser_SwitchValueList_FriendName_Bad() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <Type>("PersonType", "PersonType", true, -1, "Employee", "Customer") .FinishBuilding("/PersonType:Boss"); ParamsObjectTestHelpers.AssertCheckParams(_paramObj, "PersonType parsing failed", true); }
public virtual string GetSwitchHelp(ParamsObject InputParams) { int _atLeastOne = 0; IEnumerable <SwitchAttribute> switchAttrs = getOrderedSwitches(InputParams); ///If allowed to call with no default ordinal bool defaultOrdinalAllowed = switchAttrs.Where(sa => sa.Required && sa.DefaultOrdinal == null).Count() == 0; string _defaultOrdinalMessage = ""; if (!defaultOrdinalAllowed) { _defaultOrdinalMessage = "Anonymous parameters not allowed."; } else { _defaultOrdinalMessage = "Anonymous parameters must be passed in their default ordinal, as listed below."; } string _help = _defaultOrdinalMessage + Environment.NewLine; foreach (SwitchAttribute switchAttr in switchAttrs) { SwitchHelpTextAttribute helpText = InputParams.GetPropertyByAttribute(switchAttr).GetCustomAttribute <SwitchHelpTextAttribute>(); if (helpText == null) { continue; } _atLeastOne++; string _name = helpText.Name; string _description = helpText.Description; string _isoptional = switchAttr.Required ? "Required" : "Optional"; string _acceptedValues = ""; string _noDefaultOrdinal = defaultOrdinalAllowed && switchAttr.DefaultOrdinal.HasValue ? "" : "No default ordinal. "; string[] _acceptedValArr = _parser.GetAcceptedValues(InputParams.GetPropertyByAttribute(switchAttr).PropertyType); if (_acceptedValArr.Length == 0) { _acceptedValArr = switchAttr.SwitchValues; } if (_acceptedValArr.Length > 0) { _acceptedValues = _acceptedValArr.Length == 0 ? "" : string.Format("Accepted Values: {0}`{1}`.", string.Concat( _acceptedValArr .Take(_acceptedValArr.Length - 1) .Select(s => "`" + s.ToUpper() + "`| ") ), _acceptedValArr.ElementAt(_acceptedValArr.Length - 1).ToUpper()); } if (string.IsNullOrEmpty(_name)) { _name = switchAttr.SwitchName; } _name = _name.ToUpper(); _help += Environment.NewLine + string.Format("/{0," + (_options.HelpTextIndentLength * -1).ToString() + "} {1}. {2}{3}. {4}", _name + ": ", _isoptional, _noDefaultOrdinal, _description, _acceptedValues); } _help += Environment.NewLine; return(_help); }
public override bool Resolve(ParamsObject requestParams, out Vector2 cellCenter) { Vector2 cellWorldPosition = requestParams.GetParam <Vector2>(); Vector3Int cellGridPosition = grid.WorldToCell(cellWorldPosition); float xCenter = Mathf.Floor(cellGridPosition.x) + grid.cellSize.x / 2; float yCenter = Mathf.Floor(cellGridPosition.y) + grid.cellSize.y / 2; cellCenter = new Vector2(xCenter, yCenter); return(true); }
public void Test_TypeTypeParser_SwitchValueList_ClassName_Good() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <Type>("PersonType", "PersonType", true, -1, "Employee", "Customer") .FinishBuilding("/PersonType:EmployeePersonType"); ParamsObjectTestHelpers.AssertCheckParams(_paramObj, "PersonType parsing failed"); Assert.IsTrue(_paramObj.GetPropertyValue <Type>("PersonType") == typeof(EmployeePersonType)); }
public void Test_BasicEnumParsing_Good() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <MyColors>("Color") .FinishBuilding("/Color:Yellow"); ParamsObjectTestHelpers.AssertCheckParams(_paramObj); Assert.IsTrue(_paramObj.GetPropertyValue <MyColors>("Color") == MyColors.Yellow); }
public void Test_FlagsEnumParsing_Good() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <MyPets>("Pets") .FinishBuilding("/Pets:Dog,Turtle"); ParamsObjectTestHelpers.AssertCheckParams(_paramObj); Assert.IsTrue(_paramObj.GetPropertyValue <MyPets>("Pets") == (MyPets.Dog | MyPets.Turtle)); }
private void OnReturnGun(ParamsObject paramsObj) { gunTransform.parent = transform; gunTransform.localPosition = startLocalPosition; gunTransform.rotation = transform.rotation; eventManager.TriggerEvent(GunEvents.OnReload); Destroy(gunTransform.gameObject.GetComponent <StopOnCollision>()); Destroy(gunTransform.gameObject.GetComponent <BoxCollider2D>()); Destroy(gunTransform.gameObject.GetComponent <Rigidbody2D>()); Destroy(gunTransform.gameObject.GetComponent <PlayerGunThrowReturn>()); }
protected IEnumerable <SwitchAttribute> getOrderedSwitches(ParamsObject inputParams) { IEnumerable <SwitchAttribute> allAttrs = inputParams.GetType().GetProperties() .Where(pi => pi.GetCustomAttribute <SwitchAttribute>() != null) .Select(pi => pi.GetCustomAttribute <SwitchAttribute>()); IEnumerable <SwitchAttribute> withOrdinals = allAttrs.Where(sa => sa.DefaultOrdinal != null).OrderBy(sa => sa.DefaultOrdinal); IEnumerable <SwitchAttribute> noOrdinals = allAttrs.Where(sa => sa.DefaultOrdinal == null); return(withOrdinals.Concat(noOrdinals)); }
public virtual string GetUsage(ParamsObject InputParams) { string _usage = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location) + " "; IEnumerable <SwitchAttribute> switchAttrs = getOrderedSwitches(InputParams); foreach (SwitchAttribute switchAttr in switchAttrs) { string _name = switchAttr.SwitchName; _usage += string.Format("/{0}:\"{1}\" ", _name.ToUpper(), InputParams.GetPropertyByAttribute(switchAttr).Name); } return(_usage); }
public virtual string GetHelpIfNeeded(string[] args, ParamsObject InputParams) { bool _needsHelp = args != null && args.Count() > 0 && _options.HelpCommands.Select(c => c.ToUpper()).Contains(args[0].ToUpper()); if (_needsHelp) { return(GetHelp(InputParams)); } else { return(string.Empty); } }
public void TestKeyValueArray_Good() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <KeyValuePair <string, int>[]>("NameAges") .FinishBuilding("/NameAges:Yisrael:30,Srully:10,Yitschak:40"); ParamsObjectTestHelpers.AssertCheckParams(_paramObj); KeyValuePair <string, int>[] _nameAges = _paramObj.GetPropertyValue <KeyValuePair <string, int>[]>("NameAges"); Assert.IsNotNull(_nameAges); Assert.IsTrue(_nameAges.Length == 3); Assert.IsTrue(_nameAges[1].Key == "Srully"); Assert.IsTrue(_nameAges[1].Value == 10); }
public void TestEnumArray_Good() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <MyColors[]>("Colors") .FinishBuilding("/Colors:Orange,Yellow"); ParamsObjectTestHelpers.AssertCheckParams(_paramObj); MyColors[] _colors = _paramObj.GetPropertyValue <MyColors[]>("Colors"); Assert.IsNotNull(_colors); Assert.IsTrue(_colors.Length == 2); Assert.IsTrue(_colors[0] == MyColors.Orange); Assert.IsTrue(_colors[1] == MyColors.Yellow); }
protected override void OnShoot(ParamsObject paramsObj) { bool shootOverride = paramsObj != null ? paramsObj.Bool : false; if (!shootLocked || shootOverride) { base.OnShoot(paramsObj); //GetComponentInParent<Animator>().SetTrigger("Shoot"); if (!automatic || shootOverride) { shootLocked = true; } } }
public void Test_Override_DateTimeParsing_Good() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <DateTime>("Bday") .AddSwitch <int>("Age") .AddSwitch <string>("Name") .OverrideTypeParsers(() => new TypeParserContainer(true, new DefaultTypeContainer(), new DayFirstDashOnlyDateTimeParser())) .FinishBuilding("/Bday:28-11-1987", "/Age:30", "/Name:Yisrael Lax"); ParamsObjectTestHelpers.AssertCheckParams(_paramObj); Assert.IsTrue(_paramObj.GetPropertyValue <DateTime>("Bday") == new DateTime(1987, 11, 28)); Assert.IsTrue(_paramObj.GetPropertyValue <int>("Age") == 30); Assert.IsTrue(_paramObj.GetPropertyValue <string>("Name") == "Yisrael Lax"); }
public void CreateParamsObject() { /*string _switchName = name; * bool _required = false; * int _defaultOrdinal = -1; * string[] _switchValues = new string[0] { }; */ ParamsObject _paramsObject = (ParamsObject)DynamicParamsCreator .Create() .AddSwitch("fName", typeof(string), "f") .AddSwitch("lName", typeof(string), "l") .FinishBuilding("/F:Yisrael", "/L:Lax"); Assert.IsTrue(TestValue(_paramsObject, "fName", "Yisrael")); Assert.IsTrue(TestValue(_paramsObject, "lName", "Lax")); _paramsObject.CheckParams(); }
protected virtual void OnShoot(ParamsObject paramsObj) { bool shootOverride = paramsObj != null ? paramsObj.Bool : false; if ((CurrentAmmo > 0 && !reloading && !shotFiredCooldown) || shootOverride) { FireProjectile(paramsObj.Vector2); shotFiredCooldown = true; eventManager.TriggerEvent(GunEvents.OnLockFire); return; } if (CurrentAmmo <= 0) { eventManager.TriggerEvent(GunEvents.OnReload); } }
public override bool Resolve(ParamsObject requestParams, out LayerMask resolveParams) { Vector2 cellWorldPosition = requestParams.GetParam <Vector2>(); Vector3Int cellGridPosition = grid.WorldToCell(cellWorldPosition); Tilemap tilemap = tilemaps.FirstOrDefault(t => t.HasTile(cellGridPosition)); if (tilemap != null) { resolveParams = tilemap.gameObject.layer; return(true); } else { resolveParams = default; return(false); } }
public void Test_Override_DateTimeParsing_Bad() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <DateTime>("Bday") .AddSwitch <int>("Age") .AddSwitch <string>("Name") .FinishBuilding("/Bday:28/11/1987", "/Age:30", "/Name:Yisrael Lax"); ParamsObjectTestHelpers.AssertCheckParams(_paramObj, "Parsing should have failed b/c incorrect DateTime format", true, "string was not recognized as a valid datetime"); Assert.IsTrue(_paramObj.GetPropertyValue <DateTime>("Bday") == default(DateTime)); Assert.IsTrue(_paramObj.GetPropertyValue <int>("Age") == 30); Assert.IsTrue(_paramObj.GetPropertyValue <string>("Name") == "Yisrael Lax"); }
protected override void Update() { base.Update(); if (IsAbilityActive()) { if (sprayTimer.Tick()) { AbilityEnd(); return; } if (sprayIntervalTimer.Tick()) { ParamsObject paramsObj = new ParamsObject(true); paramsObj.Vector2 = (rigidbody.rotation.ToVector2() * 2) + rigidbody.position; eventManager.TriggerEvent(GunEvents.OnShoot, paramsObj); } } }
public void Test_DateTimeParsing_Bad() { ParamsObject _paramObj = DynamicParamsCreator .Create() .AddSwitch <DateTime>("Bday") .AddSwitch <int>("Age") .AddSwitch <string>("Name") .FinishBuilding("/Bday:28/11/1987", "/Age:30", "/Name:Yisrael Lax"); bool _parseErr = false; try { _paramObj.CheckParams(); } catch { _parseErr = true; } Assert.IsTrue(_parseErr, "Parsing should have failed b/c incorrect DateTime format"); Assert.IsTrue(_paramObj.GetPropertyValue <DateTime>("Bday") == default(DateTime)); }