bool ScriptEngine::LoadScript(const char* str) { FILE* file = fopen(str, "rb"); if (!file) { return false; } fseek(file, 0, SEEK_END); AnzUInt ScriptLength = ftell(file); rewind(file); ScriptBuffer = new AnzUInt8[ScriptLength]; fread(ScriptBuffer, 1, ScriptLength, file); fclose(file); HeaderOffset = *(AnzUInt*)(ScriptBuffer); #if SDL_BYTEORDER != SDL_LIL_ENDIAN HeaderOffset = SDL_Swap32(HeaderOffset); #endif Title = std::string((char*)(ScriptBuffer + HeaderOffset + sizeof(HcbHeader))); if (HeaderOffset >= ScriptLength) { delete[] ScriptBuffer; ScriptBuffer = nullptr; return false; } HcbHeader *hcbHeader = (HcbHeader*)(ScriptBuffer + HeaderOffset); #if SDL_BYTEORDER == SDL_LIL_ENDIAN EntryOffset = hcbHeader->EntryPoint; #else EntryPoint = SDL_Swap32(hcbHeader->EntryPoint); #endif AnzUShort ResolutionIndex = 1; #if SDL_BYTEORDER == SDL_LIL_ENDIAN ResolutionIndex = hcbHeader->ResolutionIndex; #else ResolutionIndex = SDL_Swap16(hcbHeader->ResolutionIndex); #endif switch (ResolutionIndex) { case 1: Width = 800; Height = 600; break; case 7: Width = 1024; Height = 640; break; default: ErrorLog(fvpError, "Unknown resolutionIndex, using[800x600] as default!\n"); Height = 800; Width = 600; break; } ErrorLog(fvpLog, "Script Loaded!"); return true; }
/** * Creates a CAT file stream. A CAT file starts with an index of the * offset and size of every file contained within. Each file consists * of a filename followed by its contents. * @param path Full path to CAT file. */ CatFile::CatFile(const char *path) : std::ifstream(path, std::ios::in | std::ios::binary), _amount(0), _offset(0), _size(0) { if (!this) return; // Get amount of files read((char*)&_amount, sizeof(_amount)); #if SDL_BYTEORDER == SDL_BIG_ENDIAN _amount = (unsigned int)SDL_Swap32( _amount ); #endif _amount /= 2 * sizeof(_amount); // Get object offsets seekg(0, std::ios::beg); _offset = new unsigned int[_amount]; _size = new unsigned int[_amount]; for (unsigned int i = 0; i < _amount; ++i) { read((char*)&_offset[i], sizeof(*_offset)); #if SDL_BYTEORDER == SDL_BIG_ENDIAN _offset[i] = (unsigned int)SDL_Swap32( _offset[i] ); #endif read((char*)&_size[i], sizeof(*_size)); #if SDL_BYTEORDER == SDL_BIG_ENDIAN _size[i] = (unsigned int)SDL_Swap32( _size[i] ); #endif } }
//0x02 CALL bool __fastcall Call(ScriptObject *stack) { stack->ScriptPos++; AnzUInt hcbCall = *(AnzUInt*)(stack->Buffer + stack->ScriptPos); #if SDL_BYTEORDER != SDL_LIL_ENDIAN hcbCall = SDL_Swap32(hcbCall); #endif stack->Next = new ScriptObject; stack->Next->ScriptPos = hcbCall; stack->Next->ThreadID = stack->ThreadID; stack->Next->Prev = stack; //Push stack ThreadInstance _co->SetIterNext(stack); AnzUInt8 Count = *(AnzUInt8*)(stack->Buffer+hcbCall+1); AnzUInt8 start = 0xFE; if(Count != 0) { while(Count--) { stack->Next->LocalStack[start] = stack->LocalStack[stack->CurStackFrame]; stack->LocalStack[stack->CurStackFrame].Relase(); stack->CurStackFrame--; start--; } } stack->ScriptPos += 4; return true; }
int Luola_ReadLE32(SDL_RWops *src,Uint32 *value) { if(SDL_RWread(src, value, 4, 1)==-1) return -1; *value = SDL_Swap32(*value); return 4; }
// endian-swapping fread that dies if the expected amount cannot be read size_t efread( void *buffer, size_t size, size_t num, FILE *stream ) { size_t num_read = fread(buffer, size, num, stream); switch (size) { #if SDL_BYTEORDER == SDL_BIG_ENDIAN case 2: for (size_t i = 0; i < num; i++) ((Uint16 *)buffer)[i] = SDL_Swap16(((Uint16 *)buffer)[i]); break; case 4: for (size_t i = 0; i < num; i++) ((Uint32 *)buffer)[i] = SDL_Swap32(((Uint32 *)buffer)[i]); break; case 8: for (size_t i = 0; i < num; i++) ((Uint64 *)buffer)[i] = SDL_Swap64(((Uint64 *)buffer)[i]); break; #endif default: break; } if (num_read != num) { fprintf(stderr, "error: An unexpected problem occurred while reading from a file.\n"); JE_tyrianHalt(1); } return num_read; }
/** Decodes "FINF" Chunk * @param pointer to store the offsets in. * @return 0 on success, 1 on failure */ int VQAMovie::DecodeFINFChunk() { Uint8 ChunkID[4]; int i; vqafile->readByte(ChunkID, 4); if (strncmp((const char*)ChunkID, "FINF", 4)) { fprintf(stderr, "Error decoding FINF chunk - Expected \"FINF\", got \"%c%c%c%c\"\n", ChunkID[0], ChunkID[1], ChunkID[2], ChunkID[3]); return 1; } /* Skip chunk len. its not important yet? */ vqafile->seekCur(4); vqafile->readByte((Uint8*)offsets, header.NumFrames<<2); for (i = 0; i < header.NumFrames; i++) { #if SDL_BYTEORDER == SDL_BIG_ENDIAN offsets[i] = SDL_Swap32(offsets[i]); #endif offsets[i] &= 0x3FFFFFFF; offsets[i] <<= 1; } return 0; }
/* Byte-swap the pixels in the display image */ static void X11_SwapAllPixels(SDL_Surface *screen) { int x, y; switch (screen->format->BytesPerPixel) { case 2: { Uint16 *spot; for ( y=0; y<screen->h; ++y ) { spot = (Uint16 *) ((Uint8 *)screen->pixels + y * screen->pitch); for ( x=0; x<screen->w; ++x, ++spot ) { *spot = SDL_Swap16(*spot); } } } break; case 4: { Uint32 *spot; for ( y=0; y<screen->h; ++y ) { spot = (Uint32 *) ((Uint8 *)screen->pixels + y * screen->pitch); for ( x=0; x<screen->w; ++x, ++spot ) { *spot = SDL_Swap32(*spot); } } } break; default: /* should never get here */ break; } }
/** * Attach a 16x16 icon, of ARGB32 format, to the window. * @param *pixels Buffer of pixels in ARGB format */ GFraMe_ret GFraMe_set_icon(char *pixels) { GFraMe_ret rv; SDL_Surface *surf = NULL; Uint32 *pix32 = (Uint32*)pixels; GFraMe_assertRV(GFraMe_window, "Window not yet initialized", rv = GFraMe_ret_failed, _ret); // Check if byte order is correct and fix it if (SDL_BYTEORDER == SDL_LIL_ENDIAN) { int i = 0; int l = 16 * 16; while (i < l) { pix32[i] = SDL_Swap32(pix32[i]); i++; } } surf = SDL_CreateRGBSurfaceFrom(pix32, 16, 16, 32, 16*4, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); GFraMe_SDLassertRV(surf, "Failed to create surface", rv = GFraMe_ret_failed, _ret); SDL_SetWindowIcon(GFraMe_window, surf); rv = GFraMe_ret_ok; _ret: return rv; }
void swap32 (void *d) { if (bendian) { *((int32_t *)d) = SDL_Swap32(*((int32_t *)d)); } }
uint32_t host32(uint32_t number) { #if SDL_BYTEORDER == SDL_LIL_ENDIAN return SDL_Swap32(number); #else return number; #endif }
int File::read( Uint32 *n, int count ) { int ret = read( n, sizeof( Uint32 ), count ); if( SDL_BYTEORDER != SCOURGE_BYTE_ORDER ) { for( int i = 0; i < count; i++ ) { *( n + i ) = SDL_Swap32( *( n + i ) ); } } return ret; }
inline void Utl_writeInt(Uint32 num,char* data){ #if NEED_SWAP num = SDL_Swap32(num); #endif data[0] = (num & 0xFF000000) >> 24; data[1] = (num & 0x00FF0000) >> 16; data[2] = (num & 0x0000FF00) >> 8; data[3] = (num & 0x000000FF) >> 0; }
inline void VQAMovie::DecodeUnknownChunk() { Uint32 ChunkLen; vqafile->readDWord(&ChunkLen, 1); ChunkLen = SDL_Swap32(ChunkLen); vqafile->seekCur(ChunkLen); }
void MessageOut::writeInt32(Sint32 value) { expand(4); #if SDL_BYTEORDER == SDL_BIG_ENDIAN Sint32 swap=SDL_Swap32(value); memcpy(mData + mPos, &swap, sizeof(Sint32)); #else memcpy(mData + mPos, &value, sizeof(Sint32)); #endif mPos += 4; }
int File::write( Uint32 *n, int count ) { // always save as big endian if( SDL_BYTEORDER != SCOURGE_BYTE_ORDER ) { for( int i = 0; i < count; i++ ) { tmp32[ i ] = SDL_Swap32( *( n + i ) ); } return write( tmp32, sizeof( Uint32 ), count ); } else { return write( n, sizeof( Uint32 ), count ); } }
inline Uint32 Utl_readInt(char* data){ Uint32 num; num = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + (data[3] << 0); #if NEED_SWAP num = SDL_Swap32(num); #endif return num; }
inline void VQAMovie::DecodeCBPChunk() { Uint32 ChunkLen; vqafile->readDWord(&ChunkLen, 1); ChunkLen = SDL_Swap32(ChunkLen); vqafile->readByte(CBP_LookUp + CBPOffset, ChunkLen); CBPOffset += ChunkLen; CBPChunks++; }
/** Decodes "FORM" Chunk * @return 0 on success, 1 on failure */ int VQAMovie::DecodeFORMChunk() { char ChunkID[4]; vqafile->readByte((Uint8*)ChunkID, 4); if (strncmp(ChunkID, "FORM", 4)) { fprintf(stderr, "Error decoding FORM Chunk - Expected \"FORM\", got \"%c%c%c%c\"\n", ChunkID[0], ChunkID[1], ChunkID[2], ChunkID[3]); return 1; } /* skip chunklen */ vqafile->seekCur(4); #if SDL_BYTEORDER == SDL_LIL_ENDIAN vqafile->readByte(reinterpret_cast<Uint8 *>(&header), header_size); #else vqafile->readByte((Uint8*)&header.Signature, sizeof(header.Signature)); vqafile->readDWord(&header.RStartPos, 1); vqafile->readWord(&header.Version, 1); vqafile->readWord(&header.Flags, 1); vqafile->readWord(&header.NumFrames, 1); vqafile->readWord(&header.Width, 1); vqafile->readWord(&header.Height, 1); vqafile->readByte(&header.BlockW, 1); vqafile->readByte(&header.BlockH, 1); vqafile->readByte(&header.FrameRate, 1); vqafile->readByte(&header.CBParts, 1); vqafile->readWord(&header.Colors, 1); vqafile->readWord(&header.MaxBlocks, 1); vqafile->readWord(&header.Unknown1, 1); vqafile->readDWord(&header.Unknown2, 1); vqafile->readWord(&header.Freq, 1); vqafile->readByte(&header.Channels, 1); vqafile->readByte(&header.Bits, 1); vqafile->readByte((Uint8*)&header.Unknown3, sizeof(header.Unknown3)); #endif // Weird: need to byteswap on both BE and LE // readDWord probably swaps back on BE. header.RStartPos = SDL_Swap32(header.RStartPos); /* Check if header is valid */ if (strncmp((const char*)header.Signature, "WVQAVQHD", 8) == 1 || header.Version != 2) { fprintf(stderr, "Invalid Header. Either signature is invalid or it is in a version that we do not support\n"); return 1; } /* set some constants based on the header */ lowoffset = (header.Width/header.BlockW)*(header.Height/header.BlockH); modifier = header.BlockH == 2 ? 0x0f : 0xff; return 0; }
void swap_buf32_if_need(Uint8 src_endian,Uint32* buf,Uint32 size) { int i; #ifdef WORDS_BIGENDIAN Uint8 my_endian=1; #else Uint8 my_endian=0; #endif if (my_endian!=src_endian) { for (i=0;i<size;i++) buf[i]=SDL_Swap32(buf[i]); } }
void MessageOut::writeInt32(int32_t value) { DEBUGLOG("writeInt32: " + toString(value)); expand(4); #if SDL_BYTEORDER == SDL_BIG_ENDIAN int32_t swap = SDL_Swap32(value); memcpy(mData + mPos, &swap, sizeof(int32_t)); #else memcpy(mData + mPos, &value, sizeof(int32_t)); #endif mPos += 4; PacketCounters::incOutBytes(4); }
void restartEcran() { SDL_FreeSurface(ecran); SDL_FreeSurface(fond); ecran = SDL_SetVideoMode(LARGEUR, HAUTEUR, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); fond = SDL_CreateRGBSurface(SDL_HWSURFACE, LARGEUR, HAUTEUR, 32, 0, 0 , 0, 0); //on initialise le fond #ifdef MACOS SDL_FillRect(fond, NULL, SDL_Swap32(SDL_MapRGB(ecran->format, FOND_R, FOND_G, FOND_B))); //We change background color #else SDL_FillRect(fond, NULL, SDL_MapRGB(ecran->format, FOND_R, FOND_G, FOND_B)); //We change background color #endif SDL_BlitSurface(fond, NULL, ecran, NULL); }
inline Uint32 NetUtl_recvInt(TCPsocket* sock){ Uint32 num; int size; int total_size = 0; while((total_size < sizeof(num)) && ((size = SDLNet_TCP_Recv(*sock,&num,sizeof(num))) > 0)){ total_size += size; } #if NEED_SWAP num = SDL_Swap32(num); #endif return num; }
//0x06 bool __fastcall JMP(ScriptObject *stack) { stack->ScriptPos++; AnzUInt hcbJMP = *(AnzUInt*)(stack->Buffer + stack->ScriptPos); #if SDL_BYTEORDER != SDL_LIL_ENDIAN hcbJMP = SDL_Swap32(hcbJMP); #endif stack->ScriptPos = hcbJMP; return true; }
int MessageIn::readInt32() { int value = -1; if (mPos + 4 <= mLength) { #if SDL_BYTEORDER == SDL_BIG_ENDIAN value = SDL_Swap32(*(Sint32*)(mData + mPos)); #else value = (*(Sint32*)(mData + mPos)); #endif } mPos += 4; return value; }
void MessageOut::writeInt32(const int32_t value, const char *const str) { DEBUGLOG2("writeInt32: " + toStringPrint(CAST_U32(value)), mPos, str); expand(4); #if SDL_BYTEORDER == SDL_BIG_ENDIAN int32_t swap = SDL_Swap32(value); memcpy(mData + CAST_SIZE(mPos), &swap, sizeof(int32_t)); #else memcpy(mData + CAST_SIZE(mPos), &value, sizeof(int32_t)); #endif mPos += 4; PacketCounters::incOutBytes(4); }
/** Decodes SND Chunk * @param pointer to decoded chunk * @return length of chunk */ Uint32 VQAMovie::DecodeSNDChunk(Uint8 *outbuf) { Uint32 ChunkLen; Uint32 ChunkID; Uint8 *inbuf; /* header Flags tells us that this VQA does not support sounds.. so lets quit */ if (!(header.Flags & 1)) return 0; /* seek to correct offset */ if (vqafile->tell() & 1) vqafile->seekCur(1); vqafile->readDWord(&ChunkID, 1); if ((ChunkID & vqa_t_mask) != vqa_snd_id) { fprintf(stderr, "Error decoding SND chunk - Expected 0x%X, got 0x%X\n", vqa_snd_id, ChunkID & vqa_t_mask); return 0; /* Returning zero here, to set length of sound chunk to zero */ } vqafile->readDWord( &ChunkLen, 1); ChunkLen = SDL_Swap32(ChunkLen); inbuf = new Uint8[ChunkLen]; vqafile->readByte(inbuf, ChunkLen); switch (VQA_HI_BYTE(ChunkID)) { case '0': /* Raw uncompressed wave data */ memcpy(outbuf, inbuf, ChunkLen); break; case '1': /* Westwoods own algorithm */ /* TODO: Add support for this algorithm */ fprintf(stderr, "Error decoding SND chunk - sound compressed using unsupported westwood algorithm\n"); ChunkLen = 0; break; case '2': /* IMA ADPCM algorithm */ Compression::IMADecode(inbuf, outbuf, ChunkLen, &sndindex, &sndsample); ChunkLen <<= 2; /* IMA ADPCM decompresses sound to a size 4 times larger than the compressed size */ break; default: fprintf(stderr, "Error decoding SND chunk - sound in unknown format\n"); ChunkLen = 0; break; } delete[] inbuf; return ChunkLen; }
/** * Optimized 8 bit zoomer for resizing by a factor of 2. Doesn't flip. * 32-bit version for sad old x86 chips which run out of registers * with the 64-bit version. * Used internally by _zoomSurfaceY() below. * source and dest. widths must be multiples of 4 bytes for 32-bit access */ static int zoomSurface2X_32bit(SDL_Surface *src, SDL_Surface *dst) { Uint32 dataSrc; Uint32 dataDst; Uint8 *pixelSrc = (Uint8*)src->pixels; Uint8 *pixelDstRow = (Uint8*)dst->pixels; int sx, sy; static bool proclaimed = false; if (!proclaimed) { proclaimed = true; Log(LOG_INFO) << "Using 32-bit 2X zoom routine."; } for (sy = 0; sy < src->h; ++sy, pixelDstRow += dst->pitch*2) { Uint32 *pixelDst = (Uint32*)pixelDstRow; Uint32 *pixelDst2 = (Uint32*)(pixelDstRow + dst->pitch); for (sx = 0; sx < src->w; sx += 4, pixelSrc += 4) { dataSrc = *((Uint32*) pixelSrc); #if SDL_BYTEORDER == SDL_BIG_ENDIAN // boo SDL_Swap32(dataSrc); #endif dataDst = (dataSrc & 0xFF) | ((dataSrc & 0xFFFF) << 8) | ((dataSrc & 0xFF00) << 16); *pixelDst = dataDst; *pixelDst2 = dataDst; pixelDst++; // forward 4 bytes! pixelDst2++; dataSrc >>= 16; dataDst = (dataSrc & 0xFF) | ((dataSrc & 0xFFFF) << 8) | ((dataSrc & 0xFF00) << 16); *pixelDst = dataDst; *pixelDst2 = dataDst; pixelDst++; // forward 4 bytes! pixelDst2++; } } return 0; }
inline void VQAMovie::DecodeCPLChunk(SDL_Color *palette) { Uint32 ChunkLen; int i; vqafile->readDWord(&ChunkLen, 1); ChunkLen = SDL_Swap32(ChunkLen); for (i = 0; i < header.Colors; i++) { vqafile->readByte((Uint8*)&palette[i], 3); palette[i].r <<= 2; palette[i].g <<= 2; palette[i].b <<= 2; } }
bool __fastcall PushInt8(ScriptObject *stack) { stack->ScriptPos++; stack->CurStackFrame++; VarInfo* var = &(stack->LocalStack[stack->CurStackFrame]); var->type = VarInfo::pINT; var->Value.IntVar = *(AnzUChar*)(stack->Buffer + stack->ScriptPos); #if SDL_BYTEORDER != SDL_LIL_ENDIAN var->Value.IntVar = SDL_Swap32(var->Value.IntVar); #endif stack->ScriptPos += 1; return true; }
uint32_t MessageIn::readInt32() { uint32_t value = 0; if (mPos + 4 <= mLength) { #if SDL_BYTEORDER == SDL_BIG_ENDIAN uint32_t swap; memcpy(&swap, mData + mPos, sizeof(uint32_t)); value = SDL_Swap32(swap); #else memcpy(&value, mData + mPos, sizeof(uint32_t)); #endif } mPos += 4; return value; }