Skip to content

Commit

Permalink
Property drawer for certificate settings
Browse files Browse the repository at this point in the history
  • Loading branch information
aallbrig committed Dec 25, 2023
1 parent ca18473 commit 9de3224
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
70 changes: 65 additions & 5 deletions Assets/Mirror/Transports/SSL/CertificateSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Security.Cryptography.X509Certificates;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;

namespace Mirror
{
Expand All @@ -13,7 +14,7 @@ public class CertificateSettings
public string CertificatePath = "";

[Tooltip("Is the certificate private key file password protected? (default: false)")]
public bool CertificatePasswordProtected;
public bool PasswordProtected;

[Tooltip(
"Path to a file that just contains the password for the certificate file (pass in either a relative path or an absolute path)")]
Expand All @@ -23,7 +24,7 @@ public X509Certificate2 Certificate
{
get
{
if (CertificatePasswordProtected)
if (PasswordProtected)
{
return NewPasswordProtectedCertificate();
}
Expand Down Expand Up @@ -97,8 +98,67 @@ private static bool ValidateCertificatePath(string certificatePath)
}

#if UNITY_EDITOR
// [CustomPropertyDrawer(typeof(CertificateSettings))]
public class CertificateSettingsDrawer: PropertyDrawer
{}
[CustomPropertyDrawer(typeof(CertificateSettings))]
public class CertificateSettingsDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);

position.height = EditorGUIUtility.singleLineHeight;
EditorGUI.LabelField(position, label);

position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

int originalIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel++;

Rect certPathRect = new Rect(position.x, position.y, position.width - 60, EditorGUIUtility.singleLineHeight); // Reduce width for button
Rect certBrowseRect = new Rect(position.x + position.width - 60, position.y, 60, EditorGUIUtility.singleLineHeight); // Button rect
Rect certPasswordProtectedRect = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUIUtility.singleLineHeight);

SerializedProperty certPath = property.FindPropertyRelative(nameof(CertificateSettings.CertificatePath));
EditorGUI.PropertyField(certPathRect, certPath);

if (GUI.Button(certBrowseRect, "Browse"))
{
string path = EditorUtility.OpenFilePanel("Select Certificate File", "", "pfx,crt,cert,pem");
if (!string.IsNullOrEmpty(path))
{
certPath.stringValue = path;
}
}

SerializedProperty certPasswordProtected = property.FindPropertyRelative(nameof(CertificateSettings.PasswordProtected));
EditorGUI.PropertyField(certPasswordProtectedRect, certPasswordProtected);

if (certPasswordProtected.boolValue)
{
position.y += (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) * 2;
Rect passwordFilePathRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
SerializedProperty passwordFilePath = property.FindPropertyRelative(nameof(CertificateSettings.PasswordFilePath));
EditorGUI.PropertyField(passwordFilePathRect, passwordFilePath);
Rect passwordBrowseRect = new Rect(position.x + position.width - 60, position.y, 60, EditorGUIUtility.singleLineHeight); // Button rect
if (GUI.Button(passwordBrowseRect, "Browse"))
{
string path = EditorUtility.OpenFilePanel("Select Password File", "", "");
if (!string.IsNullOrEmpty(path))
{
passwordFilePath.stringValue = path;
}
}
}

EditorGUI.indentLevel = originalIndent;

EditorGUI.EndProperty();
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
bool certPasswordProtected = property.FindPropertyRelative(nameof(CertificateSettings.PasswordProtected)).boolValue;
return certPasswordProtected ? (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) * 4 : (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) * 3;
}
}
#endif
}
6 changes: 0 additions & 6 deletions Assets/Mirror/Transports/SSL/SSLSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,4 @@ public Stream CreateStream(NetworkStream stream, X509Certificate2 certificate)
return true;
}
}

#if UNITY_EDITOR
// [CustomPropertyDrawer(typeof(SSLSettings))]
public class SSLSettingsDrawer: PropertyDrawer
{}
#endif
}

0 comments on commit 9de3224

Please sign in to comment.