From 9cf3910db13667043e4161937ef0e7673edfc90a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 19 Jun 2019 21:48:03 +1000 Subject: [PATCH] PacketHandlers -> Protocol and more comments --- src/ClassiCube.vcxproj | 4 ++-- src/ClassiCube.vcxproj.filters | 12 ++++++------ src/{PacketHandlers.c => Protocol.c} | 0 src/{PacketHandlers.h => Protocol.h} | 4 ++-- src/Stream.c | 27 ++++----------------------- src/Stream.h | 5 +---- src/Vorbis.h | 6 ++++++ 7 files changed, 21 insertions(+), 37 deletions(-) rename src/{PacketHandlers.c => Protocol.c} (100%) rename src/{PacketHandlers.h => Protocol.h} (90%) diff --git a/src/ClassiCube.vcxproj b/src/ClassiCube.vcxproj index 03176bdfc..0fb2c2115 100644 --- a/src/ClassiCube.vcxproj +++ b/src/ClassiCube.vcxproj @@ -217,7 +217,7 @@ - + @@ -282,7 +282,7 @@ - + diff --git a/src/ClassiCube.vcxproj.filters b/src/ClassiCube.vcxproj.filters index 02f93edc0..f0c65ad9a 100644 --- a/src/ClassiCube.vcxproj.filters +++ b/src/ClassiCube.vcxproj.filters @@ -252,9 +252,6 @@ Header Files\TexturePack - - Header Files\Network - Header Files\Platform @@ -318,6 +315,9 @@ Header Files\Map + + Header Files\Network + @@ -434,9 +434,6 @@ Source Files\TexturePack - - Source Files\Network - Source Files\Rendering @@ -548,5 +545,8 @@ Source Files\Map + + Source Files\Network + \ No newline at end of file diff --git a/src/PacketHandlers.c b/src/Protocol.c similarity index 100% rename from src/PacketHandlers.c rename to src/Protocol.c diff --git a/src/PacketHandlers.h b/src/Protocol.h similarity index 90% rename from src/PacketHandlers.h rename to src/Protocol.h index cff25011f..b3c4663b0 100644 --- a/src/PacketHandlers.h +++ b/src/Protocol.h @@ -1,5 +1,5 @@ -#ifndef CC_PACKETHANDLERS_H -#define CC_PACKETHANDLERS_H +#ifndef CC_PROTOCOL_H +#define CC_PROTOCOL_H #include "Input.h" #include "String.h" #include "Vectors.h" diff --git a/src/Stream.c b/src/Stream.c index 8490e8839..4d4e84040 100644 --- a/src/Stream.c +++ b/src/Stream.c @@ -230,16 +230,6 @@ static ReturnCode Stream_MemoryReadU8(struct Stream* s, uint8_t* data) { return 0; } -static ReturnCode Stream_MemoryWrite(struct Stream* s, const uint8_t* data, uint32_t count, uint32_t* modified) { - count = min(count, s->Meta.Mem.Left); - Mem_Copy(s->Meta.Mem.Cur, data, count); - - s->Meta.Mem.Cur += count; - s->Meta.Mem.Left -= count; - *modified = count; - return 0; -} - static ReturnCode Stream_MemorySkip(struct Stream* s, uint32_t count) { if (count > s->Meta.Mem.Left) return ERR_INVALID_ARGUMENT; @@ -263,8 +253,10 @@ static ReturnCode Stream_MemoryLength(struct Stream* s, uint32_t* length) { *length = s->Meta.Mem.Length; return 0; } -static void Stream_CommonMemory(struct Stream* s, void* data, uint32_t len) { +void Stream_ReadonlyMemory(struct Stream* s, void* data, uint32_t len) { Stream_Init(s); + s->Read = Stream_MemoryRead; + s->ReadU8 = Stream_MemoryReadU8; s->Skip = Stream_MemorySkip; s->Seek = Stream_MemorySeek; s->Position = Stream_MemoryPosition; @@ -276,17 +268,6 @@ static void Stream_CommonMemory(struct Stream* s, void* data, uint32_t len) { s->Meta.Mem.Base = (uint8_t*)data; } -void Stream_ReadonlyMemory(struct Stream* s, void* data, uint32_t len) { - Stream_CommonMemory(s, data, len); - s->Read = Stream_MemoryRead; - s->ReadU8 = Stream_MemoryReadU8; -} - -void Stream_WriteonlyMemory(struct Stream* s, void* data, uint32_t len) { - Stream_CommonMemory(s, data, len); - s->Write = Stream_MemoryWrite; -} - /*########################################################################################################################* *----------------------------------------------------BufferedStream-------------------------------------------------------* @@ -296,6 +277,7 @@ static ReturnCode Stream_BufferedRead(struct Stream* s, uint8_t* data, uint32_t uint32_t read; ReturnCode res; + /* Refill buffer */ if (!s->Meta.Buffered.Left) { source = s->Meta.Buffered.Source; s->Meta.Buffered.Cur = s->Meta.Buffered.Base; @@ -342,7 +324,6 @@ static ReturnCode Stream_BufferedSeek(struct Stream* s, uint32_t position) { source = s->Meta.Buffered.Source; res = source->Seek(source, position); - if (res) return res; s->Meta.Buffered.Cur = s->Meta.Buffered.Base; diff --git a/src/Stream.h b/src/Stream.h index 6e2cbcaa7..b16ace0e3 100644 --- a/src/Stream.h +++ b/src/Stream.h @@ -12,7 +12,7 @@ struct Stream; struct Stream { /* Attempts to read some bytes from this stream. */ ReturnCode (*Read)(struct Stream* s, uint8_t* data, uint32_t count, uint32_t* modified); - /* Attempts to efficiently read a single byte from this stream. (fallbacks to Read) */ + /* Attempts to efficiently read a single byte from this stream. (falls back to Read) */ ReturnCode (*ReadU8)(struct Stream* s, uint8_t* data); /* Attempts to write some bytes to this stream. */ ReturnCode (*Write)(struct Stream* s, const uint8_t* data, uint32_t count, uint32_t* modified); @@ -31,7 +31,6 @@ struct Stream { union { FileHandle File; void* Inflate; - /* NOTE: These structs rely on overlapping Meta_Mem fields being the same! Don't change them */ struct { uint8_t* Cur; uint32_t Left, Length; uint8_t* Base; } Mem; struct { struct Stream* Source; uint32_t Left, Length; } Portion; struct { uint8_t* Cur; uint32_t Left, Length; uint8_t* Base; struct Stream* Source; uint32_t End; } Buffered; @@ -62,8 +61,6 @@ CC_API void Stream_FromFile(struct Stream* s, FileHandle file); CC_API void Stream_ReadonlyPortion(struct Stream* s, struct Stream* source, uint32_t len); /* Wraps a block of memory, allowing reading from and seeking in the block. */ CC_API void Stream_ReadonlyMemory(struct Stream* s, void* data, uint32_t len); -/* Wraps a block of memory, allowing writing to and seeking in the block. */ -CC_API void Stream_WriteonlyMemory(struct Stream* s, void* data, uint32_t len); /* Wraps another Stream, reading through an intermediary buffer. (Useful for files, since each read call is expensive) */ CC_API void Stream_ReadonlyBuffered(struct Stream* s, struct Stream* source, void* data, uint32_t size); diff --git a/src/Vorbis.h b/src/Vorbis.h index 745ea8a58..a5048a692 100644 --- a/src/Vorbis.h +++ b/src/Vorbis.h @@ -10,6 +10,8 @@ struct Stream; #define VORBIS_MAX_BLOCK_SIZE 8192 #define OGG_BUFFER_SIZE (255 * 256) +/* Wraps an OGG container stream around an existing stream. */ +/* NOTE: buffer must be of size OGG_BUFFER_SIZE at least. */ void Ogg_MakeStream(struct Stream* stream, uint8_t* buffer, struct Stream* source); struct Codebook; struct Floor; struct Residue; struct Mapping; struct Mode; @@ -46,8 +48,12 @@ struct VorbisState { struct imdct_state imdct[2]; }; +/* Frees all dynamic memory allocated to decode the given vorbis audio. */ void Vorbis_Free(struct VorbisState* ctx); +/* Reads and decodes the initial vorbis headers and setup data. */ ReturnCode Vorbis_DecodeHeaders(struct VorbisState* ctx); +/* Reads and decodes the current frame's audio data. */ ReturnCode Vorbis_DecodeFrame(struct VorbisState* ctx); +/* Produces final interleaved audio samples for the current frame. */ int Vorbis_OutputFrame(struct VorbisState* ctx, int16_t* data); #endif