use of org.pepsoft.worldpainter.WorldIO in project WorldPainter by Captain-Chaos.
the class GetWorldOp method go.
@Override
public World2 go() throws ScriptException {
goCalled();
File file = sanityCheckFileName(fileName);
WorldIO worldIO = new WorldIO();
try {
worldIO.load(new FileInputStream(file));
} catch (IOException e) {
throw new ScriptException("I/O error while loading world " + fileName, e);
} catch (UnloadableWorldException e) {
throw new ScriptException("Unloadable world " + fileName + " (not a WorldPainter world?)", e);
}
return worldIO.getWorld();
}
use of org.pepsoft.worldpainter.WorldIO in project WorldPainter by Captain-Chaos.
the class SaveWorldOp method go.
@Override
public Void go() throws ScriptException {
goCalled();
// TODO: add .world if it is not there
File file = new File(fileName);
File dir = file.getAbsoluteFile().getParentFile();
if (!dir.isDirectory()) {
throw new ScriptException("Destination directory " + dir + " does not exist or is not a directory");
}
if (!dir.canWrite()) {
throw new ScriptException("Access denied to destination directory " + dir);
}
try {
Configuration config = Configuration.getInstance();
if ((config.getWorldFileBackups() > 0) && file.isFile()) {
for (int i = config.getWorldFileBackups(); i > 0; i--) {
File nextBackupFile = (i > 1) ? BackupUtil.getBackupFile(file, i - 1) : file;
if (nextBackupFile.isFile()) {
File backupFile = BackupUtil.getBackupFile(file, i);
if (backupFile.isFile()) {
if (!backupFile.delete()) {
throw new ScriptException("Could not delete old backup file " + backupFile);
}
}
if (!nextBackupFile.renameTo(backupFile)) {
throw new ScriptException("Could not move " + nextBackupFile + " to " + backupFile);
}
}
}
}
WorldIO worldIO = new WorldIO(world);
worldIO.save(new FileOutputStream(file));
} catch (IOException e) {
throw new ScriptException("I/O error saving file (message: " + e.getMessage() + ")", e);
}
return null;
}
Aggregations