Skip to content

Commit

Permalink
[Patch]: Added support for absolute paths in development (#1130)
Browse files Browse the repository at this point in the history
closes #1124
  • Loading branch information
SigmundGranaas authored Dec 5, 2024
1 parent 5106405 commit 3a1e3e3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 57 deletions.
3 changes: 2 additions & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ subprojects {
api(project(":${rootProject.mod_id}-core"))

// Content modules

project(":content").subprojects.each { subproject ->
dependencies.add("runtimeOnly", project(path: ":content:${subproject.name}"))
}
Expand All @@ -37,6 +36,8 @@ dependencies {
include(project(":fabric:${rootProject.mod_id}-fabric-core"))
include(project(":fabric:minecraft-common"))

api(project(":fabric:${rootProject.mod_id}-fabric-core"))

// Fabric mod compat
include(project(":fabric:${rootProject.mod_id}-fabric-compat"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
public class Constant {
public static String CORE_PATH = "/data/forgero/core_test/";
public static String MINECRAFT_PACKAGE = "/data/forgero/packs/minecraft-material";
public static String MINECRAFT_VARIANTS = "/data/forgero/packs/minecraft-variants";
public static String VANILLA_PACKAGE = "/data/forgero/packs/forgero-vanilla";
public static String EXTENDED_PACKAGE = "/data/forgero/packs/forgero-extended";
public static String MINECRAFT_EXTENDED_PACKAGE = "/data/forgero/packs/minecraft-extended";
public static String TRINKETS_PACKAGE = "/data/forgero/packs/forgero-trinkets";
public static String SWORD_ADDITIONS_PACKAGE = "/data/forgero/packs/sword-additions";
public static String JSON_TEST_PATH = "/data/forgero/json_test/";
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,18 @@ public Optional<DataResource> loadResource(String path) {

private List<DataResource> rawResources(List<Path> paths) {
var resources = paths.stream()
.map(this::getFilePath)
.flatMap(Optional::stream)
.map(Path::toString)
.map(this::fileProvider)
.map(CompletableFuture::supplyAsync)
.toList();

var completedResources = resources.stream()
return resources.stream()
.map(CompletableFuture::join)
.flatMap(Optional::stream)
.toList();
return completedResources;
}

private FileResourceProvider fileProvider(String path) {
return new FileResourceProvider(path, streamLoader);
}

private Optional<String> getFilePath(Path path) {
String[] elements = path.toString().split("data");
if (elements.length == 2) {
return Optional.of( "data" + elements[1]);
}
Forgero.LOGGER.error("Unable to resolve path {}, as it could not be split using default split operator {}", path.toString(), File.separator);
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,39 @@
import com.sigmundgranaas.forgero.core.Forgero;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Optional;

public class ClassLoader implements InputStreamLoader {
private static final Logger logger = Forgero.LOGGER;

/**
* Strategies for loading resources
*/
public enum LoadStrategy {
CLASSLOADER, // Uses ClassLoader.getResourceAsStream()
CLASS // Uses Class.getResourceAsStream()
CLASSLOADER, CLASS
}

@Override
public Optional<InputStream> load(String location) {
// Default to CLASS strategy if path starts with "/", otherwise CLASSLOADER
LoadStrategy strategy = location.startsWith("/") ?
LoadStrategy.CLASS : LoadStrategy.CLASSLOADER;
return load(location, strategy);
try {
// Try direct file access first for development environment
if (isAbsolutePath(location)) {
File file = new File(location);
if (file.exists() && file.isFile()) {
return Optional.of(new FileInputStream(file));
}
// If absolute path doesn't exist as file, try to extract relative path
location = extractResourcePath(location);
}

LoadStrategy strategy = location.startsWith("/") ? LoadStrategy.CLASS : LoadStrategy.CLASSLOADER;
return load(location, strategy);
} catch (Exception e) {
logger.error("Failed to load resource: {}", location, e);
return Optional.empty();
}
}

/**
* Load a resource using a specific loading strategy.
*
* @param location The resource location
* @param strategy The loading strategy to use
* @return Optional containing the InputStream if found
*/
public Optional<InputStream> load(String location, LoadStrategy strategy) {
if (location == null || location.trim().isEmpty()) {
throw new IllegalArgumentException("Resource location cannot be null or empty");
Expand All @@ -53,37 +57,38 @@ public Optional<InputStream> load(String location, LoadStrategy strategy) {
}

private String normalizePath(String location, LoadStrategy strategy) {
String normalized = location.replace("\\", "/").trim();

switch (strategy) {
case CLASSLOADER:
// Remove leading slash for ClassLoader.getResourceAsStream()
normalized = normalized.replaceAll("^/+", "");
break;

case CLASS:
// Ensure leading slash for absolute paths in Class.getResourceAsStream()
if (!normalized.startsWith("/")) {
normalized = "/" + normalized;
}
break;
String normalized = location.replace('\\', '/').trim();

if (isAbsolutePath(normalized)) {
normalized = extractResourcePath(normalized);
}

return switch (strategy) {
case CLASSLOADER -> normalized.replaceAll("^/+", "");
case CLASS -> normalized.startsWith("/") ? normalized : "/" + normalized;
};
}

private String extractResourcePath(String absolutePath) {
String[] segments = absolutePath.split("/resources/");
if (segments.length == 2) {
return segments[1];
}
return absolutePath;
}

// Remove any duplicate slashes
return normalized.replaceAll("/+", "/");
private boolean isAbsolutePath(String path) {
return path.startsWith("/") && (path.contains("/resources/"));
}

private InputStream loadWithStrategy(String normalizedPath, LoadStrategy strategy) {
return switch (strategy) {
case CLASSLOADER -> this.getClass().getClassLoader()
.getResourceAsStream(normalizedPath);
case CLASS -> this.getClass()
.getResourceAsStream(normalizedPath);
case CLASSLOADER -> this.getClass().getClassLoader().getResourceAsStream(normalizedPath);
case CLASS -> this.getClass().getResourceAsStream(normalizedPath);
};
}

private InputStream tryAlternateStrategy(String normalizedPath, LoadStrategy strategy) {
// If one strategy fails, try the other
LoadStrategy alternateStrategy = (strategy == LoadStrategy.CLASS) ?
LoadStrategy.CLASSLOADER : LoadStrategy.CLASS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public static PipelineBuilder defaultResourcePipeLineTest() {
@Test
void loadResources() {
defaultResourcePipeLineTest().build().execute();
Assertions.assertTrue(ForgeroStateRegistry.stateFinder().find("forgero:oak-pickaxe").isPresent());
Assertions.assertTrue(ForgeroStateRegistry.stateFinder().find("minecraft:oak").isPresent());
}
}

0 comments on commit 3a1e3e3

Please sign in to comment.