Skip to content

Commit

Permalink
WIP enhancement on play lists
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed Sep 9, 2023
1 parent f81f419 commit a61055a
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 17 deletions.
114 changes: 103 additions & 11 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/BindEventPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,102 @@ static void ProcessEntry(UnityObject targetObj, MemberInfo member, UnityAction<s
if (targetObj == null) return;
var srcType = targetObj.GetType();
foreach (var attribute in member.GetCustomAttributes<BindEventAttribute>(true))
BindSingleEvent(targetObj, srcType, attribute, call, index);
BindSingleEvent(targetObj, attribute.SourceType ?? srcType, attribute, call, index);
}

static bool TryGetValue(object source, Type srcType, string fieldName, out object result) {
static bool TryGetValue(UnityObject source, Type srcType, string fieldName, out object result) {
int hashIndex = fieldName.IndexOf('#');
if (hashIndex >= 0) {
var pathElements = fieldName.Substring(0, hashIndex).Split('/');
fieldName = fieldName.Substring(hashIndex + 1);
if (source is Transform transform) {}
else if (source is GameObject gameObject)
transform = gameObject.transform;
else if (source is Component component)
transform = component.transform;
else {
result = null;
return false;
}
source = null;
var pending = new Stack<(int, Transform)>();
var children = new Stack<Transform>();
var flattenChildren = new Stack<Transform>();
pending.Push((0, transform));
while (pending.Count > 0) {
var (depth, current) = pending.Pop();
if (depth >= pathElements.Length) {
if (srcType == typeof(GameObject)) {
source = current.gameObject;
break;
} else if (srcType.IsAssignableFrom(current.GetType())) {
source = current;
break;
} else if (current.TryGetComponent(srcType, out var component)) {
source = component;
break;
}
continue;
}
var pathPart = pathElements[depth];
switch (pathPart) {
case "": {
var next = depth == 0 ? current.root : current;
if (next != null) pending.Push((depth + 1, next));
break;
}
case ".": {
pending.Push((depth + 1, current));
break;
}
case "..": {
var parent = current.parent;
if (parent != null) pending.Push((depth + 1, parent));
break;
}
case "..*": {
for (var parent = current; parent != null; parent = parent.parent)
flattenChildren.Push(parent);
while (flattenChildren.Count > 0)
pending.Push((depth + 1, flattenChildren.Pop()));
break;
}
case "*": {
for (int i = current.childCount - 1; i >= 0; i--)
pending.Push((depth + 1, current.GetChild(i)));
break;
}
case "**": {
children.Push(current);
while (children.Count > 0) {
var child = children.Pop();
flattenChildren.Push(child);
for (int i = 0, count = child.childCount; i < count; i++)
children.Push(child.GetChild(i));
}
while (flattenChildren.Count > 0)
pending.Push((depth + 1, flattenChildren.Pop()));
break;
}
default: {
var child = current.Find(pathPart);
if (child != null) pending.Push((depth + 1, child));
break;
}
}
}
} else if (!srcType.IsAssignableFrom(source.GetType())) {
if (source is GameObject gameObject)
source = gameObject.GetComponent(srcType);
else if (source is Component component)
source = component.GetComponent(srcType);
else
source = null;
}
if (source == null) {
result = null;
return false;
}
var otherProp = srcType.GetProperty(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
if (otherProp != null) {
result = otherProp.GetValue(source);
Expand Down Expand Up @@ -162,21 +254,21 @@ static void ValidateType(Type type, BindEventAttribute attribute, FieldInfo fiel
Debug.LogError($"{messagePrefix} Source is empty.", currentScript);
return;
}
int hashIndex = srcName.IndexOf('#');
bool srcIsAPath = hashIndex >= 0;
if (srcIsAPath) srcName = srcName.Substring(hashIndex + 1);
var destName = attribute.Destination;
if (string.IsNullOrEmpty(destName)) {
Debug.LogError($"{messagePrefix} Destination is empty.", currentScript);
return;
}
Type srcType;
if (field != null) {
srcType = field.FieldType;
if (attribute.SourceType != null) Debug.LogWarning($"{messagePrefix} SourceType is ignored because the attribute is on a field.", currentScript);
} else {
srcType = attribute.SourceType;
if (srcType == null) {
var srcType = attribute.SourceType;
if (srcType == null) {
if (field != null)
srcType = field.FieldType;
else
Debug.LogError($"{messagePrefix} SourceType is empty.", currentScript);
return;
}
return;
}
if (srcType.IsArray) srcType = srcType.GetElementType();
var srcProperty = srcType.GetProperty(srcName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
Expand Down
161 changes: 160 additions & 1 deletion Packages/idv.jlchntoz.vvmw/Runtime/VVMW/UIHandler.asset
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ MonoBehaviour:
Data:
- Name:
Entry: 12
Data: 67
Data: 70
- Name:
Entry: 7
Data:
Expand Down Expand Up @@ -3801,6 +3801,165 @@ MonoBehaviour:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: joinTime
- Name: $v
Entry: 7
Data: 216|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: joinTime
- Name: <UserType>k__BackingField
Entry: 7
Data: 217|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.DateTime, mscorlib
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 217
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 218|System.Collections.Generic.List`1[[System.Attribute, mscorlib]],
mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: playListLastInteractTime
- Name: $v
Entry: 7
Data: 219|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: playListLastInteractTime
- Name: <UserType>k__BackingField
Entry: 9
Data: 217
- Name: <SystemType>k__BackingField
Entry: 9
Data: 217
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 220|System.Collections.Generic.List`1[[System.Attribute, mscorlib]],
mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 7
Data:
- Name: $k
Entry: 1
Data: interactCoolDown
- Name: $v
Entry: 7
Data: 221|UdonSharp.Compiler.FieldDefinition, UdonSharp.Editor
- Name: <Name>k__BackingField
Entry: 1
Data: interactCoolDown
- Name: <UserType>k__BackingField
Entry: 7
Data: 222|System.RuntimeType, mscorlib
- Name:
Entry: 1
Data: System.TimeSpan, mscorlib
- Name:
Entry: 8
Data:
- Name: <SystemType>k__BackingField
Entry: 9
Data: 222
- Name: <SyncMode>k__BackingField
Entry: 7
Data: System.Nullable`1[[UdonSharp.UdonSyncMode, UdonSharp.Runtime]], mscorlib
- Name:
Entry: 6
Data:
- Name:
Entry: 8
Data:
- Name: <IsSerialized>k__BackingField
Entry: 5
Data: false
- Name: _fieldAttributes
Entry: 7
Data: 223|System.Collections.Generic.List`1[[System.Attribute, mscorlib]],
mscorlib
- Name:
Entry: 12
Data: 0
- Name:
Entry: 13
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 8
Data:
- Name:
Entry: 13
Data:
Expand Down
Loading

0 comments on commit a61055a

Please sign in to comment.