liblcf
Loading...
Searching...
No Matches
lmu_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#include <memory>
14
15#include "lcf/lmu/reader.h"
16#include "lcf/lmu/chunks.h"
17#include "lcf/reader_lcf.h"
18#include "lcf/reader_util.h"
19#include "log.h"
20#include "reader_struct.h"
21
22namespace lcf {
23
24void LMU_Reader::PrepareSave(rpg::Map& map) {
25 ++map.save_count;
26}
27
28std::unique_ptr<rpg::Map> LMU_Reader::Load(std::string_view filename, std::string_view encoding) {
29 std::ifstream stream(ToString(filename), std::ios::binary);
30 if (!stream.is_open()) {
31 Log::Error("Failed to open LMU file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
32 return nullptr;
33 }
34 return LMU_Reader::Load(stream, encoding);
35}
36
37bool LMU_Reader::Save(std::string_view filename, const rpg::Map& save, EngineVersion engine, std::string_view encoding, SaveOpt opt) {
38 std::ofstream stream(ToString(filename), std::ios::binary);
39 if (!stream.is_open()) {
40 Log::Error("Failed to open LMU file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
41 return false;
42 }
43 return LMU_Reader::Save(stream, save, engine, encoding, opt);
44}
45
46bool LMU_Reader::SaveXml(std::string_view filename, const rpg::Map& save, EngineVersion engine) {
47 std::ofstream stream(ToString(filename), std::ios::binary);
48 if (!stream.is_open()) {
49 Log::Error("Failed to open LMU XML file '%s' for writing: %s", ToString(filename).c_str(), strerror(errno));
50 return false;
51 }
52 return LMU_Reader::SaveXml(stream, save, engine);
53}
54
55std::unique_ptr<rpg::Map> LMU_Reader::LoadXml(std::string_view filename) {
56 std::ifstream stream(ToString(filename), std::ios::binary);
57 if (!stream.is_open()) {
58 Log::Error("Failed to open LMU XML file '%s' for reading: %s", ToString(filename).c_str(), strerror(errno));
59 return nullptr;
60 }
61 return LMU_Reader::LoadXml(stream);
62}
63
64std::unique_ptr<rpg::Map> LMU_Reader::Load(std::istream& filestream, std::string_view encoding) {
65 LcfReader reader(filestream, ToString(encoding));
66 if (!reader.IsOk()) {
67 LcfReader::SetError("Couldn't parse map file.");
68 return {};
69 }
70 std::string header;
71 reader.ReadString(header, reader.ReadInt());
72 if (header.length() != 10) {
73 LcfReader::SetError("This is not a valid RPG2000 map.");
74 return {};
75 }
76 if (header != "LcfMapUnit") {
77 Log::Warning("Header %s != LcfMapUnit and might not be a valid RPG2000 map.", header.c_str());
78 }
79
80 auto map = std::make_unique<rpg::Map>();
81 map->lmu_header = std::move(header);
82 Struct<rpg::Map>::ReadLcf(*map, reader);
83 return map;
84}
85
86bool LMU_Reader::Save(std::ostream& filestream, const rpg::Map& map, EngineVersion engine, std::string_view encoding, SaveOpt opt) {
87 LcfWriter writer(filestream, engine, ToString(encoding));
88 if (!writer.IsOk()) {
89 LcfReader::SetError("Couldn't parse map file.");
90 return false;
91 }
92 std::string header;
93 if ( map.lmu_header.empty() || !bool(opt & SaveOpt::ePreserveHeader)) {
94 header = "LcfMapUnit";
95 } else {
96 header= map.lmu_header;
97 }
98 writer.WriteInt(header.size());
99 writer.Write(header);
100
101 Struct<rpg::Map>::WriteLcf(map, writer);
102 return true;
103}
104
105bool LMU_Reader::SaveXml(std::ostream& filestream, const rpg::Map& map, EngineVersion engine) {
106 XmlWriter writer(filestream, engine);
107 if (!writer.IsOk()) {
108 LcfReader::SetError("Couldn't parse map file.");
109 return false;
110 }
111 writer.BeginElement("LMU");
112 Struct<rpg::Map>::WriteXml(map, writer);
113 writer.EndElement("LMU");
114 return true;
115}
116
117std::unique_ptr<rpg::Map> LMU_Reader::LoadXml(std::istream& filestream) {
118 XmlReader reader(filestream);
119 if (!reader.IsOk()) {
120 LcfReader::SetError("Couldn't parse map file.");
121 return {};
122 }
123
124 auto map = std::make_unique<rpg::Map>();
125 reader.SetHandler(new RootXmlHandler<rpg::Map>(*map, "LMU"));
126 reader.Parse();
127 return map;
128}
129
130} //namespace lcf
static void WriteXml(const S &obj, XmlWriter &stream)
static void WriteLcf(const S &obj, LcfWriter &stream)
static void ReadLcf(S &obj, LcfReader &stream)
void Warning(const char *fmt,...) LIKE_PRINTF
void Error(const char *fmt,...) LIKE_PRINTF