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)); } } } }
private static void updateOffsets(string spr, List <int> offsets) { // Start of tmx offsets int pos = 36; using (Stream stream = FileIOWrapper.Open(spr, FileMode.Open)) { foreach (int offset in offsets) { byte[] offsetBytes = BitConverter.GetBytes(offset); stream.Position = pos; stream.Write(offsetBytes, 0, 4); pos += 8; } } }
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; } } }
private static void updateOffsets(string spd, List <SpdHeaderHelper> helpers) { // Start of dds offsets int pos = 40; using (Stream stream = FileIOWrapper.Open(spd, FileMode.Open)) { foreach (var helper in helpers) { stream.Position = pos; stream.Write(BitConverter.GetBytes(helper.offset), 0, 4); pos += 4; stream.Position = pos; stream.Write(BitConverter.GetBytes(helper.size), 0, 4); pos += 44; } } }
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}"); } }