Esempio n. 1
0
void Settings::generateName(WormSettings& ws)
{
#ifdef NSPIRE
	FILE* f = fopen(joinPath(lieroDataRoot, "NAMES.DAT.tns").c_str(), "rb");
#else
	FILE* f = fopen(joinPath(lieroDataRoot, "NAMES.DAT").c_str(), "rb");
#endif
	
	if(!f)
		return;
		
	std::vector<std::string> names;
	
	std::size_t len = fileLength(f);
	
	std::vector<char> chars(len);
	
	fread(&chars[0], 1, len, f);
	
	fclose(f);
	
	std::size_t begin = 0;
	for(std::size_t i = 0; i < len; ++i)
	{
		if(chars[i] == '\r'
		|| chars[i] == '\n')
		{
			if(i > begin)
			{
				names.push_back(std::string(chars.begin() + begin, chars.begin() + i));
			}
			
			begin = i + 1;
		}
	}
	
	if(!names.empty())
	{
		ws.name = names[gfx.rand(Uint32(names.size()))];
		ws.randomName = true;
	}
}
Esempio n. 2
0
int Sys_FileOpenRead(char *path, int *size) {
	int handleIdx = findHandle();

	FILE* fileHandle = FileHandles[handleIdx];

	fopen_s(&fileHandle, path, "rb");

	if (fileHandle == 0) {
		if (size != 0) {
			*size = -1;
		}
		return -1;
	}

	FileHandles[handleIdx] = fileHandle;

	if (size != 0) {
		*size = fileLength(fileHandle);
	}

	return handleIdx;
}
Esempio n. 3
0
BinaryInput::BinaryInput(
    const std::string&  filename,
    G3DEndian           fileEndian,
    bool                compressed) {

    alreadyRead = 0;
    freeBuffer = true;
    this->fileEndian = fileEndian;
    this->filename = filename;
    buffer = NULL;
    bufferLength = 0;
    length = 0;
    pos = 0;
    beginEndBits = 0;
    bitPos = 0;

    // Update global file tracker
    _internal::currentFilesUsed.append(filename);

    swapBytes = needSwapBytes(fileEndian);

    // Figure out how big the file is and verify that it exists.
    length = fileLength(filename);

    // Read the file into memory
    FILE* file = fopen(filename.c_str(), "rb");

    if (! file || (length == -1)) {
        throw format("File not found: \"%s\"", filename.c_str());
        return;
    }

    if (! compressed && (length > INITIAL_BUFFER_LENGTH)) {
        // Read only a subset of the file so we don't consume
        // all available memory.
        bufferLength = INITIAL_BUFFER_LENGTH;
    } else {
        // Either the length is fine or the file is compressed
        // and requires us to read the whole thing for zlib.
        bufferLength = length;
    }

    debugAssert(freeBuffer);
    buffer = (uint8*)System::malloc(bufferLength);
    if (buffer == NULL) {
        if (compressed) {
            throw "Not enough memory to load compressed file. (1)";
        }

        // Try to allocate a small array; not much memory is available.
        // Give up if we can't allocate even 1k.
        while ((buffer == NULL) && (bufferLength > 1024)) {
            bufferLength /= 2;
            buffer = (uint8*)System::malloc(bufferLength);
        }
    }
    debugAssert(buffer);

    fread(buffer, bufferLength, sizeof(int8), file);
    fclose(file);
    file = NULL;

    pos = 0;

    if (compressed) {
        if (bufferLength != length) {
            throw "Not enough memory to load compressed file. (2)";
        }

        // Decompress
        // Use the existing buffer as the source, allocate
        // a new buffer to use as the destination.

        int64 tempLength = length;
        length = G3D::readUInt32(buffer, swapBytes);

        uint8* tempBuffer = buffer;
        buffer = (uint8*)System::malloc(length);

        debugAssert(buffer);
        debugAssert(isValidHeapPointer(tempBuffer));
        debugAssert(isValidHeapPointer(buffer));

        unsigned long L = length;
        int64 result = uncompress(buffer, &L, tempBuffer + 4, tempLength - 4);
        length = L;
        bufferLength = length;

        debugAssert(result == Z_OK);
        (void)result;

        System::free(tempBuffer);
    }
}
Esempio n. 4
0
BinaryInput::BinaryInput(
    const std::string&  filename,
    G3DEndian           fileEndian,
    bool                compressed) :
    m_filename(filename),
    m_bitPos(0),
    m_bitString(0),
    m_beginEndBits(0),
    m_alreadyRead(0),
    m_length(0),
    m_bufferLength(0),
    m_buffer(NULL),
    m_pos(0),
    m_freeBuffer(true) {

    setEndian(fileEndian);

    // Update global file tracker
    _internal::currentFilesUsed.insert(m_filename);


    if (! fileExists(m_filename, false)) {
        std::string zipfile;
        std::string internalfile;
        if (zipfileExists(m_filename, zipfile, internalfile)) {
            // Load from zipfile
            void* v;
            size_t s;
            zipRead(filename, v, s);
            m_buffer = reinterpret_cast<uint8*>(v);
            m_bufferLength = m_length = s;
            if (compressed) {
                decompress();
            }
            m_freeBuffer = true;
        } else {
            Log::common()->printf("Warning: File not found: %s\n", m_filename.c_str());
        }
        return;
    }

    // Figure out how big the file is and verify that it exists.
    m_length = fileLength(m_filename);

    // Read the file into memory
    FILE* file = fopen(m_filename.c_str(), "rb");

    if (! file || (m_length == -1)) {
        throw format("File not found: \"%s\"", m_filename.c_str());
        return;
    }

    if (! compressed && (m_length > INITIAL_BUFFER_LENGTH)) {
        // Read only a subset of the file so we don't consume
        // all available memory.
        m_bufferLength = INITIAL_BUFFER_LENGTH;
    } else {
        // Either the length is fine or the file is compressed
        // and requires us to read the whole thing for zlib.
        m_bufferLength = m_length;
    }

    debugAssert(m_freeBuffer);
    m_buffer = (uint8*)System::alignedMalloc(m_bufferLength, 16);
    if (m_buffer == NULL) {
        if (compressed) {
            throw "Not enough memory to load compressed file. (1)";
        }

        // Try to allocate a small array; not much memory is available.
        // Give up if we can't allocate even 1k.
        while ((m_buffer == NULL) && (m_bufferLength > 1024)) {
            m_bufferLength /= 2;
            m_buffer = (uint8*)System::alignedMalloc(m_bufferLength, 16);
        }
    }
    debugAssert(m_buffer);

    fread(m_buffer, m_bufferLength, sizeof(int8), file);
    fclose(file);
    file = NULL;

    if (compressed) {
        if (m_bufferLength != m_length) {
            throw "Not enough memory to load compressed file. (2)";
        }

        decompress();
    }
}
 // conveniance function, since I think this name is more c++ typic
 int64_t length()
 {
     return fileLength();
 }
Esempio n. 6
0
bool Settings::load(std::string const& path)
{
	FILE* opt = tolerantFOpen(path.c_str(), "rb");
	
	if(!opt)
		return false;
		
	std::size_t size = fileLength(opt);
	
	if(size < 155)
		return false; // .dat is too short
	
	maxBonuses = readUint8(opt);
	loadingTime = readUint16(opt);
	lives = readUint16(opt);
	timeToLose = readUint16(opt);
	flagsToWin = readUint16(opt);
	
	screenSync = readUint8(opt) != 0;
	map = readUint8(opt) != 0;
	wormSettings[0].controller = readUint8(opt) & 0x1;
	wormSettings[1].controller = readUint8(opt) & 0x1;
	randomLevel = readUint8(opt) != 0;
	blood = readUint16(opt);
	gameMode = readUint8(opt);
	namesOnBonuses = readUint8(opt) != 0;
	regenerateLevel = readUint8(opt) != 0;
	shadow = readUint8(opt) != 0;
	
	//fread(weapTable, 1, 40, opt);
	
	for(int i = 0; i < 40; ++i)
	{
		weapTable[i] = limit<0, 3>(fgetc(opt));
	}
	
	for(int i = 0; i < 2; ++i)
	for(int j = 0; j < 3; ++j)
		wormSettings[i].rgb[j] = readUint8(opt) & 63;
		
	for(int i = 0; i < 2; ++i)
	{
		for(int j = 0; j < 5; ++j)
		{
			wormSettings[i].weapons[j] = readUint8(opt);
		}
	}

	wormSettings[0].health = readUint16(opt);
	wormSettings[1].health = readUint16(opt);

	for(int i = 0; i < 2; ++i)
	{
		wormSettings[i].name = readPascalString(opt, 21);
	}
	
	//fgetc(opt); // What's this?
	
	loadChange = readUint8(opt) != 0;
	
	// 0x7B-83 is the string LIERO
	
	fseek(opt, 0x84, SEEK_SET);
	for(int i = 0; i < 2; ++i)
	{
		for(int j = 0; j < 7; ++j)
		{
			wormSettings[i].controls[j] = limit<0, 177>(readUint8(opt));
		}
	}
	
	levelFile = readPascalString(opt, 9);
	
	for(int i = 0; i < 2; ++i)
	{
		if(wormSettings[i].name.empty())
			generateName(wormSettings[i]);
		else
			wormSettings[i].randomName = false;
	}
	
	fclose(opt);
	
	return true;
}