public static VarConfig ReadNew(Stream stream) { VarConfig config = new VarConfig(); // Because of the Hash, two passes are necessary byte[] buffer = new byte[stream.Length - stream.Position]; if (stream.Read(buffer, 0, buffer.Length) != buffer.Length) { throw new InvalidDataException("Could not read VarConfig buffer"); } using BinaryReader reader = new BinaryReader(new MemoryStream(buffer, false)); byte[] expectedChecksum = reader.ReadBytes(16); config.header = reader.ReadBytes(3); config.firstValue = VarConfigValue.ReadNew(reader); int startOfHashed = (int)(reader.BaseStream.Position); while (reader.PeekChar() >= 0) { string name = ReadEncryptedString(reader); VarConfigValue value = VarConfigValue.ReadNew(reader); config.variables[name] = value; } using var md5 = MD5.Create(); byte[] actualChecksum = md5.ComputeHash(buffer, startOfHashed, buffer.Length - startOfHashed); if (!actualChecksum.SequenceEqual(expectedChecksum)) { throw new InvalidDataException("VarConfig checksums do not match"); } return(config); }
public void Write(BinaryWriter writer) { writer.Write(floatValue); writer.Write((byte)(stringValue.Length > 0 ? 1 : 0)); if (stringValue.Length > 0) { VarConfig.WriteEncryptedString(writer, stringValue); } writer.Write((byte)1); }
public static VarConfigValue ReadNew(BinaryReader reader) { float floatValue = reader.ReadSingle(); byte isString = reader.ReadByte(); string stringValue = isString == 0 ? "" : VarConfig.ReadEncryptedString(reader); reader.ReadByte(); // ignored byte at the end return(new VarConfigValue(floatValue, stringValue)); }