Search in sources :

Example 6 with AssetDataFile

use of org.terasology.assets.format.AssetDataFile in project Terasology by MovingBlocks.

the class NUISkinEditorScreen method selectAsset.

/**
 * {@inheritDoc}
 */
@Override
public void selectAsset(ResourceUrn urn) {
    boolean isLoaded = assetManager.isLoaded(urn, UISkin.class);
    Optional<UISkin> asset = assetManager.getAsset(urn, UISkin.class);
    if (asset.isPresent()) {
        UISkin skin = asset.get();
        if (!isLoaded) {
            asset.get().dispose();
        }
        AssetDataFile source = skin.getSource();
        String content = null;
        try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) {
            reader.setLenient(true);
            content = new JsonParser().parse(reader).toString();
        } catch (IOException e) {
            logger.error(String.format("Could not load asset source file for %s", urn.toString()), e);
        }
        if (content != null) {
            JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content));
            selectedAssetPending = urn.toString();
            resetState(node);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UISkin(org.terasology.rendering.nui.skin.UISkin) AssetDataFile(org.terasology.assets.format.AssetDataFile) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 7 with AssetDataFile

use of org.terasology.assets.format.AssetDataFile in project Terasology by MovingBlocks.

the class NUISkinEditorScreen method setSelectedAssetPath.

/**
 * {@inheritDoc}
 */
@Override
protected void setSelectedAssetPath(ResourceUrn urn) {
    boolean isLoaded = assetManager.isLoaded(urn, UISkin.class);
    Optional<UISkin> asset = assetManager.getAsset(urn, UISkin.class);
    if (asset.isPresent()) {
        UISkin skin = asset.get();
        if (!isLoaded) {
            asset.get().dispose();
        }
        AssetDataFile source = skin.getSource();
        selectedAssetPath = getPath(source);
    }
}
Also used : UISkin(org.terasology.rendering.nui.skin.UISkin) AssetDataFile(org.terasology.assets.format.AssetDataFile)

Example 8 with AssetDataFile

use of org.terasology.assets.format.AssetDataFile in project Terasology by MovingBlocks.

the class GLSLShaderFormat method load.

@Override
public ShaderData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    String vertProgram = null;
    String fragProgram = null;
    ShaderMetadata metadata = new ShaderMetadata();
    for (AssetDataFile input : inputs) {
        if (input.getFilename().endsWith(VERTEX_SUFFIX)) {
            vertProgram = readInput(input);
        } else if (input.getFilename().endsWith(FRAGMENT_SUFFIX)) {
            fragProgram = readInput(input);
        } else {
            metadata = readMetadata(input);
        }
    }
    if (vertProgram != null && fragProgram != null) {
        return new ShaderData(vertProgram, fragProgram, metadata.getParameters());
    }
    throw new IOException("Failed to load shader '" + urn + "' - missing vertex or fragment program");
}
Also used : AssetDataFile(org.terasology.assets.format.AssetDataFile) IOException(java.io.IOException)

Example 9 with AssetDataFile

use of org.terasology.assets.format.AssetDataFile in project Terasology by MovingBlocks.

the class TranslationFormat method load.

@Override
public TranslationData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    if (inputs.size() != 1) {
        throw new IOException("Failed to load translation data '" + urn + "': " + inputs);
    }
    AssetDataFile file = inputs.get(0);
    Locale locale = localeFromFilename(file.getFilename());
    Name projName = basenameFromFilename(file.getFilename());
    SimpleUri projUri = new SimpleUri(urn.getModuleName(), projName);
    TranslationData data = new TranslationData(projUri, locale);
    try (InputStreamReader isr = new InputStreamReader(file.openStream(), Charsets.UTF_8)) {
        Map<String, String> entry = gson.fromJson(isr, MAP_TOKEN.getType());
        data.addAll(entry);
    } catch (JsonParseException e) {
        throw new IOException("Could not parse file '" + file + "'", e);
    }
    return data;
}
Also used : Locale(java.util.Locale) InputStreamReader(java.io.InputStreamReader) AssetDataFile(org.terasology.assets.format.AssetDataFile) SimpleUri(org.terasology.engine.SimpleUri) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) Name(org.terasology.naming.Name)

Example 10 with AssetDataFile

use of org.terasology.assets.format.AssetDataFile in project Terasology by MovingBlocks.

the class TranslationFormatTests method testEmptyDataGenRoot.

@Test
public void testEmptyDataGenRoot() throws IOException, InvalidAssetFilenameException {
    AssetDataFile assetDataFile = mockAssetDataFile("menu.lang", "{}".getBytes(StandardCharsets.UTF_8));
    ResourceUrn urn = createUrnFromFile(format, assetDataFile);
    TranslationData data = format.load(urn, Collections.singletonList(assetDataFile));
    Assert.assertEquals(new SimpleUri("engine:menu"), data.getProjectUri());
    Assert.assertEquals(Locale.ROOT, data.getLocale());
}
Also used : AssetDataFile(org.terasology.assets.format.AssetDataFile) SimpleUri(org.terasology.engine.SimpleUri) TranslationData(org.terasology.i18n.assets.TranslationData) ResourceUrn(org.terasology.assets.ResourceUrn) Test(org.junit.Test)

Aggregations

AssetDataFile (org.terasology.assets.format.AssetDataFile)11 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)4 JsonParser (com.google.gson.JsonParser)3 JsonReader (com.google.gson.stream.JsonReader)3 Test (org.junit.Test)3 ResourceUrn (org.terasology.assets.ResourceUrn)3 TranslationData (org.terasology.i18n.assets.TranslationData)3 UIElement (org.terasology.rendering.nui.asset.UIElement)3 UISkin (org.terasology.rendering.nui.skin.UISkin)3 JsonTree (org.terasology.rendering.nui.widgets.treeView.JsonTree)3 SimpleUri (org.terasology.engine.SimpleUri)2 JsonElement (com.google.gson.JsonElement)1 JsonParseException (com.google.gson.JsonParseException)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Locale (java.util.Locale)1 Name (org.terasology.naming.Name)1 UIWidget (org.terasology.rendering.nui.UIWidget)1