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);
}
}
}
}
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;
}
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);
}
}
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();
}
}
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());
}
Aggregations