Search in sources :

Example 51 with ResourceUrn

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

the class TranslationSystemImpl method refresh.

@Override
public void refresh() {
    Set<ResourceUrn> urns = assetManager.getAvailableAssets(Translation.class);
    for (ResourceUrn urn : urns) {
        Optional<Translation> asset = assetManager.getAsset(urn, Translation.class);
        if (asset.isPresent()) {
            Translation trans = asset.get();
            ResourceUrn projectUrn = trans.getProjectUrn();
            if (!projectUrn.getModuleName().isEmpty() && !projectUrn.getResourceName().isEmpty()) {
                TranslationProject proj = projects.computeIfAbsent(projectUrn, e -> new StandardTranslationProject());
                proj.add(trans);
                trans.subscribe(this::onAssetChanged);
                logger.info("Discovered " + trans);
            } else {
                logger.warn("Ignoring invalid project projectUrn: {}", projectUrn);
            }
        }
    }
}
Also used : Translation(org.terasology.engine.i18n.assets.Translation) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn)

Example 52 with ResourceUrn

use of org.terasology.gestalt.assets.ResourceUrn 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 53 with ResourceUrn

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

the class BehaviorNodeFactory method refreshPrefabs.

private void refreshPrefabs() {
    Collection<Prefab> prefabs = prefabManager.listPrefabs(BehaviorNodeComponent.class);
    if (prefabs.size() == 0) {
        // called from main menu
        List<String> nodes = Arrays.asList("counter", "timer", "loop", "lookup", "dynselector", "fail", "parallel", "playMusic", "playSound", "running", "selector", "setAnimation", "sequence", "succeed");
        prefabs = Lists.newArrayList();
        for (String node : nodes) {
            prefabs.add(Assets.get(new ResourceUrn("engine:" + node), Prefab.class).orElse(null));
        }
    }
    for (Prefab prefab : prefabs) {
        EntityBuilder entityBuilder = entityManager.newBuilder(prefab);
        entityBuilder.setPersistent(false);
        EntityRef entityRef = entityBuilder.build();
        BehaviorNodeComponent component = entityRef.getComponent(BehaviorNodeComponent.class);
        addToCategory(component);
        nodeComponents.add(component);
    }
}
Also used : EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 54 with ResourceUrn

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

the class BehaviorTreeBuilder method initGson.

private void initGson() {
    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeHierarchyAdapter(BehaviorNode.class, this);
        gsonBuilder.registerTypeAdapterFactory(new UriTypeAdapterFactory());
        gsonBuilder.registerTypeAdapter(BehaviorTree.class, new TypeAdapter<BehaviorTree>() {

            @Override
            public void write(JsonWriter out, BehaviorTree value) throws IOException {
                if (value != null) {
                    // TODO doublecheck URN
                    out.value(value.getUrn().toString());
                } else {
                    out.value("");
                }
            }

            @Override
            public BehaviorTree read(JsonReader in) throws IOException {
                String uri = in.nextString();
                AssetManager assetManager = CoreRegistry.get(AssetManager.class);
                return assetManager.getAsset(new ResourceUrn(uri), BehaviorTree.class).orElse(assetManager.getAsset(new ResourceUrn("engine:default"), BehaviorTree.class).get());
            }
        });
        gson = gsonBuilder.create();
    }
}
Also used : AssetManager(org.terasology.gestalt.assets.management.AssetManager) GsonBuilder(com.google.gson.GsonBuilder) UriTypeAdapterFactory(org.terasology.engine.utilities.gson.UriTypeAdapterFactory) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) BehaviorTree(org.terasology.engine.logic.behavior.asset.BehaviorTree) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) JsonWriter(com.google.gson.stream.JsonWriter)

Example 55 with ResourceUrn

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

the class PojoPrefabManagerTest method testRetrievePrefab.

@Test
public void testRetrievePrefab() {
    PrefabData data = new PrefabData();
    data.addComponent(new StringComponent("Test"));
    Assets.generateAsset(new ResourceUrn(PREFAB_NAME), data, Prefab.class);
    Prefab ref = prefabManager.getPrefab(PREFAB_NAME);
    assertNotNull(ref);
    assertEquals(PREFAB_NAME, ref.getName());
}
Also used : PrefabData(org.terasology.engine.entitySystem.prefab.PrefabData) StringComponent(org.terasology.unittest.stubs.StringComponent) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) PojoPrefab(org.terasology.engine.entitySystem.prefab.internal.PojoPrefab) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)90 Test (org.junit.jupiter.api.Test)19 Name (org.terasology.gestalt.naming.Name)12 BeforeEach (org.junit.jupiter.api.BeforeEach)11 BlockFamilyDefinitionData (org.terasology.engine.world.block.loader.BlockFamilyDefinitionData)10 IOException (java.io.IOException)9 AssetManager (org.terasology.gestalt.assets.management.AssetManager)9 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)8 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)8 PrefabData (org.terasology.engine.entitySystem.prefab.PrefabData)7 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)7 BlockUri (org.terasology.engine.world.block.BlockUri)7 AssetDataFile (org.terasology.gestalt.assets.format.AssetDataFile)7 Texture (org.terasology.engine.rendering.assets.texture.Texture)5 BlockManagerImpl (org.terasology.engine.world.block.internal.BlockManagerImpl)5 BlockFamilyDefinition (org.terasology.engine.world.block.loader.BlockFamilyDefinition)5 NullWorldAtlas (org.terasology.engine.world.block.tiles.NullWorldAtlas)5 ExtraBlockDataManager (org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager)5 StringComponent (org.terasology.unittest.stubs.StringComponent)5 ByteBuffer (java.nio.ByteBuffer)4