Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
release 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
KisaragiEffective committed May 2, 2021
1 parent b0289da commit c1fbd88
Show file tree
Hide file tree
Showing 20 changed files with 563 additions and 192 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ org.gradle.jvmargs=-Xmx1G
loom_version=0.5-SNAPSHOT

# Mod Properties
mod_version = 1.0.5
maven_group = net.fabricmc
mod_version = 1.1.0
maven_group = com.github.kisaragieffective.signpictureported
archives_base_name = SignPictureReloaded

# Kotlin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.kisaragieffective.signpictureported;

import java.io.Closeable;
import java.io.IOException;
import java.util.Objects;
import java.util.function.Supplier;

public class Box<T> implements Closeable {
public final Supplier<? extends T> value;
private boolean needsInit = true;
// late-bind
private T cache = null;
public Box(Supplier<? extends T> value) {
this.value = value;
}

public T getValue() {
if (needsInit) {
cache = value.get();
needsInit = false;
}
return cache;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Box<?> box = (Box<?>) o;
return Objects.equals(cache, box.cache);
}

@Override
public int hashCode() {
return Objects.hash(cache);
}

@Override
public void close() throws IOException {
if (cache instanceof Closeable) {
((Closeable) cache).close();
needsInit = true;
cache = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.kisaragieffective.signpictureported;

import net.minecraft.client.texture.NativeImageBackedTexture;

import java.io.Closeable;
import java.lang.ref.SoftReference;
import java.util.Objects;

public class ImageWrapper implements Closeable {
public final SoftReference<NativeImageBackedTexture> nibt;

public ImageWrapper(NativeImageBackedTexture nibt) {
this.nibt = new SoftReference<>(nibt);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ImageWrapper imageWrapper = (ImageWrapper) o;
return Objects.equals(nibt, imageWrapper.nibt);
}

@Override
public int hashCode() {
return Objects.hash(nibt);
}

@Override
public String toString() {
return "ImageWrapper{" +
"nibt=" + nibt.get() +
'}';
}

@Override
public void close() {
NativeImageBackedTexture nibt = this.nibt.get();
if (nibt != null) nibt.close();
this.nibt.clear();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.kisaragieffective.signpictureported;

import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.NativeImageBackedTexture;

public class NativeImageFactory {
public static final Box<NativeImageBackedTexture> errorImage = new Box<>(NativeImageFactory::getErrorImage);
public static NativeImageBackedTexture getErrorImage() {
final NativeImage nini = new NativeImage(128, 128, true);
for (int xv = 0; xv < 128; xv++) {
nini.setPixelColor(xv, 0, 0xFF_00_00_FF);
nini.setPixelColor(xv, 127, 0xFF_00_00_FF);
nini.setPixelColor(0, xv, 0xFF_00_00_FF);
nini.setPixelColor(127, xv, 0xFF_00_00_FF);
}
return new NativeImageBackedTexture(nini);
}

public static final Box<NativeImageBackedTexture> loadingImage = new Box<>(NativeImageFactory::getLoadingImage);

public static NativeImageBackedTexture getLoadingImage() {
final NativeImage nini = new NativeImage(128, 128, true);
final int gray = 0x80808080;
for (int xv = 0; xv < 128; xv++) {
nini.setPixelColor(xv, 0, gray);
nini.setPixelColor(xv, 127, gray);
nini.setPixelColor(0, xv, gray);
nini.setPixelColor(127, xv, gray);
}
return new NativeImageBackedTexture(nini);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.github.kisaragieffective.signpictureported;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.texture.NativeImageBackedTexture;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public class OutsideCache {
private OutsideCache() {

}

public static final Map<BlockPos, ImageWrapper> cache = new ConcurrentHashMap<>();
public static final Map<BlockPos, ImageWrapper> flippedCache = new ConcurrentHashMap<>();
public static final Set<String> invalidURL = new HashSet<>();

private static final Map<BlockPos, Identifier> identifierMap = new ConcurrentHashMap<>();
private static final Map<BlockPos, Identifier> flippedIdentifierMap = new ConcurrentHashMap<>();
public static final Set<BlockPos> sbp = new HashSet<>();

public static ImageWrapper putOrCached(BlockPos pos, Supplier<? extends NativeImageBackedTexture> nibt) {
return putImage(cache, pos, nibt);
}

public static ImageWrapper putFlippedOrCached(BlockPos pos, Supplier<? extends NativeImageBackedTexture> flipped) {
return putImage(flippedCache, pos, flipped);
}

public static void put(BlockPos pos, NativeImageBackedTexture nibt, boolean flipped) {
putNewIdentifier(pos, nibt);
if (flipped) {
flippedCache.put(pos, new ImageWrapper(nibt));
} else {
cache.put(pos, new ImageWrapper(nibt));
}
}

public static Optional<ImageWrapper> get(BlockPos pos, boolean flipped) {
if (flipped) {
return flippedCache.containsKey(pos) ? Optional.of(flippedCache.get(pos)) : Optional.empty();
} else {
return cache.containsKey(pos) ? Optional.of(cache.get(pos)) : Optional.empty();
}
}

public static Set<? extends BlockPos> locations() {
return identifierMap.keySet();
}

public static void drop(BlockPos pos) {
if (flippedCache.containsKey(pos)) {
final ImageWrapper nibt1 = flippedCache.get(pos);
nibt1.close();
}
if (cache.containsKey(pos)) {
final ImageWrapper nibt = flippedCache.get(pos);
nibt.close();
}
identifierMap.remove(pos);
flippedIdentifierMap.remove(pos);
}

private static ImageWrapper putImage(
Map<BlockPos, ImageWrapper> m,
BlockPos pos,
Supplier<? extends NativeImageBackedTexture> v
) {
return m.computeIfAbsent(pos, ig -> {
SignPicturePorted.LOGGER.info("cache(" + pos + "): no hit, creating");
return new ImageWrapper(v.get());
});
}

public static Identifier putNewIdentifierOrCached(BlockPos pos, NativeImageBackedTexture nibt) {
return identifierMap.computeIfAbsent(pos, p -> computeNewIdentifier(p, nibt));
}

public static Identifier putNewIdentifier(BlockPos pos, NativeImageBackedTexture nibt) {
return identifierMap.put(pos, computeNewIdentifier(pos, nibt));
}

public static Identifier computeNewIdentifier(BlockPos pos, NativeImageBackedTexture nibt) {
Identifier newIdentifier = MinecraftClient.getInstance().getTextureManager().registerDynamicTexture("sgpc_reloaded", nibt);
SignPicturePorted.LOGGER.info("New identifier payed out (for " + pos + "): " + newIdentifier);
return newIdentifier;
}

public static Identifier putFlippedNewIdentifierOrCached(BlockPos pos, NativeImageBackedTexture nibt) {
return flippedIdentifierMap.computeIfAbsent(pos, p -> computeNewIdentifier(p, nibt));
}

public static Identifier putFlippedNewIdentifier(BlockPos pos, NativeImageBackedTexture nibt) {
return flippedIdentifierMap.put(pos, computeNewIdentifier(pos, nibt));
}

public static void invalidate() {
OutsideCache.cache.values()
.forEach(x -> x.close());
OutsideCache.cache.clear();
OutsideCache.flippedCache.values()
.forEach(x -> x.close());
OutsideCache.identifierMap.clear();
OutsideCache.invalidURL.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
public class SignPicturePorted implements ModInitializer, ClientModInitializer, DedicatedServerModInitializer {
public static final String MOD_ID = "signpictureported";
public static final Logger LOGGER = LogManager.getLogger();
public static final boolean DEBUG = false;

static {
if (DEBUG) {
LOGGER.info("Hint: This build is DEBUG build.");
LOGGER.debug("If you can see this message, it's ok.");
LOGGER.info("Or else: logger can't log debug message.");
}
}

@Override
public void onInitialize() {
LOGGER.info("SignPictureReloaded was loaded");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.github.kisaragieffective.signpictureported.api;

import it.unimi.dsi.fastutil.doubles.DoubleList;
import it.unimi.dsi.fastutil.doubles.DoubleLists;

import java.util.Optional;
import java.util.OptionalDouble;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.DoubleStream;

import static com.github.kisaragieffective.signpictureported.Functions.compose;
import static com.github.kisaragieffective.signpictureported.Functions.switchBasedNullish;
import static com.github.kisaragieffective.signpictureported.internal.Functions.compose;
import static com.github.kisaragieffective.signpictureported.internal.Functions.switchBasedNullish;

public final class DisplayConfigurationParseResult {
public final double offsetRight;
Expand Down Expand Up @@ -67,10 +73,13 @@ public DisplayConfigurationParseResult(OptionalDouble offsetRight, OptionalDoubl
);

public static final String DOUBLE = "[-+]?(?:\\d+|\\d+\\.\\d+)";
// see: https://github.com/Team-Fruit/SignPicture/blob/3a10ae618ed32415d524a76e3c90b60cb33af6b2/sources/universal/src/main/java/net/teamfruit/signpic/attr/prop/PropSyntax.java
public static final String REGEX = "(?:(?<scale2>(?<sx>" + DOUBLE + ")x(?<sy>" + DOUBLE + "))" +
"|(?<scale1>x(?<s>" + DOUBLE + ")))?" +
"(?<x>X" + DOUBLE + ")?(?<y>Y" + DOUBLE + ")?(?<z>Z" + DOUBLE + ")?" +
"(?<r>R" + DOUBLE + ")?(?<u>U" + DOUBLE + ")?(?<d>D" + DOUBLE + ")?";
"(?:(?<right>R" + DOUBLE + ")|(?<left>L" + DOUBLE + "))?" +
"(?:(?<up>U" + DOUBLE + ")|(?<down>D" + DOUBLE + "))?" +
"((?<front>F" + DOUBLE + ")|(?<beside>B" + DOUBLE + "))?";
public static final Pattern PATTERN = Pattern.compile(REGEX);

/**
Expand All @@ -80,8 +89,7 @@ public DisplayConfigurationParseResult(OptionalDouble offsetRight, OptionalDoubl
* @return parse result
*/
public static DisplayConfigurationParseResult parse(String from) {
Matcher m = PATTERN
.matcher(from);
Matcher m = PATTERN.matcher(from);
if (m.matches()) {
Function<String, OptionalDouble> extractor = groupName -> switchBasedNullish(
m.group(groupName),
Expand All @@ -103,12 +111,32 @@ public static DisplayConfigurationParseResult parse(String from) {
final OptionalDouble rotateX = extractor.apply("x");
final OptionalDouble rotateY = extractor.apply("y");
final OptionalDouble rotateZ = extractor.apply("z");
final OptionalDouble offsetRight = extractor.apply("r");
final OptionalDouble offsetUp = extractor.apply("u");
final OptionalDouble offsetDepth = extractor.apply("d");
final OptionalDouble offsetRight = or(extractor.apply("right"), () ->
toStream(extractor.apply("left"))
.map(x -> -x)
.findFirst()
);
final OptionalDouble offsetUp = or(extractor.apply("up"), () ->
toStream(extractor.apply("down"))
.map(x -> -x)
.findFirst()
);
final OptionalDouble offsetDepth = or(extractor.apply("front"), () ->
toStream(extractor.apply("beside"))
.map(x -> -x)
.findFirst()
);
return new DisplayConfigurationParseResult(offsetRight, offsetUp, offsetDepth, rotateX, rotateY, rotateZ, scaleX, scaleY);
} else {
return DisplayConfigurationParseResult.DEFAULT;
}
}

private static OptionalDouble or(OptionalDouble od1, Supplier<? extends OptionalDouble> od2) {
return od1.isPresent() ? od1 : od2.get();
}

private static DoubleStream toStream(OptionalDouble od) {
return od.isPresent() ? DoubleStream.of(od.getAsDouble()) : DoubleStream.empty();
}
}
Loading

0 comments on commit c1fbd88

Please sign in to comment.