Search in sources :

Example 36 with ResourceUrn

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

the class AbstractEditorScreen method loadAutosave.

/**
 * Resets the editor based on the state of the autosave file.
 */
protected void loadAutosave() {
    if (!disableAutosave) {
        try (JsonReader reader = new JsonReader(new InputStreamReader(Files.newInputStream(getAutosaveFile())))) {
            reader.setLenient(true);
            String autosaveString = new JsonParser().parse(reader).toString();
            JsonObject autosaveObject = new JsonParser().parse(autosaveString).getAsJsonObject();
            String selectedAsset = autosaveObject.get("selectedAsset").getAsString();
            setSelectedAsset(selectedAsset);
            try {
                ResourceUrn urn = new ResourceUrn(selectedAsset);
                setSelectedAssetPath(urn);
            } catch (InvalidUrnException ignored) {
            }
            JsonTree editorContents = JsonTreeConverter.serialize(autosaveObject.get("editorContents"));
            resetState(editorContents);
            setUnsavedChangesPresent(true);
        } catch (NoSuchFileException ignored) {
        } catch (IOException e) {
            logger.warn("Could not load autosaved info", e);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InvalidUrnException(org.terasology.gestalt.assets.exceptions.InvalidUrnException) JsonTree(org.terasology.nui.widgets.treeView.JsonTree) NoSuchFileException(java.nio.file.NoSuchFileException) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) JsonParser(com.google.gson.JsonParser)

Example 37 with ResourceUrn

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

the class EntitySerializerTest method testComponentWithDoNotPersistIsNotPersisted.

@Test
public void testComponentWithDoNotPersistIsNotPersisted() {
    componentLibrary.register(new ResourceUrn("test", "nonpersisted"), NonpersistedComponent.class);
    EntityRef entity = entityManager.create();
    entity.addComponent(new NonpersistedComponent());
    entitySerializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
    EntityData.Entity entityData = entitySerializer.serialize(entity);
    long nextId = entityManager.getNextId();
    entityManager.clear();
    entityManager.setNextId(nextId);
    EntityRef loadedEntity = entitySerializer.deserialize(entityData);
    assertTrue(loadedEntity.exists());
    assertFalse(loadedEntity.hasComponent(NonpersistedComponent.class));
}
Also used : PersistenceComponentSerializeCheck(org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck) EntityData(org.terasology.protobuf.EntityData) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Example 38 with ResourceUrn

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

the class EntitySerializerTest method testComponentWithDoNotPersistIsIgnoredUponDeserialization.

@Test
public void testComponentWithDoNotPersistIsIgnoredUponDeserialization() {
    componentLibrary.register(new ResourceUrn("test", "nonpersisted"), NonpersistedComponent.class);
    EntityRef entity = entityManager.create();
    entity.addComponent(new NonpersistedComponent());
    entitySerializer.setComponentSerializeCheck(metadata -> true);
    EntityData.Entity entityData = entitySerializer.serialize(entity);
    long nextId = entityManager.getNextId();
    entityManager.clear();
    entityManager.setNextId(nextId);
    entitySerializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
    EntityRef loadedEntity = entitySerializer.deserialize(entityData);
    assertTrue(loadedEntity.exists());
    assertFalse(loadedEntity.hasComponent(NonpersistedComponent.class));
}
Also used : PersistenceComponentSerializeCheck(org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck) EntityData(org.terasology.protobuf.EntityData) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Example 39 with ResourceUrn

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

the class EntitySerializerTest method testMappedTypeHandling.

@Test
public void testMappedTypeHandling() throws Exception {
    componentLibrary.register(new ResourceUrn("test", "mappedtype"), MappedTypeComponent.class);
    EntityRef entity = entityManager.create();
    entity.addComponent(new MappedTypeComponent());
    EntityData.Entity entityData = entitySerializer.serialize(entity);
    long nextId = entityManager.getNextId();
    entityManager.clear();
    entityManager.setNextId(nextId);
    EntityRef loadedEntity = entitySerializer.deserialize(entityData);
    assertTrue(loadedEntity.exists());
    assertTrue(loadedEntity.hasComponent(MappedTypeComponent.class));
}
Also used : EntityData(org.terasology.protobuf.EntityData) MappedTypeComponent(org.terasology.unittest.stubs.MappedTypeComponent) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Example 40 with ResourceUrn

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

the class ComponentSerializerTest method setup.

@BeforeEach
public void setup() {
    context = new ContextImpl();
    context.put(RecordAndReplayCurrentStatus.class, new RecordAndReplayCurrentStatus());
    context.put(ModuleManager.class, moduleManager);
    CoreRegistry.setContext(context);
    Reflections reflections = new Reflections(getClass().getClassLoader());
    TypeHandlerLibrary serializationLibrary = new TypeHandlerLibraryImpl(reflections);
    serializationLibrary.addTypeHandler(Vector3f.class, new Vector3fTypeHandler());
    serializationLibrary.addTypeHandler(Quaternionf.class, new QuaternionfTypeHandler());
    NetworkSystem networkSystem = mock(NetworkSystem.class);
    when(networkSystem.getMode()).thenReturn(NetworkMode.NONE);
    context.put(NetworkSystem.class, networkSystem);
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    EngineEntityManager entityManager = context.get(EngineEntityManager.class);
    entityManager.getComponentLibrary().register(new ResourceUrn("test", "gettersetter"), GetterSetterComponent.class);
    entityManager.getComponentLibrary().register(new ResourceUrn("test", "string"), StringComponent.class);
    entityManager.getComponentLibrary().register(new ResourceUrn("test", "integer"), IntegerComponent.class);
    ComponentLibrary componentLibrary = entityManager.getComponentLibrary();
    componentSerializer = new ComponentSerializer(componentLibrary, serializationLibrary);
}
Also used : EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) QuaternionfTypeHandler(org.terasology.engine.persistence.typeHandling.mathTypes.QuaternionfTypeHandler) Vector3fTypeHandler(org.terasology.engine.persistence.typeHandling.mathTypes.Vector3fTypeHandler) TypeHandlerLibrary(org.terasology.persistence.typeHandling.TypeHandlerLibrary) NetworkSystem(org.terasology.engine.network.NetworkSystem) ComponentLibrary(org.terasology.engine.entitySystem.metadata.ComponentLibrary) RecordAndReplayCurrentStatus(org.terasology.engine.recording.RecordAndReplayCurrentStatus) ContextImpl(org.terasology.engine.context.internal.ContextImpl) TypeHandlerLibraryImpl(org.terasology.engine.persistence.typeHandling.TypeHandlerLibraryImpl) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) ComponentSerializer(org.terasology.engine.persistence.serializers.ComponentSerializer) Reflections(org.reflections.Reflections) BeforeEach(org.junit.jupiter.api.BeforeEach)

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