Add Position/Length methods to Stream interface

This commit is contained in:
UnknownShadow200 2018-05-03 21:09:37 +10:00
parent f7210f8f77
commit 00d0203df1
7 changed files with 44 additions and 40 deletions

1
.gitignore vendored
View file

@ -28,6 +28,7 @@ bld/
[sS]rc/Client/texturecache
[sS]rc/Client/logs
[sS]rc/Client/options.txt
[sS]rc/Client/screenshots
# Roslyn cache directories
*.ide/

View file

@ -483,10 +483,8 @@ void Bitmap_Crc32Stream(Stream* stream, Stream* underlying) {
stream->Meta_CRC32_Underlying = underlying;
stream->Meta_CRC32 = 0xFFFFFFFFUL;
stream->Read = Stream_UnsupportedIO;
Stream_SetDefaultOps(stream);
stream->Write = Bitmap_Crc32StreamWrite;
stream->Seek = Stream_UnsupportedSeek;
stream->Close = Stream_UnsupportedClose;
}
void Png_Filter(UInt8 filter, UInt8* cur, UInt8* prior, UInt8* best, Int32 lineLen) {
@ -623,7 +621,8 @@ void Bitmap_EncodePng(Bitmap* bmp, Stream* stream) {
}
zlStream.Close(&zlStream);
}
UInt32 dataEnd = Platform_FilePosition(underlying->Meta_File);
UInt32 dataEnd; ReturnCode result = underlying->Position(underlying, &dataEnd);
ErrorHandler_CheckOrFail(result, "PNG - getting position of data end");
stream = underlying;
Stream_WriteU32_BE(stream, crc32Stream.Meta_CRC32 ^ 0xFFFFFFFFUL);
@ -633,7 +632,7 @@ void Bitmap_EncodePng(Bitmap* bmp, Stream* stream) {
Stream_WriteU32_BE(stream, 0xAE426082UL); /* crc32 of iend */
/* Come back to write size of data chunk */
ReturnCode result = stream->Seek(stream, 33, STREAM_SEEKFROM_BEGIN);
result = stream->Seek(stream, 33, STREAM_SEEKFROM_BEGIN);
ErrorHandler_CheckOrFail(result, "PNG - seeking to write data size");
Stream_WriteU32_BE(stream, dataEnd - 41);
}

View file

@ -685,10 +685,8 @@ void Inflate_MakeStream(Stream* stream, InflateState* state, Stream* underlying)
Stream_SetName(stream, &underlying->Name);
stream->Meta_Inflate = state;
Stream_SetDefaultOps(stream);
stream->Read = Inflate_StreamRead;
stream->Write = Stream_UnsupportedIO;
stream->Close = Stream_UnsupportedClose;
stream->Seek = Stream_UnsupportedSeek;
}
@ -740,9 +738,8 @@ void Deflate_MakeStream(Stream* stream, DeflateState* state, Stream* underlying)
Stream_SetName(stream, &underlying->Name);
stream->Meta_Inflate = state;
stream->Read = Stream_UnsupportedIO;
Stream_SetDefaultOps(stream);
stream->Write = Deflate_StreamWrite;
stream->Seek = Stream_UnsupportedSeek;
stream->Close = Deflate_StreamClose;
}

View file

@ -48,8 +48,8 @@ ReturnCode Platform_FileRead(void* file, UInt8* buffer, UInt32 count, UInt32* by
ReturnCode Platform_FileWrite(void* file, UInt8* buffer, UInt32 count, UInt32* bytesWritten);
ReturnCode Platform_FileClose(void* file);
ReturnCode Platform_FileSeek(void* file, Int32 offset, Int32 seekType);
UInt32 Platform_FilePosition(void* file);
UInt32 Platform_FileLength(void* file);
ReturnCode Platform_FilePosition(void* file, UInt32* position);
ReturnCode Platform_FileLength(void* file, UInt32* length);
void Platform_ThreadSleep(UInt32 milliseconds);
typedef void Platform_ThreadFunc(void);

View file

@ -72,11 +72,21 @@ ReturnCode Stream_Skip(Stream* stream, UInt32 count) {
return count > 0;
}
ReturnCode Stream_UnsupportedIO(Stream* stream, UInt8* data, UInt32 count, UInt32* modified) {
ReturnCode Stream_DefaultIO(Stream* stream, UInt8* data, UInt32 count, UInt32* modified) {
*modified = 0; return 1;
}
ReturnCode Stream_UnsupportedClose(Stream* stream) { return 0; }
ReturnCode Stream_UnsupportedSeek(Stream* stream, Int32 offset, Int32 seekType) { return 1; }
ReturnCode Stream_DefaultClose(Stream* stream) { return 0; }
ReturnCode Stream_DefaultSeek(Stream* stream, Int32 offset, Int32 seekType) { return 1; }
ReturnCode Stream_DefaultGet(Stream* stream, UInt32* value) { *value = 0; return 1; }
void Stream_SetDefaultOps(Stream* stream) {
stream->Read = Stream_DefaultIO;
stream->Write = Stream_DefaultIO;
stream->Close = Stream_DefaultClose;
stream->Seek = Stream_DefaultSeek;
stream->Position = Stream_DefaultGet;
stream->Length = Stream_DefaultGet;
}
/*########################################################################################################################*
@ -96,6 +106,12 @@ ReturnCode Stream_FileClose(Stream* stream) {
ReturnCode Stream_FileSeek(Stream* stream, Int32 offset, Int32 seekType) {
return Platform_FileSeek(stream->Meta_File, offset, seekType);
}
ReturnCode Stream_FilePosition(Stream* stream, UInt32* position) {
return Platform_FilePosition(stream->Meta_File, position);
}
ReturnCode Stream_FileLength(Stream* stream, UInt32* length) {
return Platform_FileLength(stream->Meta_File, length);
}
void Stream_FromFile(Stream* stream, void* file, STRING_PURE String* name) {
Stream_SetName(stream, name);
@ -105,6 +121,8 @@ void Stream_FromFile(Stream* stream, void* file, STRING_PURE String* name) {
stream->Write = Stream_FileWrite;
stream->Close = Stream_FileClose;
stream->Seek = Stream_FileSeek;
stream->Position = Stream_FilePosition;
stream->Length = Stream_FileLength;
}
@ -124,10 +142,8 @@ void Stream_ReadonlyPortion(Stream* stream, Stream* underlying, UInt32 len) {
stream->Meta_Portion_Underlying = underlying;
stream->Meta_Portion_Count = len;
Stream_SetDefaultOps(stream);
stream->Read = Stream_PortionRead;
stream->Write = Stream_UnsupportedIO;
stream->Close = Stream_UnsupportedClose;
stream->Seek = Stream_UnsupportedSeek;
}
@ -159,15 +175,13 @@ void Stream_ReadonlyMemory(Stream* stream, void* data, UInt32 len, STRING_PURE S
stream->Meta_Mem_Buffer = data;
stream->Meta_Mem_Count = len;
Stream_SetDefaultOps(stream);
stream->Read = Stream_MemoryRead;
stream->Write = Stream_UnsupportedIO;
stream->Close = Stream_UnsupportedClose;
stream->Seek = Stream_UnsupportedSeek;
}
void Stream_WriteonlyMemory(Stream* stream, void* data, UInt32 len, STRING_PURE String* name) {
Stream_ReadonlyMemory(stream, data, len, name);
stream->Read = Stream_UnsupportedIO;
stream->Read = Stream_DefaultIO;
stream->Write = Stream_MemoryWrite;
}

View file

@ -18,6 +18,8 @@ typedef struct Stream_ {
ReturnCode (*Write)(Stream* stream, UInt8* data, UInt32 count, UInt32* modified);
ReturnCode (*Close)(Stream* stream);
ReturnCode (*Seek)(Stream* stream, Int32 offset, Int32 seekType);
ReturnCode (*Position)(Stream* stream, UInt32* pos);
ReturnCode (*Length)(Stream* stream, UInt32* length);
union {
void* Meta_File;
@ -34,11 +36,8 @@ void Stream_Read(Stream* stream, UInt8* buffer, UInt32 count);
void Stream_Write(Stream* stream, UInt8* buffer, UInt32 count);
Int32 Stream_TryReadByte(Stream* stream);
void Stream_SetName(Stream* stream, STRING_PURE String* name);
ReturnCode Stream_Skip(Stream* stream, UInt32 count);
ReturnCode Stream_UnsupportedIO(Stream* stream, UInt8* data, UInt32 count, UInt32* modified);
ReturnCode Stream_UnsupportedClose(Stream* stream);
ReturnCode Stream_UnsupportedSeek(Stream* stream, Int32 offset, Int32 seekType);
ReturnCode Stream_Skip(Stream* stream, UInt32 count);
void Stream_SetDefaultOps(Stream* stream);
void Stream_FromFile(Stream* stream, void* file, STRING_PURE String* name);
/* Readonly Stream wrapping another Stream, only allows reading up to 'len' bytes from the wrapped stream. */

View file

@ -288,20 +288,14 @@ ReturnCode Platform_FileSeek(void* file, Int32 offset, Int32 seekType) {
return pos == INVALID_SET_FILE_POINTER ? GetLastError() : 0;
}
UInt32 Platform_FilePosition(void* file) {
DWORD pos = SetFilePointer(file, 0, NULL, 1); /* SEEK_CUR */
if (pos == INVALID_SET_FILE_POINTER) {
ErrorHandler_FailWithCode(GetLastError(), "Getting file position");
}
return pos;
ReturnCode Platform_FilePosition(void* file, UInt32* position) {
*position = SetFilePointer(file, 0, NULL, 1); /* SEEK_CUR */
return *position == INVALID_SET_FILE_POINTER ? GetLastError() : 0;
}
UInt32 Platform_FileLength(void* file) {
DWORD pos = GetFileSize(file, NULL);
if (pos == INVALID_FILE_SIZE) {
ErrorHandler_FailWithCode(GetLastError(), "Getting file length");
}
return pos;
ReturnCode Platform_FileLength(void* file, UInt32* length) {
*length = GetFileSize(file, NULL);
return *length == INVALID_FILE_SIZE ? GetLastError() : 0;
}
@ -405,7 +399,7 @@ void Platform_MakeFont(FontDesc* desc, STRING_PURE String* fontName, UInt16 size
desc->Style = style;
LOGFONTA font = { 0 };
font.lfHeight = -Math_Ceil(size * GetDeviceCaps(hdc, LOGPIXELSY) / 72.0f);
font.lfHeight = -Math_CeilDiv(size * GetDeviceCaps(hdc, LOGPIXELSY), 72);
font.lfUnderline = style == FONT_STYLE_UNDERLINE;
font.lfWeight = style == FONT_STYLE_BOLD ? FW_BOLD : FW_NORMAL;
font.lfQuality = ANTIALIASED_QUALITY; /* TODO: CLEARTYPE_QUALITY looks slightly better */