Exemplo n.º 1
0
/*-----------------------------------------------------------------------------
    Name        : spTitleFind
    Description : Finds the title of a mission file (on first line, in brackets)
    Inputs      : directory, fileName - combine to form path to the file.
    Outputs     : Allocated memory for and duplicates a string.
    Return      : Pointer to title string.
----------------------------------------------------------------------------*/
char *spTitleFind(char *directory, char *fileName)
{
    char string[256], fullName[_MAX_PATH];
    filehandle handle;
    sdword status;

    memStrncpy(fullName, directory, _MAX_PATH - 1);
    strcat(fullName, fileName);
    strcat(fullName,"\\");
    strcat(fullName, fileName);
    strcat(fullName,".level");
    handle = fileOpen(fullName, FF_TextMode|FF_ReturnNULLOnFail);
    if (!handle)
    {
        return NULL;
    }

    for (;;)
    {
        status = fileLineRead(handle,string,256);

        if (status == FR_EndOfFile)
        {
            break;
        }

        if ((string[0] == '\n') || (string[0] == '/') || (string[0] == ';') || (string[0] == ' '))
        {
            continue;
        }

        if (strlen(string) >= 3 && *string == '[' && string[strlen(string) - 1] == ']')
        {
            fileClose(handle);
            string[strlen(string) - 1] = 0;
            return(memStringDupe(&string[1]));
        }
    }

    fileClose(handle);

    return memStringDupe(fullName);
}
Exemplo n.º 2
0
void hrChooseRandomBitmap(char *pFilenameBuffer)
{
#ifdef _WIN32
    struct _finddata_t  FindData;
    long hFind;
#else
    DIR *dp;
    struct dirent* dir_entry;
    FILE* fp;
#endif
    filehandle handle;
    long userScreenShotCount = 0, BigFileCount = 0,
         chosenFileIndex = 0, currentFileIndex = 0, Result;
    char BigName[PATH_MAX], CurDir[PATH_MAX], NewDir[PATH_MAX];

    // Remember the current directory
    getcwd(CurDir, PATH_MAX);

    NewDir[0] = 0;
    strcpy(NewDir, filePathPrepend("ScreenShots", FF_UserSettingsPath));

    // Prefer user screenshots over pre-saved ones; they're more likely to be 
    // at a higher resolution, not to mention more interesting...

    // Switch to the screenshots directory and count the ones in there
    /*SetCurrentDirectory(NewDir);*/
    chdir(NewDir);
#ifdef _WIN32
    hFind = _findfirst("*.jpg", &FindData);
    if(hFind != -1)
    {
        do {
            if( ((FindData.attrib & _A_SUBDIR) == 0) &&
                ((FindData.attrib & _A_HIDDEN) == 0) )
                userScreenShotCount++;
        } while (_findnext(hFind, &FindData) == 0);
        _findclose(hFind);
    }
#else
    dp = opendir(".");

    if (dp)
    {
        unsigned int dir_str_len;

        while ((dir_entry = readdir(dp)))
        {
            if (dir_entry->d_name[0] == '.')
                continue;
            dir_str_len = strlen(dir_entry->d_name);
            if (dir_str_len < 4 ||
                strcasecmp(dir_entry->d_name + dir_str_len - 4, ".jpg"))
                continue;

            /* See if the current process can actually open the file (simple
               check for read permissions and if it's a directory). */
            if (!(fp = fopen(dir_entry->d_name, "rb")))
                continue;
            fclose(fp);

            userScreenShotCount++;
        }

        closedir(dp);
    }
#endif

    if (userScreenShotCount > 0)
    {
        chosenFileIndex = (utyTimerLast % 32777) % userScreenShotCount;

#ifdef _WIN32
        hFind = _findfirst("*.jpg", &FindData);
        if(hFind != -1)
        {
            do {
                if( ((FindData.attrib & _A_SUBDIR) == 0) &&
                    ((FindData.attrib & _A_HIDDEN) == 0) )
                {
                    if(currentFileIndex == chosenFileIndex)
                    {
                        _findclose(hFind);
                        strcpy(pFilenameBuffer, filePathPrepend("ScreenShots\\", FF_UserSettingsPath));
                        strcat(pFilenameBuffer, FindData.name);
                        break;
                    }
                    else
                        currentFileIndex++;
                }
            } while (_findnext(hFind, &FindData) == 0);
        }
#else
        dp = opendir(".");

        if (dp)
        {
            unsigned int dir_str_len;

            while ((dir_entry = readdir(dp)))
            {
                if (dir_entry->d_name[0] == '.')
                    continue;
                dir_str_len = strlen(dir_entry->d_name);
                if (dir_str_len < 4 ||
                    strcasecmp(dir_entry->d_name + dir_str_len - 4, ".jpg"))
                    continue;
                if (!(fp = fopen(dir_entry->d_name, "rb")))
                    continue;
                fclose(fp);

                if (currentFileIndex != chosenFileIndex)
                {
                    currentFileIndex++;
                    continue;
                }

                strcat(pFilenameBuffer, filePathPrepend("ScreenShots/", FF_UserSettingsPath));
                strcat(pFilenameBuffer, dir_entry->d_name);
                
                break;
            }

            closedir(dp);
        }
#endif
    }
    else // look in the big file for fallback images
    {   
        // First, find screen shots listed in the BigFile
#ifdef _WIN32
        handle = fileOpen("ScreenShots\\ShotList.script", FF_ReturnNULLOnFail | FF_TextMode);
#else
        handle = fileOpen("ScreenShots/ShotList.script", FF_ReturnNULLOnFail | FF_TextMode);
#endif
        if(handle)
        {
            do {
                Result = fileLineRead(handle, BigName, 512);

                if(Result != FR_EndOfFile && Result > 0)    // Found one!
                    BigFileCount++;

            } while(Result != FR_EndOfFile);

            fileClose(handle);
        }
        
        if (BigFileCount > 0)
        {
            chosenFileIndex = (utyTimerLast % 32777) % BigFileCount;

#ifdef _WIN32
            handle = fileOpen("ScreenShots\\ShotList.script", FF_ReturnNULLOnFail | FF_TextMode);
#else
            handle = fileOpen("ScreenShots/ShotList.script", FF_ReturnNULLOnFail | FF_TextMode);
#endif
            if(handle)
            {
                do {
                    Result = fileLineRead(handle, BigName, 512);

                    if(Result != FR_EndOfFile && Result > 0)    // Found one!
                        chosenFileIndex--;

                } while( (chosenFileIndex >= 0) && (Result != FR_EndOfFile));
#ifdef _WIN32
                strcpy(pFilenameBuffer, "ScreenShots\\");
#else
                strcpy(pFilenameBuffer, "ScreenShots/");
#endif
                strcat(pFilenameBuffer, BigName);
            }
        }
    }

    // Did we find any at all?
    if(userScreenShotCount == 0 && BigFileCount == 0)
    {
        pFilenameBuffer[0] = 0;
    }

    /*SetCurrentDirectory(CurDir);*/
    chdir(CurDir);
}
Exemplo n.º 3
0
/*-----------------------------------------------------------------------------
    Name        : spTitleListLoad
    Description : Scans the missions directory and loads in the titles of all
                    the mission files available.
    Inputs      : void
    Outputs     : Fills in spScenarios and spNumberScenarios
    Return      : void
----------------------------------------------------------------------------*/
void spTitleListLoad(void)
{
#if !(defined(CGW) || defined (Downloadable) || defined(DLPublicBeta) || defined(OEM))
    struct _finddata_t find;
    sdword handle, startHandle;
#endif
    sdword index, numplayers;
    char fileName[_MAX_PATH], nameBuffer[_MAX_PATH];
#if MAIN_Password
    char upperNameBuffer[_MAX_PATH];
#endif
    char bitmapfileName[_MAX_PATH];
    char *pString;
    char *title;
    filehandle scriptFile;

#if defined(CGW) || defined(Downloadable) || defined(DLPublicBeta)
    scriptFile = fileOpen("DemoMissions.script", FF_TextMode);
#else
    //oem has all missions!
    scriptFile = fileOpen("multiPlayerMissions.script", FF_TextMode);
#endif
    dbgAssert(scriptFile != 0);

    while (fileLineRead(scriptFile, nameBuffer, _MAX_PATH) != FR_EndOfFile)
    {
        if (nameBuffer[0] == ';' || (nameBuffer[0] == '/' && nameBuffer[1] == '/'))
        {
            continue;
        }
        numplayers = 0;
        for (pString = nameBuffer; *pString != 0; pString++)
        {                                                   //search for a numeral character
            if (strchr("0123456789", *pString) != NULL)
            {                                               //if this is a numeral
                numplayers = atoi(pString);
                memset(fileName, 0, _MAX_PATH);
                memStrncpy(fileName, nameBuffer, pString - nameBuffer + 1);//copy the start of the string
                memStrncpy(bitmapfileName, nameBuffer, pString - nameBuffer + 1);//copy the start of the string
                strcat(fileName, "%d");                     //make something like:
                strcat(fileName, pString + 1);              //'StdGame%d.level'
            }
        }

        if (numplayers == 0)
        {
            goto alreadyLoaded;
        }

        title = spTitleFind("MultiPlayer\\", nameBuffer);
        if (title == NULL)
        {
            goto alreadyLoaded;                             //break-continue
        }
#if MAIN_Password
        if (!mainEnableSpecialMissions)
        {                                                   //if this is an "off-limits" mission
            strcpy(upperNameBuffer, title);
            _strupr(upperNameBuffer);
            if (strstr(upperNameBuffer, "ALL"))
            {
                memFree(title);
                goto alreadyLoaded;                         //break-continue
            }
        }
#endif //MAIN_Password

        for (index = 0; index < spNumberScenarios; index++)
        {
            if (!_stricmp(spScenarios[index].fileSpec, fileName))
            {                                               //if matching file specs,
                // matches, but we should update max number of player if necessary
                if (numplayers > spScenarios[index].maxplayers) spScenarios[index].maxplayers = numplayers;
                if (numplayers < spScenarios[index].minplayers) spScenarios[index].minplayers = numplayers;
                memFree(title);
                goto alreadyLoaded;                         //break-continue
            }
        }

        if (spNumberScenarios >= spScenarioListLength)
        {
            spScenarioListLength += SP_ScenarioListGrowth;
            spScenarios = memRealloc(spScenarios, spScenarioListLength * sizeof(spscenario), "spScenarios", NonVolatile);
        }
        dbgAssert(spNumberScenarios < spScenarioListLength);
        spScenarios[spNumberScenarios].fileSpec = memStringDupe(fileName);
        spScenarios[spNumberScenarios].bitmapfileSpec = memStringDupe(bitmapfileName);
        spScenarios[spNumberScenarios].title = title;
        spScenarios[spNumberScenarios].maxplayers = numplayers;
        spScenarios[spNumberScenarios].minplayers = numplayers;
        spNumberScenarios++;

alreadyLoaded:;
    }
    fileClose(scriptFile);

#if !(defined(CGW) || defined (Downloadable) || defined(DLPublicBeta) || defined(OEM))
    startHandle = handle = _findfirst(filePathPrepend("MultiPlayer\\*.", 0), &find);

    while (handle != -1)
    {
        if (find.name[0] == '.')
        {
            goto alreadyLoadedFromFileSystem;
        }
        fileName[0] = 0;
        numplayers = 0;
        for (pString = find.name; *pString != 0; pString++)
        {                                                   //search for a numeral character
            if (strchr("0123456789", *pString) != NULL)
            {                                               //if this is a numeral
                numplayers = atoi(pString);
                memset(fileName, 0, _MAX_PATH);
                strncpy(fileName, find.name, pString - find.name);//copy the start of the string
                memStrncpy(bitmapfileName, find.name, pString - find.name + 1);//copy the start of the string
                strcat(fileName, "%d");                     //make something like:
                strcat(fileName, pString + 1);              //'StdGame%d.level'
            }
        }
        if (numplayers == 0)
        {
            goto alreadyLoadedFromFileSystem;
        }

        if (fileName[0] == 0)
        {
            goto alreadyLoadedFromFileSystem;
        }

        title = spTitleFind("MultiPlayer\\", find.name);
        if (title == NULL)
        {
            goto alreadyLoadedFromFileSystem;
        }
#if MAIN_Password
        if (!mainEnableSpecialMissions)
        {                                                   //if this is an "off-limits" mission
            strcpy(upperNameBuffer, title);
            _strupr(upperNameBuffer);
            if (strstr(upperNameBuffer, "ALL"))
            {
                memFree(title);
                goto alreadyLoadedFromFileSystem;           //break-continue
            }
        }
#endif //MAIN_Password

        for (index = 0; index < spNumberScenarios; index++)
        {
            if (!_stricmp(spScenarios[index].fileSpec, fileName))
            {                                               //if matching file specs,
                // matches, but we should update max number of player if necessary
                if (numplayers > spScenarios[index].maxplayers) spScenarios[index].maxplayers = numplayers;
                if (numplayers < spScenarios[index].minplayers) spScenarios[index].minplayers = numplayers;
                memFree(title);
                goto alreadyLoadedFromFileSystem;                         //break-continue
            }
        }

        if (spNumberScenarios >= spScenarioListLength)
        {
            spScenarioListLength += SP_ScenarioListGrowth;
            spScenarios = memRealloc(spScenarios, spScenarioListLength * sizeof(spscenario), "spScenarios", NonVolatile);
        }
        dbgAssert(spNumberScenarios < spScenarioListLength);
        spScenarios[spNumberScenarios].fileSpec = memStringDupe(fileName);
        spScenarios[spNumberScenarios].bitmapfileSpec = memStringDupe(bitmapfileName);
        spScenarios[spNumberScenarios].title = title;
        spScenarios[spNumberScenarios].maxplayers = numplayers;
        spScenarios[spNumberScenarios].minplayers = numplayers;
        spNumberScenarios++;

alreadyLoadedFromFileSystem:;
        handle = _findnext(startHandle, &find);
    }
#endif //defined(CGW) || defined (Downloadable) || defined(DLPublicBeta) || defined(OEM)

    dbgAssert(spNumberScenarios > 0);
    if (spNumberScenarios > 1)
    {
        //alphabetically sort the scenario list
        qsort(&spScenarios[0],spNumberScenarios,sizeof(spscenario),compareScenariosCB);
    }

    //find the default scenario's index
    if (spCurrentSelected == 0)
    {                                                       //if it's not already selected
        spCurrentSelected = spScenarioFind(DefaultScenario);
    }
}