Skip to content

Commit

Permalink
Remove remapping stuff on Neo (runtime mojmap baby!)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxNeedsSnacks committed Dec 26, 2023
1 parent f572603 commit 27d6d29
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class RemappingHelper {
public static final Logger LOGGER = LoggerFactory.getLogger("Rhino Script Remapper");
private static final Map<String, Optional<Class<?>>> CLASS_CACHE = new HashMap<>();

private static final MinecraftRemapper NOP_REMAPPER = new MinecraftRemapper(Map.of(), Map.of());

private static Optional<Class<?>> loadClass(String name) {
return switch (name) {
case "void" -> Optional.of(Void.TYPE);
Expand Down Expand Up @@ -68,23 +70,29 @@ public static MinecraftRemapper getMinecraftRemapper(boolean debug) {
if (minecraftRemapper == null) {
LOGGER.info("Loading Rhino Minecraft remapper...");
long time = System.currentTimeMillis();
var configPath = RhinoProperties.getGameDir().resolve("config/mm.jsmappings");

if (Files.exists(configPath)) {
try (var in = new BufferedInputStream(new GZIPInputStream(Objects.requireNonNull(Files.newInputStream(configPath))))) {
minecraftRemapper = MinecraftRemapper.load(in, debug);
} catch (Exception ex) {
ex.printStackTrace();
LOGGER.error("Failed to load Rhino Minecraft remapper from config/mm.jsmappings!", ex);
minecraftRemapper = new MinecraftRemapper(Map.of(), Map.of());
}
// this is a botch, but i'm fine with it for now
if (!RhinoProperties.needsRemapping()) {
minecraftRemapper = NOP_REMAPPER;
} else {
try (var in = new BufferedInputStream(new GZIPInputStream(Objects.requireNonNull(RhinoProperties.openResource("mm.jsmappings"))))) {
minecraftRemapper = MinecraftRemapper.load(in, debug);
} catch (Exception ex) {
ex.printStackTrace();
LOGGER.error("Failed to load Rhino Minecraft remapper from mod jar!", ex);
minecraftRemapper = new MinecraftRemapper(Map.of(), Map.of());
var configPath = RhinoProperties.getGameDir().resolve("config/mm.jsmappings");

if (Files.exists(configPath)) {
try (var in = new BufferedInputStream(new GZIPInputStream(Objects.requireNonNull(Files.newInputStream(configPath))))) {
minecraftRemapper = MinecraftRemapper.load(in, debug);
} catch (Exception ex) {
ex.printStackTrace();
LOGGER.error("Failed to load Rhino Minecraft remapper from config/mm.jsmappings!", ex);
minecraftRemapper = new MinecraftRemapper(Map.of(), Map.of());
}
} else {
try (var in = new BufferedInputStream(new GZIPInputStream(Objects.requireNonNull(RhinoProperties.openResource("mm.jsmappings"))))) {
minecraftRemapper = MinecraftRemapper.load(in, debug);
} catch (Exception ex) {
ex.printStackTrace();
LOGGER.error("Failed to load Rhino Minecraft remapper from mod jar!", ex);
minecraftRemapper = NOP_REMAPPER;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static boolean isDev() {
throw new AssertionError();
}

@ExpectPlatform
public static boolean needsRemapping() {
throw new AssertionError();
}

@ExpectPlatform
public static InputStream openResource(String path) throws Exception {
throw new AssertionError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public static boolean isDev() {
return FabricLoader.getInstance().isDevelopmentEnvironment();
}

public static boolean needsRemapping() {
return true;
}

public static InputStream openResource(String path) throws Exception {
return Files.newInputStream(FabricLoader.getInstance().getModContainer("rhino").get().findPath(path).get());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,91 +1,9 @@
package dev.latvian.mods.rhino.mod.neoforge;

import dev.latvian.mods.rhino.mod.util.MojangMappings;
import dev.latvian.mods.rhino.mod.util.RemappingHelper;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.fml.loading.FMLLoader;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.regex.Pattern;

@Mod("rhino")
public class RhinoModForge {
public RhinoModForge(IEventBus bus) {
bus.register(RhinoModForge.class);
}

@SubscribeEvent
public static void loaded(FMLCommonSetupEvent event) {
if (RemappingHelper.GENERATE) {
RemappingHelper.run(FMLLoader.versionInfo().mcVersion(), RhinoModForge::generateMappings);
}
}

private static void generateMappings(RemappingHelper.MappingContext context) throws Exception {
MojangMappings.ClassDef current = null;

var srg = new ArrayList<String>();

try (var reader = new BufferedReader(RemappingHelper.createReader("https://raw.githubusercontent.com/MinecraftForge/MCPConfig/master/versions/release/" + context.mcVersion() + "/joined.tsrg"))) {
String line;

while ((line = reader.readLine()) != null) {
srg.add(line);
}
}

var pattern = Pattern.compile("[\t ]");

for (int i = 1; i < srg.size(); i++) {
var s = pattern.split(srg.get(i));

if (s.length < 3 || s[1].isEmpty()) {
continue;
}

if (!s[0].isEmpty()) {
s[0] = s[0].replace('/', '.');
current = context.mappings().getClass(s[0]);

if (current != null) {
RemappingHelper.LOGGER.info("- Checking class " + s[0] + " ; " + current.displayName);
} else {
RemappingHelper.LOGGER.info("- Skipping class " + s[0]);
}
} else if (current != null) {
if (s.length == 5) {
if (s[1].equals("<init>") || s[1].equals("<clinit>")) {
continue;
}

var sigs = s[2].substring(0, s[2].lastIndexOf(')') + 1).replace('/', '.');
var sig = new MojangMappings.NamedSignature(s[1], context.mappings().readSignatureFromDescriptor(sigs));
var m = current.members.get(sig);

if (m != null && !m.mmName().equals(s[3])) {
m.unmappedName().setValue(s[3]);
RemappingHelper.LOGGER.info("Remapped method " + s[3] + sigs + " to " + m.mmName());
} else if (m == null && !current.ignoredMembers.contains(sig)) {
RemappingHelper.LOGGER.info("Method " + s[3] + " [" + sig + "] not found!");
}
} else if (s.length == 4) {
var sig = new MojangMappings.NamedSignature(s[1], null);
var m = current.members.get(sig);

if (m != null) {
if (!m.mmName().equals(s[2])) {
m.unmappedName().setValue(s[2]);
RemappingHelper.LOGGER.info("Remapped field " + s[2] + " [" + m.rawName() + "] to " + m.mmName());
}
} else if (!current.ignoredMembers.contains(sig)) {
RemappingHelper.LOGGER.info("Field " + s[2] + " [" + s[1] + "] not found!");
}
}
}
}
public RhinoModForge() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static boolean isDev() {
return !FMLLoader.isProduction();
}

public static boolean needsRemapping() {
return false;
}

public static InputStream openResource(String path) throws Exception {
return Files.newInputStream(ModList.get().getModFileById("rhino").getFile().findResource(path));
}
Expand Down
Binary file removed neoforge/src/main/resources/mm.jsmappings
Binary file not shown.

0 comments on commit 27d6d29

Please sign in to comment.