 |
Gridarta Editor
|
Go to the documentation of this file.
20 package net.sf.gridarta.model.index;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutputStream;
30 import java.io.OutputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Arrays;
33 import java.util.concurrent.Semaphore;
47 import org.apache.log4j.Category;
48 import org.apache.log4j.Logger;
49 import org.jetbrains.annotations.NotNull;
50 import org.jetbrains.annotations.Nullable;
162 public void mapCreated(@NotNull
final MapControl<G, A, R> mapControl,
final boolean interactive) {
163 if (!mapControl.isPickmap()) {
175 if (!mapControl.isPickmap()) {
190 final MapFile mapFile = mapControl.getMapModel().getMapFile();
191 if (mapFile !=
null) {
206 public void mapsDirectoryChanged(@NotNull
final File
mapsDirectory) {
222 public void valueAdded(@NotNull
final MapFile value) {
227 public void valueRemoved(@NotNull
final MapFile value) {
232 public void nameChanged() {
237 public void pendingChanged() {
242 public void indexingFinished() {
253 while (!Thread.currentThread().isInterrupted()) {
259 }
catch (
final InterruptedException ignored) {
260 Thread.currentThread().interrupt();
305 public void stop() throws InterruptedException {
313 if (!mapControl.isPickmap()) {
343 final File tmpMapsDirectory;
354 assert tmpMapsDirectory !=
null;
385 try (OutputStream outputStream =
new FileOutputStream(cacheFile)) {
386 try (ObjectOutputStream objectOutputStream =
new ObjectOutputStream(outputStream)) {
390 if (
LOG.isInfoEnabled()) {
393 }
catch (
final IOException ex) {
394 LOG.warn(cacheFile +
": cannot save cache file: " + ex.getMessage());
406 try (InputStream inputStream =
new FileInputStream(cacheFile)) {
407 try (ObjectInputStream objectInputStream =
new ObjectInputStream(inputStream)) {
411 if (
LOG.isInfoEnabled()) {
414 }
catch (
final FileNotFoundException ex) {
415 if (
LOG.isDebugEnabled()) {
416 LOG.debug(cacheFile +
": cache file does not exist: " + ex.getMessage());
419 }
catch (
final IOException ex) {
420 LOG.warn(cacheFile +
": cannot load cache file: " + ex.getMessage());
431 final File[]
files = dir.getFile().listFiles();
436 for (
final File file :
files) {
438 if (file.isFile() && !file.getName().endsWith(
"~")) {
440 }
else if (file.isDirectory() && !file.getName().equalsIgnoreCase(
".svn") && !file.getName().equalsIgnoreCase(
".dedit")) {
452 if (mapFile ==
null) {
458 final long timestamp = mapFile.
getFile().lastModified();
462 }
catch (
final IOException ex) {
463 if (
LOG.isInfoEnabled()) {
464 LOG.info(mapFile +
": load failed:" + ex.getMessage());
470 if (
LOG.isDebugEnabled()) {
471 LOG.debug(mapFile +
": indexing as '" + mapName +
"'");
486 final byte[] key =
new byte[16];
488 final byte[] data =
mapsDirectory.getAbsoluteFile().toString().getBytes(StandardCharsets.UTF_8);
489 final byte[] hash =
new byte[8];
490 final byte[] in =
new byte[8];
491 final byte[] out =
new byte[8];
493 for (i = 0; i + 8 < data.length; i++) {
494 System.arraycopy(data, i, in, 0, 8);
496 for (
int j = 0; j < 8; j++) {
500 final int len = data.length % 8;
501 System.arraycopy(data, i, in, 0, len);
503 Arrays.fill(in, len + 1, 8, (
byte) 0);
505 for (
int j = 0; j < 8; j++) {
509 final StringBuilder sb =
new StringBuilder(
"index/maps/");
510 for (
int j = 0; j < 8; j++) {
511 sb.append(String.format(
"%02x", hash[j] & 0xff));
515 final File dir = file.getParentFile();
516 if (dir !=
null && !dir.exists() && !dir.mkdirs()) {
517 LOG.warn(
"cannot create directory: " + dir);
528 if (this.state ==
state) {
542 if (
LOG.isDebugEnabled()) {
static final Category LOG
The Logger for printing log messages.
INDEX
Indexer is indexing maps differing from the current index.
MapsIndexer(@NotNull final MapsIndex mapsIndex, @NotNull final MapManager< G, A, R > mapManager, @NotNull final ProjectSettings projectSettings)
Creates a new instance.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
A MapManager manages all opened maps.
static File getHomeFile(@NotNull final String filename)
Return the filename to use when dealing with this application's and current users' home directory.
void encrypt(final byte @NotNull[] plaintext, final byte @NotNull[] ciphertext)
Encrypts a data block.
final Object syncState
The object for synchronizing access to state.
void removeMapManagerListener(@NotNull MapManagerListener< G, A, R > listener)
Removes a MapManagerListener to be notified.
Base package of all Gridarta classes.
Standard Edition Runtime Environment README Import and export control rules on cryptographic software vary from country to country The Java Cryptography Java provides two different sets of cryptographic policy files
void removeIndexListener(@NotNull final IndexListener< V > listener)
Removes an IndexListener to be notified of changes.
final MapsIndex mapsIndex
The MapsIndex being updated.
void add(@NotNull final V value, final long timestamp)
Adds a value to the cache.
Interface for listeners listening to MapManager changes.
void saveMapsIndex()
Saves mapsIndex to its cache file if modified.
void addIndexListener(@NotNull final IndexListener< V > listener)
Adds an IndexListener to be notified of changes.
File newMapsDirectory
The maps directory to index.
void addMapManagerListener(@NotNull MapManagerListener< G, A, R > listener)
Adds a MapManagerListener to be notified.
Reflects a game object (object on a map).
void setName(@NotNull final V value, final long timestamp, @NotNull final String name)
Associates a value with a name.
void setPending(@NotNull final V value)
Marks a value as pending.
void beginUpdate()
Starts an update.
void start()
Starts indexing.
List< MapControl< G, A, R > > getOpenedMaps()
Returns all opened maps.
V removePending()
Returns one pending value.
final Object syncMapsDirectory
The object for synchronizing access to mapsDirectory and {}.
GameObjects are the objects based on Archetypes found on maps.
void stop()
Stops indexing.
final MapManagerListener< G, A, R > mapManagerListener
The MapManagerListener attached to mapManager to detect current map changes.
Implements the XTEA algorithm.
void reportStateChange()
Logs a changed value of state.
final ProjectSettings projectSettings
The ProjectSettings instance.
void save(@NotNull final ObjectOutputStream objectOutputStream)
Saves the state to an ObjectOutputStream.
SCAN
Indexer is scanning the maps directory searching for files differing to the current index.
final Thread thread
The Thread executing runnable.
Interface for MapArchObjects.
Interface for listeners interested in ProjectSettings events.
final Runnable runnable
The Runnable scanning the maps directory and updating the index.
File getFile()
Returns a File for this map file.
File mapsDirectory
The currently indexed maps directory.
final IndexListener< MapFile > indexListener
The IndexListener attached to mapsIndex to detect newly added pending maps.
MapControl< G, A, R > openMapFile(@NotNull MapFile mapFile, boolean interactive)
Loads a map file.
void addProjectSettingsListener(@NotNull ProjectSettingsListener listener)
Adds a ProjectSettingsListener to be notified of changes.
void setState(@NotNull final State state)
Sets a new value to state.
Maintains a MapsIndex for the maps directory.
boolean isModified()
Returns whether the state was modified since last save.
The location of a map file with a map directory.
Settings that apply to a project.
void release(@NotNull MapControl< G, A, R > mapControl)
Releases a MapControl instance.
void updateMapsDirectory()
Checks whether newMapsDirectory has been set and updates {} accordingly.
void waitForIdle()
Blocks the calling thread until all pending maps have been indexed.
void loadMapsIndex()
Loads mapsIndex from its cache file.
final ProjectSettingsListener projectSettingsListener
The ProjectSettingsListener attached to projectSettings for tracking changed maps directories.
void removeProjectSettingsListener(@NotNull ProjectSettingsListener listener)
Removes a ProjectSettingsListener to be notified of changes.
void indexPendingMaps()
Indexes one pending map from mapsIndex.
boolean saveIndices()
Returns whether indices should be saved to disk.
Interface for listeners interested in Index related events.
final Semaphore mapsIndexSemaphore
A pending map may exist in mapsIndex if a permit is available.
void clear()
Clears all values from the index.
Currently nothing more than a marker interface for unification.
Interface for listeners listening on changes in MapControl instances.
static File getCacheFile(@NotNull final File mapsDirectory)
Returns the cache file for a given maps directory.
IDLE
Indexer is idle; the current index is up-to-date.
Indexes maps by map name.
MapModel< G, A, R > getMapModel()
Returns the map model.
State state
The indexer state.
void scanMapsDirectoryInt(@NotNull final MapFile dir, @NotNull final String mapPath)
Scans a directory for files.
final MapManager< G, A, R > mapManager
The MapManager for loading map files.
void indexingFinished()
Should be called after indexing has finished.
File getMapsDirectory()
Returns the default maps directory.
int size()
Returns the number of values in this cache.
boolean hasPending()
Returns whether at least one pending value exists.
void load(@NotNull final ObjectInputStream objectInputStream)
@noinspection unchecked
final MapControlListener< G, A, R > mapControlListener
The MapControlListener attached to all opened maps.
void endUpdate()
Ends an update.
INIT
Indexer has been created but is not yet running.
Loader for loading resources the user's home directory.