private MethodCallExpression NewDiffReplaced(ParameterExpression ret, Property property, ParameterExpression @base, ParameterExpression changed) { MemberExpression baseProperty = Expression.Property(@base, property.ReflectionPropertyInfo); MemberExpression changedProperty = Expression.Property(changed, property.ReflectionPropertyInfo); return(Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), Expression.New( DiffItemsMembers.NewClassReplaced(property.Type), Expression.Constant(property), baseProperty, changedProperty ) )); }
private Expression <Func <TType, TType, List <IDiffItem> > > Compile() { List <Expression> body = new List <Expression>(); ParameterExpression ret = Expression.Parameter(typeof(List <IDiffItem>), "ret"); ParameterExpression @base = Expression.Parameter(typeof(TType), "base"); ParameterExpression changed = Expression.Parameter(typeof(TType), "changed"); body.Add( Expression.Assign( ret, Expression.New( ListMembers.NewWithCount(typeof(IDiffItem)), Expression.Constant(Class <TType> .Properties.Count) // maximum number of different items ) ) ); foreach (Property property in Class <TType> .Properties) { if (this.aIgnoreProperties.Contains(property)) { continue; } IDiffAlgorithm diff = this.aMergerImplementation.Partial.Algorithms.GetDiffAlgorithm(property.Type); if (diff.IsDirect) { body.Add(this.MightBeReplaced(ret, property, @base, changed)); } else { body.Add(this.MightBeChanged(ret, property, @base, changed)); } } body.Add(ret); return(Expression.Lambda <Func <TType, TType, List <IDiffItem> > >( Expression.Block(new[] { ret }, body), @base, changed )); }
private Expression NewDiffChanged(ParameterExpression ret, Property property, ParameterExpression @base, ParameterExpression changed) { IDiffAlgorithm diff = this.aMergerImplementation.Partial.Algorithms.GetDiffAlgorithm(property.Type); if (diff.IsDirect) { return(this.NewDiffReplaced(ret, property, @base, changed)); } MemberExpression baseProperty = Expression.Property(@base, property.ReflectionPropertyInfo); MemberExpression changedProperty = Expression.Property(changed, property.ReflectionPropertyInfo); ParameterExpression tmp = Expression.Parameter(typeof(IDiff <>).MakeGenericType(property.Type), "tmp"); return(Expression.Block( new[] { tmp }, Expression.Assign( tmp, Expression.Call( Expression.Constant(diff), DiffAlgorithmMembers.Compute(property.Type), baseProperty, changedProperty ) ), Expression.IfThenElse( Expression.NotEqual( Expression.Property(tmp, DiffMembers.Count()), Expression.Constant(0) ), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), Expression.New( DiffItemsMembers.NewClassChanged(property.Type), Expression.Constant(property), tmp ) ), this.NewDiffUnchanged(ret, property, @base) ) )); }
private Expression NewDiffUnchanged(ParameterExpression ret, Property property, ParameterExpression value) { if (!this.aAlwaysIncludedProperties.Contains(property)) { return(Expression.Empty()); } MemberExpression valueProperty = Expression.Property(value, property.ReflectionPropertyInfo); return(Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), Expression.New( DiffItemsMembers.NewClassUnchanged(property.Type), Expression.Constant(property), valueProperty ) )); }
private static Task SaveConfig(ListMembers cfgjson) { // first, let's load our configuration file _ = JsonConvert.SerializeObject(cfgjson.ToString()); //write string to file //await File.OpenWrite("configMAL.json"); using (StreamWriter file = File.CreateText("member.json")) { JsonSerializer serializer = new JsonSerializer(); //serialize object directly into file stream serializer.Serialize(file, cfgjson); } // next, let's load the values from that file // to our client's configuration //var cfgjson = JsonConvert.DeserializeObject<ConfigMAL>(json); return(Task.CompletedTask); }
private Expression <Func <IDiff <TType>, IDiff <TType>, IConflictContainer, List <IDiffItem> > > Compile() { ParameterExpression left = Expression.Parameter(typeof(IDiff <TType>), "left"); ParameterExpression right = Expression.Parameter(typeof(IDiff <TType>), "right"); ParameterExpression conflicts = Expression.Parameter(typeof(IConflictContainer), "ret"); ParameterExpression ret = Expression.Parameter(typeof(List <IDiffItem>), "ret"); ParameterExpression leftEnumerator = Expression.Parameter(typeof(IEnumerator <IDiffItem>), "leftEnumerator"); ParameterExpression leftSomeLeft = Expression.Parameter(typeof(bool), "leftSomeLeft"); ParameterExpression rightEnumerator = Expression.Parameter(typeof(IEnumerator <IDiffItem>), "rightEnumerator"); ParameterExpression rightSomeLeft = Expression.Parameter(typeof(bool), "rightSomeLeft"); return(Expression.Lambda <Func <IDiff <TType>, IDiff <TType>, IConflictContainer, List <IDiffItem> > >( Expression.Block( new[] { ret, leftEnumerator, leftSomeLeft, rightEnumerator, rightSomeLeft }, Expression.Assign( ret, Expression.New( ListMembers.NewWithCount(typeof(IDiffItem)), Expression.Add( Expression.Property(left, DiffMembers.Count()), Expression.Property(right, DiffMembers.Count()) ) ) ), Expression.Assign( leftEnumerator, Expression.Call(left, EnumerableMembers.GetEnumerator(typeof(IDiffItem))) ), Expression.Assign( rightEnumerator, Expression.Call(right, EnumerableMembers.GetEnumerator(typeof(IDiffItem))) ), this.MoveEnumerator(leftEnumerator, leftSomeLeft), this.MoveEnumerator(rightEnumerator, rightSomeLeft), Expression.Block(Class <TType> .Properties.Select(x => this.EvaluateProperty(leftEnumerator, leftSomeLeft, rightEnumerator, rightSomeLeft, conflicts, ret, x))), ret ), left, right, conflicts )); }
//check et affichage par channel des lives private static async Task Check(DiscordClient client) { try { ListMembers members = LoadConfig(); DiscordRole roleMembre = client.Guilds[138283154589876224].GetRole(361927682671378442); foreach (ulong id in members.Members) { try { DiscordMember member = await client.Guilds[138283154589876224].GetMemberAsync(id); if (!member.Roles.Contains(roleMembre)) { if ((DateTime.Now - member.JoinedAt) >= TimeSpan.FromMinutes(10)) { await member.GrantRoleAsync(roleMembre); members.Members.Remove(id); } } else { members.Members.Remove(id); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } await SaveConfig(members); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
private Expression EvaluateProperty(ParameterExpression leftEnumerator, ParameterExpression leftSomeLeft, ParameterExpression rightEnumerator, ParameterExpression rightSomeLeft, Expression conflicts, Expression ret, Property property) { Expression leftCurrent = Expression.Property(leftEnumerator, EnumerableMembers.Current(typeof(IDiffItem))); Expression rightCurrent = Expression.Property(rightEnumerator, EnumerableMembers.Current(typeof(IDiffItem))); /* PSEUDO CODE FOR THIS: * if (leftSomeLeft && leftCurrent.UniqueID == ID) * { * if (rightSomeLeft && rightCurrent.UniqueID == ID) * { * CheckConflicts(leftCurrent, rightCurrent) * rightSomeLeft = next(rightCurrent) * } * else * add(leftCurrent) * leftSomeLeft = next(leftCurrent) * } * else if (rightSomeLeft && rightCurrent.UniqueID == ID) * { * add(rightCurrent) * rightSomeLeft = next(rightCurrent) * } */ Expression leftShouldBeProcessed = Expression.AndAlso( leftSomeLeft, Expression.Equal( Expression.Property( Expression.Property( Expression.Convert( leftCurrent, typeof(IDiffClassItem) ), DiffItemsMembers.ClassProperty() ), FastPropertyMembers.UniqueID() ), Expression.Constant(property.UniqueID) ) ); Expression rightShouldBeProcessed = Expression.AndAlso( rightSomeLeft, Expression.Equal( Expression.Property( Expression.Property( Expression.Convert( rightCurrent, typeof(IDiffClassItem) ), DiffItemsMembers.ClassProperty() ), FastPropertyMembers.UniqueID() ), Expression.Constant(property.UniqueID) ) ); return(Expression.IfThenElse( leftShouldBeProcessed, Expression.Block( Expression.IfThenElse( rightShouldBeProcessed, Expression.Block( this.CompileCheckConflicts(property, leftCurrent, rightCurrent, conflicts, ret), this.MoveEnumerator(rightEnumerator, rightSomeLeft) ), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), leftCurrent ) ), this.MoveEnumerator(leftEnumerator, leftSomeLeft) ), Expression.IfThen( rightShouldBeProcessed, Expression.Block( Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), rightCurrent ), this.MoveEnumerator(rightEnumerator, rightSomeLeft) ) ) )); }
private Expression CompileCheckConflicts(Property property, Expression leftCurrent, Expression rightCurrent, Expression conflicts, Expression ret) { /* PSEUDO CODE FOR THIS: * if (leftCurrent is Unchanged) * { * add(rightCurrent) * } * else if (rightCurrent is Unchanged) * { * add(leftCurrent) * } * else if (leftCurrent is Replace || rightCurrent is Replace) * { * if (leftCurrent == rightCurrent) * add(leftCurrent) * else * { * conflict = Conflict(leftCurrent, rightCurrent) * add(conflict) * conflicts.register(conflict) * } * } * else if (leftCurrent is Changed && rightCurrent is Changed) * { * add(algorithm.Merge(leftCurrent.ValueDiff, rightCurrent.ValueDiff, conflicts)) * } * else * throw */ IMergeDiffsAlgorithm algorithm = this.aMergerImplementation.Partial.Algorithms.GetMergeDiffsAlgorithm(property.Type); Type itemReplacedType = typeof(IDiffItemReplaced <>).MakeGenericType(property.Type); ParameterExpression conflict = Expression.Parameter(typeof(IDiffItemConflicted), "conflict"); return(Expression.IfThenElse( Expression.TypeIs(leftCurrent, typeof(IDiffItemUnchanged)), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), rightCurrent ), Expression.IfThenElse( Expression.TypeIs(rightCurrent, typeof(IDiffItemUnchanged)), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), leftCurrent ), Expression.IfThenElse( Expression.OrElse( Expression.TypeIs(leftCurrent, itemReplacedType), Expression.TypeIs(rightCurrent, itemReplacedType) ), Expression.IfThenElse( Expression.Call( null, ObjectMembers.Equals(), leftCurrent, rightCurrent ), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), leftCurrent ), Expression.Block( new[] { conflict }, Expression.Assign( conflict, Expression.New( DiffItemsMembers.NewConflict(), leftCurrent, rightCurrent ) ), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), conflict ), Expression.Call( conflicts, ConflictContainerMembers.RegisterConflict(), conflict ) ) ), Expression.IfThenElse( Expression.AndAlso( Expression.TypeIs(leftCurrent, typeof(IDiffItemChanged)), Expression.TypeIs(rightCurrent, typeof(IDiffItemChanged)) ), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), Expression.New( DiffItemsMembers.NewClassChanged(property.Type), Expression.Constant(property), Expression.Call( Expression.Constant(algorithm, typeof(IMergeDiffsAlgorithm <>).MakeGenericType(property.Type)), MergeDiffsAlgorithmMembers.MergeDiffs(property.Type), Expression.Property( Expression.Convert(leftCurrent, typeof(IDiffItemChanged <>).MakeGenericType(property.Type)), DiffItemsMembers.ChangedDiff(property.Type) ), Expression.Property( Expression.Convert(rightCurrent, typeof(IDiffItemChanged <>).MakeGenericType(property.Type)), DiffItemsMembers.ChangedDiff(property.Type) ), conflicts ) ) ), Expression.Throw( Expression.New(typeof(Exception)) ) ) ) ) )); }
private Func <TType, TType, List <IDiffItem> > CompileDiff() { ParameterExpression ret = Expression.Parameter(typeof(List <IDiffItem>), "ret"); ParameterExpression @base = Expression.Parameter(typeof(TType), "base"); ParameterExpression changed = Expression.Parameter(typeof(TType), "changed"); Expression differ; if (this.aIDProperty == null) { differ = Expression.IfThen( ExpressionExtensions.NotEqual(@base, changed), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), Expression.New( DiffItemsMembers.NewValueReplaced(typeof(TType)), @base, changed ) ) ); } else { differ = Expression.IfThen( ExpressionExtensions.NotEqual( Expression.Property(@base, this.aIDProperty.ReflectionPropertyInfo), Expression.Property(changed, this.aIDProperty.ReflectionPropertyInfo) ), Expression.Call( ret, ListMembers.Add(typeof(IDiffItem)), Expression.New( DiffItemsMembers.NewValueReplaced(typeof(TType)), @base, changed ) ) ); } if (!typeof(TType).IsValueType) { differ = Expression.IfThenElse( Expression.OrElse( Expression.ReferenceEqual(@base, Expression.Constant(null)), Expression.ReferenceEqual(changed, Expression.Constant(null)) ), Expression.IfThen( Expression.ReferenceEqual(@base, changed), Expression.New( DiffItemsMembers.NewValueReplaced(typeof(TType)), @base, changed ) ), differ ); } Expression <Func <TType, TType, List <IDiffItem> > > comparer = Expression.Lambda <Func <TType, TType, List <IDiffItem> > >( Expression.Block( new[] { ret }, Expression.Assign( ret, Expression.New( ListMembers.NewWithCount(typeof(IDiffItem)), Expression.Constant(1) // maximum number of changes ) ), differ, ret ), @base, changed ); return(comparer.Compile()); }
public static async Task RunBotAsync() { Console.Title = "Zerator Discord"; var cfg = new ConfigJson(); string json; if (!File.Exists("config.json")) { json = JsonConvert.SerializeObject(cfg); File.WriteAllText("config.json", json, new UTF8Encoding(false)); Console.WriteLine("Config file was not found, a new one was generated. Fill it with proper values and rerun this program"); Console.ReadKey(); return; } if (!File.Exists("member.json")) { var cfgProg = new ListMembers(); json = JsonConvert.SerializeObject(cfgProg); File.WriteAllText("member.json", json, new UTF8Encoding(false)); Console.WriteLine("member file was not found, a new one was generated."); } if (!File.Exists("prog.json")) { var cfgProg = new ProgJson(); json = JsonConvert.SerializeObject(cfgProg); File.WriteAllText("prog.json", json, new UTF8Encoding(false)); Console.WriteLine("prog file was not found, a new one was generated."); } if (!File.Exists("twitch.json")) { var cfgTwitch = new StreamerStatus(); json = JsonConvert.SerializeObject(cfgTwitch); File.WriteAllText("twitch.json", json, new UTF8Encoding(false)); Console.WriteLine("twitch file was not found, a new one was generated."); } if (!File.Exists("configMAL.json")) { var cfgTwitch = new Dictionary <string, string>(new Dictionary <string, string>());; json = JsonConvert.SerializeObject(cfgTwitch); File.WriteAllText("configMAL.json", json, new UTF8Encoding(false)); Console.WriteLine("configMAL file was not found, a new one was generated."); } json = File.ReadAllText("config.json", new UTF8Encoding(false)); cfg = JsonConvert.DeserializeObject <ConfigJson>(json); var tskl = new List <Task>(); for (var i = 0; i < cfg.ShardCount; i++) { var bot = new Bot(cfg, i); Shards.Add(bot); tskl.Add(bot.RunAsync()); await Task.Delay(7500).ConfigureAwait(false); } await Task.WhenAll(tskl).ConfigureAwait(false); try { await Task.Delay(-1, CancelToken).ConfigureAwait(false); } catch (Exception) { /* shush */ } }