Skip to content

Commit

Permalink
#5231: Refactoring and renaming in ImageLoader modules.
Browse files Browse the repository at this point in the history
Move static file extension list to the module instance itself.
  • Loading branch information
codereader committed May 3, 2020
1 parent 150cd31 commit 30bd303
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 97 deletions.
42 changes: 10 additions & 32 deletions include/iimage.h
@@ -1,26 +1,4 @@
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant 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.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#if !defined(INCLUDED_IIMAGE_H)
#define INCLUDED_IIMAGE_H
#pragma once

#include "igl.h"
#include "imodule.h"
Expand Down Expand Up @@ -82,7 +60,7 @@ typedef std::shared_ptr<Image> ImagePtr;
class ArchiveFile;

/// Module responsible for loading images from VFS or disk filesystem
class ImageLoader :
class IImageLoader :
public RegisterableModule
{
public:
Expand All @@ -107,15 +85,15 @@ class ImageLoader :
virtual ImagePtr imageFromFile(const std::string& filename) const = 0;
};

typedef std::shared_ptr<ImageLoader> ImageLoaderPtr;

const std::string MODULE_IMAGELOADER("ImageLoader");
const char* const MODULE_IMAGELOADER("ImageLoader");

inline const ImageLoader& GlobalImageLoader()
inline const IImageLoader& GlobalImageLoader()
{
return *std::static_pointer_cast<ImageLoader>(
module::GlobalModuleRegistry().getModule(MODULE_IMAGELOADER)
static IImageLoader& _imageLoader(
*std::static_pointer_cast<IImageLoader>(
module::GlobalModuleRegistry().getModule(MODULE_IMAGELOADER)
)
);
}

#endif // _IIMAGE_H
return _imageLoader;
}
2 changes: 1 addition & 1 deletion radiantcore/Makefile.am
Expand Up @@ -36,7 +36,7 @@ libradiantcore_la_SOURCES = Radiant.cpp \
imagefile/dds.cpp \
imagefile/ddslib.cpp \
imagefile/DDSImage.cpp \
imagefile/Doom3ImageLoader.cpp \
imagefile/ImageLoader.cpp \
imagefile/ImageLoaderWx.cpp \
imagefile/TGALoader.cpp \
log/COutRedirector.cpp \
Expand Down
@@ -1,4 +1,4 @@
#include "Doom3ImageLoader.h"
#include "ImageLoader.h"
#include "ImageLoaderWx.h"
#include "TGALoader.h"
#include "dds.h"
Expand All @@ -19,45 +19,21 @@ namespace image

namespace
{

// Registry key holding texture types
const char* RKEY_IMAGE_TYPES = "/filetypes/texture//extension";

ImageTypeLoader::Extensions getGameFileImageExtensions()
{
static ImageTypeLoader::Extensions _extensions;

if (_extensions.empty())
{
// Load the texture types from the .game file
xml::NodeList texTypes = GlobalGameManager().currentGame()->getLocalXPath(RKEY_IMAGE_TYPES);

for (xml::NodeList::const_iterator i = texTypes.begin();
i != texTypes.end();
++i)
{
// Get the file extension
std::string extension = i->getContent();
string::to_lower(extension);
_extensions.push_back(extension);
}
}

return _extensions;
// Registry key holding texture types
const char* const GKEY_IMAGE_TYPES = "/filetypes/texture//extension";
}

} // namespace

void Doom3ImageLoader::addLoaderToMap(ImageTypeLoader::Ptr loader)
void ImageLoader::addLoaderToMap(const ImageTypeLoader::Ptr& loader)
{
ImageTypeLoader::Extensions exts = loader->getExtensions();
for (auto i = exts.begin(); i != exts.end(); ++i)
auto extensions = loader->getExtensions();

for (const auto& extension : extensions)
{
_loadersByExtension[string::to_lower_copy(*i)] = loader;
_loadersByExtension.emplace(string::to_lower_copy(extension), loader);
}
}

Doom3ImageLoader::Doom3ImageLoader()
ImageLoader::ImageLoader()
{
// Wx loader (this handles regular image file types like BMP and PNG)
addLoaderToMap(std::make_shared<ImageLoaderWx>());
Expand All @@ -70,31 +46,31 @@ Doom3ImageLoader::Doom3ImageLoader()
}

// Load image from VFS
ImagePtr Doom3ImageLoader::imageFromVFS(const std::string& name) const
ImagePtr ImageLoader::imageFromVFS(const std::string& name) const
{
const ImageTypeLoader::Extensions exts = getGameFileImageExtensions();
for (auto i = exts.begin(); i != exts.end(); ++i)
for (const auto& extension : _extensions)
{
// Find the loader for this extension
auto loaderIter = _loadersByExtension.find(*i);
auto loaderIter = _loadersByExtension.find(extension);

if (loaderIter == _loadersByExtension.end())
{
rWarning() << "Doom3ImageLoader: failed to find loader"
" for extension '" << *i << "'" << std::endl;
" for extension '" << extension << "'" << std::endl;
continue;
}

ImageTypeLoader& ldr = *loaderIter->second;

// Construct the full name of the image to load, including the
// prefix (e.g. "dds/") and the file extension.
std::string fullName = ldr.getPrefix() + name + "." + *i;
std::string fullName = ldr.getPrefix() + name + "." + extension;

// Try to open the file (will fail if the extension does not fit)
ArchiveFilePtr file = GlobalFileSystem().openFile(fullName);
auto file = GlobalFileSystem().openFile(fullName);

// Has the file been loaded?
if (file != NULL)
if (file)
{
// Try to invoke the imageloader with a reference to the
// ArchiveFile
Expand All @@ -106,13 +82,12 @@ ImagePtr Doom3ImageLoader::imageFromVFS(const std::string& name) const
return ImagePtr();
}

ImagePtr Doom3ImageLoader::imageFromFile(const std::string& filename) const
ImagePtr ImageLoader::imageFromFile(const std::string& filename) const
{
ImagePtr image;

// Construct a DirectoryArchiveFile out of the filename
std::shared_ptr<archive::DirectoryArchiveFile> file =
std::make_shared<archive::DirectoryArchiveFile>(filename, filename);
auto file = std::make_shared<archive::DirectoryArchiveFile>(filename, filename);

if (!file->failed())
{
Expand All @@ -135,19 +110,38 @@ ImagePtr Doom3ImageLoader::imageFromFile(const std::string& filename) const
return image;
}

const std::string& Doom3ImageLoader::getName() const
const std::string& ImageLoader::getName() const
{
static std::string _name(MODULE_IMAGELOADER);
return _name;
}

const StringSet& Doom3ImageLoader::getDependencies() const
const StringSet& ImageLoader::getDependencies() const
{
static StringSet _dependencies; // no dependencies
static StringSet _dependencies;

if (_dependencies.empty())
{
_dependencies.insert(MODULE_GAMEMANAGER);
}

return _dependencies;
}

void ImageLoader::initialiseModule(const ApplicationContext&)
{
// Load the texture types from the .game file
auto texTypes = GlobalGameManager().currentGame()->getLocalXPath(GKEY_IMAGE_TYPES);

for (const auto& node : texTypes)
{
// Get the file extension, store it as lowercase
std::string extension = node.getContent();
_extensions.emplace_back(string::to_lower_copy(extension));
}
}

// Static module instance
module::StaticModule<Doom3ImageLoader> doom3ImageLoaderModule;
module::StaticModule<ImageLoader> imageLoaderModule;

} // namespace shaders
Expand Up @@ -9,29 +9,33 @@ namespace image
{

/// ImageLoader implementing module
class Doom3ImageLoader: public ImageLoader
class ImageLoader:
public IImageLoader
{
private:
// Map of image extension to loader class. Multiple image extensions may
// map to the same loader class.
typedef std::map<std::string, ImageTypeLoader::Ptr> LoadersByExtension;
LoadersByExtension _loadersByExtension;

ImageTypeLoader::Extensions _extensions;

private:
void addLoaderToMap(ImageTypeLoader::Ptr loader);
void addLoaderToMap(const ImageTypeLoader::Ptr& loader);

public:

// Construct and initialise loaders
Doom3ImageLoader();
ImageLoader();

// ImageLoader implementation
ImagePtr imageFromVFS(const std::string& vfsPath) const;
ImagePtr imageFromFile(const std::string& filename) const;
ImagePtr imageFromVFS(const std::string& vfsPath) const override;
ImagePtr imageFromFile(const std::string& filename) const override;

// RegisterableModule implementation
const std::string& getName() const;
const StringSet& getDependencies() const;
void initialiseModule(const ApplicationContext&) { }
const std::string& getName() const override;
const StringSet& getDependencies() const override;
void initialiseModule(const ApplicationContext&) override;
};

}
4 changes: 2 additions & 2 deletions tools/msvc/DarkRadiantCore.vcxproj
Expand Up @@ -22,7 +22,7 @@
<ClCompile Include="..\..\radiantcore\imagefile\dds.cpp" />
<ClCompile Include="..\..\radiantcore\imagefile\DDSImage.cpp" />
<ClCompile Include="..\..\radiantcore\imagefile\ddslib.cpp" />
<ClCompile Include="..\..\radiantcore\imagefile\Doom3ImageLoader.cpp" />
<ClCompile Include="..\..\radiantcore\imagefile\ImageLoader.cpp" />
<ClCompile Include="..\..\radiantcore\imagefile\ImageLoaderWx.cpp" />
<ClCompile Include="..\..\radiantcore\imagefile\TGALoader.cpp" />
<ClCompile Include="..\..\radiantcore\map\format\MapFormatManager.cpp" />
Expand Down Expand Up @@ -59,7 +59,7 @@
<ClInclude Include="..\..\radiantcore\imagefile\dds.h" />
<ClInclude Include="..\..\radiantcore\imagefile\DDSImage.h" />
<ClInclude Include="..\..\radiantcore\imagefile\ddslib.h" />
<ClInclude Include="..\..\radiantcore\imagefile\Doom3ImageLoader.h" />
<ClInclude Include="..\..\radiantcore\imagefile\ImageLoader.h" />
<ClInclude Include="..\..\radiantcore\imagefile\ImageLoaderWx.h" />
<ClInclude Include="..\..\radiantcore\imagefile\ImageTypeLoader.h" />
<ClInclude Include="..\..\radiantcore\imagefile\TGALoader.h" />
Expand Down
12 changes: 6 additions & 6 deletions tools/msvc/DarkRadiantCore.vcxproj.filters
Expand Up @@ -139,15 +139,15 @@
<ClCompile Include="..\..\radiantcore\imagefile\ddslib.cpp">
<Filter>src\imagefile</Filter>
</ClCompile>
<ClCompile Include="..\..\radiantcore\imagefile\Doom3ImageLoader.cpp">
<Filter>src\imagefile</Filter>
</ClCompile>
<ClCompile Include="..\..\radiantcore\imagefile\ImageLoaderWx.cpp">
<Filter>src\imagefile</Filter>
</ClCompile>
<ClCompile Include="..\..\radiantcore\imagefile\TGALoader.cpp">
<Filter>src\imagefile</Filter>
</ClCompile>
<ClCompile Include="..\..\radiantcore\imagefile\ImageLoader.cpp">
<Filter>src\imagefile</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\radiantcore\modulesystem\ModuleLoader.h">
Expand Down Expand Up @@ -318,9 +318,6 @@
<ClInclude Include="..\..\radiantcore\imagefile\ddslib.h">
<Filter>src\imagefile</Filter>
</ClInclude>
<ClInclude Include="..\..\radiantcore\imagefile\Doom3ImageLoader.h">
<Filter>src\imagefile</Filter>
</ClInclude>
<ClInclude Include="..\..\radiantcore\imagefile\ImageLoaderWx.h">
<Filter>src\imagefile</Filter>
</ClInclude>
Expand All @@ -330,5 +327,8 @@
<ClInclude Include="..\..\radiantcore\imagefile\TGALoader.h">
<Filter>src\imagefile</Filter>
</ClInclude>
<ClInclude Include="..\..\radiantcore\imagefile\ImageLoader.h">
<Filter>src\imagefile</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 30bd303

Please sign in to comment.