forked from Snoooopy/MetroUnlocker
First release build.
This commit is contained in:
29
MetroUnlocker/LibTSForge/PhysicalStore/BasicBlock.cs
Normal file
29
MetroUnlocker/LibTSForge/PhysicalStore/BasicBlock.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
{
|
||||
public class BasicBlock
|
||||
{
|
||||
public byte[] Key;
|
||||
public string KeyAsString
|
||||
{
|
||||
get { return Utils.DecodeString(Key); }
|
||||
set { Key = Utils.EncodeString(value); }
|
||||
}
|
||||
|
||||
public byte[] Value;
|
||||
public string ValueAsString
|
||||
{
|
||||
get { return Utils.DecodeString(Value); }
|
||||
set { Value = Utils.EncodeString(value); }
|
||||
}
|
||||
public uint ValueAsInteger
|
||||
{
|
||||
get { return BitConverter.ToUInt32(Value, 0); }
|
||||
set { Value = BitConverter.GetBytes(value); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,55 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
{
|
||||
public enum CRCBlockType : uint
|
||||
{
|
||||
UINT = 1 << 0,
|
||||
STRING = 1 << 1,
|
||||
BINARY = 1 << 2
|
||||
UInt = 1 << 0,
|
||||
String = 1 << 1,
|
||||
Binary = 1 << 2
|
||||
}
|
||||
|
||||
public class CRCBlock
|
||||
public class CRCBlock : BasicBlock
|
||||
{
|
||||
public CRCBlockType DataType;
|
||||
public byte[] Key;
|
||||
public string KeyAsStr
|
||||
|
||||
public CRCBlock() { }
|
||||
|
||||
public CRCBlock(BinaryReader reader)
|
||||
{
|
||||
get
|
||||
{
|
||||
return Utils.DecodeString(Key);
|
||||
}
|
||||
set
|
||||
{
|
||||
Key = Utils.EncodeString(value);
|
||||
}
|
||||
}
|
||||
public byte[] Value;
|
||||
public string ValueAsStr
|
||||
{
|
||||
get
|
||||
{
|
||||
return Utils.DecodeString(Value);
|
||||
}
|
||||
set
|
||||
{
|
||||
Value = Utils.EncodeString(value);
|
||||
}
|
||||
}
|
||||
public uint ValueAsInt
|
||||
{
|
||||
get
|
||||
{
|
||||
return BitConverter.ToUInt32(Value, 0);
|
||||
}
|
||||
set
|
||||
{
|
||||
Value = BitConverter.GetBytes(value);
|
||||
}
|
||||
uint crc = reader.ReadUInt32();
|
||||
uint type = reader.ReadUInt32();
|
||||
uint lenName = reader.ReadUInt32();
|
||||
uint lenVal = reader.ReadUInt32();
|
||||
|
||||
byte[] key = reader.ReadBytes((int)lenName);
|
||||
|
||||
reader.Align(8);
|
||||
|
||||
byte[] value = reader.ReadBytes((int)lenVal);
|
||||
reader.Align(8);
|
||||
|
||||
DataType = (CRCBlockType)type;
|
||||
Key = key;
|
||||
Value = value;
|
||||
|
||||
if (CRC() != crc)
|
||||
throw new InvalidDataException("Invalid CRC in variable bag.");
|
||||
}
|
||||
|
||||
public void Encode(BinaryWriter writer)
|
||||
@@ -67,45 +54,16 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
writer.Align(8);
|
||||
}
|
||||
|
||||
public static CRCBlock Decode(BinaryReader reader)
|
||||
{
|
||||
uint crc = reader.ReadUInt32();
|
||||
uint type = reader.ReadUInt32();
|
||||
uint lenName = reader.ReadUInt32();
|
||||
uint lenVal = reader.ReadUInt32();
|
||||
|
||||
byte[] key = reader.ReadBytes((int)lenName);
|
||||
|
||||
reader.Align(8);
|
||||
|
||||
byte[] value = reader.ReadBytes((int)lenVal);
|
||||
reader.Align(8);
|
||||
|
||||
CRCBlock block = new CRCBlock
|
||||
{
|
||||
DataType = (CRCBlockType)type,
|
||||
Key = key,
|
||||
Value = value,
|
||||
};
|
||||
|
||||
if (block.CRC() != crc)
|
||||
{
|
||||
throw new InvalidDataException("Invalid CRC in variable bag.");
|
||||
}
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
public uint CRC()
|
||||
{
|
||||
BinaryWriter wtemp = new BinaryWriter(new MemoryStream());
|
||||
wtemp.Write(0);
|
||||
wtemp.Write((uint)DataType);
|
||||
wtemp.Write(Key.Length);
|
||||
wtemp.Write(Value.Length);
|
||||
wtemp.Write(Key);
|
||||
wtemp.Write(Value);
|
||||
return Utils.CRC32(((MemoryStream)wtemp.BaseStream).ToArray());
|
||||
BinaryWriter writer = new BinaryWriter(new MemoryStream());
|
||||
writer.Write(0);
|
||||
writer.Write((uint)DataType);
|
||||
writer.Write(Key.Length);
|
||||
writer.Write(Value.Length);
|
||||
writer.Write(Key);
|
||||
writer.Write(Value);
|
||||
return Utils.CRC32(((MemoryStream)writer.BaseStream).ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,68 +6,41 @@ using System.IO;
|
||||
|
||||
namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
{
|
||||
public class ModernBlock
|
||||
public class ModernBlock : BasicBlock
|
||||
{
|
||||
public BlockType Type;
|
||||
public uint Flags;
|
||||
public uint Unknown;
|
||||
public byte[] Key;
|
||||
public string KeyAsStr
|
||||
{
|
||||
get
|
||||
{
|
||||
return Utils.DecodeString(Key);
|
||||
}
|
||||
set
|
||||
{
|
||||
Key = Utils.EncodeString(value);
|
||||
}
|
||||
}
|
||||
public byte[] Value;
|
||||
public string ValueAsStr
|
||||
{
|
||||
get
|
||||
{
|
||||
return Utils.DecodeString(Value);
|
||||
}
|
||||
set
|
||||
{
|
||||
Value = Utils.EncodeString(value);
|
||||
}
|
||||
}
|
||||
public uint ValueAsInt
|
||||
{
|
||||
get
|
||||
{
|
||||
return BitConverter.ToUInt32(Value, 0);
|
||||
}
|
||||
set
|
||||
{
|
||||
Value = BitConverter.GetBytes(value);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Data;
|
||||
public string DataAsStr
|
||||
public string DataAsString
|
||||
{
|
||||
get
|
||||
{
|
||||
return Utils.DecodeString(Data);
|
||||
}
|
||||
set
|
||||
{
|
||||
Data = Utils.EncodeString(value);
|
||||
}
|
||||
get { return Utils.DecodeString(Data); }
|
||||
set { Data = Utils.EncodeString(value); }
|
||||
}
|
||||
|
||||
public uint DataAsInt
|
||||
{
|
||||
get
|
||||
{
|
||||
return BitConverter.ToUInt32(Data, 0);
|
||||
}
|
||||
set
|
||||
{
|
||||
Data = BitConverter.GetBytes(value);
|
||||
}
|
||||
get { return BitConverter.ToUInt32(Data, 0); }
|
||||
set { Data = BitConverter.GetBytes(value); }
|
||||
}
|
||||
|
||||
public ModernBlock() { }
|
||||
public ModernBlock(string key, string value, byte[] data, BlockType type = BlockType.NAMED, uint flags = 0)
|
||||
{
|
||||
Type = type;
|
||||
Flags = flags;
|
||||
KeyAsString = key;
|
||||
ValueAsString = value;
|
||||
Data = data;
|
||||
}
|
||||
public ModernBlock(ModernBlock block)
|
||||
{
|
||||
Type = block.Type;
|
||||
Flags = block.Flags;
|
||||
Unknown = block.Unknown;
|
||||
Value = block.Value;
|
||||
Data = block.Data;
|
||||
}
|
||||
|
||||
public void Encode(BinaryWriter writer)
|
||||
@@ -81,7 +54,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
writer.Write(Data);
|
||||
}
|
||||
|
||||
public static ModernBlock Decode(BinaryReader reader)
|
||||
public ModernBlock(BinaryReader reader)
|
||||
{
|
||||
uint type = reader.ReadUInt32();
|
||||
uint flags = reader.ReadUInt32();
|
||||
@@ -93,14 +66,11 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
byte[] value = reader.ReadBytes((int)valueLen);
|
||||
byte[] data = reader.ReadBytes((int)dataLen);
|
||||
|
||||
return new ModernBlock
|
||||
{
|
||||
Type = (BlockType)type,
|
||||
Flags = flags,
|
||||
Unknown = unk3,
|
||||
Value = value,
|
||||
Data = data,
|
||||
};
|
||||
Type = (BlockType)type;
|
||||
Flags = flags;
|
||||
Unknown = unk3;
|
||||
Value = value;
|
||||
Data = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
private byte[] PreHeaderBytes = new byte[] { };
|
||||
private readonly Dictionary<string, List<ModernBlock>> Data = new Dictionary<string, List<ModernBlock>>();
|
||||
private readonly FileStream TSFile;
|
||||
private readonly PSVersion Version;
|
||||
private readonly PhysicalStoreVersion Version;
|
||||
private readonly bool Production;
|
||||
|
||||
public byte[] Serialize()
|
||||
@@ -66,7 +66,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
|
||||
for (int j = 0; j < numValues; j++)
|
||||
{
|
||||
Data[keyName].Add(ModernBlock.Decode(reader));
|
||||
Data[keyName].Add(new ModernBlock(reader));
|
||||
reader.Align(4);
|
||||
}
|
||||
}
|
||||
@@ -75,12 +75,10 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
|
||||
public void AddBlock(ModernBlock block)
|
||||
{
|
||||
if (!Data.ContainsKey(block.KeyAsStr))
|
||||
{
|
||||
Data[block.KeyAsStr] = new List<ModernBlock>();
|
||||
}
|
||||
if (!Data.ContainsKey(block.KeyAsString))
|
||||
Data[block.KeyAsString] = new List<ModernBlock>();
|
||||
|
||||
Data[block.KeyAsStr].Add(new ModernBlock
|
||||
Data[block.KeyAsString].Add(new ModernBlock
|
||||
{
|
||||
Type = block.Type,
|
||||
Flags = block.Flags,
|
||||
@@ -104,7 +102,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
if (block.ValueAsStr == value)
|
||||
if (block.ValueAsString == value)
|
||||
{
|
||||
return new ModernBlock
|
||||
{
|
||||
@@ -126,7 +124,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
if (block.ValueAsInt == value)
|
||||
if (block.ValueAsInteger == value)
|
||||
{
|
||||
return new ModernBlock
|
||||
{
|
||||
@@ -150,7 +148,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
{
|
||||
ModernBlock block = blocks[i];
|
||||
|
||||
if (block.ValueAsStr == value)
|
||||
if (block.ValueAsString == value)
|
||||
{
|
||||
block.Data = data;
|
||||
blocks[i] = block;
|
||||
@@ -169,7 +167,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
{
|
||||
ModernBlock block = blocks[i];
|
||||
|
||||
if (block.ValueAsInt == value)
|
||||
if (block.ValueAsInteger == value)
|
||||
{
|
||||
block.Data = data;
|
||||
blocks[i] = block;
|
||||
@@ -208,7 +206,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
if (block.ValueAsStr == value)
|
||||
if (block.ValueAsString == value)
|
||||
{
|
||||
blocks.Remove(block);
|
||||
break;
|
||||
@@ -227,7 +225,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
if (block.ValueAsInt == value)
|
||||
if (block.ValueAsInteger == value)
|
||||
{
|
||||
blocks.Remove(block);
|
||||
break;
|
||||
@@ -245,7 +243,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
return Path.Combine(Environment.ExpandEnvironmentVariables(sppRoot), "data.dat");
|
||||
}
|
||||
|
||||
public PhysicalStore(PSVersion version, bool production)
|
||||
public PhysicalStore(PhysicalStoreVersion version, bool production)
|
||||
{
|
||||
Version = version;
|
||||
Production = production;
|
||||
|
||||
@@ -10,10 +10,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
public List<CRCBlock> Blocks = new List<CRCBlock>();
|
||||
|
||||
public VariableBag() { }
|
||||
public VariableBag(byte[] data)
|
||||
{
|
||||
Deserialize(data);
|
||||
}
|
||||
public VariableBag(byte[] data) { Deserialize(data); }
|
||||
|
||||
public void Deserialize(byte[] data)
|
||||
{
|
||||
@@ -22,9 +19,7 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
BinaryReader reader = new BinaryReader(new MemoryStream(data));
|
||||
|
||||
while (reader.BaseStream.Position < len - 0x10)
|
||||
{
|
||||
Blocks.Add(CRCBlock.Decode(reader));
|
||||
}
|
||||
Blocks.Add(new CRCBlock(reader));
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
@@ -39,40 +34,19 @@ namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
|
||||
public CRCBlock GetBlock(string key)
|
||||
{
|
||||
foreach (CRCBlock block in Blocks)
|
||||
{
|
||||
if (block.KeyAsStr == key)
|
||||
{
|
||||
return block;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return Blocks.Find(block => block.KeyAsString == key);
|
||||
}
|
||||
|
||||
public void SetBlock(string key, byte[] value)
|
||||
{
|
||||
for (int i = 0; i < Blocks.Count; i++)
|
||||
{
|
||||
CRCBlock block = Blocks[i];
|
||||
|
||||
if (block.KeyAsStr == key)
|
||||
{
|
||||
block.Value = value;
|
||||
Blocks[i] = block;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int index = Blocks.FindIndex(block => block.KeyAsString == key);
|
||||
if (index != -1) Blocks[index].Value = value;
|
||||
}
|
||||
|
||||
public void DeleteBlock(string key)
|
||||
{
|
||||
foreach (CRCBlock block in Blocks)
|
||||
if (block.KeyAsStr == key)
|
||||
{
|
||||
Blocks.Remove(block);
|
||||
return;
|
||||
}
|
||||
CRCBlock block = GetBlock(key);
|
||||
if (block != null) Blocks.Remove(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user