public static KnyttSave FromBinary(KnyttWorld world, byte[] data, bool slim = false) { var save = new KnyttSave(world); int bposition = 0; if (!slim) { var y = data[bposition++]; var k = data[bposition++]; if (y != 'Y' || k != 'K') { throw new SystemException("Magic number YK not present"); } } using (SHA1Managed sha1 = new SHA1Managed()) { var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(save.getWorldDirectory())); for (int i = 0; i < (slim ? SLIM_HASH_BYTES : HASH_BYTES); i++) { if (!(hash[i] == data[bposition++])) { throw new SystemException("World name hash doesn't match."); } } } save.setArea(new KnyttPoint((int)new VarInt(data, ref bposition).Value + 1000, (int)new VarInt(data, ref bposition).Value + 1000)); int bitpack = (int)new VarInt(data, ref bposition).Value; save.setAreaPosition(new KnyttPoint(bitpack & 0x1F, (bitpack & (0xF << 5)) >> 5)); JuniValues values = new JuniValues(); BinaryTools.readBoolArray(bitpack, values.Powers, 9); BinaryTools.readBoolArray(bitpack, values.Flags, 21); values.writeToSave(save); return(save); }
public byte[] ToBinary(bool slim = false) { List <byte> result = new List <byte>(); if (!slim) { result.Add((byte)'Y'); result.Add((byte)'K'); } using (SHA1Managed sha1 = new SHA1Managed()) { var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(getWorldDirectory())); for (int i = 0; i < (slim ? SLIM_HASH_BYTES : HASH_BYTES); i++) { result.Add(hash[i]); } } var area = getArea(); result.AddRange(new VarInt(area.x - 1000).Bytes); result.AddRange(new VarInt(area.y - 1000).Bytes); int bitpack = 0; var pos = getAreaPosition(); bitpack |= pos.x & 0x1F; bitpack |= (pos.y & 0xF) << 5; JuniValues values = new JuniValues(this); bitpack = BinaryTools.writeBoolArray(values.Powers, bitpack, 9); bitpack = BinaryTools.writeBoolArray(values.Flags, bitpack, 21); result.AddRange(new VarInt(bitpack).Bytes); return(result.ToArray()); }