Skip to content

Commit b758ef0

Browse files
committed
Feat: add support copy resource packs and behavior packs
1 parent ed8c173 commit b758ef0

4 files changed

Lines changed: 333 additions & 19 deletions

File tree

src/main/java/com/reider745/hooks/RhinoOverrides.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import cn.nukkit.utils.MainLogger;
44
import com.reider745.api.ReflectHelper;
5+
import com.reider745.api.hooks.annotation.ShadowGetField;
6+
import com.reider745.api.hooks.annotation.ShadowSetField;
57
import javassist.CannotCompileException;
68
import javassist.CtClass;
79
import javassist.CtMethod;
@@ -96,9 +98,17 @@ static boolean testIfCanLoadRhinoClasses(ClassLoader loader) {
9698
return true;
9799
}
98100

101+
@ShadowGetField(className = "org.mozilla.javascript.Context", field = "applicationClassLoader")
102+
public static ClassLoader getApplicationClassLoaderField(Context self) {
103+
return null;
104+
}
105+
106+
@ShadowSetField(className = "org.mozilla.javascript.Context", field = "applicationClassLoader")
107+
public static void setApplicationClassLoaderField(Context self, ClassLoader loader) {}
108+
99109
@Inject(className = "org.mozilla.javascript.Context")
100110
public static ClassLoader getApplicationClassLoader(Context self) throws ClassNotFoundException {
101-
ClassLoader applicationClassLoader = ReflectHelper.getField(self, "applicationClassLoader");
111+
ClassLoader applicationClassLoader = getApplicationClassLoaderField(self);
102112
if (applicationClassLoader == null) {
103113
ContextFactory f = self.getFactory();
104114
ClassLoader loader = f.getApplicationClassLoader();
@@ -124,7 +134,7 @@ public static ClassLoader getApplicationClassLoader(Context self) throws ClassNo
124134
}
125135
}
126136
applicationClassLoader = new ClassLoaderPool(loader);
127-
ReflectHelper.setField(self, "applicationClassLoader", applicationClassLoader);
137+
setApplicationClassLoaderField(self, applicationClassLoader);
128138
}
129139
return applicationClassLoader;
130140
}

src/main/java/com/zhekasmirnov/horizon/util/FileUtils.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.zhekasmirnov.horizon.util;
22

3+
import org.json.JSONArray;
34
import org.json.JSONException;
45
import org.json.JSONObject;
56

67
import java.io.*;
8+
import java.nio.ByteBuffer;
9+
import java.nio.channels.Channels;
10+
import java.nio.channels.ReadableByteChannel;
11+
import java.nio.channels.WritableByteChannel;
712
import java.util.ArrayList;
813
import java.util.List;
914

@@ -56,6 +61,11 @@ public static void writeJSON(File file, JSONObject jsonObject) throws IOExceptio
5661
FileUtils.writeFileText(file, result);
5762
}
5863

64+
public static void writeJSON(File file, JSONArray jsonObject) throws IOException {
65+
String result = jsonObject.toString();
66+
FileUtils.writeFileText(file, result);
67+
}
68+
5969
public static String cleanupPath(String path) {
6070
path = path.replaceAll("\\\\", "/");
6171
while (path.startsWith("/")) {
@@ -101,4 +111,42 @@ public static void setFileFlag(File directory, String name, boolean exists) {
101111
flag.delete();
102112
}
103113
}
114+
115+
public static void copy(File src, File dst) throws IOException {
116+
FileInputStream inputStream = new FileInputStream(src);
117+
FileOutputStream outputStream = new FileOutputStream(dst);
118+
119+
ReadableByteChannel inputChannel = Channels.newChannel(inputStream);
120+
WritableByteChannel outputChannel = Channels.newChannel(outputStream);
121+
122+
final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
123+
while (inputChannel.read(buffer) != -1) {
124+
buffer.flip();
125+
outputChannel.write(buffer);
126+
buffer.compact();
127+
}
128+
buffer.flip();
129+
while (buffer.hasRemaining()) {
130+
outputChannel.write(buffer);
131+
}
132+
}
133+
134+
135+
public static void copyFileTree(File srcDir, File dstDir, Object dialog, String prefix) throws IOException {
136+
if (!dstDir.isDirectory() && !dstDir.mkdirs()) {
137+
throw new IOException("mkdirs failed: " + dstDir);
138+
}
139+
for (String name : srcDir.list()) {
140+
if (dialog != null) {
141+
return;
142+
}
143+
File srcFile = new File(srcDir, name);
144+
File dstFile = new File(dstDir, name);
145+
if (srcFile.isDirectory()) {
146+
copyFileTree(srcFile, dstFile, dialog, prefix + name + "/");
147+
} else {
148+
copy(srcFile, dstFile);
149+
}
150+
}
151+
}
104152
}

0 commit comments

Comments
 (0)