Skip to content

Commit

Permalink
chore: allow regex for OSC Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Nov 28, 2024
1 parent 71f6c3a commit 07c5bc7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 38 deletions.
81 changes: 69 additions & 12 deletions Editor/AssetDescription.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using nadena.dev.ndmf.localization;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -60,8 +61,51 @@ internal class AssetDescription : ScriptableObject
class AssetDescriptionData
{
public HashSet<Type> meaninglessComponents = new();
public HashSet<OscParameter> parametersReadByExternalTools = new();
public HashSet<OscParameter> parametersChangedByExternalTools = new();
public OscParameterInfo parametersReadByExternalTools = OscParameterInfo.New();
public OscParameterInfo parametersChangedByExternalTools = OscParameterInfo.New();
}

internal struct OscParameterInfo
{
public HashSet<string> ExactMatch;
public List<Regex> RegexMatch;

public static OscParameterInfo New()
{
return new OscParameterInfo
{
ExactMatch = new HashSet<string>(),
RegexMatch = new List<Regex>(),
};
}

public void Add(OscParameter parameter, AssetDescription desc)
{
if (parameter.name == "") return;
switch (parameter.matchMode)
{
case OscParameter.MatchMode.Exact:
ExactMatch.Add(parameter.name);
break;
case OscParameter.MatchMode.Regex:
try
{
_ = new Regex($"{parameter.name}", RegexOptions.CultureInvariant | RegexOptions.Compiled);
var regex = new Regex($"^(?:{parameter.name})$", RegexOptions.CultureInvariant | RegexOptions.Compiled);
RegexMatch.Add(regex);
}
catch (Exception e)
{
Debug.LogException(
new ArgumentException(
$"Invalid regex: {parameter.name} in asset description {desc.name}", e), desc);
}

break;
default:
throw new ArgumentOutOfRangeException();
}
}
}

static AssetDescriptionData LoadData()
Expand All @@ -73,13 +117,12 @@ static AssetDescriptionData LoadData()
if (GetMonoScriptFromGuid(component.guid, component.fileID) is MonoScript monoScript)
data.meaninglessComponents.Add(monoScript.GetClass());

data.parametersReadByExternalTools.UnionWith(description.parametersReadByExternalTools);
data.parametersChangedByExternalTools.UnionWith(description.parametersChangedByExternalTools);
foreach (var parameter in description.parametersReadByExternalTools)
data.parametersReadByExternalTools.Add(parameter, description);
foreach (var parameter in description.parametersChangedByExternalTools)
data.parametersChangedByExternalTools.Add(parameter, description);
}

data.parametersReadByExternalTools.RemoveWhere(x => x.name == "");
data.parametersChangedByExternalTools.RemoveWhere(x => x.name == "");

return data;
}

Expand All @@ -95,8 +138,8 @@ private static IEnumerable<AssetDescription> GetAllAssetDescriptions()

public static void Reload() => _data = LoadData();
public static HashSet<Type> GetMeaninglessComponents() => Data.meaninglessComponents;
public static HashSet<OscParameter> GetParametersReadByExternalTools() => Data.parametersReadByExternalTools;
public static HashSet<OscParameter> GetParametersChangedByExternalTools() => Data.parametersChangedByExternalTools;
public static OscParameterInfo GetParametersReadByExternalTools() => Data.parametersReadByExternalTools;
public static OscParameterInfo GetParametersChangedByExternalTools() => Data.parametersChangedByExternalTools;

private static Object GetMonoScriptFromGuid(string guid, ulong fileid)
{
Expand Down Expand Up @@ -246,9 +289,7 @@ public struct OscParameter : IEquatable<OscParameter>
public enum MatchMode
{
Exact,
Prefix,
Suffix,
Contains,
Regex,
}

public bool Equals(OscParameter other) => name == other.name && matchMode == other.matchMode;
Expand Down Expand Up @@ -278,13 +319,29 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
else
width = rect.width / 2 - 0.5f;

var color = GUI.color;
if (matchModeProperty.enumValueIndex == (int)OscParameter.MatchMode.Regex)
{
try
{
_ = new Regex($"{nameProperty.stringValue}", RegexOptions.CultureInvariant | RegexOptions.Compiled);
_ = new Regex($"^(?:{nameProperty.stringValue})$", RegexOptions.CultureInvariant | RegexOptions.Compiled);
}
catch
{
GUI.color = Color.red;
}
}

text = rect with { width = rect.width - width - 1 };
popup = rect with { x = rect.xMax - width, width = width };

EditorGUI.BeginChangeCheck();
var newName = EditorGUI.TextField(text, nameProperty.stringValue);
if (EditorGUI.EndChangeCheck())
nameProperty.stringValue = newName;

GUI.color = color;

EditorGUI.BeginChangeCheck();
var newMatchMode = (OscParameter.MatchMode)EditorGUI.EnumPopup(popup, (OscParameter.MatchMode)matchModeProperty.enumValueIndex);
Expand Down
28 changes: 2 additions & 26 deletions Editor/Processors/TraceAndOptimize/ComponentDependencyCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,36 +136,12 @@ private static Predicate<string> GetRootAnimatorParameters(GameObject rootGameOb

// OSC and other External Tools Parameters
var externalParameters = AssetDescription.GetParametersReadByExternalTools();
var prefixes = new HashSet<string>();
var suffixes = new HashSet<string>();
var substrings = new HashSet<string>();
foreach (var externalParameter in externalParameters)
{
switch (externalParameter.matchMode)
{
case AssetDescription.OscParameter.MatchMode.Exact:
parameters.Add(externalParameter.name);
break;
case AssetDescription.OscParameter.MatchMode.Prefix:
prefixes.Add(externalParameter.name);
break;
case AssetDescription.OscParameter.MatchMode.Suffix:
suffixes.Add(externalParameter.name);
break;
case AssetDescription.OscParameter.MatchMode.Contains:
substrings.Add(externalParameter.name);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
parameters.UnionWith(externalParameters.ExactMatch);

return parameter =>
{
return parameters.Contains(parameter) ||
prefixes.Any(prefix => parameter.StartsWith(prefix, StringComparison.Ordinal)) ||
suffixes.Any(suffix => parameter.EndsWith(suffix, StringComparison.Ordinal)) ||
substrings.Any(substring => parameter.Contains(substring, StringComparison.Ordinal));
externalParameters.RegexMatch.Any(regex => regex.IsMatch(parameter));
};
}

Expand Down

0 comments on commit 07c5bc7

Please sign in to comment.