public static void replaceDDS(string spd, string dds) { List <DDS> ddsFiles = getDDSFiles(spd); byte[] ddsBytes = FileIOWrapper.ReadAllBytes(dds); foreach (var ddsFile in ddsFiles) { if (ddsFile.name == Path.GetFileNameWithoutExtension(dds)) { if (ddsBytes.Length == ddsFile.size) { using (Stream stream = FileIOWrapper.Open(spd, FileMode.Open)) { stream.Position = ddsFile.pos; stream.Write(ddsBytes, 0, ddsFile.size); } } else { byte[] spdBytes = FileIOWrapper.ReadAllBytes(spd); byte[] newSpd = new byte[spdBytes.Length + (ddsBytes.Length - ddsFile.size)]; SliceArray(spdBytes, 0, ddsFile.pos).CopyTo(newSpd, 0); SliceArray(spdBytes, ddsFile.pos + ddsFile.size, spdBytes.Length).CopyTo(newSpd, ddsFile.pos + ddsBytes.Length); ddsBytes.CopyTo(newSpd, ddsFile.pos); FileIOWrapper.WriteAllBytes(spd, newSpd); updateOffsets(spd, getDDSOffsets(spd)); } } } }
public static Dictionary <string, int> getTmxNames(string spr) { Dictionary <string, int> tmxNames = new Dictionary <string, int>(); byte[] sprBytes = FileIOWrapper.ReadAllBytes(spr); byte[] pattern = Encoding.ASCII.GetBytes("TMX0"); int offset = 0; int found = 0; while (found != -1) { // Start search after "TMX0" found = Search(SliceArray(sprBytes, offset, sprBytes.Length), pattern); offset = found + offset + 4; if (found != -1) { string ogTmxName = getTmxName(SliceArray(sprBytes, (offset + 24), sprBytes.Length)); string tmxName = ogTmxName; int index = 2; while (tmxNames.ContainsKey(tmxName)) { tmxName = $"{ogTmxName}({index})"; index += 1; } tmxNames.Add(tmxName, offset - 12); } } return(tmxNames); }
public static byte[] extractTmx(string spr, string tmx) { string tmxPattern = Path.GetFileNameWithoutExtension(tmx); int offset = findTmx(spr, tmxPattern); if (offset > -1) { byte[] sprBytes = FileIOWrapper.ReadAllBytes(spr); int tmxLen = BitConverter.ToInt32(sprBytes, (offset + 4)); return(SliceArray(sprBytes, offset, offset + tmxLen)); } return(null); }
public static void replaceSPDKey(string spd, string spdspr) { List <SPDKey> spdKeys = getSPDKeys(spd); byte[] spdBytes = FileIOWrapper.ReadAllBytes(spdspr); foreach (var spdKey in spdKeys) { if (spdKey.id.ToString() == Path.GetFileNameWithoutExtension(spdspr)) { using (Stream stream = FileIOWrapper.Open(spd, FileMode.Open)) { stream.Position = spdKey.pos; stream.Write(spdBytes, 0, 160); } return; } } }
public static List <SPDKey> getSPDKeys(string spd) { List <SPDKey> spdKeys = new List <SPDKey>(); byte[] spdBytes = FileIOWrapper.ReadAllBytes(spd); int numKeys = BitConverter.ToUInt16(spdBytes, 22); int pos = BitConverter.ToInt32(spdBytes, 28); SPDKey spdKey; for (int i = 0; i < numKeys; i++) { spdKey = new SPDKey(); spdKey.pos = pos; spdKey.id = BitConverter.ToInt32(spdBytes, pos); spdKey.file = SliceArray(spdBytes, spdKey.pos, spdKey.pos + 160); spdKeys.Add(spdKey); pos += 160; } return(spdKeys); }
private static List <int> getTmxOffsets(string spr) { List <int> tmxOffsets = new List <int>(); byte[] sprBytes = FileIOWrapper.ReadAllBytes(spr); byte[] pattern = Encoding.ASCII.GetBytes("TMX0"); int offset = 0; int found = 0; while (found != -1) { // Start search after "TMX0" found = Search(SliceArray(sprBytes, offset, sprBytes.Length), pattern); offset = found + offset + 4; if (found != -1) { tmxOffsets.Add(offset - 12); } } return(tmxOffsets); }
public static List <DDS> getDDSFiles(string spd) { List <DDS> ddsNames = new List <DDS>(); byte[] spdBytes = FileIOWrapper.ReadAllBytes(spd); int numTextures = BitConverter.ToUInt16(spdBytes, 20); int pos = 32; DDS dds; for (int i = 0; i < numTextures; i++) { dds = new DDS(); int tag = BitConverter.ToInt32(spdBytes, pos); dds.pos = BitConverter.ToUInt32(spdBytes, pos + 8); dds.size = BitConverter.ToInt32(spdBytes, pos + 12); dds.name = $"{Encoding.ASCII.GetString(SliceArray(spdBytes, pos + 32, pos + 47)).TrimEnd('\0')}[{tag}]"; dds.file = SliceArray(spdBytes, dds.pos, dds.pos + dds.size - 1); ddsNames.Add(dds); pos += 48; } return(ddsNames); }
private static List <SpdHeaderHelper> getDDSOffsets(string spd) { List <SpdHeaderHelper> ddsHelpers = new List <SpdHeaderHelper>(); byte[] spdBytes = FileIOWrapper.ReadAllBytes(spd); byte[] pattern = Encoding.ASCII.GetBytes("DDS"); int offset = 0; int found = 0; SpdHeaderHelper helper; while (found != -1) { helper = new SpdHeaderHelper(); // Start search after "DDS" found = Search(SliceArray(spdBytes, offset, spdBytes.Length), pattern); offset = found + offset; if (found != -1) { helper.offset = offset; ddsHelpers.Add(helper); } offset += 4; } for (int i = 0; i < ddsHelpers.Count; i++) { if (i != ddsHelpers.Count - 1) { ddsHelpers[i].size = (int)(ddsHelpers[i + 1].offset - ddsHelpers[i].offset); } else { ddsHelpers[i].size = (int)(spdBytes.Length - ddsHelpers[i].offset); } } return(ddsHelpers); }
public static void replaceTmx(string spr, string tmx) { string tmxPattern = Path.GetFileNameWithoutExtension(tmx); int offset = findTmx(spr, tmxPattern); if (offset > -1) { Console.WriteLine($"[INFO] Merging {tmx} onto {spr}"); byte[] tmxBytes = FileIOWrapper.ReadAllBytes(tmx); int repTmxLen = tmxBytes.Length; int ogTmxLen = BitConverter.ToInt32(FileIOWrapper.ReadAllBytes(spr), (offset + 4)); if (repTmxLen == ogTmxLen) { using (Stream stream = FileIOWrapper.Open(spr, FileMode.Open)) { stream.Position = offset; stream.Write(tmxBytes, 0, repTmxLen); } } else // Insert and update offsets { byte[] sprBytes = FileIOWrapper.ReadAllBytes(spr); byte[] newSpr = new byte[sprBytes.Length + (repTmxLen - ogTmxLen)]; SliceArray(sprBytes, 0, offset).CopyTo(newSpr, 0); SliceArray(sprBytes, offset + ogTmxLen, sprBytes.Length).CopyTo(newSpr, offset + repTmxLen); tmxBytes.CopyTo(newSpr, offset); FileIOWrapper.WriteAllBytes(spr, newSpr); updateOffsets(spr, getTmxOffsets(spr)); } } else { Console.WriteLine($"[WARNING] Couldn't find {tmx} in {spr}"); } }