Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add type handlers for SimpleUri and BlockUri #5061

Merged
merged 11 commits into from
Aug 23, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import org.joml.Vector4i;
import org.joml.Vector4ic;
import org.reflections.Reflections;
import org.terasology.engine.core.SimpleUri;
import org.terasology.engine.core.module.ModuleManager;
import org.terasology.engine.math.IntegerRange;
import org.terasology.engine.persistence.typeHandling.extensionTypes.ChunkMeshTypeHandler;
import org.terasology.engine.persistence.typeHandling.extensionTypes.ColorTypeHandler;
import org.terasology.engine.persistence.typeHandling.extensionTypes.ColorcTypeHandler;
import org.terasology.engine.persistence.typeHandling.extensionTypes.NameTypeHandler;
import org.terasology.engine.persistence.typeHandling.extensionTypes.SimpleUriTypeHandler;
import org.terasology.engine.persistence.typeHandling.extensionTypes.TextureRegionTypeHandler;
import org.terasology.engine.persistence.typeHandling.extensionTypes.UITextureRegionTypeHandler;
import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.AssetTypeHandlerFactory;
Expand Down Expand Up @@ -113,6 +115,7 @@ private static void populateWithDefaultHandlers(TypeHandlerLibrary serialization
serializationLibrary.addTypeHandlerFactory(new AssetTypeHandlerFactory());

serializationLibrary.addTypeHandler(Name.class, new NameTypeHandler());
serializationLibrary.addTypeHandler(SimpleUri.class, new SimpleUriTypeHandler());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll want to add BlockUri's handler here too, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thank you! will add that later today 👍

serializationLibrary.addTypeHandler(TextureRegion.class, new TextureRegionTypeHandler());
serializationLibrary.addTypeHandler(UITextureRegion.class, new UITextureRegionTypeHandler());
serializationLibrary.addTypeHandler(ChunkMesh.class, new ChunkMeshTypeHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.engine.persistence.typeHandling.extensionTypes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.core.SimpleUri;
import org.terasology.persistence.typeHandling.StringRepresentationTypeHandler;

public class SimpleUriTypeHandler extends StringRepresentationTypeHandler<SimpleUri> {

private static final Logger logger = LoggerFactory.getLogger(SimpleUriTypeHandler.class);

@Override
public String getAsString(SimpleUri uri) {
if (uri == null) {
return "";
}
return uri.toString();
}

@Override
public SimpleUri getFromString(String representation) {
SimpleUri uri = new SimpleUri(representation);
if (!uri.isValid()) {
logger.error("Failed to create valid SimpleURI from string '{}'", representation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.error("Failed to create valid SimpleURI from string '{}'", representation);
logger.error("Failed to create valid SimpleURI from string '{}'", representation);
// StringRepresentationTypeHandler will turn this 'null' value into an empty Optional
return null;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ✔️

also added tests

}

return uri;
}
}