1 #include "UMPSystem.h"
2 
3 #include "radiant_i18n.h"
4 
5 #include "ifilesystem.h"
6 #include "iradiant.h"
7 #include "ieventmanager.h"
8 
9 #include "gtkutil/dialog.h"
10 #include "os/path.h"
11 
12 #include "UMPFile.h"
13 #include "UMPTile.h"
14 #include "../ui/umpeditor/UMPEditor.h"
15 #include "../map/map.h"
16 
UMPCollector(std::set<std::string> & list)17 UMPSystem::UMPCollector::UMPCollector (std::set<std::string> &list) :
18 	_list(list)
19 {
20 	GlobalFileSystem().forEachFile("maps/", "*", makeCallback1(*this), 0);
21 	std::size_t size = _list.size();
22 	globalOutputStream() << "Found " << string::toString(size) << " ump files\n";
23 }
24 
25 // Functor operator needed for the forEachFile() call
operator ()(const std::string & file)26 void UMPSystem::UMPCollector::operator() (const std::string& file)
27 {
28 	std::string extension = os::getExtension(file);
29 
30 	if (extension == "ump")
31 		_list.insert(file);
32 }
33 
editUMPDefinition()34 void UMPSystem::editUMPDefinition ()
35 {
36 	const std::string umpFileName = getUMPFilename(GlobalMap().getName());
37 	if (umpFileName.empty()) {
38 		gtkutil::infoDialog(_("Could not find the map in any ump file"));
39 		return;
40 	}
41 	ui::UMPEditor editor(map::getMapsPath() + umpFileName);
42 	editor.show();
43 }
44 
45 /**
46  * @return A vector with ump filesnames
47  */
getFiles() const48 const std::set<std::string> UMPSystem::getFiles () const
49 {
50 	return _umpFiles;
51 }
52 
init()53 void UMPSystem::init ()
54 {
55 	UMPCollector collector(_umpFiles);
56 	// TODO: Register observer for base directory
57 
58 	GlobalEventManager().addCommand("EditUMPDefinition", MemberCaller<IUMPSystem, &IUMPSystem::editUMPDefinition> (GlobalUMPSystem()));
59 }
60 
61 /**
62  * @return The ump filename for the given map
63  */
getUMPFilename(const std::string & map)64 std::string UMPSystem::getUMPFilename (const std::string& map)
65 {
66 	if (map.empty())
67 		return "";
68 
69 	UMPFileMap::const_iterator i = _umpFileMap.find(map);
70 	if (i != _umpFileMap.end())
71 		return i->second;
72 
73 	for (UMPFilesIterator i = _umpFiles.begin(); i != _umpFiles.end(); ++i) {
74 		try {
75 			map::ump::UMPFile umpFile(*i);
76 			if (!umpFile.load()) {
77 				globalErrorStream() << "Could not load the ump file " << (*i) << "\n";
78 				continue;
79 			}
80 
81 			map::ump::UMPTile* tile = umpFile.getTileForMap(map);
82 			if (tile) {
83 				_umpFileMap[map] = *i;
84 				return *i;
85 			}
86 		} catch (map::ump::UMPException& e) {
87 			globalErrorStream() << e.getMessage() << "\n";
88 			gtkutil::errorDialog(e.getMessage());
89 		}
90 	}
91 	// not found in any ump file
92 	_umpFileMap[map] = "";
93 	return "";
94 }
95