Skip to content

Commit

Permalink
Move datagen to its own source set
Browse files Browse the repository at this point in the history
MC 1.21.4 means we have to move more data generation code into the
client source set. Given all this code movement, it probably makes sense
to put data generation in a separate source set instead.

1.21.4 also has split data generators for client and server, but neither
mod loader recommends this. This means we can/should merge DataProviders
and ClientDataProviders into a single class.

Data generators are no longer bundled with the jar, which does reduce
file size, but by a tiny amount (~70KiB).
  • Loading branch information
SquidDev committed Dec 5, 2024
1 parent 7c02979 commit 9bbf3f3
Show file tree
Hide file tree
Showing 28 changed files with 84 additions and 87 deletions.
2 changes: 1 addition & 1 deletion REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ path = [
"projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/.ignoreme",
"projects/core/src/main/resources/data/computercraft/lua/rom/modules/turtle/.ignoreme",
"projects/core/src/main/resources/data/computercraft/lua/rom/motd.txt",
"projects/fabric-api/src/main/modJson/fabric.mod.json",
"projects/fabric/src/client/resources/computercraft-client.fabric.mixins.json",
"projects/fabric/src/datagen/resources/fabric.mod.json",
"projects/fabric/src/main/resources/computercraft.fabric.mixins.json",
"projects/fabric/src/main/resources/fabric.mod.json",
"projects/fabric/src/testMod/resources/computercraft-gametest.fabric.mixins.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,32 @@ plugins {
val main = sourceSets["main"]
val client = sourceSets["client"]

// Both testMod and testFixtures inherit from the main and client classpath, just so we have access to Minecraft classes.
// datagen and testMod inherit from the main and client classpath, just so we have access to Minecraft classes.
val datagen by sourceSets.creating {
compileClasspath += main.compileClasspath + client.compileClasspath
runtimeClasspath += main.runtimeClasspath + client.runtimeClasspath
}

val testMod by sourceSets.creating {
compileClasspath += main.compileClasspath + client.compileClasspath
runtimeClasspath += main.runtimeClasspath + client.runtimeClasspath
}

configurations {
named(testMod.compileClasspathConfigurationName) {
shouldResolveConsistentlyWith(compileClasspath.get())
}
val extraConfigurations = listOf(datagen, testMod)

named(testMod.runtimeClasspathConfigurationName) {
shouldResolveConsistentlyWith(runtimeClasspath.get())
configurations {
for (config in extraConfigurations) {
named(config.compileClasspathConfigurationName) { shouldResolveConsistentlyWith(compileClasspath.get()) }
named(config.runtimeClasspathConfigurationName) { shouldResolveConsistentlyWith(runtimeClasspath.get()) }
}
}

// Like the main test configurations, we're safe to depend on source set outputs.
dependencies {
add(testMod.implementationConfigurationName, main.output)
add(testMod.implementationConfigurationName, client.output)
for (config in extraConfigurations) {
add(config.implementationConfigurationName, main.output)
add(config.implementationConfigurationName, client.output)
}
}

// Similar to java-test-fixtures, but tries to avoid putting the obfuscated jar on the classpath.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,13 @@ abstract class CCTweakedExtension(
val otherJava = otherProject.extensions.getByType(JavaPluginExtension::class.java)
val main = otherJava.sourceSets.getByName("main")
val client = otherJava.sourceSets.getByName("client")
val testMod = otherJava.sourceSets.findByName("testMod")
val testFixtures = otherJava.sourceSets.findByName("testFixtures")

// Pull in sources from the other project.
extendSourceSet(otherProject, main)
extendSourceSet(otherProject, client)
if (testMod != null) extendSourceSet(otherProject, testMod)
if (testFixtures != null) extendSourceSet(otherProject, testFixtures)
for (sourceSet in listOf("datagen", "testMod", "testFixtures")) {
otherJava.sourceSets.findByName(sourceSet)?.let { extendSourceSet(otherProject, it) }
}

// The extra source-processing tasks should include these files too.
project.tasks.named(main.javadocTaskName, Javadoc::class.java) { source(main.allJava, client.allJava) }
Expand Down
2 changes: 1 addition & 1 deletion projects/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import cc.tweaked.gradle.*

plugins {
id("cc-tweaked.vanilla")
id("cc-tweaked.gametest")
id("cc-tweaked.illuaminate")
id("cc-tweaked.mod")
id("cc-tweaked.publishing")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.client.render.ComputerBorderRenderer;
import dan200.computercraft.data.client.ClientDataProviders;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureManager;
Expand Down Expand Up @@ -113,7 +112,6 @@ public Stream<ResourceLocation> textures() {
* @param pocketBottom The texture for the bottom of a pocket computer.
* @param sidebar The texture for the computer sidebar.
* @see ComputerBorderRenderer
* @see ClientDataProviders
*/
public record ComputerTextures(
ResourceLocation border,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
package dan200.computercraft.data;

import com.mojang.serialization.Codec;
import dan200.computercraft.client.gui.GuiSprites;
import dan200.computercraft.client.model.LecternPrintoutModel;
import dan200.computercraft.shared.turtle.inventory.UpgradeSlot;
import net.minecraft.client.renderer.texture.atlas.SpriteSource;
import net.minecraft.client.renderer.texture.atlas.SpriteSources;
import net.minecraft.client.renderer.texture.atlas.sources.SingleFile;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.data.tags.TagsProvider;
Expand All @@ -13,9 +19,14 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* All data providers for ComputerCraft. We require a mod-loader abstraction {@link GeneratorSink} (instead of
Expand All @@ -39,14 +50,30 @@ public static void add(GeneratorSink generator) {

generator.add(out -> new LanguageProvider(out, turtleUpgrades, pocketUpgrades));

// Unfortunately we rely on some client-side classes in this code. We just load in the client side data provider
// and invoke that.
try {
Class.forName("dan200.computercraft.data.client.ClientDataProviders")
.getMethod("add", GeneratorSink.class).invoke(null, generator);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
generator.addFromCodec("Block atlases", PackType.CLIENT_RESOURCES, "atlases", SpriteSources.FILE_CODEC, out -> {
out.accept(new ResourceLocation("blocks"), makeSprites(Stream.of(
UpgradeSlot.LEFT_UPGRADE,
UpgradeSlot.RIGHT_UPGRADE,
LecternPrintoutModel.TEXTURE
)));
out.accept(GuiSprites.SPRITE_SHEET, makeSprites(
// Buttons
GuiSprites.TURNED_OFF.textures(),
GuiSprites.TURNED_ON.textures(),
GuiSprites.TERMINATE.textures(),
// Computers
GuiSprites.COMPUTER_NORMAL.textures(),
GuiSprites.COMPUTER_ADVANCED.textures(),
GuiSprites.COMPUTER_COMMAND.textures(),
GuiSprites.COMPUTER_COLOUR.textures()
));
});
}

@SafeVarargs
@SuppressWarnings("varargs")
private static List<SpriteSource> makeSprites(final Stream<ResourceLocation>... files) {
return Arrays.stream(files).flatMap(Function.identity()).<SpriteSource>map(x -> new SingleFile(x, Optional.empty())).toList();
}

public interface GeneratorSink {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import dan200.computercraft.shared.util.PrettyJsonWriter;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public TagAppender<T> addTag(TagKey<T> tag) {
/**
* A wrapper over {@link ItemTagsProvider}.
*/
interface ItemTagConsumer extends TagConsumer<Item> {
public interface ItemTagConsumer extends TagConsumer<Item> {
void copy(TagKey<Block> block, TagKey<Item> item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MPL-2.0

package dan200.computercraft.data;
package dan200.computercraft.shared.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -23,7 +23,7 @@
/**
* Alternative version of {@link JsonWriter} which attempts to lay out the JSON in a more compact format.
*
* @see PrettyDataProvider
* @see dan200.computercraft.data.PrettyDataProvider
*/
public class PrettyJsonWriter extends JsonWriter {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.data.PrettyJsonWriter;
import dan200.computercraft.gametest.core.TestHooks;
import dan200.computercraft.shared.platform.RegistryWrappers;
import dan200.computercraft.shared.util.PrettyJsonWriter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
Expand Down
4 changes: 3 additions & 1 deletion projects/fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.*

plugins {
id("cc-tweaked.fabric")
id("cc-tweaked.gametest")
id("cc-tweaked.mod")
id("cc-tweaked.mod-publishing")
}

Expand Down Expand Up @@ -146,6 +146,8 @@ loom {
configName = "Datagen"
client()

source(sourceSets.datagen.get())

runDir("run/dataGen")
property("fabric-api.datagen")
property("fabric-api.datagen.output-dir", layout.buildDirectory.dir("generatedResources").getAbsolutePath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class FabricDataGenerators implements DataGeneratorEntrypoint {
public class FabricDataProviders implements DataGeneratorEntrypoint {
@Override
public void onInitializeDataGenerator(FabricDataGenerator generator) {
var pack = new PlatformGeneratorsImpl(generator.createPack());
Expand Down
13 changes: 13 additions & 0 deletions projects/fabric/src/datagen/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"schemaVersion": 1,
"id": "cc-datagen",
"version": "1.0.0",
"entrypoints": {
"fabric-datagen": [
"dan200.computercraft.data.FabricDataProviders"
]
},
"depends": {
"computercraft": "*"
}
}
3 changes: 0 additions & 3 deletions projects/fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
"client": [
"dan200.computercraft.client.ComputerCraftClient::init"
],
"fabric-datagen": [
"dan200.computercraft.data.FabricDataGenerators"
],
"jei_mod_plugin": [
"dan200.computercraft.client.integration.jei.JEIComputerCraft"
],
Expand Down
2 changes: 1 addition & 1 deletion projects/fabric/src/testMod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"id": "cctest",
"id": "cc-datagen",
"version": "1.0.0",
"entrypoints": {
"main": [
Expand Down
6 changes: 5 additions & 1 deletion projects/forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import net.minecraftforge.gradle.common.util.RunConfig

plugins {
id("cc-tweaked.forge")
id("cc-tweaked.gametest")
id("cc-tweaked.mod")
id("cc-tweaked.mod-publishing")
}

Expand Down Expand Up @@ -57,6 +57,10 @@ minecraft {
"--existing", project(":common").file("src/main/resources/"),
"--existing", file("src/main/resources/"),
)

mods.named("computercraft") {
source(sourceSets["datagen"])
}
}

fun RunConfig.configureForGameTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.function.Consumer;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class Generators {
public class ForgeDataProviders {
@SubscribeEvent
public static void gather(GatherDataEvent event) {
var generator = event.getGenerator();
Expand Down

0 comments on commit 9bbf3f3

Please sign in to comment.