feat: relative path support for module "files" and require()

This commit is contained in:
hyb1996 2018-01-24 11:14:58 +08:00
parent 5ccf434f1b
commit c8450dfc3a
5 changed files with 96 additions and 157 deletions

View File

@ -1,6 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="Stardust">
<words>
<w>capturer</w>
<w>dismissable</w>
<w>interruptible</w>
<w>loopers</w>

View File

@ -88,3 +88,11 @@ require("__globals__")(__runtime__, this);
__importClass__(android.view.KeyEvent);
__importClass__(com.stardust.autojs.core.util.Shell);
(function(){
var __require__ = require;
require = function(path){
path = files.path(path);
return __require__(path);
};
})();

View File

@ -39,7 +39,6 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
private static int contextCount = 0;
private static StringScriptSource sInitScript;
private List<String> mRequirePath = Collections.emptyList();
private Context mContext;
private Scriptable mScriptable;
@ -99,7 +98,6 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
public void init() {
mThread = Thread.currentThread();
ScriptableObject.putProperty(mScriptable, "__engine__", this);
mRequirePath = (List<String>) getTag(TAG_ENV_PATH);
initRequireBuilder(mContext, mScriptable);
mContext.evaluateString(mScriptable, getInitScript().getScript(), "<init>", 1, null);
}
@ -119,13 +117,8 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
}
void initRequireBuilder(Context context, Scriptable scope) {
List<URI> list = new ArrayList<>();
if (mRequirePath != null) {
for (String path : mRequirePath) {
list.add(new File(path).toURI());
}
}
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(mAndroidContext, list);
AssetAndUrlModuleSourceProvider provider = new AssetAndUrlModuleSourceProvider(mAndroidContext,
Collections.singletonList(new File("/").toURI()));
new RequireBuilder()
.setModuleScriptProvider(new SoftCachingModuleScriptProvider(provider))
.setSandboxed(false)

View File

@ -27,219 +27,165 @@ public class Files {
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() {
return ((ScriptEngine.AbstractScriptEngine) mRuntime.engines.myEngine()).cwd();
}
public static PFileInterface open(String path, String mode, String encoding, int bufferSize) {
return PFiles.open(path, mode, encoding, bufferSize);
public PFileInterface open(String path, String mode, String encoding, int bufferSize) {
return PFiles.open(path(path), mode, encoding, bufferSize);
}
public static Object open(String path, String mode, String encoding) {
return PFiles.open(path, mode, encoding);
public Object open(String path, String mode, String encoding) {
return PFiles.open(path(path), mode, encoding);
}
public static Object open(String path, String mode) {
return PFiles.open(path, mode);
public Object open(String path, String mode) {
return PFiles.open(path(path), mode);
}
public static Object open(String path) {
return PFiles.open(path);
public Object open(String path) {
return PFiles.open(path(path));
}
public static boolean create(String path) {
return PFiles.create(path);
public boolean create(String path) {
return PFiles.create(path(path));
}
public static boolean createIfNotExists(String path) {
return PFiles.createIfNotExists(path);
public boolean createIfNotExists(String path) {
return PFiles.createIfNotExists(path(path));
}
public static boolean createWithDirs(String path) {
return PFiles.createWithDirs(path);
public boolean createWithDirs(String path) {
return PFiles.createWithDirs(path(path));
}
public static boolean exists(String path) {
return PFiles.exists(path);
public boolean exists(String path) {
return PFiles.exists(path(path));
}
public static boolean ensureDir(String path) {
return PFiles.ensureDir(path);
public boolean ensureDir(String path) {
return PFiles.ensureDir(path(path));
}
public static String read(String path, String encoding) {
return PFiles.read(path, encoding);
public String read(String path, String encoding) {
return PFiles.read(path(path), encoding);
}
public static String read(String path) {
return PFiles.read(path);
public String read(String path) {
return PFiles.read(path(path));
}
public static String read(File file, String encoding) {
return PFiles.read(file, encoding);
public void write(String path, String text) {
PFiles.write(path(path), text);
}
public static String read(File file) {
return PFiles.read(file);
public void write(String path, String text, String encoding) {
PFiles.write(path(path), text, encoding);
}
public static String read(InputStream is, String encoding) {
return PFiles.read(is, encoding);
public void append(String path, String text) {
PFiles.append(path(path), text);
}
public static String read(InputStream inputStream) {
return PFiles.read(inputStream);
public void append(String path, String text, String encoding) {
PFiles.append(path(path), text, encoding);
}
public static byte[] readBytes(InputStream is) {
return PFiles.readBytes(is);
public void appendBytes(String path, byte[] bytes) {
PFiles.appendBytes(path(path), bytes);
}
public static boolean copyRaw(Context context, int rawId, String path) {
return PFiles.copyRaw(context, rawId, path);
public void writeBytes(String path, byte[] bytes) {
PFiles.writeBytes(path(path), bytes);
}
public static boolean copyStream(InputStream is, String path) {
return PFiles.copyStream(is, path);
public boolean copy(String pathFrom, String pathTo) {
return PFiles.copy(path(pathFrom), path(pathTo));
}
public static void write(InputStream is, OutputStream os) {
PFiles.write(is, os);
public boolean renameWithoutExtension(String path, String newName) {
return PFiles.renameWithoutExtension(path(path), newName);
}
public static void write(String path, String text) {
PFiles.write(path, text);
public boolean rename(String path, String newName) {
return PFiles.rename(path(path), newName);
}
public static void write(String path, String text, String encoding) {
PFiles.write(path, text, encoding);
public boolean move(String path, String newPath) {
return PFiles.move(path(path), newPath);
}
public static void write(File file, String text) {
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) {
public String getExtension(String fileName) {
return PFiles.getExtension(fileName);
}
public static String generateNotExistingPath(String path, String extension) {
return PFiles.generateNotExistingPath(path, extension);
}
public static String getName(String filePath) {
public String getName(String filePath) {
return PFiles.getName(filePath);
}
public static String getNameWithoutExtension(String filePath) {
public String getNameWithoutExtension(String filePath) {
return PFiles.getNameWithoutExtension(filePath);
}
public static File copyAssetToTmpFile(Context context, String path) {
return PFiles.copyAssetToTmpFile(context, path);
public boolean remove(String path) {
return PFiles.remove(path(path));
}
public static boolean deleteRecursively(File file) {
return PFiles.deleteRecursively(file);
public boolean removeDir(String path) {
return PFiles.removeDir(path(path));
}
public static boolean remove(String path) {
return PFiles.remove(path);
}
public static boolean removeDir(String path) {
return PFiles.removeDir(path);
}
public static String getSdcardPath() {
public String getSdcardPath() {
return PFiles.getSdcardPath();
}
public static String readAsset(AssetManager assets, String path) {
return PFiles.readAsset(assets, path);
public String[] listDir(String path) {
return PFiles.listDir(path(path));
}
public static String[] listDir(String path) {
return PFiles.listDir(path);
public String[] listDir(String path, Func1<String, Boolean> filter) {
return PFiles.listDir(path(path), filter);
}
public static String[] listDir(String path, Func1<String, Boolean> filter) {
return PFiles.listDir(path, filter);
public boolean isFile(String path) {
return PFiles.isFile(path(path));
}
public static boolean isFile(String path) {
return PFiles.isFile(path);
public boolean isDir(String path) {
return PFiles.isDir(path(path));
}
public static boolean isDir(String path) {
return PFiles.isDir(path);
}
public static boolean isEmptyDir(String path) {
return PFiles.isEmptyDir(path);
public boolean isEmptyDir(String path) {
return PFiles.isEmptyDir(path(path));
}
public static String join(String parent, String... child) {
return PFiles.join(parent, child);
}
public static String getHumanReadableSize(long bytes) {
public String getHumanReadableSize(long bytes) {
return PFiles.getHumanReadableSize(bytes);
}
public static String getSimplifiedPath(String path) {
public String getSimplifiedPath(String path) {
return PFiles.getSimplifiedPath(path);
}
}

View File

@ -118,6 +118,7 @@ public class Images {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean captureScreen(String path) {
path = mScriptRuntime.files.path(path);
ImageWrapper image = captureScreen();
if (image != null) {
saveImage(image, path);
@ -130,21 +131,10 @@ public class Images {
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) {
if(image == null){
throw new NullPointerException("image = null");
}
x = ScreenMetrics.rescaleX(x, image.getWidth());
y = ScreenMetrics.rescaleY(y, image.getHeight());
return image.pixel(x, y);
@ -152,6 +142,7 @@ public class Images {
public ImageWrapper read(String path) {
path = mScriptRuntime.files.path(path);
Bitmap bitmap = BitmapFactory.decodeFile(path);
return ImageWrapper.ofBitmap(bitmap);
}