[Add][Change] Framework-API, Core 添加 FrameworkContext 用于向 Framework 传递与 Bot 有关的相关对象;

[Add] FrameworkContext, DefaultFrameworkContext 添加 FrameworkContext 和一个默认实现;
[Change] Framework, FrameworkFactory 调整代码以支持传递 FrameworkContext;
This commit is contained in:
2020-11-05 00:58:07 +08:00
parent 4a2337afd7
commit 2875426f72
4 changed files with 114 additions and 5 deletions

View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.framework;
import net.lamgc.cgj.bot.cache.CacheStoreBuilder;
import net.lamgc.cgj.bot.event.EventExecutor;
/**
* 框架上下文的默认实现.
* @author LamGC
*/
class DefaultFrameworkContext implements FrameworkContext {
private final EventExecutor eventExecutor;
private final CacheStoreBuilder cacheStoreBuilder;
public DefaultFrameworkContext(EventExecutor eventExecutor, CacheStoreBuilder cacheStoreBuilder) {
this.eventExecutor = eventExecutor;
this.cacheStoreBuilder = cacheStoreBuilder;
}
@Override
public EventExecutor getEventExecutor() {
return eventExecutor;
}
@Override
public CacheStoreBuilder getCacheStoreBuilder() {
return cacheStoreBuilder;
}
}

View File

@ -17,6 +17,8 @@
package net.lamgc.cgj.bot.framework;
import net.lamgc.cgj.bot.cache.CacheStoreBuilder;
import net.lamgc.cgj.bot.event.EventExecutor;
import org.pf4j.Plugin;
import org.pf4j.PluginFactory;
import org.pf4j.PluginWrapper;
@ -36,9 +38,13 @@ final class FrameworkFactory implements PluginFactory {
private final static Logger log = LoggerFactory.getLogger(FrameworkFactory.class);
private final File dataRootFolder;
private final CacheStoreBuilder cacheStoreBuilder;
private final EventExecutor eventExecutor;
public FrameworkFactory(File dataRootFolder) {
public FrameworkFactory(File dataRootFolder, CacheStoreBuilder cacheStoreBuilder, EventExecutor eventExecutor) {
this.dataRootFolder = dataRootFolder;
this.cacheStoreBuilder = cacheStoreBuilder;
this.eventExecutor = eventExecutor;
if (!this.dataRootFolder.exists() && !this.dataRootFolder.mkdirs()) {
log.warn("框架数据目录创建异常, 可能会导致后续框架存取数据失败!");
}
@ -67,9 +73,11 @@ final class FrameworkFactory implements PluginFactory {
try {
// <init>(PluginWrapper, DataFolder)
Constructor<?> constructor = pluginClass.getConstructor(PluginWrapper.class, File.class);
return (Plugin) constructor.newInstance(pluginWrapper,
new File(dataRootFolder, pluginWrapper.getPluginId()));
Constructor<?> constructor = pluginClass
.getConstructor(PluginWrapper.class, File.class, FrameworkContext.class);
return (Framework) constructor.newInstance(pluginWrapper,
new File(dataRootFolder, pluginWrapper.getPluginId()),
new DefaultFrameworkContext(eventExecutor, cacheStoreBuilder));
} catch (Exception e) {
log.error(e.getMessage(), e);
}