| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "inode.h" |
| 4 |
#include "imodule.h" |
| 5 |
#include "itextstream.h" |
| 6 |
#include "imap.h" |
| 7 |
|
| 8 |
#include <sigc++/signal.h> |
| 9 |
|
| 10 |
namespace map |
| 11 |
{ |
| 12 |
class MapFormat; |
| 13 |
typedef std::shared_ptr<MapFormat> MapFormatPtr; |
| 14 |
} |
| 15 |
|
| 16 |
class IMapResource |
| 17 |
{ |
| 18 |
public: |
| 19 |
virtual ~IMapResource() {} |
| 20 |
|
| 21 |
// Renames this map resource to the new path |
| 22 |
virtual void rename(const std::string& fullPath) = 0; |
| 23 |
|
| 24 |
/** |
| 25 |
* Attempts to load the resource from disk. Returns true |
| 26 |
* on success, in which case the getRootNode() method can be |
| 27 |
* used to acquire a reference to the parsed map. |
| 28 |
* Will throw an OperationException on failure. |
| 29 |
*/ |
| 30 |
virtual bool load() = 0; |
| 31 |
|
| 32 |
// Exception type thrown by the the MapResource implementation |
| 33 |
class OperationException : |
| 34 |
public std::runtime_error |
| 35 |
{ |
| 36 |
private: |
| 37 |
bool _cancelled; |
| 38 |
|
| 39 |
public: |
| 40 |
OperationException(const std::string& msg) : |
| 41 |
OperationException(msg, false) |
| 42 |
{} |
| 43 |
|
| 44 |
OperationException(const std::string& msg, bool cancelled) : |
| 45 |
runtime_error(msg), |
| 46 |
_cancelled(cancelled) |
| 47 |
{ |
| 48 |
if (!_cancelled) |
| 49 |
{ |
| 50 |
rMessage() << "MapResource Operation: " << msg << std::endl; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// Returns true if the operation has been cancelled by the user |
| 55 |
bool operationCancelled() const |
| 56 |
{ |
| 57 |
return _cancelled; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns true if this resource refers to a read-only location. |
| 63 |
* This returns true for archived resources (loaded from PAK files), |
| 64 |
* but might be returning true for other resource types too (e.g. when |
| 65 |
* this process is lacking write access to the resource). |
| 66 |
*/ |
| 67 |
virtual bool isReadOnly() = 0; |
| 68 |
|
| 69 |
/** |
| 70 |
* Save this resource |
| 71 |
* |
| 72 |
* It's possible to pass a mapformat to be used for saving. If the map |
| 73 |
* format argument is omitted, the format corresponding to the current |
| 74 |
* game type is used. |
| 75 |
* |
| 76 |
* Throws an OperationException if anything prevented the save from |
| 77 |
* completion (user cancellation, I/O errors) |
| 78 |
*/ |
| 79 |
virtual void save(const map::MapFormatPtr& mapFormat = map::MapFormatPtr()) = 0; |
| 80 |
|
| 81 |
virtual const scene::IMapRootNodePtr& getRootNode() = 0; |
| 82 |
|
| 83 |
// Sets the root node of this resource. The use case is to create a resource |
| 84 |
// around an existing map root for e.g. saving a read-only resource to a new path |
| 85 |
virtual void setRootNode(const scene::IMapRootNodePtr& root) = 0; |
| 86 |
|
| 87 |
virtual void clear() = 0; |
| 88 |
|
| 89 |
// Check if the target file on disk has been modified since it was last saved |
| 90 |
virtual bool fileOnDiskHasBeenModifiedSinceLastSave() = 0; |
| 91 |
|
| 92 |
// A signal that is emitted as soon as the map modified status changes due |
| 93 |
// to a regular edit, undo or redo. The bool value corresponds to the new modification state. |
| 94 |
virtual sigc::signal<void(bool)>& signal_modifiedStatusChanged() = 0; |
| 95 |
}; |
| 96 |
typedef std::shared_ptr<IMapResource> IMapResourcePtr; |
| 97 |
|
| 98 |
const char* const MODULE_MAPRESOURCEMANAGER("MapResourceManager"); |
| 99 |
|
| 100 |
class IMapResourceManager : |
| 101 |
public RegisterableModule |
| 102 |
{ |
| 103 |
public: |
| 104 |
/** |
| 105 |
* Create a named map resource from VFS or from a physical path. |
| 106 |
*/ |
| 107 |
virtual IMapResourcePtr createFromPath(const std::string& path) = 0; |
| 108 |
|
| 109 |
/** |
| 110 |
* Create a named map resource that is contained within a PAK archive |
| 111 |
* outside the VFS. |
| 112 |
* archivePath is the absolute path to the archive file, e.g. "/home/greebo/outpost.pk4" |
| 113 |
* filePathWithinArchive is the relative path within the archive, e.g. "maps/outpost.map" |
| 114 |
*/ |
| 115 |
virtual IMapResourcePtr createFromArchiveFile(const std::string& archivePath, |
| 116 |
const std::string& filePathWithinArchive) = 0; |
| 117 |
|
| 118 |
// Signal emitted when a MapExport is starting / is finished |
| 119 |
typedef sigc::signal<void, const scene::IMapRootNodePtr&> ExportEvent; |
| 120 |
|
| 121 |
// Event sent out right before a scene is sent to the exporter |
| 122 |
virtual ExportEvent& signal_onResourceExporting() = 0; |
| 123 |
|
| 124 |
// Event sent out right after a scene is sent to the exporter |
| 125 |
virtual ExportEvent& signal_onResourceExported() = 0; |
| 126 |
}; |
| 127 |
|
| 128 |
inline IMapResourceManager& GlobalMapResourceManager() |
| 129 |
{ |
| 130 |
static module::InstanceReference<IMapResourceManager> _reference(MODULE_MAPRESOURCEMANAGER); |
| 131 |
return _reference; |
| 132 |
} |