From bbf016443d737179b89cc08a7e742af304f3f0e1 Mon Sep 17 00:00:00 2001 From: twanvl Date: Sat, 7 Oct 2006 16:20:19 +0000 Subject: [PATCH] Package manager for maanging the data files git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@10 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/util/io/package.hpp | 2 +- src/util/io/package_manager.cpp | 37 ++++++++++++++++++++ src/util/io/package_manager.hpp | 60 +++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/util/io/package_manager.cpp create mode 100644 src/util/io/package_manager.hpp diff --git a/src/util/io/package.hpp b/src/util/io/package.hpp index 89dd4c08..02a4a46e 100644 --- a/src/util/io/package.hpp +++ b/src/util/io/package.hpp @@ -172,7 +172,7 @@ class Packaged : public Package { protected: /// filename of the data file, and extension of the package file - virtual String typeName() = 0; + virtual String typeName() const = 0; /// Can be overloaded to do validation after loading virtual void validate() {} diff --git a/src/util/io/package_manager.cpp b/src/util/io/package_manager.cpp new file mode 100644 index 00000000..b3d73215 --- /dev/null +++ b/src/util/io/package_manager.cpp @@ -0,0 +1,37 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +// ----------------------------------------------------------------------------- : Includes + +#include +#include + +// ----------------------------------------------------------------------------- : PackageManager + +String programDir() { + return _("."); //TODO +} + +PackageManager packages; + + +PackageManager::PackageManager() { + // determine data directory + dataDirectory = programDir(); + // check if this is the actual data directory, especially during debugging, + // the data may be higher up: + // exe path = mse/build/debug/mse.exe + // data path = mse/data + while (!wxDirExists(dataDirectory + _("/data"))) { + String d = dataDirectory; + dataDirectory = wxPathOnly(dataDirectory); + if (d == dataDirectory) { + // we are at the root -> 'data' not found anywhere in the path -> fatal error + throw Error(_("The MSE data files can not be found, there should be a directory called 'data' with these files")); + } + } + dataDirectory += _("/data"); +} diff --git a/src/util/io/package_manager.hpp b/src/util/io/package_manager.hpp new file mode 100644 index 00000000..372fedfb --- /dev/null +++ b/src/util/io/package_manager.hpp @@ -0,0 +1,60 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +#ifndef HEADER_UTIL_IO_PACKAGE_MANAGER +#define HEADER_UTIL_IO_PACKAGE_MANAGER + +// ----------------------------------------------------------------------------- : Includes + +#include +#include +#include + +DECLARE_POINTER_TYPE(Packaged); + +// ----------------------------------------------------------------------------- : PackageManager + +/// Package manager, loads data files from the default data directory +/** The PackageManager ensures that each package is only loaded once. + * There is a single global instance of the PackageManager, called packages + */ +class PackageManager { + public: + PackageManager(); + + /// Open a package with the specified name (including extension) + template + shared_ptr open(const String& name) { + wxFileName fn(dataDirectory + _("/") + name); + fn.Normalize(); + String filename = fn.GetFullPath(); + // Is this package already loaded? + PackagedP& p = loadedPackages[filename]; + shared_ptr typedP = dynamic_pointer_cast(p); + if (typedP) { + return typedP; + } else { + // not loaded, or loaded with wrong type + p = typedP = new_shared(); + typedP->open(filename); + return typedP; + } + } + + /// Open a package with the specified name + /// the type of package is determined by its extension! + PackagedP openAnyPackage(const String& filename); + + private: + map loadedPackages; + String dataDirectory; +}; + +/// The global PackageManager instance +extern PackageManager packages; + +// ----------------------------------------------------------------------------- : EOF +#endif