Skip to content

Commit

Permalink
biiig fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
emperorofmars committed Nov 29, 2024
1 parent c3b809d commit e208544
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 87 deletions.
8 changes: 2 additions & 6 deletions AVA/UNIVRM0/Editor/Processors/AVA_Avatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AVA_Avatar_UNIVRM0Processor : IGlobalProcessor
{
public const string _Type = "ava.avatar";
public string Type => _Type;
public const uint _Order = 1;
public const uint _Order = NNA_Humanoid_JsonProcessor._Order + 1;
public uint Order => _Order;

public void Process(NNAContext Context)
Expand All @@ -23,11 +23,7 @@ public void Process(NNAContext Context)
var Json = Context.GetOnlyJsonComponentByType("ava.avatar", (new JObject(), null)).Component;
var avatar = AVAUNICRM0Utils.InitAvatarDescriptor(Context);

if(Json.ContainsKey("id"))
{
avatar.name = "$nna:" + (string)Json["id"];
Context.AddResultById((string)Json["id"], avatar);
}
if(Json.ContainsKey("id")) Context.AddResultById((string)Json["id"], avatar);

if(Context.Root.GetComponent<VRMMeta>() == null)
{
Expand Down
8 changes: 2 additions & 6 deletions AVA/VRChat/Editor/Processors/AVA_Avatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AVA_Avatar_VRCProcessor : IGlobalProcessor
{
public const string _Type = "ava.avatar";
public string Type => _Type;
public const uint _Order = 1;
public const uint _Order = NNA_Humanoid_JsonProcessor._Order + 1;
public uint Order => _Order;

public void Process(NNAContext Context)
Expand All @@ -23,11 +23,7 @@ public void Process(NNAContext Context)
var Json = Context.GetOnlyJsonComponentByType("ava.avatar", (new JObject(), null)).Component;
var avatar = AVAVRCUtils.InitAvatarDescriptor(Context);

if(Json.ContainsKey("id"))
{
avatar.name = "$nna:" + (string)Json["id"];
Context.AddResultById((string)Json["id"], avatar);
}
if(Json.ContainsKey("id")) Context.AddResultById((string)Json["id"], avatar);

if(Context.Root.GetComponent<VRCAvatarDescriptor>() == null)
{
Expand Down
27 changes: 9 additions & 18 deletions AVA/VRChat/Editor/Processors/AVA_Collider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class AVA_Collider_VRCNameProcessor : INameProcessor
public const uint _Order = 0;
public uint Order => _Order;

public const string _Match_Sphere = @"(?i)ColSphere(?<inside_bounds>In)(?<radius>[0-9]*[.][0-9]+)(?<side>(([._\-|:][lr])|[._\-|:\s]?(right|left))$)?$";
public const string _Match_Capsule = @"(?i)ColCapsule(?<inside_bounds>In)(?<radius>[0-9]*[.][0-9]+)(?<height>[0-9]*[.][0-9]+)(?<side>(([._\-|:][lr])|[._\-|:\s]?(right|left))$)?$";
public const string _Match_Plane = @"(?i)ColPlane(?<inside_bounds>In)(?<side>(([._\-|:][lr])|[._\-|:\s]?(right|left))$)?$";
public const string _Match_Sphere = @"(?i)ColSphere(?<inside_bounds>In)?(?<radius>[0-9]*[.][0-9]+)(?<side>(([._\-|:][lr])|[._\-|:\s]?(right|left))$)?$";
public const string _Match_Capsule = @"(?i)ColCapsule(?<inside_bounds>In)?(?<radius>[0-9]*[.][0-9]+)(?<height>[0-9]*[.][0-9]+)(?<side>(([._\-|:][lr])|[._\-|:\s]?(right|left))$)?$";
public const string _Match_Plane = @"(?i)ColPlane(?<inside_bounds>In)?(?<side>(([._\-|:][lr])|[._\-|:\s]?(right|left))$)?$";

public int CanProcessName(NNAContext Context, string Name)
{
Expand All @@ -41,11 +41,8 @@ private static void BuildSphereCollider(NNAContext Context, Transform Node, Matc
collider.shapeType = VRC.Dynamics.VRCPhysBoneColliderBase.ShapeType.Sphere;
collider.insideBounds = NameMatch.Groups["inside_bounds"].Success;
collider.radius = float.Parse(NameMatch.Groups["radius"].Value);
if(ParseUtil.GetNameComponentId(Node.name, (uint)NameMatch.Index) is var componentId && componentId != null)
{
Context.AddResultById(componentId, collider);
collider.name = "$nna:" + componentId;
}

Context.AddResultById(ParseUtil.GetNameComponentId(Node.name, NameMatch.Index), collider);
}

private static void BuildCapsuleCollider(NNAContext Context, Transform Node, Match NameMatch)
Expand All @@ -55,23 +52,17 @@ private static void BuildCapsuleCollider(NNAContext Context, Transform Node, Mat
collider.insideBounds = NameMatch.Groups["inside_bounds"].Success;
collider.radius = float.Parse(NameMatch.Groups["radius"].Value);
collider.height = float.Parse(NameMatch.Groups["height"].Value);
if(ParseUtil.GetNameComponentId(Node.name, (uint)NameMatch.Index) is var componentId && componentId != null)
{
Context.AddResultById(componentId, collider);
collider.name = "$nna:" + componentId;
}

Context.AddResultById(ParseUtil.GetNameComponentId(Node.name, NameMatch.Index), collider);
}

private static void BuildPlaneCollider(NNAContext Context, Transform Node, Match NameMatch)
{
var collider = Node.gameObject.AddComponent<VRCPhysBoneCollider>();
collider.shapeType = VRC.Dynamics.VRCPhysBoneColliderBase.ShapeType.Plane;
collider.insideBounds = NameMatch.Groups["inside_bounds"].Success;
if(ParseUtil.GetNameComponentId(Node.name, (uint)NameMatch.Index) is var componentId && componentId != null)
{
Context.AddResultById(componentId, collider);
collider.name = "$nna:" + componentId;
}

Context.AddResultById(ParseUtil.GetNameComponentId(Node.name, NameMatch.Index), collider);
}
}

Expand Down
3 changes: 2 additions & 1 deletion AVA/VRChat/Editor/Processors/AVA_SecondaryMotion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public class AVA_SecondaryMotion_VRCJsonProcessor : IJsonProcessor
public void Process(NNAContext Context, Transform Node, JObject Json)
{
var physbone = Node.gameObject.AddComponent<VRCPhysBone>();
if(Json.ContainsKey("id") && ((string)Json["id"]).Length > 0) physbone.name = (string)Json["id"];

// TODO: Proper conversion from the nna secondary motion values to physbone values

if(Json.ContainsKey("id")) Context.AddResultById((string)Json["id"], physbone);
}
}

Expand Down
14 changes: 3 additions & 11 deletions AVA/VRChat/Editor/Processors/NNA_Twist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public void Process(NNAContext Context, Transform Node, JObject Json)
? ((string)ParseUtil.GetMulkikey(Json, "s", "source")).Contains('&') ? ParseUtil.FindNode(Context.Root.transform, (string)ParseUtil.GetMulkikey(Json, "s", "source"), '&') : ParseUtil.FindNodeNearby(Node, (string)ParseUtil.GetMulkikey(Json, "s", "source"))
: Node.transform.parent.parent;
var converted = CreateVRCTwistBoneConstraint.CreateConstraint(Node, sourceNode, sourceWeight);
if(Json.ContainsKey("id"))
{
Context.AddResultById((string)Json["id"], converted);
converted.name = "$nna:" + (string)Json["id"];
}
if(Json.ContainsKey("id")) Context.AddResultById((string)Json["id"], converted);
}
}

Expand All @@ -52,12 +48,8 @@ public void Process(NNAContext Context, Transform Node, string Name)
? sourceNodeName.Contains('&') ? ParseUtil.FindNode(Context.Root.transform, sourceNodeName, '&') : ParseUtil.FindNodeNearby(Node, sourceNodeName)
: Node.transform.parent.parent;

var constraint = CreateVRCTwistBoneConstraint.CreateConstraint(Node, sourceNode, sourceWeight);
if(ParseUtil.GetNameComponentId(Node.name, startIndex) is var componentId && componentId != null)
{
Context.AddResultById(componentId, constraint);
constraint.name = "$nna:" + componentId;
}
var converted = CreateVRCTwistBoneConstraint.CreateConstraint(Node, sourceNode, sourceWeight);
if(ParseUtil.GetNameComponentId(Node.name, startIndex) is var componentId && componentId != null) Context.AddResultById(componentId, converted);
}
}

Expand Down
6 changes: 3 additions & 3 deletions AVA/VRChat/Editor/Processors/VRC_Physbone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class VRC_Physbone_VRCJsonProcessor : IJsonProcessor
public void Process(NNAContext Context, Transform Node, JObject Json)
{
var physbone = Node.gameObject.AddComponent<VRCPhysBone>();
if(Json.ContainsKey("id") && ((string)Json["id"]).Length > 0) physbone.name = (string)Json["id"];

JsonUtility.FromJsonOverwrite(Json["parsed"].ToString(), physbone);

Expand All @@ -33,15 +32,16 @@ public void Process(NNAContext Context, Transform Node, JObject Json)
physbone.ignoreTransforms.Add(node);
}
}
if(Json.TryGetValue("colliders", out var colliders) && colliders.Type != JTokenType.Array)
/*if(Json.TryGetValue("colliders", out var colliders) && colliders.Type != JTokenType.Array)
{
foreach(string name in colliders)
{
var node = ParseUtil.FindNode(Context.Root.transform, name);
if(node.TryGetComponent<VRCPhysBoneColliderBase>(out var collider))
physbone.colliders.Add(collider);
}
}
}*/
if(Json.ContainsKey("id")) Context.AddResultById((string)Json["id"], physbone);
}
}

Expand Down
26 changes: 17 additions & 9 deletions NNA/Runtime/NNAContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NNAContext
public readonly GameObject Root;
private readonly NNAImportState ImportState;

private readonly Dictionary<string, System.Object> ResultsByID = new();
private readonly Dictionary<string, List<object>> ResultsById = new();

public NNAContext(
GameObject Root,
Expand All @@ -39,22 +39,30 @@ public JObject GetJsonComponentByNode(Transform Node, string TypeName)
return ImportState.JsonComponentByNode[Node].Find(c => (string)c["t"] == TypeName);
}

public void AddResultById(string ID, System.Object Result) { ResultsByID.Add(ID, Result); }
public System.Object GetResultById(string ID) { return ResultsByID.GetValueOrDefault(ID); }
public void AddResultById(string Id, object Result) {
if(!string.IsNullOrWhiteSpace(Id))
{
if(ResultsById.ContainsKey(Id)) ResultsById[Id].Add(Result);
else ResultsById.Add(Id, new List<object> {Result});
}
}
public List<object> GetResultsById(string Id) {
return ResultsById.GetValueOrDefault(Id);
}

public (JObject Component, UnityEngine.Transform Node) GetJsonComponentById(string ID) { return ImportState.JsonComponentsByID.GetValueOrDefault(ID); }
public UnityEngine.Object GetNameComponentById(string ID) { return ImportState.NameComponentsByID.GetValueOrDefault(ID); }
public List<(JObject Component, UnityEngine.Transform Node)> GetJsonComponentByType(string Type) { return ImportState.JsonComponentsByType.GetValueOrDefault(Type, new List<(JObject Component, Transform Node)>()); }
public (JObject Component, UnityEngine.Transform Node) GetOnlyJsonComponentByType(string Type)
public (JObject Component, Transform Node) GetJsonComponentById(string Id) { return ImportState.JsonComponentsById.GetValueOrDefault(Id); }
public Object GetNameComponentById(string Id) { return ImportState.NameComponentsById.GetValueOrDefault(Id); }
public List<(JObject Component, Transform Node)> GetJsonComponentByType(string Type) { return ImportState.JsonComponentsByType.GetValueOrDefault(Type, new List<(JObject Component, Transform Node)>()); }
public (JObject Component, Transform Node) GetOnlyJsonComponentByType(string Type)
{
return GetOnlyJsonComponentByType(Type, (null, null));
}
public (JObject Component, UnityEngine.Transform Node) GetOnlyJsonComponentByType(string Type, (JObject, UnityEngine.Transform) Default)
public (JObject Component, Transform Node) GetOnlyJsonComponentByType(string Type, (JObject, Transform) Default)
{
var list = ImportState.JsonComponentsByType.GetValueOrDefault(Type);
return list.Count == 1 ? list[0] : Default;
}
public List<UnityEngine.Transform> GetNameComponentByType(string Type) { return ImportState.NameComponentsByType.GetValueOrDefault(Type); }
public List<Transform> GetNameComponentByType(string Type) { return ImportState.NameComponentsByType.GetValueOrDefault(Type); }
public ImmutableList<JObject> GetJsonComponentsByNode(Transform Node) { return ImportState.JsonComponentByNode.ContainsKey(Node) ? ImportState.JsonComponentByNode[Node].ToImmutableList() : ImmutableList<JObject>.Empty; }

public bool IsOverridden(string Id) { return ImportState.Overrides.ContainsKey(Id); }
Expand Down
2 changes: 1 addition & 1 deletion NNA/Runtime/NNAConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static ImportResult Convert(
}
if(selectedProcessor != null)
{
State.RegisterNameComponent(node, selectedProcessor.Type, (uint)shortestStartIndex);
State.RegisterNameComponent(node, selectedProcessor.Type, shortestStartIndex);
State.AddProcessorTask(selectedProcessor.Order, new Task(() => {
selectedProcessor.Process(Context, node, node.name);
}));
Expand Down
14 changes: 7 additions & 7 deletions NNA/Runtime/NNAImportState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace nna
{
public struct ImportResult
{
public List<(string Name, UnityEngine.Object Object)> NewObjects;
public List<(UnityEngine.Object Original, UnityEngine.Object New)> Remaps;
public List<(string Name, Object Object)> NewObjects;
public List<(Object Original, Object New)> Remaps;
}

/// <summary>
Expand All @@ -28,8 +28,8 @@ public class NNAImportState
public readonly Dictionary<string, (JObject Json, Transform Node)> Overrides = new();
public readonly Dictionary<string, List<(JObject Component, Transform Node)>> JsonComponentsByType = new();
public readonly Dictionary<string, List<Transform>> NameComponentsByType = new();
public readonly Dictionary<string, (JObject Component, Transform Node)> JsonComponentsByID = new();
public readonly Dictionary<string, Transform> NameComponentsByID = new();
public readonly Dictionary<string, (JObject Component, Transform Node)> JsonComponentsById = new();
public readonly Dictionary<string, Transform> NameComponentsById = new();
public readonly Dictionary<Transform, List<JObject>> JsonComponentByNode = new();

public readonly List<(string, Object)> NewObjects = new();
Expand Down Expand Up @@ -73,17 +73,17 @@ public void AddComponentMap(Transform Node, List<JObject> Components)
foreach(var component in Components)
{
var id = GetID(component);
if(id != null) JsonComponentsByID.Add(id, (component, Node));
if(id != null) JsonComponentsById.Add(id, (component, Node));

var type = GetType(component);
if(JsonComponentsByType.ContainsKey(type)) JsonComponentsByType[type].Add((component, Node));
else JsonComponentsByType.Add(type, new List<(JObject Component, Transform Node)> {(component, Node)});
}
}
public void RegisterNameComponent(Transform Node, string Type, uint DefinitionStartIndex)
public void RegisterNameComponent(Transform Node, string Type, int DefinitionStartIndex)
{
var id = ParseUtil.GetNameComponentId(Node.name, DefinitionStartIndex);
if(id != null) NameComponentsByID.Add(id, Node);
if(id != null && !NameComponentsById.ContainsKey(id)) NameComponentsById.Add(id, Node);
if(NameComponentsByType.ContainsKey(Type)) NameComponentsByType[Type].Add(Node);
else NameComponentsByType.Add(Type, new List<Transform> {Node});
}
Expand Down
12 changes: 8 additions & 4 deletions NNA/Runtime/Processors/NNA_Humanoid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ public class NNA_Humanoid_JsonProcessor : IJsonProcessor
{
public const string _Type = "nna.humanoid";
public string Type => _Type;
public uint Order => 0;
public const uint _Order = 0;
public uint Order => _Order;

public void Process(NNAContext Context, Transform Node, JObject Json)
{
var locomotionType = (string)ParseUtil.GetMulkikeyOrDefault(Json, "planti", "lt", "locomotion_type");
var noJaw = (bool)ParseUtil.GetMulkikeyOrDefault(Json, false, "nj", "no_jaw");

var converted = CreateHumanoidMapping.Create(Context, Node, locomotionType, noJaw);
if(Json.ContainsKey("id")) converted.name = "$nna:" + (string)Json["id"];
if(Json.ContainsKey("id")) Context.AddResultById((string)Json["id"], converted);
}
}

public class NNA_Humanoid_NameProcessor : INameProcessor
{
public const string _Type = "nna.humanoid";
public string Type => _Type;
public uint Order => 0;
public const uint _Order = 0;
public uint Order => _Order;

public const string Match = @"(?i)humanoid(?<digi>digi)?(?<no_jaw>nojaw)?(([._\-|:][lr])|[._\-|:\s]?(right|left))?$";

Expand All @@ -42,7 +44,9 @@ public void Process(NNAContext Context, Transform Node, string Name)
var locomotionType = match.Groups["digi"].Success ? "digi" : "planti";
var noJaw = match.Groups["no_jaw"].Success;

CreateHumanoidMapping.Create(Context, Node, locomotionType, noJaw);
var converted = CreateHumanoidMapping.Create(Context, Node, locomotionType, noJaw);

if(ParseUtil.GetNameComponentId(Node.name, match.Index) is var componentId && componentId != null) Context.AddResultById(componentId, converted);
}
}

Expand Down
16 changes: 4 additions & 12 deletions NNA/Runtime/Processors/NNA_Twist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ public void Process(NNAContext Context, Transform Node, JObject Json)
: Node.transform.parent.parent;
var converted = CreateTwistBoneConstraint.CreateConstraint(Node, sourceNode, sourceWeight);

if(Json.ContainsKey("id"))
{
Context.AddResultById((string)Json["id"], converted);
converted.name = "$nna:" + (string)Json["id"];
}
if(Json.ContainsKey("id")) Context.AddResultById((string)Json["id"], converted);
}
}

Expand All @@ -50,19 +46,15 @@ public void Process(NNAContext Context, Transform Node, string Name)
? sourceNodeName.Contains('&') ? ParseUtil.FindNode(Context.Root.transform, sourceNodeName, '&') : ParseUtil.FindNodeNearby(Node, sourceNodeName)
: Node.transform.parent.parent;
var constraint = CreateTwistBoneConstraint.CreateConstraint(Node, sourceNode, sourceWeight);
if(ParseUtil.GetNameComponentId(Node.name, startIndex) is var componentId && componentId != null)
{
Context.AddResultById(componentId, constraint);
constraint.name = "$nna:" + componentId;
}
if(ParseUtil.GetNameComponentId(Node.name, startIndex) is var componentId && componentId != null) Context.AddResultById(componentId, constraint);
}

public static (string SourceName, float Weight, uint startIndex) ParseName(Transform Node, string Name)
public static (string SourceName, float Weight, int startIndex) ParseName(Transform Node, string Name)
{
var match = Regex.Match(Name, Match);
var sourcePath = match.Groups["source_node_path"].Success ? match.Groups["source_node_path"].Value : null;
var weight = match.Groups["weight"].Success ? float.Parse(match.Groups["weight"].Value) : 0.5f;
return (sourcePath, weight, (uint)match.Index);
return (sourcePath, weight, match.Index);
}
}

Expand Down
4 changes: 1 addition & 3 deletions NNA/Runtime/UnityToNNAUtils/INNASerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ private void RegisterObject(UnityEngine.Object UnityObject)
{
if(!IdMap.ContainsKey(UnityObject))
{
if(UnityObject.name.StartsWith("$nna:")) IdMap.Add(UnityObject, UnityObject.name[5..]);
else IdMap.Add(UnityObject, System.Guid.NewGuid().ToString().Split("-")[0]);
IdMap.Add(UnityObject, UnityObject.name + "_" + System.Guid.NewGuid().ToString().Split("-")[0]);
}
}

public string GetId(UnityEngine.Object UnityObject)
{
if(IdMap.TryGetValue(UnityObject, out var ret)) return ret;
else if(UnityObject.name.StartsWith("$nna:")) return UnityObject.name[5..];
else return UnityObject.name;
}
}
Expand Down
6 changes: 3 additions & 3 deletions NNA/Runtime/Util/ParseUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ public static string ParseNodeRaw(Transform Node, List<Transform> Trash)
}

public const string MatchSideSignifier = @"(?i)(?<side>([._\-|:][lr])|[._\-|:\s]?(right|left))?$";
public static string GetNameComponentId(string NodeName, uint DefinitionStartIndex)
public static string GetNameComponentId(string NodeName, int DefinitionStartIndex)
{
if(DefinitionStartIndex == 0) return null;
if(DefinitionStartIndex <= 0) return null;

var match = Regex.Match(NodeName, MatchSideSignifier);
var sideSignifier = match.Groups["side"].Success ? match.Groups["side"].Value : "";

return NodeName + sideSignifier;
return NodeName[..DefinitionStartIndex] + sideSignifier;
}

public static Transform ResolvePath(Transform Root, Transform NNANode, string Path)
Expand Down
Loading

0 comments on commit e208544

Please sign in to comment.