private void OnClickCopy(object sender, EventArgs e) { int x1 = (int)numericUpDownX1.Value; int x2 = (int)numericUpDownX2.Value; int y1 = (int)numericUpDownY1.Value; int y2 = (int)numericUpDownY2.Value; if ((x1<0) || (x1>workingmap.Width)) { MessageBox.Show("Invalid X1 coordinate!", "Map Diff Insert", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((x2<0) || (x2>workingmap.Width)) { MessageBox.Show("Invalid X2 coordinate!", "Map Diff Insert", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((y1 < 0) || (y1 > workingmap.Height)) { MessageBox.Show("Invalid Y1 coordinate!", "Map Diff Insert", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((y2 < 0) || (y2 > workingmap.Height)) { MessageBox.Show("Invalid Y2 coordinate!", "Map Diff Insert", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((x1 > x2) || (y1 > y2)) { MessageBox.Show("X1 and Y1 cannot be bigger than X2 and Y2!", "Map Diff Insert", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } x1 >>= 3; x2 >>= 3; y1 >>= 3; y2 >>= 3; int blocky = workingmap.Height >> 3; int blockx = workingmap.Width >> 3; progressBar1.Step = 1; progressBar1.Value = 0; progressBar1.Maximum=0; if (checkBoxMap.Checked) progressBar1.Maximum += blocky * blockx; if (checkBoxStatics.Checked) progressBar1.Maximum += blocky * blockx; if (checkBoxMap.Checked) { string mapPath = Ultima.Files.GetFilePath(String.Format("map{0}.mul", workingmap.FileIndex)); FileStream m_map; BinaryReader m_mapReader; if (mapPath != null) { m_map = new FileStream(mapPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_mapReader = new BinaryReader(m_map); } else { MessageBox.Show("Map file not found!", "Map Diff Insert", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } string mul = Path.Combine(FiddlerControls.Options.OutputPath, String.Format("map{0}.mul", workingmap.FileIndex)); using (FileStream fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { using (BinaryWriter binmul = new BinaryWriter(fsmul)) { for (int x = 0; x < blockx; ++x) { for (int y = 0; y < blocky; ++y) { m_mapReader.BaseStream.Seek(((x * blocky) + y) * 196, SeekOrigin.Begin); int header = m_mapReader.ReadInt32(); binmul.Write(header); ushort tileid; sbyte z; bool patched = false; if ((x1 <= x) && (x <= x2) && (y1 <= y) && (y <= y2)) { if (workingmap.Tiles.Patch.IsLandBlockPatched(x, y)) { patched = true; Tile[] patchtile = workingmap.Tiles.Patch.GetLandBlock(x, y); for (int i = 0; i < 64; ++i) { tileid = patchtile[i].ID; z = (sbyte)patchtile[i].Z; tileid = Art.GetLegalItemID(tileid); if (z < -128) z = -128; if (z > 127) z = 127; binmul.Write(tileid); binmul.Write(z); } } } if (!patched) { for (int i = 0; i < 64; ++i) { tileid = m_mapReader.ReadUInt16(); z = m_mapReader.ReadSByte(); tileid = Art.GetLegalItemID(tileid); if (z < -128) z = -128; if (z > 127) z = 127; binmul.Write(tileid); binmul.Write(z); } } progressBar1.PerformStep(); } } } } m_mapReader.Close(); } if (checkBoxStatics.Checked) { string indexPath = Files.GetFilePath(String.Format("staidx{0}.mul", workingmap.FileIndex)); FileStream m_Index; BinaryReader m_IndexReader; if (indexPath != null) { m_Index = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_IndexReader = new BinaryReader(m_Index); } else { MessageBox.Show("Static file not found!", "Map Diff Insert", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } string staticsPath = Files.GetFilePath(String.Format("statics{0}.mul", workingmap.FileIndex)); FileStream m_Statics; BinaryReader m_StaticsReader; if (staticsPath != null) { m_Statics = new FileStream(staticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_StaticsReader = new BinaryReader(m_Statics); } else { MessageBox.Show("Static file not found!", "Map Diff Insert", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } string idx = Path.Combine(FiddlerControls.Options.OutputPath, String.Format("staidx{0}.mul", workingmap.FileIndex)); string mul = Path.Combine(FiddlerControls.Options.OutputPath, String.Format("statics{0}.mul", workingmap.FileIndex)); using (FileStream fsidx = new FileStream(idx, FileMode.Create, FileAccess.Write, FileShare.Write), fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { using (BinaryWriter binidx = new BinaryWriter(fsidx), binmul = new BinaryWriter(fsmul)) { for (int x = 0; x < blockx; ++x) { for (int y = 0; y < blocky; ++y) { int lookup, length, extra; m_IndexReader.BaseStream.Seek(((x * blocky) + y) * 12, SeekOrigin.Begin); lookup = m_IndexReader.ReadInt32(); length = m_IndexReader.ReadInt32(); extra = m_IndexReader.ReadInt32(); bool patched = false; if ((x1 <= x) && (x <= x2) && (y1 <= y) && (y <= y2)) { if (workingmap.Tiles.Patch.IsStaticBlockPatched(x, y)) patched = true; } if (patched) { HuedTile[][][] patchstat = workingmap.Tiles.Patch.GetStaticBlock(x, y); int count = 0; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { if (patchstat[i][j] != null) count += patchstat[i][j].Length; } } if (count == 0) { binidx.Write((int)-1); // lookup binidx.Write((int)-1); // length binidx.Write((int)-1); // extra } else { int fsmullength = (int)fsmul.Position; if (RemoveDupl.Checked) { StaticTile[] tilelist = new StaticTile[count]; int m = 0; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { foreach (HuedTile htile in patchstat[i][j]) { StaticTile tile = new StaticTile(); tile.m_ID = (ushort)(htile.ID); tile.m_Z = (sbyte)htile.Z; tile.m_X = (byte)i; tile.m_Y = (byte)j; tile.m_Hue = (short)htile.Hue; if ((tile.m_ID >= 0) && (tile.m_ID <= Art.GetMaxItemID())) { if (tile.m_Hue < 0) tile.m_Hue = 0; bool first = true; for (int k = 0; k < m; ++k) { if ((tilelist[k].m_ID == tile.m_ID) && ((tilelist[k].m_X == tile.m_X) && (tilelist[k].m_Y == tile.m_Y)) && (tilelist[k].m_Z == tile.m_Z) && (tilelist[k].m_Hue == tile.m_Hue)) { first = false; break; } } if (first) { tilelist[m] = tile; ++m; } } } } } if (m > 0) { binidx.Write((int)fsmul.Position); //lookup for (int i = 0; i < m; ++i) { binmul.Write(tilelist[i].m_ID); binmul.Write(tilelist[i].m_X); binmul.Write(tilelist[i].m_Y); binmul.Write(tilelist[i].m_Z); binmul.Write(tilelist[i].m_Hue); } } } else { ushort graphic; short shue; sbyte sz; bool firstitem = true; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { foreach (HuedTile tile in patchstat[i][j]) { graphic = tile.ID; sz = (sbyte)tile.Z; shue = (short)tile.Hue; if ((graphic >= 0) && (graphic <= Art.GetMaxItemID())) { if (shue < 0) shue = 0; if (firstitem) { binidx.Write((int)fsmul.Position); //lookup firstitem = false; } binmul.Write(graphic); binmul.Write((byte)i); //x binmul.Write((byte)j); //y binmul.Write(sz); binmul.Write(shue); } } } } } fsmullength = (int)fsmul.Position - fsmullength; if (fsmullength > 0) { binidx.Write(fsmullength); //length binidx.Write(extra); //extra } else { binidx.Write((int)-1); //lookup binidx.Write((int)-1); //length binidx.Write((int)-1); //extra } } } else { if (lookup < 0 || length <= 0) { binidx.Write((int)-1); // lookup binidx.Write((int)-1); // length binidx.Write((int)-1); // extra } else { m_Statics.Seek(lookup, SeekOrigin.Begin); int fsmullength = (int)fsmul.Position; int count = length / 7; if (RemoveDupl.Checked) { StaticTile[] tilelist = new StaticTile[count]; int j = 0; for (int i = 0; i < count; ++i) { StaticTile tile = new StaticTile(); tile.m_ID = m_StaticsReader.ReadUInt16(); tile.m_X = m_StaticsReader.ReadByte(); tile.m_Y = m_StaticsReader.ReadByte(); tile.m_Z = m_StaticsReader.ReadSByte(); tile.m_Hue = m_StaticsReader.ReadInt16(); if ((tile.m_ID >= 0) && (tile.m_ID <= Art.GetMaxItemID())) { if (tile.m_Hue < 0) tile.m_Hue = 0; bool first = true; for (int k = 0; k < j; ++k) { if ((tilelist[k].m_ID == tile.m_ID) && ((tilelist[k].m_X == tile.m_X) && (tilelist[k].m_Y == tile.m_Y)) && (tilelist[k].m_Z == tile.m_Z) && (tilelist[k].m_Hue == tile.m_Hue)) { first = false; break; } } if (first) tilelist[j++] = tile; } } if (j > 0) { binidx.Write((int)fsmul.Position); //lookup for (int i = 0; i < j; ++i) { binmul.Write(tilelist[i].m_ID); binmul.Write(tilelist[i].m_X); binmul.Write(tilelist[i].m_Y); binmul.Write(tilelist[i].m_Z); binmul.Write(tilelist[i].m_Hue); } } } else { bool firstitem = true; for (int i = 0; i < count; ++i) { ushort graphic; short shue; byte sx, sy; sbyte sz; graphic = m_StaticsReader.ReadUInt16(); sx = m_StaticsReader.ReadByte(); sy = m_StaticsReader.ReadByte(); sz = m_StaticsReader.ReadSByte(); shue = m_StaticsReader.ReadInt16(); if ((graphic >= 0) && (graphic <= Art.GetMaxItemID())) { if (shue < 0) shue = 0; if (firstitem) { binidx.Write((int)fsmul.Position); //lookup firstitem = false; } binmul.Write(graphic); binmul.Write(sx); binmul.Write(sy); binmul.Write(sz); binmul.Write(shue); } } } fsmullength = (int)fsmul.Position - fsmullength; if (fsmullength > 0) { binidx.Write(fsmullength); //length binidx.Write(extra); //extra } else { binidx.Write((int)-1); //lookup binidx.Write((int)-1); //length binidx.Write((int)-1); //extra } } } progressBar1.PerformStep(); } } } } m_IndexReader.Close(); m_StaticsReader.Close(); } MessageBox.Show(String.Format("Files saved to {0}", FiddlerControls.Options.OutputPath), "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); }
private void OnClickCopy(object sender, EventArgs e) { string path = textBox1.Text; if (!Directory.Exists(path)) { MessageBox.Show("Path not found!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } SupportedMaps replacemap = comboBoxMapID.SelectedItem as SupportedMaps; if (replacemap == null) { MessageBox.Show("Invalid Map ID!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } int x1 = (int)numericUpDownX1.Value; int x2 = (int)numericUpDownX2.Value; int y1 = (int)numericUpDownY1.Value; int y2 = (int)numericUpDownY2.Value; int tox = (int)numericUpDownToX1.Value; int toy = (int)numericUpDownToY1.Value; if ((x1 < 0) || (x1 > replacemap.Width)) { MessageBox.Show("Invalid X1 coordinate!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((x2 < 0) || (x2 > replacemap.Width)) { MessageBox.Show("Invalid X2 coordinate!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((y1 < 0) || (y1 > replacemap.Height)) { MessageBox.Show("Invalid Y1 coordinate!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((y2 < 0) || (y2 > replacemap.Height)) { MessageBox.Show("Invalid Y2 coordinate!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((x1 > x2) || (y1 > y2)) { MessageBox.Show("X1 and Y1 cannot be bigger than X2 and Y2!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((tox < 0) || (tox > workingmap.Width) || ((tox + (x2 - x1)) > workingmap.Width)) { MessageBox.Show("Invalid toX coordinate!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } if ((toy < 0) || (toy > workingmap.Height) || ((toy + (y2 - y1)) > workingmap.Height)) { MessageBox.Show("Invalid toX coordinate!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } x1 >>= 3; x2 >>= 3; y1 >>= 3; y2 >>= 3; tox >>= 3; toy >>= 3; int tox2 = (x2 - x1) + tox; int toy2 = (y2 - y1) + toy; int blocky = workingmap.Height >> 3; int blockx = workingmap.Width >> 3; int blockyreplace = replacemap.Height >> 3; int blockxreplace = replacemap.Width >> 3; progressBar1.Step = 1; progressBar1.Value = 0; progressBar1.Maximum=0; if (checkBoxMap.Checked) progressBar1.Maximum += blocky * blockx; if (checkBoxStatics.Checked) progressBar1.Maximum += blocky * blockx; if (checkBoxMap.Checked) { string copymap = Path.Combine(path, String.Format("map{0}.mul", replacemap.ID)); if (!File.Exists(copymap)) { MessageBox.Show("Map file not found!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } FileStream m_map_copy = new FileStream(copymap, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader m_mapReader_copy = new BinaryReader(m_map_copy); string mapPath = Ultima.Files.GetFilePath(String.Format("map{0}.mul", workingmap.FileIndex)); FileStream m_map; BinaryReader m_mapReader; if (mapPath != null) { m_map = new FileStream(mapPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_mapReader = new BinaryReader(m_map); } else { MessageBox.Show("Map file not found!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } string mul = Path.Combine(FiddlerControls.Options.OutputPath, String.Format("map{0}.mul", workingmap.FileIndex)); using (FileStream fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { using (BinaryWriter binmul = new BinaryWriter(fsmul)) { for (int x = 0; x < blockx; ++x) { for (int y = 0; y < blocky; ++y) { if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { m_mapReader_copy.BaseStream.Seek((((x - tox + x1) * blockyreplace) + (y - toy + y1)) * 196, SeekOrigin.Begin); int header = m_mapReader_copy.ReadInt32(); binmul.Write(header); } else { m_mapReader.BaseStream.Seek(((x * blocky) + y) * 196, SeekOrigin.Begin); int header = m_mapReader.ReadInt32(); binmul.Write(header); } for (int i = 0; i < 64; ++i) { ushort tileid; sbyte z; if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { tileid = m_mapReader_copy.ReadUInt16(); z = m_mapReader_copy.ReadSByte(); } else { tileid = m_mapReader.ReadUInt16(); z = m_mapReader.ReadSByte(); } tileid = Art.GetLegalItemID(tileid); if (z < -128) z = -128; if (z > 127) z = 127; binmul.Write(tileid); binmul.Write(z); } progressBar1.PerformStep(); } } } } m_mapReader.Close(); m_mapReader_copy.Close(); } if (checkBoxStatics.Checked) { string indexPath = Files.GetFilePath(String.Format("staidx{0}.mul", workingmap.FileIndex)); FileStream m_Index; BinaryReader m_IndexReader; if (indexPath != null) { m_Index = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_IndexReader = new BinaryReader(m_Index); } else { MessageBox.Show("Static file not found!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } string staticsPath = Files.GetFilePath(String.Format("statics{0}.mul", workingmap.FileIndex)); FileStream m_Statics; BinaryReader m_StaticsReader; if (staticsPath != null) { m_Statics = new FileStream(staticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_StaticsReader = new BinaryReader(m_Statics); } else { MessageBox.Show("Static file not found!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } string copyindexPath = Path.Combine(path, String.Format("staidx{0}.mul", replacemap.ID)); if (!File.Exists(copyindexPath)) { MessageBox.Show("Static file not found!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } FileStream m_Index_copy = new FileStream(copyindexPath, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader m_IndexReader_copy = new BinaryReader(m_Index_copy); string copystaticsPath = Path.Combine(path, String.Format("statics{0}.mul", replacemap.ID)); if (!File.Exists(copystaticsPath)) { MessageBox.Show("Static file not found!", "Map Replace", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } FileStream m_Statics_copy = new FileStream(copystaticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader m_StaticsReader_copy = new BinaryReader(m_Statics_copy); string idx = Path.Combine(FiddlerControls.Options.OutputPath, String.Format("staidx{0}.mul", workingmap.FileIndex)); string mul = Path.Combine(FiddlerControls.Options.OutputPath, String.Format("statics{0}.mul", workingmap.FileIndex)); using (FileStream fsidx = new FileStream(idx, FileMode.Create, FileAccess.Write, FileShare.Write), fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { using (BinaryWriter binidx = new BinaryWriter(fsidx), binmul = new BinaryWriter(fsmul)) { for (int x = 0; x < blockx; ++x) { for (int y = 0; y < blocky; ++y) { int lookup, length, extra; if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { m_IndexReader_copy.BaseStream.Seek((((x - tox + x1) * blockyreplace) + (y - toy + y1)) * 12, SeekOrigin.Begin); lookup = m_IndexReader_copy.ReadInt32(); length = m_IndexReader_copy.ReadInt32(); extra = m_IndexReader_copy.ReadInt32(); } else { m_IndexReader.BaseStream.Seek(((x * blocky) + y) * 12, SeekOrigin.Begin); lookup = m_IndexReader.ReadInt32(); length = m_IndexReader.ReadInt32(); extra = m_IndexReader.ReadInt32(); } if (lookup < 0 || length <= 0) { binidx.Write((int)-1); // lookup binidx.Write((int)-1); // length binidx.Write((int)-1); // extra } else { if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) m_Statics_copy.Seek(lookup, SeekOrigin.Begin); else m_Statics.Seek(lookup, SeekOrigin.Begin); int fsmullength = (int)fsmul.Position; int count = length / 7; if (RemoveDupl.Checked) { StaticTile[] tilelist = new StaticTile[count]; int j = 0; for (int i = 0; i < count; ++i) { StaticTile tile = new StaticTile(); if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { tile.m_ID = m_StaticsReader_copy.ReadUInt16(); tile.m_X = m_StaticsReader_copy.ReadByte(); tile.m_Y = m_StaticsReader_copy.ReadByte(); tile.m_Z = m_StaticsReader_copy.ReadSByte(); tile.m_Hue = m_StaticsReader_copy.ReadInt16(); } else { tile.m_ID = m_StaticsReader.ReadUInt16(); tile.m_X = m_StaticsReader.ReadByte(); tile.m_Y = m_StaticsReader.ReadByte(); tile.m_Z = m_StaticsReader.ReadSByte(); tile.m_Hue = m_StaticsReader.ReadInt16(); } if ((tile.m_ID >= 0) && (tile.m_ID <= Art.GetMaxItemID())) { if (tile.m_Hue < 0) tile.m_Hue = 0; bool first = true; for (int k = 0; k < j; ++k) { if ((tilelist[k].m_ID == tile.m_ID) && ((tilelist[k].m_X == tile.m_X) && (tilelist[k].m_Y == tile.m_Y)) && (tilelist[k].m_Z == tile.m_Z) && (tilelist[k].m_Hue == tile.m_Hue)) { first = false; break; } } if (first) tilelist[j++] = tile; } } if (j > 0) { binidx.Write((int)fsmul.Position); //lookup for (int i = 0; i < j; ++i) { binmul.Write(tilelist[i].m_ID); binmul.Write(tilelist[i].m_X); binmul.Write(tilelist[i].m_Y); binmul.Write(tilelist[i].m_Z); binmul.Write(tilelist[i].m_Hue); } } } else { bool firstitem = true; for (int i = 0; i < count; ++i) { ushort graphic; short shue; byte sx, sy; sbyte sz; if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { graphic = m_StaticsReader_copy.ReadUInt16(); sx = m_StaticsReader_copy.ReadByte(); sy = m_StaticsReader_copy.ReadByte(); sz = m_StaticsReader_copy.ReadSByte(); shue = m_StaticsReader_copy.ReadInt16(); } else { graphic = m_StaticsReader.ReadUInt16(); sx = m_StaticsReader.ReadByte(); sy = m_StaticsReader.ReadByte(); sz = m_StaticsReader.ReadSByte(); shue = m_StaticsReader.ReadInt16(); } if ((graphic >= 0) && (graphic <= Art.GetMaxItemID())) { if (shue < 0) shue = 0; if (firstitem) { binidx.Write((int)fsmul.Position); //lookup firstitem = false; } binmul.Write(graphic); binmul.Write(sx); binmul.Write(sy); binmul.Write(sz); binmul.Write(shue); } } } fsmullength = (int)fsmul.Position - fsmullength; if (fsmullength > 0) { binidx.Write(fsmullength); //length binidx.Write(extra); //extra } else { binidx.Write((int)-1); //lookup binidx.Write((int)-1); //length binidx.Write((int)-1); //extra } } progressBar1.PerformStep(); } } } } m_IndexReader.Close(); m_StaticsReader.Close(); m_Index_copy.Close(); m_StaticsReader_copy.Close(); } MessageBox.Show(String.Format("Files saved to {0}", FiddlerControls.Options.OutputPath), "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); }
private unsafe int PatchStatics( TileMatrix matrix, string dataPath, string indexPath, string lookupPath ) { using ( FileStream fsData = new FileStream( dataPath, FileMode.Open, FileAccess.Read, FileShare.Read ) ) { using ( FileStream fsIndex = new FileStream( indexPath, FileMode.Open, FileAccess.Read, FileShare.Read ) ) { using ( FileStream fsLookup = new FileStream( lookupPath, FileMode.Open, FileAccess.Read, FileShare.Read ) ) { BinaryReader indexReader = new BinaryReader( fsIndex ); BinaryReader lookupReader = new BinaryReader( fsLookup ); int count = (int)(indexReader.BaseStream.Length / 4); HuedTileList[][] lists = new HuedTileList[8][]; for ( int x = 0; x < 8; ++x ) { lists[x] = new HuedTileList[8]; for ( int y = 0; y < 8; ++y ) lists[x][y] = new HuedTileList(); } for ( int i = 0; i < count; ++i ) { int blockID = indexReader.ReadInt32(); int blockX = blockID / matrix.BlockHeight; int blockY = blockID % matrix.BlockHeight; int offset = lookupReader.ReadInt32(); int length = lookupReader.ReadInt32(); lookupReader.ReadInt32(); // Extra if ( offset < 0 || length <= 0 ) { matrix.SetStaticBlock( blockX, blockY, matrix.EmptyStaticBlock ); continue; } fsData.Seek( offset, SeekOrigin.Begin ); int tileCount = length / 7; StaticTile[] staTiles = new StaticTile[tileCount]; fixed ( StaticTile *pTiles = staTiles ) { _lread(fsData.SafeFileHandle, pTiles, length); StaticTile *pCur = pTiles, pEnd = pTiles + tileCount; while ( pCur < pEnd ) { lists[pCur->m_X & 0x7][pCur->m_Y & 0x7].Add( (short)((pCur->m_ID & 0x3FFF) + 0x4000), pCur->m_Hue, pCur->m_Z ); ++pCur; } HuedTile[][][] tiles = new HuedTile[8][][]; for ( int x = 0; x < 8; ++x ) { tiles[x] = new HuedTile[8][]; for ( int y = 0; y < 8; ++y ) tiles[x][y] = lists[x][y].ToArray(); } matrix.SetStaticBlock( blockX, blockY, tiles ); } } return count; } } } }
public static void DefragStatics(string path, Map map, int width, int height, bool remove) { string indexPath = Files.GetFilePath("staidx{0}.mul", map.FileIndex); FileStream m_Index; BinaryReader m_IndexReader; if (indexPath != null) { m_Index = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_IndexReader = new BinaryReader(m_Index); } else return; string staticsPath = Files.GetFilePath("statics{0}.mul", map.FileIndex); FileStream m_Statics; BinaryReader m_StaticsReader; if (staticsPath != null) { m_Statics = new FileStream(staticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_StaticsReader = new BinaryReader(m_Statics); } else return; int blockx = width >> 3; int blocky = height >> 3; string idx = Path.Combine(path, String.Format("staidx{0}.mul", map.FileIndex)); string mul = Path.Combine(path, String.Format("statics{0}.mul", map.FileIndex)); using (FileStream fsidx = new FileStream(idx, FileMode.Create, FileAccess.Write, FileShare.Write), fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { MemoryStream memidx = new MemoryStream(); MemoryStream memmul = new MemoryStream(); using (BinaryWriter binidx = new BinaryWriter(memidx), binmul = new BinaryWriter(memmul)) { for (int x = 0; x < blockx; ++x) { for (int y = 0; y < blocky; ++y) { try { m_IndexReader.BaseStream.Seek(((x * blocky) + y) * 12, SeekOrigin.Begin); int lookup = m_IndexReader.ReadInt32(); int length = m_IndexReader.ReadInt32(); int extra = m_IndexReader.ReadInt32(); if (((lookup < 0 || length <= 0) && (!map.Tiles.PendingStatic(x, y))) || (map.Tiles.IsStaticBlockRemoved(x, y))) { binidx.Write((int)-1); // lookup binidx.Write((int)-1); // length binidx.Write((int)-1); // extra } else { if ((lookup >= 0) && (length > 0)) m_Statics.Seek(lookup, SeekOrigin.Begin); int fsmullength = (int)binmul.BaseStream.Position; int count = length / 7; if (!remove) //without duplicate remove { bool firstitem = true; for (int i = 0; i < count; ++i) { ushort graphic = m_StaticsReader.ReadUInt16(); byte sx = m_StaticsReader.ReadByte(); byte sy = m_StaticsReader.ReadByte(); sbyte sz = m_StaticsReader.ReadSByte(); short shue = m_StaticsReader.ReadInt16(); if ((graphic >= 0) && (graphic <= Art.GetMaxItemID())) { if (shue < 0) shue = 0; if (firstitem) { binidx.Write((int)binmul.BaseStream.Position); //lookup firstitem = false; } binmul.Write(graphic); binmul.Write(sx); binmul.Write(sy); binmul.Write(sz); binmul.Write(shue); } } StaticTile[] tilelist = map.Tiles.GetPendingStatics(x, y); if (tilelist != null) { for (int i = 0; i < tilelist.Length; ++i) { if ((tilelist[i].m_ID >= 0) && (tilelist[i].m_ID <= Art.GetMaxItemID())) { if (tilelist[i].m_Hue < 0) tilelist[i].m_Hue = 0; if (firstitem) { binidx.Write((int)binmul.BaseStream.Position); //lookup firstitem = false; } binmul.Write(tilelist[i].m_ID); binmul.Write(tilelist[i].m_X); binmul.Write(tilelist[i].m_Y); binmul.Write(tilelist[i].m_Z); binmul.Write(tilelist[i].m_Hue); } } } } else //with duplicate remove { StaticTile[] tilelist = new StaticTile[count]; int j = 0; for (int i = 0; i < count; ++i) { StaticTile tile = new StaticTile(); tile.m_ID = m_StaticsReader.ReadUInt16(); tile.m_X = m_StaticsReader.ReadByte(); tile.m_Y = m_StaticsReader.ReadByte(); tile.m_Z = m_StaticsReader.ReadSByte(); tile.m_Hue = m_StaticsReader.ReadInt16(); if ((tile.m_ID >= 0) && (tile.m_ID <= Art.GetMaxItemID())) { if (tile.m_Hue < 0) tile.m_Hue = 0; bool first = true; for (int k = 0; k < j; ++k) { if ((tilelist[k].m_ID == tile.m_ID) && ((tilelist[k].m_X == tile.m_X) && (tilelist[k].m_Y == tile.m_Y)) && (tilelist[k].m_Z == tile.m_Z) && (tilelist[k].m_Hue == tile.m_Hue)) { first = false; break; } } if (first) { tilelist[j] = tile; j++; } } } if (map.Tiles.PendingStatic(x, y)) { StaticTile[] pending = map.Tiles.GetPendingStatics(x, y); StaticTile[] old = tilelist; tilelist = new StaticTile[old.Length + pending.Length]; old.CopyTo(tilelist, 0); for (int i = 0; i < pending.Length; ++i) { if ((pending[i].m_ID >= 0) && (pending[i].m_ID <= Art.GetMaxItemID())) { if (pending[i].m_Hue < 0) pending[i].m_Hue = 0; bool first = true; for (int k = 0; k < j; ++k) { if ((tilelist[k].m_ID == pending[i].m_ID) && ((tilelist[k].m_X == pending[i].m_X) && (tilelist[k].m_Y == pending[i].m_Y)) && (tilelist[k].m_Z == pending[i].m_Z) && (tilelist[k].m_Hue == pending[i].m_Hue)) { first = false; break; } } if (first) tilelist[j++] = pending[i]; } } } if (j > 0) { binidx.Write((int)binmul.BaseStream.Position); //lookup for (int i = 0; i < j; ++i) { binmul.Write(tilelist[i].m_ID); binmul.Write(tilelist[i].m_X); binmul.Write(tilelist[i].m_Y); binmul.Write(tilelist[i].m_Z); binmul.Write(tilelist[i].m_Hue); } } } fsmullength = (int)binmul.BaseStream.Position - fsmullength; if (fsmullength > 0) { binidx.Write(fsmullength); //length if (extra == -1) extra = 0; binidx.Write(extra); //extra } else { binidx.Write((int)-1); //lookup binidx.Write((int)-1); //length binidx.Write((int)-1); //extra } } } catch // fill the rest { binidx.BaseStream.Seek(((x * blocky) + y) * 12, SeekOrigin.Begin); for (; x < blockx; ++x) { for (; y < blocky; ++y) { binidx.Write((int)-1); //lookup binidx.Write((int)-1); //length binidx.Write((int)-1); //extra } y = 0; } } } } memidx.WriteTo(fsidx); memmul.WriteTo(fsmul); } } m_IndexReader.Close(); m_StaticsReader.Close(); }
public void AddPendingStatic(int blockx, int blocky, StaticTile toadd) { if (m_StaticTiles_ToAdd == null) { m_StaticTiles_ToAdd = new List<StaticTile>[BlockHeight][]; } if (m_StaticTiles_ToAdd[blocky] == null) { m_StaticTiles_ToAdd[blocky] = new List<StaticTile>[BlockWidth]; } if (m_StaticTiles_ToAdd[blocky][blockx] == null) { m_StaticTiles_ToAdd[blocky][blockx] = new List<StaticTile>(); } m_StaticTiles_ToAdd[blocky][blockx].Add(toadd); }
private void StaticImport(string Filename) { StreamReader ip = new StreamReader(Filename); string line; StaticTile newtile = new StaticTile(); newtile.m_ID = 0xFFFF; newtile.m_Hue = 0; int x, y, blockx, blocky; blockx = blocky = 0; while ((line = ip.ReadLine()) != null) { if ((line = line.Trim()).Length == 0 || line.StartsWith("#") || line.StartsWith("//")) continue; try { if (line.StartsWith("SECTION WORLDITEM")) { if (newtile.m_ID != 0xFFFF) { currmap.Tiles.AddPendingStatic(blockx, blocky, newtile); blockx = blocky = 0; } newtile = new StaticTile(); newtile.m_ID = 0xFFFF; newtile.m_Hue = 0; } else if (line.StartsWith("ID")) { line = line.Remove(0, 2); line = line.TrimStart(' '); line = line.TrimEnd(' '); newtile.m_ID = Art.GetLegalItemID(Convert.ToUInt16(line)); } else if (line.StartsWith("X")) { line = line.Remove(0, 1); line = line.TrimStart(' '); line = line.TrimEnd(' '); x = Convert.ToInt32(line); blockx = x >> 3; x &= 0x7; newtile.m_X = (byte)x; } else if (line.StartsWith("Y")) { line = line.Remove(0, 1); line = line.TrimStart(' '); line = line.TrimEnd(' '); y = Convert.ToInt32(line); blocky = y >> 3; y &= 0x7; newtile.m_Y = (byte)y; } else if (line.StartsWith("Z")) { line = line.Remove(0, 1); line = line.TrimStart(' '); line = line.TrimEnd(' '); newtile.m_Z = Convert.ToSByte(line); } else if (line.StartsWith("COLOR")) { line = line.Remove(0, 5); line = line.TrimStart(' '); line = line.TrimEnd(' '); newtile.m_Hue = Convert.ToInt16(line); } } catch { } } if (newtile.m_ID != 0xFFFF) currmap.Tiles.AddPendingStatic(blockx, blocky, newtile); ip.Close(); MessageBox.Show("Done", "Freeze Static", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); currmap.ResetCache(); pictureBox.Invalidate(); }
private unsafe int PatchStatics(TileMatrix matrix, string dataPath, string indexPath, string lookupPath) { using (FileStream fsData = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (FileStream fsIndex = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (FileStream fsLookup = new FileStream(lookupPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { BinaryReader indexReader = new BinaryReader(fsIndex); BinaryReader lookupReader = new BinaryReader(fsLookup); int count = (int)(indexReader.BaseStream.Length / 4); HuedTileList[][] lists = new HuedTileList[8][]; for (int x = 0; x < 8; ++x) { lists[x] = new HuedTileList[8]; for (int y = 0; y < 8; ++y) { lists[x][y] = new HuedTileList(); } } for (int i = 0; i < count; ++i) { int blockID = indexReader.ReadInt32(); int blockX = blockID / matrix.BlockHeight; int blockY = blockID % matrix.BlockHeight; int offset = lookupReader.ReadInt32(); int length = lookupReader.ReadInt32(); lookupReader.ReadInt32(); // Extra if (offset < 0 || length <= 0) { matrix.SetStaticBlock(blockX, blockY, matrix.EmptyStaticBlock); continue; } fsData.Seek(offset, SeekOrigin.Begin); int tileCount = length / 7; StaticTile[] staTiles = new StaticTile[tileCount]; fixed(StaticTile *pTiles = staTiles) { NativeMethods._lread(fsData.SafeFileHandle, pTiles, length); StaticTile *pCur = pTiles, pEnd = pTiles + tileCount; while (pCur < pEnd) { lists[pCur->m_X & 0x7][pCur->m_Y & 0x7].Add((short)((pCur->m_ID & 0x3FFF) + 0x4000), pCur->m_Hue, pCur->m_Z); ++pCur; } HuedTile[][][] tiles = new HuedTile[8][][]; for (int x = 0; x < 8; ++x) { tiles[x] = new HuedTile[8][]; for (int y = 0; y < 8; ++y) { tiles[x][y] = lists[x][y].ToArray(); } } matrix.SetStaticBlock(blockX, blockY, tiles); } } return(count); } } } }
private unsafe HuedTile[][][] ReadStaticBlock(int x, int y) { try { if (!StaticIndexInit) { InitStatics(); } if (m_Statics == null || !m_Statics.CanRead || !m_Statics.CanSeek) { if (staticsPath == null) { m_Statics = null; } else { m_Statics = new FileStream(staticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); } } if (m_Statics == null) { return(EmptyStaticBlock); } int lookup = m_StaticIndex[(x * BlockHeight) + y].lookup; int length = m_StaticIndex[(x * BlockHeight) + y].length; if (lookup < 0 || length <= 0) { return(EmptyStaticBlock); } else { int count = length / (Art.IsPreAlpha ? 11 : 7); m_Statics.Seek(lookup, SeekOrigin.Begin); if (m_Buffer == null || m_Buffer.Length < length) { m_Buffer = new byte[length]; } GCHandle gc = GCHandle.Alloc(m_Buffer, GCHandleType.Pinned); try { m_Statics.Read(m_Buffer, 0, length); if (m_Lists == null) { m_Lists = new HuedTileList[8][]; for (int i = 0; i < 8; ++i) { m_Lists[i] = new HuedTileList[8]; for (int j = 0; j < 8; ++j) { m_Lists[i][j] = new HuedTileList(); } } } HuedTileList[][] lists = m_Lists; if (Art.IsPreAlpha) { for (int i = 0; i < count; ++i) { IntPtr ptr = new IntPtr((long)gc.AddrOfPinnedObject() + i * sizeof(OldStaticTile)); StaticTile cur = new StaticTile((OldStaticTile)Marshal.PtrToStructure(ptr, typeof(OldStaticTile))); lists[cur.m_X & 0x7][cur.m_Y & 0x7].Add((ushort)(Math.Min(cur.m_ID, Art.StaticLength)), cur.m_Hue, cur.m_Z); } } else { for (int i = 0; i < count; ++i) { IntPtr ptr = new IntPtr((long)gc.AddrOfPinnedObject() + i * sizeof(StaticTile)); StaticTile cur = (StaticTile)Marshal.PtrToStructure(ptr, typeof(StaticTile)); lists[cur.m_X & 0x7][cur.m_Y & 0x7].Add((ushort)(Math.Min(cur.m_ID, Art.StaticLength)), cur.m_Hue, cur.m_Z); } } HuedTile[][][] tiles = new HuedTile[8][][]; for (int i = 0; i < 8; ++i) { tiles[i] = new HuedTile[8][]; for (int j = 0; j < 8; ++j) { tiles[i][j] = lists[i][j].ToArray(); } } return(tiles); } finally { gc.Free(); } } } finally { //if (m_Statics != null) // m_Statics.Close(); } }
private unsafe HuedTile[][][] ReadStaticBlock(int x, int y) { m_IndexReader.BaseStream.Seek(((x * m_BlockHeight) + y) * 12, SeekOrigin.Begin); int lookup = m_IndexReader.ReadInt32(); int length = m_IndexReader.ReadInt32(); if (lookup < 0 || length <= 0) { return(m_EmptyStaticBlock); } else { int count = length / 7; m_Statics.Seek(lookup, SeekOrigin.Begin); StaticTile[] staTiles = new StaticTile[count]; fixed(StaticTile *pTiles = staTiles) { _lread(m_Statics.Handle, pTiles, length); if (m_Lists == null) { m_Lists = new HuedTileList[8][]; for (int i = 0; i < 8; ++i) { m_Lists[i] = new HuedTileList[8]; for (int j = 0; j < 8; ++j) { m_Lists[i][j] = new HuedTileList(); } } } HuedTileList[][] lists = m_Lists; StaticTile *pCur = pTiles, pEnd = pTiles + count; while (pCur < pEnd) { lists[pCur->m_X & 0x7][pCur->m_Y & 0x7].Add((short)((pCur->m_ID & 0x3FFF) + 0x4000), pCur->m_Hue, pCur->m_Z); ++pCur; } HuedTile[][][] tiles = new HuedTile[8][][]; for (int i = 0; i < 8; ++i) { tiles[i] = new HuedTile[8][]; for (int j = 0; j < 8; ++j) { tiles[i][j] = lists[i][j].ToArray(); } } return(tiles); } } }
public static void DefragStatics(string path, Map map, int width, int height, bool remove) { string indexPath = Files.GetFilePath("staidx{0}.mul", map.FileIndex); BinaryReader indexReader; if (indexPath != null) { FileStream index = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read); indexReader = new BinaryReader(index); } else { return; } string staticsPath = Files.GetFilePath("statics{0}.mul", map.FileIndex); FileStream staticsStream; BinaryReader staticsReader; if (staticsPath != null) { staticsStream = new FileStream(staticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); staticsReader = new BinaryReader(staticsStream); } else { return; } int blockx = width >> 3; int blocky = height >> 3; string idx = Path.Combine(path, $"staidx{map.FileIndex}.mul"); string mul = Path.Combine(path, $"statics{map.FileIndex}.mul"); using (var fsidx = new FileStream(idx, FileMode.Create, FileAccess.Write, FileShare.Write)) using (var fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { var memidx = new MemoryStream(); var memmul = new MemoryStream(); using (var binidx = new BinaryWriter(memidx)) using (var binmul = new BinaryWriter(memmul)) { for (int x = 0; x < blockx; ++x) { for (int y = 0; y < blocky; ++y) { try { indexReader.BaseStream.Seek(((x * blocky) + y) * 12, SeekOrigin.Begin); int lookup = indexReader.ReadInt32(); int length = indexReader.ReadInt32(); int extra = indexReader.ReadInt32(); if (((lookup < 0 || length <= 0) && (!map.Tiles.PendingStatic(x, y))) || (map.Tiles.IsStaticBlockRemoved(x, y))) { binidx.Write(-1); // lookup binidx.Write(-1); // length binidx.Write(-1); // extra } else { if ((lookup >= 0) && (length > 0)) { staticsStream.Seek(lookup, SeekOrigin.Begin); } var fsmullength = (int)binmul.BaseStream.Position; int count = length / 7; if (!remove) //without duplicate remove { bool firstitem = true; for (int i = 0; i < count; ++i) { ushort graphic = staticsReader.ReadUInt16(); byte sx = staticsReader.ReadByte(); byte sy = staticsReader.ReadByte(); sbyte sz = staticsReader.ReadSByte(); short shue = staticsReader.ReadInt16(); if (graphic > Art.GetMaxItemID()) { continue; } if (shue < 0) { shue = 0; } if (firstitem) { binidx.Write((int)binmul.BaseStream.Position); //lookup firstitem = false; } binmul.Write(graphic); binmul.Write(sx); binmul.Write(sy); binmul.Write(sz); binmul.Write(shue); } StaticTile[] tileList = map.Tiles.GetPendingStatics(x, y); if (tileList != null) { for (int i = 0; i < tileList.Length; ++i) { if (tileList[i].m_ID > Art.GetMaxItemID()) { continue; } if (tileList[i].m_Hue < 0) { tileList[i].m_Hue = 0; } if (firstitem) { binidx.Write((int)binmul.BaseStream.Position); // lookup firstitem = false; } binmul.Write(tileList[i].m_ID); binmul.Write(tileList[i].m_X); binmul.Write(tileList[i].m_Y); binmul.Write(tileList[i].m_Z); binmul.Write(tileList[i].m_Hue); } } } else // with duplicate remove { var tileList = new StaticTile[count]; int j = 0; for (int i = 0; i < count; ++i) { var tile = new StaticTile { m_ID = staticsReader.ReadUInt16(), m_X = staticsReader.ReadByte(), m_Y = staticsReader.ReadByte(), m_Z = staticsReader.ReadSByte(), m_Hue = staticsReader.ReadInt16() }; if (tile.m_ID > Art.GetMaxItemID()) { continue; } if (tile.m_Hue < 0) { tile.m_Hue = 0; } bool first = true; for (int k = 0; k < j; ++k) { if ((tileList[k].m_ID == tile.m_ID) && ((tileList[k].m_X == tile.m_X) && (tileList[k].m_Y == tile.m_Y)) && (tileList[k].m_Z == tile.m_Z) && (tileList[k].m_Hue == tile.m_Hue)) { first = false; break; } } if (!first) { continue; } tileList[j] = tile; j++; } if (map.Tiles.PendingStatic(x, y)) { StaticTile[] pending = map.Tiles.GetPendingStatics(x, y); StaticTile[] old = tileList; tileList = new StaticTile[old.Length + pending.Length]; old.CopyTo(tileList, 0); for (int i = 0; i < pending.Length; ++i) { if (pending[i].m_ID > Art.GetMaxItemID()) { continue; } if (pending[i].m_Hue < 0) { pending[i].m_Hue = 0; } bool first = true; for (int k = 0; k < j; ++k) { if ((tileList[k].m_ID == pending[i].m_ID) && ((tileList[k].m_X == pending[i].m_X) && (tileList[k].m_Y == pending[i].m_Y)) && (tileList[k].m_Z == pending[i].m_Z) && (tileList[k].m_Hue == pending[i].m_Hue)) { first = false; break; } } if (first) { tileList[j++] = pending[i]; } } } if (j > 0) { binidx.Write((int)binmul.BaseStream.Position); //lookup for (int i = 0; i < j; ++i) { binmul.Write(tileList[i].m_ID); binmul.Write(tileList[i].m_X); binmul.Write(tileList[i].m_Y); binmul.Write(tileList[i].m_Z); binmul.Write(tileList[i].m_Hue); } } } fsmullength = (int)binmul.BaseStream.Position - fsmullength; if (fsmullength > 0) { binidx.Write(fsmullength); // length if (extra == -1) { extra = 0; } binidx.Write(extra); // extra } else { binidx.Write(-1); // lookup binidx.Write(-1); // length binidx.Write(-1); // extra } } } catch // fill the rest { binidx.BaseStream.Seek(((x * blocky) + y) * 12, SeekOrigin.Begin); for (; x < blockx; ++x) { for (; y < blocky; ++y) { binidx.Write(-1); //lookup binidx.Write(-1); //length binidx.Write(-1); //extra } y = 0; } } } } memidx.WriteTo(fsidx); memmul.WriteTo(fsmul); } } indexReader.Close(); staticsReader.Close(); }
private int PatchStatics(TileMatrix matrix, string dataPath, string indexPath, string lookupPath) { using ( FileStream fsData = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read), fsIndex = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read), fsLookup = new FileStream(lookupPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BinaryReader indexReader = new BinaryReader(fsIndex), lookupReader = new BinaryReader(fsLookup)) { int count = Math.Min((int)(indexReader.BaseStream.Length / 4), (int)(lookupReader.BaseStream.Length / 12)); var lists = new HuedTileList[8][]; for (int x = 0; x < 8; ++x) { lists[x] = new HuedTileList[8]; for (int y = 0; y < 8; ++y) { lists[x][y] = new HuedTileList(); } } for (int i = 0; i < count; ++i) { int blockID = indexReader.ReadInt32(); int blockX = blockID / matrix.BlockHeight; int blockY = blockID % matrix.BlockHeight; int offset = lookupReader.ReadInt32(); int length = lookupReader.ReadInt32(); lookupReader.ReadInt32(); // Extra if (offset < 0 || length <= 0) { if (StaticBlocks[blockX] == null) { StaticBlocks[blockX] = new HuedTile[matrix.BlockHeight][][][]; } StaticBlocks[blockX][blockY] = TileMatrix.EmptyStaticBlock; continue; } fsData.Seek(offset, SeekOrigin.Begin); int tileCount = length / 7; if (m_TileBuffer.Length < tileCount) { m_TileBuffer = new StaticTile[tileCount]; } StaticTile[] staTiles = m_TileBuffer; GCHandle gc = GCHandle.Alloc(staTiles, GCHandleType.Pinned); try { if (m_Buffer == null || m_Buffer.Length < length) { m_Buffer = new byte[length]; } fsData.Read(m_Buffer, 0, length); Marshal.Copy(m_Buffer, 0, gc.AddrOfPinnedObject(), length); for (int j = 0; j < tileCount; ++j) { StaticTile cur = staTiles[j]; lists[cur.m_X & 0x7][cur.m_Y & 0x7].Add(Art.GetLegalItemID(cur.m_ID), cur.m_Hue, cur.m_Z); } var tiles = new HuedTile[8][][]; for (int x = 0; x < 8; ++x) { tiles[x] = new HuedTile[8][]; for (int y = 0; y < 8; ++y) { tiles[x][y] = lists[x][y].ToArray(); } } if (StaticBlocks[blockX] == null) { StaticBlocks[blockX] = new HuedTile[matrix.BlockHeight][][][]; } StaticBlocks[blockX][blockY] = tiles; } finally { gc.Free(); } } return(count); } } }
private unsafe int PatchStatics(TileMatrix matrix, string dataPath, string indexPath, string lookupPath) { using (FileStream fileStream1 = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (FileStream fileStream2 = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (FileStream fileStream3 = new FileStream(lookupPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { BinaryReader binaryReader1 = new BinaryReader((Stream) fileStream2); BinaryReader binaryReader2 = new BinaryReader((Stream) fileStream3); int num1 = (int) (binaryReader1.BaseStream.Length / 4L); HuedTileList[][] huedTileListArray = new HuedTileList[8][]; for (int index1 = 0; index1 < 8; ++index1) { huedTileListArray[index1] = new HuedTileList[8]; for (int index2 = 0; index2 < 8; ++index2) huedTileListArray[index1][index2] = new HuedTileList(); } for (int index1 = 0; index1 < num1; ++index1) { int num2 = binaryReader1.ReadInt32(); int x = num2 / matrix.BlockHeight; int y = num2 % matrix.BlockHeight; int num3 = binaryReader2.ReadInt32(); int wBytes = binaryReader2.ReadInt32(); binaryReader2.ReadInt32(); if (num3 < 0 || wBytes <= 0) { matrix.SetStaticBlock(x, y, matrix.EmptyStaticBlock); } else { fileStream1.Seek((long) num3, SeekOrigin.Begin); int length = wBytes / 7; StaticTile[] staticTileArray = new StaticTile[length]; fixed (StaticTile* staticTilePtr1 = &staticTileArray[0]) { TileMatrixPatch._lread(fileStream1.Handle, (void*) staticTilePtr1, wBytes); StaticTile* staticTilePtr2 = staticTilePtr1; for (StaticTile* staticTilePtr3 = staticTilePtr1 + length; staticTilePtr2 < staticTilePtr3; ++staticTilePtr2) huedTileListArray[(int) staticTilePtr2->m_X & 7][(int) staticTilePtr2->m_Y & 7].Add((short) (((int) staticTilePtr2->m_ID & 16383) + 16384), staticTilePtr2->m_Hue, staticTilePtr2->m_Z); HuedTile[][][] huedTileArray = new HuedTile[8][][]; for (int index2 = 0; index2 < 8; ++index2) { huedTileArray[index2] = new HuedTile[8][]; for (int index3 = 0; index3 < 8; ++index3) huedTileArray[index2][index3] = huedTileListArray[index2][index3].ToArray(); } matrix.SetStaticBlock(x, y, huedTileArray); } } } return num1; } } } }
private unsafe HuedTile[][][] ReadStaticBlock(int x, int y) { try { if (!StaticIndexInit) InitStatics(); if (m_Statics == null || !m_Statics.CanRead || !m_Statics.CanSeek) { if (staticsPath == null) m_Statics = null; else m_Statics = new FileStream(staticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); } if (m_Statics == null) return EmptyStaticBlock; int lookup = m_StaticIndex[(x * BlockHeight) + y].lookup; int length = m_StaticIndex[(x * BlockHeight) + y].length; if (lookup < 0 || length <= 0) return EmptyStaticBlock; else { int count = length / (Art.IsPreAlpha ? 11 : 7); m_Statics.Seek(lookup, SeekOrigin.Begin); if (m_Buffer == null || m_Buffer.Length < length) m_Buffer = new byte[length]; GCHandle gc = GCHandle.Alloc(m_Buffer, GCHandleType.Pinned); try { m_Statics.Read(m_Buffer, 0, length); if (m_Lists == null) { m_Lists = new HuedTileList[8][]; for (int i = 0; i < 8; ++i) { m_Lists[i] = new HuedTileList[8]; for (int j = 0; j < 8; ++j) m_Lists[i][j] = new HuedTileList(); } } HuedTileList[][] lists = m_Lists; if (Art.IsPreAlpha) { for (int i = 0; i < count; ++i) { IntPtr ptr = new IntPtr((long)gc.AddrOfPinnedObject() + i * sizeof(OldStaticTile)); StaticTile cur = new StaticTile((OldStaticTile)Marshal.PtrToStructure(ptr, typeof(OldStaticTile))); lists[cur.m_X & 0x7][cur.m_Y & 0x7].Add((ushort)(Math.Min(cur.m_ID, Art.StaticLength)), cur.m_Hue, cur.m_Z); } } else { for (int i = 0; i < count; ++i) { IntPtr ptr = new IntPtr((long)gc.AddrOfPinnedObject() + i * sizeof(StaticTile)); StaticTile cur = (StaticTile)Marshal.PtrToStructure(ptr, typeof(StaticTile)); lists[cur.m_X & 0x7][cur.m_Y & 0x7].Add((ushort)(Math.Min(cur.m_ID, Art.StaticLength)), cur.m_Hue, cur.m_Z); } } HuedTile[][][] tiles = new HuedTile[8][][]; for (int i = 0; i < 8; ++i) { tiles[i] = new HuedTile[8][]; for (int j = 0; j < 8; ++j) tiles[i][j] = lists[i][j].ToArray(); } return tiles; } finally { gc.Free(); } } } finally { //if (m_Statics != null) // m_Statics.Close(); } }
private void OnClickCopy(object sender, EventArgs e) { string path = textBox1.Text; if (!Directory.Exists(path)) { MessageBox.Show(this, String.Format("Указан не существующие каталог: '{0}'", path), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int x1 = (int)numericUpDownX1.Value; int x2 = (int)numericUpDownX2.Value; int y1 = (int)numericUpDownY1.Value; int y2 = (int)numericUpDownY2.Value; int tox = (int)numericUpDownToX1.Value; int toy = (int)numericUpDownToY1.Value; int w = (int)numericUpDownWidth.Value; int h = (int)numericUpDownHeight.Value; int id = (int)numericUpDownId.Value; if ((x1 >= x2) || (y1 >= y2)) { MessageBox.Show(this, "Не правильно заданы координаты области.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if ((x1 < 0) || (x1 >= w) || (x2 < 0) || (x2 >= w) || (y1 < 0) || (y1 >= h) || (y2 < 0) || (y2 >= h) ) { MessageBox.Show(this, "Координаты области копируемой карты выходят за границу карты.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if ((tox < 0) || (tox >= workingmap.Width) || ((tox + (x2 - x1)) >= workingmap.Width) || (toy < 0) || (toy >= workingmap.Height) || ((toy + (y2 - y1)) >= workingmap.Height)) { MessageBox.Show(this, "Координаты области теущей карты выходят за границу карты.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (id < 0) return; x1 >>= 3; x2 >>= 3; y1 >>= 3; y2 >>= 3; tox >>= 3; toy >>= 3; int tox2 = (x2 - x1) + tox; int toy2 = (y2 - y1) + toy; int blocky = workingmap.Height >> 3; int blockx = workingmap.Width >> 3; int blocky_copy = h >> 3; int blockx_copy = w >> 3; progressBar1.Step = 1; progressBar1.Value = 0; progressBar1.Maximum=0; if (checkBoxMap.Checked) progressBar1.Maximum += blocky * blockx; if (checkBoxStatics.Checked) progressBar1.Maximum += blocky * blockx; if (checkBoxMap.Checked) { string copymap = Path.Combine(path, String.Format("map{0}.mul", id)); if (!File.Exists(copymap)) { MessageBox.Show(this, String.Format("Файл: '{0}' не существует.", copymap), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } FileStream m_map_copy = new FileStream(copymap, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader m_mapReader_copy = new BinaryReader(m_map_copy); string mapPath = Ultima.Files.GetFilePath(String.Format("map{0}.mul", workingmap.FileIndex)); FileStream m_map; BinaryReader m_mapReader; if (mapPath != null) { m_map = new FileStream(mapPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_mapReader = new BinaryReader(m_map); } else return; string mul = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, String.Format("map{0}.mul", workingmap.FileIndex)); using (FileStream fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { using (BinaryWriter binmul = new BinaryWriter(fsmul)) { for (int x = 0; x < blockx; ++x) { for (int y = 0; y < blocky; ++y) { if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { m_mapReader_copy.BaseStream.Seek((((x-tox+x1) * blocky_copy) + (y-toy+y1)) * 196, SeekOrigin.Begin); int header = m_mapReader_copy.ReadInt32(); binmul.Write(header); } else { m_mapReader.BaseStream.Seek(((x * blocky) + y) * 196, SeekOrigin.Begin); int header = m_mapReader.ReadInt32(); binmul.Write(header); } for (int i = 0; i < 64; ++i) { ushort tileid; sbyte z; if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { tileid = m_mapReader_copy.ReadUInt16(); z = m_mapReader_copy.ReadSByte(); } else { tileid = m_mapReader.ReadUInt16(); z = m_mapReader.ReadSByte(); } if ((tileid < 0) || (tileid >= Art.StaticLength)) tileid = 0; if (z < -128) z = -128; if (z > 127) z = 127; binmul.Write(tileid); binmul.Write(z); } progressBar1.PerformStep(); } } } } m_mapReader.Close(); m_mapReader_copy.Close(); } if (checkBoxStatics.Checked) { string indexPath = Files.GetFilePath(String.Format("staidx{0}.mul", workingmap.FileIndex)); FileStream m_Index; BinaryReader m_IndexReader; if (indexPath != null) { m_Index = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_IndexReader = new BinaryReader(m_Index); } else return; string staticsPath = Files.GetFilePath(String.Format("statics{0}.mul", workingmap.FileIndex)); FileStream m_Statics; BinaryReader m_StaticsReader; if (staticsPath != null) { m_Statics = new FileStream(staticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); m_StaticsReader = new BinaryReader(m_Statics); } else return; string copyindexPath = Path.Combine(path, String.Format("staidx{0}.mul", id)); if (!File.Exists(copyindexPath)) { MessageBox.Show(this, String.Format("Файл: '{0}' не существует.", copyindexPath), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } FileStream m_Index_copy = new FileStream(copyindexPath, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader m_IndexReader_copy = new BinaryReader(m_Index_copy); string copystaticsPath = Path.Combine(path, String.Format("statics{0}.mul", id)); if (!File.Exists(copystaticsPath)) { MessageBox.Show(this, String.Format("Файл: '{0}' не существует.", copystaticsPath), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } FileStream m_Statics_copy = new FileStream(copystaticsPath, FileMode.Open, FileAccess.Read, FileShare.Read); BinaryReader m_StaticsReader_copy = new BinaryReader(m_Statics_copy); string idx = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, String.Format("staidx{0}.mul", workingmap.FileIndex)); string mul = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, String.Format("statics{0}.mul", workingmap.FileIndex)); using (FileStream fsidx = new FileStream(idx, FileMode.Create, FileAccess.Write, FileShare.Write), fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { using (BinaryWriter binidx = new BinaryWriter(fsidx), binmul = new BinaryWriter(fsmul)) { for (int x = 0; x < blockx; ++x) { for (int y = 0; y < blocky; ++y) { int lookup, length, extra; if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { m_IndexReader_copy.BaseStream.Seek((((x - tox + x1) * blocky_copy) + (y - toy + y1)) * 12, SeekOrigin.Begin); lookup = m_IndexReader_copy.ReadInt32(); length = m_IndexReader_copy.ReadInt32(); extra = m_IndexReader_copy.ReadInt32(); } else { m_IndexReader.BaseStream.Seek(((x * blocky) + y) * 12, SeekOrigin.Begin); lookup = m_IndexReader.ReadInt32(); length = m_IndexReader.ReadInt32(); extra = m_IndexReader.ReadInt32(); } if (lookup < 0 || length <= 0) { binidx.Write((int)-1); // lookup binidx.Write((int)-1); // length binidx.Write((int)-1); // extra } else { if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) m_Statics_copy.Seek(lookup, SeekOrigin.Begin); else m_Statics.Seek(lookup, SeekOrigin.Begin); int fsmullength = (int)fsmul.Position; int count = length / 7; if (RemoveDupl.Checked) { StaticTile[] tilelist = new StaticTile[count]; int j = 0; for (int i = 0; i < count; ++i) { StaticTile tile = new StaticTile(); if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { tile.m_ID = m_StaticsReader_copy.ReadUInt16(); tile.m_X = m_StaticsReader_copy.ReadByte(); tile.m_Y = m_StaticsReader_copy.ReadByte(); tile.m_Z = m_StaticsReader_copy.ReadSByte(); tile.m_Hue = m_StaticsReader_copy.ReadInt16(); } else { tile.m_ID = m_StaticsReader.ReadUInt16(); tile.m_X = m_StaticsReader.ReadByte(); tile.m_Y = m_StaticsReader.ReadByte(); tile.m_Z = m_StaticsReader.ReadSByte(); tile.m_Hue = m_StaticsReader.ReadInt16(); } if ((tile.m_ID >= 0) && (tile.m_ID < Art.StaticLength)) { if (tile.m_Hue < 0) tile.m_Hue = 0; bool first = true; for (int k = 0; k < j; ++k) { if ((tilelist[k].m_ID == tile.m_ID) && ((tilelist[k].m_X == tile.m_X) && (tilelist[k].m_Y == tile.m_Y)) && (tilelist[k].m_Z == tile.m_Z) && (tilelist[k].m_Hue == tile.m_Hue)) { first = false; break; } } if (first) tilelist[j++] = tile; } } if (j > 0) { binidx.Write((int)fsmul.Position); //lookup for (int i = 0; i < j; ++i) { binmul.Write(tilelist[i].m_ID); binmul.Write(tilelist[i].m_X); binmul.Write(tilelist[i].m_Y); binmul.Write(tilelist[i].m_Z); binmul.Write(tilelist[i].m_Hue); } } } else { bool firstitem = true; for (int i = 0; i < count; ++i) { ushort graphic; short shue; byte sx, sy; sbyte sz; if ((tox <= x) && (x <= tox2) && (toy <= y) && (y <= toy2)) { graphic = m_StaticsReader_copy.ReadUInt16(); sx = m_StaticsReader_copy.ReadByte(); sy = m_StaticsReader_copy.ReadByte(); sz = m_StaticsReader_copy.ReadSByte(); shue = m_StaticsReader_copy.ReadInt16(); } else { graphic = m_StaticsReader.ReadUInt16(); sx = m_StaticsReader.ReadByte(); sy = m_StaticsReader.ReadByte(); sz = m_StaticsReader.ReadSByte(); shue = m_StaticsReader.ReadInt16(); } if ((graphic >= 0) && (graphic < Ultima.Art.StaticLength)) { if (shue < 0) shue = 0; if (firstitem) { binidx.Write((int)fsmul.Position); //lookup firstitem = false; } binmul.Write(graphic); binmul.Write(sx); binmul.Write(sy); binmul.Write(sz); binmul.Write(shue); } } } fsmullength = (int)fsmul.Position - fsmullength; if (fsmullength > 0) { binidx.Write(fsmullength); //length binidx.Write(extra); //extra } else { binidx.Write((int)-1); //lookup binidx.Write((int)-1); //length binidx.Write((int)-1); //extra } } progressBar1.PerformStep(); } } } } m_IndexReader.Close(); m_StaticsReader.Close(); m_Index_copy.Close(); m_StaticsReader_copy.Close(); } MessageBox.Show(this, String.Format("Files saved to {0}", AppDomain.CurrentDomain.SetupInformation.ApplicationBase), "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); }
private unsafe HuedTile[][][] ReadStaticBlock( int x, int y ) { m_IndexReader.BaseStream.Seek( ((x * m_BlockHeight) + y) * 12, SeekOrigin.Begin ); int lookup = m_IndexReader.ReadInt32(); int length = m_IndexReader.ReadInt32(); if ( lookup < 0 || length <= 0 ) { return m_EmptyStaticBlock; } else { int count = length / 7; m_Statics.Seek( lookup, SeekOrigin.Begin ); StaticTile[] staTiles = new StaticTile[count]; fixed ( StaticTile *pTiles = staTiles ) { _lread(m_Statics.Handle, pTiles, length); if ( m_Lists == null ) { m_Lists = new HuedTileList[8][]; for ( int i = 0; i < 8; ++i ) { m_Lists[i] = new HuedTileList[8]; for ( int j = 0; j < 8; ++j ) m_Lists[i][j] = new HuedTileList(); } } HuedTileList[][] lists = m_Lists; StaticTile *pCur = pTiles, pEnd = pTiles + count; while ( pCur < pEnd ) { lists[pCur->m_X & 0x7][pCur->m_Y & 0x7].Add( (short)((pCur->m_ID & 0x3FFF) + 0x4000), pCur->m_Hue, pCur->m_Z ); ++pCur; } HuedTile[][][] tiles = new HuedTile[8][][]; for ( int i = 0; i < 8; ++i ) { tiles[i] = new HuedTile[8][]; for ( int j = 0; j < 8; ++j ) tiles[i][j] = lists[i][j].ToArray(); } return tiles; } } }