public static void Build()
        {
            TagFileName scenario_tag = ProjectManager.ScenarioTagFileName;

            //IndexBuilder IndexBuild = new IndexBuilder();
            Compiler_Gren.DependencyBuilder IndexBuild = new Compiler_Gren.DependencyBuilder();
            //FileInfo TagFile_info = new FileInfo(scenario_tag.RelativePath);
            string BasFolder    = Application.StartupPath + @"\Extracted Tags\";
            string StructFolder = Application.StartupPath + @"\Tag Structures\PcHalo\";

            //string MapsFolder = Application.StartupPath + @"\Maps\";
            if (true)
            {
                //Create the index table
                //lbCurTag.Text = "Building Index Table....";
                Application.DoEvents();
                //string[] IndexList = IndexBuild.BuildIndex(scenario_tag.RelativePath, BasFolder, StructFolder);
                string[]         IndexList = IndexBuild.BuildIndex(scenario_tag);
                HaloPCMAP.HaloPC NewMap    = new HaloPCMAP.HaloPC();
                NewMap.PCIndexHeader.Create((uint)IndexList.Length);
                NewMap.PCIndexHeader.IndexMagic = 0x40440028;
                uint StringTableSize = 0;
                //Fixing index items
                ushort gt = 0xE174;
                //lbCurTag.Text = "Building Index Table Items....";
                Application.DoEvents();

                for (int sc = 0; sc < IndexList.Length; sc++)
                {
                    //FileInfo TagFile2_info = new FileInfo(BasFolder + IndexList[sc]);
                    //FileStream TagFile2;
                    //TagFile2 = TagFile2_info.Open(FileMode.Open,FileAccess.Read);
                    //byte[] tmp = new byte[0x40];
                    //TagFile2.Read(tmp,0x00,tmp.Length);
                    //TagFile2.Close();

                    TagFileName tfnTAG = new TagFileName(IndexList[sc], MapfileVersion.HALOPC);
                    TagBase     tbTAG  = new TagBase();
                    tbTAG.LoadTagBuffer(tfnTAG);

                    //uint TagClass1 = BitConverter.ToUInt32(tmp,0x24);
                    //uint TagClass2 = BitConverter.ToUInt32(tmp,0x28);
                    //uint TagClass3 = BitConverter.ToUInt32(tmp,0x2C);
                    NewMap.PCIndexHeader.IndexItems[sc].Create();
                    NewMap.PCIndexHeader.IndexItems[sc].TagClass1  = SwapUInt(tbTAG.Header.TagClass0); // TagClass1;
                    NewMap.PCIndexHeader.IndexItems[sc].TagClass2  = SwapUInt(tbTAG.Header.TagClass1); // TagClass2;
                    NewMap.PCIndexHeader.IndexItems[sc].TagClass3  = SwapUInt(tbTAG.Header.TagClass2); // TagClass3;
                    NewMap.PCIndexHeader.IndexItems[sc].IndexID1   = (ushort)sc;
                    NewMap.PCIndexHeader.IndexItems[sc].IndexID2   = gt;
                    NewMap.PCIndexHeader.IndexItems[sc].NameString = IndexList[sc];
                    if (tbTAG.Header.TagClass0 == 0x70736273 || tbTAG.Header.TagClass0 == 0x73627370)
                    {
                        NewMap.PCIndexHeader.IndexItems[sc].MetaSize = IndexBuild.BSPSizes[0];
                    }
                    else
                    {
                        NewMap.PCIndexHeader.IndexItems[sc].MetaSize = 0;
                    }
                    if (tbTAG.Header.TagClass0 == 0x7274656d || tbTAG.Header.TagClass0 == 0x6d657472)//  6d657472)
                    {
                        gt += 2;
                    }
                    else
                    {
                        gt += 1;
                    }
                    string[] RemoveExt = IndexList[sc].Split(new char[] { '.' }, 256);
                    NewMap.PCIndexHeader.IndexItems[sc].String = new byte[RemoveExt[0].Length + 1];
                    HaloPCMAP.StringToByteArray(ref NewMap.PCIndexHeader.IndexItems[sc].String, RemoveExt[0]);
                    StringTableSize += (uint)(IndexList[sc].Length + 1);
                    tbTAG            = null;
                }
                //Creating the temp files for map building
                FileInfo   VertsFile_info = new FileInfo(Application.StartupPath + "TempVerts.Map");
                FileStream VertsFile;
                VertsFile = VertsFile_info.Open(FileMode.Create, FileAccess.ReadWrite);

                FileInfo   IndicesFile_info = new FileInfo(Application.StartupPath + "TempIndices.Map");
                FileStream IndicesFile;
                IndicesFile = IndicesFile_info.Open(FileMode.Create, FileAccess.ReadWrite);

                FileInfo   MetaFile_info = new FileInfo(Application.StartupPath + "TempMeta.map");
                FileStream MetaFile;
                MetaFile = MetaFile_info.Open(FileMode.Create, FileAccess.ReadWrite);

                FileInfo   BSPFile_info = new FileInfo(Application.StartupPath + "TempBSP.map");
                FileStream BSPFile;
                BSPFile = BSPFile_info.Open(FileMode.Create, FileAccess.ReadWrite);

                //lbCurTag.Text = "Writeing Index Table....";
                Application.DoEvents();

                //writing the index table the temp files
                for (int sc = 0; sc < NewMap.PCIndexHeader.IndexItems.Length; sc++)
                {
                    NewMap.PCIndexHeader.IndexItems[sc].Write(ref MetaFile);
                }
                //Rewriting index items to temp file to fix string offsets.
                //lbCurTag.Text = "Rewriteing Index Table....";
                Application.DoEvents();

                for (int sc = 0; sc < NewMap.PCIndexHeader.IndexItems.Length; sc++)
                {
                    NewMap.PCIndexHeader.IndexItems[sc].StringOffset = (uint)MetaFile.Position + NewMap.PCIndexHeader.IndexMagic;
                    MetaFile.Write(NewMap.PCIndexHeader.IndexItems[sc].String, 0, NewMap.PCIndexHeader.IndexItems[sc].String.Length);
                }
                //save MetaFile position to return for meta writing.
                long TmpPosSave = MetaFile.Position;
                //lbCurTag.Text = "Writing Index Table with Srtring Offsets....";
                Application.DoEvents();

                for (int sc = 0; sc < NewMap.PCIndexHeader.IndexItems.Length; sc++)
                {
                    MetaFile.Seek(NewMap.PCIndexHeader.IndexItems[sc].Position, SeekOrigin.Begin);
                    NewMap.PCIndexHeader.IndexItems[sc].Write(ref MetaFile);
                }
                //restore position for meta writing
                MetaFile.Seek(TmpPosSave, SeekOrigin.Begin);

                //Add info to the Meta info struct
                PCMeta.StructInfo Info = new PCMeta.StructInfo();
                Info.MapMagic      = NewMap.PCIndexHeader.IndexMagic;
                Info.TagMagic      = NewMap.PCIndexHeader.IndexMagic;
                Info.StructurePath = StructFolder;
                Info.TagsPath      = BasFolder;
                Info.Items         = NewMap.PCIndexHeader.IndexItems;
                //Starts processing meta and writing it to the meta file
                //lbCurTag.Text = "Adding Tags to map....";
                Application.DoEvents();

                for (int sc = 0; sc < NewMap.PCIndexHeader.IndexItems.Length; sc++)
                {
                    if (NewMap.PCIndexHeader.IndexItems[sc].TagClass1 != 0x70736273) //   70736273)
                    {
                        PCMeta Meta = new PCMeta();
                        //FileInfo Tag_info = new FileInfo(BasFolder + IndexList[sc]);
                        //FileStream Tag;
                        //Tag = Tag_info.Open(FileMode.Open,FileAccess.Read);
                        //byte[] head = new byte[0x40];
                        //Tag.Read(head,0,head.Length);
                        TagFileName tfnTAG = new TagFileName(IndexList[sc], MapfileVersion.HALOPC);
                        TagBase     tbTAG  = new TagBase();
                        tbTAG.LoadTagBuffer(tfnTAG);

                        //string Class = Meta.GetTagClass(head,0x24);
                        //MAG sr = new MAG(StructFolder + Class.Trim() + ".mag");
                        //sr.Seek(1);
                        MTSFReader mr = new MTSFReader();
                        mr.MTSFRead("Core.Compiler.HALO_PC_SET.MTSF");

                        TSFReader STSF = new TSFReader();

                        //SwapInt(TagClasses,0);
                        STSF.TSF(ref mr, tbTAG.Header.TagClass0);



                        HaloPCMAP.FixIntPosition(ref MetaFile);
                        Info.CurrentOffset = (uint)MetaFile.Position;
                        NewMap.PCIndexHeader.IndexItems[sc].MetaOffset = (uint)(MetaFile.Position + NewMap.PCIndexHeader.IndexMagic);

                        Meta.DoProcessMeta(ref STSF, tbTAG.Stream, ref MetaFile, ref VertsFile, ref IndicesFile, ref BSPFile, 1, IndexList, Info);

                        tbTAG = null;
                        //Tag.Flush();
                        //Tag.Close();
                    }
                }
                //Create a new map header
                //lbCurTag.Text = "Creating New Map....";
                Application.DoEvents();

                NewMap.PCHeader.Create();
                string[] tmpName = scenario_tag.RelativePath.Split(new char[] { '\\' }, 256);
                tmpName = tmpName[tmpName.Length - 1].Split(new char[] { '.' }, 256);
                NewMap.PCHeader.MapName = tmpName[0];//otf.FileName.Split(new char[]{'/'},256   //"Cool";
                //NewMap.PCHeader.Write(ref HeadFile);
                //Rewrite indextable of meta offsets
                //lbCurTag.Text = "Fixing Meta Offsets in Index Table....";
                Application.DoEvents();

                TmpPosSave = MetaFile.Position;
                for (int sc = 0; sc < NewMap.PCIndexHeader.IndexItems.Length; sc++)
                {
                    MetaFile.Seek(NewMap.PCIndexHeader.IndexItems[sc].Position, SeekOrigin.Begin);
                    NewMap.PCIndexHeader.IndexItems[sc].Write(ref MetaFile);
                }
                MetaFile.Seek(TmpPosSave, SeekOrigin.Begin);

                //Create the new map file to begin construction of the new map.
                FileInfo   Map_info = new FileInfo(Application.StartupPath + NewMap.PCHeader.MapName + ".Map");
                FileStream Map;
                Map = Map_info.Open(FileMode.Create, FileAccess.ReadWrite);

                //lbCurTag.Text = "Copying Data to new map file....";
                Application.DoEvents();

                //Write the header to the new map file
                NewMap.PCHeader.Write(ref Map);
                //Flush the tmp BSPFile to get FileSize.
                BSPFile.Flush();
                uint BSPFileSize = (uint)BSPFile.Length;
                //Seek to the begining of the tmp bsp file
                BSPFile.Seek(0, SeekOrigin.Begin);
                //Copy the Tmp bsp file to the new map file
                for (int bc = 0; bc < BSPFileSize; bc += 4)
                {
                    byte[] copy = new byte[4];
                    BSPFile.Read(copy, 0, copy.Length);
                    Map.Write(copy, 0, copy.Length);
                }
                //Flush the Temp verts file to get File size;
                VertsFile.Flush();
                uint VertsFileSize = (uint)VertsFile.Length;
                //Put Current Map Position in the indexheader, this sets the position of the verts in the map.
                NewMap.PCIndexHeader.VertsOffset = (uint)Map.Position;
                //seek to begining of the Verts file for coping to the new map file
                VertsFile.Seek(0, SeekOrigin.Begin);
                //Copy the verts to the map file
                for (int bc = 0; bc < VertsFileSize; bc += 4)
                {
                    byte[] copy = new byte[4];
                    VertsFile.Read(copy, 0, copy.Length);
                    Map.Write(copy, 0, copy.Length);
                }
                //Flush Indeices file to get size
                IndicesFile.Flush();
                uint IndicesFileSize = (uint)IndicesFile.Length;
                //Put the Scnr index ID in the IndexHeader, you can move the scnr any were as long as you set this to the currect ID.
                NewMap.PCIndexHeader.ScnrID = (uint)((NewMap.PCIndexHeader.IndexItems[0].IndexID2 << 16) + NewMap.PCIndexHeader.IndexItems[0].IndexID1);
                //ajust the IndicesOffset with verts offset and map position
                NewMap.PCIndexHeader.IndicesOffset = (uint)Map.Position - NewMap.PCIndexHeader.VertsOffset;
                NewMap.PCIndexHeader.ModelAreaSize = VertsFileSize + IndicesFileSize;
                //Seek to begining of IndicesFile for coping
                IndicesFile.Seek(0, SeekOrigin.Begin);
                //Copy Indices to new map file
                for (int bc = 0; bc < IndicesFileSize; bc += 4)
                {
                    byte[] copy = new byte[4];
                    IndicesFile.Read(copy, 0, copy.Length);
                    Map.Write(copy, 0, copy.Length);
                }
                //put the Current map position in the header so we know where the index header will be
                NewMap.PCHeader.IndexOffset = (uint)Map.Position;
                //Now we write the Index header to the map.
                NewMap.PCIndexHeader.Write(ref Map);

                //Flush the temp meta file so we can get the size.
                MetaFile.Flush();
                uint MetaFileSize = (uint)MetaFile.Length;
                //seek to the begining of the temp meta file for coping to the new map file
                MetaFile.Seek(0, SeekOrigin.Begin);
                //Copy the meta file to the new map file.
                for (int bc = 0; bc < MetaFileSize; bc += 4)
                {
                    byte[] copy = new byte[4];
                    MetaFile.Read(copy, 0, copy.Length);
                    Map.Write(copy, 0, copy.Length);
                }
                //Now we fix the map version map size and rewrite it to the map.
                Map.Flush();
                NewMap.PCHeader.MapVersion = 0x07;
                NewMap.PCHeader.MapSize    = (uint)Map.Length;
                NewMap.PCHeader.MetaSize   = MetaFileSize + 0x28;
                Map.Seek(NewMap.PCHeader.Position, SeekOrigin.Begin);
                NewMap.PCHeader.Write(ref Map);


                //Now close the files and delete the temp files.
                VertsFile.Close();
                IndicesFile.Close();
                MetaFile.Flush();
                MetaFile.Close();
                BSPFile.Close();
                Map.Close();
                //FileStream TagFile;
                //TagFile = Tag
            }
        }
Beispiel #2
0
        public static void Build()
        {
            TagFileName  scenario_tag = ProjectManager.ScenarioTagFileName;           //new TagFileName(@"levels\test\beavercreek\beavercreek.scenario",MapfileVersion.XHALO1);
            IndexBuilder IndexBuild   = new IndexBuilder();
            string       BasFolder    = Application.StartupPath + @"\Extracted Tags\";
            string       StructFolder = Application.StartupPath + @"\Tag Structures\PcHalo\";

            if (true)
            {
                //Create the index table
                Application.DoEvents();
                string[] IndexList           = IndexBuild.BuildIndex(scenario_tag.RelativePath, BasFolder, StructFolder, MapfileVersion.XHALO1);
                XBoxHaloMap.sXBoxHalo NewMap = new XBoxHaloMap.sXBoxHalo();
                NewMap.XBoxIndexHeader.Create((uint)IndexList.Length);
                NewMap.XBoxIndexHeader.IndexMagic = 0x803a6024;                 //0x40440028;
                uint StringTableSize = 0;
                //Fixing index items
                ushort gt = 0xE174;
                Application.DoEvents();

                for (int sc = 0; sc < IndexList.Length; sc++)
                {
                    TagFileName tfnTAG = new TagFileName(IndexList[sc], MapfileVersion.XHALO1);
                    TagBase     tbTAG  = new TagBase();
                    tbTAG.LoadTagBuffer(tfnTAG);

                    NewMap.XBoxIndexHeader.IndexItems[sc].Create();
                    NewMap.XBoxIndexHeader.IndexItems[sc].TagClass1  = CompUtil.SwapUInt(tbTAG.Header.TagClass0);
                    NewMap.XBoxIndexHeader.IndexItems[sc].TagClass2  = CompUtil.SwapUInt(tbTAG.Header.TagClass1);
                    NewMap.XBoxIndexHeader.IndexItems[sc].TagClass3  = CompUtil.SwapUInt(tbTAG.Header.TagClass2);
                    NewMap.XBoxIndexHeader.IndexItems[sc].IndexID1   = (ushort)sc;
                    NewMap.XBoxIndexHeader.IndexItems[sc].IndexID2   = gt;
                    NewMap.XBoxIndexHeader.IndexItems[sc].NameString = IndexList[sc];
                    if (tbTAG.Header.TagClass0 == 0x70736273 || tbTAG.Header.TagClass0 == 0x73627370)
                    {
                        NewMap.XBoxIndexHeader.IndexItems[sc].MetaSize = IndexBuild.BSPSizes[0];
                    }
                    else
                    {
                        NewMap.XBoxIndexHeader.IndexItems[sc].MetaSize = 0;
                    }
                    if (tbTAG.Header.TagClass0 == 0x7274656d || tbTAG.Header.TagClass0 == 0x6d657472)                   //  6d657472)
                    {
                        gt += 2;
                    }
                    else
                    {
                        gt += 1;
                    }
                    string[] RemoveExt = IndexList[sc].Split(new char[] { '.' }, 256);
                    NewMap.XBoxIndexHeader.IndexItems[sc].String = new byte[RemoveExt[0].Length + 1];
                    HaloPCMAP.StringToByteArray(ref NewMap.XBoxIndexHeader.IndexItems[sc].String, RemoveExt[0]);
                    StringTableSize += (uint)(IndexList[sc].Length + 1);
                    tbTAG            = null;
                }
                //Creating the temp files for map building
                FileInfo   MetaFile_info = new FileInfo(Application.StartupPath + "TempMeta.map");
                FileStream MetaFile;
                MetaFile = MetaFile_info.Open(FileMode.Create, FileAccess.ReadWrite);

                FileInfo   BSPFile_info = new FileInfo(Application.StartupPath + "TempBSP.map");
                FileStream BSPFile;
                BSPFile = BSPFile_info.Open(FileMode.Create, FileAccess.ReadWrite);

                Application.DoEvents();

                //writing the index table the temp files
                for (int sc = 0; sc < NewMap.XBoxIndexHeader.IndexItems.Length; sc++)
                {
                    NewMap.XBoxIndexHeader.IndexItems[sc].Write(ref MetaFile);
                }
                //Rewriting index items to temp file to fix string offsets.
                Application.DoEvents();

                for (int sc = 0; sc < NewMap.XBoxIndexHeader.IndexItems.Length; sc++)
                {
                    NewMap.XBoxIndexHeader.IndexItems[sc].StringOffset = (uint)MetaFile.Position + NewMap.XBoxIndexHeader.IndexMagic;
                    MetaFile.Write(NewMap.XBoxIndexHeader.IndexItems[sc].String, 0, NewMap.XBoxIndexHeader.IndexItems[sc].String.Length);
                }
                CompUtil.FixIntPosition(ref MetaFile);
                //save MetaFile position to return for meta writing.
                long TmpPosSave = MetaFile.Position;
                Application.DoEvents();

                for (int sc = 0; sc < NewMap.XBoxIndexHeader.IndexItems.Length; sc++)
                {
                    MetaFile.Seek(NewMap.XBoxIndexHeader.IndexItems[sc].Position, SeekOrigin.Begin);
                    NewMap.XBoxIndexHeader.IndexItems[sc].Write(ref MetaFile);
                }
                //restore position for meta writing
                #region ModelData
                MetaFile.Seek(TmpPosSave, SeekOrigin.Begin);
                NewMap.XBoxIndexHeader.VertsOffset = (uint)MetaFile.Position + NewMap.XBoxIndexHeader.IndexMagic;
                NewMap.XBoxIndexHeader.ModelCount1 = (uint)IndexBuild.XBoxMapData.XVerts.ModelCount;
                NewMap.IndirectVerts = (uint)MetaFile.Position;
                IndexBuild.XBoxMapData.XVerts.FixOffsets((uint)MetaFile.Position, NewMap.XBoxIndexHeader.IndexMagic);
                MetaFile.Write(IndexBuild.XBoxMapData.XVerts.IndirectOffsets, 0, IndexBuild.XBoxMapData.XVerts.IndirectOffsets.Length);
                MetaFile.Write(IndexBuild.XBoxMapData.XVerts.Verts, 0, IndexBuild.XBoxMapData.XVerts.Verts.Length);
                NewMap.XBoxIndexHeader.ModelCount2   = (uint)IndexBuild.XBoxMapData.XIndices.ModelCount;
                NewMap.XBoxIndexHeader.IndicesOffset = (uint)MetaFile.Position + NewMap.XBoxIndexHeader.IndexMagic;
                NewMap.IndirectIndices = (uint)MetaFile.Position;
                IndexBuild.XBoxMapData.XIndices.FixOffsets((uint)MetaFile.Position, NewMap.XBoxIndexHeader.IndexMagic);
                MetaFile.Write(IndexBuild.XBoxMapData.XIndices.IndirectOffsets, 0, IndexBuild.XBoxMapData.XIndices.IndirectOffsets.Length);
                MetaFile.Write(IndexBuild.XBoxMapData.XIndices.Verts, 0, IndexBuild.XBoxMapData.XIndices.Verts.Length);
                #endregion
                //Add info to the Meta info struct
                XBoxMeta.StructInfo Info = new XBoxMeta.StructInfo();
                Info.MapMagic      = NewMap.XBoxIndexHeader.IndexMagic;
                Info.TagMagic      = NewMap.XBoxIndexHeader.IndexMagic;
                Info.StructurePath = StructFolder;
                Info.TagsPath      = BasFolder;
                Info.Items         = NewMap.XBoxIndexHeader.IndexItems;
                //Starts processing meta and writing it to the meta file
                Application.DoEvents();

                for (int sc = 0; sc < NewMap.XBoxIndexHeader.IndexItems.Length; sc++)
                {
                    if (NewMap.XBoxIndexHeader.IndexItems[sc].TagClass1 != 0x70736273)                     //   70736273)
                    {
                        XBoxMeta Meta = new XBoxMeta();
                        Trace.WriteLine("Meta Processor: " + IndexList[sc]);
                        TagFileName tfnTAG = new TagFileName(IndexList[sc], MapfileVersion.XHALO1);
                        TagBase     tbTAG  = new TagBase();
                        tbTAG.LoadTagBuffer(tfnTAG);

                        MTSFReader mr = new MTSFReader();
                        mr.MTSFRead("Core.Compiler.HALO_PC_SET.MTSF");

                        TSFReader STSF = new TSFReader();

                        STSF.TSF(ref mr, tbTAG.Header.TagClass0);

                        HaloPCMAP.FixIntPosition(ref MetaFile);
                        Info.CurrentOffset = (uint)MetaFile.Position;
                        NewMap.XBoxIndexHeader.IndexItems[sc].MetaOffset = (uint)(MetaFile.Position + NewMap.XBoxIndexHeader.IndexMagic);

                        Meta.DoProcessMeta(ref STSF, tbTAG.Stream, ref MetaFile, ref BSPFile, 1, IndexList, Info, ref NewMap, ref IndexBuild);

                        tbTAG = null;
                    }
                }
                //Create a new map header
                Application.DoEvents();

                NewMap.XBoxHeader.Create();
                string[] tmpName = scenario_tag.RelativePath.Split(new char[] { '\\' }, 256);
                tmpName = tmpName[tmpName.Length - 1].Split(new char[] { '.' }, 256);
                NewMap.XBoxHeader.MapName = tmpName[0];                //otf.FileName.Split(new char[]{'/'},256   //"Cool";
                //Rewrite indextable of meta offsets
                Application.DoEvents();

                TmpPosSave = MetaFile.Position;
                for (int sc = 0; sc < NewMap.XBoxIndexHeader.IndexItems.Length; sc++)
                {
                    MetaFile.Seek(NewMap.XBoxIndexHeader.IndexItems[sc].Position, SeekOrigin.Begin);
                    NewMap.XBoxIndexHeader.IndexItems[sc].Write(ref MetaFile);
                }
                MetaFile.Seek(TmpPosSave, SeekOrigin.Begin);

                //Create the new map file to begin construction of the new map.
                FileInfo   Map_info = new FileInfo(Application.StartupPath + NewMap.XBoxHeader.MapName + ".Map");
                FileStream Map;
                Map = Map_info.Open(FileMode.Create, FileAccess.ReadWrite);

                Application.DoEvents();

                //Write the header to the new map file
                NewMap.XBoxHeader.Write(ref Map);
                //Flush the tmp BSPFile to get FileSize.
                CompUtil.FixIntPosition2K(ref BSPFile);
                BSPFile.Flush();
                uint BSPFileSize = (uint)BSPFile.Length;
                //Seek to the begining of the tmp bsp file
                BSPFile.Seek(0, SeekOrigin.Begin);
                //Copy the Tmp bsp file to the new map file
                for (int bc = 0; bc < BSPFileSize; bc += 0x800)
                {
                    byte[] copy = new byte[0x800];
                    BSPFile.Read(copy, 0, copy.Length);
                    Map.Write(copy, 0, copy.Length);
                }
                //Put the Scnr index ID in the IndexHeader, you can move the scnr any were as long as you set this to the currect ID.
                NewMap.XBoxIndexHeader.ScnrID = (uint)((NewMap.XBoxIndexHeader.IndexItems[0].IndexID2 << 16) + NewMap.XBoxIndexHeader.IndexItems[0].IndexID1);
                //put the Current map position in the header so we know where the index header will be
                NewMap.XBoxHeader.IndexOffset = (uint)Map.Position;
                //Now we write the Index header to the map.
                NewMap.XBoxIndexHeader.Write(ref Map);

                //Flush the temp meta file so we can get the size.
                MetaFile.Flush();
                uint MetaFileSize = (uint)MetaFile.Length;
                //seek to the begining of the temp meta file for coping to the new map file
                MetaFile.Seek(0, SeekOrigin.Begin);
                //Copy the meta file to the new map file.
                for (int bc = 0; bc < MetaFileSize; bc += 0x800)
                {
                    byte[] copy = new byte[0x800];
                    MetaFile.Read(copy, 0, copy.Length);
                    Map.Write(copy, 0, copy.Length);
                }
                //Now we fix the map version map size and rewrite it to the map.
                Map.Flush();
                NewMap.XBoxHeader.MapVersion = 0x05;
                NewMap.XBoxHeader.MapSize    = (uint)Map.Length;
                NewMap.XBoxHeader.MetaSize   = MetaFileSize + 0x24;
                Map.Seek(NewMap.XBoxHeader.Position, SeekOrigin.Begin);
                NewMap.XBoxHeader.Write(ref Map);

                //Now close the files and delete the temp files.
                MetaFile.Flush();
                MetaFile.Close();
                File.Delete(MetaFile.Name);
                BSPFile.Close();
                File.Delete(BSPFile.Name);
                Map.Close();
            }
        }