Skip to content

Commit 3832170

Browse files
Fix uninstall logic
1 parent e188e66 commit 3832170

File tree

6 files changed

+60
-39
lines changed

6 files changed

+60
-39
lines changed

src/main/java/org/hydev/mcpm/client/loader/UnloadBoundary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public interface UnloadBoundary
1313
* @param name Loaded plugin name
1414
* @throws PluginNotFoundException If a loaded plugin of the name isn't found
1515
*/
16-
void unloadPlugin(String name) throws PluginNotFoundException;
16+
File unloadPlugin(String name) throws PluginNotFoundException;
1717
}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
package org.hydev.mcpm.client.uninstall;
22

3-
import org.hydev.mcpm.client.injector.LocalJarBoundary;
4-
import org.hydev.mcpm.client.injector.PluginNotFoundException;
5-
6-
import java.io.File;
7-
8-
import static org.hydev.mcpm.client.uninstall.UninstallResult.State.FAILED_TO_DELETE;
9-
import static org.hydev.mcpm.client.uninstall.UninstallResult.State.NOT_FOUND;
10-
113
/**
124
* Interface for removing a file
135
*/
@@ -18,6 +10,5 @@ public interface FileRemove {
1810
* @param pluginName
1911
* @return int 0 - true, 1 - NOT_FOUND, 2 - FAILED_TO_DELETE
2012
*/
21-
int removeFile(String pluginName);
22-
13+
FileRemoveResult removeFile(String pluginName);
2314
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.hydev.mcpm.client.uninstall;
2+
3+
/**
4+
* Result for removing a file
5+
*/
6+
public enum FileRemoveResult
7+
{
8+
SUCCESS,
9+
NOT_FOUND,
10+
FAILED_TO_DELETE
11+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
package org.hydev.mcpm.client.uninstall;
22

3-
import org.hydev.mcpm.client.injector.LocalJarBoundary;
4-
import org.hydev.mcpm.client.injector.PluginNotFoundException;
5-
import org.hydev.mcpm.client.uninstall.FileRemove;
3+
import org.hydev.mcpm.client.loader.LocalJarBoundary;
4+
import org.hydev.mcpm.client.loader.PluginNotFoundException;
65

76
import java.io.File;
87

98
/**
109
* Removes file for FileRemove.java
1110
*/
12-
public class RemoveFile implements FileRemove{
11+
public class PluginRemover implements FileRemove{
1312
private final LocalJarBoundary jarFinder;
1413

15-
public RemoveFile(LocalJarBoundary jarFinder) {
14+
public PluginRemover(LocalJarBoundary jarFinder) {
1615
this.jarFinder = jarFinder;
1716
}
1817

19-
public int removeFile(String pluginName) {
18+
@Override
19+
public FileRemoveResult removeFile(String pluginName) {
2020
// 2. If it isn't loaded, find the plugin jar file in local file system
2121
// (This will throw PluginNotFoundException when a plugin of the name in the file system
2222
// could not be found).
2323
File jar = null;
2424
try {
2525
jar = jarFinder.findJar(pluginName);
2626
} catch (PluginNotFoundException e) {
27-
return 1;
27+
return FileRemoveResult.NOT_FOUND;
2828
}
2929

3030
// 3. Delete plugin jar
3131
if (!jar.delete()) {
32-
return 2;
32+
return FileRemoveResult.FAILED_TO_DELETE;
3333
}
34-
return 0;
34+
return FileRemoveResult.SUCCESS;
3535
}
3636
}

src/main/java/org/hydev/mcpm/client/uninstall/UninstallInput.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
*
66
* @param name Plugin name
77
* @param recursive remove dependencies or not
8+
* @param delete Whether to delete the file after unloading
89
*/
9-
public record UninstallInput(
10+
public record UninstallInput(
1011
String name,
11-
boolean recursive
12-
) { }
12+
boolean recursive,
13+
boolean delete
14+
)
15+
{
16+
/**
17+
* Default constructor with delete=true
18+
*
19+
* @param name Plugin name
20+
* @param recursive remove dependencies or not
21+
*/
22+
public UninstallInput(String name, boolean recursive)
23+
{
24+
this(name, recursive, true);
25+
}
26+
}
1327

1428

src/main/java/org/hydev/mcpm/client/uninstall/Uninstaller.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package org.hydev.mcpm.client.uninstall;
22

33
import org.hydev.mcpm.client.database.tracker.PluginTracker;
4-
import org.hydev.mcpm.client.injector.LocalJarBoundary;
5-
import org.hydev.mcpm.client.injector.PluginNotFoundException;
6-
import org.hydev.mcpm.client.injector.UnloadBoundary;
7-
import org.hydev.mcpm.client.uninstall.FileRemove;
84
import org.hydev.mcpm.client.loader.LocalJarBoundary;
95
import org.hydev.mcpm.client.loader.PluginNotFoundException;
106
import org.hydev.mcpm.client.loader.UnloadBoundary;
117

12-
import java.io.File;
13-
148
import static org.hydev.mcpm.client.uninstall.UninstallResult.State.*;
159

1610

@@ -45,21 +39,32 @@ public UninstallResult uninstall(UninstallInput input) {
4539
// found error here.
4640
if (unloader != null) {
4741
try {
48-
unloader.unloadPlugin(input.name());
42+
// In the minecraft server environment, we can find the exact jar of the loaded
43+
// plugin because java's url class loader keeps track of jar file paths.
44+
var jar = unloader.unloadPlugin(input.name());
45+
46+
// Delete file
47+
if (input.delete() && !jar.delete()) {
48+
return new UninstallResult(FAILED_TO_DELETE);
49+
}
4950
}
5051
catch (PluginNotFoundException ignored) { }
5152
}
52-
53-
// Steps 2. + 3.
54-
RemoveFile fileRemover = new RemoveFile(jarFinder);
55-
56-
if (fileRemover.removeFile(input.name())==1) {
57-
return new UninstallResult(NOT_FOUND);
58-
} else if (fileRemover.removeFile(input.name())==2) {
59-
return new UninstallResult(FAILED_TO_DELETE);
53+
else if (input.delete())
54+
{
55+
// When unloader is not null, it means that we are in CLI environment, so we need to
56+
// find the plugin of the name
57+
PluginRemover fileRemover = new PluginRemover(jarFinder);
58+
59+
// Delete file
60+
var deleteResult = fileRemover.removeFile(input.name());
61+
if (deleteResult == FileRemoveResult.NOT_FOUND) {
62+
return new UninstallResult(NOT_FOUND);
63+
} else if (deleteResult == FileRemoveResult.FAILED_TO_DELETE) {
64+
return new UninstallResult(FAILED_TO_DELETE);
65+
}
6066
}
6167

62-
6368
// 4. Remove manually installed flag from the tracker
6469
tracker.removeEntry(input.name());
6570
var result = new UninstallResult(SUCCESS);

0 commit comments

Comments
 (0)