More file moving.

This commit is contained in:
sk89q
2011-05-01 01:30:33 -07:00
parent 4bcbfa76ef
commit 582b98dad0
206 changed files with 133 additions and 2 deletions

View File

@ -0,0 +1,29 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.snapshots;
/**
*
* @author sk89q
*/
public class InvalidSnapshotException extends Exception {
private static final long serialVersionUID = 7307139106494852893L;
}

View File

@ -0,0 +1,34 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.snapshots;
import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ModificationTimerParser implements SnapshotDateParser {
public Calendar detectDate(File file) {
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(file.lastModified());
return cal;
}
}

View File

@ -0,0 +1,176 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.snapshots;
import com.sk89q.worldedit.data.*;
import java.io.*;
import java.util.Calendar;
import java.util.logging.Logger;
/**
*
* @author sk89q
*/
public class Snapshot implements Comparable<Snapshot> {
protected static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/**
* Stores snapshot file.
*/
protected File file;
/**
* Name of the snapshot;
*/
protected String name;
/**
* Stores the date associated with the snapshot.
*/
protected Calendar date;
/**
* Construct a snapshot restoration operation.
*
* @param repo
* @param snapshot
*/
public Snapshot(SnapshotRepository repo, String snapshot) {
file = new File(repo.getDirectory(), snapshot);
name = snapshot;
}
/**
* Get a chunk store.
*
* @return
* @throws IOException
* @throws DataException
*/
public ChunkStore getChunkStore() throws IOException, DataException {
ChunkStore chunkStore = _getChunkStore();
logger.info("WorldEdit: Using " + chunkStore.getClass().getCanonicalName()
+ " for loading snapshot '" + file.getAbsolutePath() + "'");
return chunkStore;
}
/**
* Get a chunk store.
*
* @return
* @throws IOException
* @throws DataException
*/
public ChunkStore _getChunkStore() throws IOException, DataException {
if (file.getName().toLowerCase().endsWith(".zip")) {
try {
ChunkStore chunkStore = new TrueZipMcRegionChunkStore(file);
if (!chunkStore.isValid()) {
return new TrueZipLegacyChunkStore(file);
}
return chunkStore;
} catch (NoClassDefFoundError e) {
ChunkStore chunkStore = new ZippedMcRegionChunkStore(file);
if (!chunkStore.isValid()) {
return new ZippedLegacyChunkStore(file);
}
return chunkStore;
}
} else if (file.getName().toLowerCase().endsWith(".tar.bz2")
|| file.getName().toLowerCase().endsWith(".tar.gz")
|| file.getName().toLowerCase().endsWith(".tar")) {
try {
ChunkStore chunkStore = new TrueZipMcRegionChunkStore(file);
if (!chunkStore.isValid()) {
return new TrueZipLegacyChunkStore(file);
}
return chunkStore;
} catch (NoClassDefFoundError e) {
throw new DataException("TrueZIP is required for .tar support");
}
} else {
ChunkStore chunkStore = new FileMcRegionChunkStore(file);
if (!chunkStore.isValid()) {
return new FileLegacyChunkStore(file);
}
return chunkStore;
}
}
/**
* Get the snapshot's name.
*
* @return
*/
public String getName() {
return name;
}
/**
* Get the file for the snapshot.
*
* @return
*/
public File getFile() {
return file;
}
/**
* Get the date associated with this snapshot.
*
* @return
*/
public Calendar getDate() {
return date;
}
/**
* Set the date of the snapshot.
*
* @param date
*/
public void setDate(Calendar date) {
this.date = date;
}
public int compareTo(Snapshot o) {
if (o.date == null || date == null) {
return name.compareTo(o.name);
} else {
return date.compareTo(o.date);
}
}
@Override
public boolean equals(Object o) {
if (o instanceof Snapshot) {
return file.equals(((Snapshot) o).file);
}
return false;
}
}

View File

@ -0,0 +1,38 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.snapshots;
import java.io.File;
import java.util.Calendar;
/**
* A name parser attempts to make sense of a filename for a snapshot.
*
* @author sk89q
*/
public interface SnapshotDateParser {
/**
* Attempt to detect a date from a file.
*
* @param file
* @return date or null
*/
public Calendar detectDate(File file);
}

View File

@ -0,0 +1,226 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.snapshots;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
/**
*
* @author sk89q
*/
public class SnapshotRepository {
/**
* Stores the directory the snapshots come from.
*/
protected File dir;
/**
* List of date parsers.
*/
protected List<SnapshotDateParser> dateParsers
= new ArrayList<SnapshotDateParser>();
/**
* Create a new instance of a repository.
*
* @param dir
*/
public SnapshotRepository(File dir) {
this.dir = dir;
dateParsers.add(new YYMMDDHHIISSParser());
dateParsers.add(new ModificationTimerParser());
}
/**
* Create a new instance of a repository.
*
* @param dir
*/
public SnapshotRepository(String dir) {
this(new File(dir));
}
/**
* Get a list of snapshots in a directory. The newest snapshot is
* near the top of the array.
*
* @param newestFirst
* @return
*/
public List<Snapshot> getSnapshots(boolean newestFirst) {
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return isValidSnapshot(f);
}
};
String[] snapshotNames = dir.list(filter);
List<Snapshot> list = new ArrayList<Snapshot>(snapshotNames.length);
for (String name : snapshotNames) {
Snapshot snapshot = new Snapshot(this, name);
detectDate(snapshot);
list.add(snapshot);
}
if (newestFirst) {
Collections.sort(list, Collections.reverseOrder());
} else {
Collections.sort(list);
}
return list;
}
/**
* Get the first snapshot after a date.
*
* @param date
* @return
*/
public Snapshot getSnapshotAfter(Calendar date) {
List<Snapshot> snapshots = getSnapshots(true);
Snapshot last = null;
for (Snapshot snapshot : snapshots) {
if (snapshot.getDate() != null
&& snapshot.getDate().before(date)) {
return last;
}
last = snapshot;
}
return last;
}
/**
* Get the first snapshot before a date.
*
* @param date
* @return
*/
public Snapshot getSnapshotBefore(Calendar date) {
List<Snapshot> snapshots = getSnapshots(false);
Snapshot last = null;
for (Snapshot snapshot : snapshots) {
if (snapshot.getDate().after(date)) {
return last;
}
last = snapshot;
}
return last;
}
/**
* Attempt to detect a snapshot's date and assign it.
*
* @param snapshot
*/
protected void detectDate(Snapshot snapshot) {
for (SnapshotDateParser parser : dateParsers) {
Calendar date = parser.detectDate(snapshot.getFile());
if (date != null) {
snapshot.setDate(date);
return;
}
}
snapshot.setDate(null);
}
/**
* Get the default snapshot.
*
* @return
*/
public Snapshot getDefaultSnapshot() {
List<Snapshot> snapshots = getSnapshots(true);
if (snapshots.size() == 0) {
return null;
}
return snapshots.get(0);
}
/**
* Check to see if a snapshot is valid.
*
* @param snapshot
* @return whether it is a valid snapshot
*/
public boolean isValidSnapshotName(String snapshot) {
return isValidSnapshot(new File(dir, snapshot));
}
/**
* Check to see if a snapshot is valid.
*
* @param f
* @return whether it is a valid snapshot
*/
public boolean isValidSnapshot(File f) {
if (!f.getName().matches("^[A-Za-z0-9_\\- \\./\\\\'\\$@~!%\\^\\*\\(\\)\\[\\]\\+\\{\\},\\?]+$")) {
return false;
}
return (f.isDirectory() && (new File(f, "level.dat")).exists())
|| (f.isFile() && (
f.getName().toLowerCase().endsWith(".zip")
|| f.getName().toLowerCase().endsWith(".tar.bz2")
|| f.getName().toLowerCase().endsWith(".tar.gz")
|| f.getName().toLowerCase().endsWith(".tar")
));
}
/**
* Get a snapshot.
*
* @param name
* @return
* @throws InvalidSnapshotException
*/
public Snapshot getSnapshot(String name) throws InvalidSnapshotException {
if (!isValidSnapshotName(name)) {
throw new InvalidSnapshotException();
}
return new Snapshot(this, name);
}
/**
* Get the snapshot directory.
*
* @return
*/
public File getDirectory() {
return dir;
}
}

View File

@ -0,0 +1,206 @@
package com.sk89q.worldedit.snapshots;
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.regions.*;
import com.sk89q.worldedit.blocks.*;
import com.sk89q.worldedit.data.*;
import java.io.IOException;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ArrayList;
/**
*
* @author sk89q
*/
public class SnapshotRestore {
/**
* Store a list of chunks that are needed and the points in them.
*/
private Map<BlockVector2D,ArrayList<Vector>> neededChunks =
new LinkedHashMap<BlockVector2D,ArrayList<Vector>>();
/**
* Chunk store.
*/
private ChunkStore chunkStore;
/**
* Count of the number of missing chunks.
*/
private ArrayList<Vector2D> missingChunks;
/**
* Count of the number of chunks that could be loaded for other reasons.
*/
private ArrayList<Vector2D> errorChunks;
/**
* Last error message.
*/
private String lastErrorMessage;
/**
* Construct the snapshot restore operation.
*
* @param chunkStore
* @param region
*/
public SnapshotRestore(ChunkStore chunkStore, Region region) {
this.chunkStore = chunkStore;
if (region instanceof CuboidRegion) {
findNeededCuboidChunks(region);
} else {
findNeededChunks(region);
}
}
/**
* Find needed chunks in the cuboid of the region.
*
* @param region
*/
private void findNeededCuboidChunks(Region region) {
Vector min = region.getMinimumPoint();
Vector max = region.getMaximumPoint();
// First, we need to group points by chunk so that we only need
// to keep one chunk in memory at any given moment
for (int x = min.getBlockX(); x <= max.getBlockX(); x++) {
for (int y = min.getBlockY(); y <= max.getBlockY(); y++) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
Vector pos = new Vector(x, y, z);
BlockVector2D chunkPos = ChunkStore.toChunk(pos);
// Unidentified chunk
if (!neededChunks.containsKey(chunkPos)) {
neededChunks.put(chunkPos, new ArrayList<Vector>());
}
neededChunks.get(chunkPos).add(pos);
}
}
}
}
/**
* Find needed chunks in the region.
*
* @param region
*/
private void findNeededChunks(Region region) {
// First, we need to group points by chunk so that we only need
// to keep one chunk in memory at any given moment
for (Vector pos : region) {
BlockVector2D chunkPos = ChunkStore.toChunk(pos);
// Unidentified chunk
if (!neededChunks.containsKey(chunkPos)) {
neededChunks.put(chunkPos, new ArrayList<Vector>());
}
neededChunks.get(chunkPos).add(pos);
}
}
/**
* Get the number of chunks that are needed.
*
* @return
*/
public int getChunksAffected() {
return neededChunks.size();
}
/**
* Restores to world.
*
* @param editSession
* @throws MaxChangedBlocksException
*/
public void restore(EditSession editSession)
throws MaxChangedBlocksException {
missingChunks = new ArrayList<Vector2D>();
errorChunks = new ArrayList<Vector2D>();
// Now let's start restoring!
for (Map.Entry<BlockVector2D,ArrayList<Vector>> entry :
neededChunks.entrySet()) {
BlockVector2D chunkPos = entry.getKey();
Chunk chunk;
try {
chunk = chunkStore.getChunk(chunkPos);
// Good, the chunk could be at least loaded
// Now just copy blocks!
for (Vector pos : entry.getValue()) {
BaseBlock block = chunk.getBlock(pos);
editSession.setBlock(pos, block);
}
} catch (MissingChunkException me) {
missingChunks.add(chunkPos);
} catch (DataException de) {
errorChunks.add(chunkPos);
lastErrorMessage = de.getMessage();
} catch (IOException ioe) {
errorChunks.add(chunkPos);
lastErrorMessage = ioe.getMessage();
}
}
}
/**
* Get a list of the missing chunks. restore() must have been called
* already.
*
* @return
*/
public List<Vector2D> getMissingChunks() {
return missingChunks;
}
/**
* Get a list of the chunks that could not have been loaded for other
* reasons. restore() must have been called already.
*
* @return
*/
public List<Vector2D> getErrorChunks() {
return errorChunks;
}
/**
* Checks to see where the backup succeeded in any capacity. False will
* be returned if no chunk could be successfully loaded.
*
* @return
*/
public boolean hadTotalFailure() {
return missingChunks.size() + errorChunks.size() == getChunksAffected();
}
/**
* @return the lastErrorMessage
*/
public String getLastErrorMessage() {
return lastErrorMessage;
}
}

View File

@ -0,0 +1,50 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.snapshots;
import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class YYMMDDHHIISSParser implements SnapshotDateParser {
protected Pattern patt =
Pattern.compile("([0-9]+)[^0-9]?([0-9]+)[^0-9]?([0-9]+)[^0-9]?"
+ "([0-9]+)[^0-9]?([0-9]+)[^0-9]?([0-9]+)");
public Calendar detectDate(File file) {
Matcher matcher = patt.matcher(file.getName());
if (matcher.matches()) {
int year = Integer.parseInt(matcher.group(1));
int month = Integer.parseInt(matcher.group(2));
int day = Integer.parseInt(matcher.group(3));
int hrs = Integer.parseInt(matcher.group(4));
int min = Integer.parseInt(matcher.group(5));
int sec = Integer.parseInt(matcher.group(6));
Calendar calender = new GregorianCalendar();
calender.set(year, month, day, hrs, min, sec);
return calender;
}
return null;
}
}