Skip to content

Commit

Permalink
chore: Updated discord-rpc-csharp to 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lachee committed Jul 27, 2023
1 parent bf9bd52 commit 9491b09
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 136 deletions.
29 changes: 11 additions & 18 deletions Editor/DiscordUserDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
EditorGUI.indentLevel = 0;
{
SerializedProperty foldout = prop.FindPropertyRelative("e_foldout");
SerializedProperty avatar = prop.FindPropertyRelative("_avatar");
SerializedProperty name = prop.FindPropertyRelative("_username");
SerializedProperty discrim = prop.FindPropertyRelative("_discriminator");
SerializedProperty snowflake = prop.FindPropertyRelative("_snowflake");

SerializedProperty cacheSize = prop.FindPropertyRelative("_cacheSize");
SerializedProperty cacheFormat = prop.FindPropertyRelative("_cacheFormat");
SerializedProperty avatarHash = prop.FindPropertyRelative("_avatarHash");
User user = prop.GetSerializedValue() as User;
if (user == null) return;



string displayName = "@" + name.stringValue + "#" + discrim.intValue.ToString("D4");
string displayName = user.ToString();

Rect imageRectangle = new Rect(16, 16, 108, 108);
imageRectangle.position += pos.position;
Expand All @@ -45,7 +38,7 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
//Draw the label then the left over space it gave us
if (foldout.boolValue = EditorGUI.Foldout(new Rect(pos.x, pos.y, pos.width, EditorGUIUtility.singleLineHeight), foldout.boolValue, label))
{
DrawAvatar(imageRectangle, avatar);
DrawAvatar(imageRectangle, user.avatar);

DrawRect(usernameRectangle, Color.green);
DrawRect(snowflakeRectange, Color.white);
Expand All @@ -55,25 +48,25 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)

EditorGUI.LabelField(usernameRectangle, new GUIContent(displayName));

if (snowflake.longValue != 0)
EditorGUI.LabelField(snowflakeRectange, new GUIContent("(" + snowflake.longValue.ToString() + ")"));
if (user.ID != 0)
EditorGUI.LabelField(snowflakeRectange, new GUIContent("(" + user.ID.ToString() + ")"));

EditorGUI.LabelField(cacheSizeRectangle, new GUIContent(cacheSize.intValue + " x " + cacheSize.intValue + ", " + cacheFormat.enumNames[cacheFormat.enumValueIndex]));
EditorGUI.LabelField(avatarHashRectangle, new GUIContent(avatarHash.stringValue));
EditorGUI.LabelField(cacheSizeRectangle, new GUIContent($"{user.cacheSize} x {user.cacheSize}, {user.cacheFormat}"));
EditorGUI.LabelField(avatarHashRectangle, new GUIContent(user.avatarHash));
}
}
EditorGUI.indentLevel = indent;
}

/// <summary>Draws the avatar box </summary>
private void DrawAvatar(Rect position, SerializedProperty avatarProperty)
private void DrawAvatar(Rect position, Texture2D avatarProperty)
{
//Draw the backing colour
EditorGUI.HelpBox(position, "", MessageType.None);

//Draw the avatar if we have one
if (avatarProperty != null && avatarProperty.objectReferenceValue != null)
EditorGUI.DrawTextureTransparent(position, avatarProperty.objectReferenceValue as Texture2D, ScaleMode.ScaleToFit);
if (avatarProperty != null)
EditorGUI.DrawTextureTransparent(position, avatarProperty, ScaleMode.ScaleToFit);
}

private void DrawRect(Rect rect, Color color)
Expand Down
164 changes: 164 additions & 0 deletions Editor/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace Lachee.Discord.Editor
{
public static class SerializedPropertyExtensions
{
/// <summary>
/// Gets the type of the underlying field the SerializedProperty is of.
/// </summary>
/// <param name="property">The property to get the type from</param>
/// <returns></returns>
public static System.Type GetSerializedType(this SerializedProperty property)
{
return property.GetSerializedFieldInfo()?.FieldType;
}

/// <summary>
/// Gets the FieldInfo of the underlying field
/// </summary>
/// <param name="property">The property to get the FieldInfo off</param>
/// <returns></returns>
public static FieldInfo GetSerializedFieldInfo(this SerializedProperty property)
{
System.Type parentType = property.serializedObject.targetObject.GetType();
return parentType.GetFieldInfoFromPath(property.propertyPath);
}

/// <summary>
/// Gets the underlying value this property represents
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
public static object GetSerializedValue(this SerializedProperty property)
{
#if !DISABLE_FAST_SERIALIZED_VALUE_LOOKUP
switch (property.propertyType)
{
default: // If we cant find anything, we should just use the raw .ToString of the value
case SerializedPropertyType.Enum: // Its easier to just lookup the enum properties than recreating it
break;

// Manually get a bunch because its more efficient than looking up serialized values
case SerializedPropertyType.ObjectReference:
return property.objectReferenceValue ? property.objectReferenceValue : null;
case SerializedPropertyType.Boolean:
return property.boolValue;
case SerializedPropertyType.Integer:
return property.intValue;
case SerializedPropertyType.Float:
return property.floatValue;
case SerializedPropertyType.String:
return property.stringValue;
case SerializedPropertyType.Color:
return property.colorValue;
case SerializedPropertyType.Vector2:
return property.vector2Value;
case SerializedPropertyType.Vector3:
return property.vector3Value;
case SerializedPropertyType.Vector4:
return property.vector4Value;
case SerializedPropertyType.Vector2Int:
return property.vector2IntValue;
case SerializedPropertyType.Vector3Int:
return property.vector3IntValue;
case SerializedPropertyType.Quaternion:
return property.quaternionValue;
case SerializedPropertyType.Bounds:
return property.boundsValue;
case SerializedPropertyType.BoundsInt:
return property.boundsIntValue;
case SerializedPropertyType.Rect:
return property.rectValue;
case SerializedPropertyType.RectInt:
return property.rectIntValue;
}
#endif
// Lookup the property path and pull teh value directly
System.Type parentType = property.serializedObject.targetObject.GetType();
return parentType.GetValueFromPath(property.serializedObject.targetObject, property.propertyPath);
}

/// <summary>
/// Finds the field info for the given type at the given path.
/// </summary>
/// <param name="type"></param>
/// <param name="path"></param>
/// <param name="flag"></param>
/// <remarks>Does not work with arrays yet as they would return a PropertyInfo instead</remarks>
/// <returns></returns>
public static FieldInfo GetFieldInfoFromPath(this System.Type type, string path, BindingFlags flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField)
{
System.Type parentType = type;
FieldInfo fi = type.GetField(path, flag);
if (fi != null) return fi;

string[] perDot = path.Split('.');
foreach (string fieldName in perDot)
{
fi = parentType.GetField(fieldName, flag);
if (fi != null)
parentType = fi.FieldType;
else
return null;
}
if (fi != null)
return fi;
else return null;
}

/// <summary>
/// Gets the field values from the given path
/// </summary>
/// <param name="type">The type of the root object</param>
/// <param name="context">The root object to get the value from</param>
/// <param name="path">The SerializedProperty formatted path</param>
/// <param name="flag">The flag used to search fields.</param>
/// <returns></returns>
public static object GetValueFromPath(this System.Type type, object context, string path, BindingFlags flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField)
{
object result = context;
System.Type resultType = type;

// We need to delve deeper until we hit the final result.
string[] segments = path.Split('.');
for (int i = 0; i < segments.Length; i++)
{
// If the field name is an array we need to break apart the next segment to extract its index.
// Once we have the index we can then use the `this` property arrays have to get the appropriate item and
// continue our search through the list of paths.
string fieldName = segments[i];
if (fieldName == "Array")
{
// parse the index
string arrIndexPath = segments[++i];
string arrIndexStr = arrIndexPath.Substring(5, arrIndexPath.Length - 1 - 5);
int arrIndex = int.Parse(arrIndexStr);

// get the property
var thisProperty = resultType.GetProperty("Item", new System.Type[] { arrIndex.GetType() });
var thisGetter = thisProperty.GetMethod;

// Update the current state
result = thisGetter.Invoke(result, new object[] { arrIndex });
resultType = result.GetType();
}
else
{
var fi = resultType.GetField(fieldName, flag);
if (fi == null) return null;

resultType = fi.FieldType;
result = fi.GetValue(result);
}
}

return result;
}
}
}
11 changes: 11 additions & 0 deletions Editor/Extensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions LICENSE.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Runtime/Plugins/DiscordRPC.dll
Binary file not shown.
Binary file modified Runtime/Plugins/DiscordRPC.pdb
Binary file not shown.
22 changes: 16 additions & 6 deletions Runtime/Plugins/DiscordRPC.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Runtime/Presence/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public sealed class Asset
/// The key of the image to be displayed.
/// <para>Max 32 Bytes.</para>
/// </summary>
[CharacterLimit(32, enforce = true)]
[Tooltip("The key of the image to be displayed in the large square.")]
[CharacterLimit(256, enforce = true)]
[Tooltip("The key or URL of the image to be displayed in the large square.")]
public string image;

/// <summary>
/// The tooltip of the image.
/// <para>Max 128 Bytes.</para>
/// </summary>
[CharacterLimit(128, enforce = true)]
[Tooltip("The tooltip of the large image.")]
[Tooltip("The tooltip of the image.")]
public string tooltip;

[Tooltip("Snowflake ID of the image.")]
Expand Down
Loading

0 comments on commit 9491b09

Please sign in to comment.