//--------------------------------------------------------------------- // SAVING //--------------------------------------------------------------------- /// <summary> /// Saves the HXM file to a given stream. /// </summary> /// <param name="stream">The stream to write to.</param> public void Write(Stream stream) { ReplacedJoints.Clear(); foreach (Robot robot in ReplacedRobots) { LoadAnimations(robot, GetModel(robot.ModelNum)); } LoadModelTextures(); CreateDataLists(); BaseFile.Write(stream); }
private void CreateLocalLists() { foreach (Robot robot in BaseFile.ReplacedRobots) { ReplacedRobots.Add(robot); } foreach (JointPos joint in BaseFile.ReplacedJoints) { ReplacedJoints.Add(joint); } foreach (Polymodel model in BaseFile.ReplacedModels) { ReplacedModels.Add(model); } foreach (ReplacedBitmapElement bm in BaseFile.ReplacedObjBitmaps) { ReplacedObjBitmaps.Add(bm); } foreach (ReplacedBitmapElement bm in BaseFile.ReplacedObjBitmapPtrs) { ReplacedObjBitmapPtrs.Add(bm); } }
/// <summary> /// Generates a robot's anim_states and creates the joint elements for the robot /// </summary> /// <param name="robot">The robot to generate joints for.</param> /// <param name="model">The model to use to generate joints.</param> private void LoadAnimations(Robot robot, Polymodel model) { int NumRobotJoints = robot.baseJoint; robot.NumGuns = (sbyte)model.NumGuns; for (int i = 0; i < 8; i++) { robot.GunPoints[i] = model.GunPoints[i]; robot.GunSubmodels[i] = (byte)model.GunSubmodels[i]; } for (int m = 0; m < 9; m++) { for (int f = 0; f < 5; f++) { robot.AnimStates[m, f].NumJoints = 0; robot.AnimStates[m, f].Offset = 0; } } if (!model.IsAnimated) { return; } int[] gunNums = new int[10]; for (int i = 1; i < model.NumSubmodels; i++) { gunNums[i] = robot.NumGuns; } gunNums[0] = -1; for (int g = 0; g < robot.NumGuns; g++) { int m = robot.GunSubmodels[g]; while (m != 0) { gunNums[m] = g; m = model.Submodels[m].Parent; } } for (int g = 0; g < robot.NumGuns + 1; g++) { for (int state = 0; state < 5; state++) { robot.AnimStates[g, state].NumJoints = 0; robot.AnimStates[g, state].Offset = (short)NumRobotJoints; for (int m = 0; m < model.NumSubmodels; m++) { if (gunNums[m] == g) { JointPos joint = new JointPos(); joint.JointNum = (short)m; joint.Angles = model.AnimationMatrix[m, state]; joint.ReplacementID = NumRobotJoints; ReplacedJoints.Add(joint); robot.AnimStates[g, state].NumJoints++; NumRobotJoints++; } } } } }
/// <summary> /// Loads an HXM file from a given stream. /// </summary> /// <param name="stream">The stream to load the HXM data from.</param> public void Read(Stream stream) { BinaryReader br; br = new BinaryReader(stream); HAMDataReader data = new HAMDataReader(); int sig = br.ReadInt32(); int ver = br.ReadInt32(); if (sig != 559435080) { br.Dispose(); throw new InvalidDataException("HXMFile::Read: HXM file has bad header."); } if (ver != 1) { br.Dispose(); throw new InvalidDataException(string.Format("HXMFile::Read: HXM file has bad version. Got {0}, but expected 1", ver)); } int replacedRobotCount = br.ReadInt32(); for (int x = 0; x < replacedRobotCount; x++) { int replacementID = br.ReadInt32(); Robot robot = data.ReadRobot(br); robot.replacementID = replacementID; ReplacedRobots.Add(robot); } int replacedJointCount = br.ReadInt32(); for (int x = 0; x < replacedJointCount; x++) { int replacementID = br.ReadInt32(); JointPos joint = new JointPos(); joint.JointNum = br.ReadInt16(); joint.Angles.P = br.ReadInt16(); joint.Angles.B = br.ReadInt16(); joint.Angles.H = br.ReadInt16(); joint.ReplacementID = replacementID; ReplacedJoints.Add(joint); } int modelsToReplace = br.ReadInt32(); for (int x = 0; x < modelsToReplace; x++) { int replacementID = br.ReadInt32(); Polymodel model = data.ReadPolymodelInfo(br); model.ReplacementID = replacementID; model.InterpreterData = br.ReadBytes(model.ModelIDTASize); ReplacedModels.Add(model); model.DyingModelnum = br.ReadInt32(); model.DeadModelnum = br.ReadInt32(); } int objBitmapsToReplace = br.ReadInt32(); for (int x = 0; x < objBitmapsToReplace; x++) { ReplacedBitmapElement objBitmap = new ReplacedBitmapElement(); objBitmap.ReplacementID = br.ReadInt32(); objBitmap.Data = br.ReadUInt16(); ReplacedObjBitmaps.Add(objBitmap); //Console.WriteLine("Loading replacement obj bitmap, replacing slot {0} with {1} ({2})", objBitmap.replacementID, objBitmap.data, baseFile.piggyFile.images[objBitmap.data].name); } int objBitmapPtrsToReplace = br.ReadInt32(); for (int x = 0; x < objBitmapPtrsToReplace; x++) { ReplacedBitmapElement objBitmap = new ReplacedBitmapElement(); objBitmap.ReplacementID = br.ReadInt32(); objBitmap.Data = br.ReadUInt16(); ReplacedObjBitmapPtrs.Add(objBitmap); } }