This repository has been archived on 2024-06-13. You can view files and clone it, but cannot push or open issues or pull requests.
TrueCraft/TrueCraft.API/NibbleSlice.cs
Drew DeVault 6fb8ee7ba5 Many more optimizations and bugfixes
Again, sorry for the huge commit. Just taking on performance issues as I
see them. Changes in this:

- Deadlocks in region code finally fixed
- Chunk packet preparation optimized (saves ~10-20ms per packet, since
  we're sending these like 30 at a time that's pretty important) by
  storing chunks pre-encoded in memory (basically just using a single
  big array for IDs, metadata, and light)
- Move chunk generation and compression to the thread pool
- Move client chunk updates to the scheduler
- Improve profiler coverage
- Add knob to disable scheduling chunk events on chunk load
- Make it possible to disable specific scheduled events in config.yml
2017-05-23 18:17:44 -04:00

82 lines
2.1 KiB
C#

using fNbt;
using fNbt.Serialization;
using System;
using System.Collections.ObjectModel;
namespace TrueCraft.API
{
/// <summary>
/// Represents a slice of an array of 4-bit values.
/// </summary>
public class NibbleSlice : INbtSerializable
{
/// <summary>
/// The data in the nibble array. Each byte contains
/// two nibbles, stored in big-endian.
/// </summary>
public byte[] Data { get; private set; }
public int Offset { get; private set; }
public int Length { get; private set; }
public NibbleSlice(byte[] data, int offset, int length)
{
Data = data;
Offset = offset;
Length = length;
}
/// <summary>
/// Gets or sets a nibble at the given index.
/// </summary>
[NbtIgnore]
public byte this[int index]
{
get { return (byte)(Data[Offset + index / 2] >> (index % 2 * 4) & 0xF); }
set
{
value &= 0xF;
Data[Offset + index / 2] &= (byte)(~(0xF << (index % 2 * 4)));
Data[Offset + index / 2] |= (byte)(value << (index % 2 * 4));
}
}
public byte[] ToArray()
{
byte[] array = new byte[Length];
Buffer.BlockCopy(Data, Offset, array, 0, Length);
return array;
}
public NbtTag Serialize(string tagName)
{
return new NbtByteArray(tagName, ToArray());
}
public void Deserialize(NbtTag value)
{
Length = value.ByteArrayValue.Length;
Buffer.BlockCopy(value.ByteArrayValue, 0,
Data, Offset, Length);
}
}
public class ReadOnlyNibbleArray
{
private NibbleSlice NibbleArray { get; set; }
public ReadOnlyNibbleArray(NibbleSlice array)
{
NibbleArray = array;
}
public byte this[int index]
{
get { return NibbleArray[index]; }
}
public ReadOnlyCollection<byte> Data
{
get { return Array.AsReadOnly(NibbleArray.Data); }
}
}
}