Gridarta Editor
FaceObjectsCollectable.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.collectable;
21 
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.io.OutputStreamWriter;
30 import java.io.Writer;
31 import java.nio.Buffer;
32 import java.nio.ByteBuffer;
33 import java.nio.channels.FileChannel;
34 import java.nio.charset.Charset;
35 import java.nio.charset.StandardCharsets;
40 import net.sf.japi.swing.action.ActionBuilder;
41 import net.sf.japi.swing.action.ActionBuilderFactory;
42 import net.sf.japi.swing.misc.Progress;
43 import org.jetbrains.annotations.NotNull;
44 
52 public class FaceObjectsCollectable implements Collectable {
53 
57  @NotNull
58  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
59 
63  @NotNull
64  private final FaceObjects faceObjects;
65 
69  @NotNull
71 
78  this.faceObjects = faceObjects;
79  this.archFaceProvider = archFaceProvider;
80  }
81 
92  @Override
93  public void collect(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
94  collectTreeFile(progress, collectedDirectory);
95  collectBmapsFile(progress, collectedDirectory);
96  collectImageFile(progress, collectedDirectory);
97  }
98 
105  private void collectImageFile(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
106  final File file = new File(collectedDirectory, ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.image.name"));
107  try (FileOutputStream fos = new FileOutputStream(file)) {
108  try (FileChannel outChannel = fos.getChannel()) {
109  final int numOfFaceObjects = faceObjects.size();
110  progress.setLabel(ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectImages"), numOfFaceObjects);
111  final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
112  final Charset charset = StandardCharsets.ISO_8859_1;
113  int i = 0;
114  for (final FaceObject faceObject : faceObjects) {
115  final String face = faceObject.getFaceName();
116  final String path = archFaceProvider.getFilename(face);
117  try {
118  try (FileInputStream fin = new FileInputStream(path)) {
119  final FileChannel inChannel = fin.getChannel();
120  final long imageSize = inChannel.size();
121  ((Buffer) byteBuffer).clear();
122  byteBuffer.put(("IMAGE " + (faceObjects.isIncludeFaceNumbers() ? i + " " : "") + imageSize + " " + face + "\n").getBytes(charset));
123  ((Buffer) byteBuffer).flip();
124  outChannel.write(byteBuffer);
125  inChannel.transferTo(0L, imageSize, outChannel);
126  }
127  } catch (final FileNotFoundException ignored) {
128  ACTION_BUILDER.showMessageDialog(progress.getParentComponent(), "archCollectErrorFileNotFound", path);
129  return;
130  } catch (final IOException e) {
131  ACTION_BUILDER.showMessageDialog(progress.getParentComponent(), "archCollectErrorIOException", path, e);
132  return;
133  }
134 
135  if (i++ % 100 == 0) {
136  progress.setValue(i);
137  }
138  }
139  progress.setValue(faceObjects.size()); // finished
140  }
141  }
142  }
143 
150  private void collectTreeFile(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
151  collectFile(progress, new File(collectedDirectory, ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.facetree.name")), ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectTree"), ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.facetree.output"));
152  }
153 
160  private void collectBmapsFile(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
161  collectFile(progress, new File(collectedDirectory, ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.face.name")), ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectBmaps"), ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.face.output"));
162  }
163 
172  private void collectFile(@NotNull final Progress progress, @NotNull final File file, @NotNull final String label, @NotNull final String format) throws IOException {
173  try (OutputStream fos = new FileOutputStream(file)) {
174  try (Writer osw = new OutputStreamWriter(fos)) {
175  try (Writer bw = new BufferedWriter(osw)) {
176  final int numOfFaceObjects = faceObjects.size();
177  progress.setLabel(label, numOfFaceObjects);
178  int i = 0;
179  for (final FaceObject faceObject : faceObjects) {
180  final String path = faceObject.getPath();
181  final String face = faceObject.getFaceName();
182  bw.append(String.format(format, i, path, face)).append('\n');
183  if (i++ % 100 == 0) {
184  progress.setValue(i);
185  }
186  }
187  progress.setValue(numOfFaceObjects);
188  }
189  }
190  }
191  }
192 
193 }
net.sf.gridarta.model.face.ArchFaceProvider.getFilename
String getFilename(@NotNull final String faceName)
Get the filename for a face.
Definition: ArchFaceProvider.java:70
net.sf.gridarta.model.collectable.FaceObjectsCollectable
A Collectable that creates the atrinik.0/crossfire.0/daimonin.0 files which contains all defined face...
Definition: FaceObjectsCollectable.java:52
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.collectable.Collectable
A Collectable has information that can be collected.
Definition: Collectable.java:33
net.sf.gridarta.model.face.ArchFaceProvider
Implementation of FaceProvider which reads images from the arch directory.
Definition: ArchFaceProvider.java:38
net.sf
net.sf.gridarta.model.data.NamedObjects.size
int size()
Get the number of objects.
net.sf.gridarta.model.collectable.FaceObjectsCollectable.collect
void collect(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Collects information.the progress to report progress to the destination directory to collect data to ...
Definition: FaceObjectsCollectable.java:93
net
net.sf.gridarta.model.face.FaceObjects
FaceObjects is a container for FaceObjects.
Definition: FaceObjects.java:31
net.sf.gridarta.model.face.FaceObjects.isIncludeFaceNumbers
boolean isIncludeFaceNumbers()
Returns whether the images file contains face numbers.
net.sf.gridarta.model.collectable.FaceObjectsCollectable.collectFile
void collectFile(@NotNull final Progress progress, @NotNull final File file, @NotNull final String label, @NotNull final String format)
Creates an output file containing all faces.
Definition: FaceObjectsCollectable.java:172
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.model.collectable.FaceObjectsCollectable.collectBmapsFile
void collectBmapsFile(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Creates the bmaps file.
Definition: FaceObjectsCollectable.java:160
net.sf.gridarta.model.face.FaceObject
Common interface for FaceObject.
Definition: FaceObject.java:30
net.sf.gridarta.model
net.sf.gridarta.model.collectable.FaceObjectsCollectable.collectTreeFile
void collectTreeFile(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Creates the tree file.
Definition: FaceObjectsCollectable.java:150
net.sf.gridarta.model.collectable.FaceObjectsCollectable.faceObjects
final FaceObjects faceObjects
The FaceObjects being collected.
Definition: FaceObjectsCollectable.java:64
net.sf.gridarta.model.collectable.FaceObjectsCollectable.FaceObjectsCollectable
FaceObjectsCollectable(@NotNull final FaceObjects faceObjects, @NotNull final ArchFaceProvider archFaceProvider)
Creates a new instance.
Definition: FaceObjectsCollectable.java:77
net.sf.gridarta.model.face
The face is the appearance of an object.
Definition: AbstractFaceObjects.java:20
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.collectable.FaceObjectsCollectable.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
The ActionBuilder instance.
Definition: FaceObjectsCollectable.java:58
net.sf.gridarta.model.collectable.FaceObjectsCollectable.archFaceProvider
final ArchFaceProvider archFaceProvider
The ArchFaceProvider to use for collection.
Definition: FaceObjectsCollectable.java:70
net.sf.gridarta.model.collectable.FaceObjectsCollectable.collectImageFile
void collectImageFile(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Creates the image file.
Definition: FaceObjectsCollectable.java:105