PageRenderTime 37ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/Snapshot.java

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