private void AddDelegate(ITypeDefinition dlgte, StringBuilder key) { if (dlgte.Namespace == "") { lock (Delegates) { foreach (var typeArg in dlgte.TypeParameters) { foreach (var constraint in typeArg.DirectBaseTypes) { key.Append(constraint.Name); } } if (Delegates.ContainsKey(key.ToString())) { return; } Delegates.Add(key.ToString(), new DelegateProperties(dlgte)); } } else { AddNamespace(key, dlgte); } }
public void Off(EventToken registrar) { if (Delegates.ContainsKey(registrar)) { Delegates.Remove(registrar); } }
private void AddDelegate(IType dlgte, StringBuilder key) { lock (Delegates) { foreach (var typeArg in dlgte.TypeParameters) { foreach (var constraint in typeArg.Constraints) { key.Append(constraint.Name); } } if (Delegates.ContainsKey(key.ToString())) { return; } Delegates.Add(key.ToString(), new DelegateProperties(dlgte)); } }
private void _Invoke(ref object RetObject, params object[] args) { if (args.Length < ArgumentTypes.Length) //check if a argument is using "params x[] args" { throw new Exception("missing arguments"); } List <int> usedDelegates = new List <int>(); PayloadWriter pw = new PayloadWriter(); pw.WriteInteger(sharedClass.SharedId); pw.WriteInteger(MethodId); pw.WriteByte(isDelegate ? (byte)1 : (byte)0); if (isDelegate) { pw.WriteInteger(this.DelegateId); pw.WriteInteger(this.sharedClass.SharedId); } SmartSerializer serializer = new SmartSerializer(); for (int i = 0; i < args.Length; i++) { object obj = ArgumentTypes[i].IsByRef ? null : args[i]; if (DelegateIndex.ContainsKey(i)) { obj = null; } byte[] SerializedObj = serializer.Serialize(obj); pw.WriteInteger(SerializedObj.Length); pw.WriteBytes(SerializedObj); } for (int i = 0; i < DelegateIndex.Count; i++) { Delegate del = args[DelegateIndex.Keys[i]] as Delegate; if (del != null) { if (del.Method == null) { throw new Exception("Target delegate is NULL"); } int id = rnd.Next(); while (Delegates.ContainsKey(id)) { id = rnd.Next(); } pw.WriteByte(1); SharedDelegate sharedDel = new SharedDelegate(del.Method, sharedClass, del.GetType(), id, del, this.MethodId); sharedDel.sharedMethod.Unchecked = DelegateIndex.Values[i].isUnchecked; sharedDel.sharedMethod.usePacketQueue = DelegateIndex.Values[i].UsePacketQueue; sharedDel.sharedMethod.useUdp = DelegateIndex.Values[i].UseUDP; sharedDel.sharedMethod.NoWaitingTime = DelegateIndex.Values[i].NoWaitingTime; pw.WriteObject(sharedDel); if (!isDelegate) { Delegates.Add(id, sharedDel); } continue; } pw.WriteByte(0); } if (Unchecked || useUdp) { //just execute the method and don't wait for response sharedClass.connection.Connection.SendMessage(new MsgExecuteMethod(0, pw.ToByteArray(), false)); } else { SyncObject syncObject = null; Random rnd = new Random(); int RequestId = rnd.Next(); lock (sharedClass.connection.MethodRequests) { while (sharedClass.connection.MethodRequests.ContainsKey(RequestId)) { RequestId = rnd.Next(); } syncObject = new SyncObject(sharedClass.connection.Connection.Connection); sharedClass.connection.MethodRequests.Add(RequestId, syncObject); sharedClass.connection.Connection.SendMessage(new MsgExecuteMethod(RequestId, pw.ToByteArray(), true)); } RetObject = syncObject.Wait <ReturnResult>(null, 0); } /*if (callback != null) * { * sharedClass.connection.BeginSendRequest(pw, callback, true, this.usePacketQueue); * } * else * { * if (Unchecked || useUdp) * { * //just don't wait till we received something back since it's a VOID anyway * sharedClass.connection.BeginSendRequest(pw, (object obj) => { }, false, this.usePacketQueue); * } * else * { * RetObject = sharedClass.connection.SendRequest(pw, this.usePacketQueue); * } * }*/ serializer = null; }
/// <summary> /// Verifies the provided rule and provides a list of issues with the rules. /// </summary> /// <param name="rule"> A Rule. </param> /// <returns> Enumerable of issues with the Rule. </returns> public IEnumerable <Violation> EnumerateRuleIssues(Rule rule) { var clauseLabels = rule.Clauses.GroupBy(x => x.Label); // If clauses have duplicate names foreach (var duplicateClause in clauseLabels.Where(x => x.Key != null && x.Count() > 1)) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseDuplicateName"), rule.Name, duplicateClause.Key ?? string.Empty), rule, duplicateClause.AsEnumerable().ToArray())); } // If clause label contains illegal characters foreach (var clause in rule.Clauses) { if (clause.Label is string label) { if (label.Contains(' ') || label.Contains('(') || label.Contains(')')) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseInvalidLabel"), rule.Name, label), rule, clause)); } } if (Delegates.ContainsKey(clause.Key)) { foreach (var violation in Delegates[clause.Key].ValidationDelegate.Invoke(rule, clause)) { yield return(violation); } } else { yield return(new Violation(string.Format(Strings.Get("Err_ClauseUnsuppportedOperator_{0}{1}{2}{3}"), rule.Name, clause.Label ?? rule.Clauses.IndexOf(clause).ToString(CultureInfo.InvariantCulture), clause.Operation.ToString(), clause.CustomOperation), rule, clause)); } if (clause.Script is ScriptData clauseScript && !string.IsNullOrEmpty(clauseScript.Code)) { if (!Options.RunScripts) { yield return(new Violation(string.Format(Strings.Get("Err_ScriptingDisabled_{0}{1}"), rule.Name, clause.Label ?? rule.Clauses.IndexOf(clause).ToString(CultureInfo.InvariantCulture)), rule, clause)); } } } if (rule.Expression is string expression) { // Are parenthesis balanced? // Are spaces correct? // Are all variables defined by clauses? // Are variables and operators alternating? var splits = expression.Split(' '); var foundStarts = 0; var foundEnds = 0; var expectingOperator = false; for (var i = 0; i < splits.Length; i++) { foundStarts += splits[i].Count(x => x.Equals('(')); foundEnds += splits[i].Count(x => x.Equals(')')); if (foundEnds > foundStarts) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseUnbalancedParentheses"), expression, rule.Name), rule)); } // Variable if (!expectingOperator) { var lastOpen = -1; var lastClose = -1; for (var j = 0; j < splits[i].Length; j++) { // Check that the parenthesis are balanced if (splits[i][j] == '(') { // If we've seen a ) this is now invalid if (lastClose != -1) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseParenthesisInLabel"), expression, rule.Name, splits[i]), rule)); } // If there were any characters between open parenthesis if (j - lastOpen != 1) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseCharactersBetweenOpenParentheses"), expression, rule.Name, splits[i]), rule)); } // If there was a random parenthesis not starting the variable else if (j > 0) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseCharactersBeforeOpenParentheses"), expression, rule.Name, splits[i]), rule)); } lastOpen = j; } else if (splits[i][j] == ')') { // If we've seen a close before update last if (lastClose != -1 && j - lastClose != 1) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseCharactersBetweenClosedParentheses"), expression, rule.Name, splits[i]), rule)); } lastClose = j; } else { // If we've set a close this is invalid because we can't have other characters // after it if (lastClose != -1) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseCharactersAfterClosedParentheses"), expression, rule.Name, splits[i]), rule)); } } } var variable = splits[i].Replace("(", "").Replace(")", ""); if (variable == "NOT") { if (splits[i].Contains(')')) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseCloseParenthesesInNot"), expression, rule.Name, splits[i]), rule)); } } else { if (string.IsNullOrWhiteSpace(variable) || (!rule.Clauses.Any(x => x.Label == variable) && !(int.TryParse(variable, out var result) && result < rule.Clauses.Count))) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseUndefinedLabel"), expression, rule.Name, splits[i].Replace("(", "").Replace(")", "")), rule)); } expectingOperator = true; } } //Operator else { // If we can't enum parse the operator if (!Enum.TryParse <BoolOperator>(splits[i], out var op)) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseInvalidOperator"), expression, rule.Name, splits[i]), rule)); } // We don't allow NOT operators to modify other Operators, so we can't allow NOT here else { if (op is BoolOperator boolOp && boolOp == BoolOperator.NOT) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseInvalidNotOperator"), expression, rule.Name), rule)); } } expectingOperator = false; } } // We should always end on expecting an operator (having gotten a variable) if (!expectingOperator) { yield return(new Violation(string.Format(Strings.Get("Err_ClauseEndsWithOperator"), expression, rule.Name), rule)); } } }
private void _Invoke(ref object RetObject, params object[] args) { if (args.Length < ArgumentTypes.Length) //check if a argument is using "params x[] args" { throw new Exception("missing arguments"); } List <int> usedDelegates = new List <int>(); PayloadWriter pw = new PayloadWriter(); SmartSerializer serializer = new SmartSerializer(); for (int i = 0; i < args.Length; i++) { object obj = ArgumentTypes[i].IsByRef ? null : args[i]; if (DelegateIndex.ContainsKey(i)) { obj = null; } byte[] SerializedObj = serializer.Serialize(obj); pw.WriteInteger(SerializedObj.Length); pw.WriteBytes(SerializedObj); } for (int i = 0; i < DelegateIndex.Count; i++) { Delegate del = args[DelegateIndex.Keys[i]] as Delegate; if (del != null) { if (del.Method == null) { throw new Exception("Target delegate is NULL"); } int id = rnd.Next(); while (Delegates.ContainsKey(id)) { id = rnd.Next(); } pw.WriteBool(true); SharedDelegate sharedDel = new SharedDelegate(del.Method, sharedClass, del.GetType(), id, del, this.MethodId); sharedDel.sharedMethod.Unchecked = this.Unchecked; //DelegateIndex.Values[i].isUnchecked; sharedDel.sharedMethod.usePacketQueue = this.usePacketQueue; //DelegateIndex.Values[i].UsePacketQueue; sharedDel.sharedMethod.useUdp = this.useUdp; //DelegateIndex.Values[i].UseUDP; pw.WriteObject(sharedDel); if (!isDelegate) { Delegates.Add(id, sharedDel); } continue; } pw.WriteBool(false); } try { if (Unchecked || useUdp) { //just execute the method and don't wait for response sharedClass.Client.Send(new MsgExecuteMethod(0, pw.ToByteArray(), false, sharedClass.SharedId, MethodId, this.DelegateId, this.sharedClass.SharedId)); } else { SyncObject syncObject = null; Random rnd = new Random(); int RequestId = rnd.Next(); lock (sharedClass.Client.Requests) { while (sharedClass.Client.Requests.ContainsKey(RequestId)) { RequestId = rnd.Next(); } syncObject = new SyncObject(sharedClass.Client); sharedClass.Client.Requests.Add(RequestId, syncObject); sharedClass.Client.Send(new MsgExecuteMethod(RequestId, pw.ToByteArray(), true, sharedClass.SharedId, MethodId, this.DelegateId, this.sharedClass.SharedId)); } RetObject = syncObject.Wait <ReturnResult>(null, TimeOutLength); if (syncObject.TimedOut) { //copying the object in memory, maybe a strange way todo it but it works RetObject = new ReturnResult(serializer.Deserialize(serializer.Serialize(this.TimeOutValue)), false); } } } catch { //client most likely disconnected and was unable to send the message RetObject = null; } /*if (callback != null) * { * sharedClass.connection.BeginSendRequest(pw, callback, true, this.usePacketQueue); * } * else * { * if (Unchecked || useUdp) * { * //just don't wait till we received something back since it's a VOID anyway * sharedClass.connection.BeginSendRequest(pw, (object obj) => { }, false, this.usePacketQueue); * } * else * { * RetObject = sharedClass.connection.SendRequest(pw, this.usePacketQueue); * } * }*/ serializer = null; }