Skip to content

Commit

Permalink
Revert "ServiceLoader is cool"
Browse files Browse the repository at this point in the history
This reverts commit 769de91.
  • Loading branch information
ZZZank committed Aug 6, 2024
1 parent 8e8353e commit ece1326
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CompilerEnvirons() {
this.strictMode = false;
this.warningAsError = false;
this.allowSharpComments = false;
this.optimizationLevel = RhinoProperties.get().optimizationLevel;
this.optimizationLevel = RhinoProperties.INSTANCE.optimizationLevel;
}

public void initFromContext(Context cx) {
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/dev/latvian/mods/rhino/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ protected Context(ContextFactory factory) {
}
this.factory = factory;
maximumInterpreterStackDepth = Integer.MAX_VALUE;
optimizationLevel = RhinoProperties.get().optimizationLevel;
optimizationLevel = RhinoProperties.INSTANCE.optimizationLevel;
remapper = null;
customProperties = new HashMap<>();
}
Expand Down Expand Up @@ -2116,7 +2116,7 @@ private ScriptNode parse(String sourceString,
}

private Evaluator createCompiler() {
if(!RhinoProperties.get().enableCompiler) {
if(!RhinoProperties.INSTANCE.enableCompiler) {
return createInterpreter();
}
Evaluator result = null;
Expand Down
34 changes: 18 additions & 16 deletions common/src/main/java/dev/latvian/mods/rhino/ScriptableObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
* This is the default implementation of the Scriptable interface. This
Expand Down Expand Up @@ -1219,9 +1223,8 @@ static <T extends Scriptable> BaseFunction buildClassCtor(Scriptable scope, Clas
&& parmTypes[0] == ScriptRuntime.ContextClass
&& parmTypes[1] == ScriptRuntime.ScriptableClass
&& parmTypes[2] == Boolean.TYPE
&& Modifier.isStatic(method.getModifiers())
) {
Object[] args = {Context.getContext(), scope, Boolean.valueOf(sealed)};
&& Modifier.isStatic(method.getModifiers())) {
Object[] args = {Context.getContext(), scope, sealed ? Boolean.TRUE : Boolean.FALSE};
method.invoke(null, args);
return null;
}
Expand Down Expand Up @@ -1374,7 +1377,7 @@ static <T extends Scriptable> BaseFunction buildClassCtor(Scriptable scope, Clas
names.add(propName);
name = propName;

if (annotation instanceof JSGetter || Objects.equals(prefix, getterPrefix)) {
if (annotation instanceof JSGetter || prefix == getterPrefix) {
if (!(proto instanceof ScriptableObject)) {
throw Context.reportRuntimeError2("msg.extend.scriptable", proto.getClass().toString(), name);
}
Expand Down Expand Up @@ -1450,20 +1453,19 @@ private static String getPropertyName(String methodName, String prefix, Annotati
String propName = null;
if (annotation instanceof JSGetter) {
propName = ((JSGetter) annotation).value();
if ((propName == null || propName.length() == 0)
&& methodName.length() > 3
&& methodName.startsWith("get")
) {
propName = methodName.substring(3);
if (Character.isUpperCase(propName.charAt(0))) {
if (propName.length() == 1) {
propName = propName.toLowerCase();
} else if (!Character.isUpperCase(propName.charAt(1))) {
propName = Character.toLowerCase(propName.charAt(0)) + propName.substring(1);
if (propName == null || propName.length() == 0) {
if (methodName.length() > 3 && methodName.startsWith("get")) {
propName = methodName.substring(3);
if (Character.isUpperCase(propName.charAt(0))) {
if (propName.length() == 1) {
propName = propName.toLowerCase();
} else if (!Character.isUpperCase(propName.charAt(1))) {
propName = Character.toLowerCase(propName.charAt(0)) + propName.substring(1);
}
}
}
}
} else if (annotation instanceof JSFunction jsFn) {
} else if (annotation instanceof JSFunction) {
propName = ((JSFunction) annotation).value();
} else if (annotation instanceof JSStaticFunction) {
propName = ((JSStaticFunction) annotation).value();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dev.latvian.mods.rhino.mod;

import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.latvian.mods.rhino.mod.remapper.MappingIO;
import dev.latvian.mods.rhino.util.Lazy;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.io.InputStream;
Expand All @@ -11,30 +12,42 @@
import java.nio.file.Path;
import java.util.Properties;

public abstract class RhinoProperties {
/**
* @see dev.latvian.mods.rhino.mod.forge.RhinoPropertiesImpl
* @see dev.latvian.mods.rhino.mod.fabric.RhinoPropertiesImpl
*/
public class RhinoProperties {

private static final Lazy<RhinoProperties> implementation = Lazy.serviceLoader(RhinoProperties.class);

public static RhinoProperties get() {
return implementation.get();
}
public static final RhinoProperties INSTANCE = new RhinoProperties();

public boolean generateMapping;
public boolean enableCompiler;
public int optimizationLevel;

public abstract Path getGameDir();
@ExpectPlatform
@Contract(value = " -> _", pure = true)
public static Path getGameDir() {
throw new AssertionError();
}

public abstract boolean isDev();
@ExpectPlatform
@Contract(value = " -> _", pure = true)
public static boolean isDev() {
throw new AssertionError();
}

@ExpectPlatform
@NotNull
public abstract InputStream openResource(String path) throws Exception;
@Contract(value = " -> _", pure = true)
public static InputStream openResource(String path) throws Exception {
throw new AssertionError();
}

private final Properties properties;
// public boolean forceLocalMappings;
private boolean writeProperties;

public RhinoProperties() {
RhinoProperties() {
this.properties = new Properties();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ private RhizoRemapper() {
}

private static InputStream locateMappingFile() {
val cfgPath = RhinoProperties.get().getGameDir().resolve("config/" + RhizoMappingGen.MAPPING_FILENAME);
val cfgPath = RhinoProperties.getGameDir().resolve("config/" + RhizoMappingGen.MAPPING_FILENAME);
try {
if (Files.exists(cfgPath)) {
MappingIO.LOGGER.info("Found Rhizo mapping file from config/{}.", RhizoMappingGen.MAPPING_FILENAME);
return new GZIPInputStream(Files.newInputStream(cfgPath));
}
val in = new GZIPInputStream(RhinoProperties.get().openResource(RhizoMappingGen.MAPPING_FILENAME));
val in = new GZIPInputStream(RhinoProperties.openResource(RhizoMappingGen.MAPPING_FILENAME));
MappingIO.LOGGER.info("Found Rhizo mapping file from Rhizo mod jar.");
return in;
} catch (Exception e) {
Expand Down
58 changes: 0 additions & 58 deletions common/src/main/java/dev/latvian/mods/rhino/util/Lazy.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.github.bsideup.jabel.Desugar;
import dev.latvian.mods.rhino.mod.RhinoProperties;
import dev.latvian.mods.rhino.mod.remapper.RhizoMappingGen;
import dev.latvian.mods.rhino.mod.remapper.RhizoRemapper;
import dev.latvian.mods.rhino.mod.remapper.info.Clazz;
import dev.latvian.mods.rhino.util.remapper.AnnotatedRemapper;
import dev.latvian.mods.rhino.util.remapper.DualRemapper;
import dev.latvian.mods.rhino.util.remapper.RemapperManager;
import lombok.val;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.impl.launch.FabricLauncherBase;
Expand All @@ -19,7 +23,7 @@ public class RhinoModFabric implements ModInitializer {

@Override
public void onInitialize() {
if (RhinoProperties.get().generateMapping) {
if (RhinoProperties.INSTANCE.generateMapping) {
RhizoMappingGen.generate(
"1.16.5",
new RenameOnlyMappingLoader(loadNativeMappingClassMap())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.latvian.mods.rhino.mod.fabric;

import dev.latvian.mods.rhino.mod.RhinoProperties;
import net.fabricmc.loader.api.FabricLoader;
import org.jetbrains.annotations.NotNull;

Expand All @@ -11,17 +10,17 @@
/**
* @see dev.latvian.mods.rhino.mod.RhinoProperties
*/
public class RhinoPropertiesImpl extends RhinoProperties {
public Path getGameDir() {
public class RhinoPropertiesImpl {
public static Path getGameDir() {
return FabricLoader.getInstance().getGameDir();
}

public boolean isDev() {
public static boolean isDev() {
return FabricLoader.getInstance().isDevelopmentEnvironment();
}

@NotNull
public InputStream openResource(String path) throws Exception {
public static InputStream openResource(String path) throws Exception {
return Files.newInputStream(FabricLoader.getInstance().getModContainer("rhino").get().findPath(path).get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public RhinoModForge() {

@SubscribeEvent
public static void onCommonSetup(FMLCommonSetupEvent event) {
if (RhinoProperties.get().generateMapping) {
if (RhinoProperties.INSTANCE.generateMapping) {
Thread t = new Thread(() -> RhizoMappingGen.generate(
"1.16.5",
(mcVersion, vanillaMapping) -> IMappingFile.load(MappingIO.getUrlConnection(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.latvian.mods.rhino.mod.forge;

import dev.latvian.mods.rhino.mod.RhinoProperties;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLLoader;
import org.jetbrains.annotations.NotNull;
Expand All @@ -12,17 +11,17 @@
/**
* @see dev.latvian.mods.rhino.mod.RhinoProperties
*/
public class RhinoPropertiesImpl extends RhinoProperties {
public Path getGameDir() {
public class RhinoPropertiesImpl {
public static Path getGameDir() {
return FMLLoader.getGamePath();
}

public boolean isDev() {
public static boolean isDev() {
return !FMLLoader.isProduction();
}

@NotNull
public InputStream openResource(String path) throws Exception {
public static InputStream openResource(String path) throws Exception {
return Files.newInputStream(ModList.get().getModFileById("rhino").getFile().findResource(path));
}
}

0 comments on commit ece1326

Please sign in to comment.