liblcf
Loading...
Searching...
No Matches
lmt_reader.cpp
Go to the documentation of this file.
1/*
2 * This file is part of liblcf. Copyright (c) liblcf authors.
3 * https://github.com/EasyRPG/liblcf - https://easyrpg.org
4 *
5 * liblcf is Free/Libre Open Source Software, released under the MIT License.
6 * For the full copyright and license information, please view the COPYING
7 * file that was distributed with this source code.
8 */
9
10#include <fstream>
11#include <cerrno>
12#include <cstring>
13
14#include "lcf/lmt/reader.h"
15#include "lcf/lmt/chunks.h"
16#include "lcf/reader_util.h"
17#include "log.h"
18#include "reader_struct.h"
19
20namespace lcf {
21
22std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::Load(std::string_view filename, std::string_view encoding) {
23 std::ifstream stream(ToString(filename), std::ios::binary);
24 if (!stream.is_open()) {
25 Log::Error("Failed to open LMT file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
26 return nullptr;
27 }
28 return LMT_Reader::Load(stream, encoding);
29}
30
31bool LMT_Reader::Save(std::string_view filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine, std::string_view encoding, SaveOpt opt) {
32 std::ofstream stream(ToString(filename), std::ios::binary);
33 if (!stream.is_open()) {
34 Log::Error("Failed to open LMT file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
35 return false;
36 }
37 return LMT_Reader::Save(stream, tmap, engine, encoding, opt);
38}
39
40bool LMT_Reader::SaveXml(std::string_view filename, const lcf::rpg::TreeMap& tmap, EngineVersion engine) {
41 std::ofstream stream(ToString(filename), std::ios::binary);
42 if (!stream.is_open()) {
43 Log::Error("Failed to open LMT XML file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
44 return false;
45 }
46 return LMT_Reader::SaveXml(stream, tmap, engine);
47}
48
49std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::LoadXml(std::string_view filename) {
50 std::ifstream stream(ToString(filename), std::ios::binary);
51 if (!stream.is_open()) {
52 Log::Error("Failed to open LMT XML file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
53 return nullptr;
54 }
55 return LMT_Reader::LoadXml(stream);
56}
57
58std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::Load(std::istream& filestream, std::string_view encoding) {
59 LcfReader reader(filestream, ToString(encoding));
60 if (!reader.IsOk()) {
61 LcfReader::SetError("Couldn't parse map tree file.");
62 return nullptr;
63 }
64 std::string header;
65 reader.ReadString(header, reader.ReadInt());
66 if (header.length() != 10) {
67 LcfReader::SetError("This is not a valid RPG2000 map tree.");
68 return nullptr;
69 }
70 if (header != "LcfMapTree") {
71 Log::Warning("Header %s != LcfMapTree and might not be a valid RPG2000 map tree.", header.c_str());
72 }
73 auto tmap = std::make_unique<lcf::rpg::TreeMap>();
74 tmap->lmt_header = std::move(header);
75 TypeReader<rpg::TreeMap>::ReadLcf(*tmap, reader, 0);
76 return tmap;
77}
78
79bool LMT_Reader::Save(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, EngineVersion engine, std::string_view encoding, SaveOpt opt) {
80 LcfWriter writer(filestream, engine, ToString(encoding));
81 if (!writer.IsOk()) {
82 LcfReader::SetError("Couldn't parse map tree file.");
83 return false;
84 }
85 std::string header;
86 if ( tmap.lmt_header.empty() || !bool(opt & SaveOpt::ePreserveHeader)) {
87 header = "LcfMapTree";
88 } else {
89 header= tmap.lmt_header;
90 }
91 writer.WriteInt(header.size());
92 writer.Write(header);
93 TypeReader<rpg::TreeMap>::WriteLcf(tmap, writer);
94 return true;
95}
96
97bool LMT_Reader::SaveXml(std::ostream& filestream, const lcf::rpg::TreeMap& tmap, EngineVersion engine) {
98 XmlWriter writer(filestream, engine);
99 if (!writer.IsOk()) {
100 LcfReader::SetError("Couldn't parse map tree file.");
101 return false;
102 }
103 writer.BeginElement("LMT");
104 TypeReader<rpg::TreeMap>::WriteXml(tmap, writer);
105 writer.EndElement("LMT");
106 return true;
107}
108
109std::unique_ptr<lcf::rpg::TreeMap> LMT_Reader::LoadXml(std::istream& filestream) {
110 XmlReader reader(filestream);
111 if (!reader.IsOk()) {
112 LcfReader::SetError("Couldn't parse map tree file.");
113 return nullptr;
114 }
115 auto tmap = std::make_unique<lcf::rpg::TreeMap>();
116 reader.SetHandler(new RootXmlHandler<rpg::TreeMap>(*tmap, "LMT"));
117 reader.Parse();
118 return tmap;
119}
120
121} //namespace lcf
void Warning(const char *fmt,...) LIKE_PRINTF
void Error(const char *fmt,...) LIKE_PRINTF