示例#1
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;
}
示例#2
0
bool GmExe::Load(const std::string& filename, Gmk* gmk, unsigned int ver) {
    if (!gmk)
        return false;

    // Set handles
    version = ver;
    gmkHandle = gmk;
    exeFilename = filename;

    // Try to load the EXE
    if (!exeHandle->Load(filename))
        return false;

    // Now we need to check the ver so we know how to treat the EXE
    switch(version) {
    case 800: // GM8.0 EXE
    {
        std::cout << "Detected GM8.0 game!" << std::endl;

        Gm80* gm80 = new Gm80();

        if (!gm80->FindGameData(exeHandle))
            return false;

        delete gm80;
        break;
    }

    case 810: // GM8.1 EXE
    {
        std::cout << "Detected GM8.1 game!" << std::endl;

        Gm81* gm81 = new Gm81();

        if (!gm81->FindGameData(exeHandle))
            return false;

        gm81->Decrypt(exeHandle);

        delete gm81;
        break;
    }

    default: // Auto-detect version
        Gm81* gm81 = new Gm81();
        Gm80* gm80 = new Gm80();

        // Attempt to detect GM8.0 first
        if (gm80->FindGameData(exeHandle)) {
            version = 800;
            std::cout << "Detected GM8.0 game!" << std::endl;
            break;
        }

        // Attempt to detect GM8.1 next
        if (gm81->FindGameData(exeHandle)) {
            version = 810;
            std::cout << "Detected GM8.1 game!" << std::endl;

            if (!gm81->Decrypt(exeHandle)) {
                std::cout << "[Error  ] Failed to decrypt game data!" << std::endl;
                return false;
            }

            break;
        }

        std::cout << "Unknown game version!" << std::endl;

        delete gm80;
        delete gm81;
        return false;
    }

    // Read header
    std::cout << "Reading header..." << std::endl;
    if (version == 800)
        exeHandle->SkipDwords(2);	// Version, Debug flag
    else if (version == 810)
        exeHandle->SkipDwords(3);	// Null Magic, Null Version, Debug flag
    else
        return false;

    // Read settings
    std::cout << "Reading settings..." << std::endl;
    if (!ReadSettings())
        return false;

    // Read wrapper
    std::cout << "Reading d3d wrapper..." << std::endl;
    if (!ReadWrapper())
        return false;

    // Decrypt main game data paragraph
    std::cout << "Decrypting game data..." << std::endl;
    if (!DecryptGameData())
        return false;

    // Skip yet another garbage field -- and the pro flag, silly pro flag
    exeHandle->SkipDwords(exeHandle->ReadDword() + 1);

    gmkHandle->gameId = exeHandle->ReadDword();

    for(int i = 0; i < 4; i++)
        exeHandle->ReadDword();

    // Read extensions
    std::cout << "Reading extensions..." << std::endl;
    if (!ReadExtensions())
        return false;

    // Read triggers
    std::cout << "Reading triggers..." << std::endl;
    if (!ReadTriggers())
        return false;

    // Read constants
    std::cout << "Reading constants..." << std::endl;
    if (!ReadConstants())
        return false;

    // Read sounds
    std::cout << "Reading sounds..." << std::endl;
    if (!ReadSounds())
        return false;

    // Read sprites
    std::cout << "Reading sprites..." << std::endl;
    if (!ReadSprites())
        return false;

    // Read backgrounds
    std::cout << "Reading backgrounds..." << std::endl;
    if (!ReadBackgrounds())
        return false;

    // Read paths
    std::cout << "Reading paths..." << std::endl;
    if (!ReadPaths())
        return false;

    // Read scripts
    std::cout << "Reading scripts..." << std::endl;
    if (!ReadScripts())
        return false;

    // Read fonts
    std::cout << "Reading fonts..." << std::endl;
    if (!ReadFonts())
        return false;

    // Read timelines
    std::cout << "Reading timelines..." << std::endl;
    if (!ReadTimelines())
        return false;

    // Read objects
    std::cout << "Reading objects..." << std::endl;
    if (!ReadObjects())
        return false;

    // Read rooms
    std::cout << "Reading rooms..." << std::endl;
    if (!ReadRooms())
        return false;

    // Read last ID of object/tile placed
    gmkHandle->lastInstance = exeHandle->ReadDword();
    gmkHandle->lastTile = exeHandle->ReadDword();

    // Read includes
    std::cout << "Reading include files..." << std::endl;
    if (!ReadIncludes())
        return false;

    // Read game information
    std::cout << "Reading game information..." << std::endl;
    if (!ReadGameInformation())
        return false;

    // Ignore library initialization code
    std::cout << "Reading library initialization code..." << std::endl;
    exeHandle->SkipDwords(1);

    unsigned int i = exeHandle->ReadDword();
    for(; i; i--)
        exeHandle->iPosition += exeHandle->ReadDword();

    // Read room order
    std::cout << "Reading room order..." << std::endl;
    exeHandle->SkipDwords(1);

    i = exeHandle->ReadDword();
    for(; i; i--)
        gmkHandle->roomExecutionOrder.push_back(exeHandle->ReadDword());

    // Correct room order
    ResourceTree* tmpTree = new ResourceTree();
    tmpTree->group = RCT_GROUP_ROOMS;
    tmpTree->name = "Rooms";
    tmpTree->index = 0;
    tmpTree->status = 1;

    for(size_t a = 0; a < gmkHandle->roomExecutionOrder.size(); a++) {
        ResourceTree* rcItem = new ResourceTree();

        rcItem->name = gmkHandle->rooms[gmkHandle->roomExecutionOrder[a]]->name;
        rcItem->group = RCT_GROUP_ROOMS;
        rcItem->index = gmkHandle->roomExecutionOrder[a];
        rcItem->status = 3;

        tmpTree->contents.push_back(rcItem);
    }

    delete gmkHandle->resourceTree[RCT_ID_ROOMS];
    gmkHandle->resourceTree[RCT_ID_ROOMS] = tmpTree;

    return true;
}