Browse Source

Cleaned up DynamicIL functions

tags/docs-0.9
RogueException 9 years ago
parent
commit
ff1c4f5e32
10 changed files with 26 additions and 16 deletions
  1. +15
    -5
      src/Discord.Net/DynamicIL.cs
  2. +1
    -1
      src/Discord.Net/Models/Channel.cs
  3. +1
    -1
      src/Discord.Net/Models/Color.cs
  4. +1
    -1
      src/Discord.Net/Models/Invite.cs
  5. +1
    -1
      src/Discord.Net/Models/Message.cs
  6. +3
    -3
      src/Discord.Net/Models/Permissions.cs
  7. +1
    -1
      src/Discord.Net/Models/Profile.cs
  8. +1
    -1
      src/Discord.Net/Models/Role.cs
  9. +1
    -1
      src/Discord.Net/Models/Server.cs
  10. +1
    -1
      src/Discord.Net/Models/User.cs

+ 15
- 5
src/Discord.Net/DynamicIL.cs View File

@@ -7,9 +7,9 @@ namespace Discord
{
internal static class DynamicIL
{
public static Action<T, T> CreateCloner<T>()
public static Action<T, T> CreateCopyMethod<T>()
{
var method = new DynamicMethod("CopyFields", null, new[] { typeof(T), typeof(T) }, typeof(T), true);
var method = new DynamicMethod("CopyTo", null, new[] { typeof(T), typeof(T) }, typeof(T), true);
var generator = method.GetILGenerator();
var typeInfo = typeof(T).GetTypeInfo();

@@ -26,14 +26,24 @@ namespace Discord
return method.CreateDelegate(typeof(Action<T, T>)) as Action<T, T>;
}
public static void ForEachField(this TypeInfo typeInfo, Action<FieldInfo> fieldProcessor)
public static void ForEachField(this TypeInfo typeInfo, Action<FieldInfo> func)
{
var baseType = typeInfo.BaseType;
if (baseType != null)
ForEachField(baseType.GetTypeInfo(), fieldProcessor);
baseType.GetTypeInfo().ForEachField(func);

foreach (var field in typeInfo.DeclaredFields.Where(x => !x.IsStatic))
fieldProcessor(field);
func(field);
}
public static void ForEachProperty(this TypeInfo typeInfo, Action<PropertyInfo> func)
{
var baseType = typeInfo.BaseType;
if (baseType != null)
baseType.GetTypeInfo().ForEachProperty(func);

foreach (var prop in typeInfo.DeclaredProperties.Where(x =>
(!x.CanRead || !x.GetMethod.IsStatic) && (!x.CanWrite || !x.SetMethod.IsStatic)))
func(prop);
}
}
}

+ 1
- 1
src/Discord.Net/Models/Channel.cs View File

@@ -14,7 +14,7 @@ namespace Discord
{
public class Channel : IMentionable
{
private readonly static Action<Channel, Channel> _cloner = DynamicIL.CreateCloner<Channel>();
private readonly static Action<Channel, Channel> _cloner = DynamicIL.CreateCopyMethod<Channel>();

private struct Member
{


+ 1
- 1
src/Discord.Net/Models/Color.cs View File

@@ -4,7 +4,7 @@ namespace Discord
{
public class Color : IEquatable<Color>
{
private readonly static Action<Color, Color> _cloner = DynamicIL.CreateCloner<Color>();
private readonly static Action<Color, Color> _cloner = DynamicIL.CreateCopyMethod<Color>();

public static readonly Color Default = PresetColor(0);



+ 1
- 1
src/Discord.Net/Models/Invite.cs View File

@@ -10,7 +10,7 @@ namespace Discord
{
public class Invite
{
private readonly static Action<Invite, Invite> _cloner = DynamicIL.CreateCloner<Invite>();
private readonly static Action<Invite, Invite> _cloner = DynamicIL.CreateCopyMethod<Invite>();

public class ServerInfo
{


+ 1
- 1
src/Discord.Net/Models/Message.cs View File

@@ -24,7 +24,7 @@ namespace Discord

public class Message
{
private readonly static Action<Message, Message> _cloner = DynamicIL.CreateCloner<Message>();
private readonly static Action<Message, Message> _cloner = DynamicIL.CreateCopyMethod<Message>();

private static readonly Regex _userRegex = new Regex(@"<@[0-9]+>");
private static readonly Regex _channelRegex = new Regex(@"<#[0-9]+>");


+ 3
- 3
src/Discord.Net/Models/Permissions.cs View File

@@ -33,7 +33,7 @@ namespace Discord

public class ServerPermissions : Permissions
{
private readonly static Action<ServerPermissions, ServerPermissions> _cloner = DynamicIL.CreateCloner<ServerPermissions>();
private readonly static Action<ServerPermissions, ServerPermissions> _cloner = DynamicIL.CreateCopyMethod<ServerPermissions>();

public static ServerPermissions None { get; } = new ServerPermissions();
public static ServerPermissions All { get; } = new ServerPermissions(Convert.ToUInt32("00000011111100111111110000111111", 2));
@@ -62,7 +62,7 @@ namespace Discord

public class ChannelPermissions : Permissions
{
private readonly static Action<ChannelPermissions, ChannelPermissions> _cloner = DynamicIL.CreateCloner<ChannelPermissions>();
private readonly static Action<ChannelPermissions, ChannelPermissions> _cloner = DynamicIL.CreateCopyMethod<ChannelPermissions>();

public static ChannelPermissions None { get; } = new ChannelPermissions();
public static ChannelPermissions TextOnly { get; } = new ChannelPermissions(Convert.ToUInt32("00000000000000111111110000011001", 2));
@@ -169,7 +169,7 @@ namespace Discord

public class DualChannelPermissions
{
private readonly static Action<DualChannelPermissions, DualChannelPermissions> _cloner = DynamicIL.CreateCloner<DualChannelPermissions>();
private readonly static Action<DualChannelPermissions, DualChannelPermissions> _cloner = DynamicIL.CreateCopyMethod<DualChannelPermissions>();

public ChannelPermissions Allow { get; }
public ChannelPermissions Deny { get; }


+ 1
- 1
src/Discord.Net/Models/Profile.cs View File

@@ -8,7 +8,7 @@ namespace Discord
{
public class Profile
{
private readonly static Action<Profile, Profile> _cloner = DynamicIL.CreateCloner<Profile>();
private readonly static Action<Profile, Profile> _cloner = DynamicIL.CreateCopyMethod<Profile>();

internal DiscordClient Client { get; }



+ 1
- 1
src/Discord.Net/Models/Role.cs View File

@@ -11,7 +11,7 @@ namespace Discord
{
public class Role : IMentionable
{
private readonly static Action<Role, Role> _cloner = DynamicIL.CreateCloner<Role>();
private readonly static Action<Role, Role> _cloner = DynamicIL.CreateCopyMethod<Role>();

internal DiscordClient Client => Server.Client;



+ 1
- 1
src/Discord.Net/Models/Server.cs View File

@@ -14,7 +14,7 @@ namespace Discord
/// <summary> Represents a Discord server (also known as a guild). </summary>
public class Server
{
private readonly static Action<Server, Server> _cloner = DynamicIL.CreateCloner<Server>();
private readonly static Action<Server, Server> _cloner = DynamicIL.CreateCopyMethod<Server>();

internal static string GetIconUrl(ulong serverId, string iconId)
=> iconId != null ? $"{DiscordConfig.ClientAPIUrl}guilds/${serverId}/icons/${iconId}.jpg" : null;


+ 1
- 1
src/Discord.Net/Models/User.cs View File

@@ -11,7 +11,7 @@ namespace Discord
{
public class User
{
private readonly static Action<User, User> _cloner = DynamicIL.CreateCloner<User>();
private readonly static Action<User, User> _cloner = DynamicIL.CreateCopyMethod<User>();

internal static string GetAvatarUrl(ulong userId, string avatarId)
=> avatarId != null ? $"{DiscordConfig.ClientAPIUrl}users/{userId}/avatars/{avatarId}.jpg" : null;


Loading…
Cancel
Save