/src/main/java/com/sk89q/worldedit/snapshots/Snapshot.java

https://github.com/GuntherDW/worldedit-1 · Java · 212 lines · 108 code · 24 blank · 80 comment · 24 complexity · 544f178e923abbf91af556763d136e9a MD5 · raw file

  1. // $Id$
  2. /*
  3. * WorldEdit
  4. * Copyright (C) 2010 sk89q <http://www.sk89q.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldedit.snapshots;
  20. import com.sk89q.worldedit.data.*;
  21. import java.io.*;
  22. import java.util.Calendar;
  23. import java.util.logging.Logger;
  24. import java.util.zip.ZipFile;
  25. /**
  26. *
  27. * @author sk89q
  28. */
  29. public class Snapshot implements Comparable<Snapshot> {
  30. protected static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
  31. /**
  32. * Stores snapshot file.
  33. */
  34. protected File file;
  35. /**
  36. * Name of the snapshot;
  37. */
  38. protected String name;
  39. /**
  40. * Stores the date associated with the snapshot.
  41. */
  42. protected Calendar date;
  43. /**
  44. * Construct a snapshot restoration operation.
  45. *
  46. * @param repo
  47. * @param snapshot
  48. */
  49. public Snapshot(SnapshotRepository repo, String snapshot) {
  50. file = new File(repo.getDirectory(), snapshot);
  51. name = snapshot;
  52. }
  53. /**
  54. * Get a chunk store.
  55. *
  56. * @return
  57. * @throws IOException
  58. * @throws DataException
  59. */
  60. public ChunkStore getChunkStore() throws IOException, DataException {
  61. ChunkStore chunkStore = _getChunkStore();
  62. logger.info("WorldEdit: Using " + chunkStore.getClass().getCanonicalName()
  63. + " for loading snapshot '" + file.getAbsolutePath() + "'");
  64. return chunkStore;
  65. }
  66. /**
  67. * Get a chunk store.
  68. *
  69. * @return
  70. * @throws IOException
  71. * @throws DataException
  72. */
  73. public ChunkStore _getChunkStore() throws IOException, DataException {
  74. if (file.getName().toLowerCase().endsWith(".zip")) {
  75. try {
  76. ChunkStore chunkStore = new TrueZipMcRegionChunkStore(file);
  77. if (!chunkStore.isValid()) {
  78. return new TrueZipLegacyChunkStore(file);
  79. }
  80. return chunkStore;
  81. } catch (NoClassDefFoundError e) {
  82. ChunkStore chunkStore = new ZippedMcRegionChunkStore(file);
  83. if (!chunkStore.isValid()) {
  84. return new ZippedLegacyChunkStore(file);
  85. }
  86. return chunkStore;
  87. }
  88. } else if (file.getName().toLowerCase().endsWith(".tar.bz2")
  89. || file.getName().toLowerCase().endsWith(".tar.gz")
  90. || file.getName().toLowerCase().endsWith(".tar")) {
  91. try {
  92. ChunkStore chunkStore = new TrueZipMcRegionChunkStore(file);
  93. if (!chunkStore.isValid()) {
  94. return new TrueZipLegacyChunkStore(file);
  95. }
  96. return chunkStore;
  97. } catch (NoClassDefFoundError e) {
  98. throw new DataException("TrueZIP is required for .tar support");
  99. }
  100. } else {
  101. ChunkStore chunkStore = new FileMcRegionChunkStore(file);
  102. if (!chunkStore.isValid()) {
  103. return new FileLegacyChunkStore(file);
  104. }
  105. return chunkStore;
  106. }
  107. }
  108. /**
  109. * Check the zip/tar file it contains the given world.
  110. *
  111. * @return true if the zip/tar file contains the given world
  112. */
  113. public boolean containsWorld(String worldname) {
  114. try {
  115. if (file.getName().toLowerCase().endsWith(".zip")) {
  116. ZipFile entry = new ZipFile(file);
  117. return entry.getEntry(worldname) != null;
  118. } else if (file.getName().toLowerCase().endsWith(".tar.bz2")
  119. || file.getName().toLowerCase().endsWith(".tar.gz")
  120. || file.getName().toLowerCase().endsWith(".tar")) {
  121. try {
  122. de.schlichtherle.util.zip.ZipFile entry = new de.schlichtherle.util.zip.ZipFile(file);
  123. return entry.getEntry(worldname) != null;
  124. } catch (NoClassDefFoundError e) {
  125. throw new DataException("TrueZIP is required for .tar support");
  126. }
  127. } else {
  128. return (file.getName().equalsIgnoreCase(worldname));
  129. }
  130. } catch (IOException ex) {
  131. // Skip the file, but print an error
  132. logger.info("Could not load snapshot: "
  133. + file.getPath());
  134. } catch (DataException ex) {
  135. // No truezip, so tar file not supported.
  136. // Dont print, just skip the file.
  137. }
  138. return false;
  139. }
  140. /**
  141. * Get the snapshot's name.
  142. *
  143. * @return
  144. */
  145. public String getName() {
  146. return name;
  147. }
  148. /**
  149. * Get the file for the snapshot.
  150. *
  151. * @return
  152. */
  153. public File getFile() {
  154. return file;
  155. }
  156. /**
  157. * Get the date associated with this snapshot.
  158. *
  159. * @return
  160. */
  161. public Calendar getDate() {
  162. return date;
  163. }
  164. /**
  165. * Set the date of the snapshot.
  166. *
  167. * @param date
  168. */
  169. public void setDate(Calendar date) {
  170. this.date = date;
  171. }
  172. public int compareTo(Snapshot o) {
  173. if (o.date == null || date == null) {
  174. // Remove the folder from the name
  175. int i = name.indexOf("/"), j = o.name.indexOf("/");
  176. return name.substring((i > 0 ? 0 : i)).compareTo(o.name.substring((j > 0 ? 0 : j)));
  177. } else {
  178. return date.compareTo(o.date);
  179. }
  180. }
  181. @Override
  182. public boolean equals(Object o) {
  183. if (o instanceof Snapshot) {
  184. return file.equals(((Snapshot) o).file);
  185. }
  186. return false;
  187. }
  188. }