Initial commit.

This commit is contained in:
Jim Gray
2013-04-04 14:32:05 -07:00
parent ba5c81da32
commit d71d53e8ec
2180 changed files with 1393544 additions and 1 deletions

378
code/rufl/hfile.cpp Normal file
View File

@@ -0,0 +1,378 @@
////////////////////////////////////////////////////////////////////////////////////////
// RAVEN STANDARD USEFUL FUNCTION LIBRARY
// (c) 2002 Activision
//
//
// Handle File
// -----------
//
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////////////
#include "hfile.h"
#if !defined(RATL_HANDLE_POOL_VS_INC)
#include "..\Ratl\handle_pool_vs.h"
#endif
#if !defined(RATL_VECTOR_VS_INC)
#include "..\Ratl\vector_vs.h"
#endif
#if !defined(RUFL_HSTRING_INC)
#include "hstring.h"
#endif
extern bool HFILEopen_read(int& handle, const char* filepath);
extern bool HFILEopen_write(int& handle, const char* filepath);
extern bool HFILEread(int& handle, void* data, int size);
extern bool HFILEwrite(int& handle, const void* data, int size);
extern bool HFILEclose(int& handle);
////////////////////////////////////////////////////////////////////////////////////////
// Defines
////////////////////////////////////////////////////////////////////////////////////////
#define MAX_OPEN_FILES 20
////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////
struct SOpenFile
{
#ifdef _XBOX
dllNamespace::hstring mPath;
#else
hstring mPath;
#endif
bool mForRead;
int mHandle;
float mVersion;
int mChecksum;
};
typedef ratl::handle_pool_vs<SOpenFile, MAX_OPEN_FILES> TFilePool;
TFilePool& Pool()
{
static TFilePool TFP;
return TFP;
}
////////////////////////////////////////////////////////////////////////////////////////
// Constructor
//
// Allocates a new OpenFile structure and initializes it. DOES NOT OPEN!
//
////////////////////////////////////////////////////////////////////////////////////////
hfile::hfile(const char* file)
{
if (Pool().full())
{
mHandle = 0;
assert("HFILE: Too Many Files Open, Unable To Grab An Unused Handle"==0);
return;
}
mHandle = Pool().alloc();
SOpenFile& sfile = Pool()[mHandle];
sfile.mPath = file;
sfile.mHandle = 0;
sfile.mForRead = true;
}
////////////////////////////////////////////////////////////////////////////////////////
// Destructor
//
// Releases the open file structure for resue. Also closes the file if open.
//
////////////////////////////////////////////////////////////////////////////////////////
hfile::~hfile()
{
if (is_open())
{
close();
}
if (mHandle && Pool().is_used(mHandle))
{
Pool().free(mHandle);
}
mHandle = 0;
}
////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////
bool hfile::is_open(void) const
{
if (mHandle && Pool().is_used(mHandle))
{
return (Pool()[mHandle].mHandle!=0);
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////
bool hfile::is_open_for_read(void) const
{
if (mHandle && Pool().is_used(mHandle))
{
SOpenFile& sfile = Pool()[mHandle];
return (sfile.mHandle!=0 && sfile.mForRead);
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////
bool hfile::is_open_for_write(void) const
{
if (mHandle && Pool().is_used(mHandle))
{
SOpenFile& sfile = Pool()[mHandle];
return (sfile.mHandle!=0 && !sfile.mForRead);
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
bool hfile::open(float version, int checksum, bool read)
{
// Make Sure This Is A Valid Handle
//----------------------------------
if (!mHandle || !Pool().is_used(mHandle))
{
assert("HFILE: Invalid Handle"==0);
return false;
}
// Make Sure The File Is Not ALREADY Open
//----------------------------------------
SOpenFile& sfile = Pool()[mHandle];
if (sfile.mHandle!=0)
{
assert("HFILE: Attempt To Open An Already Open File"==0);
return false;
}
sfile.mForRead = read;
if (read)
{
HFILEopen_read(sfile.mHandle, *sfile.mPath);
}
else
{
HFILEopen_write(sfile.mHandle, *sfile.mPath);
}
// If The Open Failed, Report It And Free The SOpenFile
//------------------------------------------------------
if (sfile.mHandle==0)
{
if (!read)
{
assert("HFILE: Unable To Open File"==0);
}
return false;
}
// Read The File's Header
//------------------------
if (read)
{
if (!HFILEread(sfile.mHandle, &sfile.mVersion, sizeof(sfile.mVersion)))
{
assert("HFILE: Unable To Read File Header"==0);
close();
return false;
}
if (!HFILEread(sfile.mHandle, &sfile.mChecksum, sizeof(sfile.mChecksum)))
{
assert("HFILE: Unable To Read File Header"==0);
close();
return false;
}
// Make Sure The Checksum & Version Match
//----------------------------------------
if (sfile.mVersion!=version || sfile.mChecksum!=checksum)
{
close();
return false; // Failed To Match Checksum Or Version Number -> Old Data
}
}
else
{
sfile.mVersion = version;
sfile.mChecksum = checksum;
if (!HFILEwrite(sfile.mHandle, &sfile.mVersion, sizeof(sfile.mVersion)))
{
assert("HFILE: Unable To Write File Header"==0);
close();
return false;
}
if (!HFILEwrite(sfile.mHandle, &sfile.mChecksum, sizeof(sfile.mChecksum)))
{
assert("HFILE: Unable To Write File Header"==0);
close();
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
bool hfile::close()
{
if (!mHandle || !Pool().is_used(mHandle))
{
assert("HFILE: Invalid Handle"==0);
return false;
}
SOpenFile& sfile = Pool()[mHandle];
if (sfile.mHandle==0)
{
assert("HFILE: Unable TO Close Unopened File"==0);
return false;
}
if (!HFILEclose(sfile.mHandle))
{
sfile.mHandle = 0;
assert("HFILE: Unable To Close File"==0);
return false;
}
sfile.mHandle = 0;
return true;
}
////////////////////////////////////////////////////////////////////////////////////
// Searches for the first block with the matching data size, and reads it in.
////////////////////////////////////////////////////////////////////////////////////
bool hfile::load(void* data, int datasize)
{
// Go Ahead And Open The File For Reading
//----------------------------------------
bool auto_opened = false;
if (!is_open())
{
if (!open_read())
{
return false;
}
auto_opened = true;
}
// Make Sure That The File Is Readable
//-------------------------------------
SOpenFile& sfile = Pool()[mHandle];
if (!sfile.mForRead)
{
assert("HFILE: Unable to load from a file that is opened for save"==0);
if (auto_opened)
{
close();
}
return false;
}
// Now Read It
//-------------
if (!HFILEread(sfile.mHandle, data, datasize))
{
assert("HFILE: Unable To Read Object"==0);
if (auto_opened)
{
close();
}
return false;
}
// Success!
//----------
if (auto_opened)
{
close();
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
bool hfile::save(void* data, int datasize)
{
// Go Ahead And Open The File For Reading
//----------------------------------------
bool auto_opened = false;
if (!is_open())
{
if (!open_write())
{
return false;
}
auto_opened = true;
}
// Make Sure That The File Is Readable
//-------------------------------------
SOpenFile& sfile = Pool()[mHandle];
if (sfile.mForRead)
{
assert("HFILE: Unable to save to a file that is opened for read"==0);
if (auto_opened)
{
close();
}
return false;
}
// Write The Actual Object
//-------------------------
if (!HFILEwrite(sfile.mHandle, data, datasize))
{
assert("HFILE: Unable To Write File Data"==0);
if (auto_opened)
{
close();
}
return false;
}
if (auto_opened)
{
close();
}
return true;
}

61
code/rufl/hfile.h Normal file
View File

@@ -0,0 +1,61 @@
////////////////////////////////////////////////////////////////////////////////////////
// RAVEN STANDARD USEFUL FUNCTION LIBRARY
// (c) 2002 Activision
//
//
// Handle File
// -----------
//
////////////////////////////////////////////////////////////////////////////////////////
#if !defined(RUFL_HFILE_INC)
#define RUFL_HFILE_INC
////////////////////////////////////////////////////////////////////////////////////////
// HFile Bindings
//
// These are the standard C hfile bindings, copy these function wrappers to your .cpp
// before including hfile, and modify them if needed to support a different file
// system.
////////////////////////////////////////////////////////////////////////////////////////
//bool HFILEopen_read(int& handle, const char* filepath) {handle=(int)fopen(filepath, "rb"); return (handle!=0);}
//bool HFILEopen_write(int& handle, const char* filepath) {handle=(int)fopen(filepath, "wb"); return (handle!=0);}
//bool HFILEread(int& handle, void* data, int size) {return (fread(data, size, 1, (FILE*)(handle))>0);}
//bool HFILEwrite(int& handle, const void* data, int size) {return (fwrite(data, size, 1, (FILE*)(handle))>0);}
//bool HFILEclose(int& handle) {return (fclose((FILE*)handle)==0);}
////////////////////////////////////////////////////////////////////////////////////////
// The Handle String Class
////////////////////////////////////////////////////////////////////////////////////////
class hfile
{
public:
////////////////////////////////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////////////////////////////////
hfile(const char *file);
~hfile();
bool load(void* data, int datasize);
bool save(void* data, int datasize);
bool is_open(void) const;
bool is_open_for_read(void) const;
bool is_open_for_write(void) const;
bool open_read(float version=1.0f, int checksum=0) {return open(version, checksum, true);}
bool open_write(float version=1.0f, int checksum=0) {return open(version, checksum, false);}
bool close();
private:
bool open(float version, int checksum, bool read);
int mHandle;
};
#endif // hfile_H

192
code/rufl/hstring.cpp Normal file
View File

@@ -0,0 +1,192 @@
////////////////////////////////////////////////////////////////////////////////////////
// RAVEN STANDARD USEFUL FUNCTION LIBRARY
// (c) 2002 Activision
//
//
// Handle String
// -------------
// Handle strings are allocated once in a static buffer (with a hash index), and are
// never cleared out. You should use these for very common string names which are
// redundant or intended to last a long time.
//
// Handle strings are also good for comparison and storage because they compare only
// the handles, which are simple unique integers.
//
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////////////
#include "hstring.h"
#include <string.h>
#include "..\Ratl\hash_pool_vs.h"
////////////////////////////////////////////////////////////////////////////////////////
// Defines
////////////////////////////////////////////////////////////////////////////////////////
#define MAX_HASH 16384 // How Many Hash
#define BLOCK_SIZE 65536 // Size of a string storage block in bytes.
////////////////////////////////////////////////////////////////////////////////////////
// The Hash Pool
////////////////////////////////////////////////////////////////////////////////////////
typedef ratl::hash_pool<BLOCK_SIZE, MAX_HASH> TStrPool;
TStrPool& Pool()
{
static TStrPool TSP;
return TSP;
}
#ifdef _XBOX
// Utility for clearing the pool between levels
void ClearHStringPool( void )
{
Pool().clear();
}
namespace dllNamespace
{
#endif
////////////////////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////////////////////
hstring::hstring()
{
mHandle = 0;
#ifdef _DEBUG
mStr = 0;
#endif
}
////////////////////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////////////////////
hstring::hstring(const char *str)
{
init(str);
}
////////////////////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////////////////////
hstring::hstring(const hstring &str)
{
mHandle = str.mHandle;
#ifdef _DEBUG
mStr = str.mStr;
#endif
}
////////////////////////////////////////////////////////////////////////////////////////
// Assignment
////////////////////////////////////////////////////////////////////////////////////////
hstring& hstring::operator= (const char *str)
{
init(str);
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////////////////////
hstring& hstring::operator= (const hstring &str)
{
mHandle = str.mHandle;
#ifdef _DEBUG
mStr = str.mStr;
#endif
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////
const char* hstring::c_str(void) const
{
if (!mHandle)
{
return("");
}
return ((const char*)Pool()[mHandle]);
}
////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////
const char* hstring::operator *(void) const
{
return c_str();
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
int hstring::length() const
{
return strlen(c_str());
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
int hstring::handle() const
{
return mHandle;
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
void hstring::init(const char *str)
{
if (!str)
{
mHandle = 0;
}
else
{
mHandle = Pool().get_handle(str, strlen(str)+1); // +1 for null character
}
#ifdef _DEBUG
mStr = (char*)Pool()[mHandle];
#endif
}
////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
float hstring::ave_collisions() {return Pool().average_collisions();}
int hstring::total_strings() {return Pool().total_allocs();}
int hstring::total_bytes() {return Pool().size();}
int hstring::total_finds() {return Pool().total_finds();}
int hstring::total_collisions() {return Pool().total_collisions();}
#endif
#ifdef _XBOX
} // dllNamespace
#endif

106
code/rufl/hstring.h Normal file
View File

@@ -0,0 +1,106 @@
////////////////////////////////////////////////////////////////////////////////////////
// RAVEN STANDARD USEFUL FUNCTION LIBRARY
// (c) 2002 Activision
//
//
// Handle String
// -------------
// Handle strings are allocated once in a static buffer (with a hash index), and are
// never cleared out. You should use these for very common string names which are
// redundant or intended to last a long time.
//
// Handle strings are also good for comparison and storage because they compare only
// the handles, which are simple unique integers.
//
////////////////////////////////////////////////////////////////////////////////////////
#if !defined(RUFL_HSTRING_INC)
#define RUFL_HSTRING_INC
#ifdef _XBOX
namespace dllNamespace
{
#endif
////////////////////////////////////////////////////////////////////////////////////////
// The Handle String Class
////////////////////////////////////////////////////////////////////////////////////////
class hstring
{
public:
////////////////////////////////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////////////////////////////////
hstring();
hstring(const char *str);
hstring(const hstring &str);
////////////////////////////////////////////////////////////////////////////////////
// Assignment
////////////////////////////////////////////////////////////////////////////////////
hstring& operator= (const char *str);
hstring& operator= (const hstring &str);
////////////////////////////////////////////////////////////////////////////////////
// Comparison
////////////////////////////////////////////////////////////////////////////////////
bool operator== (const hstring &str) const {return (mHandle==str.mHandle);}
bool operator< (const hstring &str) const {return (mHandle< str.mHandle);}
bool operator! () const {return (mHandle==0);}
////////////////////////////////////////////////////////////////////////////////////
// Conversion
////////////////////////////////////////////////////////////////////////////////////
const char* c_str(void) const;
const char* operator *(void) const;
////////////////////////////////////////////////////////////////////////////////////
// Access Functions
////////////////////////////////////////////////////////////////////////////////////
int length(void) const;
int handle(void) const;
bool empty() const {return handle()==0;}
////////////////////////////////////////////////////////////////////////////////////
// Debug Statistics Routines
////////////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
static float ave_collisions();
static int total_strings();
static int total_bytes();
static int total_finds();
static int total_collisions();
#endif
private:
////////////////////////////////////////////////////////////////////////////////////
// Helper Functions
////////////////////////////////////////////////////////////////////////////////////
void init(const char *str);
////////////////////////////////////////////////////////////////////////////////////
// Data
////////////////////////////////////////////////////////////////////////////////////
int mHandle;
#ifdef _DEBUG
char* mStr;
#endif
};
#ifdef _XBOX
} // dllNamespace
using namespace dllNamespace;
#endif
#endif // HSTRING_H

0
code/rufl/random.cpp Normal file
View File

0
code/rufl/random.h Normal file
View File