Start porting stream I/O to C

This commit is contained in:
UnknownShadow200 2017-05-17 22:44:03 +10:00
parent ad760bcc96
commit 73b6d9f489
9 changed files with 230 additions and 8 deletions

View file

@ -24,7 +24,7 @@ namespace ClassicalSharp.Map {
nbt.Write(NbtTagType.Compound); nbt.Write("ClassicWorld");
nbt.Write(NbtTagType.Int8);
nbt.Write("FormatVersion"); nbt.WriteInt8(1);
nbt.Write("FormatVersion"); nbt.WriteUInt8(1);
nbt.Write(NbtTagType.Int8Array);
nbt.Write("UUID"); nbt.WriteInt32(16);

View file

@ -47,8 +47,6 @@ namespace ClassicalSharp.Map {
public void WriteInt16(short v) { writer.Write(IPAddress.HostToNetworkOrder(v)); }
public void WriteInt8(sbyte v) { writer.Write((byte)v); }
public void WriteUInt8(int v) { writer.Write((byte)v); }
public void WriteUInt8(byte v) { writer.Write(v); }

View file

@ -179,6 +179,7 @@
<ClInclude Include="Drawer.h" />
<ClInclude Include="ErrorHandler.h" />
<ClInclude Include="FrustumCulling.h" />
<ClInclude Include="Stream.h" />
<ClInclude Include="TerrainAtlas1D.h" />
<ClInclude Include="TerrainAtlas2D.h" />
<ClInclude Include="WorldEvents.h" />
@ -222,6 +223,7 @@
<ClCompile Include="Noise.c" />
<ClCompile Include="NotchyGenerator.c" />
<ClCompile Include="Random.c" />
<ClCompile Include="Stream.c" />
<ClCompile Include="String.c" />
<ClCompile Include="TerrainAtlas1D.c" />
<ClCompile Include="TerrainAtlas2D.c" />

View file

@ -94,6 +94,12 @@
<Filter Include="Header Files\Rendering">
<UniqueIdentifier>{2134e4ed-c57e-40b2-b329-5f5b7b71a799}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\IO">
<UniqueIdentifier>{759424b7-9977-45d7-9946-b6cc9875231b}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\IO">
<UniqueIdentifier>{82a3fbdf-5459-4e08-bff9-04dd23aa4342}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="NotchyGenerator.h">
@ -201,6 +207,9 @@
<ClInclude Include="FrustumCulling.h">
<Filter>Header Files\Rendering</Filter>
</ClInclude>
<ClInclude Include="Stream.h">
<Filter>Header Files\IO</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="NotchyGenerator.c">
@ -287,5 +296,8 @@
<ClCompile Include="FrustumCulling.c">
<Filter>Source Files\Rendering</Filter>
</ClCompile>
<ClCompile Include="Stream.c">
<Filter>Source Files\IO</Filter>
</ClCompile>
</ItemGroup>
</Project>

104
src/Client/Stream.c Normal file
View file

@ -0,0 +1,104 @@
#include "Stream.h"
/*void Stream_Read(Stream* stream, UInt8* buffer, UInt32 count) {
UInt32 read;
ReturnCode result = 0;
while (count > 0) {
ReturnCode result = stream->Read(buffer, count, &read);
if (read == 0 || !ErrorHandler_Check(result)) {
error here or something
}
count -= read;
}
}
void Stream_Write(Stream* stream, UInt8* buffer, UInt32 count) {
UInt32 totalWritten = 0;
while (count > 0) {
}
}*/
// TODO: This one is called frequently, probably needs inline.
UInt8 Stream_ReadUInt8(Stream* stream) {
UInt8 result;
Stream_Read(stream, &result, sizeof(UInt8));
return result;
}
UInt16 Stream_ReadUInt16_LE(Stream* stream) {
UInt8 buffer[sizeof(UInt16)];
Stream_Read(stream, buffer, sizeof(UInt16));
return (UInt16)(buffer[0] | (buffer[1] << 8));
}
UInt16 Stream_ReadUInt16_BE(Stream* stream) {
UInt8 buffer[sizeof(UInt16)];
Stream_Read(stream, buffer, sizeof(UInt16));
return (UInt16)((buffer[0] << 8) | buffer[1]);
}
UInt32 Stream_ReadUInt32_LE(Stream* stream) {
UInt8 buffer[sizeof(UInt32)];
Stream_Read(stream, buffer, sizeof(UInt32));
return (UInt32)(
(UInt32)buffer[0] | ((UInt32)buffer[1] << 8) |
((UInt32)buffer[2] << 16) | ((UInt32)buffer[3] << 24));
}
UInt32 Stream_ReadUInt32_BE(Stream* stream) {
UInt8 buffer[sizeof(UInt32)];
Stream_Read(stream, buffer, sizeof(UInt32));
return (UInt32)(
((UInt32)buffer[0] << 24) | ((UInt32)buffer[1] << 16) |
((UInt32)buffer[2] << 8) | (UInt32)buffer[3]);
}
UInt64 Stream_ReadUInt64_BE(Stream* stream) {
UInt8 buffer[sizeof(UInt64)];
Stream_Read(stream, buffer, sizeof(UInt64));
UInt32 hi = (UInt32)(
((UInt32)buffer[0] << 24) | ((UInt32)buffer[1] << 16) |
((UInt32)buffer[2] << 8) | (UInt32)buffer[3]);
UInt32 lo = (UInt32)(
((UInt32)buffer[4] << 24) | ((UInt32)buffer[5] << 16) |
((UInt32)buffer[6] << 8) | (UInt32)buffer[7]);
return (UInt64)(((UInt64)hi) << 32) | ((UInt64)lo);
}
void Stream_WriteUInt16_LE(Stream* stream, UInt16 value) {
UInt8 buffer[sizeof(UInt16)];
buffer[0] = (UInt8)(value ); buffer[1] = (UInt8)(value >> 8 );
Stream_Write(stream, buffer, sizeof(UInt16));
}
void Stream_WriteUInt16_BE(Stream* stream, UInt16 value) {
UInt8 buffer[sizeof(UInt16)];
buffer[0] = (UInt8)(value >> 8 ); buffer[1] = (UInt8)(value );
Stream_Write(stream, buffer, sizeof(UInt16));
}
void Stream_WriteUInt32_LE(Stream* stream, UInt32 value) {
UInt8 buffer[sizeof(UInt32)];
buffer[0] = (UInt8)(value ); buffer[1] = (UInt8)(value >> 8 );
buffer[2] = (UInt8)(value >> 16); buffer[3] = (UInt8)(value >> 24);
Stream_Write(stream, buffer, sizeof(UInt32));
}
void Stream_WriteUInt32_BE(Stream* stream, UInt32 value) {
UInt8 buffer[sizeof(UInt32)];
buffer[0] = (UInt8)(value >> 24); buffer[1] = (UInt8)(value >> 16);
buffer[2] = (UInt8)(value >> 8 ); buffer[3] = (UInt8)(value);
Stream_Write(stream, buffer, sizeof(UInt32));
}
void Stream_WriteUInt64_BE(Stream* stream, UInt64 value) {
UInt8 buffer[sizeof(UInt64)];
buffer[0] = (UInt8)(value >> 56); buffer[1] = (UInt8)(value >> 48);
buffer[2] = (UInt8)(value >> 40); buffer[3] = (UInt8)(value >> 32);
buffer[4] = (UInt8)(value >> 24); buffer[5] = (UInt8)(value >> 16);
buffer[6] = (UInt8)(value >> 8 ); buffer[7] = (UInt8)(value );
Stream_Write(stream, buffer, sizeof(UInt64));
}

102
src/Client/Stream.h Normal file
View file

@ -0,0 +1,102 @@
#ifndef CS_STREAM_H
#define CS_STREAM_H
#include "Typedefs.h"
#include "String.h"
#include "ErrorHandler.h"
typedef ReturnCode (*Stream_Operation)(UInt8* data, UInt32 count, UInt32* modified);
typedef ReturnCode (*Stream_Seek)(Int32 offset);
typedef struct Stream {
/* The name of the stream. */
String Name;
/* Performs a read operation on the stream.
Result is a ReturnCode, number of read bytes is output via pointer. */
Stream_Operation Read;
/* Performs a write operation on the stream.
Result is a ReturnCode, number of written bytes is output via pointer. */
Stream_Operation Write;
/* Moves backwards or forwards by given number of bytes in the stream.
Result is a ReturnCode. */
Stream_Operation Seek;
} Stream;
/* === block stream i/o operations */
/* Fully reads up to count bytes or fails. */
void Stream_Read(Stream* stream, UInt8* buffer, UInt32 count);
/* Fully writes up to count bytes or fails. */
void Stream_Write(Stream* stream, UInt8* buffer, UInt32 count);
/* === integer read operations === */
/* Reads an unsigned 8 bit integer from the given stream. */
UInt8 Stream_ReadUInt8(Stream* stream);
/* Reads a little endian unsigned 16 bit integer from the given stream. */
UInt16 Stream_ReadUInt16_LE(Stream* stream);
/* Reads a little endian signed 16 bit integer from the given stream. */
#define Stream_ReadInt16_LE(stream) (Int32)Stream_ReadUInt16_LE(stream)
/* Reads a big endian unsigned 16 bit integer from the given stream. */
UInt16 Stream_ReadUInt16_BE(Stream* stream);
/* Reads a big endian signed 16 bit integer from the given stream. */
#define Stream_ReadInt16_BE(stream) (Int32)Stream_ReadUInt16_BE(stream)
/* Reads a little endian unsigned 32 bit integer from the given stream. */
UInt32 Stream_ReadUInt32_LE(Stream* stream);
/* Reads a little endian signed 32 bit integer from the given stream. */
#define Stream_ReadInt32_LE(stream) (Int32)Stream_ReadUInt32_LE(stream)
/* Reads a big endian unsigned 64 bit integer from the given stream. */
UInt32 Stream_ReadUInt32_BE(Stream* stream);
/* Reads a big endian signed 64 bit integer from the given stream. */
#define Stream_ReadInt32_BE(stream) (Int32)Stream_ReadUInt32_BE(stream)
/* Reads a big endian unsigned 64 bit integer from the given stream. */
UInt64 Stream_ReadUInt64_BE(Stream* stream);
/* Reads a big endian signed 64 bit integer from the given stream. */
#define Stream_ReadInt64_BE(stream) (Int64)Stream_ReadUInt64_BE(stream)
/* === integer write operations === */
/* Writes an unsigned 8 bit integer from the given stream. */
#define Stream_WriteUInt8(stream, value) Stream_Write(stream, &value, sizeof(UInt8));
/* Writes a little endian unsigned 16 bit integer from the given stream. */
void Stream_WriteUInt16_LE(Stream* stream, UInt16 value);
/* Writes a little endian signed 16 bit integer from the given stream. */
#define Stream_WriteInt16_LE(stream, value) Stream_WriteUInt16_LE(stream, (UInt16)value)
/* Writes a big endian unsigned 16 bit integer from the given stream. */
void Stream_WriteUInt16_BE(Stream* stream, UInt16 value);
/* Writes a big endian signed 16 bit integer from the given stream. */
#define Stream_WriteInt16_BE(stream, value) Stream_WriteUInt16_BE(stream, (UInt16)value)
/* Writes a little endian unsigned 32 bit integer from the given stream. */
void Stream_WriteUInt32_LE(Stream* stream, UInt32 value);
/* Writes a little endian signed 32 bit integer from the given stream. */
#define Stream_WriteInt32_LE(stream, value) Stream_WriteUInt32_LE(stream, (UInt32)value)
/* Writes a big endian unsigned 64 bit integer from the given stream. */
void Stream_WriteUInt32_BE(Stream* stream, UInt32 value);
/* Writes a big endian signed 64 bit integer from the given stream. */
#define Stream_WriteInt32_BE(stream, value) Stream_WriteUInt32_BE(stream, (UInt32)value)
/* Writes a big endian unsigned 64 bit integer from the given stream. */
void Stream_WriteUInt64_BE(Stream* stream, UInt64 value);
/* Writes a big endian signed 64 bit integer from the given stream. */
#define Stream_WriteInt64_BE(stream, value) Stream_WriteUInt64_BE(stream, (UInt64)value)
#endif

View file

@ -26,7 +26,7 @@ typedef int16_t Int16;
typedef int32_t Int32;
typedef int64_t Int64;
#else
#error "I didn't add typedefs for this compiler. You'll need to define them in Typedefs.h!'"
#error "I didn't add typedefs for this compiler. You'll need to define them in Typedefs.h!"
#endif
typedef float Real32;

View file

@ -19,6 +19,7 @@ void World_SetNewMap(BlockID* blocks, Int32 blocksSize, Int32 width, Int32 heigh
if (blocksSize != (width * height * length)) {
ErrorHandler_Fail("Blocks array size does not match volume of map");
}
World_OneY = width * length;
if (WorldEnv_EdgeHeight == -1) {
WorldEnv_EdgeHeight = height / 2;

View file

@ -22,16 +22,19 @@ y = (index / World_Width) / World_Length;
BlockID* World_Blocks = NULL;
/* Size of blocks array. */
Int32 World_BlocksSize = 0;
Int32 World_BlocksSize;
/* Length of world on X axis.*/
Int32 World_Width = 0;
Int32 World_Width;
/* Length of world on Y axis (vertical).*/
Int32 World_Height = 0;
Int32 World_Height;
/* Length of world on Z axis.*/
Int32 World_Length = 0;
Int32 World_Length;
/* Amount a packed index must be changed by to advance Y coordinate. */
Int32 World_OneY;
/* Unique uuid/guid of this particular world. */
UInt8 World_Uuid[16];