static void ParseMdlFile() { if (MDL_Header.id != 0x54534449) { throw new FileLoadException(String.Format("{0}: File signature does not match 'IDST'", ModelObject.name + ".mdl")); } MDL_BodyParts = new mstudiobodyparts_t[MDL_Header.bodypart_count]; ModelFileLoader.ReadArray(ref MDL_BodyParts, MDL_Header.bodypart_offset); Int32 ModelInputFilePosition = MDL_Header.bodypart_offset + MDL_BodyParts[0].modelindex; MDL_Models = new mstudiomodel_t[MDL_BodyParts[0].nummodels]; ModelFileLoader.ReadArray(ref MDL_Models, ModelInputFilePosition); Int32 MeshInputFilePosition = ModelInputFilePosition + MDL_Models[0].meshindex; MDL_Meshes = new mstudiomesh_t[MDL_Models[0].nummeshes]; ModelFileLoader.ReadArray(ref MDL_Meshes, MeshInputFilePosition); mstudiotexture_t[] MDL_TexturesInfo = new mstudiotexture_t[MDL_Header.texture_count]; ModelFileLoader.ReadArray(ref MDL_TexturesInfo, MDL_Header.texture_offset); MDL_Textures = new String[MDL_Header.texture_count]; for (Int32 i = 0; i < MDL_Header.texture_count; i++) { Int32 StringInputFilePosition = MDL_Header.texture_offset + (Marshal.SizeOf(typeof(mstudiotexture_t)) * i) + MDL_TexturesInfo[i].sznameindex; MDL_Textures[i] = ModelFileLoader.ReadNullTerminatedString(StringInputFilePosition); } Int32[] TDirOffsets = new Int32[MDL_Header.texturedir_count]; ModelFileLoader.ReadArray(ref TDirOffsets, MDL_Header.texturedir_offset); MDL_TDirectories = new String[MDL_Header.texturedir_count]; for (Int32 i = 0; i < MDL_Header.texturedir_count; i++) { MDL_TDirectories[i] = ModelFileLoader.ReadNullTerminatedString(TDirOffsets[i]); } mstudiobone_t[] MDL_BonesInfo = new mstudiobone_t[MDL_Header.bone_count]; ModelFileLoader.ReadArray(ref MDL_BonesInfo, MDL_Header.bone_offset); for (Int32 i = 0; i < MDL_Header.bone_count; i++) { Int32 StringInputFilePosition = MDL_Header.bone_offset + (Marshal.SizeOf(typeof(mstudiobone_t)) * i) + MDL_BonesInfo[i].sznameindex; GameObject BoneObject = new GameObject(ModelFileLoader.ReadNullTerminatedString(StringInputFilePosition)); MDL_Bones.Add(BoneObject.transform); Transform FixUp = null; // WIP - It works incorrectly (nearly) //Temporary "Fix" Bone Transform //TODO: Remove this sh*t code, rly :D if (MDL_BonesInfo[i].parent >= 0) { MDL_Bones[i].parent = MDL_Bones[MDL_BonesInfo[i].parent]; MDL_Bones[i].transform.localPosition = -MDL_BonesInfo[i].pos * ConfigLoader.WorldScale; MDL_Bones[i].transform.localRotation = MDL_BonesInfo[i].quat; } else { FixUp = new GameObject("FIXUP").transform; FixUp.transform.rotation = Quaternion.Euler(new Vector3(-270, 0, 0)); MDL_Bones[i].parent = FixUp; MDL_Bones[i].transform.localPosition = -MDL_BonesInfo[i].pos * ConfigLoader.WorldScale; MDL_Bones[i].transform.localRotation = MDL_BonesInfo[i].quat; MDL_Bones[i].parent = ModelObject.transform; UnityEngine.Object.DestroyImmediate(FixUp.gameObject); } } }
public static void Load(string BSPName) { if (!File.Exists((ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[0] + "/maps/" + BSPName + ".bsp"))) { throw new FileNotFoundException(String.Format("Map file ({0}) wasn't found in the ({1}) mod-folder. Check weather a path is valid.", BSPName, ConfigLoader.ModFolders[0])); } BSPFileReader = new MemUtils(File.OpenRead(ConfigLoader.GamePath + "/" + ConfigLoader.ModFolders[0] + "/maps/" + BSPName + ".bsp")); BSPFileReader.ReadType(ref BSP_Header); if (BSP_Header.Ident != 0x50534256) { throw new FileLoadException(String.Format("{0}: File signature does not match 'VBSP'", BSPName)); } if (BSP_Header.Version < 19 || BSP_Header.Version > 21) { throw new FileLoadException(String.Format("{0}: BSP version ({1}) isn't supported", BSPName, BSP_Header.Version)); } if (BSP_Header.Lumps[0].FileOfs == 0) { Debug.Log("Found Left 4 Dead 2 header"); for (Int32 i = 0; i < BSP_Header.Lumps.Length; i++) { BSP_Header.Lumps[i].FileOfs = BSP_Header.Lumps[i].FileLen; BSP_Header.Lumps[i].FileLen = BSP_Header.Lumps[i].Version; } } if (BSP_Header.Lumps[58].FileLen / 56 <= 0) { BSP_Faces = new dface_t[BSP_Header.Lumps[7].FileLen / 56]; BSPFileReader.ReadArray(ref BSP_Faces, BSP_Header.Lumps[7].FileOfs); } else { BSP_Faces = new dface_t[BSP_Header.Lumps[58].FileLen / 56]; BSPFileReader.ReadArray(ref BSP_Faces, BSP_Header.Lumps[58].FileOfs); } BSP_Models = new dmodel_t[BSP_Header.Lumps[14].FileLen / 48]; BSPFileReader.ReadArray(ref BSP_Models, BSP_Header.Lumps[14].FileOfs); BSP_Overlay = new doverlay_t[BSP_Header.Lumps[45].FileLen / 352]; BSPFileReader.ReadArray(ref BSP_Overlay, BSP_Header.Lumps[45].FileOfs); BSP_DispInfo = new ddispinfo_t[BSP_Header.Lumps[26].FileLen / 176]; BSPFileReader.ReadArray(ref BSP_DispInfo, BSP_Header.Lumps[26].FileOfs); BSP_DispVerts = new dDispVert[BSP_Header.Lumps[33].FileLen / 20]; BSPFileReader.ReadArray(ref BSP_DispVerts, BSP_Header.Lumps[33].FileOfs); BSP_TexInfo = new texinfo_t[BSP_Header.Lumps[18].FileLen / 12]; BSPFileReader.ReadArray(ref BSP_TexInfo, BSP_Header.Lumps[18].FileOfs); BSP_TexInfo = new texinfo_t[BSP_Header.Lumps[6].FileLen / 72]; BSPFileReader.ReadArray(ref BSP_TexInfo, BSP_Header.Lumps[6].FileOfs); BSP_TexData = new dtexdata_t[BSP_Header.Lumps[2].FileLen / 32]; BSPFileReader.ReadArray(ref BSP_TexData, BSP_Header.Lumps[2].FileOfs); BSP_TextureStringData = new String[BSP_Header.Lumps[44].FileLen / 4]; Int32[] BSP_TextureStringTable = new Int32[BSP_Header.Lumps[44].FileLen / 4]; BSPFileReader.ReadArray(ref BSP_TextureStringTable, BSP_Header.Lumps[44].FileOfs); for (Int32 i = 0; i < BSP_TextureStringTable.Length; i++) { BSP_TextureStringData[i] = BSPFileReader.ReadNullTerminatedString(BSP_Header.Lumps[43].FileOfs + BSP_TextureStringTable[i]); } BSP_Edges = new dedge_t[BSP_Header.Lumps[12].FileLen / 4]; BSPFileReader.ReadArray(ref BSP_Edges, BSP_Header.Lumps[12].FileOfs); BSPFileReader.BaseStream.Seek(BSP_Header.Lumps[3].FileOfs, SeekOrigin.Begin); BSP_Vertices = new Vector3[BSP_Header.Lumps[3].FileLen / 12]; for (Int32 i = 0; i < BSP_Vertices.Length; i++) { BSP_Vertices[i] = BSPFileReader.ReadVector3D(true) * ConfigLoader.WorldScale; } BSP_Surfedges = new Int32[BSP_Header.Lumps[13].FileLen / 4]; BSPFileReader.ReadArray(ref BSP_Surfedges, BSP_Header.Lumps[13].FileOfs); BSP_WorldSpawn = new GameObject(BSPName); BSP_Brushes = new List <GameObject>(); //Create new lightmap list ConfigLoader.lightmapsData = new List <LightmapData>(); LightmapSettings.lightmapsMode = LightmapsMode.NonDirectional; LoadEntities(); LoadStaticProps(); BSPFileReader.BaseStream.Dispose();//.Close(); }