-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add template factory to help with template provisioning
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
- Loading branch information
1 parent
7224841
commit 0a26253
Showing
8 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...src/main/java/com/blamejared/contenttweaker/core/api/resource/ResourceTemplateHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.blamejared.contenttweaker.core.api.resource; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
public interface ResourceTemplateHelper { | ||
@FunctionalInterface | ||
interface Provider { | ||
void provide(final String path, final byte[] data, final ResourceSerializer<byte[]> serializer); | ||
} | ||
|
||
void provideFrom(final String templatePath, final String fragmentPath, final Provider provider); | ||
void provideFrom(final Path templatePath, final String fragmentPath, final Provider provider); | ||
|
||
default void provideFrom(final String templatePath, final String fragmentPath, final ResourceFragment fragment) { | ||
this.provideFrom(templatePath, fragmentPath, Objects.requireNonNull(fragment)::provideFixed); | ||
} | ||
|
||
default void provideFrom(final Path templatePath, final String fragmentPath, final ResourceFragment fragment) { | ||
this.provideFrom(templatePath, fragmentPath, Objects.requireNonNull(fragment)::provideFixed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Common/src/main/java/com/blamejared/contenttweaker/core/resource/TemplateHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.blamejared.contenttweaker.core.resource; | ||
|
||
import com.blamejared.contenttweaker.core.api.resource.ResourceTemplateHelper; | ||
import com.blamejared.contenttweaker.core.api.resource.StandardResourceSerializers; | ||
import com.blamejared.contenttweaker.core.service.ServiceManager; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.lang.annotation.Target; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
final class TemplateHelper implements ResourceTemplateHelper { | ||
private TemplateHelper() {} | ||
|
||
static TemplateHelper of() { | ||
return new TemplateHelper(); | ||
} | ||
|
||
@Override | ||
public void provideFrom(final String templatePath, final String fragmentPath, final Provider provider) { | ||
Objects.requireNonNull(templatePath); | ||
Objects.requireNonNull(fragmentPath); | ||
Objects.requireNonNull(provider); | ||
try { | ||
final Path base = ServiceManager.platform().locateResource("meta", "template"); | ||
final Path target = base.resolve(templatePath); | ||
this.provideFromChecked(target, fragmentPath, provider); | ||
} catch (final IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void provideFrom(final Path templatePath, final String fragmentPath, final Provider provider) { | ||
Objects.requireNonNull(templatePath); | ||
Objects.requireNonNull(fragmentPath); | ||
Objects.requireNonNull(provider); | ||
try { | ||
this.provideFromChecked(templatePath, fragmentPath, provider); | ||
} catch (final IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private void provideFromChecked(final Path templatePath, final String fragmentPath, final Provider provider) throws IOException { | ||
if (!Files.exists(templatePath)) { | ||
throw new IOException("Unable to read template " + templatePath + " as it does not exist"); | ||
} | ||
final byte[] bytes = Files.readAllBytes(templatePath); | ||
provider.provide(fragmentPath, bytes, StandardResourceSerializers.BYTE_ARRAY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
|
||
public interface PlatformService { | ||
Path gameDirectory(); | ||
Path locateResource(final String... components); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
Fabric/src/main/java/com/blamejared/contenttweaker/fabric/service/FabricPlatformService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
package com.blamejared.contenttweaker.fabric.service; | ||
|
||
import com.blamejared.contenttweaker.core.api.ContentTweakerConstants; | ||
import com.blamejared.contenttweaker.core.service.PlatformService; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public final class FabricPlatformService implements PlatformService { | ||
@Override | ||
public Path gameDirectory() { | ||
return FabricLoader.getInstance().getGameDir(); | ||
} | ||
|
||
@Override | ||
public Path locateResource(final String... components) { | ||
Objects.requireNonNull(components); | ||
if (components.length < 1) { | ||
throw new IllegalArgumentException("At least one component required"); | ||
} | ||
final List<Path> possiblePaths = FabricLoader.getInstance() | ||
.getModContainer(ContentTweakerConstants.MOD_ID) | ||
.orElseThrow(IllegalStateException::new) | ||
.getRootPaths() | ||
.stream() | ||
.map(it -> it.resolve(String.join("/", components))) | ||
.toList(); | ||
return possiblePaths.size() == 1? possiblePaths.get(0) : possiblePaths.stream() | ||
.filter(Files::exists) | ||
.findFirst() | ||
.orElse(possiblePaths.get(0)); // Guaranteed at least one possible root path | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Forge/src/main/java/com/blamejared/contenttweaker/forge/service/ForgePlatformService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
package com.blamejared.contenttweaker.forge.service; | ||
|
||
import com.blamejared.contenttweaker.core.api.ContentTweakerConstants; | ||
import com.blamejared.contenttweaker.core.service.PlatformService; | ||
import net.minecraftforge.fml.ModList; | ||
import net.minecraftforge.fml.loading.FMLPaths; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
public final class ForgePlatformService implements PlatformService { | ||
@Override | ||
public Path gameDirectory() { | ||
return FMLPaths.GAMEDIR.get(); | ||
} | ||
|
||
@Override | ||
public Path locateResource(final String... components) { | ||
Objects.requireNonNull(components); | ||
if (components.length < 1) { | ||
throw new IllegalArgumentException("At least one component required"); | ||
} | ||
return ModList.get() | ||
.getModFileById(ContentTweakerConstants.MOD_ID) | ||
.getFile() | ||
.findResource(components); | ||
} | ||
} |