Skip to content

Commit

Permalink
修复配置文件不保存的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Sep 6, 2024
1 parent 765e641 commit c63f0e6
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 95 deletions.
168 changes: 88 additions & 80 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

<groupId>com.ghostchu.peerbanhelper</groupId>
<artifactId>BiglyBT-PBH-Plugin</artifactId>
<version>1.2</version>
<!-- <packaging>takari-jar</packaging>-->
<version>1.2.1</version>
<!-- <packaging>takari-jar</packaging>-->
<packaging>jar</packaging>

<properties>
Expand All @@ -28,24 +28,24 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<!-- <skip>true</skip>-->
<!-- <skipMain>true</skipMain>-->
<!-- <skip>true</skip>-->
<!-- <skipMain>true</skipMain>-->
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- <plugin>-->
<!-- <groupId>io.takari.maven.plugins</groupId>-->
<!-- <artifactId>takari-lifecycle-plugin</artifactId>-->
<!-- <version>2.1.5</version>-->
<!-- <extensions>true</extensions>-->
<!-- <configuration>-->
<!-- <proc>proc</proc>-->
<!-- <source>${java.version}</source>-->
<!-- <target>${java.version}</target>-->
<!-- <sourceJar>true</sourceJar>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>io.takari.maven.plugins</groupId>-->
<!-- <artifactId>takari-lifecycle-plugin</artifactId>-->
<!-- <version>2.1.5</version>-->
<!-- <extensions>true</extensions>-->
<!-- <configuration>-->
<!-- <proc>proc</proc>-->
<!-- <source>${java.version}</source>-->
<!-- <target>${java.version}</target>-->
<!-- <sourceJar>true</sourceJar>-->
<!-- </configuration>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down Expand Up @@ -103,7 +103,21 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
<!-- <resources>-->
<!-- <resource>-->
<!-- <directory>src/main/resources</directory>-->
<!-- <filtering>true</filtering>-->
<!-- <includes>-->
<!-- <include>*</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!-- </resources>-->
</build>

<dependencies>
Expand All @@ -130,12 +144,6 @@
<version>24.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bspfsystems</groupId>
<artifactId>yamlconfiguration</artifactId>
<version>2.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -147,68 +155,68 @@
<artifactId>slf4j-simple</artifactId>
<version>2.0.10</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.biglybt</groupId>-->
<!-- <artifactId>biglybt-core</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- <version>3.4.0.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.biglybt</groupId>-->
<!-- <artifactId>biglybt-ui</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- <version>3.4.0.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.biglybt</groupId>-->
<!-- <artifactId>biglybt-core</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- <version>3.4.0.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.biglybt</groupId>-->
<!-- <artifactId>biglybt-ui</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- <version>3.4.0.1</version>-->
<!-- </dependency>-->
</dependencies>


<profiles>
<!-- <profile>-->
<!-- <id>mac-swt</id>-->
<!-- <activation>-->
<!-- <os>-->
<!-- <family>mac</family>-->
<!-- </os>-->
<!-- </activation>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.swt</groupId>-->
<!-- <artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </profile>-->
<!-- <profile>-->
<!-- <id>linux-swt</id>-->
<!-- <activation>-->
<!-- <os>-->
<!-- <family>linux</family>-->
<!-- </os>-->
<!-- </activation>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.swt</groupId>-->
<!-- <artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </profile>-->
<!-- <profile>-->
<!-- <id>mac-swt</id>-->
<!-- <activation>-->
<!-- <os>-->
<!-- <family>mac</family>-->
<!-- </os>-->
<!-- </activation>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.swt</groupId>-->
<!-- <artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </profile>-->
<!-- <profile>-->
<!-- <id>linux-swt</id>-->
<!-- <activation>-->
<!-- <os>-->
<!-- <family>linux</family>-->
<!-- </os>-->
<!-- </activation>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.swt</groupId>-->
<!-- <artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </profile>-->

<!-- <profile>-->
<!-- <id>windows-swt</id>-->
<!-- <activation>-->
<!-- <os>-->
<!-- <family>windows</family>-->
<!-- </os>-->
<!-- </activation>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.swt</groupId>-->
<!-- <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>-->
<!-- <version>4.9</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </profile>-->
<!-- <profile>-->
<!-- <id>windows-swt</id>-->
<!-- <activation>-->
<!-- <os>-->
<!-- <family>windows</family>-->
<!-- </os>-->
<!-- </activation>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.eclipse.swt</groupId>-->
<!-- <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>-->
<!-- <version>4.9</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </profile>-->
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.biglybt.core.config.COConfigurationManager;
import com.biglybt.core.networkmanager.Transport;
import com.biglybt.pif.PluginException;
import com.biglybt.pif.PluginInterface;
import com.biglybt.pif.UnloadablePlugin;
import com.biglybt.pif.*;
import com.biglybt.pif.config.PluginConfigSource;
import com.biglybt.pif.download.Download;
import com.biglybt.pif.download.DownloadException;
import com.biglybt.pif.download.DownloadStats;
Expand All @@ -15,6 +14,8 @@
import com.biglybt.pif.tag.Tag;
import com.biglybt.pif.torrent.Torrent;
import com.biglybt.pif.ui.config.IntParameter;
import com.biglybt.pif.ui.config.Parameter;
import com.biglybt.pif.ui.config.ParameterListener;
import com.biglybt.pif.ui.config.StringParameter;
import com.biglybt.pif.ui.model.BasicPluginConfigModel;
import com.biglybt.pifimpl.local.peers.PeerImpl;
Expand All @@ -28,13 +29,14 @@
import io.javalin.Javalin;
import io.javalin.http.Context;
import io.javalin.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

@Slf4j
public class Plugin implements UnloadablePlugin {
public static final Gson GSON = new Gson();
private static final String PBH_IDENTIFIER = "<PeerBanHelper>";
Expand All @@ -46,6 +48,8 @@ public class Plugin implements UnloadablePlugin {
private JavalinWebContainer webContainer;
private int port;
private String token;
private PluginConfig cfg;
private PluginConfigSource configSource;

private static TorrentRecord getTorrentRecord(Torrent torrent) {
if (torrent == null) return null;
Expand Down Expand Up @@ -137,20 +141,31 @@ public void unload() throws PluginException {
@Override
public void initialize(PluginInterface pluginInterface) {
this.pluginInterface = pluginInterface;
this.cfg = pluginInterface.getPluginconfig();
this.port = cfg.getPluginIntParameter("web.port",7759);
this.token = cfg.getPluginStringParameter("web.token", UUID.randomUUID().toString());
configModel = pluginInterface.getUIManager().createBasicPluginConfigModel("peerbanhelper.configui");
listenPortParam = configModel.addIntParameter2("api-port", "peerbanhelper.port", 7756);
accessKeyParam = configModel.addStringParameter2("api-token", "peerbanhelper.token", UUID.randomUUID().toString());
this.port = listenPortParam.getValue();
this.token = accessKeyParam.getValue();
listenPortParam.addListener(parameter -> {
listenPortParam = configModel.addIntParameter2("api-port", "peerbanhelper.port", port);
listenPortParam.addListener(lis-> {
this.port = listenPortParam.getValue();
reloadPlugin();
saveAndReload();
});

accessKeyParam.addListener(parameter -> {
accessKeyParam = configModel.addStringParameter2("api-token", "peerbanhelper.token", token);
accessKeyParam.addListener(lis-> {
this.token = accessKeyParam.getValue();
reloadPlugin();
saveAndReload();
});
saveAndReload();
}

private void saveAndReload() {
cfg.setPluginParameter("web.token", token);
cfg.setPluginParameter("web.port", port);
try {
this.cfg.save();
}catch (Exception e){
throw new RuntimeException(e);
}
reloadPlugin();
}

Expand All @@ -159,7 +174,12 @@ private void reloadPlugin() {
webContainer.stop();
}
webContainer = new JavalinWebContainer();
webContainer.start("0.0.0.0", port, token);
webContainer.start("0.0.0.0",
port,
token);
log.info("PBH-Adapter WebServer started with: port={}, token={}",
port,
token);
initEndpoints(webContainer.javalin());
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ plugin.class=com.ghostchu.peerbanhelper.downloaderplug.biglybt.Plugin
plugin.name=PeerBanHelper-Adapter
plugin.langfile=com.ghostchu.peerbanhelper.downloaderplug.biglybt.Messages
plugin.id=peerbanhelperadatper
plugin.version=1.0
plugin.version=1.2.3

0 comments on commit c63f0e6

Please sign in to comment.