COMMON: Begin to merge some NE/PE code

The ID classes are now common to both. The files have been renamed to better illustrate their purpose.
This commit is contained in:
Matthew Hoops 2011-02-22 20:01:19 -05:00
parent 3a3fd9f009
commit a2a0b13de2
10 changed files with 201 additions and 213 deletions

View file

@ -17,8 +17,6 @@ MODULE_OBJS := \
memorypool.o \ memorypool.o \
md5.o \ md5.o \
mutex.o \ mutex.o \
ne_exe.o \
pe_exe.o \
random.o \ random.o \
rational.o \ rational.o \
str.o \ str.o \
@ -30,6 +28,9 @@ MODULE_OBJS := \
unarj.o \ unarj.o \
unzip.o \ unzip.o \
util.o \ util.o \
winexe.o \
winexe_ne.o \
winexe_pe.o \
xmlparser.o \ xmlparser.o \
zlib.o zlib.o

84
common/winexe.cpp Normal file
View file

@ -0,0 +1,84 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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.
*
* $URL$
* $Id$
*
*/
#include "common/str.h"
#include "common/winexe.h"
namespace Common {
WinResourceID &WinResourceID::operator=(String string) {
_name = string;
_idType = kIDTypeString;
return *this;
}
WinResourceID &WinResourceID::operator=(uint32 x) {
_id = x;
_idType = kIDTypeNumerical;
return *this;
}
bool WinResourceID::operator==(const String &x) const {
return _idType == kIDTypeString && _name.equalsIgnoreCase(x);
}
bool WinResourceID::operator==(const uint32 &x) const {
return _idType == kIDTypeNumerical && _id == x;
}
bool WinResourceID::operator==(const WinResourceID &x) const {
if (_idType != x._idType)
return false;
if (_idType == kIDTypeString)
return _name.equalsIgnoreCase(x._name);
if (_idType == kIDTypeNumerical)
return _id == x._id;
return true;
}
String WinResourceID::getString() const {
if (_idType != kIDTypeString)
return "";
return _name;
}
uint32 WinResourceID::getID() const {
if (_idType != kIDTypeNumerical)
return 0xffffffff;
return _idType;
}
String WinResourceID::toString() const {
if (_idType == kIDTypeString)
return _name;
else if (_idType == kIDTypeNumerical)
return String::format("%08x", _id);
return "";
}
} // End of namespace Common

74
common/winexe.h Normal file
View file

@ -0,0 +1,74 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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.
*
* $URL$
* $Id$
*
*/
#ifndef COMMON_WINEXE_H
#define COMMON_WINEXE_H
#include "common/hash-str.h"
namespace Common {
class String;
class WinResourceID {
public:
WinResourceID() { _idType = kIDTypeNull; }
WinResourceID(String x) { _idType = kIDTypeString; _name = x; }
WinResourceID(uint32 x) { _idType = kIDTypeNumerical; _id = x; }
WinResourceID &operator=(String string);
WinResourceID &operator=(uint32 x);
bool operator==(const String &x) const;
bool operator==(const uint32 &x) const;
bool operator==(const WinResourceID &x) const;
String getString() const;
uint32 getID() const;
String toString() const;
private:
/** An ID Type. */
enum IDType {
kIDTypeNull, ///< No type set
kIDTypeNumerical, ///< A numerical ID.
kIDTypeString ///< A string ID.
} _idType;
String _name; ///< The resource's string ID.
uint32 _id; ///< The resource's numerical ID.
};
struct WinResourceID_Hash {
uint operator()(const WinResourceID &id) const { return hashit(id.toString()); }
};
struct WinResourceID_EqualTo {
bool operator()(const WinResourceID &id1, const WinResourceID &id2) const { return id1 == id2; }
};
} // End of namespace Common
#endif

View file

@ -26,9 +26,9 @@
#include "common/debug.h" #include "common/debug.h"
#include "common/file.h" #include "common/file.h"
#include "common/memstream.h" #include "common/memstream.h"
#include "common/ne_exe.h"
#include "common/str.h" #include "common/str.h"
#include "common/stream.h" #include "common/stream.h"
#include "common/winexe_ne.h"
namespace Common { namespace Common {
@ -174,59 +174,6 @@ void NECursor::clear() {
delete[] _surface; _surface = 0; delete[] _surface; _surface = 0;
} }
NEResourceID &NEResourceID::operator=(String string) {
_name = string;
_idType = kIDTypeString;
return *this;
}
NEResourceID &NEResourceID::operator=(uint16 x) {
_id = x;
_idType = kIDTypeNumerical;
return *this;
}
bool NEResourceID::operator==(const String &x) const {
return _idType == kIDTypeString && _name.equalsIgnoreCase(x);
}
bool NEResourceID::operator==(const uint16 &x) const {
return _idType == kIDTypeNumerical && _id == x;
}
bool NEResourceID::operator==(const NEResourceID &x) const {
if (_idType != x._idType)
return false;
if (_idType == kIDTypeString)
return _name.equalsIgnoreCase(x._name);
if (_idType == kIDTypeNumerical)
return _id == x._id;
return true;
}
String NEResourceID::getString() const {
if (_idType != kIDTypeString)
return "";
return _name;
}
uint16 NEResourceID::getID() const {
if (_idType != kIDTypeNumerical)
return 0xffff;
return _idType;
}
String NEResourceID::toString() const {
if (_idType == kIDTypeString)
return _name;
else if (_idType == kIDTypeNumerical)
return String::format("%04x", _id);
return "";
}
NEResources::NEResources() { NEResources::NEResources() {
_exe = 0; _exe = 0;
} }
@ -464,7 +411,7 @@ String NEResources::getResourceString(SeekableReadStream &exe, uint32 offset) {
return string; return string;
} }
const NEResources::Resource *NEResources::findResource(uint16 type, NEResourceID id) const { const NEResources::Resource *NEResources::findResource(uint16 type, WinResourceID id) const {
for (List<Resource>::const_iterator it = _resources.begin(); it != _resources.end(); ++it) for (List<Resource>::const_iterator it = _resources.begin(); it != _resources.end(); ++it)
if (it->type == type && it->id == id) if (it->type == type && it->id == id)
return &*it; return &*it;
@ -472,7 +419,7 @@ const NEResources::Resource *NEResources::findResource(uint16 type, NEResourceID
return 0; return 0;
} }
SeekableReadStream *NEResources::getResource(uint16 type, NEResourceID id) { SeekableReadStream *NEResources::getResource(uint16 type, WinResourceID id) {
const Resource *res = findResource(type, id); const Resource *res = findResource(type, id);
if (!res) if (!res)
@ -482,8 +429,8 @@ SeekableReadStream *NEResources::getResource(uint16 type, NEResourceID id) {
return _exe->readStream(res->size); return _exe->readStream(res->size);
} }
const Array<NEResourceID> NEResources::getIDList(uint16 type) const { const Array<WinResourceID> NEResources::getIDList(uint16 type) const {
Array<NEResourceID> idArray; Array<WinResourceID> idArray;
for (List<Resource>::const_iterator it = _resources.begin(); it != _resources.end(); ++it) for (List<Resource>::const_iterator it = _resources.begin(); it != _resources.end(); ++it)
if (it->type == type) if (it->type == type)

View file

@ -23,11 +23,12 @@
* *
*/ */
#ifndef COMMON_NE_EXE_H #ifndef COMMON_WINEXE_NE_H
#define COMMON_NE_EXE_H #define COMMON_WINEXE_NE_H
#include "common/array.h" #include "common/array.h"
#include "common/list.h" #include "common/list.h"
#include "common/winexe.h"
namespace Common { namespace Common {
@ -74,38 +75,9 @@ private:
void clear(); void clear();
}; };
class NEResourceID {
public:
NEResourceID() { _idType = kIDTypeNull; }
NEResourceID(String x) { _idType = kIDTypeString; _name = x; }
NEResourceID(uint16 x) { _idType = kIDTypeNumerical; _id = x; }
NEResourceID &operator=(String string);
NEResourceID &operator=(uint16 x);
bool operator==(const String &x) const;
bool operator==(const uint16 &x) const;
bool operator==(const NEResourceID &x) const;
String getString() const;
uint16 getID() const;
String toString() const;
private:
/** An ID Type. */
enum IDType {
kIDTypeNull, ///< No type set
kIDTypeNumerical, ///< A numerical ID.
kIDTypeString ///< A string ID.
} _idType;
String _name; ///< The resource's string ID.
uint16 _id; ///< The resource's numerical ID.
};
/** A New Executable cursor group. */ /** A New Executable cursor group. */
struct NECursorGroup { struct NECursorGroup {
NEResourceID id; WinResourceID id;
Array<NECursor *> cursors; ///< The cursors. Array<NECursor *> cursors; ///< The cursors.
}; };
@ -161,15 +133,15 @@ public:
const Array<NECursorGroup> &getCursors() const; const Array<NECursorGroup> &getCursors() const;
/** Return a list of resources for a given type. */ /** Return a list of resources for a given type. */
const Array<NEResourceID> getIDList(uint16 type) const; const Array<WinResourceID> getIDList(uint16 type) const;
/** Return a stream to the specified resource (or 0 if non-existent). */ /** Return a stream to the specified resource (or 0 if non-existent). */
SeekableReadStream *getResource(uint16 type, NEResourceID id); SeekableReadStream *getResource(uint16 type, WinResourceID id);
private: private:
/** A resource. */ /** A resource. */
struct Resource { struct Resource {
NEResourceID id; WinResourceID id;
uint16 type; ///< Type of the resource. uint16 type; ///< Type of the resource.
@ -200,7 +172,7 @@ private:
bool readCursor(NECursor &cursor, const Resource &resource, uint32 size); bool readCursor(NECursor &cursor, const Resource &resource, uint32 size);
/** Find a specific resource. */ /** Find a specific resource. */
const Resource *findResource(uint16 type, NEResourceID id) const; const Resource *findResource(uint16 type, WinResourceID id) const;
/** Read a resource string. */ /** Read a resource string. */
static String getResourceString(SeekableReadStream &exe, uint32 offset); static String getResourceString(SeekableReadStream &exe, uint32 offset);
@ -208,4 +180,4 @@ private:
} // End of namespace Common } // End of namespace Common
#endif // COMMON_NE_EXE_H #endif

View file

@ -26,65 +26,12 @@
#include "common/debug.h" #include "common/debug.h"
#include "common/file.h" #include "common/file.h"
#include "common/memstream.h" #include "common/memstream.h"
#include "common/pe_exe.h"
#include "common/str.h" #include "common/str.h"
#include "common/stream.h" #include "common/stream.h"
#include "common/winexe_pe.h"
namespace Common { namespace Common {
PEResourceID &PEResourceID::operator=(String string) {
_name = string;
_idType = kIDTypeString;
return *this;
}
PEResourceID &PEResourceID::operator=(uint32 x) {
_id = x;
_idType = kIDTypeNumerical;
return *this;
}
bool PEResourceID::operator==(const String &x) const {
return _idType == kIDTypeString && _name.equalsIgnoreCase(x);
}
bool PEResourceID::operator==(const uint32 &x) const {
return _idType == kIDTypeNumerical && _id == x;
}
bool PEResourceID::operator==(const PEResourceID &x) const {
if (_idType != x._idType)
return false;
if (_idType == kIDTypeString)
return _name.equalsIgnoreCase(x._name);
if (_idType == kIDTypeNumerical)
return _id == x._id;
return true;
}
String PEResourceID::getString() const {
if (_idType != kIDTypeString)
return "";
return _name;
}
uint32 PEResourceID::getID() const {
if (_idType != kIDTypeNumerical)
return 0xffffffff;
return _idType;
}
String PEResourceID::toString() const {
if (_idType == kIDTypeString)
return _name;
else if (_idType == kIDTypeNumerical)
return String::format("%08x", _id);
return "";
}
PEResources::PEResources() { PEResources::PEResources() {
_exe = 0; _exe = 0;
} }
@ -179,7 +126,7 @@ void PEResources::parseResourceLevel(Section &section, uint32 offset, int level)
for (uint32 i = 0; i < namedEntryCount + intEntryCount; i++) { for (uint32 i = 0; i < namedEntryCount + intEntryCount; i++) {
uint32 value = _exe->readUint32LE(); uint32 value = _exe->readUint32LE();
PEResourceID id; WinResourceID id;
if (value & 0x80000000) { if (value & 0x80000000) {
value &= 0x7fffffff; value &= 0x7fffffff;
@ -230,8 +177,8 @@ void PEResources::parseResourceLevel(Section &section, uint32 offset, int level)
} }
} }
const Array<PEResourceID> PEResources::getTypeList() const { const Array<WinResourceID> PEResources::getTypeList() const {
Array<PEResourceID> array; Array<WinResourceID> array;
if (!_exe) if (!_exe)
return array; return array;
@ -242,8 +189,8 @@ const Array<PEResourceID> PEResources::getTypeList() const {
return array; return array;
} }
const Array<PEResourceID> PEResources::getNameList(const PEResourceID &type) const { const Array<WinResourceID> PEResources::getNameList(const WinResourceID &type) const {
Array<PEResourceID> array; Array<WinResourceID> array;
if (!_exe || !_resources.contains(type)) if (!_exe || !_resources.contains(type))
return array; return array;
@ -256,8 +203,8 @@ const Array<PEResourceID> PEResources::getNameList(const PEResourceID &type) con
return array; return array;
} }
const Array<PEResourceID> PEResources::getLangList(const PEResourceID &type, const PEResourceID &name) const { const Array<WinResourceID> PEResources::getLangList(const WinResourceID &type, const WinResourceID &name) const {
Array<PEResourceID> array; Array<WinResourceID> array;
if (!_exe || !_resources.contains(type)) if (!_exe || !_resources.contains(type))
return array; return array;
@ -275,8 +222,8 @@ const Array<PEResourceID> PEResources::getLangList(const PEResourceID &type, con
return array; return array;
} }
SeekableReadStream *PEResources::getResource(const PEResourceID &type, const PEResourceID &name) { SeekableReadStream *PEResources::getResource(const WinResourceID &type, const WinResourceID &name) {
Array<PEResourceID> langList = getLangList(type, name); Array<WinResourceID> langList = getLangList(type, name);
if (langList.empty()) if (langList.empty())
return 0; return 0;
@ -286,7 +233,7 @@ SeekableReadStream *PEResources::getResource(const PEResourceID &type, const PER
return _exe->readStream(resource.size); return _exe->readStream(resource.size);
} }
SeekableReadStream *PEResources::getResource(const PEResourceID &type, const PEResourceID &name, const PEResourceID &lang) { SeekableReadStream *PEResources::getResource(const WinResourceID &type, const WinResourceID &name, const WinResourceID &lang) {
if (!_exe || !_resources.contains(type)) if (!_exe || !_resources.contains(type))
return 0; return 0;

View file

@ -23,55 +23,18 @@
* *
*/ */
#ifndef COMMON_PE_EXE_H #ifndef COMMON_WINEXE_PE_H
#define COMMON_PE_EXE_H #define COMMON_WINEXE_PE_H
#include "common/array.h" #include "common/array.h"
#include "common/hashmap.h" #include "common/hashmap.h"
#include "common/hash-str.h" #include "common/winexe.h"
namespace Common { namespace Common {
class SeekableReadStream; class SeekableReadStream;
class String; class String;
class PEResourceID {
public:
PEResourceID() { _idType = kIDTypeNull; }
PEResourceID(String x) { _idType = kIDTypeString; _name = x; }
PEResourceID(uint32 x) { _idType = kIDTypeNumerical; _id = x; }
PEResourceID &operator=(String string);
PEResourceID &operator=(uint32 x);
bool operator==(const String &x) const;
bool operator==(const uint32 &x) const;
bool operator==(const PEResourceID &x) const;
String getString() const;
uint32 getID() const;
String toString() const;
private:
/** An ID Type. */
enum IDType {
kIDTypeNull, ///< No type set
kIDTypeNumerical, ///< A numerical ID.
kIDTypeString ///< A string ID.
} _idType;
String _name; ///< The resource's string ID.
uint32 _id; ///< The resource's numerical ID.
};
struct PEResourceID_Hash {
uint operator()(const PEResourceID &id) const { return hashit(id.toString()); }
};
struct PEResourceID_EqualTo {
bool operator()(const PEResourceID &id1, const PEResourceID &id2) const { return id1 == id2; }
};
/** The default Windows PE resources. */ /** The default Windows PE resources. */
enum PEResourceType { enum PEResourceType {
kPECursor = 0x01, kPECursor = 0x01,
@ -114,19 +77,19 @@ public:
bool loadFromEXE(SeekableReadStream *stream); bool loadFromEXE(SeekableReadStream *stream);
/** Return a list of resource types. */ /** Return a list of resource types. */
const Array<PEResourceID> getTypeList() const; const Array<WinResourceID> getTypeList() const;
/** Return a list of names for a given type. */ /** Return a list of names for a given type. */
const Array<PEResourceID> getNameList(const PEResourceID &type) const; const Array<WinResourceID> getNameList(const WinResourceID &type) const;
/** Return a list of languages for a given type and name. */ /** Return a list of languages for a given type and name. */
const Array<PEResourceID> getLangList(const PEResourceID &type, const PEResourceID &name) const; const Array<WinResourceID> getLangList(const WinResourceID &type, const WinResourceID &name) const;
/** Return a stream to the specified resource, taking the first language found (or 0 if non-existent). */ /** Return a stream to the specified resource, taking the first language found (or 0 if non-existent). */
SeekableReadStream *getResource(const PEResourceID &type, const PEResourceID &name); SeekableReadStream *getResource(const WinResourceID &type, const WinResourceID &name);
/** Return a stream to the specified resource (or 0 if non-existent). */ /** Return a stream to the specified resource (or 0 if non-existent). */
SeekableReadStream *getResource(const PEResourceID &type, const PEResourceID &name, const PEResourceID &lang); SeekableReadStream *getResource(const WinResourceID &type, const WinResourceID &name, const WinResourceID &lang);
private: private:
struct Section { struct Section {
@ -140,16 +103,16 @@ private:
SeekableReadStream *_exe; SeekableReadStream *_exe;
void parseResourceLevel(Section &section, uint32 offset, int level); void parseResourceLevel(Section &section, uint32 offset, int level);
PEResourceID _curType, _curName, _curLang; WinResourceID _curType, _curName, _curLang;
struct Resource { struct Resource {
uint32 offset; uint32 offset;
uint32 size; uint32 size;
}; };
typedef HashMap<PEResourceID, Resource, PEResourceID_Hash, PEResourceID_EqualTo> LangMap; typedef HashMap<WinResourceID, Resource, WinResourceID_Hash, WinResourceID_EqualTo> LangMap;
typedef HashMap<PEResourceID, LangMap, PEResourceID_Hash, PEResourceID_EqualTo> NameMap; typedef HashMap<WinResourceID, LangMap, WinResourceID_Hash, WinResourceID_EqualTo> NameMap;
typedef HashMap<PEResourceID, NameMap, PEResourceID_Hash, PEResourceID_EqualTo> TypeMap; typedef HashMap<WinResourceID, NameMap, WinResourceID_Hash, WinResourceID_EqualTo> TypeMap;
TypeMap _resources; TypeMap _resources;
}; };

View file

@ -31,8 +31,8 @@
#include "mohawk/riven_cursors.h" #include "mohawk/riven_cursors.h"
#include "common/macresman.h" #include "common/macresman.h"
#include "common/ne_exe.h"
#include "common/system.h" #include "common/system.h"
#include "common/winexe_ne.h"
#include "graphics/cursorman.h" #include "graphics/cursorman.h"
namespace Mohawk { namespace Mohawk {

View file

@ -31,7 +31,7 @@
#define SCUMM_HE_RESOURCE_HE_H #define SCUMM_HE_RESOURCE_HE_H
#include "common/macresman.h" #include "common/macresman.h"
#include "common/pe_exe.h" #include "common/winexe_pe.h"
namespace Scumm { namespace Scumm {

View file

@ -23,8 +23,8 @@
*/ */
#include "common/file.h" #include "common/file.h"
#include "common/ne_exe.h"
#include "common/str.h" #include "common/str.h"
#include "common/winexe_ne.h"
#include "graphics/fonts/winfont.h" #include "graphics/fonts/winfont.h"
namespace Graphics { namespace Graphics {