Skip to content

Commit

Permalink
Refactor and implement playlist import from VideoTXL
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed Feb 15, 2024
1 parent 74290a1 commit 2397aef
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 55 deletions.
15 changes: 4 additions & 11 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/TrustedUrlUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
using UnityEngine;
using UnityEditor;
using VRC.Core;
using VRC.SDKBase;
using UdonSharp;
using UdonSharpEditor;
using VRC.SDKBase;

namespace JLChnToZ.VRC.VVMW.Editors {
public sealed class TrustedUrlUtils {
public static event Action OnTrustedUrlsReady;
static readonly Dictionary<TrustedUrlTypes, TrustedUrlUtils> instances = new Dictionary<TrustedUrlTypes, TrustedUrlUtils>();
static AsyncLazy getTrustedUrlsTask = UniTask.Lazy(GetTrustedUrlsLazy);
static GUIContent tempContent, warningContent;
static GUIContent warningContent;
readonly Dictionary<string, bool> trustedDomains = new Dictionary<string, bool>();
readonly Dictionary<string, string> messageCache = new Dictionary<string, string>();
readonly HashSet<string> supportedProtocols;
Expand All @@ -40,13 +40,6 @@ static TrustedUrlUtils() {
instances[TrustedUrlTypes.StringUrl] = new TrustedUrlUtils(supportedProtocolsCurl);
}

static GUIContent GetContent(string label, string tooltip = null) {
if (tempContent == null) tempContent = new GUIContent();
tempContent.text = label;
tempContent.tooltip = tooltip;
return tempContent;
}

static GUIContent GetWarningContent(string tooltip) {
if (warningContent == null) {
warningContent = new GUIContent {
Expand Down Expand Up @@ -113,7 +106,7 @@ public static void DrawUrlField(SerializedProperty urlProperty, TrustedUrlTypes
}

public static void DrawUrlField(SerializedProperty urlProperty, TrustedUrlTypes urlTypes, Rect rect, GUIContent content = null) {
if (content == null) content = GetContent(urlProperty.displayName, urlProperty.tooltip);
if (content == null) content = Utils.GetTempContent(urlProperty.displayName, urlProperty.tooltip);
if (urlProperty.propertyType == SerializedPropertyType.Generic) // VRCUrl
urlProperty = urlProperty.FindPropertyRelative("url");
var url = urlProperty.stringValue;
Expand All @@ -122,7 +115,7 @@ public static void DrawUrlField(SerializedProperty urlProperty, TrustedUrlTypes
}

public static string DrawUrlField(string url, TrustedUrlTypes urlType, Rect rect, string propertyLabel = null, string propertyTooltip = null) =>
DrawUrlField(url, urlType, rect, GetContent(propertyLabel, propertyTooltip));
DrawUrlField(url, urlType, rect, Utils.GetTempContent(propertyLabel, propertyTooltip));

public static string DrawUrlField(string url, TrustedUrlTypes urlType, Rect rect, GUIContent content) {
var instnace = instances[urlType];
Expand Down
18 changes: 16 additions & 2 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;

namespace JLChnToZ.VRC.VVMW {
public static class Utils {
static GUIContent tempContent;

public static IEnumerable<T> IterateAllComponents<T>(this Scene scene, bool includeEditorOnly = false) where T : Component {
var pending = new Stack<Transform>();
var components = new List<T>();
Expand Down Expand Up @@ -59,7 +61,19 @@ public static TDelegate ToDelegate<TDelegate>(this MethodInfo method, object tar
Delegate.CreateDelegate(typeof(TDelegate), method, false) :
Delegate.CreateDelegate(typeof(TDelegate), target, method, false)
);


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static GUIContent GetTempContent(SerializedProperty property) =>
GetTempContent(property.displayName, property.tooltip);

public static GUIContent GetTempContent(string text = "", string tooltip = "", Texture2D image = null) {
if (tempContent == null) tempContent = new GUIContent();
tempContent.text = text;
tempContent.tooltip = tooltip;
tempContent.image = image;
return tempContent;
}

#if !NETSTANDARD2_1
// Polyfill for old .NET Framework
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
8 changes: 7 additions & 1 deletion Packages/idv.jlchntoz.vvmw/Editor/Common/YtdlpResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ public static async UniTask DownLoadYtDlpIfNotExists() {
return;
}
}
if (request.isNetworkError) {
if (
#if UNITY_2020_1_OR_NEWER
request.result != UnityWebRequest.Result.Success
#else
request.isNetworkError || request.isHttpError
#endif
) {
if (File.Exists(path)) File.Delete(path);
} else {
File.Move(path, ytdlpPath);
Expand Down
30 changes: 10 additions & 20 deletions Packages/idv.jlchntoz.vvmw/Editor/VVMW/CoreEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace JLChnToZ.VRC.VVMW.Editors {
public class CoreEditor : VVMWEditorBase {
static readonly Dictionary<Type, FieldInfo> controllableTypes = new Dictionary<Type, FieldInfo>();
readonly Dictionary<Core, UdonSharpBehaviour> autoPlayControllers = new Dictionary<Core, UdonSharpBehaviour>();
static GUIContent tempContent;
static readonly string[] materialModeOptions = new [] { "Property Block", "Shared Material", "Cloned Materal" };
static GUIStyle textFieldDropDownTextStyle, textFieldDropDownStyle;
SerializedProperty trustedUrlDomainsProperty;
Expand Down Expand Up @@ -130,7 +129,7 @@ public override void OnInspectorGUI() {
EditorGUILayout.PropertyField(audioLinkProperty);
EditorGUILayout.PropertyField(yttlManagerProperty);
using (new EditorGUILayout.HorizontalScope()) {
showTrustUrlList = EditorGUILayout.Foldout(showTrustUrlList, GetTempContent(
showTrustUrlList = EditorGUILayout.Foldout(showTrustUrlList, Utils.GetTempContent(
"Trusted URL List",
"The list of trusted URL domains from VRChat. This list is for display proper error message when the video URL is not trusted."
), true);
Expand Down Expand Up @@ -171,7 +170,7 @@ void DrawAutoPlayField() {
}
}
var rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
var content = GetTempContent(autoPlayPlayerTypeProperty);
var content = Utils.GetTempContent(autoPlayPlayerTypeProperty);
using (new EditorGUI.PropertyScope(rect, content, autoPlayPlayerTypeProperty))
using (var changed = new EditorGUI.ChangeCheckScope()) {
rect = EditorGUI.PrefixLabel(rect, content);
Expand All @@ -185,7 +184,7 @@ void DrawAutoPlayField() {
}

void DrawPlayerHandlersListHeader(Rect rect) {
GetTempContent("Auto Find");
var tempContent = Utils.GetTempContent("Auto Find");
var miniButtonStyle = EditorStyles.miniButton;
var size = miniButtonStyle.CalcSize(tempContent);
var buttonRect = new Rect(rect.xMax - size.x, rect.y, size.x, rect.height);
Expand All @@ -200,7 +199,7 @@ void DrawPlayerHandlersListHeader(Rect rect) {
}

void DrawAudioSourcesListHeader(Rect rect) {
GetTempContent("Setup Speakers");
var tempContent = Utils.GetTempContent("Setup Speakers");
var miniButtonStyle = EditorStyles.miniButton;
var size = miniButtonStyle.CalcSize(tempContent);
var buttonRect = new Rect(rect.xMax - size.x, rect.y, size.x, rect.height);
Expand Down Expand Up @@ -284,7 +283,7 @@ void DrawScreenList() {
EditorGUIUtility.labelWidth -= 16;
using (new EditorGUILayout.HorizontalScope()) {
screenTargetVisibilityState[i] = EditorGUILayout.Toggle(screenTargetVisibilityState[i], EditorStyles.foldout, GUILayout.Width(13));
EditorGUILayout.PropertyField(targetProperty, GetTempContent($"Video Screen Target {i + 1}"));
EditorGUILayout.PropertyField(targetProperty, Utils.GetTempContent($"Video Screen Target {i + 1}"));
var value = targetProperty.objectReferenceValue;
if (value is GameObject gameObject) {
if (gameObject.TryGetComponent(out Renderer renderer))
Expand Down Expand Up @@ -358,20 +357,20 @@ void DrawScreenList() {
var nameProperty = screenTargetPropertyNamesProperty.GetArrayElementAtIndex(i);
var avProProperty = avProPropertyNamesProperty.GetArrayElementAtIndex(i);
DrawShaderPropertiesField(
nameProperty, GetTempContent("Video Texture Property Name", "The name of the property in material to set the video texture."),
nameProperty, Utils.GetTempContent("Video Texture Property Name", "The name of the property in material to set the video texture."),
selectedShader, materials, ShaderUtil.ShaderPropertyType.TexEnv
);
using (var changed = new EditorGUI.ChangeCheckScope()) {
useST = EditorGUILayout.Toggle(GetTempContent("Use Scale Offset", "Will use scale offset (_Texture_ST) to adjust the texture if it is flipped upside-down."), useST);
useST = EditorGUILayout.Toggle(Utils.GetTempContent("Use Scale Offset", "Will use scale offset (_Texture_ST) to adjust the texture if it is flipped upside-down."), useST);
if (!useST) DrawShaderPropertiesField(
avProProperty, GetTempContent("AVPro Flag Property Name", "If it is using AVPro player, this property value will set to 1, otherwise 0."),
avProProperty, Utils.GetTempContent("AVPro Flag Property Name", "If it is using AVPro player, this property value will set to 1, otherwise 0."),
selectedShader, materials, ShaderUtil.ShaderPropertyType.Float
);
}
}
var textureProperty = screenTargetDefaultTexturesProperty.GetArrayElementAtIndex(i);
var rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
var label = GetTempContent("Default Texture", "The texture to display when no video is playing. Will use the global default texture if it is null.");
var label = Utils.GetTempContent("Default Texture", "The texture to display when no video is playing. Will use the global default texture if it is null.");
using (new EditorGUI.PropertyScope(rect, label, textureProperty))
using (var changed = new EditorGUI.ChangeCheckScope()) {
var texture = textureProperty.objectReferenceValue;
Expand All @@ -383,7 +382,7 @@ void DrawScreenList() {
}
}
using (var changed = new EditorGUI.ChangeCheckScope()) {
var newTarget = EditorGUILayout.ObjectField(GetTempContent("Add Video Screen Target", "Drag renderers, materials, custom render textures, UI raw images here to receive video texture."), null, typeof(UnityObject), true);
var newTarget = EditorGUILayout.ObjectField(Utils.GetTempContent("Add Video Screen Target", "Drag renderers, materials, custom render textures, UI raw images here to receive video texture."), null, typeof(UnityObject), true);
if (changed.changed && newTarget != null) {
if (AppendScreen(
newTarget,
Expand Down Expand Up @@ -567,15 +566,6 @@ static string FindAVProPropertyName(Material material) {
return matchedName;
}

static GUIContent GetTempContent(SerializedProperty property) => GetTempContent(property.displayName, property.tooltip);

static GUIContent GetTempContent(string text, string tooltip = "") {
if (tempContent == null) tempContent = new GUIContent();
tempContent.text = text;
tempContent.tooltip = tooltip;
return tempContent;
}

static void AppendElement(SerializedProperty property, UnityObject value) {
int size = property.arraySize;
property.arraySize++;
Expand Down
4 changes: 1 addition & 3 deletions Packages/idv.jlchntoz.vvmw/Editor/VVMW/EditorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ public abstract class VVMWEditorBase : Editor {
static PackageSelfUpdater selfUpdater;
static Font font;
static GUIStyle versionLabelStyle;
static GUIContent tempContent;

protected virtual void OnEnable() {
if (tempContent == null) tempContent = new GUIContent();
if (selfUpdater == null) {
selfUpdater = new PackageSelfUpdater(GetType().Assembly, listingsID, listingsURL);
selfUpdater.CheckInstallationInBackground();
Expand Down Expand Up @@ -53,7 +51,7 @@ public override void OnInspectorGUI() {
fontStyle = FontStyle.Bold,
font = font,
};
tempContent.text = $"v{selfUpdater.CurrentVersion}";
var tempContent = Utils.GetTempContent($"v{selfUpdater.CurrentVersion}");
var versionSize = versionLabelStyle.CalcSize(tempContent);
GUI.Label(new Rect(bannerRect.xMax - versionSize.x, bannerRect.yMin, versionSize.x, versionSize.y), tempContent, versionLabelStyle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ public class FrontendHandlerEditor : VVMWEditorBase {
SerializedReorderableList targetsPropertyList;
SerializedObject coreSerializedObject;
string[] playListNames;
GUIContent tempContent;

protected override void OnEnable() {
base.OnEnable();
if (tempContent == null) tempContent = new GUIContent();
coreProperty = serializedObject.FindProperty("core");
lockedProperty = serializedObject.FindProperty("locked");
defaultLoopProperty = serializedObject.FindProperty("defaultLoop");
Expand Down Expand Up @@ -66,7 +64,7 @@ public override void OnInspectorGUI() {
if (GUILayout.Button("Edit Play Lists..."))
PlayListEditorWindow.StartEditPlayList(target as FrontendHandler);
var rect = GUILayoutUtility.GetRect(0, EditorGUIUtility.singleLineHeight);
tempContent.text = "Default Play List";
var tempContent = Utils.GetTempContent("Default Play List");
using (new EditorGUI.PropertyScope(rect, tempContent, playListTitlesProperty))
using (var changed = new EditorGUI.ChangeCheckScope()) {
rect = EditorGUI.PrefixLabel(rect, tempContent);
Expand Down
Loading

0 comments on commit 2397aef

Please sign in to comment.