Added GemRB sources

This commit is contained in:
pelya
2010-12-14 09:47:29 +00:00
parent 674da851ef
commit 38bceffddd
455 changed files with 137680 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/
#include "2DAImporter.h"
#include "win32def.h"
#include "Interface.h"
#include "System/FileStream.h"
#define MAXLENGTH 4096 //if a 2da has longer lines, change this
#define SIGNLENGTH 256 //if a 2da has longer default value, change this
p2DAImporter::p2DAImporter(void)
{
str = NULL;
autoFree = false;
}
p2DAImporter::~p2DAImporter(void)
{
if (str && autoFree) {
delete( str );
}
for (unsigned int i = 0; i < ptrs.size(); i++) {
free( ptrs[i] );
}
}
bool p2DAImporter::Open(DataStream* stream, bool autoFree)
{
if (stream == NULL) {
return false;
}
if (str && this->autoFree) {
delete( str );
}
str = stream;
this->autoFree = autoFree;
char Signature[SIGNLENGTH];
str->CheckEncrypted();
str->ReadLine( Signature, sizeof(Signature) );
char* strp = Signature;
while (*strp == ' ')
strp++;
if (strncmp( strp, "2DA V1.0", 8 ) != 0) {
printMessage("2DAImporter", "Bad signature!\n",YELLOW );
// we don't care about this, so exptable.2da of iwd2 won't cause a bigger problem
// return false;
}
Signature[0] = 0;
str->ReadLine( Signature, sizeof(Signature) );
char* token = strtok( Signature, " " );
if (token) {
strncpy(defVal, token, sizeof(defVal) );
} else { // no whitespace
strncpy(defVal, Signature, sizeof(defVal) );
}
bool colHead = true;
int row = 0;
while (true) {
char* line = ( char* ) malloc( MAXLENGTH );
int len = str->ReadLine( line, MAXLENGTH-1 );
if (len <= 0) {
free( line );
break;
}
if (line[0] == '#') { // allow comments
free( line );
continue;
}
if (len < MAXLENGTH)
line = ( char * ) realloc( line, len + 1 );
ptrs.push_back( line );
if (colHead) {
colHead = false;
char* str = strtok( line, " " );
while (str != NULL) {
colNames.push_back( str );
str = strtok( NULL, " " );
}
} else {
char* str = strtok( line, " " );
if (str == NULL)
continue;
rowNames.push_back( str );
RowEntry r;
rows.push_back( r );
while (( str = strtok( NULL, " " ) ) != NULL) {
rows[row].push_back( str );
}
row++;
}
}
return true;
}
#include "plugindef.h"
GEMRB_PLUGIN(0xB22F938, "2DA File Importer")
PLUGIN_CLASS(IE_2DA_CLASS_ID, p2DAImporter)
END_PLUGIN()

View File

@@ -0,0 +1,169 @@
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/
#ifndef P2DAIMPORTER_H
#define P2DAIMPORTER_H
#include "TableMgr.h"
#include "globals.h"
#include <cstring>
typedef std::vector< char*> RowEntry;
class p2DAImporter : public TableMgr {
private:
DataStream* str;
bool autoFree;
std::vector< char*> colNames;
std::vector< char*> rowNames;
std::vector< char*> ptrs;
std::vector< RowEntry> rows;
char defVal[32];
public:
p2DAImporter(void);
~p2DAImporter(void);
bool Open(DataStream* stream, bool autoFree = true);
/** Returns the actual number of Rows in the Table */
inline ieDword GetRowCount() const
{
return ( ieDword ) rows.size();
}
inline ieDword GetColNamesCount() const
{
return (ieDword) colNames.size();
}
/** Returns the actual number of Columns in the Table */
inline ieDword GetColumnCount(unsigned int row = 0) const
{
if (rows.size() <= row) {
return 0;
}
return ( ieDword ) rows[row].size();
}
/** Returns a pointer to a zero terminated 2da element,
if it cannot return a value, it returns the default */
inline const char* QueryField(unsigned int row = 0, unsigned int column = 0) const
{
if (rows.size() <= row) {
return ( char * ) defVal;
}
if (rows[row].size() <= column) {
return ( char * ) defVal;
}
if (rows[row][column][0]=='*' && !rows[row][column][1]) {
return ( char * ) defVal;
}
return rows[row][column];
}
/** Returns a pointer to a zero terminated 2da element,
uses column name and row name to search the field */
inline const char* QueryField(const char* row, const char* column) const
{
int rowi, coli;
rowi = GetRowIndex(row);
if (rowi < 0) {
return ( char * ) defVal;
}
coli = GetColumnIndex(column);
if (coli < 0) {
return ( char * ) defVal;
}
return QueryField((unsigned int) rowi, (unsigned int) coli);
}
virtual const char* QueryDefault() const
{
return defVal;
}
inline int GetRowIndex(const char* string) const
{
for (unsigned int index = 0; index < rowNames.size(); index++) {
if (stricmp( rowNames[index], string ) == 0) {
return (int) index;
}
}
return -1;
}
inline int GetColumnIndex(const char* string) const
{
for (unsigned int index = 0; index < colNames.size(); index++) {
if (stricmp( colNames[index], string ) == 0) {
return (int) index;
}
}
return -1;
}
inline const char* GetColumnName(unsigned int index) const
{
if (index < colNames.size()) {
return colNames[index];
}
return "";
}
inline const char* GetRowName(unsigned int index) const
{
if (index < rowNames.size()) {
return rowNames[index];
}
return "";
}
inline int FindTableValue(unsigned int col, long val, int start) const
{
ieDword row, max;
max = GetRowCount();
for (row = start; row < max; row++) {
const char* ret = QueryField( row, col );
long Value;
if (valid_number( ret, Value ) && (Value == val) )
return (int) row;
}
return -1;
}
inline int FindTableValue(unsigned int col, const char* val, int start) const
{
ieDword row, max;
max = GetRowCount();
for (row = start; row < max; row++) {
const char* ret = QueryField( row, col );
if (stricmp(ret, val) == 0)
return (int) row;
}
return -1;
}
};
#endif

View File

@@ -0,0 +1 @@
ADD_GEMRB_PLUGIN(2DAImporter 2DAImporter.cpp 2DAImporter.h )

View File

@@ -0,0 +1,3 @@
plugin_LTLIBRARIES = 2DAImporter.la
2DAImporter_la_LDFLAGS = -module -avoid-version -shared
2DAImporter_la_SOURCES = 2DAImporter.cpp 2DAImporter.h