Skip to main content

Stream

An abstract class in Alta.Serialization.Runtime, Alta.Serialization.

The bit reader and writer under every network message and save file. Each SerializeX(ref value) writes on a StreamWriter and reads on a StreamReader, so one method covers both directions.

Constructors​

MemberWhat it is
Stream(bool isReading, bool isWriting)Creates a Stream from isReading and isWriting.

Fields​

MemberWhat it is
bool IsReadingTrue on a reader. A public field, so code can clear it to skip the reading side.
readonly bool IsWritingTrue on a writer.
static readonly BitWriter.GetMoreData ResizeDataFuncPass to new StreamWriter(...) for a writer that doubles its buffer as needed. Buffers come from a shared pool.
protected static readonly byte[] EmptyBytesA shared empty byte array for subclasses.
4 private and internal fields
MemberWhat it is
private const float EpsilonTolerance (1E-31) used by SerializeDirection to decide whether a vector needs normalizing before it is written.
private const float NormalizedMaximumLargest value (about 0.7071) a non-largest component of a unit quaternion or direction can have; the top of the compressed range.
private const float NormalizedMinimumSmallest value (about -0.7071) a compressed quaternion or direction component can have; the bottom of the compressed range.
private const float NormalizedRangeWidth of the compressed component range (about 1.4142), used to scale components in quaternion and direction compression.

Properties​

MemberWhat it is
abstract uint BitIndex { get; }How many bits have been read or written so far.
virtual bool IsCounter { get; }True for a StreamCounter, which only counts bits and stores nothing (used to measure a save before writing it).
abstract uint TotalByteCount { get; }The size of the data in bytes.

Methods​

MemberWhat it is
abstract void Align()Moves to the next byte boundary.
void AlignAndSerializeBytes(byte[] value, int byteCount)Aligns to a byte, then reads or writes a run of raw bytes.
void Assert(uint value, string label)Editor builds only: writes or reads a check value and logs an error if it doesn't match. Compiled out of the game.
abstract void BackTrack(uint bitCount)Moves the position back by a number of bits.
static int CalculateRequiredBits(uint number)How many bits it takes to hold a number. Always at least 1.
abstract object Clone()A copy of the stream at the same position. Use it to keep a message for later.
abstract void Dispose()Releases the stream's buffer. Copy a writer's data out first.
abstract void SerializeBits(ref uint value, int bitCount)The primitive: reads or writes the low bitCount bits of a value. The array overload does a run of words.
void SerializeBits(uint[] bits, uint bitCount, bool isAligned)The primitive: reads or writes the low bitCount bits of a value. The array overload does a run of words.
void SerializeBool(ref bool value)One bit.
void SerializeByte(ref byte value)Eight bits.
bool SerializeCheck(bool check)One bit that returns the value, for if (stream.SerializeCheck(hasThing)) patterns.
void SerializeColor(ref Color color)A colour as four full floats, or four 0 to 1 values in a given number of bits each.
void SerializeColor(ref Color color, int bitsPerPart)A colour as four full floats, or four 0 to 1 values in a given number of bits each.
void SerializeDirection(ref Vector3 vector, int directionBitsPerPart = 10)A unit vector in about 23 bits by default. The vector is normalized before writing.
void SerializeDirectionMagnitude(ref Vector3 vector, float maximumMagnitude, float magnitudePrecision, int directionBitsPerPart = 10)A vector as compressed direction plus a length clamped to a maximum. Lengths under the precision come back as zero.
void SerializeFloat(ref float value)A full 32-bit float, or a value packed into a min to max range by precision or bit count. Out-of-range values are clamped.
void SerializeFloat(ref float value, float minimum, float maximum, float precision)A full 32-bit float, or a value packed into a min to max range by precision or bit count. Out-of-range values are clamped.
void SerializeFloat(ref float value, float minimum, float maximum, int bits)A full 32-bit float, or a value packed into a min to max range by precision or bit count. Out-of-range values are clamped.
void SerializeFloat01(ref float value, float precision)A 0 to 1 value. With no arguments it uses 7 bits in steps of 1/101; overloads take a precision or bit count.
void SerializeFloat01(ref float value, int bits)A 0 to 1 value. With no arguments it uses 7 bits in steps of 1/101; overloads take a precision or bit count.
void SerializeFloat01(ref float value)A 0 to 1 value. With no arguments it uses 7 bits in steps of 1/101; overloads take a precision or bit count.
void SerializeIndex(ref uint lastIndex, ref uint identifier, int maximumEntities, int start = 1)Writes an increasing id as a delta from the last one, in few bits when ids are close. Used for entity lists.
void SerializeInteger(ref int value)A 32-bit int, or one in a min to max range using just enough bits. Out-of-range values wrap rather than clamp.
void SerializeInteger(ref int value, int minimum, int maximum)A 32-bit int, or one in a min to max range using just enough bits. Out-of-range values wrap rather than clamp.
void SerializeIntegerBits(ref int value, int bits)An int in a given number of bits, with no sign handling.
void Serializelong(ref long value)A 64-bit long. Alta's spelling.
void SerializePlayerIdentifier(ref int playerId)A user id as 32 bits.
void SerializeQuaternion(ref Quaternion value)A rotation as four full floats (128 bits).
void SerializeQuaternionCompressed(ref Quaternion value, int bitsPerPart = 10)A rotation in 2 bits plus three parts of 10 bits by default. What the game uses for synced rotations.
void SerializeSmallUnsignedInt(ref uint value, int startBits, int maxValue)A small number in few bits, growing as needed up to maxValue.
abstract void SerializeString(ref string value, Stream.StringEncoding encoding = 0)A string with a length prefix. ASCII by default, so pass StringEncoding.UTF8 for player text.
void SerializeTimespan(ref TimeSpan value)A TimeSpan as 64-bit ticks.
void SerializeUnsignedInt(ref uint value)A 32-bit uint, or one in a min to max range using just enough bits.
void SerializeUnsignedInt(ref uint value, uint minimum, uint maximum)A 32-bit uint, or one in a min to max range using just enough bits.
void SerializeVector2(ref Vector2 vector)Two floats, full or packed into a range by precision or bits.
void SerializeVector2(ref Vector2 vector, Vector2 min, Vector2 max, float precision)Two floats, full or packed into a range by precision or bits.
void SerializeVector2(ref Vector2 vector, Vector2 min, Vector2 max, int bits)Two floats, full or packed into a range by precision or bits.
void SerializeVector3(ref Vector3 vector)Three floats, full or packed into a range by precision or bits.
void SerializeVector3(ref Vector3 vector, Vector3 min, Vector3 max, float precision)Three floats, full or packed into a range by precision or bits.
void SerializeVector3(ref Vector3 vector, Vector3 min, Vector3 max, int bits)Three floats, full or packed into a range by precision or bits.
void SerializeVector4(ref Vector4 vector)Four full floats.
void SetializeDatetime(ref DateTime value)A DateTime as 64-bit ticks. Alta's spelling.
void SkipBits(uint bitCount)Reads past a number of bits, discarding them.
protected abstract void SerializeBytes(byte[] value, int byteCount)Protected subclass hook that reads or writes raw bytes. Callers use AlignAndSerializeBytes.
1 private and internal method
MemberWhat it is
private void InternalSerializeFloat(ref float value, int bits, uint maximumInteger)Shared body of the SerializeFloat01 overloads: maps a 0 to 1 value onto an integer of the given bit count and back.

Used in​

Members of other types that take, return or hold a Stream:

ActionItem.SyncEffect, ActionItem.SyncOwner, AdditionalStaticContent.Sync, EffectDefinitionApplication.Serialize, PlayerEffectController.NetworkTeleport, ForgedModel<TB, TA>.SyncComplete, ForgedPoint.Sync, Recycler.SyncFull, Recycler.SyncOutputEffect, Recycler.SyncTemperatureValue, RecyclerOutputSlot.Serialize, Smelter.SyncEffects, Smelter.SyncIcons, ChisellingDeck.NetworkUpdateBlock, TimberBlock.Serialize, TimberBlock.SyncPickup, TimberBlock.SyncPieceEffects, TimberBlock.SyncStatus, TimberBlock.Stage.Serialize, TimberBlockSpawner.SyncPosition, TimberBlockSpawner.SyncRotation, TimberPiece.Serialize, TimberPieceBase.Serialize, TimberPieceGroup.Serialize, BiomeSerializer.SerializeBiome, CaveLayerManager.SyncSeed, CaveTeleporter.SyncLayer, CaveTeleporterManager.SyncEffect, CaveTeleporterManager.SyncFull, CaveTeleporterManager.SyncProxy, GenerationBase.ClientSpawn, LevelSelector.Serialize, HandGrip.SerializeGrip, HandGrip.GripFinger.SerializeGrip, HandGrip.GripJoint.SerializeGrip, CaveLayerStreamer.SyncLayer, CaveLayerStreamer.SyncLayerSetup, Chunk.SerializeChunkAndPosition, Chunk.SerializeEntityPosition, Chunk.SerializePosition, Chunk.SyncIndex, IClusterSerialized.SerializeClusterPart, LocationChunk.SerializePosition, PlayerEntitySync.ClientLoad, StaticCaveChunk.SyncSetup, StreamerHelper.SerializeEmpty, StreamerHelper.SerializePrefab, StreamerHelper.WritePositionlessPrefab, Cookable.SyncEffect, StewManager.FadeOutAddedIngredient, StewManager.SerializeGarnishesList, StewManager.SerializeIngredientsList, StewManager.SyncStewCreated, ConfirmationCraftingPart.OnCompleteEffect, ConfirmationCraftingPart.SyncAttemptCraft, ConfirmationCraftingPart.SyncClientNailHit, ConfirmationCraftingPart.SyncNail, ConfirmationCraftingPart.SyncPopout, ConfirmationCraftingPart.SyncSetup, ConfirmationCraftingPart.ClientSideNailHitSync.Serialize and 656 more.