Skip to content

Commit

Permalink
new: ResourceInterceptorPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
1fxe committed Dec 7, 2020
1 parent 96dfcf1 commit 0532838
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main/java/me/coley/recaf/command/impl/LoadWorkspace.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.coley.recaf.command.impl;

import me.coley.recaf.command.completion.*;
import me.coley.recaf.plugin.PluginsManager;
import me.coley.recaf.plugin.api.ResourceInterceptorPlugin;
import me.coley.recaf.util.IOUtil;
import me.coley.recaf.util.LangUtil;
import me.coley.recaf.util.ShortcutUtil;
Expand All @@ -12,6 +14,7 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;

import static me.coley.recaf.util.Log.*;

Expand Down Expand Up @@ -41,20 +44,27 @@ public class LoadWorkspace implements Callable<Workspace> {
public Workspace call() throws Exception {
status = LangUtil.translate("ui.load.resolve");
String name = input.getFileName().toString().toLowerCase();
String ext = IOUtil.getExtension(input);
AtomicReference<String> ext = new AtomicReference<>(IOUtil.getExtension(input));
// Handle symbolic links
int symLevel = 0;
if (ShortcutUtil.isPotentialValidLink(input)) {
input = Paths.get(new ShortcutUtil(input).getRealFilename());
name = input.getFileName().toString().toLowerCase();
ext = name.substring(name.lastIndexOf(".") + 1);
ext.set(name.substring(name.lastIndexOf(".") + 1));
}
while (Files.isSymbolicLink(input) && symLevel < 5) {
input = Files.readSymbolicLink(input);
symLevel++;
}
JavaResource resource = null;
switch(ext) {

final String resourceName = name;
final String resourceExt = ext.get();

PluginsManager.getInstance().ofType(ResourceInterceptorPlugin.class)
.forEach(plugin -> ext.set(plugin.onLoad(resourceName, resourceExt)));

JavaResource resource;
switch(ext.get()) {
case "class":
status = LangUtil.translate("ui.load.initialize.resource");
resource = new ClassResource(input);
Expand All @@ -70,7 +80,7 @@ public Workspace call() throws Exception {
case "json":
status = LangUtil.translate("ui.load.initialize.workspace");
// Represents an already existing workspace, so we can parse and return that here
Workspace workspace = null;
Workspace workspace;
try {
workspace = WorkspaceIO.fromJson(input);
} catch(Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.coley.recaf.plugin.api;

/**
* Allows loading of custom resource
*
* @author Filip
*/
public interface ResourceInterceptorPlugin extends BasePlugin {

/**
* @param name resource name
* @param ext resource extension
* @return the actual extension of the resource
*/
String onLoad(String name, String ext);

}

0 comments on commit 0532838

Please sign in to comment.