public void read(Reader reader, bool skipHeader = false, int clrCnt = 0x80) { #region Header if (!skipHeader) { reader.seek(6, System.IO.SeekOrigin.Begin); // GIF89a width = reader.ReadByte(); width |= (ushort)(reader.ReadByte() << 8); height = reader.ReadByte(); height |= (ushort)(reader.ReadByte() << 8); byte info = reader.ReadByte(); // Palette Size clrCnt = (info & 0x7) + 1; if (clrCnt > 0) { clrCnt = 1 << clrCnt; } reader.ReadByte(); // background colour index reader.ReadByte(); // unused if (clrCnt != 0x100) { throw new Exception("RSDK-Formatted Gif files must use 256 colours!"); } } for (int c = 0; c < clrCnt; ++c) { palette[c].R = reader.ReadByte(); palette[c].G = reader.ReadByte(); palette[c].B = reader.ReadByte(); } #endregion #region Blocks byte blockType = reader.ReadByte(); while (blockType != 0 && blockType != ';') { switch (blockType) { default: // Unknown Console.WriteLine($"Unknown Block Type ({blockType})"); break; case (byte)'!': // Extension { byte extensionType = reader.ReadByte(); switch (extensionType) { case 0xF9: // Graphics Control Extension { int blockSize = reader.ReadByte(); byte disposalFlag = reader.ReadByte(); ushort frameDelay = reader.ReadUInt16(); byte transparentIndex = reader.ReadByte(); reader.ReadByte(); // terminator } break; case 0x01: // Plain Text Extension case 0xFE: // Comment Extension case 0xFF: // Application Extension { int blockSize = reader.ReadByte(); while (blockSize != 0) { // Read block reader.BaseStream.Position += blockSize; blockSize = reader.ReadByte(); // next block Size, if its 0 we know its the end of block } } break; default: // Unknown Console.WriteLine($"Unknown Extension Type ({extensionType})"); return; } } break; case (byte)',': // Image descriptor { int left = reader.ReadUInt16(); int top = reader.ReadUInt16(); int right = reader.ReadUInt16(); int bottom = reader.ReadUInt16(); byte info2 = reader.ReadByte(); bool interlaced = (info2 & 0x40) != 0; if (info2 >> 7 == 1) { for (int c = 0x80; c < 0x100; ++c) { palette[c].R = reader.ReadByte(); palette[c].G = reader.ReadByte(); palette[c].B = reader.ReadByte(); } } readPictureData(width, height, interlaced, reader); } break; } blockType = reader.ReadByte(); } #endregion if (!skipHeader) { reader.Close(); } }
public void read(Reader reader) { if (!reader.readBytes(4).SequenceEqual(signature)) { reader.Close(); throw new Exception("Invalid Static Object v5 signature"); } int memPos = 0; while (!reader.isEof) { int type = reader.ReadByte(); int arraySize = reader.ReadInt32(); if ((type & 0x80) != 0) { uint count = reader.ReadUInt32(); type &= 0x7F; ArrayInfo array = new ArrayInfo(); array.type = (byte)type; array.size = arraySize; array.valueCount = (int)count; array.values = new int[(int)count]; switch (type) { default: Console.WriteLine($"ERROR: Encountered unexpected array type ({type})!"); break; //INT8 case (int)VariableTypes.UINT8: for (int i = 0; i < count; ++i) { array.values[i] = reader.ReadByte(); } memPos += arraySize; break; case (int)VariableTypes.INT8: for (int i = 0; i < count; ++i) { array.values[i] = reader.ReadSByte(); } memPos += arraySize; break; //IN16 case (int)VariableTypes.UINT16: int tmpMemPos = (int)((memPos & 0xFFFFFFFE) + 2); if ((memPos & 0xFFFFFFFE) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos; for (int i = 0; i < count; ++i) { byte valA = reader.ReadByte(); byte valB = reader.ReadByte(); array.values[i] = (ushort)(valA + (valB << 8)); } memPos += 2 * arraySize; break; case (int)VariableTypes.INT16: tmpMemPos = (int)((memPos & 0xFFFFFFFE) + 2); if ((memPos & 0xFFFFFFFE) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos; for (int i = 0; i < count; ++i) { byte valA = reader.ReadByte(); byte valB = reader.ReadByte(); array.values[i] = (short)(valA + (valB << 8)); } memPos += 2 * arraySize; break; //INT32 case (int)VariableTypes.UINT32: tmpMemPos = (int)((memPos & 0xFFFFFFFC) + 4); if ((memPos & 0xFFFFFFFC) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos; for (int i = 0; i < count; ++i) { byte valA = reader.ReadByte(); byte valB = reader.ReadByte(); byte valC = reader.ReadByte(); byte valD = reader.ReadByte(); array.values[i] = (int)(uint)(valA + (valB << 8) + (valC << 16) + (valD << 24)); } memPos += 4 * arraySize; break; case (int)VariableTypes.INT32: tmpMemPos = (int)((memPos & 0xFFFFFFFC) + 4); if ((memPos & 0xFFFFFFFC) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos; for (int i = 0; i < count; ++i) { byte valA = reader.ReadByte(); byte valB = reader.ReadByte(); byte valC = reader.ReadByte(); byte valD = reader.ReadByte(); array.values[i] = valA + (valB << 8) + (valC << 16) + (valD << 24); } memPos += 4 * arraySize; break; case (int)VariableTypes.ENUM: // bool tmpMemPos = (int)((memPos & 0xFFFFFFFC) + 4); if ((memPos & 0xFFFFFFFC) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos; for (int i = 0; i < count; ++i) { byte valA = reader.ReadByte(); byte valB = reader.ReadByte(); byte valC = reader.ReadByte(); byte valD = reader.ReadByte(); array.values[i] = valA + (valB << 8) + (valC << 16) + (valD << 24); } memPos += 4 * arraySize; break; } arrays.Add(array); } else { ArrayInfo array = new ArrayInfo(); array.type = (byte)type; array.size = arraySize; array.valueCount = 0; array.values = new int[0]; arrays.Add(array); int tmpMemPos = 0; switch (type) { //INT8 case (int)VariableTypes.UINT8: case (int)VariableTypes.INT8: memPos += arraySize; break; //IN16 case (int)VariableTypes.UINT16: case (int)VariableTypes.INT16: tmpMemPos = (int)((memPos & 0xFFFFFFFE) + 2); if ((memPos & 0xFFFFFFFE) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos + 2 * arraySize; break; //INT32 case (int)VariableTypes.UINT32: case (int)VariableTypes.INT32: case 6: //bool tmpMemPos = (int)((memPos & 0xFFFFFFFC) + 4); if ((memPos & 0xFFFFFFFC) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos + 4 * arraySize; break; case 7: // Pointer tmpMemPos = (int)((memPos & 0xFFFFFFFC) + 4); if ((memPos & 0xFFFFFFFC) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos + 4 * arraySize; break; case 8: // Vector2 tmpMemPos = (int)((memPos & 0xFFFFFFFC) + 4); if ((memPos & 0xFFFFFFFC) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos + 8 * arraySize; break; case 9: // Text tmpMemPos = (int)((memPos & 0xFFFFFFFC) + 4); if ((memPos & 0xFFFFFFFC) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos + 8 * arraySize; break; case 10: // Animator tmpMemPos = (int)((memPos & 0xFFFFFFFC) + 4); if ((memPos & 0xFFFFFFFC) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos + 24 * arraySize; break; case 11: // Hitbox tmpMemPos = (int)((memPos & 0xFFFFFFFE) + 2); if ((memPos & 0xFFFFFFFE) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos + 8 * arraySize; break; case 12: // Unknown tmpMemPos = (int)((memPos & 0xFFFFFFFE) + 2); if ((memPos & 0xFFFFFFFE) >= memPos) { tmpMemPos = memPos; } memPos = tmpMemPos + 18 * arraySize; break; default: break; } } } reader.Close(); }
public StaticObject(Reader reader) { uint[] TmpData = new uint[reader.BaseStream.Length]; DataPos = 0; if (!reader.ReadBytes(4).SequenceEqual(MAGIC)) //"OBJ" Header { throw new Exception("Invalid config file header magic"); } while (!reader.IsEof) { int Unknown1 = reader.ReadByte(); reader.ReadUInt32(); //Unknown if ((Unknown1 & 0x80) != 0) { uint Unknown3 = reader.ReadUInt32(); int Variable1 = Unknown1 & 0x7F; switch (Variable1) { //INT8 case 0: case 3: for (int i = 0; i < Unknown3; i++) { TmpData[DataPos++] = reader.ReadByte(); } break; //IN16 case 1: case 4: for (int i = 0; i < Unknown3; i++) { byte valA = reader.ReadByte(); byte valB = reader.ReadByte(); int Value = valA + (valB << 8); TmpData[DataPos++] = (uint)Value; } break; //INT32 case 2: case 5: case 6: for (int i = 0; i < Unknown3; i++) { byte valA = reader.ReadByte(); byte valB = reader.ReadByte(); byte valC = reader.ReadByte(); byte valD = reader.ReadByte(); int Value = valA + (valB << 8) + (valC << 16) + (valD << 24); TmpData[DataPos++] = (uint)Value; } break; } } } reader.Close(); Data = new uint[DataPos]; for (int i = 0; i < DataPos; i++) { Data[i] = TmpData[i]; } }