Skip to content

Commit

Permalink
Merge pull request #96 from anatawa12/use-nashorn
Browse files Browse the repository at this point in the history
add new way to use nashorn
  • Loading branch information
anatawa12 authored Jun 20, 2020
2 parents 92d55e7 + ad595cf commit 464a0a3
Show file tree
Hide file tree
Showing 20 changed files with 599 additions and 222 deletions.
36 changes: 18 additions & 18 deletions patches/rtm.patch
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ index 0a49c17..d210f43 100644
.stack use locals
.end stack
diff --git a/mods/rtm.deobf.jar.src.processed/jp/ngt/rtm/modelpack/modelset/ModelSetBase.jasm b/src/main/rtm/jp/ngt/rtm/modelpack/modelset/ModelSetBase.jasm
index c1621a2..bfa23df 100644
index c1621a2..ef1f288 100644
--- a/mods/rtm.deobf.jar.src.processed/jp/ngt/rtm/modelpack/modelset/ModelSetBase.jasm
+++ b/src/main/rtm/jp/ngt/rtm/modelpack/modelset/ModelSetBase.jasm
@@ -80,22 +80,35 @@ L_0000:
Expand All @@ -910,18 +910,18 @@ index c1621a2..bfa23df 100644
checkcast jp/ngt/rtm/modelpack/cfg/ModelConfig
getfield jp/ngt/rtm/modelpack/cfg/ModelConfig/serverScriptPath Ljava/lang/String;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifDisabled (Ljava/lang/String;)V
+
invokevirtual jp/ngt/rtm/modelpack/ModelPackManager/getScript (Ljava/lang/String;)Ljava/lang/String;
invokestatic jp/ngt/ngtlib/io/ScriptUtil/doScript (Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifEnabled (Ljava/lang/String;)V
+
+ invokestatic com/anatawa12/fixRtm/scripting/FIXScriptUtil/getScriptAndDoScript (Ljp/ngt/rtm/modelpack/ModelPackManager;Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/whatever (Ljava/lang/String;)V
+
putfield jp/ngt/rtm/modelpack/modelset/ModelSetBase/serverSE Ljavax/script/ScriptEngine;
Expand All @@ -946,18 +946,18 @@ index c1621a2..bfa23df 100644
checkcast jp/ngt/rtm/modelpack/cfg/ModelConfig
getfield jp/ngt/rtm/modelpack/cfg/ModelConfig/serverScriptPath Ljava/lang/String;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifDisabled (Ljava/lang/String;)V
+
invokevirtual jp/ngt/rtm/modelpack/ModelPackManager/getScript (Ljava/lang/String;)Ljava/lang/String;
invokestatic jp/ngt/ngtlib/io/ScriptUtil/doScript (Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifEnabled (Ljava/lang/String;)V
+
+ invokestatic com/anatawa12/fixRtm/scripting/FIXScriptUtil/getScriptAndDoScript (Ljp/ngt/rtm/modelpack/ModelPackManager;Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/whatever (Ljava/lang/String;)V
+
putfield jp/ngt/rtm/modelpack/modelset/ModelSetBase/serverSE Ljavax/script/ScriptEngine;
Expand All @@ -980,18 +980,18 @@ index c1621a2..bfa23df 100644
checkcast jp/ngt/rtm/modelpack/cfg/ModelConfig
getfield jp/ngt/rtm/modelpack/cfg/ModelConfig/guiScriptPath Ljava/lang/String;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifDisabled (Ljava/lang/String;)V
+
invokevirtual jp/ngt/rtm/modelpack/ModelPackManager/getScript (Ljava/lang/String;)Ljava/lang/String;
invokestatic jp/ngt/ngtlib/io/ScriptUtil/doScript (Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifEnabled (Ljava/lang/String;)V
+
+ invokestatic com/anatawa12/fixRtm/scripting/FIXScriptUtil/getScriptAndDoScript (Ljp/ngt/rtm/modelpack/ModelPackManager;Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/whatever (Ljava/lang/String;)V
+
putfield jp/ngt/rtm/modelpack/modelset/ModelSetBase/guiSE Ljavax/script/ScriptEngine;
Expand All @@ -1005,7 +1005,7 @@ index c1621a2..bfa23df 100644
getfield jp/ngt/rtm/modelpack/cfg/ModelConfig/guiTexture Ljava/lang/String;
invokevirtual jp/ngt/rtm/modelpack/ModelPackManager/getResource (Ljava/lang/String;)Lnet/minecraft/util/ResourceLocation;
diff --git a/mods/rtm.deobf.jar.src.processed/jp/ngt/rtm/modelpack/modelset/ModelSetVehicleBase.jasm b/src/main/rtm/jp/ngt/rtm/modelpack/modelset/ModelSetVehicleBase.jasm
index e05f400..c53b685 100644
index e05f400..1418cc5 100644
--- a/mods/rtm.deobf.jar.src.processed/jp/ngt/rtm/modelpack/modelset/ModelSetVehicleBase.jasm
+++ b/src/main/rtm/jp/ngt/rtm/modelpack/modelset/ModelSetVehicleBase.jasm
@@ -192,22 +192,35 @@ L_0068:
Expand All @@ -1020,18 +1020,18 @@ index e05f400..c53b685 100644
invokevirtual jp/ngt/rtm/modelpack/modelset/ModelSetVehicleBase/getConfig ()Ljp/ngt/rtm/modelpack/cfg/VehicleBaseConfig;
getfield jp/ngt/rtm/modelpack/cfg/VehicleBaseConfig/soundScriptPath Ljava/lang/String;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifDisabled (Ljava/lang/String;)V
+
invokevirtual jp/ngt/rtm/modelpack/ModelPackManager/getScript (Ljava/lang/String;)Ljava/lang/String;
invokestatic jp/ngt/ngtlib/io/ScriptUtil/doScript (Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifEnabled (Ljava/lang/String;)V
+
+ invokestatic com/anatawa12/fixRtm/scripting/FIXScriptUtil/getScriptAndDoScript (Ljp/ngt/rtm/modelpack/ModelPackManager;Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/whatever (Ljava/lang/String;)V
+
putfield jp/ngt/rtm/modelpack/modelset/ModelSetVehicleBase/soundSE Ljavax/script/ScriptEngine;
Expand Down Expand Up @@ -1392,7 +1392,7 @@ index 283bb01..efd8f8d 100644
invokespecial java/lang/RuntimeException/<init> (Ljava/lang/String;Ljava/lang/Throwable;)V
athrow
diff --git a/mods/rtm.deobf.jar.src.processed/jp/ngt/rtm/render/RTMRenderers.jasm b/src/main/rtm/jp/ngt/rtm/render/RTMRenderers.jasm
index 7cd3941..49f40d0 100644
index 7cd3941..be222d2 100644
--- a/mods/rtm.deobf.jar.src.processed/jp/ngt/rtm/render/RTMRenderers.jasm
+++ b/src/main/rtm/jp/ngt/rtm/render/RTMRenderers.jasm
@@ -20,30 +20,48 @@ L_0005:
Expand All @@ -1407,7 +1407,7 @@ index 7cd3941..49f40d0 100644
.var 0 is par1 Lnet/minecraft/util/ResourceLocation; from L_0000 to L_0029
.var 1 is args "[Ljava/lang/String;" from L_0000 to L_0029
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifDisabled (Ljava/lang/String;)V
+
getstatic jp/ngt/rtm/modelpack/ModelPackManager/INSTANCE Ljp/ngt/rtm/modelpack/ModelPackManager;
Expand All @@ -1421,7 +1421,7 @@ index 7cd3941..49f40d0 100644
aload 2
invokestatic jp/ngt/ngtlib/io/ScriptUtil/doScript (Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/ifEnabled (Ljava/lang/String;)V
+
+ aconst_null
Expand All @@ -1431,7 +1431,7 @@ index 7cd3941..49f40d0 100644
+ invokevirtual net/minecraft/util/ResourceLocation/toString ()Ljava/lang/String;
+ invokestatic com/anatawa12/fixRtm/scripting/FIXScriptUtil/getScriptAndDoScript (Ljava/lang/String;)Ljavax/script/ScriptEngine;
+
+ ldc "cachedScripts"
+ ldc "useOurScripting"
+ invokestatic com/anatawa12/fixRtm/asm/Preprocessor/whatever (Ljava/lang/String;)V
+
astore 3
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/com/anatawa12/fixRtm/FixRtm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import com.anatawa12.fixRtm.asm.config.MainConfig
import com.anatawa12.fixRtm.asm.config.MainConfig.changeTestTrainTextureEnabled
import com.anatawa12.fixRtm.io.FIXFileLoader
import com.anatawa12.fixRtm.network.NetworkHandler
import com.anatawa12.fixRtm.ngtlib.renderer.model.CachedPolygonModel
import com.anatawa12.fixRtm.rtm.modelpack.modelset.dummies.*
import com.anatawa12.fixRtm.scripting.ExecutedScriptCache
import com.anatawa12.fixRtm.scripting.PrimitiveJavaHelper
import com.anatawa12.fixRtm.scripting.RhinoHooks
import com.anatawa12.fixRtm.scripting.loadFIXScriptUtil
import com.anatawa12.fixRtm.scripting.nashorn.CompiledImportedScriptCache
import com.anatawa12.fixRtm.scripting.rhino.ExecutedScriptCache
import com.anatawa12.fixRtm.scripting.rhino.PrimitiveJavaHelper
import com.anatawa12.fixRtm.scripting.rhino.RhinoHooks
import jp.ngt.ngtlib.NGTCore
import jp.ngt.rtm.RTMCore
import net.minecraft.block.Block
import net.minecraft.client.Minecraft
import net.minecraft.client.resources.IReloadableResourceManager
import net.minecraft.item.Item
import net.minecraft.launchwrapper.Launch
import net.minecraft.util.ResourceLocation
import net.minecraftforge.client.event.ModelRegistryEvent
import net.minecraftforge.common.MinecraftForge
Expand Down Expand Up @@ -46,12 +47,21 @@ object FixRtm {
@Mod.EventHandler
fun construct(e: FMLConstructionEvent) {
NativeJavaObject.canConvert(0, Object::class.java)// load
RhinoHooks.load()// load
FIXFileLoader.load() // init
if (MainConfig.cachedScripts) {
ExecutedScriptCache.load()// init
loadFIXScriptUtil()// init
PrimitiveJavaHelper.load()// init
when (MainConfig.scriptingMode) {
MainConfig.ScriptingMode.CacheWithRhino -> {
loadFIXScriptUtil()// init
ExecutedScriptCache.load()// init
PrimitiveJavaHelper.load()// init
RhinoHooks.load()// load
}
MainConfig.ScriptingMode.BetterWithNashorn -> {
Launch.classLoader.addClassLoaderExclusion("jdk.nashorn.")
CompiledImportedScriptCache.load() // load
}
MainConfig.ScriptingMode.UseRtmNormal -> {
// nop
}
}
if (e.side == Side.CLIENT) registerGenerators()
}
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/com/anatawa12/fixRtm/asm/RhinoTransformer.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.anatawa12.fixRtm.asm

import com.anatawa12.fixRtm.scripting.CodeGenHooks.ClassCompiler_compileToClassFiles_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.ClassCompiler_compileToClassFiles_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.ClassCompiler_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.CodeGenHooks_implDecompile_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.CodeGenHooks_implDecompile_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.CodeGenHooks_internal
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_generateNativeFunctionOverrides_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_generateNativeFunctionOverrides_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_internal
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_scriptOrFnNodes_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_scriptOrFnNodes_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_sourceString_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_sourceString_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.NativeFunction_decompile_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.NativeFunction_decompile_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.NativeFunction_name
import com.anatawa12.fixRtm.scripting.PrimitiveJavaHelper
import com.anatawa12.fixRtm.scripting.RhinoHooks
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.ClassCompiler_compileToClassFiles_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.ClassCompiler_compileToClassFiles_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.ClassCompiler_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.CodeGenHooks_implDecompile_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.CodeGenHooks_implDecompile_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.CodeGenHooks_internal
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_generateNativeFunctionOverrides_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_generateNativeFunctionOverrides_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_internal
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_scriptOrFnNodes_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_scriptOrFnNodes_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_sourceString_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_sourceString_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.NativeFunction_decompile_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.NativeFunction_decompile_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.NativeFunction_name
import com.anatawa12.fixRtm.scripting.rhino.PrimitiveJavaHelper
import com.anatawa12.fixRtm.scripting.rhino.RhinoHooks
import net.minecraft.launchwrapper.IClassTransformer
import org.objectweb.asm.*
import org.objectweb.asm.Opcodes.*
Expand Down Expand Up @@ -346,7 +346,7 @@ class RhinoTransformer : IClassTransformer {
companion object {
const val NativeJavaObject_name = "org.mozilla.javascript.NativeJavaObject"
const val NativeJavaObject_internal = "org/mozilla/javascript/NativeJavaObject"
const val CoerceTypeImplFailed_internal = "com/anatawa12/fixRtm/scripting/CoerceTypeImplFailed"
const val CoerceTypeImplFailed_internal = "com/anatawa12/fixRtm/scripting/rhino/CoerceTypeImplFailed"
const val IdScriptableObject_internal = "org/mozilla/javascript/IdScriptableObject"
const val Scriptable_internal = "org/mozilla/javascript/Scriptable"
const val Scriptable_NOT_FOUND_name = "NOT_FOUND"
Expand Down
58 changes: 54 additions & 4 deletions src/main/java/com/anatawa12/fixRtm/asm/config/MainConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.anatawa12.fixRtm.asm.config

import com.anatawa12.fixRtm.Loggers
import net.minecraftforge.common.config.Config
import net.minecraftforge.common.config.Configuration
import net.minecraftforge.fml.common.FMLLog
import net.minecraftforge.fml.common.Loader

object MainConfig {
Expand All @@ -23,11 +26,41 @@ object MainConfig {
true,
"caches obj, mqo model.")

private val scriptingModeStr = config.getString(
"scriptingMode", categoryModelLoading,
"use-default",
"scripting mode. the value is one of the list below:\n" +
"cache-with-rhino : the fastest mode but not stable. some script may make error.\n" +
"better-with-nashorn : same runtime as RTM but a little faster than RTM.\n" +
"use-rtm-normal : same as RTM. this is the slowest mode.\n" +
"use-default : use default mode.\n")

val scriptingMode: ScriptingMode

init {
val cachedScriptsEnabled = config.getCategory(categoryModelLoading).remove("cachedScriptsEnabled")
if (cachedScriptsEnabled != null) { // cachedScriptsEnabledがある
val categoryModelLoadingProp = config.getCategory(categoryModelLoading).get("scriptingMode")!!
if (categoryModelLoadingProp.string.toLowerCase() == ScriptingMode.defaultConfigValue) { // scriptingModeがuse default
if (!cachedScriptsEnabled.boolean) { // cached scriptがdisable
categoryModelLoadingProp.setValue(ScriptingMode.BetterWithNashorn.configValue)
}
}
}
var scriptingMode = ScriptingMode.getByConfigValue(scriptingModeStr.toLowerCase())
if (scriptingMode == null) {
if (scriptingModeStr.toLowerCase() == ScriptingMode.defaultConfigValue) {
scriptingMode = ScriptingMode.default
} else {
Loggers.getLogger("Config").fatal("your scriptingMode is not valid so we use default.")
scriptingMode = ScriptingMode.default
}
}
this.scriptingMode = scriptingMode
}

@JvmField
val cachedScripts = config.getBoolean(
"cachedScriptsEnabled", categoryModelLoading,
true,
"caches compiled script and executed environment")
val useOurScripting = scriptingMode != ScriptingMode.UseRtmNormal

@JvmField
val reduceConstructModelLog = config.getBoolean(
Expand Down Expand Up @@ -70,4 +103,21 @@ object MainConfig {
config.save()
}
}

enum class ScriptingMode(val configValue: String) {
CacheWithRhino("cache-with-rhino"),
BetterWithNashorn("better-with-nashorn"),
UseRtmNormal("use-rtm-normal"),
;

companion object {
private val byConfigValue = values().associateBy { it.configValue }

fun getByConfigValue(value: String) = byConfigValue[value]

val default = CacheWithRhino

const val defaultConfigValue = "use-default"
}
}
}
Loading

0 comments on commit 464a0a3

Please sign in to comment.