Пример #1
0
KuroLRC* LoadKuroLRC(const char *uri)
{
	char *utf8Buf = NULL;
	int utf8Len, res;
	gchar *encryptBuf = NULL;
	gchar *decryptBuf = NULL;
	guint encryptLen, decryptLen;
	KuroLRC *lrc = NULL;
	
	/* Check to see if it is the remote uri??? */
	ZInfo1(DBG_INIT, "NOTE: Loading LRC file->%s", uri);
	if(zg_uri_validate(uri)) {
		res = KuroLRCLoadRemote(uri, &encryptBuf, &encryptLen);
		if(res != ZAPP_SUCCESS) {
			ZError(DBG_INIT, "Failed to handle the remote LRC -> %s", uri);
			return NULL;
		}
	}
	else {
		res = KuroLRCLoadLocal(uri, &encryptBuf, &encryptLen);
		if(res != ZAPP_SUCCESS) {
			ZError(DBG_INIT, "Failed to handle the local LRC -> %s", uri);
			return NULL;
		}
	}
	//ZInfo1(DBG_INIT, "Buffer encryptText (%d)->\n%s", encryptLen, encryptBuf);
	
	ZInfo1(DBG_INIT, "NOTE: Decompress LRC data.");
	/* Decrypt buffer */
	DecompressData((const void *)encryptBuf, encryptLen, (void**)&decryptBuf, &decryptLen);
	if(decryptLen == 0) {
		ZError(DBG_INIT, "Failed to DecompressData LRC.");
		return NULL;
	}
	//ZInfo1(DBG_INIT, "Buffer plainText (%d)->\n%s", decryptLen, decryptBuf);
	
	ZInfo1(DBG_INIT, "NOTE: Convert LRC data to utf8.");
	/* Convert the buffer to UTF-8 */
	if(BufToUtf8(decryptBuf, decryptLen, &utf8Buf, &utf8Len) != 0) {
		ZError(DBG_INIT, "Failed to execute BufToUtf8.");
		ZFREE(decryptBuf);
		return NULL;
	}
	ZFREE(decryptBuf);
	ZInfo1(DBG_INIT, "UTF-8 plainText (%d)->\n%s", utf8Len, utf8Buf);
	
	ZInfo1(DBG_INIT, "NOTE: Parse LRC data.");
	/* Parse LRC */
	lrc = ParseKuroLRC(utf8Buf);
	if(lrc == NULL) {
		ZError(DBG_INIT,"Failed to parse LRC.");
		ZFREE(utf8Buf);
		return NULL;
	}
	ZInfo1(DBG_INIT, "NOT: LoadKuroLRC Done");
	ZFREE(utf8Buf);
	return lrc;
}
Пример #2
0
// Load sound to memory from rRES file (raylib Resource)
Sound LoadSoundFromRES(const char *rresName, int resId)
{
    // NOTE: rresName could be directly a char array with all the data!!! --> TODO
    Sound sound;
    bool found = false;

    char id[4];             // rRES file identifier
    unsigned char version;  // rRES file version and subversion
    char useless;           // rRES header reserved data
    short numRes;
    
    ResInfoHeader infoHeader;
    
    FILE *rresFile = fopen(rresName, "rb");

    if (!rresFile) TraceLog(WARNING, "[%s] Could not open raylib resource file", rresName);
    else
    {
        // Read rres file (basic file check - id)
        fread(&id[0], sizeof(char), 1, rresFile);
        fread(&id[1], sizeof(char), 1, rresFile);
        fread(&id[2], sizeof(char), 1, rresFile);
        fread(&id[3], sizeof(char), 1, rresFile);
        fread(&version, sizeof(char), 1, rresFile);
        fread(&useless, sizeof(char), 1, rresFile);
        
        if ((id[0] != 'r') && (id[1] != 'R') && (id[2] != 'E') &&(id[3] != 'S'))
        {
            TraceLog(WARNING, "[%s] This is not a valid raylib resource file", rresName);
        }
        else
        {
            // Read number of resources embedded
            fread(&numRes, sizeof(short), 1, rresFile);
            
            for (int i = 0; i < numRes; i++)
            {
                fread(&infoHeader, sizeof(ResInfoHeader), 1, rresFile);
                
                if (infoHeader.id == resId)
                {
                    found = true;

                    // Check data is of valid SOUND type
                    if (infoHeader.type == 1)   // SOUND data type
                    {
                        // TODO: Check data compression type
                        // NOTE: We suppose compression type 2 (DEFLATE - default)
                        
                        // Reading SOUND parameters
                        Wave wave;
                        short sampleRate, bps;
                        char channels, reserved;
                    
                        fread(&sampleRate, sizeof(short), 1, rresFile); // Sample rate (frequency)
                        fread(&bps, sizeof(short), 1, rresFile);        // Bits per sample
                        fread(&channels, 1, 1, rresFile);               // Channels (1 - mono, 2 - stereo)
                        fread(&reserved, 1, 1, rresFile);               // <reserved>
                                
                        wave.sampleRate = sampleRate;
                        wave.dataSize = infoHeader.srcSize;
                        wave.bitsPerSample = bps;
                        wave.channels = (short)channels;
                        
                        unsigned char *data = malloc(infoHeader.size);

                        fread(data, infoHeader.size, 1, rresFile);
                        
                        wave.data = DecompressData(data, infoHeader.size, infoHeader.srcSize);
                        
                        free(data);
                        
                        // Convert wave to Sound (OpenAL)
                        ALenum format = 0;
                        
                        // The OpenAL format is worked out by looking at the number of channels and the bits per sample
                        if (wave.channels == 1) 
                        {
                            if (wave.bitsPerSample == 8 ) format = AL_FORMAT_MONO8;
                            else if (wave.bitsPerSample == 16) format = AL_FORMAT_MONO16;
                        } 
                        else if (wave.channels == 2) 
                        {
                            if (wave.bitsPerSample == 8 ) format = AL_FORMAT_STEREO8;
                            else if (wave.bitsPerSample == 16) format = AL_FORMAT_STEREO16;
                        }
                        
                        
                        // Create an audio source
                        ALuint source;
                        alGenSources(1, &source);            // Generate pointer to audio source

                        alSourcef(source, AL_PITCH, 1);    
                        alSourcef(source, AL_GAIN, 1);
                        alSource3f(source, AL_POSITION, 0, 0, 0);
                        alSource3f(source, AL_VELOCITY, 0, 0, 0);
                        alSourcei(source, AL_LOOPING, AL_FALSE);
                        
                        // Convert loaded data to OpenAL buffer
                        //----------------------------------------
                        ALuint buffer;
                        alGenBuffers(1, &buffer);            // Generate pointer to buffer

                        // Upload sound data to buffer
                        alBufferData(buffer, format, (void*)wave.data, wave.dataSize, wave.sampleRate);

                        // Attach sound buffer to source
                        alSourcei(source, AL_BUFFER, buffer);
                        
                        // Unallocate WAV data
                        UnloadWave(wave);

                        TraceLog(INFO, "[%s] Sound loaded successfully from resource, sample rate: %i", rresName, (int)sampleRate);
                        
                        sound.source = source;
                        sound.buffer = buffer;
                    }
                    else
                    {
                        TraceLog(WARNING, "[%s] Required resource do not seem to be a valid SOUND resource", rresName);
                    }
                }
                else
                {
                    // Depending on type, skip the right amount of parameters
                    switch (infoHeader.type)
                    {
                        case 0: fseek(rresFile, 6, SEEK_CUR); break;   // IMAGE: Jump 6 bytes of parameters
                        case 1: fseek(rresFile, 6, SEEK_CUR); break;   // SOUND: Jump 6 bytes of parameters
                        case 2: fseek(rresFile, 5, SEEK_CUR); break;   // MODEL: Jump 5 bytes of parameters (TODO: Review)
                        case 3: break;   // TEXT: No parameters
                        case 4: break;   // RAW: No parameters
                        default: break;
                    }
                    
                    // Jump DATA to read next infoHeader
                    fseek(rresFile, infoHeader.size, SEEK_CUR);
                }    
            }
        }
        
        fclose(rresFile);
    }
    
    if (!found) TraceLog(WARNING, "[%s] Required resource id [%i] could not be found in the raylib resource file", rresName, resId);
    
    return sound;
}
Пример #3
0
int readVip(EmbPattern* pattern, const char* fileName)
{
    int fileLength, magicCode, numberOfStitches, numberOfColors;
    int i, attributeOffset, xOffset, yOffset, unknown, colorLength;
	unsigned char* stringVal;
    unsigned char prevByte = 0;
	int postitiveXHoopSize,postitiveYHoopSize,negativeXHoopSize,negativeYHoopSize;
    unsigned char *attributeData, *decodedColors, *attributeDataDecompressed;
    unsigned char *xData, *xDecompressed, *yData, *yDecompressed;
    FILE* file = fopen(fileName, "rb");
    if(file == 0)
    {
        /* TODO: set messages here "Error opening VIP file for read:" */
        return 0;
    }
    fseek(file, 0x0, SEEK_END);
    fileLength = ftell(file);
    fseek(file, 0x00, SEEK_SET);
    magicCode = binaryReadInt32(file);
    numberOfStitches = binaryReadInt32(file);
    numberOfColors = binaryReadInt32(file);

    postitiveXHoopSize = binaryReadInt16(file);
    postitiveYHoopSize = binaryReadInt16(file);
    negativeXHoopSize = binaryReadInt16(file);
    negativeYHoopSize = binaryReadInt16(file);

    attributeOffset = binaryReadInt32(file);
    xOffset = binaryReadInt32(file);
    yOffset = binaryReadInt32(file);

    stringVal = (unsigned char*)malloc(sizeof(unsigned char)*8);
    binaryReadBytes(file, stringVal, 8);

    unknown = binaryReadInt16(file);

	colorLength = binaryReadInt32(file);
	decodedColors = (unsigned char *)malloc(numberOfColors*4);
    for(i = 0; i < numberOfColors*4; ++i)
    {
        unsigned char inputByte = binaryReadByte(file);
        unsigned char tmpByte = (unsigned char) (inputByte ^ vipDecodingTable[i]);
        decodedColors[i] = (unsigned char) (tmpByte ^ prevByte);
        prevByte = inputByte;
    }
    for(i = 0; i < numberOfColors; i++)
    {
        EmbThread thread;
        int startIndex = i << 2;
        thread.color.r = decodedColors[startIndex];
        thread.color.g = decodedColors[startIndex + 1];
        thread.color.b = decodedColors[startIndex + 2];
		/* printf("%d\n", decodedColors[startIndex + 3]); */
        embPattern_addThread(pattern, thread);
    }
	fseek(file, attributeOffset, SEEK_SET);
    attributeData = (unsigned char *)malloc(xOffset - attributeOffset);
    binaryReadBytes(file, attributeData, xOffset - attributeOffset);
    attributeDataDecompressed = DecompressData(attributeData, xOffset - attributeOffset, numberOfStitches);

    fseek(file, xOffset, SEEK_SET);
    xData = (unsigned char *)malloc(yOffset - xOffset);
    binaryReadBytes(file, xData, yOffset - xOffset);
    xDecompressed = DecompressData(xData, yOffset - xOffset, numberOfStitches);

    fseek(file, yOffset, SEEK_SET);
    yData = (unsigned char *)malloc(fileLength - yOffset);
    binaryReadBytes(file, yData, fileLength - yOffset);
    yDecompressed = DecompressData(yData, fileLength - yOffset, numberOfStitches);

    for(i = 0; i < numberOfStitches; i++)
    {
        embPattern_addStitchRel(pattern, DecodeByte(xDecompressed[i]) / 10.0,
            DecodeByte(yDecompressed[i]) / 10.0, DecodeStitchType(attributeDataDecompressed[i]), 1);
    }
    embPattern_addStitchRel(pattern, 0, 0, END, 1);
    fclose(file);

    return 1; /*TODO: finish readVip */
}
Пример #4
0
VOID Buffer<T,Allocator>::Decompress(DWORD Type, CONST Buffer<BYTE>& Source)
{
  return DecompressData(m_pInfo->pData, sizeof(T)*m_pInfo->DataSize, Type);
}