Start to add more progress on cmb saving

This commit is contained in:
KillzXGaming 2019-11-10 16:24:54 -05:00
parent cc5e17e984
commit 7d4ad62dd0
9 changed files with 341 additions and 65 deletions

View file

@ -18,8 +18,8 @@ namespace FirstPlugin
public FileType FileType { get; set; } = FileType.Layout;
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "CMB" };
public string[] Extension { get; set; } = new string[] { "*CTR Model Binary" };
public string[] Description { get; set; } = new string[] { "*CTR Model Binary" };
public string[] Extension { get; set; } = new string[] { ".cmb" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
@ -117,6 +117,8 @@ namespace FirstPlugin
public void Load(System.IO.Stream stream)
{
CanSave = false;
Renderer = new CMB_Renderer();
DrawableContainer.Drawables.Add(Renderer);
Skeleton = new STSkeleton();
@ -391,6 +393,9 @@ namespace FirstPlugin
public void Save(System.IO.Stream stream)
{
using (var writer = new FileWriter(stream)) {
header.Write(writer);
}
}
public enum CMBVersion
@ -724,6 +729,52 @@ namespace FirstPlugin
values[3] * VertexAttribute.Scale);
}
private static void WriteVertexBufferData(FileWriter writer, BufferSlice Slice,
SepdVertexAttribute VertexAttribute, int elementCount)
{
int StrideSize = CalculateStrideSize(VertexAttribute.Type, elementCount);
int VertexCount = (int)Slice.Size / StrideSize;
for (int v = 0; v < VertexCount; v++)
{
WriteVertexBufferData(writer, VertexAttribute, VertexAttribute.VertexData[v], elementCount);
}
}
private static void WriteVertexBufferData(FileWriter writer, SepdVertexAttribute VertexAttribute,
Syroot.Maths.Vector4F value, int elementCount)
{
float[] values = new float[4] { value.X, value.Y, value.Z, value.W };
for (int i = 0; i < elementCount; i++)
{
switch (VertexAttribute.Type)
{
case CmbDataType.Byte:
writer.Write((sbyte)values[i]);
break;
case CmbDataType.Float:
writer.Write(values[i]);
break;
case CmbDataType.Int:
writer.Write((int)values[i]);
break;
case CmbDataType.Short:
writer.Write((short)values[i]);
break;
case CmbDataType.UByte:
writer.Write((byte)values[i]);
break;
case CmbDataType.UInt:
writer.Write((uint)values[i]);
break;
case CmbDataType.UShort:
writer.Write((ushort)values[i]);
break;
default: throw new Exception("Unknown format! " + VertexAttribute.Type);
}
}
}
private static int CalculateStrideSize(CmbDataType type, int elementCount)
{
switch (type)
@ -951,6 +1002,8 @@ namespace FirstPlugin
public ushort boneDimension;
private byte[] unks;
public void Read(FileReader reader, Header header)
{
long pos = reader.Position;
@ -960,11 +1013,9 @@ namespace FirstPlugin
uint count = reader.ReadUInt16();
if (header.Version >= CMBVersion.LM3DS)
reader.SeekBegin(pos + 0x3C);
unks = reader.ReadBytes(56);
else
reader.SeekBegin(pos + 0x24);
Console.WriteLine("sepd count " + count);
unks = reader.ReadBytes(26);
Position = ReadVertexAttrib(reader);
Normal = ReadVertexAttrib(reader);
@ -994,7 +1045,22 @@ namespace FirstPlugin
public void Write(FileWriter writer, Header header)
{
writer.WriteSignature(Magic);
writer.Write(uint.MaxValue); //section size
writer.Write(Primatives.Count);
writer.Write(unks);
WriteVertexAttrib(writer, Position);
WriteVertexAttrib(writer, Normal);
if (header.Version >= CMBVersion.MM3DS)
WriteVertexAttrib(writer, Tangent);
WriteVertexAttrib(writer, Color);
WriteVertexAttrib(writer, TexCoord0);
WriteVertexAttrib(writer, TexCoord1);
WriteVertexAttrib(writer, TexCoord2);
WriteVertexAttrib(writer, BoneIndices);
WriteVertexAttrib(writer, BoneWeights);
WriteVertexAttrib(writer, Tangent);
}
private SepdVertexAttribute ReadVertexAttrib(FileReader reader)
@ -1016,6 +1082,23 @@ namespace FirstPlugin
return att;
}
private void WriteVertexAttrib(FileWriter writer, SepdVertexAttribute attribute)
{
long pos = writer.Position;
SepdVertexAttribute att = new SepdVertexAttribute();
writer.Write(att.StartPosition);
writer.Write(att.Scale);
writer.Write(att.Type, true);
writer.Write(att.Mode, true);
writer.Write(att.Constants[0]);
writer.Write(att.Constants[1]);
writer.Write(att.Constants[2]);
writer.Write(att.Constants[3]);
writer.SeekBegin(pos + 0x1C);
}
}
public class SepdVertexAttribute

View file

@ -11,7 +11,7 @@ using Toolbox.Library.Animations;
namespace FirstPlugin
{
public class CSAB : Animation, IFileFormat
public class CSAB : STSkeletonAnimation, IFileFormat
{
public FileType FileType { get; set; } = FileType.Animation;
@ -46,8 +46,10 @@ namespace FirstPlugin
header = new Header();
header.Read(new FileReader(stream));
ImageKey = "";
foreach (var bone in header.Nodes)
AnimGroups.Add(bone);
}
public void Unload()
{
@ -122,7 +124,7 @@ namespace FirstPlugin
}
}
public class AnimationNode
public class AnimationNode : STAnimGroup
{
public ushort BoneIndex { get; set; }
@ -136,6 +138,21 @@ namespace FirstPlugin
public AnimTrack ScaleY { get; set; }
public AnimTrack ScaleZ { get; set; }
public override List<STAnimationTrack> GetTracks()
{
List<STAnimationTrack> tracks = new List<STAnimationTrack>();
tracks.Add(TranslateX);
tracks.Add(TranslateY);
tracks.Add(TranslateZ);
tracks.Add(RotationX);
tracks.Add(RotationY);
tracks.Add(RotationZ);
tracks.Add(ScaleX);
tracks.Add(ScaleY);
tracks.Add(ScaleZ);
return tracks;
}
public void Read(FileReader reader, GameVersion version)
{
long pos = reader.Position;
@ -169,7 +186,7 @@ namespace FirstPlugin
return track;
}
public class AnimTrack
public class AnimTrack : STAnimationTrack
{
public List<LinearKeyFrame> KeyFramesLinear = new List<LinearKeyFrame>();
public List<HermiteKeyFrame> KeyFramesHermite = new List<HermiteKeyFrame>();

View file

@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Toolbox.Library;
using Toolbox.Library.IO;
namespace FirstPlugin.NLG
{
public class StrikersSAnim : TreeNodeFile, IFileFormat
{
public FileType FileType { get; set; } = FileType.Animation;
public bool CanSave { get; set; }
public string[] Description { get; set; } = new string[] { "Striker Skeleton Animation" };
public string[] Extension { get; set; } = new string[] { "*.sanim" };
public string FileName { get; set; }
public string FilePath { get; set; }
public IFileInfo IFileInfo { get; set; }
public bool Identify(System.IO.Stream stream)
{
return Utils.GetExtension(FileName) == ".sanim";
}
public Type[] Types
{
get
{
List<Type> types = new List<Type>();
return types.ToArray();
}
}
public enum SectionMagic : uint
{
}
Dictionary<SectionMagic, SectionHeader> SectionLookup = new Dictionary<SectionMagic, SectionHeader>();
public void Load(System.IO.Stream stream)
{
using (var reader = new FileReader(stream))
{
ushort fileFlags = reader.ReadUInt16();
ushort fileMagic = reader.ReadUInt16();
uint fileSize = reader.ReadUInt32();
reader.SetByteOrder(true);
while (!reader.EndOfStream)
{
ushort flags = reader.ReadUInt16();
ushort magic = reader.ReadUInt16();
uint sectionSize = reader.ReadUInt32();
long pos = reader.Position;
Console.WriteLine($"Magic {(SectionMagic)magic} sectionSize {sectionSize}");
if (!SectionLookup.ContainsKey((SectionMagic)magic))
SectionLookup.Add((SectionMagic)magic, new SectionHeader(sectionSize, pos));
//This section will skip sub sections so don't do that
reader.SeekBegin(pos + sectionSize);
Nodes.Add(magic.ToString("X"));
reader.Align(4);
}
}
}
public class SectionHeader
{
public long Position;
public uint Size;
public SectionHeader(uint size, long pos)
{
Size = size;
Position = pos;
}
}
public void Unload()
{
}
public void Save(System.IO.Stream stream)
{
}
}
}

View file

@ -224,6 +224,7 @@
<Compile Include="FileFormats\Archives\PKZ.cs" />
<Compile Include="FileFormats\NLG\MarioStrikers\StrikersRLG.cs" />
<Compile Include="FileFormats\NLG\MarioStrikers\StrikersRLT.cs" />
<Compile Include="FileFormats\NLG\MarioStrikers\StrikersSAnim.cs" />
<Compile Include="FileFormats\NLG\NLG_Common.cs" />
<Compile Include="FileFormats\NLG\NLG_NLOC.cs" />
<Compile Include="FileFormats\NLG\NLOC_Wrapper.cs" />

View file

@ -397,9 +397,10 @@ namespace FirstPlugin
Formats.Add(typeof(LM2_ARCADE_Model));
Formats.Add(typeof(NLG_NLOC));
Formats.Add(typeof(NLG_PCK));
Formats.Add(typeof(NLG.StrikersSAnim));
// Formats.Add(typeof(MSBP));
// Formats.Add(typeof(BFGRP));

View file

@ -141,7 +141,10 @@ namespace Toolbox.Library.IO
if (Compress)
stream = compressionFormat.Compress(data);
else
{
compressionFormat.Identify(data, sfd.FileName);
stream = compressionFormat.Decompress(data);
}
if (stream != null)
{

View file

@ -28,14 +28,13 @@ namespace Toolbox.Library
reader.SetByteOrder(true);
bool IsValid = false;
for (int i = 0; i < 4; i++)
for (int i = 0; i < 8; i++)
{
ushort magicNumber = reader.ReadUInt16();
reader.ReadUInt16();
IsValid = magicNumber == 0x789C || magicNumber == 0x78DA;
if (IsValid) {
startPosition = reader.Position - 4;
startPosition = reader.Position - 2;
break;
}
}

View file

@ -54,6 +54,36 @@ namespace Toolbox.Library
private bool renderThreadIsUpdating = false;
public bool isOpen = true;
private STAnimation stCurrentAnimation;
public STAnimation CurrentSTAnimation
{
get
{
return stCurrentAnimation;
}
set
{
if (value == null)
return;
int frameCount = 1;
if (value.FrameCount != 0)
frameCount = (int)value.FrameCount;
ResetModels();
stCurrentAnimation = value;
totalFrame.Maximum = frameCount;
totalFrame.Value = frameCount;
currentFrameUpDown.Maximum = frameCount;
animationTrackBar.FrameCount = frameCount;
currentFrameUpDown.Value = 0;
SetAnimationsToFrame(0);
UpdateViewport();
}
}
private Animation currentAnimation;
public Animation CurrentAnimation
{
@ -202,7 +232,8 @@ namespace Toolbox.Library
private void animationPlayBtn_Click(object sender, EventArgs e)
{
if (currentAnimation == null || currentAnimation.FrameCount <= 0)
if (currentAnimation == null || currentAnimation.FrameCount <= 0 &&
stCurrentAnimation == null || stCurrentAnimation.FrameCount <= 0)
return;
if (AnimationPlayerState == PlayerState.Playing)
@ -213,18 +244,27 @@ namespace Toolbox.Library
private void totalFrame_ValueChanged(object sender, EventArgs e)
{
if (currentAnimation == null) return;
if (currentAnimation == null && stCurrentAnimation == null) return;
if (totalFrame.Value < 1)
{
totalFrame.Value = 1;
}
else
{
if (currentAnimation.Tag is Animation)
((Animation)currentAnimation.Tag).FrameCount = (int)totalFrame.Value;
currentAnimation.FrameCount = (int)totalFrame.Value;
animationTrackBar.CurrentFrame = 0;
animationTrackBar.FrameCount = currentAnimation.FrameCount;
if (stCurrentAnimation != null)
{
stCurrentAnimation.FrameCount = (int)totalFrame.Value;
animationTrackBar.CurrentFrame = 0;
animationTrackBar.FrameCount = stCurrentAnimation.FrameCount;
}
else
{
if (currentAnimation.Tag is Animation)
((Animation)currentAnimation.Tag).FrameCount = (int)totalFrame.Value;
currentAnimation.FrameCount = (int)totalFrame.Value;
animationTrackBar.CurrentFrame = 0;
animationTrackBar.FrameCount = currentAnimation.FrameCount;
}
}
}
private void UpdateViewport()
@ -286,7 +326,7 @@ namespace Toolbox.Library
}
private void animationTrackBar_ValueChanged(object sender, EventArgs e) {
if (currentAnimation == null || totalFrame.Value <= 0)
if (currentAnimation == null && stCurrentAnimation == null || totalFrame.Value <= 0)
return;
currentFrameUpDown.Value = (decimal)animationTrackBar.CurrentFrame;
@ -310,55 +350,78 @@ namespace Toolbox.Library
if (viewport == null || viewport.scene == null)
return;
var anim = currentAnimation.Tag;
if (stCurrentAnimation != null)
{
if (frameNum > stCurrentAnimation.FrameCount)
return;
float animFrameNum = frameNum;
float animFrameNum = frameNum;
if (anim is MaterialAnimation)
{
((MaterialAnimation)anim).SetFrame(animFrameNum);
((MaterialAnimation)anim).NextFrame(viewport);
}
else if (anim is VisibilityAnimation)
{
((VisibilityAnimation)anim).SetFrame(animFrameNum);
((VisibilityAnimation)anim).NextFrame(viewport);
}
else if (anim is CameraAnimation)
{
((CameraAnimation)anim).SetFrame(animFrameNum);
((CameraAnimation)anim).NextFrame(viewport);
}
else if (anim is LightAnimation)
{
((LightAnimation)anim).SetFrame(animFrameNum);
((LightAnimation)anim).NextFrame(viewport);
}
else if (anim is FogAnimation)
{
((FogAnimation)anim).SetFrame(animFrameNum);
((FogAnimation)anim).NextFrame(viewport);
}
else //Play a skeletal animation if it's not the other types
{
foreach (var drawable in viewport.scene.objects)
stCurrentAnimation.SetFrame(animFrameNum);
stCurrentAnimation.NextFrame();
//Add frames to the playing animation
stCurrentAnimation.Frame += frameNum;
//Reset it when it reaches the total frame count
if (stCurrentAnimation.Frame >= stCurrentAnimation.FrameCount && stCurrentAnimation.Loop)
{
if (drawable is STSkeleton)
{
currentAnimation.SetFrame(animFrameNum);
currentAnimation.NextFrame((STSkeleton)drawable);
}
stCurrentAnimation.Frame = 0;
}
}
//Add frames to the playing animation
currentAnimation.Frame += frameNum;
//Reset it when it reaches the total frame count
if (currentAnimation.Frame >= currentAnimation.FrameCount)
else
{
currentAnimation.Frame = 0;
var anim = currentAnimation.Tag;
float animFrameNum = frameNum;
if (anim is MaterialAnimation)
{
((MaterialAnimation)anim).SetFrame(animFrameNum);
((MaterialAnimation)anim).NextFrame(viewport);
}
else if (anim is VisibilityAnimation)
{
((VisibilityAnimation)anim).SetFrame(animFrameNum);
((VisibilityAnimation)anim).NextFrame(viewport);
}
else if (anim is CameraAnimation)
{
((CameraAnimation)anim).SetFrame(animFrameNum);
((CameraAnimation)anim).NextFrame(viewport);
}
else if (anim is LightAnimation)
{
((LightAnimation)anim).SetFrame(animFrameNum);
((LightAnimation)anim).NextFrame(viewport);
}
else if (anim is FogAnimation)
{
((FogAnimation)anim).SetFrame(animFrameNum);
((FogAnimation)anim).NextFrame(viewport);
}
else //Play a skeletal animation if it's not the other types
{
foreach (var drawable in viewport.scene.objects)
{
if (drawable is STSkeleton)
{
currentAnimation.SetFrame(animFrameNum);
currentAnimation.NextFrame((STSkeleton)drawable);
}
}
}
//Add frames to the playing animation
currentAnimation.Frame += frameNum;
//Reset it when it reaches the total frame count
if (currentAnimation.Frame >= currentAnimation.FrameCount)
{
currentAnimation.Frame = 0;
}
}
}
@ -456,6 +519,7 @@ namespace Toolbox.Library
renderThread.Abort();
renderThreadIsUpdating = false;
stCurrentAnimation = null;
currentAnimation = null;
isOpen = false;
Dispose();

View file

@ -34,6 +34,19 @@ namespace Toolbox.Library
if (viewport == null)
return;
//The new animation player
if (e.Node.Tag != null && e.Node.Tag is STAnimation)
{
var anim = e.Node.Tag as STAnimation;
if (LibraryGUI.GetAnimationPanel() != null)
{
Console.WriteLine("running" + anim.Name);
LibraryGUI.GetAnimationPanel().CurrentSTAnimation = anim;
}
}
//The old animation player (will be removed soon)
if (e.Node is Animation)
{
string AnimName = e.Node.Text;