-
Notifications
You must be signed in to change notification settings - Fork 423
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
Add UnbakedModelDeserializer
#4321
base: 1.21.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.api.client.model.loading.v1; | ||
|
||
import java.io.Reader; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import net.minecraft.client.render.model.UnbakedModel; | ||
import net.minecraft.util.Identifier; | ||
|
||
import net.fabricmc.fabric.impl.client.model.loading.UnbakedModelDeserializerRegistry; | ||
|
||
public interface UnbakedModelDeserializer { | ||
static void register(Identifier id, UnbakedModelDeserializer deserializer) { | ||
UnbakedModelDeserializerRegistry.register(id, deserializer); | ||
} | ||
|
||
@Nullable | ||
static UnbakedModelDeserializer get(Identifier id) { | ||
return UnbakedModelDeserializerRegistry.get(id); | ||
} | ||
|
||
static UnbakedModel deserialize(Reader reader) throws JsonParseException { | ||
return UnbakedModelDeserializerRegistry.deserialize(reader); | ||
} | ||
|
||
UnbakedModel deserialize(JsonObject jsonObject, JsonDeserializationContext context) throws RuntimeException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing javadoc, especially with regards to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why RuntimeException rather than a Gson one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is acceptable for the deserializer to throw any |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.impl.client.model.loading; | ||
|
||
import java.io.Reader; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import com.google.gson.JsonParseException; | ||
|
||
import net.minecraft.client.render.model.UnbakedModel; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.JsonHelper; | ||
|
||
import net.fabricmc.fabric.api.client.model.loading.v1.UnbakedModelDeserializer; | ||
import net.fabricmc.fabric.mixin.client.model.loading.JsonUnbakedModelAccessor; | ||
|
||
public class UnbakedModelDeserializerRegistry { | ||
private static final Map<Identifier, UnbakedModelDeserializer> DESERIALIZERS = new HashMap<>(); | ||
|
||
public static void register(Identifier id, UnbakedModelDeserializer deserializer) { | ||
Objects.requireNonNull(id, "id cannot be null"); | ||
Objects.requireNonNull(id, "deserializer cannot be null"); | ||
|
||
if (DESERIALIZERS.putIfAbsent(id, deserializer) != null) { | ||
throw new IllegalArgumentException("UnbakedModelDeserializer with identifier '" + id + "' already registered"); | ||
} | ||
} | ||
|
||
public static UnbakedModelDeserializer get(Identifier id) { | ||
Objects.requireNonNull(id, "id cannot be null"); | ||
|
||
return DESERIALIZERS.get(id); | ||
} | ||
|
||
public static UnbakedModel deserialize(Reader reader) throws JsonParseException { | ||
return JsonHelper.deserialize(JsonUnbakedModelAccessor.fabric_getGson(), reader, UnbakedModel.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.impl.client.model.loading; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
|
||
import net.minecraft.client.render.model.UnbakedModel; | ||
import net.minecraft.client.render.model.json.JsonUnbakedModel; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.JsonHelper; | ||
|
||
import net.fabricmc.fabric.api.client.model.loading.v1.UnbakedModelDeserializer; | ||
|
||
public class UnbakedModelJsonDeserializer implements JsonDeserializer<UnbakedModel> { | ||
private static final String TYPE_KEY = "fabric:type"; | ||
|
||
@Override | ||
public UnbakedModel deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
|
||
if (jsonObject.has(TYPE_KEY)) { | ||
Identifier id = Identifier.of(JsonHelper.getString(jsonObject, TYPE_KEY)); | ||
UnbakedModelDeserializer deserializer = UnbakedModelDeserializer.get(id); | ||
|
||
if (deserializer == null) { | ||
throw new JsonParseException("Cannot deserialize custom unbaked model of unknown type '" + id + "'"); | ||
} | ||
|
||
return deserializer.deserialize(jsonObject, context); | ||
} | ||
|
||
return context.deserialize(jsonElement, JsonUnbakedModel.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.mixin.client.model.loading; | ||
|
||
import com.google.gson.Gson; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
import net.minecraft.client.render.model.json.JsonUnbakedModel; | ||
|
||
@Mixin(JsonUnbakedModel.class) | ||
public interface JsonUnbakedModelAccessor { | ||
@Accessor("GSON") | ||
static Gson fabric_getGson() { | ||
throw new AssertionError(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.mixin.client.model.loading; | ||
|
||
import com.google.gson.GsonBuilder; | ||
import com.llamalad7.mixinextras.injector.ModifyExpressionValue; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import net.minecraft.client.render.model.UnbakedModel; | ||
import net.minecraft.client.render.model.json.JsonUnbakedModel; | ||
|
||
import net.fabricmc.fabric.impl.client.model.loading.UnbakedModelJsonDeserializer; | ||
|
||
@Mixin(JsonUnbakedModel.class) | ||
abstract class JsonUnbakedModelMixin { | ||
@ModifyExpressionValue(method = "<clinit>()V", at = @At(value = "NEW", target = "com/google/gson/GsonBuilder", remap = false)) | ||
private static GsonBuilder addUnbakedModelAdapter(GsonBuilder builder) { | ||
return builder.registerTypeHierarchyAdapter(UnbakedModel.class, new UnbakedModelJsonDeserializer()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing javadoc