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