List <DrctEntry> FindWADEntrys(type find) //Possible Types: sprite, flat //returns number of found entries { List <DrctEntry> entryList = new List <DrctEntry>(); DrctEntry markerStart = new DrctEntry(); DrctEntry markerEnd = new DrctEntry(); entryList.Clear(); //make sure the list of entries is empty if (find == type.Sprite) { //Look for sprites for (int i = 0; i < newWad.directory.Count; i++) { if (newWad.directory[i].name.Contains("S_START") && newWad.directory[i].size == 0) //The sprite marker { markerStart = newWad.directory[i]; markerStart.filepos = i; } if (newWad.directory[i].name.Contains("S_END") && newWad.directory[i].size == 0) { markerEnd = newWad.directory[i]; markerEnd.filepos = i; } } } if (find == type.Flat) { //Look for flats for (int i = 0; i < newWad.directory.Count; i++) { if (newWad.directory[i].name.Contains("F_START") && newWad.directory[i].size == 0) //The flat marker { markerStart = newWad.directory[i]; markerStart.filepos = i; } if (newWad.directory[i].name.Contains("F_END") && newWad.directory[i].size == 0) { markerEnd = newWad.directory[i]; markerEnd.filepos = i; } } } for (int i = 0; i < newWad.directory.Count; i++) { if (i > markerStart.filepos && i < markerEnd.filepos && newWad.directory[i].size > 0) { //all of the entries between the markers entryList.Add(newWad.directory[i]); } } return(entryList); }
List <DrctEntry> ReadWADPnames() { DrctEntry pnames = new DrctEntry(); int patchCount = 0; List <string> newPatches = new List <string>(); foreach (DrctEntry entry in newWad.directory)//Find PNAMES { if (entry.name.Contains("PNAMES")) { pnames = entry; break; } } byte[] pbytes = new byte[pnames.size]; wadOpener.Position = pnames.filepos; wadOpener.Read(pbytes, 0, pbytes.Length); patchCount = (int)BitConverter.ToUInt32(pbytes, 0); //number of patches for (int j = 4; j < pnames.size; j += 8) { string str1 = new String(System.Text.Encoding.ASCII.GetChars(pbytes, j, 8)); str1 = str1.Replace("\0", ""); if (!newPatches.Contains(str1)) { newPatches.Add(str1); //fill the list with all patch names UsedImages.Add(str1); } } newWad.pnames = newPatches; //save info in wad class List <DrctEntry> patchEntries = new List <DrctEntry>(); foreach (string str in newPatches)//Find PNAMES { foreach (DrctEntry entry in newWad.directory) { if (entry.name.ToUpper() == str.ToUpper()) { patchEntries.Add(entry); break; } } } return(patchEntries); }
void ReadWADDirectory() { byte[] wadDir = new byte[newWad.numlumps * 16]; //Debug.Log(wadDir.Length); wadOpener.Position = newWad.infotableofs; wadOpener.Read(wadDir, 0, wadDir.Length); for (int i = 0; i < wadDir.Length; i += 16) { DrctEntry newDirEntry = new DrctEntry(); newDirEntry.filepos = BitConverter.ToInt32(wadDir, i); newDirEntry.size = BitConverter.ToInt32(wadDir, i + 4); newDirEntry.name = new String(System.Text.Encoding.ASCII.GetChars(wadDir, i + 8, 8)); newDirEntry.name = newDirEntry.name.Replace("\0", ""); newWad.directory.Add(newDirEntry); } }
void ReadMAPEntries() { for (int i = 0; i < newWad.directory.Count - 1; i++) { if (newWad.directory[i + 1].name.Contains("THINGS") && newWad.directory[i].size == 0) { DoomMap newMap = new DoomMap(); DrctEntry dirEntry = newWad.directory[i]; newMap.name = dirEntry.name; //THINGS lump if (newWad.directory[i + 1].name.Contains("THINGS")) { byte[] thingBytes = new byte[newWad.directory[i + 1].size]; wadOpener.Position = newWad.directory[i + 1].filepos; wadOpener.Read(thingBytes, 0, thingBytes.Length); for (int j = 0; j < newWad.directory[i + 1].size; j += 10) { THINGS newThing = new THINGS(); newThing.xPos = BitConverter.ToInt16(thingBytes, j + 0); newThing.yPos = BitConverter.ToInt16(thingBytes, j + 2); newThing.angle = BitConverter.ToInt16(thingBytes, j + 4); newThing.thingType = BitConverter.ToInt16(thingBytes, j + 6); newThing.thingOptions = BitConverter.ToInt16(thingBytes, j + 8); newMap.things.Add(newThing); } } //LINEDEEFS lump if (newWad.directory[i + 2].name.Contains("LINEDEFS")) { byte[] lineDefBytes = new byte[newWad.directory[i + 2].size]; wadOpener.Position = newWad.directory[i + 2].filepos; wadOpener.Read(lineDefBytes, 0, lineDefBytes.Length); for (int j = 0; j < newWad.directory[i + 2].size; j += 14) { LINEDEFS newLineDef = new LINEDEFS(); newLineDef.firstVertIndex = BitConverter.ToInt16(lineDefBytes, j + 0); newLineDef.lastVertIndex = BitConverter.ToInt16(lineDefBytes, j + 2); newLineDef.flags = BitConverter.ToInt16(lineDefBytes, j + 4); newLineDef.types = BitConverter.ToInt16(lineDefBytes, j + 6); newLineDef.tag = BitConverter.ToInt16(lineDefBytes, j + 8); newLineDef.side1Index = BitConverter.ToInt16(lineDefBytes, j + 10); newLineDef.side2Index = BitConverter.ToInt16(lineDefBytes, j + 12); newMap.linedefs.Add(newLineDef); } } //SIDEDEFS lump if (newWad.directory[i + 3].name.Contains("SIDEDEFS")) { byte[] sideDefBytes = new byte[newWad.directory[i + 3].size]; wadOpener.Position = newWad.directory[i + 3].filepos; wadOpener.Read(sideDefBytes, 0, sideDefBytes.Length); for (int j = 0; j < newWad.directory[i + 3].size; j += 30) { SIDEDEFS newSideDef = new SIDEDEFS(); newSideDef.xOfs = BitConverter.ToInt16(sideDefBytes, j + 0); newSideDef.yOfs = BitConverter.ToInt16(sideDefBytes, j + 2); newSideDef.upTex = new String(System.Text.Encoding.ASCII.GetChars(sideDefBytes, j + 4, 8)).ToUpper(); newSideDef.lowTex = new String(System.Text.Encoding.ASCII.GetChars(sideDefBytes, j + 12, 8)).ToUpper(); newSideDef.midTex = new String(System.Text.Encoding.ASCII.GetChars(sideDefBytes, j + 20, 8)).ToUpper(); newSideDef.secNum = BitConverter.ToInt16(sideDefBytes, j + 28); newSideDef.upTex = newSideDef.upTex.Replace("\0", ""); newSideDef.lowTex = newSideDef.lowTex.Replace("\0", ""); newSideDef.midTex = newSideDef.midTex.Replace("\0", ""); newMap.sidedefs.Add(newSideDef); } } //VERTEXES lump if (newWad.directory[i + 4].name.Contains("VERTEXES")) { byte[] vertexBytes = new byte[newWad.directory[i + 4].size]; wadOpener.Position = newWad.directory[i + 4].filepos; wadOpener.Read(vertexBytes, 0, vertexBytes.Length); for (int j = 0; j < newWad.directory[i + 4].size; j += 4) { Vector3 newVertex = new Vector3(0, 0, 0); newVertex.x = BitConverter.ToInt16(vertexBytes, j + 0); newVertex.z = BitConverter.ToInt16(vertexBytes, j + 2); newMap.vertexes.Add(newVertex); } } //SECTORS lump if (newWad.directory[i + 8].name.Contains("SECTORS")) { byte[] secBytes = new byte[newWad.directory[i + 8].size]; wadOpener.Position = newWad.directory[i + 8].filepos; wadOpener.Read(secBytes, 0, secBytes.Length); for (int j = 0; j < newWad.directory[i + 8].size; j += 26) { SECTORS newSec = new SECTORS(); newSec.floorHeight = BitConverter.ToInt16(secBytes, j + 0); newSec.ceilingHeight = BitConverter.ToInt16(secBytes, j + 2); newSec.floorFlat = new String(System.Text.Encoding.ASCII.GetChars(secBytes, j + 4, 8)); newSec.ceilingFlat = new String(System.Text.Encoding.ASCII.GetChars(secBytes, j + 12, 8)); newSec.lightLevel = BitConverter.ToInt16(secBytes, j + 20); newSec.specialSec = BitConverter.ToInt16(secBytes, j + 22); newSec.sectorTag = BitConverter.ToInt16(secBytes, j + 24); newSec.floorFlat = newSec.floorFlat.Replace("\0", ""); newSec.ceilingFlat = newSec.ceilingFlat.Replace("\0", ""); newMap.sectors.Add(newSec); } } //BLOCKMAPS lump if (newWad.directory[i + 10].name.Contains("BLOCKMAP")) { byte[] blockBytes = new byte[newWad.directory[i + 10].size]; wadOpener.Position = newWad.directory[i + 10].filepos; wadOpener.Read(blockBytes, 0, blockBytes.Length); BLOCKMAP newBlockmap = new BLOCKMAP(); newBlockmap.xcoord = BitConverter.ToInt16(blockBytes, 0); newBlockmap.ycoord = BitConverter.ToInt16(blockBytes, 2); newBlockmap.numColumns = BitConverter.ToInt16(blockBytes, 4); newBlockmap.numRows = BitConverter.ToInt16(blockBytes, 6); int N = newBlockmap.numColumns * newBlockmap.numRows; //Offsets for (int j = 8; j < (8 + (2 * (N - 1))); j += 2) // (8 + (2 * (N - 1))) IS DEFINITELY RIGHT { newBlockmap.Offsets.Add(BitConverter.ToUInt16(blockBytes, j) * 2); //multiply this times 2 so its right } foreach (int offset in newBlockmap.Offsets) { int line = 0; int blockOffset = offset; // List<int> newBlock = new List<int>(); Block blk = new Block(); do { line = BitConverter.ToUInt16(blockBytes, blockOffset + 2); //newBlock.Add(line); if (line == 0xFFFF) { continue; } blk.lines.Add(newMap.linedefs[line]); blockOffset += 2; } while (line != 0xFFFF); newBlockmap.blocks.Add(blk); } newMap.blockmap = newBlockmap; } newWad.maps.Add(newMap); //Store the map in newWad } } }
//function adapted from SLADE bool isDoomGfx(DrctEntry sprite) { if (sprite.size < 10) { return(false); } PICTURES newPicture = new PICTURES(); byte[] spriteBytes = new byte[sprite.size]; //the entire picture resource in bytes wadOpener.Position = sprite.filepos; wadOpener.Read(spriteBytes, 0, spriteBytes.Length); //read the picture header (first 8 bytes of the picture resource) [0-7] newPicture.Width = BitConverter.ToInt16(spriteBytes, 0); newPicture.Height = BitConverter.ToInt16(spriteBytes, 2); newPicture.LeftOffset = BitConverter.ToInt16(spriteBytes, 4); newPicture.TopOffset = BitConverter.ToInt16(spriteBytes, 6); if (newPicture.Width < 1 || newPicture.Width > spriteBytes.Length) { return(false); } int[] pointers = new int[newPicture.Width]; //Reading pointer data (4 bytes each, as many as the width skipping the header) for (int i = 8; i < 8 + newPicture.Width * 4; i += 4) { if (BitConverter.ToInt32(spriteBytes, i) > spriteBytes.Length) { return(false); } pointers[(i - 8) / 4] = BitConverter.ToInt32(spriteBytes, i); } // Check size if (sprite.size > 4) { // Check header values are 'sane' if (newPicture.Width > 0 && newPicture.Height > 0) { // Check there is room for needed column pointers if (sprite.size < 8 + (newPicture.Width * 2)) { return(false); } // Check column pointers are within range foreach (int pointer in pointers) { if (pointer > spriteBytes.Length || pointer < 8) { return(false); } } // Passed all checks, so probably is doom gfx return(true); } } return(false); }
public MUS ReadMusEntry(int songNumber) { List <DrctEntry> musList = new List <DrctEntry>(); foreach (DrctEntry entry in newWad.directory) //find all the mus's in the wad { if (entry.name.StartsWith("D_")) { musList.Add(entry); } } DrctEntry mus = musList[songNumber]; //which song are we listening to today byte[] musBytes = new byte[mus.size]; wadOpener.Position = mus.filepos; wadOpener.Read(musBytes, 0, musBytes.Length); MUS newMUS = new MUS(); byte[] channelVolume = new byte[16]; newMUS.name = mus.name; //read the header of the mus file newMUS.id = new String(System.Text.Encoding.ASCII.GetChars(musBytes, 0, 3)); newMUS.scoreLen = BitConverter.ToUInt16(musBytes, 4); newMUS.scoreStart = BitConverter.ToUInt16(musBytes, 6); newMUS.channels = BitConverter.ToUInt16(musBytes, 8); newMUS.sec_channels = BitConverter.ToUInt16(musBytes, 10); newMUS.instrCnt = BitConverter.ToUInt16(musBytes, 12); newMUS.dummy = BitConverter.ToUInt16(musBytes, 14); List <int> instrList = new List <int>();//temporary list for storing instruments for (int j = 16; j < newMUS.scoreStart; j += 2) { instrList.Add(BitConverter.ToUInt16(musBytes, j)); //get the instrument } newMUS.instruments = instrList.ToArray(); //store the temporary list to the newMUS list int i = newMUS.scoreStart; while (i < musBytes.Length) { //read the 'score' Mus_Event newEvent = new Mus_Event(); newEvent.channelNum = (byte)(musBytes[i] & 0x0F); newEvent.musEventType = (byte)((musBytes[i] & 0x70) >> 4); newEvent.last = (((musBytes[i] & 0x80) >> 7) == 1); i++; //finished reading the descriptor byte if (newEvent.musEventType == 0) //Release Note { newEvent.note = (byte)(musBytes[i] & 0x7F); i++; //finished reading the released note byte } else if (newEvent.musEventType == 1) //Play Note { bool volBit = ((musBytes[i] & 0x80) >> 7) == 1; newEvent.note = (byte)(musBytes[i] & 0x7F); i++; //finished reading the note byte if (volBit) //if the last bit of the first byte is set, get the new vol from the 2nd byte { newEvent.vol = (byte)(musBytes[i] & 0x7F); channelVolume[newEvent.channelNum] = newEvent.vol; i++; //finished reading the vol byte } else //otherwise use the volume of the previous note on the channel is used. { newEvent.vol = channelVolume[newEvent.channelNum]; } } else if (newEvent.musEventType == 2)//Pitch Wheel { newEvent.pitch = (byte)(musBytes[i]); i++; //finished reading pitch wheel byte } else if (newEvent.musEventType == 3) //System Event { newEvent.sysEventNum = (byte)(musBytes[i] & 0x7F); i++; //finished reading sys event byte } else if (newEvent.musEventType == 4) //Change Controller { newEvent.contNum = (byte)(musBytes[i] & 0x7F); i++; //finished reading controller number byte newEvent.contVal = (byte)(musBytes[i] & 0x7F); i++; //finished reading controller value byte } else if (newEvent.musEventType == 5 || newEvent.musEventType == 7) { i++; //???????extra byte?????????? } else if (newEvent.musEventType == 6) //Score end (end of mus) { //END OF FILE EVENT newEvent.scoreEnd = true; //set the events scoreEnd bool to true newEvent.time = newMUS.musEvents[newMUS.musEvents.Count - 1].time; // set the end time to the last event's for some reason? newMUS.musEvents.Add(newEvent); //save the event newWad.music.Add(newMUS); //save the MUS break; } if (newEvent.last) //delay { int delay = 0; bool moreDelay = true; //if there is another delay byte after the current byte while (moreDelay) { moreDelay = (((BitConverter.ToChar(musBytes, i) & 0x80) >> 7) == 1); //set moreDelay by checking the LAST bit of the CURRENT BYTE byte newByte = musBytes[i]; delay = delay * 128 + (newByte & 0x7F); //set the delay i++; //next byte } newEvent.time = delay;//set the new time info } newMUS.musEvents.Add(newEvent);//save the event information } return(newMUS); //newWad.music.Add(newMUS); }