feat: relative path support for module "files" and require()
This commit is contained in:
1
.idea/dictionaries/Stardust.xml
generated
1
.idea/dictionaries/Stardust.xml
generated
@@ -1,6 +1,7 @@
|
|||||||
<component name="ProjectDictionaryState">
|
<component name="ProjectDictionaryState">
|
||||||
<dictionary name="Stardust">
|
<dictionary name="Stardust">
|
||||||
<words>
|
<words>
|
||||||
|
<w>capturer</w>
|
||||||
<w>dismissable</w>
|
<w>dismissable</w>
|
||||||
<w>interruptible</w>
|
<w>interruptible</w>
|
||||||
<w>loopers</w>
|
<w>loopers</w>
|
||||||
|
|||||||
@@ -88,3 +88,11 @@ require("__globals__")(__runtime__, this);
|
|||||||
__importClass__(android.view.KeyEvent);
|
__importClass__(android.view.KeyEvent);
|
||||||
__importClass__(com.stardust.autojs.core.util.Shell);
|
__importClass__(com.stardust.autojs.core.util.Shell);
|
||||||
|
|
||||||
|
(function(){
|
||||||
|
var __require__ = require;
|
||||||
|
require = function(path){
|
||||||
|
path = files.path(path);
|
||||||
|
return __require__(path);
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
|||||||
|
|
||||||
private static int contextCount = 0;
|
private static int contextCount = 0;
|
||||||
private static StringScriptSource sInitScript;
|
private static StringScriptSource sInitScript;
|
||||||
private List<String> mRequirePath = Collections.emptyList();
|
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private Scriptable mScriptable;
|
private Scriptable mScriptable;
|
||||||
@@ -99,7 +98,6 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
|||||||
public void init() {
|
public void init() {
|
||||||
mThread = Thread.currentThread();
|
mThread = Thread.currentThread();
|
||||||
ScriptableObject.putProperty(mScriptable, "__engine__", this);
|
ScriptableObject.putProperty(mScriptable, "__engine__", this);
|
||||||
mRequirePath = (List<String>) getTag(TAG_ENV_PATH);
|
|
||||||
initRequireBuilder(mContext, mScriptable);
|
initRequireBuilder(mContext, mScriptable);
|
||||||
mContext.evaluateString(mScriptable, getInitScript().getScript(), "<init>", 1, null);
|
mContext.evaluateString(mScriptable, getInitScript().getScript(), "<init>", 1, null);
|
||||||
}
|
}
|
||||||
@@ -119,13 +117,8 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void initRequireBuilder(Context context, Scriptable scope) {
|
void initRequireBuilder(Context context, Scriptable scope) {
|
||||||
List<URI> list = new ArrayList<>();
|
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(mAndroidContext,
|
||||||
if (mRequirePath != null) {
|
Collections.singletonList(new File("/").toURI()));
|
||||||
for (String path : mRequirePath) {
|
|
||||||
list.add(new File(path).toURI());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(mAndroidContext, list);
|
|
||||||
new RequireBuilder()
|
new RequireBuilder()
|
||||||
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(provider))
|
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(provider))
|
||||||
.setSandboxed(false)
|
.setSandboxed(false)
|
||||||
|
|||||||
@@ -27,219 +27,165 @@ public class Files {
|
|||||||
mRuntime = runtime;
|
mRuntime = runtime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String path(String relativePath) {
|
||||||
|
String cwd = cwd();
|
||||||
|
if (cwd == null || relativePath == null || relativePath.startsWith("/"))
|
||||||
|
return relativePath;
|
||||||
|
File f = new File(cwd);
|
||||||
|
String[] paths = relativePath.split("/");
|
||||||
|
for (String path : paths) {
|
||||||
|
if (path.equals("."))
|
||||||
|
continue;
|
||||||
|
if (path.equals("..")) {
|
||||||
|
f = f.getParentFile();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
f = new File(f, path);
|
||||||
|
}
|
||||||
|
return f.getPath();
|
||||||
|
}
|
||||||
|
|
||||||
public String cwd() {
|
public String cwd() {
|
||||||
return ((ScriptEngine.AbstractScriptEngine) mRuntime.engines.myEngine()).cwd();
|
return ((ScriptEngine.AbstractScriptEngine) mRuntime.engines.myEngine()).cwd();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PFileInterface open(String path, String mode, String encoding, int bufferSize) {
|
public PFileInterface open(String path, String mode, String encoding, int bufferSize) {
|
||||||
return PFiles.open(path, mode, encoding, bufferSize);
|
return PFiles.open(path(path), mode, encoding, bufferSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object open(String path, String mode, String encoding) {
|
public Object open(String path, String mode, String encoding) {
|
||||||
return PFiles.open(path, mode, encoding);
|
return PFiles.open(path(path), mode, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object open(String path, String mode) {
|
public Object open(String path, String mode) {
|
||||||
return PFiles.open(path, mode);
|
return PFiles.open(path(path), mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object open(String path) {
|
public Object open(String path) {
|
||||||
return PFiles.open(path);
|
return PFiles.open(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean create(String path) {
|
public boolean create(String path) {
|
||||||
return PFiles.create(path);
|
return PFiles.create(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean createIfNotExists(String path) {
|
public boolean createIfNotExists(String path) {
|
||||||
return PFiles.createIfNotExists(path);
|
return PFiles.createIfNotExists(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean createWithDirs(String path) {
|
public boolean createWithDirs(String path) {
|
||||||
return PFiles.createWithDirs(path);
|
return PFiles.createWithDirs(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean exists(String path) {
|
public boolean exists(String path) {
|
||||||
return PFiles.exists(path);
|
return PFiles.exists(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean ensureDir(String path) {
|
public boolean ensureDir(String path) {
|
||||||
return PFiles.ensureDir(path);
|
return PFiles.ensureDir(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String read(String path, String encoding) {
|
public String read(String path, String encoding) {
|
||||||
return PFiles.read(path, encoding);
|
return PFiles.read(path(path), encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String read(String path) {
|
public String read(String path) {
|
||||||
return PFiles.read(path);
|
return PFiles.read(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String read(File file, String encoding) {
|
public void write(String path, String text) {
|
||||||
return PFiles.read(file, encoding);
|
PFiles.write(path(path), text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String read(File file) {
|
public void write(String path, String text, String encoding) {
|
||||||
return PFiles.read(file);
|
PFiles.write(path(path), text, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String read(InputStream is, String encoding) {
|
public void append(String path, String text) {
|
||||||
return PFiles.read(is, encoding);
|
PFiles.append(path(path), text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String read(InputStream inputStream) {
|
public void append(String path, String text, String encoding) {
|
||||||
return PFiles.read(inputStream);
|
PFiles.append(path(path), text, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] readBytes(InputStream is) {
|
public void appendBytes(String path, byte[] bytes) {
|
||||||
return PFiles.readBytes(is);
|
PFiles.appendBytes(path(path), bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean copyRaw(Context context, int rawId, String path) {
|
public void writeBytes(String path, byte[] bytes) {
|
||||||
return PFiles.copyRaw(context, rawId, path);
|
PFiles.writeBytes(path(path), bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean copyStream(InputStream is, String path) {
|
public boolean copy(String pathFrom, String pathTo) {
|
||||||
return PFiles.copyStream(is, path);
|
return PFiles.copy(path(pathFrom), path(pathTo));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void write(InputStream is, OutputStream os) {
|
public boolean renameWithoutExtension(String path, String newName) {
|
||||||
PFiles.write(is, os);
|
return PFiles.renameWithoutExtension(path(path), newName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void write(String path, String text) {
|
public boolean rename(String path, String newName) {
|
||||||
PFiles.write(path, text);
|
return PFiles.rename(path(path), newName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void write(String path, String text, String encoding) {
|
public boolean move(String path, String newPath) {
|
||||||
PFiles.write(path, text, encoding);
|
return PFiles.move(path(path), newPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void write(File file, String text) {
|
public String getExtension(String fileName) {
|
||||||
PFiles.write(file, text);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void write(FileOutputStream fileOutputStream, String text) {
|
|
||||||
PFiles.write(fileOutputStream, text);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void write(OutputStream outputStream, String text, String encoding) {
|
|
||||||
PFiles.write(outputStream, text, encoding);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void append(String path, String text) {
|
|
||||||
PFiles.append(path, text);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void append(String path, String text, String encoding) {
|
|
||||||
PFiles.append(path, text, encoding);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeBytes(OutputStream outputStream, byte[] bytes) {
|
|
||||||
PFiles.writeBytes(outputStream, bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void appendBytes(String path, byte[] bytes) {
|
|
||||||
PFiles.appendBytes(path, bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeBytes(String path, byte[] bytes) {
|
|
||||||
PFiles.writeBytes(path, bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean copy(String pathFrom, String pathTo) {
|
|
||||||
return PFiles.copy(pathFrom, pathTo);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean copyAsset(Context context, String assetFile, String path) {
|
|
||||||
return PFiles.copyAsset(context, assetFile, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String renameWithoutExtensionAndReturnNewPath(String path, String newName) {
|
|
||||||
return PFiles.renameWithoutExtensionAndReturnNewPath(path, newName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean renameWithoutExtension(String path, String newName) {
|
|
||||||
return PFiles.renameWithoutExtension(path, newName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean rename(String path, String newName) {
|
|
||||||
return PFiles.rename(path, newName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean move(String path, String newPath) {
|
|
||||||
return PFiles.move(path, newPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getExtension(String fileName) {
|
|
||||||
return PFiles.getExtension(fileName);
|
return PFiles.getExtension(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String generateNotExistingPath(String path, String extension) {
|
public String getName(String filePath) {
|
||||||
return PFiles.generateNotExistingPath(path, extension);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getName(String filePath) {
|
|
||||||
return PFiles.getName(filePath);
|
return PFiles.getName(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNameWithoutExtension(String filePath) {
|
public String getNameWithoutExtension(String filePath) {
|
||||||
return PFiles.getNameWithoutExtension(filePath);
|
return PFiles.getNameWithoutExtension(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File copyAssetToTmpFile(Context context, String path) {
|
public boolean remove(String path) {
|
||||||
return PFiles.copyAssetToTmpFile(context, path);
|
return PFiles.remove(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean deleteRecursively(File file) {
|
public boolean removeDir(String path) {
|
||||||
return PFiles.deleteRecursively(file);
|
return PFiles.removeDir(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean remove(String path) {
|
public String getSdcardPath() {
|
||||||
return PFiles.remove(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean removeDir(String path) {
|
|
||||||
return PFiles.removeDir(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getSdcardPath() {
|
|
||||||
return PFiles.getSdcardPath();
|
return PFiles.getSdcardPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String readAsset(AssetManager assets, String path) {
|
public String[] listDir(String path) {
|
||||||
return PFiles.readAsset(assets, path);
|
return PFiles.listDir(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] listDir(String path) {
|
public String[] listDir(String path, Func1<String, Boolean> filter) {
|
||||||
return PFiles.listDir(path);
|
return PFiles.listDir(path(path), filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] listDir(String path, Func1<String, Boolean> filter) {
|
public boolean isFile(String path) {
|
||||||
return PFiles.listDir(path, filter);
|
return PFiles.isFile(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isFile(String path) {
|
public boolean isDir(String path) {
|
||||||
return PFiles.isFile(path);
|
return PFiles.isDir(path(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isDir(String path) {
|
public boolean isEmptyDir(String path) {
|
||||||
return PFiles.isDir(path);
|
return PFiles.isEmptyDir(path(path));
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isEmptyDir(String path) {
|
|
||||||
return PFiles.isEmptyDir(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String join(String parent, String... child) {
|
public static String join(String parent, String... child) {
|
||||||
return PFiles.join(parent, child);
|
return PFiles.join(parent, child);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getHumanReadableSize(long bytes) {
|
public String getHumanReadableSize(long bytes) {
|
||||||
return PFiles.getHumanReadableSize(bytes);
|
return PFiles.getHumanReadableSize(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getSimplifiedPath(String path) {
|
public String getSimplifiedPath(String path) {
|
||||||
return PFiles.getSimplifiedPath(path);
|
return PFiles.getSimplifiedPath(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ public class Images {
|
|||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||||
public boolean captureScreen(String path) {
|
public boolean captureScreen(String path) {
|
||||||
|
path = mScriptRuntime.files.path(path);
|
||||||
ImageWrapper image = captureScreen();
|
ImageWrapper image = captureScreen();
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
saveImage(image, path);
|
saveImage(image, path);
|
||||||
@@ -130,21 +131,10 @@ public class Images {
|
|||||||
image.saveTo(path);
|
image.saveTo(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static int pixel(Image image, int x, int y) {
|
|
||||||
int originX = x;
|
|
||||||
int originY = y;
|
|
||||||
ScreenMetrics metrics = new ScreenMetrics(image.getWidth(), image.getHeight());
|
|
||||||
x = metrics.rescaleX(x);
|
|
||||||
y = metrics.rescaleY(y);
|
|
||||||
Image.Plane plane = image.getPlanes()[0];
|
|
||||||
int offset = y * plane.getRowStride() + x * plane.getPixelStride();
|
|
||||||
int c = plane.getBuffer().getInt(offset);
|
|
||||||
Log.d("Images", String.format(Locale.getDefault(), "(%d, %d)→(%d, %d)", originX, originY, x, y));
|
|
||||||
return (c & 0xff000000) + ((c & 0xff) << 16) + (c & 0x00ff00) + ((c & 0xff0000) >> 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int pixel(ImageWrapper image, int x, int y) {
|
public static int pixel(ImageWrapper image, int x, int y) {
|
||||||
|
if(image == null){
|
||||||
|
throw new NullPointerException("image = null");
|
||||||
|
}
|
||||||
x = ScreenMetrics.rescaleX(x, image.getWidth());
|
x = ScreenMetrics.rescaleX(x, image.getWidth());
|
||||||
y = ScreenMetrics.rescaleY(y, image.getHeight());
|
y = ScreenMetrics.rescaleY(y, image.getHeight());
|
||||||
return image.pixel(x, y);
|
return image.pixel(x, y);
|
||||||
@@ -152,6 +142,7 @@ public class Images {
|
|||||||
|
|
||||||
|
|
||||||
public ImageWrapper read(String path) {
|
public ImageWrapper read(String path) {
|
||||||
|
path = mScriptRuntime.files.path(path);
|
||||||
Bitmap bitmap = BitmapFactory.decodeFile(path);
|
Bitmap bitmap = BitmapFactory.decodeFile(path);
|
||||||
return ImageWrapper.ofBitmap(bitmap);
|
return ImageWrapper.ofBitmap(bitmap);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user