Skip to content

Commit 98d1e84

Browse files
committed
plugins are now loaded more properlier, allowing custom world generation
1 parent f8b5493 commit 98d1e84

File tree

4 files changed

+80
-33
lines changed

4 files changed

+80
-33
lines changed

native/src/lib.rs

+33-12
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,25 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_loadPlugin<'a>(
7272
return;
7373
}
7474
let lib = lib.unwrap();
75-
LIBRARY_MANAGER.push_lib(file, (lib, true));
75+
let parts = file
76+
.split(std::path::MAIN_SEPARATOR)
77+
.map(|f| f.to_string())
78+
.collect::<Vec<String>>();
79+
let mut name = parts.get(parts.len() - 1).unwrap().clone();
80+
#[cfg(target_os = "windows")]
81+
{
82+
name = name.replace(".dll", "");
83+
}
84+
#[cfg(target_os = "macos")]
85+
{
86+
name = name.replace(".dylib", "");
87+
}
88+
#[cfg(target_os = "linux")]
89+
{
90+
name = name.replace(".so", "");
91+
}
92+
println!("{}", name);
93+
LIBRARY_MANAGER.push_lib(name, (lib, true));
7694
}
7795
}
7896

@@ -144,19 +162,17 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryHasFunction<'a>(
144162
libname_raw: JString,
145163
funcname_raw: JString,
146164
) -> jboolean {
147-
let libname = &env
148-
.get_string(&libname_raw)
149-
.unwrap()
165+
let libname_raw = env.get_string(&libname_raw);
166+
let libname = unwrap_result_or_java_error(&mut env, "unknown library", libname_raw)
150167
.to_string_lossy()
151168
.to_string();
152-
let funcname = &env
153-
.get_string(&funcname_raw)
154-
.unwrap()
169+
let funcname_raw = env.get_string(&funcname_raw);
170+
let funcname = unwrap_result_or_java_error(&mut env, &libname, funcname_raw)
155171
.to_string_lossy()
156172
.to_string();
157173

158174
let manager = unsafe { LIBRARY_MANAGER.loaded_libraries.lock() };
159-
let lib = manager.get(libname).unwrap().library();
175+
let lib = manager.get(&libname).unwrap().library();
160176
let func: Result<
161177
libloading::Symbol<unsafe extern "C" fn(JNIEnv<'_>, JObject<'_>)>,
162178
libloading::Error,
@@ -245,8 +261,13 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_execute<'a>(
245261
.to_string();
246262

247263
let manager = unsafe { LIBRARY_MANAGER.loaded_libraries.lock() };
248-
let lib =
249-
unwrap_option_or_java_error(&mut env, &libname, &libname, manager.get(&libname)).library();
264+
let lib = unwrap_option_or_java_error(
265+
&mut env,
266+
&libname,
267+
&format!("manager.get({})", &libname),
268+
manager.get(&libname),
269+
)
270+
.library();
250271
let func: Result<
251272
libloading::Symbol<extern "C" fn(JNIEnv<'_>, JObject<'_>) -> JObject<'a>>,
252273
libloading::Error,
@@ -277,7 +298,7 @@ where
277298
throw_with_error(
278299
&mut env,
279300
"net/ioixd/blackbox/exceptions/NativeLibrarySymbolLoadException",
280-
format!("error loading {}: {:?}", &libname, err),
301+
format!("error with {}: {:?}", &libname, err),
281302
);
282303
unreachable!();
283304
}
@@ -299,7 +320,7 @@ where
299320
throw_with_error(
300321
&mut env,
301322
"net/ioixd/blackbox/exceptions/NativeLibrarySymbolLoadException",
302-
format!("error loading {}: {} is None", &libname, valname),
323+
format!("error with {}: {} is None", &libname, valname),
303324
);
304325
unreachable!();
305326
}

src/main/java/net/ioixd/blackbox/BlackBox.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
import java.io.InputStream;
1717
import java.nio.file.Files;
1818
import java.nio.file.Paths;
19+
import java.util.ArrayList;
1920
import java.util.regex.Matcher;
2021
import java.util.regex.Pattern;
2122

2223
public final class BlackBox extends JavaPlugin {
2324
PluginManager pm = null;
2425

26+
private ArrayList<BlackBoxPlugin> plugins = new ArrayList<>();
27+
2528
@Override
2629
public void onLoad() {
2730
pm = Bukkit.getServer().getPluginManager();
@@ -68,9 +71,11 @@ public void onLoad() {
6871
if (!match.find()) {
6972
continue;
7073
}
71-
7274
try {
73-
pm.loadPlugin(f);
75+
BlackBoxPlugin p = (BlackBoxPlugin) pm.loadPlugin(f);
76+
if (p != null) {
77+
plugins.add(p);
78+
}
7479
} catch (InvalidPluginException e) {
7580
e.printStackTrace();
7681
} catch (InvalidDescriptionException e) {
@@ -90,8 +95,14 @@ public void onEnable() {
9095
clsgraph.initializeLoadedClasses();
9196
ClassInfoList info = clsgraph.scan().getSubclasses(Event.class.getName());
9297
info.forEach(cls -> {
93-
cls.loadClass();
98+
cls.loadClass(true);
9499
});
100+
for (BlackBoxPlugin p : plugins) {
101+
if (p == null) {
102+
continue;
103+
}
104+
p.updateEventListeners();
105+
}
95106
}
96107

97108
@Override

src/main/java/net/ioixd/blackbox/BlackBoxPlugin.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class BlackBoxPluginListener implements Listener {
5656

5757
public BlackBoxPluginListener(BlackBoxPlugin plugin, String library) {
5858
this.plugin = plugin;
59+
this.library = library;
5960
this.registeredListener = new RegisteredListener(this, this::onEvent, EventPriority.NORMAL, plugin, false);
6061
for (HandlerList handler : HandlerList.getHandlerLists()) {
6162
handler.register(this.registeredListener);
@@ -66,7 +67,7 @@ public void onEvent(Listener listener, Event event) {
6667
// get the event name
6768
String name = event.getEventName();
6869
String hookName = "__on__" + name;
69-
this.plugin.getLogger().info(hookName);
70+
System.out.println(this.library + ", " + hookName);
7071

7172
try {
7273
boolean result = Native.libraryHasFunction(library, hookName);
@@ -84,17 +85,21 @@ public void onEvent(Listener listener, Event event) {
8485

8586
BlackBoxPlugin(String library, BlackBox parent, PluginLoader loader) {
8687
this.library = library;
88+
this.listener = new BlackBoxPluginListener(this, this.library);
8789
this.isEnabled = true;
8890
this.naggable = true;
8991
this.parent = parent;
9092
File pluginsFolder = parent.getDataFolder().getParentFile();
91-
if (!Paths.get(pluginsFolder.getPath(), library).toFile().exists()) {
93+
if (!Paths.get(pluginsFolder.getPath(), this.library).toFile().exists()) {
9294
pluginsFolder.mkdir();
93-
Paths.get(pluginsFolder.getPath(), library).toFile().mkdir();
95+
Paths.get(pluginsFolder.getPath(), this.library).toFile().mkdir();
9496
}
95-
this.dataFolder = Paths.get(pluginsFolder.getPath(), library).toFile();
97+
this.dataFolder = Paths.get(pluginsFolder.getPath(), this.library).toFile();
9698
this.loader = loader;
97-
this.file = new File(library);
99+
this.file = new File(this.library);
100+
}
101+
102+
public void updateEventListeners() {
98103
this.listener = new BlackBoxPluginListener(this, this.library);
99104
}
100105

@@ -109,9 +114,10 @@ public String getName() {
109114

110115
Object execNative(String functionName, Object[] objects) {
111116
try {
112-
boolean hasFunc = Native.libraryHasFunction(library, "getConfig");
117+
boolean hasFunc = Native.libraryHasFunction(library, functionName);
113118
if (hasFunc) {
114-
return Native.execute(this.library, "__getConfig", this, objects);
119+
System.out.println(this.library);
120+
return Native.execute(this.library, "__" + functionName, this, objects);
115121
} else {
116122
// default
117123
return null;
@@ -121,6 +127,7 @@ Object execNative(String functionName, Object[] objects) {
121127
// this.getLogger().severe("Disabling self");
122128
// this.disable();
123129
}
130+
this.getLogger().info(library + ", " + functionName);
124131
return null;
125132
}
126133

src/main/java/net/ioixd/blackbox/BlackBoxPluginLoader.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.lang.reflect.Method;
88
import java.util.HashMap;
99
import java.util.HashSet;
10+
import java.util.List;
1011
import java.util.Map;
1112
import java.util.Set;
1213
import java.util.regex.Pattern;
@@ -44,29 +45,41 @@ public BlackBoxPluginLoader(@NotNull Server instance) {
4445
@Override
4546
@NotNull
4647
public Plugin loadPlugin(@NotNull final File file) {
47-
this.server.getLogger().warning("loadPlugin");
48+
return loadPlugin(file, null);
49+
}
50+
51+
public Plugin loadPlugin(@NotNull final File file, List<BlackBoxPlugin> returnedPlugins) {
52+
String library = file.getAbsolutePath();
4853

49-
server.getLogger().info("Loading native plugin " + file.getAbsolutePath());
54+
String[] parts = library.split(File.separator);
55+
String libraryName = parts[parts.length - 1].replace(BlackBoxPluginLoader.getFileExtension(), "").replace("-",
56+
"_");
57+
;
5058

5159
Preconditions.checkArgument(file != null, "File cannot be null");
5260

5361
if (!file.exists()) {
54-
server.getLogger().severe(file.getPath() + " does not exist");
62+
server.getLogger().severe(file.getAbsolutePath() + " does not exist");
5563
return null;
5664
}
5765

58-
Native.loadPlugin(file.getAbsolutePath());
66+
Native.loadPlugin(library);
5967

60-
BlackBoxPlugin plugin = new BlackBoxPlugin(file.getAbsolutePath(),
68+
server.getLogger().info("Loading native plugin " + libraryName);
69+
70+
BlackBoxPlugin plugin = new BlackBoxPlugin(libraryName,
6171
(BlackBox) server.getPluginManager().getPlugin("BlackBox"), this);
6272
libNameMap.put(plugin, plugin);
73+
if (returnedPlugins != null) {
74+
returnedPlugins.add(plugin);
75+
}
6376

77+
System.out.println("Returning " + plugin.toString());
6478
return plugin;
6579
}
6680

6781
@Override
6882
public void enablePlugin(@NotNull final Plugin plugin) {
69-
this.server.getLogger().warning("enablePlugin");
7083
if (libNameMap.containsKey(plugin)) {
7184
Native.enablePlugin(libNameMap.get(plugin).getInnerLibraryName());
7285
} else {
@@ -76,7 +89,6 @@ public void enablePlugin(@NotNull final Plugin plugin) {
7689

7790
@Override
7891
public void disablePlugin(@NotNull Plugin plugin) {
79-
this.server.getLogger().warning("disablePlugin");
8092
if (libNameMap.containsKey(plugin)) {
8193
Native.disablePlugin(libNameMap.get(plugin).getInnerLibraryName());
8294
} else {
@@ -87,7 +99,6 @@ public void disablePlugin(@NotNull Plugin plugin) {
8799
@Override
88100
@NotNull
89101
public PluginDescriptionFile getPluginDescription(@NotNull File file) {
90-
this.server.getLogger().warning("getPluginDescription");
91102
if (file == null) {
92103
this.server.getLogger().severe("null passed to getPluginDescription");
93104
return null;
@@ -107,12 +118,10 @@ public PluginDescriptionFile getPluginDescription(@NotNull File file) {
107118
@Override
108119
@NotNull
109120
public Pattern[] getPluginFileFilters() {
110-
this.server.getLogger().warning("getPluginFileFilters");
111121
return fileFilters;
112122
}
113123

114124
public static String getFileExtension() {
115-
System.out.println("getFileExtension");
116125
String os = System.getProperty("os.name").toLowerCase();
117126
if (os.contains("win")) {
118127
return ".so";
@@ -130,7 +139,6 @@ public static String getFileExtension() {
130139
@NotNull
131140
public Map<Class<? extends Event>, Set<RegisteredListener>> createRegisteredListeners(@NotNull Listener listener,
132141
@NotNull final Plugin plugin) {
133-
this.server.getLogger().warning("createRegisteredListeners");
134142
Preconditions.checkArgument(plugin != null, "Plugin can not be null");
135143
Preconditions.checkArgument(listener != null, "Listener can not be null");
136144

0 commit comments

Comments
 (0)