Skip to content

Commit

Permalink
modify: add plugin property to BotManager
Browse files Browse the repository at this point in the history
  • Loading branch information
MidCoard committed Mar 16, 2022
1 parent 95ae39e commit 6139359
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ Maven 依赖
<dependency>
<groupId>top.focess</groupId>
<artifactId>focess-qq</artifactId>
<version>4.0.4.2000</version>
<version>4.0.6.2000</version>
</dependency>
```

Gradle 依赖
```gradle
implementation 'top.focess:focess-qq:4.0.4.2000'
implementation 'top.focess:focess-qq:4.0.6.2000'
```

开发文档移步本项目[Wiki](https://github.com/MIdCoard/MiraiQQ/wiki)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/top/focess/qq/FocessQQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public void enable() {
requestAccountInformation();
FocessQQ.getLogger().debugLang("request-account-information");
}
bot = getBotManager().loginDirectly(username,password);
bot = getBotManager().loginDirectly(username,password,this);
FocessQQ.getLogger().debugLang("login-default-bot");
File plugins = new File("plugins");
if (plugins.exists() && options.get("noDefaultPluginLoad") == null)
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/top/focess/qq/api/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.mamoe.mirai.contact.Group;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.focess.qq.api.plugin.Plugin;

import java.util.List;

Expand Down Expand Up @@ -109,4 +110,11 @@ public interface Bot {
* @return true if this is the default bot, false otherwise
*/
boolean isDefaultBot();

/**
* Get the plugin of the bot
*
* @return the plugin of the bot
*/
Plugin getPlugin();
}
7 changes: 5 additions & 2 deletions src/main/java/top/focess/qq/api/bot/BotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.focess.qq.api.exceptions.BotLoginException;
import top.focess.qq.api.plugin.Plugin;

import java.util.List;
import java.util.concurrent.Future;
Expand All @@ -17,21 +18,23 @@ public interface BotManager {
*
* @param id the id of the bot
* @param password the password of the bot
* @param plugin the plugin
* @return the bot
*/
@NotNull Future<Bot> login(long id,String password);
@NotNull Future<Bot> login(long id,String password,Plugin plugin);

/**
* Login and get the bot with id and password
* This is a blocking method
*
* @param id the id of the bot
* @param password the password of the bot
* @param plugin the plugin
* @throws BotLoginException if the bot login failed
* @return the bot
*/
@NotNull
Bot loginDirectly(long id, String password);
Bot loginDirectly(long id, String password, Plugin plugin);

/**
* Login the bot
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/top/focess/qq/core/bot/SimpleBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.Nullable;
import top.focess.qq.FocessQQ;
import top.focess.qq.api.bot.Bot;
import top.focess.qq.api.plugin.Plugin;

import java.util.List;
import java.util.Objects;
Expand All @@ -15,12 +16,14 @@ public class SimpleBot implements Bot {

private final long username;
private final String password;
private final Plugin plugin;
private net.mamoe.mirai.Bot nativeBot;

public SimpleBot(long username, String password, net.mamoe.mirai.Bot bot) {
public SimpleBot(long username, String password, net.mamoe.mirai.Bot bot, Plugin plugin) {
this.username = username;
this.password = password;
this.nativeBot = bot;
this.plugin = plugin;
}

@Override
Expand Down Expand Up @@ -116,4 +119,8 @@ public boolean equals(Object o) {
public int hashCode() {
return nativeBot != null ? nativeBot.hashCode() : 0;
}

public Plugin getPlugin() {
return plugin;
}
}
24 changes: 19 additions & 5 deletions src/main/java/top/focess/qq/core/bot/SimpleBotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import top.focess.qq.api.event.request.GroupRequestEvent;
import top.focess.qq.api.exceptions.BotLoginException;
import top.focess.qq.api.exceptions.EventSubmitException;
import top.focess.qq.api.plugin.Plugin;
import top.focess.qq.api.schedule.Scheduler;
import top.focess.qq.api.schedule.Schedulers;
import top.focess.qq.api.util.IOHandler;
Expand All @@ -44,6 +45,8 @@ public class SimpleBotManager implements BotManager {

private static final Map<Bot,List<Listener<?>>> BOT_LISTENER_MAP = Maps.newHashMap();

private static final Map<Plugin,List<Bot>> PLUGIN_BOT_MAP = Maps.newHashMap();

private static final Map<Long,Bot> BOTS = Maps.newConcurrentMap();

public SimpleBotManager() {
Expand All @@ -54,13 +57,12 @@ public SimpleBotManager() {
}

@Override
public @NotNull Future<Bot> login(long id, String password) {
//todo add plugin argument
return SCHEDULER.submit(() -> loginDirectly(id,password));
public @NotNull Future<Bot> login(long id, String password,Plugin plugin) {
return SCHEDULER.submit(() -> loginDirectly(id,password,plugin));
}

@Override
public @NotNull Bot loginDirectly(long id, String password) {
public @NotNull Bot loginDirectly(long id, String password, Plugin plugin) {
BotConfiguration configuration = BotConfiguration.getDefault();
configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
File cache = new File("devices/" + id + "/cache");
Expand Down Expand Up @@ -107,7 +109,7 @@ public Object onSolveUnsafeDeviceLoginVerify(@NotNull net.mamoe.mirai.Bot bot, @
} catch(Exception e) {
throw new BotLoginException(id,e);
}
Bot b = new SimpleBot(id,password, bot);
Bot b = new SimpleBot(id,password, bot,plugin);
try {
EventManager.submit(new BotLoginEvent(b));
} catch (EventSubmitException e) {
Expand Down Expand Up @@ -187,6 +189,12 @@ public Object onSolveUnsafeDeviceLoginVerify(@NotNull net.mamoe.mirai.Bot bot, @
}
}));
BOT_LISTENER_MAP.put(b,listeners);
PLUGIN_BOT_MAP.compute(plugin,(k,v)->{
if (v == null)
v = Lists.newArrayList();
v.add(b);
return v;
});
BOTS.put(id,b);
return b;
}
Expand Down Expand Up @@ -383,5 +391,11 @@ public static void removeAll() {
b.logout();
}

public static void remove(Plugin plugin) {
for (Bot b : PLUGIN_BOT_MAP.getOrDefault(plugin,Lists.newArrayList()))
FocessQQ.getBotManager().remove(b.getId());
PLUGIN_BOT_MAP.remove(plugin);
}


}
7 changes: 4 additions & 3 deletions src/main/java/top/focess/qq/core/commands/BotCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ public void init() {
ioHandler.outputLang("bot-command-bot-not-exist",id);
return CommandResult.REFUSE;
}
bot.relogin();
ioHandler.outputLang("bot-command-relogin-succeed",bot.getId());
if (bot.relogin())
ioHandler.outputLang("bot-command-relogin-succeed",bot.getId());
else ioHandler.outputLang("bot-command-relogin-failed",bot.getId());
return CommandResult.ALLOW;
},CommandArgument.of("relogin"),CommandArgument.ofLong());
this.addExecutor((sender, dataCollection, ioHandler) -> {
long id = dataCollection.getLong();
Bot bot = FocessQQ.getBotManager().getBot(id);
if (bot == null) {
try {
FocessQQ.getBotManager().login(id, dataCollection.get());
FocessQQ.getBotManager().login(id, dataCollection.get(), FocessQQ.getMainPlugin());
} catch (BotLoginException e) {
ioHandler.outputLang("bot-command-login-failed",id);
return CommandResult.REFUSE;
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/top/focess/qq/core/plugin/PluginClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import top.focess.qq.api.schedule.Task;
import top.focess.qq.api.util.version.Version;
import top.focess.qq.api.util.yaml.YamlConfiguration;
import top.focess.qq.core.bot.SimpleBotManager;
import top.focess.qq.core.debug.Section;

import java.io.File;
Expand Down Expand Up @@ -169,7 +170,8 @@ else if (e instanceof CommandLoadException)
});
}

private static final Scheduler SCHEDULER = Schedulers.newThreadPoolScheduler(FocessQQ.getMainPlugin(),2,false,"EnablePlugin");
private static final Scheduler SCHEDULER = Schedulers.newThreadPoolScheduler(FocessQQ.getMainPlugin(),2,false,"PluginLoader");
private static final Scheduler GC_SCHEDULER = Schedulers.newFocessScheduler(FocessQQ.getMainPlugin(),"GC");

/**
* Used to enable plugin
Expand Down Expand Up @@ -228,6 +230,7 @@ public static File disablePlugin(Plugin plugin) {
file = callback.waitCall();
} catch (InterruptedException | ExecutionException | CancellationException ignored) {}
section.stop();
GC_SCHEDULER.run(System::gc,Duration.ofSeconds(1));
return file;
}

Expand All @@ -248,6 +251,8 @@ public static File disablePlugin0(Plugin plugin) {
FocessQQ.getLogger().debugLang("unregister-commands");
Schedulers.close(plugin);
FocessQQ.getLogger().debugLang("close-schedulers");
SimpleBotManager.remove(plugin);
FocessQQ.getLogger().debugLang("remove-bot");
if (FocessQQ.getSocket() != null)
FocessQQ.getSocket().unregister(plugin);
if (FocessQQ.getUdpSocket() != null)
Expand Down Expand Up @@ -411,6 +416,11 @@ public boolean load() {
DataCollection.unregister(plugin);
Command.unregister(plugin);
Schedulers.close(plugin);
SimpleBotManager.remove(plugin);
if (FocessQQ.getSocket() != null)
FocessQQ.getSocket().unregister(plugin);
if (FocessQQ.getUdpSocket() != null)
FocessQQ.getUdpSocket().unregister(plugin);
}
PluginCoreClassLoader.LOADERS.remove(this);
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ bot-command-relogin-succeed: Bot %d relogin successfully
bot-command-bot-exist: Bot %d already exists
bot-command-login-failed: Bot %d failed to login
bot-command-remove-succeed: Bot %d removed successfully
bot-command-relogin-failed: Bot %d failed to relogin
command-command-unload: Unload command %s
command-command-list: "The following commands are :"
command-command-no-command: There is no command
Expand Down Expand Up @@ -171,4 +172,5 @@ socket-packet-handler-not-empty: Socket packet handlers are not empty
udp-socket-packet-handler-not-empty: UDP socket packet handlers are not empty
debug-section-timeout: Section %s is timeout
plugin-unload-self: "%s unload itself"
remove-bot: Remove bot

0 comments on commit 6139359

Please sign in to comment.