Search in sources :

Example 1 with WorldIO

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();
}
Also used : UnloadableWorldException(org.pepsoft.worldpainter.UnloadableWorldException) WorldIO(org.pepsoft.worldpainter.WorldIO) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with WorldIO

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;
}
Also used : Configuration(org.pepsoft.worldpainter.Configuration) FileOutputStream(java.io.FileOutputStream) WorldIO(org.pepsoft.worldpainter.WorldIO) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 WorldIO (org.pepsoft.worldpainter.WorldIO)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 Configuration (org.pepsoft.worldpainter.Configuration)1 UnloadableWorldException (org.pepsoft.worldpainter.UnloadableWorldException)1