Ejemplo n.º 1
0
MF_API const char* MFCallstack_GetCallstackString()
{
#if defined(_MFCALLSTACK)
    char callstack[2048] = "";
    size_t bufferUsed = 0;

    for(int a = gCallDepth-1; a >= 0; a--)
    {
        const char *pTemp = MFStr("  %-32s\n",pCallstack[a]);
        size_t tempLen = MFString_Length(pTemp);
//		char *pTemp = MFStr("  %-32s\t(%s)%s\n",Callstack[a].c_str(),ModuleName(pFunc->pStats->pModuleName),pFunc->pComment ? MFStr(" [%s]",pFunc->pComment) : "");
        if(bufferUsed + tempLen < sizeof(callstack) - 1)
        {
            MFString_Cat(callstack, pTemp);
            bufferUsed += tempLen;
        }
    }

    return MFStr(callstack);
#else
    return "Callstack not available in this build.";
#endif
}
Ejemplo n.º 2
0
int F3DFile::ReadMD3(const char *pFilename)
{
	pModel = this;

	char *pBuffer = NULL;

	// open .pk3 file
	unzFile zipFile = unzOpen(pFilename);

	// iterate files in zip
	int zipFileIndex = unzGoToFirstFile(zipFile);

	while(zipFileIndex != UNZ_END_OF_LIST_OF_FILE)
	{
		MFDebug_Assert(zipFileIndex == UNZ_OK, "Error in .zip file.");

		char fileName[256];

		unz_file_info fileInfo;
		unzGetCurrentFileInfo(zipFile, &fileInfo, fileName, 256, NULL, 0, NULL, 0);

		int filenameLen = MFString_Length(fileName);

		if(!MFString_CaseCmp(".md3", &fileName[MFMax(filenameLen - 4, 0)]))
		{
			// read fle from zip
			pBuffer = (char*)malloc(fileInfo.uncompressed_size);

			unzOpenCurrentFile(zipFile);
			uint32 bytesRead = unzReadCurrentFile(zipFile, pBuffer, fileInfo.uncompressed_size);
			unzCloseCurrentFile(zipFile);

			MFDebug_Assert(bytesRead == fileInfo.uncompressed_size, "Incorrect number of bytes read..");

			// get subobject and model name..
			int a, b;
			for(a = filenameLen - 4; a >= 0; a--)
			{
				if(fileName[a] == '/' || fileName[a] == '\\')
					break;
			}

			char *pSubobjectName = &fileName[a+1];

			for(b = a-1; b >= 0; b--)
			{
				if(fileName[b] == '/' || fileName[b] == '\\')
					break;
			}

			MFString_Copy(pModel->name, &fileName[b+1]);
			pModel->name[a-b-1] = 0;

			// search for skin file
			char skinFilename[256];
			MFString_Copy(skinFilename, fileName);
			skinFilename[MFString_Length(skinFilename) - 4] = 0;
			MFString_Cat(skinFilename, "_");
			MFString_Cat(skinFilename, pModel->name);
			MFString_Cat(skinFilename, ".skin");

			// attempt to read skin..
			char *pSkinFile = NULL;

			zipFileIndex = unzLocateFile(zipFile, skinFilename, 0);

			if(zipFileIndex != UNZ_END_OF_LIST_OF_FILE)
			{
				// read skin file from zip
				unz_file_info skinInfo;
				char skinName[256];

				unzGetCurrentFileInfo(zipFile, &skinInfo, skinName, 256, NULL, 0, NULL, 0);

				pSkinFile = (char*)malloc(skinInfo.uncompressed_size + 1);
				pSkinFile[skinInfo.uncompressed_size] = 0;

				unzOpenCurrentFile(zipFile);
				uint32 skinBytesRead = unzReadCurrentFile(zipFile, pSkinFile, skinInfo.uncompressed_size);
				unzCloseCurrentFile(zipFile);

				MFDebug_Assert(skinBytesRead == skinInfo.uncompressed_size, "Incorrect number of bytes read..");
			}

			zipFileIndex = unzLocateFile(zipFile, fileName, 0);

			// parse MD3
			ParseMD3File(pBuffer, fileInfo.uncompressed_size, pSubobjectName, pSkinFile);

			// free file
			free(pBuffer);
			pBuffer = NULL;
		}
/*
		else if(!MFString_CaseCmp(".skin", &fileName[Max(filenameLen - 5, 0)]))
		{
			int a, b;
			char skinName[256];

			// get subobject and model name
			for(a = filenameLen - 5; a >= 0; a--)
			{
				if(fileName[a] == '/' || fileName[a] == '\\')
					break;
			}

			char *pSkinName = &fileName[a+1];

			for(b = a-1; b >= 0; b--)
			{
				if(fileName[b] == '/' || fileName[b] == '\\')
					break;
			}

			MFString_Copy(skinName, &fileName[b+1]);
			skinName[a-b-1] = 0;

			pSkinName = strchr(pSkinName, '_');
			DBGASSERT(pSkinName, "Incorrectly named .skin file in .pk3");
			++pSkinName;

			// check that this is the default skin for this model
			if(!MFString_CaseCmpN(pSkinName, skinName, MFString_Length(skinName)))
			{
				// read material file from zip
				pBuffer = (char*)malloc(fileInfo.uncompressed_size);

				unzOpenCurrentFile(zipFile);
				uint32 bytesRead = unzReadCurrentFile(zipFile, pBuffer, fileInfo.uncompressed_size);
				unzCloseCurrentFile(zipFile);

				// parse .skin file

				free(pBuffer);
				pBuffer = NULL;
			}
			else
			{
				// we have an alternate skin.. do nothing..
			}
		}
*/

		zipFileIndex = unzGoToNextFile(zipFile);
	}

	// close .pk3 file
	unzClose(zipFile);

	return 0;
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
    MFPlatform platform = FP_Unknown;
    MFWaveFormat targetFormat = MFWF_PCM_s16;

    char fileName[256] = "";
    char outFile[256] = "";

    int a;

    // process command line
    for(a=1; a<argc; a++)
    {
        if(argv[a][0] == '-' || argv[a][0] == '/')
        {
            for(int b=0; b<FP_Max; b++)
            {
                if(!MFString_CaseCmp(&argv[a][1], MFSystem_GetPlatformString(b)))
                {
                    platform = (MFPlatform)b;
                    break;
                }
            }

            if(!MFString_CaseCmp(&argv[a][1], "v") || !MFString_CaseCmp(&argv[a][1], "version"))
            {
                // print version
                return 0;
            }
        }
        else
        {
            if(!fileName[0])
                MFString_Copy(fileName, argv[a]);
            else if(!outFile[0])
                MFString_Copy(outFile, argv[a]);
        }
    }

    if(platform == FP_Unknown)
    {
        LOGERROR("No platform specified...\n");
        return 1;
    }

    if(!fileName[0])
    {
        LOGERROR("No file specified...\n");
        return 1;
    }

    if(!outFile[0])
    {
        // generate output filename
        MFString_Copy(outFile, fileName);
        for(int i=MFString_Length(outFile); --i; )
        {
            if(outFile[i] == '.')
            {
                outFile[i] = 0;
                break;
            }
        }

        MFString_Cat(outFile, ".snd");
    }

    // load image
    char *pBuffer;

    FILE *pFile = fopen(fileName, "rb");
    if(!pFile)
    {
        LOGERROR("Couldnt open source file '%s' for reading.", fileName);
        return 1;
    }

    fseek(pFile, 0, SEEK_END);
    int bytes = ftell(pFile) + 1;
    fseek(pFile, 0, SEEK_SET);

    pBuffer = (char*)malloc(bytes);
    fread(pBuffer, bytes, 1, pFile);
    fclose(pFile);

    FILE *pOutFile = fopen(outFile, "wb");

    if(!pOutFile)
    {
        LOGERROR("Couldnt open output file '%s' for writing.", outFile);
        return 2;
    }

    // parse the sound file...
    WAVHeader *pHeader = (WAVHeader*)pBuffer;

    if(pHeader->RIFF != MFMAKEFOURCC('R', 'I', 'F', 'F') || pHeader->WAVE != MFMAKEFOURCC('W', 'A', 'V', 'E'))
    {
        LOGERROR("Source file '%s' is not a wave file.", fileName);
        return 1;
    }

    WAVChunk *pChunk = (WAVChunk*)&pHeader[1];

    WAVFormatChunk *pFormat = NULL;
    WAVDataChunk *pData = NULL;

    while((char*)pChunk - pBuffer < bytes && bytes - ((char*)pChunk - pBuffer) >= sizeof(WAVChunk))
    {
        if(pChunk->id == MFMAKEFOURCC('f', 'm', 't', ' '))
        {
            pFormat = (WAVFormatChunk*)pChunk;
        }
        else if(pChunk->id == MFMAKEFOURCC('d', 'a', 't', 'a'))
        {
            pData = (WAVDataChunk*)pChunk;
        }

        pChunk = (WAVChunk*)((char*)pChunk + sizeof(WAVChunk) + ((pChunk->size+1) & ~1));
    }

    if(!pFormat || !pData)
    {
        LOGERROR("WAVE file '%s' has no format or data chunks.", fileName);
        return 2;
    }

    // we only output 1 stream for now..
    int numStreams = 1;
    int numSamples = ((pData->size*8) / ((pFormat->wBitsPerSample + 7) & ~7)) / pFormat->nChannels;

    int fileSize = sizeof(MFSoundTemplate) + sizeof(char*)*numStreams + bytesPerSample[targetFormat]*pFormat->nChannels*numSamples*numStreams;
    char *pSndFile = (char*)MFHeap_Alloc(fileSize);

    MFSoundTemplate *pTemplate = (MFSoundTemplate*)pSndFile;
    pTemplate->ppStreams = (char **)&pTemplate[1];
    char *pWaveData = (char*)&pTemplate->ppStreams[numStreams];
    for(int a=0; a<numStreams; a++)
    {
        pTemplate->ppStreams[a] = pWaveData;
        pWaveData += bytesPerSample[targetFormat]*pFormat->nChannels*numSamples;
    }

    pTemplate->magic = MFMAKEFOURCC('S', 'N', 'D', '1');
    pTemplate->flags = 0;
    pTemplate->format = targetFormat;
    pTemplate->numSamples = numSamples;
    pTemplate->numStreams = numStreams;
    pTemplate->numChannels = pFormat->nChannels;
    pTemplate->bitsPerSample = bytesPerSample[targetFormat]*8;
    pTemplate->sampleRate = pFormat->nSamplesPerSec;

    // write wave data into file
    if(pTemplate->bitsPerSample == pFormat->wBitsPerSample)
    {
        for(int a=0; a<pTemplate->numStreams; a++)
            MFCopyMemory(pTemplate->ppStreams[a], pData->data, bytesPerSample[targetFormat]*pTemplate->numChannels*pTemplate->numSamples);
    }
    else
    {
        // do a format conversion
        LOGERROR("Format conversion not written.");
        return 3;
    }

    // fix up template
    for(int a=0; a<numStreams; a++)
        MFFixUp(pTemplate->ppStreams[a], pTemplate, false);
    MFFixUp(pTemplate->ppStreams, pTemplate, false);

    // write to disk
    fwrite(pSndFile, fileSize, 1, pOutFile);
    fclose(pOutFile);

    printf("> %s\n", outFile);

    return 0;
}