Skip to content
This repository has been archived by the owner on Feb 14, 2025. It is now read-only.

Added an2 serialization code #104

Merged
merged 1 commit into from
Aug 9, 2024
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
1 change: 1 addition & 0 deletions Mafia2Libs/Core/IO/FileAnimation2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public FileAnimation2(FileInfo info) : base(info)
public override bool Open()
{
Animation2 anim = new(file.FullName);
//anim.WriteToFile(file.FullName + "_test");
return true;
}

Expand Down
20 changes: 12 additions & 8 deletions Mafia2Libs/ResourceTypes/FileTypes/Animations/AnimTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void Quantize(float duration)
Duration = duration;
NumKeyFrames = (short)KeyFrames.Length;

var refQuat = KeyFrames.Length > 0 ? KeyFrames[0].value : Quaternion.Identity;
var refQuat = KeyFrames.Length > 0 ? Quaternion.Inverse(KeyFrames[0].value) : Quaternion.Identity;

PackedReferenceQuat = PackReferenceQuaternion(refQuat);
refQuat = UnpackReferenceQuaternion(PackedReferenceQuat);
Expand Down Expand Up @@ -458,7 +458,7 @@ private float GetOptimalScale(Quaternion invRefQuat)
for (int i = 0; i < KeyFrames.Length; i++)
{
var frame = KeyFrames[i];
var q = invRefQuat * frame.value;
var q = invRefQuat * Quaternion.Inverse(frame.value);
List<float> values = new() { Math.Abs(q.X), Math.Abs(q.Y), Math.Abs(q.Z), Math.Abs(q.W) };
values.Remove(values.Max());
var temp = values.Max();
Expand Down Expand Up @@ -686,8 +686,8 @@ public void Dequantize()

public void Quantize()
{
Scale = ComputeScale();
Scale = Scale > 1.0f ? Scale - 1.0f : 1.0f;
Scale = GetOptimalScale();
Scale = Scale > 0.0f ? Scale : 1.0f;

var data = new BigInteger();
var chunkSize = 96;
Expand Down Expand Up @@ -717,16 +717,20 @@ public void Quantize()
Data = data.ToByteArray();
}

public float ComputeScale()
public float GetOptimalScale()
{
float scale = 0.0f;

foreach (var frame in KeyFrames)
for (int i = 0; i < KeyFrames.Length; i++)
{
scale += Math.Abs(frame.value.X) + Math.Abs(frame.value.Y) + Math.Abs(frame.value.Z);
var frame = KeyFrames[i];
var v = frame.value;
List<float> values = new() { Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z) };
var temp = values.Max();
scale = temp > scale ? temp : scale;
}

return (float)Math.Round(scale * 10 / KeyFrames.Length);
return scale;
}
}
}
62 changes: 62 additions & 0 deletions Mafia2Libs/ResourceTypes/FileTypes/Animations/Animation2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ResourceTypes.ModelHelpers.ModelExporter;
using System;
using System.IO;
using Utils.Logging;

Expand Down Expand Up @@ -97,6 +98,67 @@ public void Read(BinaryReader br)
ToolkitAssert.Ensure(br.BaseStream.Position == br.BaseStream.Length, "Animation2: Failed to reach EOF.");
}

public void WriteToFile(string fileName)
{
using (MemoryStream ms = new())
{
using (BinaryWriter bw = new(ms))
{
Write(bw);
}

File.WriteAllBytes(fileName, ms.ToArray());
}
}

public void Write(BinaryWriter bw)
{
int Count = Header.RootBoneID != 0 ? (Tracks.Length - 1) : Tracks.Length;
Header.Count = (short)Count;
Header.NumEvents = (short)Events.Length;

Header.Write(bw);
bw.Write(IsDataPresent);

foreach (var val in Events)
{
val.Write(bw);
}

bw.Write(Unk00);
bw.Write(Unk01);
bw.Write((short)Count);

foreach (var track in Tracks)
{
track.Write(bw);
}

if (UnkShorts00.Length < Unk01)
{
short[] newUnk00Shorts = new short[Unk01];
Array.Copy(UnkShorts00, 0, newUnk00Shorts, 0, UnkShorts00.Length);
UnkShorts00 = newUnk00Shorts;
}

if (UnkShorts01.Length < Count)
{
short[] newUnk01Shorts = new short[Count];
Array.Copy(UnkShorts01, 0, newUnk01Shorts, 0, UnkShorts01.Length);
UnkShorts01 = newUnk01Shorts;
}

foreach (var val in UnkShorts00)
{
bw.Write(val);
}

foreach (var val in UnkShorts01)
{
bw.Write(val);
}
}

private void ConvertToMTB()
{
MT_ObjectBundle NewBundle = new MT_ObjectBundle();
Expand Down
6 changes: 6 additions & 0 deletions Mafia2Libs/ResourceTypes/FileTypes/Animations/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ public void Read(BinaryReader br)
EventID = br.ReadUInt32();
Time = br.ReadSingle();
}

public void Write(BinaryWriter bw)
{
bw.Write(EventID);
bw.Write(Time);
}
}
}
22 changes: 22 additions & 0 deletions Mafia2Libs/ResourceTypes/FileTypes/Animations/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,27 @@ public void Read(BinaryReader br)
Unk09 = br.ReadByte();
RootBoneID = br.ReadUInt64();
}

public void Write(BinaryWriter bw)
{
bw.Write(SkeletonID);
bw.Write(Version);
bw.Write(Magic);
bw.Write(NumEvents);
bw.Write(Unk00);
Unk01.WriteToFile(bw);
bw.Write(Unk02);
bw.Write(Unk03);
bw.Write(Unk04);
bw.Write(Unk05);
bw.Write(Unk06);
bw.Write(Unk07);
bw.Write(Hash);
bw.Write(Unk08);
bw.Write(Duration);
bw.Write(Count);
bw.Write(Unk09);
bw.Write(RootBoneID);
}
}
}
Loading