public static IParsedChunk TryParse(ChunkData data) { IParsedChunk o = null; Type type = GetType(data.Header.ObjectID); if (type != null) { try { o = (IParsedChunk)System.Activator.CreateInstance(type); o.Parse(data); } catch (System.Reflection.TargetInvocationException e) { throw e.InnerException; } } return(o); }
public void Read(Stream input) { // lazy check... pc savegames are exactly 891904 bytes... bool isXBox = input.Length != 891904; int _headerOffset; int _chunkHeaderOffset; int _chunkDataOffset; if (isXBox) { EndianessSwitchableBitConverter.Mode = EndianessSwitchableBitConverter.Endianess.BIG; } BinaryReader r; if (isXBox) { r = new BigEndianBinaryReader(input); _headerOffset = OFFSET_XBOX360; _chunkHeaderOffset = CHUNK_HEADERS_OFFSET + OFFSET_XBOX360; _chunkDataOffset = CHUNK_DATA_OFFSET + OFFSET_XBOX360; } else { r = new BinaryReader(input); _headerOffset = 0; _chunkHeaderOffset = CHUNK_HEADERS_OFFSET; _chunkDataOffset = CHUNK_DATA_OFFSET; } r.BaseStream.Seek(_headerOffset, SeekOrigin.Begin); Header = new SaveFileHeader(); Header.Read(r); // file can contain up to 9 headers - probably history, BUT can also contain junk, so read only first header.... r.BaseStream.Seek(_chunkHeaderOffset, SeekOrigin.Begin); int count = r.ReadInt32(); List <ChunkHeader> _chunkHeaders = new List <ChunkHeader>(count); for (int i = 0; i < count; i++) { ChunkHeader header = new ChunkHeader(r); _chunkHeaders.Add(header); } _chunkData.Clear(); _parsedChunks.Clear(); for (int i = 0; i < count; i++) { ChunkData data = new ChunkData(); data.Read(r, _chunkHeaders[i], _chunkDataOffset); _chunkData.Add(data); IParsedChunk p = ParsedChunkTypeHelper.TryParse(data); _parsedChunks.Add(p); /* * if (p == null) * { * foreach (ChunkDataEntry e in data.DataEntries) * { * if (e.Data.Length == 4) * { * uint hash = BitConverter.ToUInt32(e.Data, 0); * string name = NameLookup.GetName(hash); * if (name != null) * { * //System.Diagnostics.Debugger.Break(); * } * } * } * }*/ } r.Close(); input.Close(); }