Skip to content

Commit

Permalink
Fixes and optimizations by BloCamLimb (#12)
Browse files Browse the repository at this point in the history
* A full port to 1.18, remove invalid code
* Fix UI and network, improve serialization
* Release images when logging out, add OnlyIn, add texture size query
* It may become better now
* Store mutable reference of slide state cache map
* Remove light mixing for slides, optimize image loading, improve filter quality
* 0.6.2
* 0.6.2 equivalent changes
  • Loading branch information
BloCamLimb authored Apr 30, 2022
1 parent f0f2584 commit 5665e48
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 293 deletions.
22 changes: 13 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

buildscript {
repositories {
maven { url = 'https://maven.minecraftforge.net' }
Expand All @@ -10,7 +11,6 @@ buildscript {
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.1.+', changing: true
classpath group: 'org.parchmentmc', name: 'librarian', version: '1.+'

}
}

Expand All @@ -23,7 +23,7 @@ apply plugin: 'org.parchmentmc.librarian.forgegradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '0.6.0-rc.2'
version = '0.6.2-rc.1'
group = 'org.teaconmc'
archivesBaseName = 'SlideShow'

Expand Down Expand Up @@ -103,13 +103,13 @@ jar {
archiveClassifier.set('pure')
manifest {
attributes([
"Specification-Title": "Slide-Show",
"Specification-Vendor": "TeaConMC",
"Specification-Version": "1",
"Implementation-Title": project.name,
"Implementation-Version": "${project.version}",
"Implementation-Vendor": "TeaConMC",
"Implementation-Timestamp": DateTimeFormatter.ISO_INSTANT.format(Instant.now().truncatedTo(ChronoUnit.SECONDS))
"Specification-Title" : "Slide-Show",
"Specification-Vendor" : "TeaConMC",
"Specification-Version" : "1",
"Implementation-Title" : project.name,
"Implementation-Version" : "${project.version}",
"Implementation-Vendor" : "TeaConMC",
"Implementation-Timestamp": DateTimeFormatter.ISO_INSTANT.format(Instant.now().truncatedTo(ChronoUnit.SECONDS))
])
}
}
Expand Down Expand Up @@ -154,6 +154,10 @@ publishing {
id = 'ustc-zzzz'
name = 'ustc-zzzz'
}
developer {
id = 'BloCamLimb'
name = 'BloCamLimb'
}
}
issueManagement {
system = 'GitHub Issues'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
10 changes: 2 additions & 8 deletions src/main/java/org/teacon/slides/cache/CacheStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import org.apache.http.client.utils.DateUtils;
import org.apache.http.impl.client.cache.FileResource;
import org.apache.http.message.BasicLineParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.*;
import org.teacon.slides.SlideShow;

import javax.annotation.Nullable;
Expand All @@ -31,10 +28,7 @@
import java.io.IOException;
import java.lang.ref.ReferenceQueue;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,51 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.cache.CacheConfig;
import org.apache.http.impl.client.cache.CachingHttpClients;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.*;
import org.teacon.slides.SlideShow;

import javax.annotation.Nonnull;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

public final class SlideImageStore {
public final class ImageCache {

private static final Logger LOGGER = LogManager.getLogger(SlideShow.class);
private static final Marker MARKER = MarkerManager.getMarker("Cache");

private static final Path LOCAL_CACHE_PATH = Paths.get("slideshow");

private static volatile SlideImageStore sInstance;
private static volatile ImageCache sInstance;

private static final int MAX_CACHE_OBJECT_SIZE = 1 << 29; // 512 MiB
private static final CacheConfig CONFIG = CacheConfig.custom().setMaxObjectSize(MAX_CACHE_OBJECT_SIZE).setSharedCache(false).build();
private static final CacheConfig CONFIG =
CacheConfig.custom().setMaxObjectSize(MAX_CACHE_OBJECT_SIZE).setSharedCache(false).build();

private static final String DEFAULT_REFERER = "https://github.com/teaconmc/SlideShow";
// user agent copied from forge gradle 2.3 (class: net.minecraftforge.gradle.common.Constants)
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, " +
"like Gecko) Chrome/23.0.1271.95 Safari/537.11";

private final CloseableHttpClient mHttpClient;
private final CacheStorage mCacheStorage;

@Nonnull
public static CompletableFuture<byte[]> getImage(@Nonnull URI uri, boolean remote) {
if (sInstance == null) {
synchronized (SlideImageStore.class) {
if (sInstance == null) {
sInstance = new SlideImageStore(LOCAL_CACHE_PATH);
}
}
}
return sInstance.getResource(uri, remote);
}

public static int cleanImages() {
public static ImageCache getInstance() {
if (sInstance != null) {
return sInstance.mCacheStorage.cleanResources();
return sInstance;
}
synchronized (ImageCache.class) {
if (sInstance == null) {
sInstance = new ImageCache(LOCAL_CACHE_PATH);
}
}
return 0;
return sInstance;
}

private SlideImageStore(Path dir) {
private ImageCache(Path dir) {
try {
Files.createDirectories(dir);
} catch (IOException e) {
Expand All @@ -75,14 +65,14 @@ private SlideImageStore(Path dir) {
}

@Nonnull
private CompletableFuture<byte[]> getResource(@Nonnull URI location, boolean remote) {
public CompletableFuture<byte[]> getResource(@Nonnull URI location, boolean online) {
return CompletableFuture.supplyAsync(() -> {
final HttpCacheContext context = HttpCacheContext.create();
try (CloseableHttpResponse response = this.createResponse(location, context, remote)) {
try (CloseableHttpResponse response = createResponse(location, context, online)) {
try {
return IOUtils.toByteArray(response.getEntity().getContent());
} catch (IOException e) {
if (remote) {
if (online) {
LOGGER.warn(MARKER, "Failed to read bytes from remote source.", e);
}
throw new CompletionException(e);
Expand All @@ -97,14 +87,14 @@ private CompletableFuture<byte[]> getResource(@Nonnull URI location, boolean rem
});
}

private CloseableHttpResponse createResponse(URI location, HttpCacheContext context, boolean remote) throws IOException {
private CloseableHttpResponse createResponse(URI location, HttpCacheContext context, boolean online) throws IOException {
HttpGet request = new HttpGet(location);

request.addHeader(HttpHeaders.REFERER, DEFAULT_REFERER);
request.addHeader(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT);
request.addHeader(HttpHeaders.ACCEPT, String.join(", ", ImageIO.getReaderMIMETypes()));

if (!remote) {
if (!online) {
request.addHeader(HttpHeaders.CACHE_CONTROL, "max-stale=2147483647");
request.addHeader(HttpHeaders.CACHE_CONTROL, "only-if-cached");
} else {
Expand All @@ -125,4 +115,8 @@ private void logRequestHeader(@Nonnull HttpCacheContext context) {
}
LOGGER.debug(MARKER, "Remote server status: {}", context.getCacheResponseStatus());
}

public int cleanResources() {
return mCacheStorage.cleanResources();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ProjectorBlock() {

@Nonnull
@Override
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
return switch (state.getValue(BASE)) {
case DOWN:
yield SHAPE_WITH_BASE_DOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ public boolean shouldRenderOffScreen(ProjectorBlockEntity tile) {

@Override
public int getViewDistance() {
return 512;
return 256;
}
}
Loading

0 comments on commit 5665e48

Please sign in to comment.