fix require() for json file;fix loadJar() not working; add loadDex(); add image.readPixels(); add util.java
This commit is contained in:
@@ -78,6 +78,9 @@ module = (typeof module === 'undefined') ? {} : module;
|
|||||||
if(builtInModules.indexOf(normalizePath) >= 0 && !files.exists(normalizePath)){
|
if(builtInModules.indexOf(normalizePath) >= 0 && !files.exists(normalizePath)){
|
||||||
return NativeRequire.require(normalizePath);
|
return NativeRequire.require(normalizePath);
|
||||||
}
|
}
|
||||||
|
if(id === "events"){
|
||||||
|
return events;
|
||||||
|
}
|
||||||
if(id.startsWith("http://") || id.startsWith("https://")){
|
if(id.startsWith("http://") || id.startsWith("https://")){
|
||||||
return NativeRequire.require(id);
|
return NativeRequire.require(id);
|
||||||
}
|
}
|
||||||
@@ -252,6 +255,9 @@ module = (typeof module === 'undefined') ? {} : module;
|
|||||||
}
|
}
|
||||||
|
|
||||||
function normalizeName (fileName, ext) {
|
function normalizeName (fileName, ext) {
|
||||||
|
if(fileName.endsWith('.json')){
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
var extension = ext || '.js';
|
var extension = ext || '.js';
|
||||||
if (fileName.endsWith(extension)) {
|
if (fileName.endsWith(extension)) {
|
||||||
return fileName;
|
return fileName;
|
||||||
|
|||||||
@@ -169,7 +169,23 @@ module.exports = function(__runtime__, scope){
|
|||||||
format = format || "png";
|
format = format || "png";
|
||||||
quality = quality == undefined ? 100 : quality;
|
quality = quality == undefined ? 100 : quality;
|
||||||
return rtImages.toBytes(img, format, quality);
|
return rtImages.toBytes(img, format, quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
images.readPixels = function(path){
|
||||||
|
var img = images.read(path);
|
||||||
|
var bitmap = img.getBitmap();
|
||||||
|
var w = bitmap.getWidth();
|
||||||
|
var h = bitmap.getHeight();
|
||||||
|
var pixels = util.java.array("int", w * h);
|
||||||
|
bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
|
||||||
|
img.recycle();
|
||||||
|
return {
|
||||||
|
data: pixels,
|
||||||
|
width: w,
|
||||||
|
height: h
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function getColorDetector(color, algorithm, threshold){
|
function getColorDetector(color, algorithm, threshold){
|
||||||
switch(algorithm){
|
switch(algorithm){
|
||||||
|
|||||||
@@ -4,6 +4,32 @@ J.instanceOf = function(obj, clazz){
|
|||||||
return java.lang.Class.forName(clazz).isAssignableFrom(obj.getClass());
|
return java.lang.Class.forName(clazz).isAssignableFrom(obj.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function typeToClass(type) {
|
||||||
|
if (typeof(type) != 'string') {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
var types = {
|
||||||
|
"int": "Integer",
|
||||||
|
"long": "Long",
|
||||||
|
"string": "String",
|
||||||
|
"double": "Double",
|
||||||
|
"char": "Character",
|
||||||
|
"byte": "Byte",
|
||||||
|
"float": "Float"
|
||||||
|
};
|
||||||
|
if (types[type]) {
|
||||||
|
return Packages["java.lang." + types[type]].TYPE;
|
||||||
|
}
|
||||||
|
return Packages[type];
|
||||||
|
}
|
||||||
|
|
||||||
|
function array(type) {
|
||||||
|
var clazz = typeToClass(type);
|
||||||
|
var args = arguments;
|
||||||
|
args[0] = clazz;
|
||||||
|
return java.lang.reflect.Array.newInstance.apply(null, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
J.array = array;
|
||||||
|
|
||||||
module.exports = J;
|
module.exports = J;
|
||||||
@@ -3,7 +3,7 @@ module.exports = function (runtime, global) {
|
|||||||
require("object-observe-lite.min")();
|
require("object-observe-lite.min")();
|
||||||
require("array-observe.min")();
|
require("array-observe.min")();
|
||||||
|
|
||||||
var J = require("__java_util__");
|
var J = util.java;
|
||||||
|
|
||||||
|
|
||||||
var ui = {};
|
var ui = {};
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
var formatRegExp = /%[sdj%]/g;
|
var formatRegExp = /%[sdj%]/g;
|
||||||
|
exports.java = require("__java_util__");
|
||||||
exports.format = function(f) {
|
exports.format = function(f) {
|
||||||
if (!isString(f)) {
|
if (!isString(f)) {
|
||||||
var objects = [];
|
var objects = [];
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import dalvik.system.DexClassLoader;
|
||||||
import dalvik.system.DexFile;
|
import dalvik.system.DexFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,10 +26,11 @@ public class AndroidClassLoader extends ClassLoader implements GeneratedClassLoa
|
|||||||
|
|
||||||
|
|
||||||
private final ClassLoader parent;
|
private final ClassLoader parent;
|
||||||
private List<DexFile> dx;
|
private List<DexClassLoader> dx;
|
||||||
private final File dexFile;
|
private final File dexFile;
|
||||||
private final File odexOatFile;
|
private final File odexOatFile;
|
||||||
private final File classFile;
|
private final File classFile;
|
||||||
|
private final File mCacheDir;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance with the given parent classloader and cache dierctory
|
* Create a new instance with the given parent classloader and cache dierctory
|
||||||
@@ -38,6 +40,7 @@ public class AndroidClassLoader extends ClassLoader implements GeneratedClassLoa
|
|||||||
*/
|
*/
|
||||||
public AndroidClassLoader(ClassLoader parent, File dir) {
|
public AndroidClassLoader(ClassLoader parent, File dir) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
mCacheDir = dir;
|
||||||
dx = new ArrayList<>();
|
dx = new ArrayList<>();
|
||||||
dexFile = new File(dir, "dex-" + hashCode() + ".jar");
|
dexFile = new File(dir, "dex-" + hashCode() + ".jar");
|
||||||
odexOatFile = new File(dir, "odex_oat-" + hashCode() + ".tmp");
|
odexOatFile = new File(dir, "odex_oat-" + hashCode() + ".tmp");
|
||||||
@@ -56,8 +59,8 @@ public class AndroidClassLoader extends ClassLoader implements GeneratedClassLoa
|
|||||||
parameters.setFileNameInZip(name.replace('.', '/') + ".class");
|
parameters.setFileNameInZip(name.replace('.', '/') + ".class");
|
||||||
parameters.setSourceExternalStream(true);
|
parameters.setSourceExternalStream(true);
|
||||||
zipFile.addStream(new ByteArrayInputStream(data), parameters);
|
zipFile.addStream(new ByteArrayInputStream(data), parameters);
|
||||||
return dexJar().loadClass(name, parent);
|
return dexJar().loadClass(name);
|
||||||
} catch (IOException | ZipException e) {
|
} catch (IOException | ZipException | ClassNotFoundException e) {
|
||||||
throw new FatalLoadingException(e);
|
throw new FatalLoadingException(e);
|
||||||
} finally {
|
} finally {
|
||||||
dexFile.delete();
|
dexFile.delete();
|
||||||
@@ -87,7 +90,13 @@ public class AndroidClassLoader extends ClassLoader implements GeneratedClassLoa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private DexFile dexJar() throws IOException {
|
public DexClassLoader loadDex(File file) throws IOException {
|
||||||
|
DexClassLoader loader = new DexClassLoader(file.getPath(), mCacheDir.getPath(), null, parent);
|
||||||
|
dx.add(loader);
|
||||||
|
return loader;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DexClassLoader dexJar() throws IOException {
|
||||||
if (!classFile.exists()) {
|
if (!classFile.exists()) {
|
||||||
classFile.createNewFile();
|
classFile.createNewFile();
|
||||||
}
|
}
|
||||||
@@ -96,9 +105,7 @@ public class AndroidClassLoader extends ClassLoader implements GeneratedClassLoa
|
|||||||
arguments.outName = dexFile.getPath();
|
arguments.outName = dexFile.getPath();
|
||||||
arguments.jarOutput = true;
|
arguments.jarOutput = true;
|
||||||
Main.run(arguments);
|
Main.run(arguments);
|
||||||
DexFile dex = DexFile.loadDex(dexFile.getPath(), odexOatFile.getPath(), 0);
|
return loadDex(dexFile);
|
||||||
dx.add(dex);
|
|
||||||
return dex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -124,8 +131,8 @@ public class AndroidClassLoader extends ClassLoader implements GeneratedClassLoa
|
|||||||
throws ClassNotFoundException {
|
throws ClassNotFoundException {
|
||||||
Class<?> loadedClass = findLoadedClass(name);
|
Class<?> loadedClass = findLoadedClass(name);
|
||||||
if (loadedClass == null) {
|
if (loadedClass == null) {
|
||||||
for (DexFile dex : dx) {
|
for (DexClassLoader dex : dx) {
|
||||||
loadedClass = dex.loadClass(name, parent);
|
loadedClass = dex.loadClass(name);
|
||||||
if (loadedClass != null) {
|
if (loadedClass != null) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -332,6 +332,14 @@ public class ScriptRuntime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadDex(String path){
|
||||||
|
try {
|
||||||
|
((AndroidClassLoader) ContextFactory.getGlobal().getApplicationClassLoader()).loadDex(new File(path));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new UncheckedIOException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void exit() {
|
public void exit() {
|
||||||
mThread.interrupt();
|
mThread.interrupt();
|
||||||
if (Looper.myLooper() != Looper.getMainLooper()) {
|
if (Looper.myLooper() != Looper.getMainLooper()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user