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-2015 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.ByteBuffer;
32 import java.nio.channels.FileChannel;
33 import java.nio.charset.Charset;
38 import net.sf.japi.swing.action.ActionBuilder;
39 import net.sf.japi.swing.action.ActionBuilderFactory;
40 import net.sf.japi.swing.misc.Progress;
41 import org.jetbrains.annotations.NotNull;
42 
50 public class FaceObjectsCollectable implements Collectable {
51 
55  @NotNull
56  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
57 
61  @NotNull
62  private final FaceObjects faceObjects;
63 
67  @NotNull
69 
75  public FaceObjectsCollectable(@NotNull final FaceObjects faceObjects, @NotNull final ArchFaceProvider archFaceProvider) {
76  this.faceObjects = faceObjects;
77  this.archFaceProvider = archFaceProvider;
78  }
79 
90  @Override
91  public void collect(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
92  collectTreeFile(progress, collectedDirectory);
93  collectBmapsFile(progress, collectedDirectory);
94  collectImageFile(progress, collectedDirectory);
95  }
96 
103  private void collectImageFile(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
104  final File file = new File(collectedDirectory, ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.image.name"));
105  try (FileOutputStream fos = new FileOutputStream(file)) {
106  try (FileChannel outChannel = fos.getChannel()) {
107  final int numOfFaceObjects = faceObjects.size();
108  progress.setLabel(ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectImages"), numOfFaceObjects);
109  final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
110  final Charset charset = Charset.forName("ISO-8859-1");
111  int i = 0;
112  for (final FaceObject faceObject : faceObjects) {
113  final String face = faceObject.getFaceName();
114  final String path = archFaceProvider.getFilename(face);
115  try {
116  try (FileInputStream fin = new FileInputStream(path)) {
117  final FileChannel inChannel = fin.getChannel();
118  final long imageSize = inChannel.size();
119  byteBuffer.clear();
120  byteBuffer.put(("IMAGE " + (faceObjects.isIncludeFaceNumbers() ? i + " " : "") + imageSize + " " + face + "\n").getBytes(charset));
121  byteBuffer.flip();
122  outChannel.write(byteBuffer);
123  inChannel.transferTo(0L, imageSize, outChannel);
124  }
125  } catch (final FileNotFoundException ignored) {
126  ACTION_BUILDER.showMessageDialog(progress.getParentComponent(), "archCollectErrorFileNotFound", path);
127  return;
128  } catch (final IOException e) {
129  ACTION_BUILDER.showMessageDialog(progress.getParentComponent(), "archCollectErrorIOException", path, e);
130  return;
131  }
132 
133  if (i++ % 100 == 0) {
134  progress.setValue(i);
135  }
136  }
137  progress.setValue(faceObjects.size()); // finished
138  }
139  }
140  }
141 
148  private void collectTreeFile(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
149  collectFile(progress, new File(collectedDirectory, ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.facetree.name")), ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectTree"), ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.facetree.output"));
150  }
151 
158  private void collectBmapsFile(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
159  collectFile(progress, new File(collectedDirectory, ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.face.name")), ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectBmaps"), ActionBuilderUtils.getString(ACTION_BUILDER, "configSource.face.output"));
160  }
161 
170  private void collectFile(@NotNull final Progress progress, @NotNull final File file, @NotNull final String label, @NotNull final String format) throws IOException {
171  try (OutputStream fos = new FileOutputStream(file)) {
172  try (Writer osw = new OutputStreamWriter(fos)) {
173  try (Writer bw = new BufferedWriter(osw)) {
174  final int numOfFaceObjects = faceObjects.size();
175  progress.setLabel(label, numOfFaceObjects);
176  int i = 0;
177  for (final FaceObject faceObject : faceObjects) {
178  final String path = faceObject.getPath();
179  final String face = faceObject.getFaceName();
180  bw.append(String.format(format, i, path, face)).append('\n');
181  if (i++ % 100 == 0) {
182  progress.setValue(i);
183  }
184  }
185  progress.setValue(numOfFaceObjects);
186  }
187  }
188  }
189  }
190 
191 }
void collectImageFile(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Creates the image file.
Common interface for FaceObject.
Definition: FaceObject.java:30
int size()
Get the number of objects.
String getFilename(@NotNull final String faceName)
Get the filename for a face.
A Collectable has information that can be collected.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
final FaceObjects faceObjects
The FaceObjects being collected.
FaceObjectsCollectable(@NotNull final FaceObjects faceObjects, @NotNull final ArchFaceProvider archFaceProvider)
Creates a new instance.
void collectBmapsFile(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Creates the bmaps file.
FaceObjects is a container for FaceObjects.
Utility class for ActionBuilder related functions.
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 ...
The face is the appearance of an object.
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.
Implementation of FaceProvider which reads images from the arch directory.
A Collectable that creates the atrinik.0/crossfire.0/daimonin.0 files which contains all defined face...
static final ActionBuilder ACTION_BUILDER
The ActionBuilder instance.
final ArchFaceProvider archFaceProvider
The ArchFaceProvider to use for collection.
void collectTreeFile(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Creates the tree file.