Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed link ordering & finicky area param popup #94

Merged
merged 14 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions Fushigi.Bfres/Model/Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Ryujinx.Common.Logging;
using System.Reflection.PortableExecutable;
using System.Numerics;
using Ryujinx.Common.Collections;
using System.Diagnostics;

namespace Fushigi.Bfres
{
Expand All @@ -34,17 +36,37 @@ public void Read(BinaryReader reader)
Bones = reader.ReadDictionary<Bone>(header.BoneDictionaryOffset, header.BoneArrayOffset);
MatrixToBoneList = reader.ReadCustom(() => reader.ReadUInt16s(num_bone_indices), header.MatrixToBoneListOffset);

foreach (var bone in Bones.Values)
{
bone.WorldMatrix = bone.CalculateWorldMatrix(this);

Matrix4x4.Invert(bone.WorldMatrix, out Matrix4x4 inv);
bone.InverseMatrix = bone.WorldMatrix * inv;
}
CalculateMatrices(true);

//return
reader.SeekBegin(pos);
}

public void RecalculateMatrices() => CalculateMatrices(false);

private void CalculateMatrices(bool calculateInverse)
{
for (int i = 0; i < Bones.Count; i++)
{
var bone = Bones[i];
var localMatrix = bone.CalculateLocalMatrix();

bone.WorldMatrix = localMatrix;

if (bone.ParentIndex != -1)
{
Debug.Assert(bone.ParentIndex < i);
var parent = Bones[bone.ParentIndex];
bone.WorldMatrix *= parent.WorldMatrix;
}

if (calculateInverse)
{
Matrix4x4.Invert(bone.WorldMatrix, out Matrix4x4 inv);
bone.InverseMatrix = inv;
}
}
}
}

public class Bone : IResData
Expand Down Expand Up @@ -102,16 +124,6 @@ public void Read(BinaryReader reader)
Name = reader.ReadStringOffset(header.NameOffset);
}

public Matrix4x4 CalculateWorldMatrix(Skeleton skeleton)
{
var matrix = CalculateLocalMatrix();

if (this.ParentIndex != -1)
return matrix * skeleton.Bones[this.ParentIndex].CalculateWorldMatrix(skeleton);

return matrix;
}

public Matrix4x4 CalculateLocalMatrix()
{
Matrix4x4 transMatrix = Matrix4x4.CreateTranslation(this.Position);
Expand All @@ -130,7 +142,6 @@ public Matrix4x4 CalculateLocalMatrix()
Matrix4x4.CreateRotationZ(this.Rotate.Z);
}
return scaleMatrix * rotMatrix * transMatrix;

}
public enum BoneFlagsRotation : uint
{
Expand Down
192 changes: 108 additions & 84 deletions Fushigi/actor_pack/ActorPack.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Fushigi.actor_pack.components;
using Fasterflect;
using Fushigi.actor_pack.components;
using Fushigi.Bfres;
using Fushigi.Byml.Serializer;
using Fushigi.gl.Bfres;
Expand Down Expand Up @@ -59,19 +60,21 @@ public ActorPack(string path)

public string GetModelName()
{
if (ModelInfoRef != null) return ModelInfoRef.mModelName;
if (DrawArrayModelInfoRef != null) return DrawArrayModelInfoRef.mModelName;
return ModelInfoRef?.mModelName ?? DrawArrayModelInfoRef?.mModelName ?? "";
// if (ModelInfoRef != null) return ModelInfoRef.mModelName;
// if (DrawArrayModelInfoRef != null) return DrawArrayModelInfoRef.mModelName;

return "";
// return "";
}


public string GetModelFileName()
{
if (ModelInfoRef != null) return ModelInfoRef.mFilePath;
if (DrawArrayModelInfoRef != null) return DrawArrayModelInfoRef.mFilePath;
return ModelInfoRef?.mFilePath ?? DrawArrayModelInfoRef?.mFilePath ?? "";
// if (ModelInfoRef != null) return ModelInfoRef.mFilePath;
// if (DrawArrayModelInfoRef != null) return DrawArrayModelInfoRef.mFilePath;

return "";
// return "";
}

private void Load(string path)
Expand All @@ -90,28 +93,25 @@ private void Load(string path)
if(paramInfo.Components != null)
paramTree.Add(GetPathGyml(paramInfo.parent ?? "root"), paramInfo);
}

foreach (var param in paramTree)
foreach (var param in paramTree.Where(x => !paramTree.ContainsKey(x.Value.path)))
{
if(param.Key == "root")
{
LoadComponents(sarc, param.Value);
LoadComponents(sarc, param.Value);

if (!string.IsNullOrEmpty(param.Value.Category))
this.Category = param.Value.Category;
if (!string.IsNullOrEmpty(param.Value.Category))
this.Category = param.Value.Category;

var parFile = param.Value.path;
while(paramTree.ContainsKey(parFile))
{
var parent = paramTree[parFile];
var parFile = param.Key;
while(parFile != "root")
{
var parent = paramTree.First(x => GetPathGyml(x.Value.path) == parFile);

LoadComponents(sarc, parent);
LoadComponents(sarc, parent.Value);

if (!string.IsNullOrEmpty(parent.Category))
this.Category = parent.Category;
if (!string.IsNullOrEmpty(parent.Value.Category))
this.Category = parent.Value.Category;

parFile = parent.path;
}
parFile = parent.Key;
}
}

Expand All @@ -135,101 +135,125 @@ private void LoadComponents(SARC.SARC sarc, ActorParam param)
switch (component.Key)
{
case "DrawArrayModelInfoRef":
if(DrawArrayModelInfoRef == null)
if(this.DrawArrayModelInfoRef == null)
this.DrawArrayModelInfoRef = BymlSerialize.Deserialize<ModelInfo>(data);
else{
var child = BymlSerialize.Deserialize<ModelInfo>(data);
foreach(var v in DrawArrayModelInfoRef.GetType().GetProperties())
var par = BymlSerialize.Deserialize<ModelInfo>(data);
foreach(var v in this.DrawArrayModelInfoRef.GetType().GetProperties())
{
if (v.GetValue(child) != null && v.GetValue(child) != default)
v.SetValue(DrawArrayModelInfoRef, v.GetValue(child));
if (v.GetValue(this.DrawArrayModelInfoRef) == null ||
(v.PropertyType == Vector3.Zero.GetType() && (Vector3)(v.GetValue(this.DrawArrayModelInfoRef) ?? Vector3.Zero) == Vector3.Zero))
v.SetValue(this.DrawArrayModelInfoRef, v.GetValue(par));
}
}
break;

while (!string.IsNullOrEmpty(this.DrawArrayModelInfoRef.parent))
{
var file = GetPathGyml(this.DrawArrayModelInfoRef.parent);
data = sarc.OpenFile(file);
var par = BymlSerialize.Deserialize<ModelInfo>(data);
foreach(var v in this.DrawArrayModelInfoRef.GetType().GetProperties())
{
if (v.GetValue(this.DrawArrayModelInfoRef) == null ||
(v.PropertyType == Vector3.Zero.GetType() && (Vector3)(v.GetValue(this.DrawArrayModelInfoRef) ?? Vector3.Zero) == Vector3.Zero))
v.SetValue(this.DrawArrayModelInfoRef, v.GetValue(par));
}
this.DrawArrayModelInfoRef.parent = par.parent;
}
break;
case "ModelInfoRef":
if(ModelInfoRef == null)
if(this.ModelInfoRef == null)
this.ModelInfoRef = BymlSerialize.Deserialize<ModelInfo>(data);
else{
var child = BymlSerialize.Deserialize<ModelInfo>(data);
var par = BymlSerialize.Deserialize<ModelInfo>(data);
foreach(var v in this.ModelInfoRef.GetType().GetProperties())
{
if (v.GetValue(this.ModelInfoRef) == null ||
(v.PropertyType == Vector3.Zero.GetType() && (Vector3)(v.GetValue(this.ModelInfoRef) ?? Vector3.Zero) == Vector3.Zero))
v.SetValue(this.ModelInfoRef, v.GetValue(par));
}
}

while (!string.IsNullOrEmpty(this.ModelInfoRef.parent))
{
var file = GetPathGyml(ModelInfoRef.parent);
data = sarc.OpenFile(file);
var par = BymlSerialize.Deserialize<ModelInfo>(data);
foreach(var v in ModelInfoRef.GetType().GetProperties())
{

if (v.GetValue(child) != null && v.GetValue(child) != default)
v.SetValue(ModelInfoRef, v.GetValue(child));
if (v.GetValue(this.ModelInfoRef) == null ||
(v.PropertyType == Vector3.Zero.GetType() && (Vector3)(v.GetValue(this.ModelInfoRef) ?? Vector3.Zero) == Vector3.Zero))
v.SetValue(this.ModelInfoRef, v.GetValue(par));
}
this.ModelInfoRef.parent = par.parent;
}
break;
break;
case "ModelExpandRef":
this.ModelExpandParamRef = BymlSerialize.Deserialize<ModelExpandParam>(data);
break;
this.ModelExpandParamRef ??= BymlSerialize.Deserialize<ModelExpandParam>(data);
break;
case "DrainPipeRef":
this.DrainPipeRef = BymlSerialize.Deserialize<DrainPipe>(data);
break;
this.DrainPipeRef ??= BymlSerialize.Deserialize<DrainPipe>(data);
break;
case "GamePhysicsRef":
this.GamePhysicsRef = BymlSerialize.Deserialize<GamePhysics>(data);
if(!string.IsNullOrEmpty(GamePhysicsRef.mPath))
ShapeParams = GetActorShape(sarc, data, filePath);
break;
if(!string.IsNullOrEmpty(this.GamePhysicsRef.mPath))
this.ShapeParams ??= GetActorShape(sarc);
break;
case "BgUnitInfo":
this.BgUnitInfo = BymlSerialize.Deserialize<BgUnitInfo>(data);
break;
break;
}
}
}

private ShapeParamList GetActorShape(SARC.SARC sarc, byte[] data, string filePath)
private ShapeParamList GetActorShape(SARC.SARC sarc)
{
filePath = GetPathGyml(GamePhysicsRef.mPath);
data = sarc.OpenFile(filePath);
ControllerPath = BymlSerialize.Deserialize<ControllerSetParam>(data);

while (!string.IsNullOrEmpty(ControllerPath.parent) &&
(ControllerPath.ShapeNamePathAry == null ||
(ControllerPath.mRigids == null && ControllerPath.mEntity == null)))

var file = GetPathGyml(GamePhysicsRef.mPath);
var dat = sarc.OpenFile(file);
this.ControllerPath = BymlSerialize.Deserialize<ControllerSetParam>(dat);

while (!string.IsNullOrEmpty(this.ControllerPath.parent) &&
(this.ControllerPath.ShapeNamePathAry == null ||
((this.ControllerPath.mRigids?.Count ?? this.ControllerPath.mEntity?.Count ?? this.ControllerPath.mSensor?.Count ?? 0) == 0)))
{
filePath = GetPathGyml(ControllerPath.parent);
data = sarc.OpenFile(filePath);
var par = BymlSerialize.Deserialize<ControllerSetParam>(data);
file = GetPathGyml(this.ControllerPath.parent);
dat = sarc.OpenFile(file);
var par = BymlSerialize.Deserialize<ControllerSetParam>(dat);
foreach(var v in ControllerPath.GetType().GetProperties())
{
if (v.GetValue(ControllerPath) == null && v.GetValue(par) != null)
v.SetValue(ControllerPath, v.GetValue(par));
v.SetValue(this.ControllerPath, v.GetValue(this.ControllerPath) ?? v.GetValue(par));
}
ControllerPath.parent = par.parent;
this.ControllerPath.parent = par.parent;
}

if(ControllerPath.ShapeNamePathAry != null &&
(ControllerPath.mRigids != null || ControllerPath.mEntity != null))
var shapes = this.ControllerPath.ShapeNamePathAry ?? [];
var rigidBodies = (this.ControllerPath.mRigids ?? [])
.Concat((this.ControllerPath.mEntity ?? [])
.Concat(this.ControllerPath.mSensor ?? []));

foreach(var rigid in rigidBodies)
{
var shapes = ControllerPath.ShapeNamePathAry;
var rigidBodies = (ControllerPath.mRigids ?? new()).Concat(ControllerPath.mEntity ?? new());
file = GetPathGyml(rigid.FilePath);
dat = sarc.OpenFile(file);
var body = BymlSerialize.Deserialize<RigidParam>(dat);

foreach(var rigid in rigidBodies)
while (!string.IsNullOrEmpty(body.parent) &&
string.IsNullOrEmpty(body.ShapeName) && (body.ShapeNames?.Count ?? 0) == 0)
{
filePath = GetPathGyml(rigid.FilePath);
data = sarc.OpenFile(filePath);
var body = BymlSerialize.Deserialize<RigidParam>(data);
file = GetPathGyml(body.parent);
dat = sarc.OpenFile(file);
body = BymlSerialize.Deserialize<RigidParam>(dat);
}

foreach(var shape in shapes)
foreach(var shape in shapes)
{
if(((body.ShapeName ?? "") == shape.Name || (body.ShapeNames?.Cast<string>() ?? []).Contains(shape.Name)) &&
shape.FilePath != null)
{
if(body.ShapeName != null)
{
if(body.ShapeName == shape.Name && shape.FilePath != null)
{
filePath = GetPathGyml(shape.FilePath);
data = sarc.OpenFile(filePath);
return BymlSerialize.Deserialize<ShapeParamList>(data);
}
}
else if(body.ShapeNames != null)
{
if(body.ShapeNames.Cast<string>().Contains(shape.Name) && shape.FilePath != null)
{
filePath = GetPathGyml(shape.FilePath);
data = sarc.OpenFile(filePath);
return BymlSerialize.Deserialize<ShapeParamList>(data);
}
}
file = GetPathGyml(shape.FilePath);
dat = sarc.OpenFile(file);
return BymlSerialize.Deserialize<ShapeParamList>(dat);
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions Fushigi/actor_pack/components/GamePhysics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ public class ControllerSetParam
[BymlProperty("$parent")]
public string parent { get; set; }

public List<ShapeName> ShapeNamePathAry { get; set; }
public List<PathAry> ShapeNamePathAry { get; set; }

[BymlProperty("MatterRigidBodyNamePathAry", DefaultValue = "")]
public List<ShapeName> mRigids { get; set; }
public List<PathAry> mRigids { get; set; }

[BymlProperty("RigidBodyEntityNamePathAry", DefaultValue = "")]
public List<ShapeName> mEntity { get; set; }
public List<PathAry> mEntity { get; set; }

[BymlProperty("RigidBodySensorNamePathAry", DefaultValue = "")]
public List<PathAry> mSensor { get; set; }
}

[Serializable]
public class ShapeName
public class PathAry
{
public string FilePath { get; set; }
public string Name { get; set; }
Expand Down Expand Up @@ -59,6 +62,8 @@ public class ShapeParamList
[Serializable]
public class RigidParam
{
[BymlProperty("$parent")]
public string parent { get; set; }
public string ShapeName { get; set; }

public List<object> ShapeNames { get; set; }
Expand Down
Loading
Loading