From 4e41f79a50b18b4e9b9fa00bbfa68ecb15fcb63e Mon Sep 17 00:00:00 2001 From: fusion32 Date: Thu, 22 May 2025 19:03:45 -0300 Subject: implement TWriteStream, T(Dynamic)WriteBuffer, and TWriteBinaryFile --- TODO.md | 4 +- reference/game.c | 807 ----------------------------------------------------- reference/types.hh | 24 -- src/common.hh | 50 ++++ src/script.cc | 127 ++++++++- src/script.hh | 36 ++- src/util.cc | 174 +++++++++++- 7 files changed, 370 insertions(+), 852 deletions(-) diff --git a/TODO.md b/TODO.md index 42dd643..992d0a0 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,5 @@ ## TODO NEXT -- TWriteStream -- TWriteBuffer -- TWriteBinaryFile +- SaveFile ?? - TCreature - TPlayer - TNonPlayer diff --git a/reference/game.c b/reference/game.c index 4692709..1ce2aba 100644 --- a/reference/game.c +++ b/reference/game.c @@ -2057,499 +2057,6 @@ void print(void) return; } -// DWARF original prototype: void writeFlag(TWriteStream * this, bool f) - -void __thiscall TWriteStream::writeFlag(TWriteStream *this,bool f) - -{ - // try { // try from 0804d47b to 0804d47d has its CatchHandler @ 0804d482 - (*this->_vptr_TWriteStream[1])(this,(uint)f); - return; -} - - - -// DWARF original prototype: void writeWord(TWriteStream * this, ushort w) - -void __thiscall TWriteStream::writeWord(TWriteStream *this,ushort w) - -{ - // try { // try from 0804d4c1 to 0804d4d7 has its CatchHandler @ 0804d4e2 - (*this->_vptr_TWriteStream[1])(this,w & 0xff); - (*this->_vptr_TWriteStream[1])(this,w >> 8); - return; -} - - - -// DWARF original prototype: void writeQuad(TWriteStream * this, ulong q) - -void __thiscall TWriteStream::writeQuad(TWriteStream *this,ulong q) - -{ - // try { // try from 0804d51e to 0804d553 has its CatchHandler @ 0804d560 - (*this->_vptr_TWriteStream[1])(this,q & 0xff); - (*this->_vptr_TWriteStream[1])(this,q >> 8 & 0xff); - (*this->_vptr_TWriteStream[1])(this,q >> 0x10 & 0xff); - (*this->_vptr_TWriteStream[1])(this,q >> 0x18); - return; -} - - - -// DWARF original prototype: void writeString(TWriteStream * this, char * s) - -void __thiscall TWriteStream::writeString(TWriteStream *this,char *s) - -{ - size_t sVar1; - - if (s == (char *)0x0) { - (*this->_vptr_TWriteStream[2])(this,0); - } - else { - sVar1 = strlen(s); - if ((int)sVar1 < 0xffff) { - (*this->_vptr_TWriteStream[2])(this,sVar1 & 0xffff); - } - else { - // try { // try from 0804d5b7 to 0804d610 has its CatchHandler @ 0804d613 - (*this->_vptr_TWriteStream[2])(this,0xffff); - (*this->_vptr_TWriteStream[3])(this,sVar1); - } - if (sVar1 != 0) { - (*this->_vptr_TWriteStream[5])(this,s,sVar1); - } - } - return; -} - - - -// DWARF original prototype: void writeBytes(TWriteStream * this, uchar * Buffer, int Count) - -void __thiscall TWriteStream::writeBytes(TWriteStream *this,uchar *Buffer,int Count) - -{ - undefined4 *puVar1; - int i; - int iVar2; - - if (Buffer == (uchar *)0x0) { - error(&DAT_080eec20); - puVar1 = (undefined4 *)__cxa_allocate_exception(4); - *puVar1 = "internal error"; - // WARNING: Subroutine does not return - __cxa_throw(puVar1,char_const*::typeinfo,0); - } - iVar2 = 0; - if (0 < Count) { - do { - // try { // try from 0804d673 to 0804d6b2 has its CatchHandler @ 0804d6b3 - (*this->_vptr_TWriteStream[1])(this,(uint)Buffer[iVar2]); - iVar2 = iVar2 + 1; - } while (iVar2 < Count); - } - return; -} - -// DWARF original prototype: void TWriteBuffer(TWriteBuffer * this, uchar * data, int size) - -void __thiscall TWriteBuffer::TWriteBuffer(TWriteBuffer *this,uchar *data,int size) - -{ - (this->super_TWriteStream)._vptr_TWriteStream = (_func_int_varargs **)&PTR_writeFlag_081267a8; - this->Data = data; - this->Size = size; - this->Position = 0; - if (data == (uchar *)0x0) { - error("TWriteBuffer::TWriteBuffer: data ist NULL.\n"); - this->Size = 0; - } - else if (size < 0) { - error(&DAT_080eed20); - return; - } - return; -} - - - -// DWARF original prototype: void TWriteBuffer(TWriteBuffer * this, uchar * data, int size) - -void __thiscall TWriteBuffer::TWriteBuffer(TWriteBuffer *this,uchar *data,int size) - -{ - (this->super_TWriteStream)._vptr_TWriteStream = (_func_int_varargs **)&PTR_writeFlag_081267a8; - this->Data = data; - this->Size = size; - this->Position = 0; - if (data == (uchar *)0x0) { - error("TWriteBuffer::TWriteBuffer: data ist NULL.\n"); - this->Size = 0; - } - else if (size < 0) { - error(&DAT_080eed20); - return; - } - return; -} - - - -// DWARF original prototype: void writeByte(TWriteBuffer * this, uchar b) - -void __thiscall TWriteBuffer::writeByte(TWriteBuffer *this,uchar b) - -{ - undefined4 *puVar1; - - if (this->Position <= this->Size + -1) { - this->Data[this->Position] = b; - this->Position = this->Position + 1; - return; - } - puVar1 = (undefined4 *)__cxa_allocate_exception(4); - *puVar1 = "buffer full"; - // WARNING: Subroutine does not return - // try { // try from 0804db5f to 0804db63 has its CatchHandler @ 0804db64 - __cxa_throw(puVar1,char_const*::typeinfo,0); -} - - - -// DWARF original prototype: void writeWord(TWriteBuffer * this, ushort w) - -void __thiscall TWriteBuffer::writeWord(TWriteBuffer *this,ushort w) - -{ - undefined4 *puVar1; - - if (this->Position <= this->Size + -2) { - this->Data[this->Position] = (uchar)w; - this->Data[this->Position + 1] = (uchar)(w >> 8); - this->Position = this->Position + 2; - return; - } - puVar1 = (undefined4 *)__cxa_allocate_exception(4); - *puVar1 = "buffer full"; - // WARNING: Subroutine does not return - // try { // try from 0804dbe0 to 0804dbe4 has its CatchHandler @ 0804dbe5 - __cxa_throw(puVar1,char_const*::typeinfo,0); -} - - - -// DWARF original prototype: void writeQuad(TWriteBuffer * this, ulong q) - -void __thiscall TWriteBuffer::writeQuad(TWriteBuffer *this,ulong q) - -{ - undefined4 *puVar1; - - if (this->Position <= this->Size + -4) { - this->Data[this->Position] = (uchar)q; - this->Data[this->Position + 1] = (uchar)(q >> 8); - this->Data[this->Position + 2] = (uchar)(q >> 0x10); - this->Data[this->Position + 3] = (uchar)(q >> 0x18); - this->Position = this->Position + 4; - return; - } - puVar1 = (undefined4 *)__cxa_allocate_exception(4); - *puVar1 = "buffer full"; - // WARNING: Subroutine does not return - // try { // try from 0804dc79 to 0804dc7d has its CatchHandler @ 0804dc7e - __cxa_throw(puVar1,char_const*::typeinfo,0); -} - - - -// DWARF original prototype: void writeBytes(TWriteBuffer * this, uchar * Buffer, int Count) - -void __thiscall TWriteBuffer::writeBytes(TWriteBuffer *this,uchar *Buffer,int Count) - -{ - undefined4 *puVar1; - - if (this->Position <= this->Size - Count) { - memcpy(this->Data + this->Position,Buffer,Count); - this->Position = this->Position + Count; - return; - } - puVar1 = (undefined4 *)__cxa_allocate_exception(4); - *puVar1 = "buffer full"; - // WARNING: Subroutine does not return - // try { // try from 0804dd08 to 0804dd0c has its CatchHandler @ 0804dd0d - __cxa_throw(puVar1,char_const*::typeinfo,0); -} - - - -// DWARF original prototype: void TDynamicWriteBuffer(TDynamicWriteBuffer * this, int InitialSize) - -void __thiscall TDynamicWriteBuffer::TDynamicWriteBuffer(TDynamicWriteBuffer *this,int InitialSize) - -{ - uchar *data; - - data = (uchar *)operator_new__(InitialSize); - TWriteBuffer::TWriteBuffer(&this->super_TWriteBuffer,data,InitialSize); - (this->super_TWriteBuffer).super_TWriteStream._vptr_TWriteStream = - (_func_int_varargs **)&PTR_writeFlag_08126768; - return; -} - - - -// DWARF original prototype: void TDynamicWriteBuffer(TDynamicWriteBuffer * this, int InitialSize) - -void __thiscall TDynamicWriteBuffer::TDynamicWriteBuffer(TDynamicWriteBuffer *this,int InitialSize) - -{ - uchar *data; - - data = (uchar *)operator_new__(InitialSize); - TWriteBuffer::TWriteBuffer(&this->super_TWriteBuffer,data,InitialSize); - (this->super_TWriteBuffer).super_TWriteStream._vptr_TWriteStream = - (_func_int_varargs **)&PTR_writeFlag_08126768; - return; -} - - - -// DWARF original prototype: void ~TDynamicWriteBuffer(TDynamicWriteBuffer * this, int __in_chrg) - -void __thiscall TDynamicWriteBuffer::~TDynamicWriteBuffer(TDynamicWriteBuffer *this,int __in_chrg) - -{ - uchar *puVar1; - - (this->super_TWriteBuffer).super_TWriteStream._vptr_TWriteStream = - (_func_int_varargs **)&PTR_writeFlag_08126768; - puVar1 = (this->super_TWriteBuffer).Data; - if (puVar1 != (uchar *)0x0) { - operator_delete__(puVar1); - } - return; -} - - - -// DWARF original prototype: void ~TDynamicWriteBuffer(TDynamicWriteBuffer * this, int __in_chrg) - -void __thiscall TDynamicWriteBuffer::~TDynamicWriteBuffer(TDynamicWriteBuffer *this,int __in_chrg) - -{ - uchar *puVar1; - - (this->super_TWriteBuffer).super_TWriteStream._vptr_TWriteStream = - (_func_int_varargs **)&PTR_writeFlag_08126768; - puVar1 = (this->super_TWriteBuffer).Data; - if (puVar1 != (uchar *)0x0) { - operator_delete__(puVar1); - } - return; -} - - - -// DWARF original prototype: void ~TDynamicWriteBuffer(TDynamicWriteBuffer * this, int __in_chrg) - -void __thiscall TDynamicWriteBuffer::~TDynamicWriteBuffer(TDynamicWriteBuffer *this,int __in_chrg) - -{ - uchar *puVar1; - - puVar1 = (this->super_TWriteBuffer).Data; - (this->super_TWriteBuffer).super_TWriteStream._vptr_TWriteStream = - (_func_int_varargs **)&PTR_writeFlag_08126768; - if (puVar1 != (uchar *)0x0) { - operator_delete__(puVar1); - } - operator_delete(this); - return; -} - - - -// DWARF original prototype: void resizeBuffer(TDynamicWriteBuffer * this) - -void __thiscall TDynamicWriteBuffer::resizeBuffer(TDynamicWriteBuffer *this) - -{ - int *piVar1; - uchar *puVar2; - uchar *__dest; - uchar *Help; - - __dest = (uchar *)operator_new__((this->super_TWriteBuffer).Size * 2); - memcpy(__dest,(this->super_TWriteBuffer).Data,(this->super_TWriteBuffer).Size); - puVar2 = (this->super_TWriteBuffer).Data; - if (puVar2 != (uchar *)0x0) { - operator_delete__(puVar2); - } - piVar1 = &(this->super_TWriteBuffer).Size; - *piVar1 = *piVar1 << 1; - (this->super_TWriteBuffer).Data = __dest; - return; -} - - - -// WARNING: Variable defined which should be unmapped: b_local -// DWARF original prototype: void writeByte(TDynamicWriteBuffer * this, uchar b) - -void __thiscall TDynamicWriteBuffer::writeByte(TDynamicWriteBuffer *this,uchar b) - -{ - int *piVar1; - uchar *puVar2; - uchar *__dest; - int iVar3; - int iVar4; - uchar *Help; - uchar b_local; - - iVar3 = (this->super_TWriteBuffer).Size; - iVar4 = (this->super_TWriteBuffer).Position; - if (iVar3 + -1 < iVar4) { - do { - // try { // try from 0804dee6 to 0804deea has its CatchHandler @ 0804df30 - __dest = (uchar *)operator_new__(iVar3 * 2); - memcpy(__dest,(this->super_TWriteBuffer).Data,(this->super_TWriteBuffer).Size); - puVar2 = (this->super_TWriteBuffer).Data; - if (puVar2 != (uchar *)0x0) { - operator_delete__(puVar2); - } - (this->super_TWriteBuffer).Data = __dest; - iVar4 = (this->super_TWriteBuffer).Position; - iVar3 = (this->super_TWriteBuffer).Size * 2; - (this->super_TWriteBuffer).Size = iVar3; - } while (iVar3 + -1 < iVar4); - } - (this->super_TWriteBuffer).Data[iVar4] = b; - piVar1 = &(this->super_TWriteBuffer).Position; - *piVar1 = *piVar1 + 1; - return; -} - - - -// DWARF original prototype: void writeWord(TDynamicWriteBuffer * this, ushort w) - -void __thiscall TDynamicWriteBuffer::writeWord(TDynamicWriteBuffer *this,ushort w) - -{ - int *piVar1; - uchar *puVar2; - uchar *__dest; - int iVar3; - int iVar4; - uchar *Help; - - iVar3 = (this->super_TWriteBuffer).Size; - iVar4 = (this->super_TWriteBuffer).Position; - if (iVar3 + -2 < iVar4) { - do { - // try { // try from 0804dfa6 to 0804dfaa has its CatchHandler @ 0804dff0 - __dest = (uchar *)operator_new__(iVar3 * 2); - memcpy(__dest,(this->super_TWriteBuffer).Data,(this->super_TWriteBuffer).Size); - puVar2 = (this->super_TWriteBuffer).Data; - if (puVar2 != (uchar *)0x0) { - operator_delete__(puVar2); - } - (this->super_TWriteBuffer).Data = __dest; - iVar4 = (this->super_TWriteBuffer).Position; - iVar3 = (this->super_TWriteBuffer).Size * 2; - (this->super_TWriteBuffer).Size = iVar3; - } while (iVar3 + -2 < iVar4); - } - (this->super_TWriteBuffer).Data[iVar4] = (uchar)w; - (this->super_TWriteBuffer).Data[(this->super_TWriteBuffer).Position + 1] = (uchar)(w >> 8); - piVar1 = &(this->super_TWriteBuffer).Position; - *piVar1 = *piVar1 + 2; - return; -} - - - -// DWARF original prototype: void writeQuad(TDynamicWriteBuffer * this, ulong q) - -void __thiscall TDynamicWriteBuffer::writeQuad(TDynamicWriteBuffer *this,ulong q) - -{ - int *piVar1; - uchar *puVar2; - uchar *__dest; - int iVar3; - int iVar4; - uchar *Help; - - iVar3 = (this->super_TWriteBuffer).Size; - iVar4 = (this->super_TWriteBuffer).Position; - if (iVar3 + -4 < iVar4) { - do { - // try { // try from 0804e076 to 0804e07a has its CatchHandler @ 0804e0c0 - __dest = (uchar *)operator_new__(iVar3 * 2); - memcpy(__dest,(this->super_TWriteBuffer).Data,(this->super_TWriteBuffer).Size); - puVar2 = (this->super_TWriteBuffer).Data; - if (puVar2 != (uchar *)0x0) { - operator_delete__(puVar2); - } - (this->super_TWriteBuffer).Data = __dest; - iVar4 = (this->super_TWriteBuffer).Position; - iVar3 = (this->super_TWriteBuffer).Size * 2; - (this->super_TWriteBuffer).Size = iVar3; - } while (iVar3 + -4 < iVar4); - } - (this->super_TWriteBuffer).Data[iVar4] = (uchar)q; - (this->super_TWriteBuffer).Data[(this->super_TWriteBuffer).Position + 1] = (uchar)(q >> 8); - (this->super_TWriteBuffer).Data[(this->super_TWriteBuffer).Position + 2] = (uchar)(q >> 0x10); - (this->super_TWriteBuffer).Data[(this->super_TWriteBuffer).Position + 3] = (uchar)(q >> 0x18); - piVar1 = &(this->super_TWriteBuffer).Position; - *piVar1 = *piVar1 + 4; - return; -} - - - -// DWARF original prototype: void writeBytes(TDynamicWriteBuffer * this, uchar * Buffer, int Count) - -void __thiscall TDynamicWriteBuffer::writeBytes(TDynamicWriteBuffer *this,uchar *Buffer,int Count) - -{ - int *piVar1; - uchar *puVar2; - uchar *__dest; - int iVar3; - int iVar4; - uchar *Help; - - iVar3 = (this->super_TWriteBuffer).Size; - iVar4 = (this->super_TWriteBuffer).Position; - if (iVar3 - Count < iVar4) { - do { - // try { // try from 0804e136 to 0804e13a has its CatchHandler @ 0804e180 - __dest = (uchar *)operator_new__(iVar3 * 2); - memcpy(__dest,(this->super_TWriteBuffer).Data,(this->super_TWriteBuffer).Size); - puVar2 = (this->super_TWriteBuffer).Data; - if (puVar2 != (uchar *)0x0) { - operator_delete__(puVar2); - } - (this->super_TWriteBuffer).Data = __dest; - iVar4 = (this->super_TWriteBuffer).Position; - iVar3 = (this->super_TWriteBuffer).Size * 2; - (this->super_TWriteBuffer).Size = iVar3; - } while (iVar3 - Count < iVar4); - } - memcpy((this->super_TWriteBuffer).Data + iVar4,Buffer,Count); - piVar1 = &(this->super_TWriteBuffer).Position; - *piVar1 = *piVar1 + Count; - return; -} - - - int val(char c) { @@ -3146,8 +2653,6 @@ void UnpackRelativeCoordinate(void) return; } - - void SaveFile(char *Filename) { @@ -3272,318 +2777,6 @@ LAB_0804eb1e: return; } -// DWARF original prototype: void TWriteBinaryFile(TWriteBinaryFile * this) - -void __thiscall TWriteBinaryFile::TWriteBinaryFile(TWriteBinaryFile *this) - -{ - (this->super_TWriteStream)._vptr_TWriteStream = (_func_int_varargs **)&PTR_writeFlag_08126888; - this->File = (FILE *)0x0; - return; -} - - - -// DWARF original prototype: void TWriteBinaryFile(TWriteBinaryFile * this) - -void __thiscall TWriteBinaryFile::TWriteBinaryFile(TWriteBinaryFile *this) - -{ - (this->super_TWriteStream)._vptr_TWriteStream = (_func_int_varargs **)&PTR_writeFlag_08126888; - this->File = (FILE *)0x0; - return; -} - - - -// DWARF original prototype: void ~TWriteBinaryFile(TWriteBinaryFile * this, int __in_chrg) - -void __thiscall TWriteBinaryFile::~TWriteBinaryFile(TWriteBinaryFile *this,int __in_chrg) - -{ - FILE *pFVar1; - char *pcVar2; - int iVar3; - int *piVar4; - - pFVar1 = this->File; - (this->super_TWriteStream)._vptr_TWriteStream = (_func_int_varargs **)&PTR_writeFlag_08126888; - if (pFVar1 != (FILE *)0x0) { - pcVar2 = ""; - if (this != (TWriteBinaryFile *)0xfffffff8) { - pcVar2 = this->Filename; - } - ::error("TWriteBinaryFile::~TWriteBinaryFile: Datei %s ist noch offen.\n",pcVar2); - iVar3 = fclose((FILE *)this->File); - if (iVar3 != 0) { - piVar4 = __errno_location(); - ::error(&DAT_080eff80,*piVar4); - } - } - return; -} - - - -// DWARF original prototype: void ~TWriteBinaryFile(TWriteBinaryFile * this, int __in_chrg) - -void __thiscall TWriteBinaryFile::~TWriteBinaryFile(TWriteBinaryFile *this,int __in_chrg) - -{ - FILE *pFVar1; - char *pcVar2; - int iVar3; - int *piVar4; - - pFVar1 = this->File; - (this->super_TWriteStream)._vptr_TWriteStream = (_func_int_varargs **)&PTR_writeFlag_08126888; - if (pFVar1 != (FILE *)0x0) { - pcVar2 = ""; - if (this != (TWriteBinaryFile *)0xfffffff8) { - pcVar2 = this->Filename; - } - ::error("TWriteBinaryFile::~TWriteBinaryFile: Datei %s ist noch offen.\n",pcVar2); - iVar3 = fclose((FILE *)this->File); - if (iVar3 != 0) { - piVar4 = __errno_location(); - ::error(&DAT_080eff80,*piVar4); - } - } - return; -} - - - -// DWARF original prototype: void ~TWriteBinaryFile(TWriteBinaryFile * this, int __in_chrg) - -void __thiscall TWriteBinaryFile::~TWriteBinaryFile(TWriteBinaryFile *this,int __in_chrg) - -{ - FILE *pFVar1; - char *pcVar2; - int iVar3; - int *piVar4; - - pFVar1 = this->File; - (this->super_TWriteStream)._vptr_TWriteStream = (_func_int_varargs **)&PTR_writeFlag_08126888; - if (pFVar1 != (FILE *)0x0) { - pcVar2 = ""; - if (this != (TWriteBinaryFile *)0xfffffff8) { - pcVar2 = this->Filename; - } - ::error("TWriteBinaryFile::~TWriteBinaryFile: Datei %s ist noch offen.\n",pcVar2); - iVar3 = fclose((FILE *)this->File); - if (iVar3 != 0) { - piVar4 = __errno_location(); - ::error(&DAT_080eff80,*piVar4); - } - } - operator_delete(this); - return; -} - - - -// DWARF original prototype: void open(TWriteBinaryFile * this, char * FileName) - -int __thiscall TWriteBinaryFile::open(TWriteBinaryFile *this,char *__file,int __oflag,...) - -{ - FILE *pFVar1; - char *pcVar2; - int *piVar3; - undefined4 *puVar4; - int iVar5; - - if ((FILE *)this->File != (FILE *)0x0) { - iVar5 = fclose((FILE *)this->File); - if (iVar5 != 0) { - piVar3 = __errno_location(); - // try { // try from 0805140e to 08051466 has its CatchHandler @ 08051467 - ::error(&DAT_080f0020,*piVar3); - } - this->File = (FILE *)0x0; - sprintf(ErrorString,"error in binary-file \"%s\": %s.",this->Filename,"File still open"); - puVar4 = (undefined4 *)__cxa_allocate_exception(4); - *puVar4 = ErrorString; - // WARNING: Subroutine does not return - __cxa_throw(puVar4,char*::typeinfo,0); - } - pFVar1 = fopen(__file,"wb"); - this->File = (FILE *)pFVar1; - if (pFVar1 != (FILE *)0x0) { - pcVar2 = strcpy(this->Filename,__file); - return (int)pcVar2; - } - piVar3 = __errno_location(); - iVar5 = *piVar3; - // try { // try from 0805138d to 080513ef has its CatchHandler @ 0805147a - ::error("TWriteBinaryFile::open: Kann Datei %s nicht anlegen.\n",__file); - pcVar2 = strerror(iVar5); - ::error("Fehler %d: %s.\n",iVar5,pcVar2); - sprintf(ErrorString,"Cannot create file %s.",__file); - puVar4 = (undefined4 *)__cxa_allocate_exception(4); - *puVar4 = ErrorString; - // WARNING: Subroutine does not return - __cxa_throw(puVar4,char*::typeinfo,0); -} - - - -// DWARF original prototype: void close(TWriteBinaryFile * this) - -int __thiscall TWriteBinaryFile::close(TWriteBinaryFile *this,int __fd) - -{ - int __errnum; - char *pcVar1; - int *piVar2; - int ErrorCode; - - pcVar1 = (char *)fclose((FILE *)this->File); - if (pcVar1 != (char *)0x0) { - piVar2 = __errno_location(); - __errnum = *piVar2; - ::error(&DAT_080f0060); - pcVar1 = strerror(__errnum); - ::error("# Datei: %s, Fehlercode: %d (%s)\n",this->Filename,__errnum,pcVar1); - } - this->File = (FILE *)0x0; - return (int)pcVar1; -} - - - -// DWARF original prototype: void error(TWriteBinaryFile * this, char * Text) - -void __thiscall TWriteBinaryFile::error(TWriteBinaryFile *this,char *Text) - -{ - int iVar1; - int *piVar2; - undefined4 *puVar3; - - if ((FILE *)this->File != (FILE *)0x0) { - iVar1 = fclose((FILE *)this->File); - if (iVar1 != 0) { - piVar2 = __errno_location(); - // try { // try from 08051531 to 08051592 has its CatchHandler @ 08051593 - ::error(&DAT_080f0020,*piVar2); - } - } - this->File = (FILE *)0x0; - sprintf(ErrorString,"error in binary-file \"%s\": %s.",this->Filename,Text); - puVar3 = (undefined4 *)__cxa_allocate_exception(4); - *puVar3 = ErrorString; - // WARNING: Subroutine does not return - __cxa_throw(puVar3,char*::typeinfo,0); -} - - - -// WARNING: Variable defined which should be unmapped: Help -// DWARF original prototype: void writeByte(TWriteBinaryFile * this, uchar Number) - -void __thiscall TWriteBinaryFile::writeByte(TWriteBinaryFile *this,uchar Number) - -{ - char *Filename; - size_t sVar1; - int *piVar2; - char *pcVar3; - int iVar4; - int iVar5; - undefined4 *puVar6; - int Result; - int ErrorCode; - uchar local_11 [4]; - uchar Help; - - local_11[0] = Number; - sVar1 = fwrite(local_11,1,1,(FILE *)this->File); - if (sVar1 == 1) { - return; - } - piVar2 = __errno_location(); - iVar5 = *piVar2; - // try { // try from 0805160f to 0805166d has its CatchHandler @ 08051700 - ::error("TWriteBinaryFile::writeByte: Fehler beim Schreiben eines Bytes\n"); - pcVar3 = strerror(iVar5); - Filename = this->Filename; - ::error(&DAT_080f00e0,Filename,sVar1,iVar5,pcVar3); - iVar4 = fclose((FILE *)this->File); - if (iVar4 != 0) { - ::error(&DAT_080f0120,*piVar2,sVar1,iVar5,pcVar3); - } - this->File = (FILE *)0x0; - SaveFile(Filename); - if ((FILE *)this->File != (FILE *)0x0) { - iVar5 = fclose((FILE *)this->File); - if (iVar5 != 0) { - // try { // try from 08051691 to 080516e6 has its CatchHandler @ 080516e7 - ::error(&DAT_080f0020,*piVar2); - } - } - this->File = (FILE *)0x0; - sprintf(ErrorString,"error in binary-file \"%s\": %s.",Filename,"Error while writing byte"); - puVar6 = (undefined4 *)__cxa_allocate_exception(4); - *puVar6 = ErrorString; - // WARNING: Subroutine does not return - __cxa_throw(puVar6,char*::typeinfo,0); -} - - - -// WARNING: Variable defined which should be unmapped: ErrorCode -// DWARF original prototype: void writeBytes(TWriteBinaryFile * this, uchar * Buffer, int Count) - -void __thiscall TWriteBinaryFile::writeBytes(TWriteBinaryFile *this,uchar *Buffer,int Count) - -{ - char *Filename; - size_t sVar1; - int *piVar2; - char *pcVar3; - int iVar4; - int iVar5; - undefined4 *puVar6; - int Result; - int ErrorCode; - - sVar1 = fwrite(Buffer,1,Count,(FILE *)this->File); - if (sVar1 == Count) { - return; - } - piVar2 = __errno_location(); - iVar5 = *piVar2; - // try { // try from 0805176e to 080517d2 has its CatchHandler @ 08051860 - ::error("TWriteBinaryFile::writeBytes: Fehler beim Schreiben von %d Bytes\n",Count); - Filename = this->Filename; - pcVar3 = strerror(iVar5); - ::error(&DAT_080f00e0,Filename,sVar1,iVar5,pcVar3); - iVar4 = fclose((FILE *)this->File); - if (iVar4 != 0) { - ::error(&DAT_080f01e0,*piVar2,sVar1,iVar5,pcVar3); - } - this->File = (FILE *)0x0; - SaveFile(Filename); - if ((FILE *)this->File != (FILE *)0x0) { - iVar5 = fclose((FILE *)this->File); - if (iVar5 != 0) { - // try { // try from 080517f6 to 0805184b has its CatchHandler @ 0805184c - ::error(&DAT_080f0020,*piVar2); - } - } - this->File = (FILE *)0x0; - sprintf(ErrorString,"error in binary-file \"%s\": %s.",Filename,"Error while writing bytes"); - puVar6 = (undefined4 *)__cxa_allocate_exception(4); - *puVar6 = ErrorString; - // WARNING: Subroutine does not return - __cxa_throw(puVar6,char*::typeinfo,0); -} - - - void __static_initialization_and_destruction_0(int __initialize_p,int __priority) { diff --git a/reference/types.hh b/reference/types.hh index 8aecd75..6b59f4d 100644 --- a/reference/types.hh +++ b/reference/types.hh @@ -639,21 +639,6 @@ struct fifoIterator { int Position; }; -struct TWriteStream { - int (**_vptr.TWriteStream)(...); -}; - -struct TWriteBuffer { - struct TWriteStream super_TWriteStream; - uchar *Data; - int Size; - int Position; -}; - -struct TDynamicWriteBuffer { - struct TWriteBuffer super_TWriteBuffer; -}; - struct TDirectReplyData { ulong CharacterID; char Message[100]; @@ -786,15 +771,6 @@ struct TReaderThreadReply { int Size; }; -struct TWriteBinaryFile { - struct TWriteStream super_TWriteStream; - undefined field1_0x1; - undefined field2_0x2; - undefined field3_0x3; - FILE *File; - char Filename[4096]; -}; - struct TNPC { struct TNonplayer super_TNonplayer; ulong Interlocutor; diff --git a/src/common.hh b/src/common.hh index 204cf2a..124dd84 100644 --- a/src/common.hh +++ b/src/common.hh @@ -164,6 +164,36 @@ struct TReadBuffer: TReadStream { bool eof(void) override; void skip(int Count) override; + // DATA + // ========================================================================= + const uint8 *Data; + int Size; + int Position; +}; + +struct TWriteStream { + // VIRTUAL FUNCTIONS + // ========================================================================= + virtual void writeFlag(bool Flag); // VTABLE[0] + virtual void writeByte(uint8 Byte) = 0; // VTABLE[1] + virtual void writeWord(uint16 Word); // VTABLE[2] + virtual void writeQuad(uint16 Quad); // VTABLE[3] + virtual void writeString(const char *String); // VTABLE[4] + virtual void writeBytes(const uint8 *Buffer, int Count); // VTABLE[5] +}; + +struct TWriteBuffer: TWriteStream { + // REGULAR FUNCTIONS + // ========================================================================= + TWriteBuffer(uint8 *Data, int Size); + + // VIRTUAL FUNCTIONS + // ========================================================================= + void writeByte(uint8 Byte) override; + void writeWord(uint16 Word) override; + void writeQuad(uint32 Quad) override; + void writeBytes(uint8 *Buffer, int Count) override; + // DATA // ========================================================================= uint8 *Data; @@ -171,4 +201,24 @@ struct TReadBuffer: TReadStream { int Position; }; +struct TDynamicWriteBuffer: TWriteBuffer { + // REGULAR FUNCTIONS + // ========================================================================= + TDynamicWriteBuffer(int InitialSize); + void resizeBuffer(void); + + // VIRTUAL FUNCTIONS + // ========================================================================= + void writeByte(uint8 Byte) override; + void writeWord(uint16 Word) override; + void writeQuad(uint32 Quad) override; + void writeBytes(uint8 *Buffer, int Count) override; + + // TODO(fusion): Appended virtual functions. These are not in the base class + // VTABLE which can be problematic if we intend to use polymorphism, although + // that doesn't seem to be case. + virtual ~TDynamicWriteBuffer(void); // VTABLE[6] + // Duplicate destructor that also calls operator delete. // VTABLE[7] +}; + #endif //TIBIA_COMMON_HH_ diff --git a/src/script.cc b/src/script.cc index 9db02f7..009fd86 100644 --- a/src/script.cc +++ b/src/script.cc @@ -9,6 +9,8 @@ // - TWriteScriptFile::error // - TReadBinaryFile::open // - TReadBinaryFile::error +// - TWriteBinaryFile::open +// - TWriteBinaryFile::error static char ErrorString[100]; // Helper Functions @@ -158,8 +160,9 @@ void TReadScriptFile::open(const char *FileName){ this->File[Depth] = fopen(this->Filename[Depth], "rb"); if(this->File[Depth] == NULL){ + int ErrCode = errno; ::error("TReadScriptFile::open: Kann Datei %s nicht öffnen.\n", this->Filename[Depth]); - ::error("Fehler %d: %s.\n", errno, strerror(errno)); + ::error("Fehler %d: %s.\n", ErrCode, strerror(ErrCode)); throw "Cannot open script-file"; } @@ -650,8 +653,9 @@ void TWriteScriptFile::open(char *FileName){ this->File = fopen(FileName, "wb"); if(this->File == NULL){ + int ErrCode = errno; ::error("TWriteScriptFile: Kann Datei %s nicht anlegen.\n", FileName); - ::error("Fehler %d: %s.\n", errno, strerror(errno)); + ::error("Fehler %d: %s.\n", ErrCode, strerror(ErrCode)); throw "Cannot create script-file"; } @@ -871,15 +875,6 @@ void TReadBinaryFile::seek(int Offset){ // TReadBinaryFile VIRTUAL FUNCTIONS //============================================================================== -TReadBinaryFile::~TReadBinaryFile(void){ - if(this->File != NULL){ - ::error("TReadBinaryFile::~TReadBinaryFile: Datei %s ist noch offen.\n", this->Filename); - if(fclose(this->File) != 0){ - ::error("TReadBinaryFile::~TReadBinaryFile: Fehler %d beim Schließen der Datei.\n", errno); - } - } -} - uint8 TReadBinaryFile::readByte(void){ uint8 Byte; int Result = (int)fread(&Byte, 1, 1, this->File); @@ -894,6 +889,7 @@ uint8 TReadBinaryFile::readByte(void){ if(fclose(this->File) != 0){ ::error("TReadBinaryFile::readByte: Fehler %d beim Schließen der Datei.\n", errno); } + this->File = NULL; SaveFile(this->Filename); this->error("Error while reading byte"); @@ -914,6 +910,7 @@ void TReadBinaryFile::readBytes(uint8 *Buffer, int Count){ if(fclose(this->File) != 0){ ::error("TReadBinaryFile::readBytes: Fehler %d beim Schließen der Datei.\n", errno); } + this->File = NULL; SaveFile(this->Filename); this->error("Error while reading bytes"); @@ -935,3 +932,111 @@ void TReadBinaryFile::skip(int Count){ this->seek(this->getPosition() + Count); } + +TReadBinaryFile::~TReadBinaryFile(void){ + if(this->File != NULL){ + ::error("TReadBinaryFile::~TReadBinaryFile: Datei %s ist noch offen.\n", this->Filename); + if(fclose(this->File) != 0){ + ::error("TReadBinaryFile::~TReadBinaryFile: Fehler %d beim Schließen der Datei.\n", errno); + } + } +} + +// TWriteBinaryFile +//============================================================================== +TWriteBinaryFile::TWriteBinaryFile(void){ + this->File = NULL; +} + +void TWriteBinaryFile::open(char *FileName){ + if(this->File != NULL){ + this->error("File still open"); + } + + this->File = fopen(FileName, "wb"); + if(this->File == NULL){ + int ErrCode = errno; + ::error("TWriteBinaryFile::open: Kann Datei %s nicht anlegen.\n", FileName); + ::error("Fehler %d: %s.\n", ErrCode, strerror(ErrCode)); + + snprintf(ErrorString, sizeof(ErrorString), + "Cannot create file %s.", FileName); + + throw ErrorString; + } + + strcpy(this->Filename, FileName); +} + +void TWriteBinaryFile::close(void){ + // TODO(fusion): Check if file is NULL? + if(fclose(this->File) != 0){ + int ErrCode = errno; + ::error("TWriteBinaryFile::close: Fehler beim Schließen der Datei.\n"); + ::error("# Datei: %s, Fehlercode: %d (%s)\n", + this->Filename, ErrCode, strerror(ErrCode)); + } + this->File = NULL; +} + +void TWriteBinaryFile::error(char *Text){ + if(this->File != NULL){ + if(fclose(this->File) != 0){ + ::error("TWriteBinaryFile::error: Fehler %d beim Schließen der Datei.\n", errno); + } + this->File = NULL; + } + + snprintf(ErrorString, sizeof(ErrorString), + "error in binary-file \"%s\": %s.", + this->Filename, Text); + + throw ErrorString; +} + +void TWriteBinaryFile::writeByte(uint8 Byte){ + int Result = (int)fwrite(&Byte, 1, 1, this->File); + if(Result != 1){ + int ErrCode = errno; + ::error("TWriteBinaryFile::writeByte: Fehler beim Schreiben eines Bytes\n"); + ::error("# Datei: %s, Rückgabewert: %d, Fehlercode: %d (%s)\n", + this->Filename, Result, ErrCode, strerror(ErrCode)); + + // NOTE(fusion): Close file and make a backup, possibly for further inspection. + if(fclose(this->File) != 0){ + ::error("TWriteBinaryFile::writeByte: Fehler %d beim Schließen der Datei.\n", errno); + } + this->File = NULL; + SaveFile(this->Filename); + + this->error("Error while writing byte"); + } +} + +void TWriteBinaryFile::writeBytes(uint8 *Buffer, int Count){ + int Result = (int)fwrite(Buffer, 1, Count, this->File); + if(Result != Count){ + int ErrCode = errno; + ::error("TWriteBinaryFile::writeBytes: Fehler beim Schreiben von %d Bytes\n", Count); + ::error("# Datei: %s, Rückgabewert: %d, Fehlercode: %d (%s)\n", + this->Filename, Result, ErrCode, strerror(ErrCode)); + + // NOTE(fusion): Close file and make a backup, possibly for further inspection. + if(fclose(this->File) != 0){ + ::error("TWriteBinaryFile::writeBytes: Fehler %d beim Schließen der Datei.\n", errno); + } + this->File = NULL; + SaveFile(this->Filename); + + this->error("Error while writing bytes"); + } +} + +TWriteBinaryFile::~TWriteBinaryFile(void){ + if(this->File != NULL){ + ::error("TWriteBinaryFile::~TWriteBinaryFile: Datei %s ist noch offen.\n", this->Filename); + if(fclose(this->File) != 0){ + ::error("TWriteBinaryFile::~TWriteBinaryFile: Fehler %d beim Schließen der Datei.\n", errno); + } + } +} diff --git a/src/script.hh b/src/script.hh index 42ddd2e..16e66fe 100644 --- a/src/script.hh +++ b/src/script.hh @@ -114,17 +114,17 @@ struct TReadBinaryFile: TReadStream { // VIRTUAL FUNCTIONS // ========================================================================= - // TODO(fusion): `TReadStream` itself doesn't have a destructor on its VTABLE - // which makes me think there is something else going on here or the compiler - // optimized it away because it was never used but it seems problematic. - virtual ~TReadBinaryFile(void) override; // VTABLE[8] - // Duplicate destructor that also calls operator delete. // VTABLE[9] - uint8 readByte(void) override; void readBytes(uint8 *Buffer, int Count) override; bool eof(TReadBinaryFile *this) override; void skip(int Count) override; + // TODO(fusion): Appended virtual functions. These are not in the base class + // VTABLE which can be problematic if we intend to use polymorphism, although + // that doesn't seem to be case. + virtual ~TReadBinaryFile(void) override; // VTABLE[8] + // Duplicate destructor that also calls operator delete. // VTABLE[9] + // DATA // ========================================================================= FILE *File; @@ -132,4 +132,28 @@ struct TReadBinaryFile: TReadStream { int FileSize; }; +struct TWriteBinaryFile: TWriteStream { + // REGULAR FUNCTIONS + // ========================================================================= + TWriteBinaryFile(void); + void open(char *FileName); + void close(void); + void error(char *Text); + + // VIRTUAL FUNCTIONS + // ========================================================================= + void writeByte(uint8 Byte) override; + void writeBytes(uint8 *Buffer, int Count) override; + + // TODO(fusion): Appended virtual functions. These are not in the base class + // VTABLE which can be problematic if we intend to use polymorphism, although + // that doesn't seem to be case. + virtual ~TWriteBinaryFile(void); + + // DATA + // ========================================================================= + FILE *File; + char Filename[4096]; +}; + #endif //TIBIA_SCRIPT_HH_ diff --git a/src/util.cc b/src/util.cc index 5cdd933..30e1ba5 100644 --- a/src/util.cc +++ b/src/util.cc @@ -80,7 +80,7 @@ void TReadStream::readString(char *Buffer, int MaxLength){ this->readBytes(Buffer, Length); Buffer[Length] = 0; }else{ - this->readBytes(Buffer, MaxLength - 1); + this->readBytes((uint8*)Buffer, MaxLength - 1); this->skip(Length - MaxLength + 1); Buffer[MaxLength - 1] = 0; } @@ -176,3 +176,175 @@ void TReadBuffer::skip(int Count){ this->Position += Count; } + +// TWriteStream +// ============================================================================= +void TWriteStream::writeFlag(bool Flag){ + this->writeByte((uint8)Flag); +} + +void TWriteStream::writeWord(uint16 Word){ + this->writeByte((uint8)(Word)); + this->writeByte((uint8)(Word >> 8)); +} + +void TWriteStream::writeQuad(uint32 Quad){ + this->writeByte((uint8)(Quad)); + this->writeByte((uint8)(Quad >> 8)); + this->writeByte((uint8)(Quad >> 16)); + this->writeByte((uint8)(Quad >> 24)); +} + +void TWriteStream::writeString(const char *String){ + if(String == NULL){ + this->writeWord(0); + return; + } + + int StringLength = (int)strlen(String); + ASSERT(StringLength >= 0); + if(StringLength < 0xFFFF){ + this->writeWord((uint16)StringLength); + }else{ + this->writeWord(0xFFFF); + this->writeQuad((uint32)StringLength); + } + + if(StringLength > 0){ + this->writeBytes((const uint8*)String, StringLength); + } +} + +void TWriteStream::writeBytes(const uint8 *Buffer, int Count){ + if(Buffer == NULL){ + error("TWriteStream::writeBytes: Übergebener Puffer existiert nicht.\n"); + throw "internal error"; + } + + for(int i = 0; i < Count; i += 1){ + this->writeByte(Buffer[i]); + } +} + +// TWriteBuffer +// ============================================================================= +TWriteBuffer::TWriteBuffer(uint8 *Data, int Size){ + if(Data == NULL){ + error("TWriteBuffer::TWriteBuffer: data ist NULL.\n"); + Size = 0; + }else if(Size < 0){ + error("TWriteBuffer::TWriteBuffer: Ungültige Datengröße %d.\n", Size); + Size = 0; + } + + this->Data = Data; + this->Size = Size; + this->Position = 0; +} + +void TWriteBuffer::writeByte(uint8 Byte){ + if((this->Size - this->Position) < 1){ + throw "buffer full"; + } + + this->Data[this->Position] = Byte; + this->Position += 1; +} + +void TWriteBuffer::writeWord(uint16 Word){ + if((this->Size - this->Position) < 2){ + throw "buffer full"; + } + + this->Data[this->Position] = (uint8)(Word); + this->Data[this->Position + 1] = (uint8)(Word >> 8); + this->Position += 2; +} + +void TWriteBuffer::writeQuad(uint32 Quad){ + if((this->Size - this->Position) < 4){ + throw "buffer full"; + } + + this->Data[this->Position] = (uint8)(Quad); + this->Data[this->Position + 1] = (uint8)(Quad >> 8); + this->Data[this->Position + 2] = (uint8)(Quad >> 16); + this->Data[this->Position + 3] = (uint8)(Quad >> 24); + this->Position += 4; +} + +void TWriteBuffer::writeBytes(uint8 *Buffer, int Count){ + if((this->Size - this->Position) < Count){ + throw "buffer full"; + } + + memcpy(&this->Data[this->Position], Buffer, Count); + this->Position += Count; +} + +// TDynamicWriteBuffer +// ============================================================================= +TDynamicWriteBuffer::TDynamicWriteBuffer(int InitialSize) + : TWriteBuffer(new uint8[InitialSize], InitialSize) +{ + // no-op +} + +void TDynamicWriteBuffer::resizeBuffer(void){ + ASSERT(this->Size > 0); + int Size = this->Size * 2; + uint8 *Data = new uint8[Size]; + if(this->Data != NULL){ + memcpy(Data, this->Data, this->Size); + delete[] this->Data; + } + + this->Data = Data; + this->Size = Size; +} + +void TDynamicWriteBuffer::writeByte(uint8 Byte){ + while((this->Size - this->Position) < 1){ + this->resizeBuffer(); + } + + this->Data[this->Position] = Byte; + this->Position += 1; +} + +void TDynamicWriteBuffer::writeWord(uint16 Word){ + while((this->Size - this->Position) < 2){ + this->resizeBuffer(); + } + + this->Data[this->Position] = (uint8)(Word); + this->Data[this->Position + 1] = (uint8)(Word >> 8); + this->Position += 2; +} + +void TDynamicWriteBuffer::writeQuad(uint32 Quad){ + while((this->Size - this->Position) < 4){ + this->resizeBuffer(); + } + + this->Data[this->Position] = (uint8)(Quad); + this->Data[this->Position + 1] = (uint8)(Quad >> 8); + this->Data[this->Position + 2] = (uint8)(Quad >> 16); + this->Data[this->Position + 3] = (uint8)(Quad >> 24); + this->Position += 4; +} + +void TDynamicWriteBuffer::writeBytes(uint8 *Buffer, int Count){ + while((this->Size - this->Position) < Count){ + this->resizeBuffer(); + } + + memcpy(&this->Data[this->Position], Buffer, Count); + this->Position += Count; +} + +TDynamicWriteBuffer::~TDynamicWriteBuffer(void){ + if(this->Data != NULL){ + delete[] this->Data; + } +} -- cgit v1.2.3