20 package net.sf.gridarta.model.io;
22 import java.awt.Point;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
29 import java.util.Map.Entry;
37 import org.apache.log4j.Category;
38 import org.apache.log4j.Logger;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
81 public G
load(@NotNull
final BufferedReader reader, @Nullable
final Collection<G> objects)
throws IOException {
82 final String line = reader.readLine();
83 return line ==
null ? null :
load(reader, line, objects);
92 public G
load(@NotNull
final BufferedReader reader, @NotNull
final String firstLine, @Nullable
final Collection<G> objects)
throws IOException {
94 if (archName ==
null) {
102 final String thisLine2 = reader.readLine();
103 if (thisLine2 ==
null) {
104 throw new IOException(
"unexpected end of file while reading 'arch " + archName +
"'");
106 final String thisLine = thisLine2.trim();
108 if (thisLine.startsWith(
"arch ")) {
109 final G invItem =
load(reader, thisLine, objects);
110 assert invItem != null :
"invItem must not be null here.";
111 gameObject.addLast(invItem);
112 }
else if (thisLine.equals(
"end")) {
113 if (objects !=
null) {
114 objects.add(gameObject);
117 }
else if (thisLine.equals(
"msg")) {
118 gameObject.setMsgText(
"");
120 final String thisLine3 = reader.readLine();
121 if (thisLine3 ==
null) {
122 throw new IOException(
"unexpected end of file while reading 'arch " + archName +
"'");
125 if (thisLine3.trim().equals(
"endmsg")) {
129 gameObject.addMsgTextLine(thisLine3);
131 }
else if (
parseLine(thisLine, gameObject)) {
133 }
else if (thisLine.startsWith(
"x ")) {
134 gameObject.setMapX(Integer.parseInt(thisLine.substring(2)));
135 }
else if (thisLine.startsWith(
"y ")) {
136 gameObject.setMapY(Integer.parseInt(thisLine.substring(2)));
138 gameObject.addObjectText(thisLine);
151 private boolean parseLine(@NotNull
final String line, @NotNull
final G gameObject) {
165 private static String
readArchName(@NotNull
final BufferedReader reader, @NotNull
final String firstLine)
throws IOException {
166 for (String thisLine2 = firstLine; thisLine2 !=
null; thisLine2 = reader.readLine()) {
167 final String thisLine = thisLine2.trim();
168 if (thisLine.startsWith(
"arch ")) {
169 return thisLine.substring(5).trim();
171 if (!thisLine.isEmpty() && !thisLine.startsWith(
"#")) {
172 throw new IOException(
"unexpected first line of game object: '" + thisLine +
"', expected 'arch ...'");
180 final Collection<G> tailList =
new ArrayList<>();
182 for (
final G gameObject : objects) {
183 if (!gameObject.isInContainer()) {
188 objects.addAll(tailList);
192 public void save(@NotNull
final Appendable appendable, @NotNull
final G gameObject)
throws IOException {
195 appendable.append(
"arch ");
196 appendable.append(gameObject.getArchetype().getArchetypeName());
197 appendable.append(
"\n");
198 for (
final Entry<String, String> entry : fields.entrySet()) {
199 final String key = entry.getKey();
200 appendable.append(key);
201 if (key.equals(
"msg")) {
202 appendable.append(
"\n");
203 appendable.append(entry.getValue());
204 appendable.append(
"endmsg\n");
206 appendable.append(
" ");
207 appendable.append(entry.getValue());
208 appendable.append(
"\n");
212 for (
final G inventoryItem : gameObject) {
213 save(appendable, inventoryItem);
216 appendable.append(
"end\n");
220 public void addModifiedFields(@NotNull
final G gameObject, @NotNull
final Map<String, String> fields) {
223 final String msgText = gameObject.getMsgText(
false);
224 final String archMsgText = archetype.
getMsgText(
false);
225 if (msgText !=
null && !msgText.equals(archMsgText ==
null ?
"" : archMsgText)) {
226 fields.put(
"msg", msgText);
229 final CharSequence objText = gameObject.getObjectText();
230 if (objText.length() != 0) {
233 if (line.length == 2) {
234 fields.put(line[0], line[1]);
236 LOG.warn(
"writeMapArch: ignoring invalid arch line: " + aTmp);
242 if (gameObject.getMapX() != 0) {
243 fields.put(
"x", Integer.toString(gameObject.getMapX()));
245 if (gameObject.getMapY() != 0) {
246 fields.put(
"y", Integer.toString(gameObject.getMapY()));
261 private void expandMulti(@NotNull
final G gameObject, @NotNull
final Collection<G> objects) {
264 if (!archetype.
isMulti() || gameObject.getMultiRefCount() > 1) {
268 if (gameObject.isInContainer()) {
269 LOG.warn(
"Multi-part expansion for a GameObject inside a container requested.");
274 final Point pos = gameObject.getMapLocation();
275 for (R oldPart = archetype.
getMultiNext(); oldPart !=
null; oldPart = oldPart.getMultiNext()) {
277 tailGameObject.setMapX(pos.x + oldPart.getMultiX());
278 tailGameObject.setMapY(pos.y + oldPart.getMultiY());
279 objects.add(tailGameObject);