Debian Codegraph @

plugins/dm.editing/MissionInfoTextFile.cpp

raw
1 #include "MissionInfoTextFile.h"
2
3 #include <stdexcept>
4 #include <fstream>
5 #include <fmt/format.h>
6
7 #include "i18n.h"
8 #include "itextstream.h"
9 #include "igame.h"
10
11 #include "os/path.h"
12
13 namespace map
14 {
15
16 std::string MissionInfoTextFile::getFullOutputPath()
17 {
18 return GetOutputPathForCurrentMod() + getFilename();
19 }
20
21 void MissionInfoTextFile::saveToCurrentMod()
22 {
23 std::string outputPath = getFullOutputPath();
24
25 rMessage() << "Writing " << getFilename() << " contents to " << outputPath << std::endl;
26
27 std::ofstream outputStream;
28
29 // Let the stream throw exceptions
30 std::ios_base::iostate exceptionMask = outputStream.exceptions() | std::ios::failbit;
31 outputStream.exceptions(exceptionMask);
32
33 try
34 {
35 outputStream.open(outputPath);
36 outputStream << toString();
37 outputStream.close();
38
39 rMessage() << "Successfully wrote " << getFilename() << " contents to " << outputPath << std::endl;
40 }
41 catch (std::ios_base::failure& ex)
42 {
43 throw std::runtime_error(fmt::format(_("Could not write {0} contents:\n{1}"), getFilename(), ex.what()));
44 }
45 }
46
47 std::string MissionInfoTextFile::GetOutputPathForCurrentMod()
48 {
49 std::string modPath = GlobalGameManager().getModPath();
50
51 if (modPath.empty())
52 {
53 rMessage() << "Mod path empty, falling back to mod base path..." << std::endl;
54 modPath = GlobalGameManager().getModBasePath();
55
56 if (modPath.empty())
57 {
58 rMessage() << "Mod base path empty as well, falling back to user engine path..." << std::endl;
59 modPath = GlobalGameManager().getUserEnginePath();
60 }
61 }
62
63 return os::standardPathWithSlash(modPath);
64 }
65
66 }