refactor: Use file extensions in ResourceType, extract IResourceProvider

This commit is contained in:
Vsevolod Kremianskii 2021-01-19 10:53:12 +07:00
parent 3cbb978433
commit 793cb45b22
21 changed files with 168 additions and 120 deletions

View file

@ -105,6 +105,7 @@ set(RESOURCE_HEADERS
src/resource/ltrfile.h
src/resource/lytfile.h
src/resource/pefile.h
src/resource/resourceprovider.h
src/resource/resources.h
src/resource/rimfile.h
src/resource/tlkfile.h

View file

@ -43,7 +43,7 @@ void Blueprints::invalidateCache() {
}
shared_ptr<CreatureBlueprint> Blueprints::getCreature(const string &resRef) {
return get<CreatureBlueprint>(resRef, ResourceType::CreatureBlueprint, _creatureCache);
return get<CreatureBlueprint>(resRef, ResourceType::Utc, _creatureCache);
}
template <class T>
@ -66,23 +66,23 @@ shared_ptr<T> Blueprints::doGet(const string &resRef, ResourceType type) {
}
shared_ptr<DoorBlueprint> Blueprints::getDoor(const string &resRef) {
return get<DoorBlueprint>(resRef, ResourceType::DoorBlueprint, _doorCache);
return get<DoorBlueprint>(resRef, ResourceType::Utd, _doorCache);
}
shared_ptr<ItemBlueprint> Blueprints::getItem(const string &resRef) {
return get<ItemBlueprint>(resRef, ResourceType::ItemBlueprint, _itemCache);
return get<ItemBlueprint>(resRef, ResourceType::Uti, _itemCache);
}
shared_ptr<PlaceableBlueprint> Blueprints::getPlaceable(const string &resRef) {
return get<PlaceableBlueprint>(resRef, ResourceType::PlaceableBlueprint, _placeableCache);
return get<PlaceableBlueprint>(resRef, ResourceType::Utp, _placeableCache);
}
shared_ptr<SoundBlueprint> Blueprints::getSound(const string &resRef) {
return get<SoundBlueprint>(resRef, ResourceType::SoundBlueprint, _soundCache);
return get<SoundBlueprint>(resRef, ResourceType::Uts, _soundCache);
}
shared_ptr<TriggerBlueprint> Blueprints::getTrigger(const string &resRef) {
return get<TriggerBlueprint>(resRef, ResourceType::TriggerBlueprint, _triggerCache);
return get<TriggerBlueprint>(resRef, ResourceType::Utt, _triggerCache);
}
} // namespace game

View file

@ -221,7 +221,7 @@ void Game::loadModule(const string &name, string entry) {
if (maybeModule != _loadedModules.end()) {
_module = maybeModule->second;
} else {
shared_ptr<GffStruct> ifo(Resources::instance().getGFF("module", ResourceType::ModuleInfo));
shared_ptr<GffStruct> ifo(Resources::instance().getGFF("module", ResourceType::Ifo));
_module = _objectFactory->newModule();
_module->load(name, *ifo);

View file

@ -56,7 +56,7 @@ void NameEntry::load() {
}
void NameEntry::loadLtrFile(const string &resRef, LtrFile &ltr) {
shared_ptr<ByteArray> data(Resources::instance().get(resRef, ResourceType::LetterComboProbability));
shared_ptr<ByteArray> data(Resources::instance().get(resRef, ResourceType::Ltr));
ltr.load(wrap(data));
}

View file

@ -169,7 +169,7 @@ void DialogGUI::onListBoxItemClick(const string &control, const string &item) {
}
void DialogGUI::startDialog(const shared_ptr<SpatialObject> &owner, const string &resRef) {
shared_ptr<GffStruct> dlg(Resources::instance().getGFF(resRef, ResourceType::Conversation));
shared_ptr<GffStruct> dlg(Resources::instance().getGFF(resRef, ResourceType::Dlg));
if (!dlg) {
_game->openInGame();
return;

View file

@ -105,7 +105,7 @@ void Area::load(const string &name, const GffStruct &are, const GffStruct &git)
void Area::loadLYT() {
LytFile lyt;
lyt.load(wrap(Resources::instance().get(_name, ResourceType::AreaLayout)));
lyt.load(wrap(Resources::instance().get(_name, ResourceType::Lyt)));
for (auto &lytRoom : lyt.rooms()) {
shared_ptr<Model> model(Models::instance().get(lytRoom.name));
@ -117,7 +117,7 @@ void Area::loadLYT() {
sceneNode->setLocalTransform(glm::translate(glm::mat4(1.0f), position));
sceneNode->playAnimation("animloop1", kAnimationLoop);
shared_ptr<Walkmesh> walkmesh(Walkmeshes::instance().get(lytRoom.name, ResourceType::Walkmesh));
shared_ptr<Walkmesh> walkmesh(Walkmeshes::instance().get(lytRoom.name, ResourceType::Wok));
unique_ptr<Room> room(new Room(lytRoom.name, position, sceneNode, walkmesh));
_rooms.insert(make_pair(room->name(), move(room)));
@ -141,7 +141,7 @@ Visibility Area::fixVisibility(const Visibility &visibility) {
}
void Area::loadPTH() {
shared_ptr<GffStruct> pth(Resources::instance().getGFF(_name, ResourceType::Path));
shared_ptr<GffStruct> pth(Resources::instance().getGFF(_name, ResourceType::Pth));
Path path;
path.load(*pth);

View file

@ -82,7 +82,7 @@ void Door::loadBlueprint(const GffStruct &gffs) {
string modelName(boost::to_lower_copy(table->getString(_genericType, "modelname")));
_model = make_unique<ModelSceneNode>(_sceneGraph, Models::instance().get(modelName));
_walkmesh = Walkmeshes::instance().get(modelName + "0", ResourceType::DoorWalkmesh);
_walkmesh = Walkmeshes::instance().get(modelName + "0", ResourceType::Dwk);
}
void Door::open(const shared_ptr<Object> &triggerrer) {

View file

@ -91,8 +91,8 @@ void Module::loadInfo(const GffStruct &ifo) {
void Module::loadArea(const GffStruct &ifo) {
reone::info("Module: load area: " + _info.entryArea);
shared_ptr<GffStruct> are(Resources::instance().getGFF(_info.entryArea, ResourceType::Area));
shared_ptr<GffStruct> git(Resources::instance().getGFF(_info.entryArea, ResourceType::GameInstance));
shared_ptr<GffStruct> are(Resources::instance().getGFF(_info.entryArea, ResourceType::Are));
shared_ptr<GffStruct> git(Resources::instance().getGFF(_info.entryArea, ResourceType::Git));
shared_ptr<Area> area(_game->objectFactory().newArea());
area->load(_info.entryArea, *are, *git);

View file

@ -86,7 +86,7 @@ void Placeable::load(const shared_ptr<PlaceableBlueprint> &blueprint) {
_model = make_unique<ModelSceneNode>(_sceneGraph, Models::instance().get(modelName));
_model->setLightingEnabled(true);
_walkmesh = Walkmeshes::instance().get(modelName, ResourceType::PlaceableWalkmesh);
_walkmesh = Walkmeshes::instance().get(modelName, ResourceType::Pwk);
}
bool Placeable::hasInventory() const {

View file

@ -54,7 +54,7 @@ shared_ptr<Model> Models::get(const string &resRef) {
}
shared_ptr<Model> Models::doGet(const string &resRef) {
shared_ptr<ByteArray> mdlData(Resources::instance().get(resRef, ResourceType::Model));
shared_ptr<ByteArray> mdlData(Resources::instance().get(resRef, ResourceType::Mdl));
shared_ptr<ByteArray> mdxData(Resources::instance().get(resRef, ResourceType::Mdx));
shared_ptr<Model> model;

View file

@ -69,7 +69,7 @@ shared_ptr<Texture> Textures::doGet(const string &resRef, TextureType type) {
tga.load(wrap(tgaData));
texture = tga.texture();
shared_ptr<ByteArray> txiData(Resources::instance().get(resRef, ResourceType::ExtraTextureInfo, false));
shared_ptr<ByteArray> txiData(Resources::instance().get(resRef, ResourceType::Txi, false));
if (txiData) {
TxiFile txi;
txi.load(wrap(txiData));
@ -78,7 +78,7 @@ shared_ptr<Texture> Textures::doGet(const string &resRef, TextureType type) {
}
if (!texture) {
shared_ptr<ByteArray> tpcData(Resources::instance().get(resRef, ResourceType::Texture, false));
shared_ptr<ByteArray> tpcData(Resources::instance().get(resRef, ResourceType::Tpc, false));
if (tpcData) {
TpcFile tpc(resRef, type);
tpc.load(wrap(tpcData));

View file

@ -18,6 +18,7 @@
#pragma once
#include "binfile.h"
#include "resourceprovider.h"
#include "types.h"
namespace reone {

View file

@ -23,6 +23,7 @@
#include "../common/types.h"
#include "resourceprovider.h"
#include "types.h"
namespace reone {

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2020-2021 The reone project contributors
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <string>
#include "../common/types.h"
#include "types.h"
namespace reone {
namespace resource {
/**
* Common interface to query for game resources.
*/
class IResourceProvider {
public:
virtual ~IResourceProvider() {
}
virtual std::shared_ptr<ByteArray> find(const std::string &resRef, ResourceType type) = 0;
/**
* @return true if this resource provider supports the specified ResType,
* false otherwise
*/
virtual bool supports(ResourceType type) const = 0;
};
} // namespace resource
} // namespace reone

View file

@ -330,7 +330,7 @@ shared_ptr<GffStruct> Resources::getGFF(const string &resRef, ResourceType type)
shared_ptr<TalkTable> Resources::getTalkTable(const string &resRef) {
return findResource<TalkTable>(resRef, g_talkTableCache, [this, &resRef]() {
shared_ptr<ByteArray> data(get(resRef, ResourceType::Conversation));
shared_ptr<ByteArray> data(get(resRef, ResourceType::Dlg));
shared_ptr<TalkTable> table;
if (data) {

View file

@ -34,6 +34,7 @@
#include "gfffile.h"
#include "keyfile.h"
#include "pefile.h"
#include "resourceprovider.h"
#include "tlkfile.h"
namespace reone {

View file

@ -18,6 +18,7 @@
#pragma once
#include "binfile.h"
#include "resourceprovider.h"
#include "types.h"
namespace reone {

View file

@ -17,12 +17,10 @@
#pragma once
#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include "../common/types.h"
namespace reone {
namespace resource {
@ -32,74 +30,69 @@ enum class GameVersion {
TheSithLords
};
/**
* Used together with a ResRef to locate game resources.
*/
enum class ResourceType : uint16_t {
Invalid = 0xffff,
Inventory = 0,
Res = 0,
Bmp = 1,
Tga = 3,
Wav = 4,
PackedLayerTexture = 6,
Plt = 6,
Ini = 7,
Text = 10,
Model = 2002,
ScriptSource = 2009,
CompiledScript = 2010,
Area = 2012,
Tileset = 2013,
ModuleInfo = 2014,
Creature = 2015,
Walkmesh = 2016,
Txt = 10,
Mdl = 2002,
Nss = 2009,
Ncs = 2010,
Are = 2012,
Set = 2013,
Ifo = 2014,
Bic = 2015,
Wok = 2016,
TwoDa = 2017,
ExtraTextureInfo = 2022,
GameInstance = 2023,
Txi = 2022,
Git = 2023,
Bti = 2024,
ItemBlueprint = 2025,
Uti = 2025,
Btc = 2026,
CreatureBlueprint = 2027,
Conversation = 2029,
TilePalette = 2030,
TriggerBlueprint = 2032,
Utc = 2027,
Dlg = 2029,
Itp = 2030,
Utt = 2032,
Dds = 2033,
SoundBlueprint = 2035,
LetterComboProbability = 2036,
Uts = 2035,
Ltr = 2036,
Gff = 2037,
Faction = 2038,
EncounterBlueprint = 2040,
DoorBlueprint = 2042,
PlaceableBlueprint = 2044,
DefaultValues = 2045,
GameInstanceComments = 2046,
Fac = 2038,
Ute = 2040,
Utd = 2042,
Utp = 2044,
Dft = 2045,
Gic = 2046,
Gui = 2047,
MerchantBlueprint = 2051,
DoorWalkmesh = 2052,
PlaceableWalkmesh = 2053,
Journal = 2056,
Utm = 2051,
Dwk = 2052,
Pwk = 2053,
Jrl = 2056,
Mod = 2057,
WaypointBlueprint = 2058,
SoundSet = 2060,
ScriptDebugger = 2064,
PlotManager = 2065,
PlotWizardBlueprint = 2066,
AreaLayout = 3000,
Utw = 2058,
Ssf = 2060,
Ndb = 2064,
Ptm = 2065,
Ptt = 2066,
Lyt = 3000,
Vis = 3001,
Path = 3003,
Pth = 3003,
Lip = 3004,
Texture = 3007,
Tpc = 3007,
Mdx = 3008,
Mp3 = 4000
Mp3 = 4000,
Invalid = 0xffff
};
typedef std::multimap<std::string, std::string> Visibility;
class IResourceProvider {
public:
virtual ~IResourceProvider() {
}
virtual bool supports(ResourceType type) const = 0;
virtual std::shared_ptr<ByteArray> find(const std::string &resRef, ResourceType type) = 0;
};
} // namespace resource
} // namespace reone

View file

@ -30,57 +30,57 @@ namespace reone {
namespace resource {
static map<ResourceType, string> g_extByType = {
{ ResourceType::Inventory, "inv" },
{ ResourceType::Res, "inv" },
{ ResourceType::Bmp, "bmp" },
{ ResourceType::Tga, "tga" },
{ ResourceType::Wav, "wav" },
{ ResourceType::PackedLayerTexture, "plt" },
{ ResourceType::Plt, "plt" },
{ ResourceType::Ini, "init" },
{ ResourceType::Text, "txt" },
{ ResourceType::Model, "mdl" },
{ ResourceType::ScriptSource, "nss" },
{ ResourceType::CompiledScript, "ncs" },
{ ResourceType::Area, "are" },
{ ResourceType::Tileset, "set" },
{ ResourceType::ModuleInfo, "ifo" },
{ ResourceType::Creature, "bic" },
{ ResourceType::Walkmesh, "wok" },
{ ResourceType::Txt, "txt" },
{ ResourceType::Mdl, "mdl" },
{ ResourceType::Nss, "nss" },
{ ResourceType::Ncs, "ncs" },
{ ResourceType::Are, "are" },
{ ResourceType::Set, "set" },
{ ResourceType::Ifo, "ifo" },
{ ResourceType::Bic, "bic" },
{ ResourceType::Wok, "wok" },
{ ResourceType::TwoDa, "2da" },
{ ResourceType::ExtraTextureInfo, "txi" },
{ ResourceType::GameInstance, "git" },
{ ResourceType::Txi, "txi" },
{ ResourceType::Git, "git" },
{ ResourceType::Bti, "bti" },
{ ResourceType::ItemBlueprint, "uti" },
{ ResourceType::Uti, "uti" },
{ ResourceType::Btc, "btc" },
{ ResourceType::CreatureBlueprint, "utc" },
{ ResourceType::Conversation, "dlg" },
{ ResourceType::TilePalette, "itp" },
{ ResourceType::TriggerBlueprint, "utt" },
{ ResourceType::Utc, "utc" },
{ ResourceType::Dlg, "dlg" },
{ ResourceType::Itp, "itp" },
{ ResourceType::Utt, "utt" },
{ ResourceType::Dds, "dds" },
{ ResourceType::SoundBlueprint, "uts" },
{ ResourceType::LetterComboProbability, "ltr" },
{ ResourceType::Uts, "uts" },
{ ResourceType::Ltr, "ltr" },
{ ResourceType::Gff, "gff" },
{ ResourceType::Faction, "fac" },
{ ResourceType::EncounterBlueprint, "ute" },
{ ResourceType::DoorBlueprint, "utd" },
{ ResourceType::PlaceableBlueprint, "utp" },
{ ResourceType::DefaultValues, "dft" },
{ ResourceType::GameInstanceComments, "gic" },
{ ResourceType::Fac, "fac" },
{ ResourceType::Ute, "ute" },
{ ResourceType::Utd, "utd" },
{ ResourceType::Utp, "utp" },
{ ResourceType::Dft, "dft" },
{ ResourceType::Gic, "gic" },
{ ResourceType::Gui, "gui" },
{ ResourceType::MerchantBlueprint, "utm" },
{ ResourceType::DoorWalkmesh, "dwk" },
{ ResourceType::PlaceableWalkmesh, "pwk" },
{ ResourceType::Journal, "jrl" },
{ ResourceType::Utm, "utm" },
{ ResourceType::Dwk, "dwk" },
{ ResourceType::Pwk, "pwk" },
{ ResourceType::Jrl, "jrl" },
{ ResourceType::Mod, "mod" },
{ ResourceType::WaypointBlueprint, "utw" },
{ ResourceType::SoundSet, "ssf" },
{ ResourceType::ScriptDebugger, "ndb" },
{ ResourceType::PlotManager, "ptm" },
{ ResourceType::PlotWizardBlueprint, "ptt" },
{ ResourceType::AreaLayout, "lyt" },
{ ResourceType::Utw, "utw" },
{ ResourceType::Ssf, "ssf" },
{ ResourceType::Ndb, "ndb" },
{ ResourceType::Ptm, "ptm" },
{ ResourceType::Ptt, "ptt" },
{ ResourceType::Lyt, "lyt" },
{ ResourceType::Vis, "vis" },
{ ResourceType::Path, "pth" },
{ ResourceType::Pth, "pth" },
{ ResourceType::Lip, "lip" },
{ ResourceType::Texture, "tpc" },
{ ResourceType::Tpc, "tpc" },
{ ResourceType::Mdx, "mdx" },
{ ResourceType::Mp3, "mp3" } };

View file

@ -50,7 +50,7 @@ shared_ptr<ScriptProgram> Scripts::get(const string &resRef) {
}
shared_ptr<ScriptProgram> Scripts::doGet(const string &resRef) {
shared_ptr<ByteArray> data(Resources::instance().get(resRef, ResourceType::CompiledScript));
shared_ptr<ByteArray> data(Resources::instance().get(resRef, ResourceType::Ncs));
shared_ptr<ScriptProgram> program;
if (data) {

View file

@ -86,7 +86,7 @@ pt::ptree ModuleProbe::describeModule() {
pt::ptree description;
GffFile ifo;
ifo.load(wrap(_rimMain.find("module", ResourceType::ModuleInfo)));
ifo.load(wrap(_rimMain.find("module", ResourceType::Ifo)));
auto ifoGffs = ifo.top();
// Entry
@ -148,15 +148,15 @@ pt::ptree ModuleProbe::describeModule() {
pt::ptree ModuleProbe::describeArea(const string &name, const TwoDaTable &appearance, const resource::TwoDaTable &placeables) {
GffFile are;
are.load(wrap(_rimMain.find(name, ResourceType::Area)));
are.load(wrap(_rimMain.find(name, ResourceType::Are)));
auto areGffs = are.top();
GffFile git;
git.load(wrap(_rimMain.find(name, ResourceType::GameInstance)));
git.load(wrap(_rimMain.find(name, ResourceType::Git)));
auto gitGffs = git.top();
LytFile lyt;
lyt.load(wrap(getResource(name, ResourceType::AreaLayout)));
lyt.load(wrap(getResource(name, ResourceType::Lyt)));
// Rooms
@ -311,7 +311,7 @@ pt::ptree ModuleProbe::describeCreature(const GffStruct &gitCreature, const TwoD
orientation.put("y", gitCreature.getFloat("YOrientation"));
GffFile utc;
utc.load(wrap(getResource(gitCreature.getString("TemplateResRef"), ResourceType::CreatureBlueprint)));
utc.load(wrap(getResource(gitCreature.getString("TemplateResRef"), ResourceType::Utc)));
auto utcGffs = utc.top();
pt::ptree equipment;
@ -372,7 +372,7 @@ pt::ptree ModuleProbe::describeCreature(const GffStruct &gitCreature, const TwoD
pt::ptree ModuleProbe::describeDoor(const GffStruct &gitDoor) {
GffFile utd;
utd.load(wrap(getResource(gitDoor.getString("TemplateResRef"), ResourceType::DoorBlueprint)));
utd.load(wrap(getResource(gitDoor.getString("TemplateResRef"), ResourceType::Utd)));
auto utdGffs = utd.top();
pt::ptree scripts;
@ -417,7 +417,7 @@ pt::ptree ModuleProbe::describeDoor(const GffStruct &gitDoor) {
pt::ptree ModuleProbe::describePlaceable(const GffStruct &gitPlaceable, const TwoDaTable &placeables) {
GffFile utp;
utp.load(wrap(getResource(gitPlaceable.getString("TemplateResRef"), ResourceType::PlaceableBlueprint)));
utp.load(wrap(getResource(gitPlaceable.getString("TemplateResRef"), ResourceType::Utp)));
auto utpGffs = utp.top();
pt::ptree items;
@ -472,7 +472,7 @@ pt::ptree ModuleProbe::describePlaceable(const GffStruct &gitPlaceable, const Tw
pt::ptree ModuleProbe::describeTrigger(const GffStruct &gitTrigger) {
GffFile utt;
utt.load(wrap(getResource(gitTrigger.getString("TemplateResRef"), ResourceType::TriggerBlueprint)));
utt.load(wrap(getResource(gitTrigger.getString("TemplateResRef"), ResourceType::Utt)));
auto uttGffs = utt.top();
pt::ptree position;
@ -542,7 +542,7 @@ pt::ptree ModuleProbe::describeWaypoint(const GffStruct &gitWaypoint) {
pt::ptree ModuleProbe::describeSound(const GffStruct &gitSound) {
GffFile uts;
uts.load(wrap(getResource(gitSound.getString("TemplateResRef"), ResourceType::SoundBlueprint)));
uts.load(wrap(getResource(gitSound.getString("TemplateResRef"), ResourceType::Uts)));
auto utsGffs = uts.top();
pt::ptree position;