org.shaman.rpg.editor.level.LevelBuildFileExtender.java Source code

Java tutorial

Introduction

Here is the source code for org.shaman.rpg.editor.level.LevelBuildFileExtender.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.shaman.rpg.editor.level;

import org.jdom2.Element;
import org.openide.util.lookup.ServiceProvider;
import org.shaman.rpg.editor.project.BuildFileExtender;
import org.shaman.rpg.editor.project.ProjectType;

/**
 * Extends the build file with a task that exports the levels.
 * @author Sebastian Wei
 */
@ServiceProvider(service = BuildFileExtender.class, position = 100)
public class LevelBuildFileExtender implements BuildFileExtender {

    @Override
    public void extendBuildScript(BuildFileExtender.Extender extender, ProjectType type) {
        if (type == ProjectType.MODULE) {
            extender.addTarget("levels", createExportLevelsTarget());
            extender.addTarget("assets", createSyncAssetsTarget());
        }
    }

    private Element createExportLevelsTarget() {
        Element target = new Element("target");

        Element mkdir = new Element("mkdir");
        mkdir.setAttribute("dir", "${" + Extender.OUTPUT_LEVELS + "}");
        target.addContent(mkdir);
        Element mkdir2 = new Element("mkdir");
        mkdir2.setAttribute("dir", "${" + Extender.INPUT_ROOT + "}" + Levels.LEVELS_FOLDER + "/");
        target.addContent(mkdir2);

        Element delEntries = new Element("delete");
        delEntries.setAttribute("file", "${" + Extender.OUTPUT_ENTRY_FILE + "}");
        target.addContent(delEntries);

        Element sync = new Element("sync");
        sync.setAttribute("todir", "${" + Extender.OUTPUT_LEVELS + "}");
        sync.setAttribute("verbose", "true");
        Element fileset = new Element("fileset");
        fileset.setAttribute("dir", "${" + Extender.INPUT_ROOT + "}" + Levels.LEVELS_FOLDER + "/");
        fileset.setAttribute("includes", "**/*.level");
        sync.addContent(fileset);
        target.addContent(sync);

        //add entries to the entry-file
        Element f = new Element("for");
        f.setAttribute("param", "filename");
        Element path = new Element("path");
        path.addContent(fileset.clone());
        f.addContent(path);
        Element sequential = new Element("sequential");
        Element path2 = new Element("path");
        path2.setAttribute("id", "engine.level.path-raw");
        path2.addContent(new Element("pathelement").setAttribute("location", "@{filename}"));
        sequential.addContent(path2);
        Element local = new Element("local");
        local.setAttribute("name", "engine.level.path");
        sequential.addContent(local);
        Element prop = new Element("pathconvert");
        prop.setAttribute("property", "engine.level.path");
        prop.setAttribute("dirsep", "/");
        prop.setAttribute("refid", "engine.level.path-raw");
        Element map = new Element("map");
        map.setAttribute("from", "${" + Extender.INPUT_ROOT + "}" + Levels.LEVELS_FOLDER + "/");
        map.setAttribute("to", "");
        prop.addContent(map);
        sequential.addContent(prop);
        Element echo = new Element("echo");
        echo.setAttribute("file", "${" + Extender.OUTPUT_ENTRY_FILE + "}");
        echo.setAttribute("append", "true");
        echo.setAttribute("message", "${engine.level.path}\n");
        sequential.addContent(echo);
        f.addContent(sequential);
        target.addContent(f);

        return target;
    }

    private Element createSyncAssetsTarget() {
        Element target = new Element("target");

        Element mkdir = new Element("mkdir");
        mkdir.setAttribute("dir", "${" + Extender.INPUT_ROOT + "}" + "assets/");
        target.addContent(mkdir);

        Element sync = new Element("copy");
        sync.setAttribute("todir", "${" + Extender.OUTPUT_ROOT + "}");
        Element fileset = new Element("fileset");
        fileset.setAttribute("dir", "${" + Extender.INPUT_ROOT + "}" + "assets/");
        sync.addContent(fileset);
        target.addContent(sync);

        return target;
    }
}