Skip to content

Commit

Permalink
In resource importer, use WorkspaceFileResourceBuilder initially sinc…
Browse files Browse the repository at this point in the history
…e all cases will eventually supply a FileInfo

This skips copying state when converting from a regular resource builder to a file backed resource builder.
  • Loading branch information
Col-E committed Feb 9, 2024
1 parent a15e7f0 commit 6060aa0
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import software.coley.recaf.util.io.ByteSources;
import software.coley.recaf.util.io.LocalFileHeaderSource;
import software.coley.recaf.workspace.model.bundle.*;
import software.coley.recaf.workspace.model.resource.WorkspaceDirectoryResource;
import software.coley.recaf.workspace.model.resource.WorkspaceFileResource;
import software.coley.recaf.workspace.model.resource.WorkspaceResource;
import software.coley.recaf.workspace.model.resource.WorkspaceResourceBuilder;
import software.coley.recaf.workspace.model.resource.*;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -60,14 +57,11 @@ public BasicResourceImporter(@Nonnull InfoImporter infoImporter,
*
* @return Read resource.
*/
private WorkspaceResource handleSingle(WorkspaceResourceBuilder builder,
private WorkspaceResource handleSingle(WorkspaceFileResourceBuilder builder,
String pathName, ByteSource source) throws IOException {
// Read input as raw info in order to determine file-type.
Info readInfo = infoImporter.readInfo(pathName.substring(pathName.lastIndexOf('/') + 1), source);

// Associate input path with the read value.
InputFilePathProperty.set(readInfo, Paths.get(pathName));

// Check if it is a single class.
if (readInfo.isClass()) {
// If it is a class, we know it MUST be a single JVM class since Android classes do not exist
Expand All @@ -82,11 +76,15 @@ private WorkspaceResource handleSingle(WorkspaceResourceBuilder builder,
.withName(readAsJvmClass.getName() + ".class")
.withRawContent(readAsJvmClass.getBytecode())
.build();
InputFilePathProperty.set(fileInfo, Paths.get(pathName)); // Associate input path with the read value.
return builder.withFileInfo(fileInfo)
.withJvmClassBundle(bundle)
.build();
}

// Associate input path with the read value.
InputFilePathProperty.set(readInfo, Paths.get(pathName));

// Must be some non-class type of file.
FileInfo readInfoAsFile = readInfo.asFile();
builder = builder.withFileInfo(readInfoAsFile);
Expand Down Expand Up @@ -118,7 +116,7 @@ private WorkspaceResource handleSingle(WorkspaceResourceBuilder builder,
.build();
}

private WorkspaceFileResource handleZip(WorkspaceResourceBuilder builder, ZipFileInfo zipInfo, ByteSource source) throws IOException {
private WorkspaceFileResource handleZip(WorkspaceFileResourceBuilder builder, ZipFileInfo zipInfo, ByteSource source) throws IOException {
logger.info("Reading input from ZIP container '{}'", zipInfo.getName());
builder.withFileInfo(zipInfo);
BasicJvmClassBundle classes = new BasicJvmClassBundle();
Expand Down Expand Up @@ -360,7 +358,7 @@ private void addInfo(BasicJvmClassBundle classes,
// Check for container file cases (Any ZIP type, JAR/WAR/etc)
if (fileInfo.isZipFile()) {
try {
WorkspaceResourceBuilder embeddedResourceBuilder = new WorkspaceResourceBuilder()
WorkspaceFileResourceBuilder embeddedResourceBuilder = new WorkspaceFileResourceBuilder()
.withFileInfo(fileInfo);
WorkspaceFileResource embeddedResource = handleZip(embeddedResourceBuilder,
fileInfo.asZipFile(), infoSource);
Expand Down Expand Up @@ -554,7 +552,7 @@ private WorkspaceResource handleModules(WorkspaceResourceBuilder builder, Module
@Nonnull
@Override
public WorkspaceResource importResource(@Nonnull ByteSource source) throws IOException {
return handleSingle(new WorkspaceResourceBuilder(), "unknown.dat", source);
return handleSingle(new WorkspaceFileResourceBuilder(), "unknown.dat", source);
}

@Nonnull
Expand All @@ -563,10 +561,10 @@ public WorkspaceResource importResource(@Nonnull Path path) throws IOException {
// Load name/data from path, parse into resource.
String absolutePath = StringUtil.pathToAbsoluteString(path);
if (Files.isDirectory(path)) {
return handleDirectory(new WorkspaceResourceBuilder(), path);
return handleDirectory(new WorkspaceFileResourceBuilder(), path);
} else {
ByteSource byteSource = ByteSources.forPath(path);
return handleSingle(new WorkspaceResourceBuilder(), absolutePath, byteSource);
return handleSingle(new WorkspaceFileResourceBuilder(), absolutePath, byteSource);
}
}

Expand All @@ -583,7 +581,7 @@ public WorkspaceResource importResource(@Nonnull URL url) throws IOException {
// Load content, parse into resource.
byte[] bytes = IOUtil.toByteArray(url.openStream());
ByteSource byteSource = ByteSources.wrap(bytes);
return handleSingle(new WorkspaceResourceBuilder(), path, byteSource);
return handleSingle(new WorkspaceFileResourceBuilder(), path, byteSource);
}

@Nonnull
Expand Down

0 comments on commit 6060aa0

Please sign in to comment.