示例#1
0
static PyObject*
unpack_unpack(PyObject *self, PyObject *args)
{
    FILE *fd;
    const char *filename;
    byte *buf;
    dword rep;
    unsigned actions_size;
    PyObject *rep_id,
             *header,
             *actions,
             *tuple;


    if (!PyArg_ParseTuple(args, "s", &filename))
        return NULL;

    fd = fopen(filename, "rb");
    if (fd == NULL) {
        PyErr_SetString(PyExc_OSError, "Could not open replay file.");
        return NULL;
    }
    
    /* Get the replay ID (must always be 0x53526572) */
    unpack_section(fd, (byte *)&rep, sizeof(rep));
    rep_id = PyInt_FromLong(rep);

    /* Get the header.  Its size is always 0x279. */
    buf = malloc(HEADER_SIZE * sizeof(byte));
    if (buf == NULL) {
        PyErr_SetString(PyExc_MemoryError, "Could not allocate space.");
        return NULL;
    }
    unpack_section(fd, buf, sizeof(byte) * HEADER_SIZE);
    header = PyString_FromStringAndSize((char *)buf,
                                        sizeof(byte) * HEADER_SIZE);
    free(buf);
    buf = NULL;

    /* Get the size of the commands and then allocate them. */
    unpack_section(fd, (byte *)&actions_size, sizeof(actions_size));
    buf = malloc(sizeof(byte) * actions_size);
    if (buf == NULL) {
        PyErr_SetString(PyExc_MemoryError, "Could not allocate space.");
        return NULL;
    }
    unpack_section(fd, buf, actions_size);
    actions = PyString_FromStringAndSize((char *)buf,
                                         sizeof(byte) * actions_size);
    free(buf);
    buf = NULL;

    fclose(fd);

    tuple = PyTuple_New(3);
    PyTuple_SetItem(tuple, 0, rep_id);
    PyTuple_SetItem(tuple, 1, header);
    PyTuple_SetItem(tuple, 2, actions);
    return tuple;
}
示例#2
0
bool BWrepFile::Load(const char* pszFileName, int options, void *rwaheader, int size)
{
	if (pszFileName == NULL)
		return false;

	// offset of RWA (if any)
	#ifdef USE_RWA_CODE
	m_rwaoffset=0;
	#endif

	bool bOk = _Open(pszFileName);
	if(bOk)
	{
		long nRepID;
		unpack_section(m_pFile, (byte*)&nRepID, sizeof(nRepID));

		bOk = (nRepID == kBWREP_ID);
		if (bOk)
		{
			// read header
			bOk = (unpack_section(m_pFile, (byte*)&m_oHeader, kBWREP_HEADER_SIZE)==0);
			m_oHeader.checkPlayerNameUnicity();

			// read actions
			#ifdef USE_ACTIONS_CODE
			if(bOk) bOk = _LoadActions(m_pFile,(options&ADDACTIONS)==0,(options&LOADACTIONS)!=0);

				// load map
				#ifdef USE_MAP_CODE
				if(bOk) 
				{
					bOk = _LoadMap(m_pFile, (options&LOADMAP)!=0);

					#ifdef USE_RWA_CODE
					if(bOk && rwaheader!=0)
					{
						// read audio header and return file pointer on audio data
						memset(rwaheader,0,size);
						unsigned long *headerSize; headerSize=(unsigned long *)rwaheader;
						if(fread(rwaheader,1,sizeof(long),m_pFile)==sizeof(long))
						{
							size_t readsize; readsize = min((unsigned long)size,*headerSize)-sizeof(long);
							fread(((char*)rwaheader)+sizeof(long),1,readsize,m_pFile);
							if(*headerSize>(unsigned long)size) fseek(m_pFile,(*headerSize)-size,SEEK_CUR);
							m_rwaoffset = ftell(m_pFile);
						}
					}
					#endif
				}
				#endif
			#endif
		}

		_Close();
	}
	return bOk;
}
示例#3
0
bool BWrepFile::_LoadMap(FILE *fp, bool decode)
{
	// get section size
	int mapSize=0;
	unpack_section(fp, (byte*)&mapSize, sizeof(mapSize));

	// alloc buffer to read it
	byte *buffer = (byte *)calloc(mapSize,sizeof(byte));
	if (buffer==0) return false;

	// unpack map section in buffer
	unpack_section(fp, buffer, mapSize);

	// decode map (dont free buffer, it belongs to m_oMap now)
	bool bOk = decode?m_oMap.DecodeMap(buffer,mapSize,m_oHeader.getMapWidth(),m_oHeader.getMapHeight()):true;
	if(!decode) free(buffer);

	return bOk;
}
示例#4
0
bool BWrepFile::_LoadActions(FILE *fp, bool clear, bool decode)
{
	// get section size
	int cmdSize=0;
	unpack_section(fp, (byte*)&cmdSize, sizeof(cmdSize));

	// alloc buffer to read it
	byte *buffer = (byte *)malloc(cmdSize * sizeof(byte));
	if (buffer==0) return false;

	// unpack cmd section in buffer
	unpack_section(fp, buffer, cmdSize);

	// decode all actions (dont free buffer, it belongs to m_oActions now)
	bool bOk = decode ? m_oActions.DecodeActions(buffer,cmdSize, clear) : true;
	if(!decode) free(buffer);

	return bOk;
}
示例#5
0
// get offset in file of audio part (if any, 0 if none)
unsigned long BWrepFile::GetAudioOffset(const char* pszFileName, void *header, int size)
{
	unsigned long offset=0;

	bool bOk = _Open(pszFileName);
	if(!bOk) return 0;

	long nRepID;
	int res = unpack_section(m_pFile, (byte*)&nRepID, sizeof(nRepID));
	if(nRepID != kBWREP_ID) goto Exit;
	if(res!=0) goto Exit;
						
	// read header
	bOk = (unpack_section(m_pFile, (byte*)&m_oHeader, kBWREP_HEADER_SIZE)==0);
	if(!bOk) goto Exit;

	// get actions section size
	int cmdSize; cmdSize=0;
	res = unpack_section(m_pFile, (byte*)&cmdSize, sizeof(cmdSize));
	if(res!=0) goto Exit;

	// alloc buffer to read it
	byte *buffer; buffer = (byte *)malloc(cmdSize * sizeof(byte));
	if (buffer==0) goto Exit;

	// unpack cmd section in buffer
	res = unpack_section(m_pFile, buffer, cmdSize);
	if(res!=0) goto Exit;
	free(buffer);

	// get map section size
	int mapSize; mapSize=0;
	res = unpack_section(m_pFile, (byte*)&mapSize, sizeof(mapSize));
	if(res!=0) goto Exit;

	// alloc buffer to read it
	buffer = (byte *)malloc(mapSize * sizeof(byte));
	if (buffer==0) goto Exit;

	// unpack map section in buffer
	res = unpack_section(m_pFile, buffer, mapSize);
	if(res!=0) goto Exit;
	free(buffer);

	// read audio header and return file pointer on audio data
	unsigned long *headerSize; headerSize=(unsigned long *)header;
	if(fread(header,1,sizeof(long),m_pFile)!=sizeof(long)) goto Exit;
	size_t readsize; readsize = min((unsigned long)size,*headerSize)-sizeof(long);
	fread(((char*)header)+sizeof(long),1,readsize,m_pFile);
	if(*headerSize>(unsigned long)size) fseek(m_pFile,(*headerSize)-size,SEEK_CUR);
	offset = ftell(m_pFile);

Exit:
	_Close();
	return offset;
}