Skip to content

Commit

Permalink
finally support crt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ecdcaeb committed Mar 16, 2024
1 parent 5d0533d commit c573fea
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 27 deletions.
4 changes: 4 additions & 0 deletions dev/example/assets/extra_recipe/crt/test/test2.zs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#loader recipeevent crafttweaker
#debug

recipes.addShapeless(<minecraft:diamond> ,[<minecraft:diamond>]);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#priority 1
#debug
#loader contenttweaker

import mods.contenttweaker.VanillaFactory;
Expand Down
3 changes: 0 additions & 3 deletions dev/example/assets/extra_recipe/crt/tests/test2.zs

This file was deleted.

11 changes: 6 additions & 5 deletions src/main/java/mods/Hileb/rml/compat/crt/RMLCrTLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ public class RMLCrTLoader {
}
}

public static LazyOptional<HashSet<ScriptProviderCustom>> cachedScriptProviders = LazyOptional.of(() -> {
HashSet<ScriptProviderCustom> cachedScriptProvider = new HashSet<>();
public static LazyOptional<HashSet<IScriptProvider>> cachedScriptProviders = LazyOptional.of(() -> {
HashSet<IScriptProvider> cachedScriptProvider = new HashSet<>();
RMLScriptProvider providerCustom = new RMLScriptProvider();
for(ContainerHolder containerHolder : ResourceModLoader.getCurrentRMLContainerHolders()){
if (containerHolder.modules.contains(ContainerHolder.Modules.MOD_CRT)){
final ModContainer modContainer = containerHolder.container;
Loader.instance().setActiveModContainer(modContainer);


ScriptProviderCustom providerCustom=new ScriptProviderCustom(modContainer.getModId());

FileHelper.findFiles(modContainer, "assets/" + modContainer.getModId() + "/crt",
(root, file) ->
{
Expand All @@ -73,18 +74,18 @@ public class RMLCrTLoader {
try{
byte[] fileBytes = FileHelper.getByteSource(file).read();

providerCustom.add(name,fileBytes);
providerCustom.add(name, fileBytes);

RMLFMLLoadingPlugin.Container.LOGGER.info("Injected {} for CrT",key);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
cachedScriptProvider.add(providerCustom);

Loader.instance().setActiveModContainer(RMLFMLLoadingPlugin.Container.INSTANCE);
}
}
cachedScriptProvider.add(providerCustom);
return cachedScriptProvider;
});

Expand Down
92 changes: 92 additions & 0 deletions src/main/java/mods/Hileb/rml/compat/crt/RMLScriptProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package mods.Hileb.rml.compat.crt;

import crafttweaker.runtime.IScriptIterator;
import crafttweaker.runtime.IScriptProvider;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Project ResourceModLoader
* @Author Hileb
* @Date 2024/3/16 20:49
**/
public class RMLScriptProvider implements IScriptProvider {
private final List<CustomScript> scripts;

public RMLScriptProvider() {
scripts = new ArrayList<>();
}

public void add(String name, byte[] content) {
scripts.add(new CustomScript(name, content));
}

@Override
public Iterator<IScriptIterator> getScripts() {
return scripts.stream().map(CustomScript::iterator).collect(Collectors.toSet()).iterator();
}

private static class CustomScript {

private final String name;
private final byte[] content;

public CustomScript(String name, byte[] content) {
this.name = name;
this.content = content;
}

public IScriptIterator iterator(){
return new ScriptIteratorSingle(name, content);
}
}

static class ScriptIteratorSingle implements IScriptIterator {

private final byte[] file;
private final String name;
private boolean first = true;

ScriptIteratorSingle(String name, byte[] file) {
this.file = file;
this.name = name;
}

@Override
public String getGroupName() {
return name;
}

@Override
public boolean next() {
if(first) {
first = false;
return true;
} else {
return false;
}
}

@Override
public String getName() {
return name;
}

@Override
public InputStream open() throws IOException {
return new BufferedInputStream(new ByteArrayInputStream(file));
}

@Override
public IScriptIterator copyCurrent() {
return new ScriptIteratorSingle(name, file);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/mods/Hileb/rml/core/RMLFMLLoadingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
public class RMLFMLLoadingPlugin implements IFMLLoadingPlugin {
public static File source;
public static boolean isDebug;
@PublicAPI public static final Logger LOGGER= LogManager.getLogger(ResourceModLoader.MODID);
@PublicAPI public static final Logger LOGGER = LogManager.getLogger(ResourceModLoader.MODID);

public RMLFMLLoadingPlugin(){
RMLBus.BUS.register(EventHandler.INSTANCE);
Expand Down Expand Up @@ -106,7 +106,7 @@ public String getAccessTransformerClass() {
@SuppressWarnings("unused")
public static class Container extends DummyModContainer{
@PublicAPI public static Container INSTANCE;
@PublicAPI public static final Logger LOGGER= LogManager.getLogger(ResourceModLoader.MODID);
@PublicAPI public static final Logger LOGGER = RMLFMLLoadingPlugin.LOGGER;
@PrivateAPI public Container(){
super(new ModMetadata());
ModMetadata metadata=this.getMetadata();
Expand Down
28 changes: 11 additions & 17 deletions src/main/java/mods/Hileb/rml/core/RMLTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public int apply(ClassNode cn) {
transformers.put("crafttweaker.mc1120.CraftTweaker",
(cn)->{
for(MethodNode mn:cn.methods){
/**
* @EventHandler
* public void onPreInitialization(FMLPreInitializationEvent ev) {
* CrTZenClassRegisterEvent.post();
* PROXY.registerEvents();
* **/
if ("onPreInitialization".equals(mn.name)){
InsnList hook = new InsnList();
hook.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "mods/Hileb/rml/compat/crt/CrTZenClassRegisterEvent", "post", "()V", false));
Expand All @@ -209,6 +215,11 @@ public int apply(ClassNode cn) {
transformers.put("net.minecraft.client.gui.GuiMainMenu",
(cn)->{
for(MethodNode mn:cn.methods){
/**
* try {
* List<String> list = MCMainScreenTextLoader.inject(Lists.newArrayList());
* iresource = Minecraft.getMinecraft().getResourceManager().getResource(SPLASH_TEXTS);
* **/
if ("<init>".equals(mn.name)){
ListIterator<AbstractInsnNode> iterator = mn.instructions.iterator();
AbstractInsnNode node;
Expand Down Expand Up @@ -240,23 +251,6 @@ public int apply(ClassNode cn) {
}
return -1;
});
transformers.put("com.teamacronymcoders.base.registrysystem.Registry",
(cn)->{
for(MethodNode mn:cn.methods){
if ("register".equals(mn.name)){
InsnList insnList = new InsnList();
insnList.add(new VarInsnNode(Opcodes.ALOAD, 0));
insnList.add(new FieldInsnNode(Opcodes.GETFIELD, "com/teamacronymcoders/base/registrysystem/Registry", "entries", "Ljava/util/Map;"));
insnList.add(new VarInsnNode(Opcodes.ALOAD, 1));
insnList.add(new VarInsnNode(Opcodes.ALOAD, 2));
insnList.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true));
insnList.add(new InsnNode(Opcodes.POP));
insnList.add(new InsnNode(Opcodes.RETURN));
mn.instructions = insnList;
}
}
return -1;
});
}
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
Expand Down

0 comments on commit c573fea

Please sign in to comment.