public remParser(Stream stream) { try { using (BinaryReader binaryReader = new BinaryReader(stream)) { string[] sectionNames = { "MATC", "BONC", "MESC", "SKIC" }; for (int sectionIdx = 0; sectionIdx < sectionNames.Length; sectionIdx++) { string sectionName = Encoding.ASCII.GetString(binaryReader.ReadBytes(4)); if (sectionName.Length < 4) { throw new Exception("file incomplete"); } if (sectionName != sectionNames[sectionIdx]) { Report.ReportLog("Strange section or order for " + sectionName); } int sectionLength = binaryReader.ReadInt32(); int numSubSections = binaryReader.ReadInt32(); byte[] sectionBuffer = binaryReader.ReadBytes((int)sectionLength); switch (sectionIdx) { case 0: this.MATC = ReadMaterials(sectionName, sectionLength, numSubSections, sectionBuffer); break; case 1: this.BONC = ReadBones(sectionName, sectionLength, numSubSections, sectionBuffer); break; case 2: this.MESC = ReadMeshes(sectionName, sectionLength, numSubSections, sectionBuffer); break; case 3: this.SKIC = ReadSkin(sectionName, sectionLength, numSubSections, sectionBuffer); break; } } } } catch (FileNotFoundException) { Report.ReportLog("file not found"); } }
private static remBONCsection ReadBones(string sectionName, int sectionLength, int numBones, byte[] sectionBuffer) { remBONCsection boneSec = new remBONCsection(numBones); int secBufIdx = 0; for (int subSection = 0; subSection < numBones; subSection++) { byte[] type = new byte[4] { sectionBuffer[secBufIdx+0], sectionBuffer[secBufIdx+1], sectionBuffer[secBufIdx+2], sectionBuffer[secBufIdx+3] }; int length = BitConverter.ToInt32(sectionBuffer, secBufIdx+4); remBone bone = new remBone(length > 256+16*4+4 ? (length - 256+16*4+4) / 256 : 0); Trace.Assert(TypeCheck(remBone.ClassType, type)); bone.name = GetIdentifier(sectionBuffer, secBufIdx+8); Matrix matrix = new Matrix(); for (int i = 0; i < 4; i++) { Vector4 row = new Vector4(); for (int j = 0; j < 4; j++) row[j] = BitConverter.ToSingle(sectionBuffer, secBufIdx+8+256 + (i*4 + j) * 4); matrix.set_Rows(i, row); } bone.matrix = matrix; int numChilds = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256 + 16*4); List<remId> childNames = new List<remId>(numChilds); for (int i = 0; i < numChilds; i++) childNames.Add(GetIdentifier(sectionBuffer, secBufIdx+8+256 + 16*4 + 4 + i*256)); AddParentBone(boneSec, bone, childNames); secBufIdx += length; } for (int i = 0; i < numBones; i++) { if (boneSec[i].Parent == null) { boneSec[i].Parent = boneSec.rootFrame; boneSec.rootFrame.AddChild(boneSec[i]); } } if (secBufIdx != sectionLength) Report.ReportLog("Warning! BONC section has wrong length."); return boneSec; }
private static void AddParentBone(remBONCsection sec, remBone parent, List<remId> childNames) { for (int i = 0; i < childNames.Count; i++) { remId child = childNames[i]; for (int j = 0; j < sec.Count; j++) { if (sec[j].name == child) { parent.AddChild(sec[j]); break; } } } sec.ChildList.Add(parent); }