Search in sources :

Example 6 with AssetDataFile

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

the class TranslationFormatTests method testDataGenGerman.

@Test
public void testDataGenGerman() throws IOException, InvalidAssetFilenameException {
    byte[] resource = createSimpleTranslationFile().getBytes(StandardCharsets.UTF_8);
    AssetDataFile assetDataFile = mockAssetDataFile("menu_de-DE.lang", resource);
    ResourceUrn urn = createUrnFromFile(format, assetDataFile);
    TranslationData data = format.load(urn, Collections.singletonList(assetDataFile));
    assertEquals("Einzelspieler", data.getTranslations().get("engine:mainMenuScreen#singleplayer#text"));
}
Also used : AssetDataFile(org.terasology.gestalt.assets.format.AssetDataFile) TranslationData(org.terasology.engine.i18n.assets.TranslationData) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Test(org.junit.jupiter.api.Test)

Example 7 with AssetDataFile

use of org.terasology.gestalt.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));
    assertEquals(new ResourceUrn("engine:menu"), data.getProjectUrn());
    assertEquals(Locale.ROOT, data.getLocale());
}
Also used : AssetDataFile(org.terasology.gestalt.assets.format.AssetDataFile) TranslationData(org.terasology.engine.i18n.assets.TranslationData) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Test(org.junit.jupiter.api.Test)

Example 8 with AssetDataFile

use of org.terasology.gestalt.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());
    ResourceUrn projUrn = new ResourceUrn(urn.getModuleName(), projName);
    TranslationData data = new TranslationData(projUrn, 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.gestalt.assets.format.AssetDataFile) IOException(java.io.IOException) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) JsonParseException(com.google.gson.JsonParseException) Name(org.terasology.gestalt.naming.Name)

Example 9 with AssetDataFile

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

the class NUIEditorScreen method selectAsset.

/**
 * {@inheritDoc}
 */
@Override
public void selectAsset(ResourceUrn urn) {
    boolean isLoaded = assetManager.isLoaded(urn, UIElement.class);
    Optional<UIElement> asset = assetManager.getAsset(urn, UIElement.class);
    if (asset.isPresent()) {
        UIElement element = asset.get();
        if (!isLoaded) {
            asset.get().dispose();
        }
        AssetDataFile source = element.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 : UIElement(org.terasology.nui.asset.UIElement) InputStreamReader(java.io.InputStreamReader) JsonTree(org.terasology.nui.widgets.treeView.JsonTree) AssetDataFile(org.terasology.gestalt.assets.format.AssetDataFile) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 10 with AssetDataFile

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

the class NUIEditorScreen method setSelectedAssetPath.

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

Aggregations

AssetDataFile (org.terasology.gestalt.assets.format.AssetDataFile)12 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)5 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)4 Test (org.junit.jupiter.api.Test)4 TranslationData (org.terasology.engine.i18n.assets.TranslationData)4 JsonParser (com.google.gson.JsonParser)3 JsonReader (com.google.gson.stream.JsonReader)3 UIElement (org.terasology.nui.asset.UIElement)3 JsonTree (org.terasology.nui.widgets.treeView.JsonTree)3 UISkinAsset (org.terasology.nui.skin.UISkinAsset)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 UIFormat (org.terasology.engine.rendering.nui.asset.UIFormat)1 UISkinFormat (org.terasology.engine.rendering.nui.skin.UISkinFormat)1 Name (org.terasology.gestalt.naming.Name)1