Ejemplo n.º 1
0
void SBNK::Read(PseudoReadFile &file)
{
	uint32_t startOfSBNK = file.pos;
	this->header.Read(file);
	try
	{
		this->header.Verify("SBNK", 0x0100FEFF);
	}
	catch (const std::exception &)
	{
		if (SDAT::failOnMissingFiles)
			throw;
		else
			return;
	}
	int8_t type[4];
	file.ReadLE(type);
	if (!VerifyHeader(type, "DATA"))
		throw std::runtime_error("SBNK DATA structure invalid");
	file.ReadLE<uint32_t>(); // size
	uint32_t reserved[8];
	file.ReadLE(reserved);
	this->count = file.ReadLE<uint32_t>();
	this->instruments.resize(this->count);
	for (uint32_t i = 0; i < this->count; ++i)
		this->instruments[i].Read(file, startOfSBNK);
}
Ejemplo n.º 2
0
void INFOSection::Read(PseudoReadFile &file)
{
	uint32_t startOfINFO = file.pos;
	file.ReadLE(this->type);
	if (!VerifyHeader(this->type, "INFO"))
		throw std::runtime_error("SDAT INFO Section invalid");
	this->size = file.ReadLE<uint32_t>();
	file.ReadLE(this->recordOffsets);
	if (this->recordOffsets[REC_SEQ])
	{
		file.pos = startOfINFO + this->recordOffsets[REC_SEQ];
		this->SEQrecord.Read(file, startOfINFO);
	}
	if (this->recordOffsets[REC_BANK])
	{
		file.pos = startOfINFO + this->recordOffsets[REC_BANK];
		this->BANKrecord.Read(file, startOfINFO);
	}
	if (this->recordOffsets[REC_WAVEARC])
	{
		file.pos = startOfINFO + this->recordOffsets[REC_WAVEARC];
		this->WAVEARCrecord.Read(file, startOfINFO);
	}
	if (this->recordOffsets[REC_PLAYER])
	{
		file.pos = startOfINFO + this->recordOffsets[REC_PLAYER];
		this->PLAYERrecord.Read(file, startOfINFO);
	}
}
Ejemplo n.º 3
0
void FATSection::Read(PseudoFile &file)
{
	int8_t type[4];
	file.ReadLE(type);
	if (!VerifyHeader(type, "FAT "))
		throw std::runtime_error("SDAT FAT Section invalid");
	file.ReadLE<uint32_t>(); // size
	uint32_t count = file.ReadLE<uint32_t>();
	this->records.resize(count);
	for (uint32_t i = 0; i < count; ++i)
		this->records[i].Read(file);
}
Ejemplo n.º 4
0
bool CWorkGrammar::LoadFromFile(const Stroka& file_name, const ITerminalDictionary& terminals)
{
    TIFStream f(file_name);

    // binary protection
    VerifyHeader(&f);

    TLz4Decompress z(&f);
    // first load kw-types table
    GetMutableKWTypePool().Load(&z);
    // then load the grammar itself
    ::LoadProtectedSize(&z);
    this->Load(&z, terminals);
    return true;
}
Ejemplo n.º 5
0
void NDSStdHeader::Verify(const std::string &typeToCheck, uint32_t magicToCheck)
{
	if (!VerifyHeader(this->type, typeToCheck) || this->magic != magicToCheck)
		throw std::runtime_error("NDS Standard Header for " + typeToCheck + " invalid");
}
Ejemplo n.º 6
0
/// Saves map data to file.
bool Map::Save(const char * filename){
	std::fstream file;
	file.open(filename, std::ios_base::out | std::ios_base::binary);
	/// If failed to open, close and return false.
	if (!file.is_open()){
		file.close();
		std::cout<<"\nERROR: Unable to open filestream to ";
		PrintPath(filename);
		lastErrorString = "Unable to open file stream.";
		return false;
	}

	/// Set version to latest
	header.version = MV_LATEST;
	header.dateCreated = time(0);	// Get current time
	strcpy(header.mapName, name);	// Set header map name to the one we're using in the editor


	/// Verify that header is OK before we save it!
	if (!VerifyHeader()){
		std::cout<<"\nERROR: Unsupported map version. Try updating the game! ^^";
		return false;
	}
	int size = sizeof(MapFileHeader);	// Should be int(4) + long(4) + 260 chars
	header.Write(file);

	/// Make sure, // NO! this is check in the WriteEntities() function!
//	assert(cEntities.Size() == entities.Size());


	/// Block types for writing
	int blockType = -1;
	// Write entities if we have any
	if (NumEntities() > 0 && NumEntities() <= MAX_ENTITIES){
		WriteEntities(file);
	}
	else{
		std::cout<<"\nInvalid amount of entities: "<<NumEntities();
		lastErrorString = "No entities to save.";
		return false;
	}
	/// Write paths block if we got any
	if (paths.Size() > 0){
		WritePaths(file);
	}

	int end = BLOCK_END_OF_FILE;
	if (header.version < MV_PATHS)
		end = OLD_BLOCK_END_OF_FILE;

	file.write((char*)&end, sizeof(int));
	int endNull = 0;
	file.write((char*)&endNull, sizeof(int));

	file.close();

	/// Consider using other mechanisms to check that everything was written correctly...
	//////////////////////////////////////////
	// Check that the written contents are the same as what we wanted to save..
	if (false){
		file.open(filename, std::ios_base::in | std::ios_base::binary);
		Map verificationMap;
		file.read((char*) &verificationMap.header, sizeof(MapFileHeader));
		// Check stuff
		bool verificationDone = false;
		while(!verificationDone){
			int blockType;
			file.read((char*) &blockType, sizeof(int));
			int blockSize;
			file.read((char*) &blockSize, sizeof(int));
			switch(blockType){
				case BLOCK_ENTITIES:
					verificationMap.ReadEntities(file);
					break;
				case OLD_BLOCK_END_OF_FILE:
					verificationDone = true;
					std::cout<<"\nVerification done! File is as it should be.";
					break;
				default:
					std::cout<<"\nUnknown block type! You failed douche!";
					assert(false && "Unknown block type when verifying saved file!");
					break;
			}
		}
		file.close();
	}
	return true;
}
Ejemplo n.º 7
0
/// Loads map data from file.
bool Map::Load(const char * fromFile){
	std::cout<<"\nLoading map from file: "<<fromFile;
	std::fstream file;
	file.open(fromFile, std::ios_base::in | std::ios_base::binary);
	/// If failed to open, close and return false.
	if (!file.is_open()){
		file.close();
		std::cout<<"\nERROR: Unable to open filestream to ";
		lastErrorString = "Unable to open filestream.";
		PrintPath(fromFile);
		return false;
	}

	/// Read and verify header before reading any more
	header.Read(file);
	if (!VerifyHeader()){
		std::cout<<"\nERROR: Unsupported map version. Try updating the game! ^^";
		return false;
	}


	bool endOfFile = false;
	while (!file.eof() && !endOfFile){
		/// Read next integer to inrepret type of next data block
		int type;
		int blockSize;
		file.read((char*) &type, sizeof(int));
		if (header.version < MV_PATHS)
			file.read((char*) &blockSize, sizeof(int));
		if (type < 0 || type > MAX_BLOCK_TYPES){
            SWAPFOUR(type);
		}
		switch(type){
			case BLOCK_ENTITIES:
				ReadEntities(file);
				break;
			case BLOCK_PATHS:
				ReadPaths(file);
				break;
			case OLD_BLOCK_END_OF_FILE: {
				/// Check version here.
				if (header.version < MV_PATHS){
					endOfFile = true;
					break;
				}
				break;
			}
			case BLOCK_END_OF_FILE:
				endOfFile = true;
				break;
			default:
				std::cout<<"\nUnknown block type: "<<type;
				std::cout<<"\nAborting loading procedure because of unknown data in stream.";
				file.close();
				return false;
		}
	}

	/// Close file ^^
	file.close();
	source = fromFile;

	mapDataOK = true;
	std::cout<<"\nFile successfully read.";
	return true;
}
Ejemplo n.º 8
0
bool WP_Message::Process()
{
    UINT8* buf = (UINT8*)&m_header;
    int    len;

    while(true)
    {
        switch(m_rxState)
        {
        case ReceiveState::Idle:
            return true;

        case ReceiveState::Initialize:
            Release();

            m_rxState = ReceiveState::WaitingForHeader;
            m_pos     = (UINT8*)&m_header;
            m_size    = sizeof(m_header);
            break;

        case ReceiveState::WaitingForHeader:
            if(m_parent->m_phy->ReceiveBytes( m_parent->m_state, m_pos, m_size ) == false) return true;

            //
            // Synch to the start of a message.
            //
            while(true)
            {
                len = sizeof(m_header) - m_size; if(len <= 0) break;

                size_t lenCmp = __min( len, sizeof(m_header.m_signature) );

                if(memcmp( &m_header, MARKER_DEBUGGER_V1, lenCmp ) == 0) break;
                if(memcmp( &m_header, MARKER_PACKET_V1  , lenCmp ) == 0) break;

                memmove( &buf[ 0 ], &buf[ 1 ], len-1 );

                m_pos--;
                m_size++;
            }

            if(len >= sizeof(m_header.m_signature))
            {
                m_rxState = ReceiveState::ReadingHeader;
            }
            break;

        case ReceiveState::ReadingHeader:
            if(m_parent->m_phy->ReceiveBytes( m_parent->m_state, m_pos, m_size ) == false) return true;

            if(m_size == 0)
            {
               m_rxState = ReceiveState::CompleteHeader;
            }
            break;

        case ReceiveState::CompleteHeader:
            {
                bool fBadPacket=true;
                if( VerifyHeader() )
                {
#if defined(BIG_ENDIAN)
                    SwapEndian();
#endif
                    if ( m_parent->m_app->ProcessHeader( m_parent->m_state, this ) )
                    {
                        fBadPacket = false;
                        if(m_header.m_size)
                        {
                            if(m_payload == NULL) // Bad, no buffer...
                            {
                                m_rxState = ReceiveState::Initialize;
                            }
                            else
                            {
                                m_payloadTicks = HAL_Time_CurrentTicks();
                                m_rxState = ReceiveState::ReadingPayload;
                                m_pos     = (UINT8*)m_payload;
                                m_size    = m_header.m_size;
                            }
                        }
                        else
                        {
                            m_rxState = ReceiveState::CompletePayload;
                        }
                    }
                }
                
                if ( fBadPacket )
                {
                    if((m_header.m_flags & WP_Flags::c_NonCritical) == 0)
                    {
                        ReplyBadPacket( WP_Flags::c_BadHeader );
                    }

                    m_rxState = ReceiveState::Initialize;
                }
            }
            break;

        case ReceiveState::ReadingPayload:
            {
                UINT64 curTicks = HAL_Time_CurrentTicks();

                // If the time between consecutive payload bytes exceeds the timeout threshold then assume that
                // the rest of the payload is not coming. Reinitialize to synch on the next header. 

                if(HAL_Time_TicksToTime( curTicks - m_payloadTicks ) < (UINT64)c_PayloadTimeout)
                {
                    m_payloadTicks = curTicks;
                
                    if(m_parent->m_phy->ReceiveBytes( m_parent->m_state, m_pos, m_size ) == false) return true;

                    if(m_size == 0)
                    {
                        m_rxState = ReceiveState::CompletePayload;
                    }
                }
                else
                {
                    m_rxState = ReceiveState::Initialize;
                }
            }
            break;

        case ReceiveState::CompletePayload:
            if(VerifyPayload() == true)
            {
                m_parent->m_app->ProcessPayload( m_parent->m_state, this );
            }
            else
            {
                ReplyBadPacket( WP_Flags::c_BadPayload );
            }

            m_rxState = ReceiveState::Initialize;
            break;


        default:
            return false;
        }
    }
}