Example #1
0
uint32_t read_le32(stream_t *src)
{
   uint32_t value;

   stream_read(src, &value, sizeof(value));
   return(SwapLE32(value));
}
Example #2
0
bool AGG::File::Open(const std::string & fname)
{
    filename = fname;
    stream = new std::ifstream(filename.c_str(), std::ios::binary);

    if(!stream || !stream->is_open())
    {
	DEBUG(DBG_ENGINE , DBG_WARN, "AGG::File: error read file: " << filename << ", skipping...");
	return false;
    }

    DEBUG(DBG_ENGINE , DBG_INFO, "AGG::File: load: " << filename);

    stream->read(reinterpret_cast<char *>(&count_items), sizeof(u16));
    SwapLE16(count_items);

    DEBUG(DBG_ENGINE , DBG_INFO, "AGG::File: count items: " << count_items);

    char buf[FATSIZENAME + 1];
    buf[FATSIZENAME] = 0;

    for(u16 ii = 0; ii < count_items; ++ii)
    {
            const u32 pos = stream->tellg();

            stream->seekg(-FATSIZENAME * (count_items - ii), std::ios_base::end);
            stream->read(buf, FATSIZENAME);

            const std::string key(buf);

            FAT & f = fat[key];
		
            stream->seekg(pos, std::ios_base::beg);

            stream->read(reinterpret_cast<char *>(&f.crc), sizeof(u32));
            SwapLE32(f.crc);

            stream->read(reinterpret_cast<char *>(&f.offset), sizeof(u32));
            SwapLE32(f.offset);

            stream->read(reinterpret_cast<char *>(&f.size), sizeof(u32));
            SwapLE32(f.size);
    }
    return true;
}
Example #3
0
//---------------------------------------------------------------------------//
  void SerialLoader::IO(uint32_t& p_Value)
  {
    uint32_t val;

    if (!m_streamer->Read(&val, sizeof(uint32_t)))
        p_Value = 0;
    else
        p_Value = SwapLE32(val);
  }
Example #4
0
int write_le32(stream_t *dst, uint32_t value)
{
   value = SwapLE32(value);
   return(stream_write(dst, &value, sizeof(value)));
}
Example #5
0
int main(int argc, char **argv)
{
    if(argc != 3)
    {
	std::cout << argv[0] << " path_heroes2.agg extract_to_dir" << std::endl;

	return EXIT_SUCCESS;
    }

    std::fstream fd_data(argv[1], std::ios::in | std::ios::binary);

    if(fd_data.fail())
    {
	std::cout << "error open file: " << argv[1] << std::endl;

	return EXIT_SUCCESS;
    }

    MKDIR(argv[2]);

    u16 count;
    u16 total = 0;
    fd_data.read(reinterpret_cast<char *>(&count), sizeof(u16));
    SwapLE16(count);

    char buf[AGGSIZENAME];
    std::vector<aggfat_t> vector(count);

    for(u16 cur = 0; cur < count; ++cur)
    {
	aggfat_t & fat = vector[cur];

	fd_data.seekg(-AGGSIZENAME * (count - cur), std::ios_base::end);
        fd_data.read(buf, AGGSIZENAME);

        for(u8 ii = 0; ii < AGGSIZENAME; ++ii) buf[ii] = tolower(buf[ii]);

        fat.name = buf;

	fd_data.seekg(sizeof(u16) + cur * (3 * sizeof(u32)), std::ios_base::beg);
	fd_data.seekg(sizeof(u32), std::ios_base::cur);

        fd_data.read(reinterpret_cast<char *>(&fat.offset), sizeof(u32));
	SwapLE32(fat.offset);

        fd_data.read(reinterpret_cast<char *>(&fat.size), sizeof(u32));
        SwapLE32(fat.size);

	fd_data.seekg(fat.offset, std::ios_base::beg);

	char *body = new char[fat.size];
        fd_data.read(body, fat.size);

	std::string full_name(argv[2]);
	full_name += SEPARATOR;
	full_name += fat.name;

	std::fstream fd_body(full_name.c_str(), std::ios::out | std::ios::binary);

	if(! fd_body.fail())
	{
    	    fd_body.write(body, fat.size);
	    fd_body.close();
	    
	    ++total;

	    std::cout << "extract: " << full_name << std::endl;
	}

	delete [] body;
    }

    fd_data.close();

    std::cout << "total: " << total << std::endl;

    return EXIT_SUCCESS;
}
bool Maps::FileInfo::ReadMP2(const std::string & filename)
{
    if(filename.empty()) return false;

    Reset();

    std::ifstream fd(filename.c_str(), std::ios::binary);

    if(!fd.is_open())
    {
	DEBUG(DBG_GAME, DBG_WARN, "file not found " << filename);
	return false;
    }

    file = filename;
    kingdom_colors = 0;
    allow_human_colors = 0;
    allow_comp_colors = 0;
    rnd_races = 0;
    localtime = 0;

    u8  byte8;
    u16 byte16;
    u32 byte32;

    // magic byte
    fd.read(reinterpret_cast<char *>(&byte32), sizeof(byte32));
    SwapLE32(byte32);

    if(byte32 != 0x0000005C)
    {
	DEBUG(DBG_GAME, DBG_WARN, "incorrect maps file " << filename);
	fd.close();
	return false;
    }

    // level
    fd.read(reinterpret_cast<char *>(&byte16), sizeof(byte16));
    SwapLE16(byte16);

    switch(byte16)
    {
	case 0x00:
	    difficulty = Difficulty::EASY;
	    break;

	case 0x01:
	    difficulty = Difficulty::NORMAL;
	    break;

	case 0x02:
	    difficulty = Difficulty::HARD;
	    break;

	case 0x03:
	    difficulty = Difficulty::EXPERT;
	    break;

	default:
	    DEBUG(DBG_GAME, DBG_WARN, "incorrect difficulty" << ", maps: " << filename << ", load easy default");
	    break;
    }

    // width
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    size_w = byte8;

    // height
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    size_h = byte8;

    // kingdom color blue
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) kingdom_colors |= Color::BLUE;

    // kingdom color green
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) kingdom_colors |= Color::GREEN;

    // kingdom color red
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) kingdom_colors |= Color::RED;

    // kingdom color yellow
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) kingdom_colors |= Color::YELLOW;

    // kingdom color orange
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) kingdom_colors |= Color::ORANGE;

    // kingdom color purple
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) kingdom_colors |= Color::PURPLE;

    // allow human blue
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_human_colors |= Color::BLUE;

    // allow human green
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_human_colors |= Color::GREEN;

    // allow human red
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_human_colors |= Color::RED;

    // allow human yellow
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_human_colors |= Color::YELLOW;

    // allow human orange
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_human_colors |= Color::ORANGE;

    // allow human purple
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_human_colors |= Color::PURPLE;

    // allow comp blue
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_comp_colors |= Color::BLUE;

    // allow comp green
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_comp_colors |= Color::GREEN;

    // allow comp red
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_comp_colors |= Color::RED;

    // allow comp yellow
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_comp_colors |= Color::YELLOW;

    // allow comp orange
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_comp_colors |= Color::ORANGE;

    // allow comp purple
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    if(byte8) allow_comp_colors |= Color::PURPLE;

    // kingdom count
    // fd.seekg(0x1A, std::ios_base::beg);
    // fd.read(&byte8, 1);

    // wins
    fd.seekg(0x1D, std::ios_base::beg);
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    conditions_wins = byte8;

    // data wins
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    wins1 = byte8;
    // data wins
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    wins2 = byte8;
    // data wins
    fd.read(reinterpret_cast<char *>(&byte16), 2);
    SwapLE16(byte16);
    wins3 = byte16;
    // data wins
    fd.seekg(0x2c, std::ios_base::beg);
    fd.read(reinterpret_cast<char *>(&byte16), 2);
    SwapLE16(byte16);
    wins4 = byte16;

    // loss
    fd.seekg(0x22, std::ios_base::beg);
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    conditions_loss = byte8;
    // data loss
    fd.read(reinterpret_cast<char *>(&byte16), 2);
    SwapLE16(byte16);
    loss1 = byte16;
    // data loss
    fd.seekg(0x2e, std::ios_base::beg);
    fd.read(reinterpret_cast<char *>(&byte16), 2);
    SwapLE16(byte16);
    loss2 = byte16;

    // start with hero
    fd.seekg(0x25, std::ios_base::beg);
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    with_heroes = 0 == byte8;

    // race color
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    races[0] = ByteToRace(byte8);
    if(Race::RAND == races[0]) rnd_races |= Color::BLUE;
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    races[1] = ByteToRace(byte8);
    if(Race::RAND == races[1]) rnd_races |= Color::GREEN;
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    races[2] = ByteToRace(byte8);
    if(Race::RAND == races[2]) rnd_races |= Color::RED;
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    races[3] = ByteToRace(byte8);
    if(Race::RAND == races[3]) rnd_races |= Color::YELLOW;
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    races[4] = ByteToRace(byte8);
    if(Race::RAND == races[4]) rnd_races |= Color::ORANGE;
    fd.read(reinterpret_cast<char *>(&byte8), 1);
    races[5] = ByteToRace(byte8);
    if(Race::RAND == races[5]) rnd_races |= Color::PURPLE;

    // name
    char bufname[LENGTHNAME];
    fd.seekg(0x3A, std::ios_base::beg);
    fd.read(bufname, LENGTHNAME);
    bufname[LENGTHNAME - 1] = 0;
    name = Game::GetEncodeString(bufname);

    // description
    char bufdescription[LENGTHDESCRIPTION];
    fd.seekg(0x76, std::ios_base::beg);
    fd.read(bufdescription, LENGTHDESCRIPTION);
    bufdescription[LENGTHDESCRIPTION - 1] = 0;
    description = Game::GetEncodeString(bufdescription);

    fd.close();
    
    //fill unions
    if(4 == conditions_wins)
    {
	u8 side1 = 0;
	u8 side2 = 0;

	const Colors colors(kingdom_colors);

	for(Colors::const_iterator
	    it = colors.begin(); it != colors.end(); ++it)
	{
	    if(Color::GetIndex(*it) < wins3)
		side1 |= *it;
	    else
		side2 |= *it;
	}

	for(u8 ii = 0; ii < KINGDOMMAX; ++ii)
	{
	    u8 cl = ByteToColor(ii);
	
	    if(side1 & cl)
		unions[ii] = side1;
	    else
	    if(side2 & cl)
		unions[ii] = side2;
	    else
		unions[ii] = cl;
	}
    }

    return true;
}