forked from Snoooopy/MetroUnlocker
Initial commit
This commit is contained in:
111
MetroUnlocker/LibTSForge/PhysicalStore/CRCBlock.cs
Normal file
111
MetroUnlocker/LibTSForge/PhysicalStore/CRCBlock.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
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
|
||||
}
|
||||
|
||||
public class CRCBlock
|
||||
{
|
||||
public CRCBlockType DataType;
|
||||
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 void Encode(BinaryWriter writer)
|
||||
{
|
||||
uint crc = CRC();
|
||||
writer.Write(crc);
|
||||
writer.Write((uint)DataType);
|
||||
writer.Write(Key.Length);
|
||||
writer.Write(Value.Length);
|
||||
|
||||
writer.Write(Key);
|
||||
writer.Align(8);
|
||||
|
||||
writer.Write(Value);
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
106
MetroUnlocker/LibTSForge/PhysicalStore/ModernBlock.cs
Normal file
106
MetroUnlocker/LibTSForge/PhysicalStore/ModernBlock.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
{
|
||||
public class ModernBlock
|
||||
{
|
||||
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
|
||||
{
|
||||
get
|
||||
{
|
||||
return Utils.DecodeString(Data);
|
||||
}
|
||||
set
|
||||
{
|
||||
Data = Utils.EncodeString(value);
|
||||
}
|
||||
}
|
||||
public uint DataAsInt
|
||||
{
|
||||
get
|
||||
{
|
||||
return BitConverter.ToUInt32(Data, 0);
|
||||
}
|
||||
set
|
||||
{
|
||||
Data = BitConverter.GetBytes(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(BinaryWriter writer)
|
||||
{
|
||||
writer.Write((uint)Type);
|
||||
writer.Write(Flags);
|
||||
writer.Write((uint)Value.Length);
|
||||
writer.Write((uint)Data.Length);
|
||||
writer.Write(Unknown);
|
||||
writer.Write(Value);
|
||||
writer.Write(Data);
|
||||
}
|
||||
|
||||
public static ModernBlock Decode(BinaryReader reader)
|
||||
{
|
||||
uint type = reader.ReadUInt32();
|
||||
uint flags = reader.ReadUInt32();
|
||||
|
||||
uint valueLen = reader.ReadUInt32();
|
||||
uint dataLen = reader.ReadUInt32();
|
||||
uint unk3 = reader.ReadUInt32();
|
||||
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
282
MetroUnlocker/LibTSForge/PhysicalStore/PhysicalStore.cs
Normal file
282
MetroUnlocker/LibTSForge/PhysicalStore/PhysicalStore.cs
Normal file
@@ -0,0 +1,282 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
using Microsoft.Win32;
|
||||
|
||||
using MetroUnlocker.LibTSForge.Crypto;
|
||||
|
||||
namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
{
|
||||
public sealed class PhysicalStore : IDisposable
|
||||
{
|
||||
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 bool Production;
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
BinaryWriter writer = new BinaryWriter(new MemoryStream());
|
||||
writer.Write(PreHeaderBytes);
|
||||
writer.Write(Data.Keys.Count);
|
||||
|
||||
foreach (string key in Data.Keys)
|
||||
{
|
||||
List<ModernBlock> blocks = Data[key];
|
||||
byte[] keyNameEnc = Utils.EncodeString(key);
|
||||
|
||||
writer.Write(keyNameEnc.Length);
|
||||
writer.Write(keyNameEnc);
|
||||
writer.Write(blocks.Count);
|
||||
writer.Align(4);
|
||||
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
block.Encode(writer);
|
||||
writer.Align(4);
|
||||
}
|
||||
}
|
||||
|
||||
return Utils.GetBytes(writer);
|
||||
}
|
||||
|
||||
public void Deserialize(byte[] data)
|
||||
{
|
||||
BinaryReader reader = new BinaryReader(new MemoryStream(data));
|
||||
PreHeaderBytes = reader.ReadBytes(8);
|
||||
|
||||
while (reader.BaseStream.Position < data.Length - 0x4)
|
||||
{
|
||||
uint numKeys = reader.ReadUInt32();
|
||||
|
||||
for (int i = 0; i < numKeys; i++)
|
||||
{
|
||||
uint lenKeyName = reader.ReadUInt32();
|
||||
string keyName = Utils.DecodeString(reader.ReadBytes((int)lenKeyName)); uint numValues = reader.ReadUInt32();
|
||||
|
||||
reader.Align(4);
|
||||
|
||||
Data[keyName] = new List<ModernBlock>();
|
||||
|
||||
for (int j = 0; j < numValues; j++)
|
||||
{
|
||||
Data[keyName].Add(ModernBlock.Decode(reader));
|
||||
reader.Align(4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddBlock(ModernBlock block)
|
||||
{
|
||||
if (!Data.ContainsKey(block.KeyAsStr))
|
||||
{
|
||||
Data[block.KeyAsStr] = new List<ModernBlock>();
|
||||
}
|
||||
|
||||
Data[block.KeyAsStr].Add(new ModernBlock
|
||||
{
|
||||
Type = block.Type,
|
||||
Flags = block.Flags,
|
||||
Unknown = block.Unknown,
|
||||
Value = block.Value,
|
||||
Data = block.Data
|
||||
});
|
||||
}
|
||||
|
||||
public void AddBlocks(IEnumerable<ModernBlock> blocks)
|
||||
{
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
AddBlock(block);
|
||||
}
|
||||
}
|
||||
|
||||
public ModernBlock GetBlock(string key, string value)
|
||||
{
|
||||
List<ModernBlock> blocks = Data[key];
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
if (block.ValueAsStr == value)
|
||||
{
|
||||
return new ModernBlock
|
||||
{
|
||||
Type = block.Type,
|
||||
Flags = block.Flags,
|
||||
Key = Utils.EncodeString(key),
|
||||
Value = block.Value,
|
||||
Data = block.Data
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public ModernBlock GetBlock(string key, uint value)
|
||||
{
|
||||
List<ModernBlock> blocks = Data[key];
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
if (block.ValueAsInt == value)
|
||||
{
|
||||
return new ModernBlock
|
||||
{
|
||||
Type = block.Type,
|
||||
Flags = block.Flags,
|
||||
Key = Utils.EncodeString(key),
|
||||
Value = block.Value,
|
||||
Data = block.Data
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void SetBlock(string key, string value, byte[] data)
|
||||
{
|
||||
List<ModernBlock> blocks = Data[key];
|
||||
|
||||
for (int i = 0; i < blocks.Count; i++)
|
||||
{
|
||||
ModernBlock block = blocks[i];
|
||||
|
||||
if (block.ValueAsStr == value)
|
||||
{
|
||||
block.Data = data;
|
||||
blocks[i] = block;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Data[key] = blocks;
|
||||
}
|
||||
|
||||
public void SetBlock(string key, uint value, byte[] data)
|
||||
{
|
||||
List<ModernBlock> blocks = Data[key];
|
||||
|
||||
for (int i = 0; i < blocks.Count; i++)
|
||||
{
|
||||
ModernBlock block = blocks[i];
|
||||
|
||||
if (block.ValueAsInt == value)
|
||||
{
|
||||
block.Data = data;
|
||||
blocks[i] = block;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Data[key] = blocks;
|
||||
}
|
||||
|
||||
public void SetBlock(string key, string value, string data)
|
||||
{
|
||||
SetBlock(key, value, Utils.EncodeString(data));
|
||||
}
|
||||
|
||||
public void SetBlock(string key, string value, uint data)
|
||||
{
|
||||
SetBlock(key, value, BitConverter.GetBytes(data));
|
||||
}
|
||||
|
||||
public void SetBlock(string key, uint value, string data)
|
||||
{
|
||||
SetBlock(key, value, Utils.EncodeString(data));
|
||||
}
|
||||
|
||||
public void SetBlock(string key, uint value, uint data)
|
||||
{
|
||||
SetBlock(key, value, BitConverter.GetBytes(data));
|
||||
}
|
||||
|
||||
public void DeleteBlock(string key, string value)
|
||||
{
|
||||
if (Data.ContainsKey(key))
|
||||
{
|
||||
List<ModernBlock> blocks = Data[key];
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
if (block.ValueAsStr == value)
|
||||
{
|
||||
blocks.Remove(block);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Data[key] = blocks;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteBlock(string key, uint value)
|
||||
{
|
||||
if (Data.ContainsKey(key))
|
||||
{
|
||||
List<ModernBlock> blocks = Data[key];
|
||||
|
||||
foreach (ModernBlock block in blocks)
|
||||
{
|
||||
if (block.ValueAsInt == value)
|
||||
{
|
||||
blocks.Remove(block);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Data[key] = blocks;
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetPath()
|
||||
{
|
||||
string sppRoot = Utils.GetTokenStorePath();
|
||||
|
||||
return Path.Combine(Environment.ExpandEnvironmentVariables(sppRoot), "data.dat");
|
||||
}
|
||||
|
||||
public PhysicalStore(PSVersion version, bool production)
|
||||
{
|
||||
Version = version;
|
||||
Production = production;
|
||||
|
||||
TSFile = File.Open(GetPath(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
||||
|
||||
Deserialize(ReadRaw());
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
WriteRaw(Serialize());
|
||||
}
|
||||
|
||||
public byte[] ReadRaw()
|
||||
{
|
||||
byte[] data = PhysicalStoreCrypto.DecryptPhysicalStore(Utils.ReadAllBytes(TSFile), Production);
|
||||
TSFile.Seek(0, SeekOrigin.Begin);
|
||||
return data;
|
||||
}
|
||||
|
||||
public void WriteRaw(byte[] data)
|
||||
{
|
||||
if (TSFile.CanWrite)
|
||||
{
|
||||
byte[] encrData = PhysicalStoreCrypto.EncryptPhysicalStore(data, Production, Version);
|
||||
TSFile.SetLength(encrData.LongLength);
|
||||
TSFile.Seek(0, SeekOrigin.Begin);
|
||||
Utils.WriteAllBytes(TSFile, encrData);
|
||||
TSFile.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
78
MetroUnlocker/LibTSForge/PhysicalStore/VariableBag.cs
Normal file
78
MetroUnlocker/LibTSForge/PhysicalStore/VariableBag.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace MetroUnlocker.LibTSForge.PhysicalStore
|
||||
{
|
||||
public class VariableBag
|
||||
{
|
||||
public List<CRCBlock> Blocks = new List<CRCBlock>();
|
||||
|
||||
public VariableBag() { }
|
||||
public VariableBag(byte[] data)
|
||||
{
|
||||
Deserialize(data);
|
||||
}
|
||||
|
||||
public void Deserialize(byte[] data)
|
||||
{
|
||||
int len = data.Length;
|
||||
|
||||
BinaryReader reader = new BinaryReader(new MemoryStream(data));
|
||||
|
||||
while (reader.BaseStream.Position < len - 0x10)
|
||||
{
|
||||
Blocks.Add(CRCBlock.Decode(reader));
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Serialize()
|
||||
{
|
||||
BinaryWriter writer = new BinaryWriter(new MemoryStream());
|
||||
|
||||
foreach (CRCBlock block in Blocks)
|
||||
block.Encode(writer);
|
||||
|
||||
return ((MemoryStream)writer.BaseStream).ToArray();
|
||||
}
|
||||
|
||||
public CRCBlock GetBlock(string key)
|
||||
{
|
||||
foreach (CRCBlock block in Blocks)
|
||||
{
|
||||
if (block.KeyAsStr == key)
|
||||
{
|
||||
return block;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteBlock(string key)
|
||||
{
|
||||
foreach (CRCBlock block in Blocks)
|
||||
if (block.KeyAsStr == key)
|
||||
{
|
||||
Blocks.Remove(block);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user