use of org.terasology.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);
}
}
}
use of org.terasology.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);
}
}
use of org.terasology.assets.format.AssetDataFile in project Terasology by MovingBlocks.
the class NUISkinEditorScreen method resetPreviewWidget.
/**
* {@inheritDoc}
*/
@Override
public void resetPreviewWidget() {
if (selectedScreen != null) {
try {
// Construct a UISkinData instance.
JsonElement skinElement = JsonTreeConverter.deserialize(getEditor().getRoot());
UISkinData data = new UISkinFormat().load(skinElement);
// Get the selected screen asset.
Optional<UIElement> sourceAsset = assetManager.getAsset(selectedScreen, UIElement.class);
if (!sourceAsset.isPresent()) {
throw new FileNotFoundException(String.format("Asset %s not found", selectedScreen));
}
AssetDataFile source = sourceAsset.get().getSource();
String content;
try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) {
reader.setLenient(true);
content = new JsonParser().parse(reader).toString();
}
if (content != null) {
JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content));
JsonElement screenElement = JsonTreeConverter.deserialize(node);
UIWidget widget = new UIFormat().load(screenElement, alternativeLocale).getRootWidget();
// Set the screen's skin using the previously generated UISkinData.
widget.setSkin(Assets.generateAsset(data, UISkin.class));
selectedScreenBox.setContent(widget);
}
} catch (Throwable t) {
String truncatedStackTrace = Joiner.on(System.lineSeparator()).join(Arrays.copyOfRange(ExceptionUtils.getStackFrames(t), 0, 10));
selectedScreenBox.setContent(new UILabel(truncatedStackTrace));
}
}
}
use of org.terasology.assets.format.AssetDataFile in project Terasology by MovingBlocks.
the class TranslationFormatTests method mockAssetDataFile.
// TODO: consider making this available to other test classes
private static AssetDataFile mockAssetDataFile(String fname, byte[] resource) throws IOException {
AssetDataFile assetDataFile = mock(AssetDataFile.class);
when(assetDataFile.openStream()).thenReturn(new BufferedInputStream(new ByteArrayInputStream(resource)));
when(assetDataFile.getFilename()).thenReturn(fname);
return assetDataFile;
}
use of org.terasology.assets.format.AssetDataFile in project Terasology by MovingBlocks.
the class TranslationFormatTests method testEmptyDataGenGermany.
@Test
public void testEmptyDataGenGermany() throws IOException, InvalidAssetFilenameException {
AssetDataFile assetDataFile = mockAssetDataFile("menu_de-DE.lang", "{}".getBytes(StandardCharsets.UTF_8));
ResourceUrn urn = createUrnFromFile(format, assetDataFile);
TranslationData data = format.load(urn, Collections.singletonList(assetDataFile));
Assert.assertEquals(Locale.GERMANY, data.getLocale());
Assert.assertTrue(data.getTranslations().isEmpty());
}
Aggregations