Skip to content

Commit

Permalink
- Goodbye Access Checks if unsafeMode is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Vbitz committed Feb 13, 2013
1 parent 813f8f0 commit 7986a85
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ Version 1.8.0
- Unified Tick Handlers
- Added killAll() which kills tnt and arrows and dropped items, basicly goodbye massive lag

Version 1.9.0
- Goodbye Access Checks if unsafeMode is enabled

Future - Roughly in order of when I will work on them
=====================================================
- Add Block API in world working on a collection of blocks
Expand Down
6 changes: 0 additions & 6 deletions Documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,6 @@ void fmlMods()

Object fmlMod(String modID)

void call(Object obj, String methodName, Object... args)

Object getField(Object obj, String name)

void setField(Object obj, String name, Object value)

Object getTileEntity(Vector3f pos)

Object getBlock(Vector3f pos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
public class MinecraftScriptContextFactory extends ContextFactory {
private static class WatchedContext extends Context {
public long startTime;

@Override
public boolean hasFeature(int featureIndex) {
if (featureIndex == FEATURE_ENHANCED_JAVA_ACCESS && MinecraftScriptMod.getUnsafeEnabled()) {
return true;
}
return super.hasFeature(featureIndex);
}
}

public static void setup() {
Expand All @@ -20,6 +28,7 @@ protected Context makeContext()
// Make Rhino runtime to call observeInstructionCount
// each 2000 bytecode instructions
cx.setInstructionObserverThreshold(2000);
cx.setWrapFactory(new MinecraftScriptWrapFactory());
return cx;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.vbitz.MinecraftScript;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;

public class MinecraftScriptJavaObject extends NativeJavaObject {

private static Object recuriveGetItem(Class<?> cls, String name) {
for (Method item : cls.getDeclaredMethods()) {
if (item.getName().equals(name)) {
return item;
}
}
for (Field item : cls.getDeclaredFields()) {
if (item.getName().equals(name)) {
return item;
}
}
if (cls.getSuperclass() != null) {
return recuriveGetItem(cls.getSuperclass(), name);
} else {
return null;
}
}

public MinecraftScriptJavaObject(Scriptable scope, Object javaObject,
Class<?> staticType) {
super(scope, javaObject, staticType);
}

@Override
public Object get(String name, Scriptable thisObject) {
if (name.equals("getClass") && !MinecraftScriptMod.getUnsafeEnabled()) {
return NOT_FOUND;
}

if (!MinecraftScriptMod.getUnsafeEnabled()) {
return super.get(name, thisObject);
}

Object reflectItem = recuriveGetItem(unwrap().getClass(), name);

if (reflectItem == null) {
return NOT_FOUND;
}

if (reflectItem instanceof Method) {
if (!((Method) reflectItem).isAccessible()) {
System.out.println("private call");
((Method) reflectItem).setAccessible(true);
}
} else if (reflectItem instanceof Field) {
if (!((Field) reflectItem).isAccessible()) {
System.out.println("private call");
((Field) reflectItem).setAccessible(true);
}
}

return super.get(name, thisObject);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import cpw.mods.fml.common.registry.TickRegistry;
import cpw.mods.fml.relauncher.Side;

@Mod(modid="MinecraftScript", name="MinecraftScript", version="1.8.0") // mental note, update this loads
@Mod(modid="MinecraftScript", name="MinecraftScript", version="1.9.0") // mental note, update this loads
@NetworkMod(clientSideRequired=false, serverSideRequired=true)
public class MinecraftScriptMod {
@Instance("MinecraftScriptMod")
Expand Down Expand Up @@ -148,7 +148,7 @@ public void load(FMLInitializationEvent e) {

this.mcLogger.setParent(FMLLog.getLogger());

this.mcLogger.info("MinecraftScript Version " + "1.8.0" + " Loading");
this.mcLogger.info("MinecraftScript Version " + "1.9.0" + " Loading");

createScriptedObjects();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public static Object fmlMod(String modID) {
return null;
}

/*
public static void call(Object obj, String methodName, Object... args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ScriptErrorException {
for (Method method : obj.getClass().getDeclaredMethods()) {
if (method.getName().equals(methodName)) {
Expand Down Expand Up @@ -211,6 +213,8 @@ public static void setField(Object obj, String name, Object value) throws Securi
f.set(obj, getRealObject(value, f.getType()));
}
*/

public static Object getTileEntity(Vector3f pos) {
return JSScriptingManager.getInstance().getScriptRunner().getWorld().getBlockTileEntity((int) pos.getX(), (int) pos.getY(), (int) pos.getZ());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.vbitz.MinecraftScript;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.WrapFactory;

public class MinecraftScriptWrapFactory extends WrapFactory {
@Override
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
Object javaObject, Class<?> staticType) {
return new MinecraftScriptJavaObject(scope, javaObject, staticType);
}
}

0 comments on commit 7986a85

Please sign in to comment.