Skip to content

Commit

Permalink
Cherry-pick changes from 1.20.4 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jun 21, 2024
1 parent c1b3a37 commit a10671e
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 48 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import groovy.json.JsonSlurper
import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id 'fabric-loom' version '1.0-SNAPSHOT'
id 'fabric-loom' version '1.7-SNAPSHOT'
id "de.undercouch.download" version "4.1.2"
id 'maven-publish'
}
Expand Down Expand Up @@ -67,7 +67,9 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}"

modApi "com.terraformersmc:modmenu:${project.mod_menu_version}"
modImplementation ("com.terraformersmc:modmenu:${project.mod_menu_version}") {
transitive = false
}
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx1G

# Mod Properties
mod_version=2.2.1
mod_version=2.3.0
maven_group=cn.zbx1425
archives_base_name=resourcepackupdater

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-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public static void dispatchSyncWork() {

while (true) {
Dispatcher syncDispatcher = new Dispatcher();
if (ResourcePackUpdater.CONFIG.selectedSource.value.baseUrl.isEmpty()) {
if (ResourcePackUpdater.CONFIG.selectedSource.value == null // TODO how did we get here?
|| ResourcePackUpdater.CONFIG.selectedSource.value.baseUrl.isEmpty()) {
if (ResourcePackUpdater.CONFIG.sourceList.value.size() > 1) {
ResourcePackUpdater.GL_PROGRESS_SCREEN.resetToSelectSource();
try {
Expand All @@ -85,6 +86,12 @@ public static void dispatchSyncWork() {
} else if (ResourcePackUpdater.CONFIG.sourceList.value.size() == 1) {
ResourcePackUpdater.CONFIG.selectedSource.value = ResourcePackUpdater.CONFIG.sourceList.value.get(0);
ResourcePackUpdater.CONFIG.selectedSource.isFromLocal = true;
} else {
ResourcePackUpdater.CONFIG.selectedSource.value = new Config.SourceProperty(
"NOT CONFIGURED",
"",
false, false, true
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public static void onAfterSetServerLock() {
}

if (localServerLock == null) {
ResourcePackUpdater.LOGGER.info("Asset coordination not required by this pack.");
ResourcePackUpdater.LOGGER.info("Asset coordination not required.");
} else if (remoteServerLock == null) {
ResourcePackUpdater.LOGGER.info("Asset coordination identifier not received.");
ResourcePackUpdater.LOGGER.info("Asset coordination received no cooperation.");
} else if (!remoteServerLock.equals(localServerLock)) {
ResourcePackUpdater.LOGGER.info("Asset coordination identifier differs.");
ResourcePackUpdater.LOGGER.info("Asset coordination received discrepancy.");
} else if (lockAllSyncedPacks) {
ResourcePackUpdater.LOGGER.info("Asset coordination is unavailable due to incomplete synchronization.");
ResourcePackUpdater.LOGGER.info("Asset coordination is unavailable for incompleteness.");
} else {
ResourcePackUpdater.LOGGER.info("Asset coordination is applicable.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public void printLog(String line) throws GlHelper.MinecraftStoppingException {
redrawScreen(true);
}

@Override
public void printLogOutsidePolling(String line) throws GlHelper.MinecraftStoppingException {
for (GlScreenForm form : forms) form.printLog(line);
ResourcePackUpdater.LOGGER.info(line);
}

@Override
public void amendLastLog(String postfix) throws GlHelper.MinecraftStoppingException {
for (GlScreenForm form : forms) form.amendLastLog(postfix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ static void drawShadowRect(float width, float height, int color) {
GlHelper.blit(8, 8, width, height, 0x66000000);
GlHelper.blit(0, 0, width, height, color);
}

default void printLogOutsidePolling(String line) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,36 @@
public class ProgressForm implements GlScreenForm {

private String primaryInfo = "";
private String auxilaryInfo = "";
private String auxiliaryInfo = "";
private float primaryProgress;
private String secondaryProgress;

private static final float progressFormWidth = 600, progressFormHeight = 240;
private static final float progressFormWidth = 600, progressFormHeight = 250;

@Override
public void render() {
GlHelper.setMatCenterForm(progressFormWidth, progressFormHeight, 0.75f);
GlHelper.begin(GlHelper.PRELOAD_FONT_TEXTURE);
GlScreenForm.drawShadowRect(progressFormWidth, progressFormHeight, 0xffdee6ea);

GlHelper.drawString(20, 20, progressFormWidth - 40, 50, 20,
primaryInfo, 0xff222222, false, false);
float barBegin = 0;
float usableBarWidth = progressFormWidth - barBegin - 0;
float progressTextStart = progressFormWidth / 2 - GlHelper.getStringWidth("88%", 16) / 2;
GlHelper.blit(barBegin, 0, usableBarWidth, 30, 0x4435aa8e);
GlHelper.drawString(progressTextStart, 0 + 10, 80, LINE_HEIGHT, 16,
String.format("%d%%", Math.round(primaryProgress * 100)), 0xff328a75, false, true);
GlHelper.end();
GlHelper.begin(GlHelper.PRELOAD_FONT_TEXTURE);
GlHelper.enableScissor(0, 0, usableBarWidth * primaryProgress, 30);
GlHelper.blit(barBegin, 0, usableBarWidth, 30, 0xff35aa8e);
GlHelper.drawString(progressTextStart, 0 + 10, 80, LINE_HEIGHT, 16,
String.format("%d%%", Math.round(primaryProgress * 100)), 0xffffffff, false, true);
GlHelper.end();
GlHelper.begin(GlHelper.PRELOAD_FONT_TEXTURE);
GlHelper.disableScissor();

float barBegin = 30;
float usableBarWidth = progressFormWidth - barBegin - 30;
GlHelper.blit(barBegin, 70, usableBarWidth, 30, 0x4446ddb9);
GlHelper.blit(barBegin, 70, usableBarWidth * primaryProgress, 30, 0xff46ddb9);
GlHelper.drawString(barBegin + usableBarWidth * primaryProgress, 70 + 10, 80, LINE_HEIGHT, 16,
String.format("%d%%", Math.round(primaryProgress * 100)), 0xff46ddb9, false, true);
GlHelper.drawString(20, 45, progressFormWidth - 40, 50, 20,
primaryInfo, 0xff222222, false, false);

/*
GlHelper.blit(barBegin, 110, usableBarWidth, 30, 0x4449baee);
Expand All @@ -37,15 +46,18 @@ public void render() {
String.format("%d%%", Math.round(secondaryProgress * 100)), 0xff49baee, false, true);
*/

GlHelper.drawString(barBegin, 110, usableBarWidth, 170 - 110, 16,
secondaryProgress, 0xff222222, false, false);
if (secondaryProgress.contains("\n")) {
GlHelper.blit(0, 70, progressFormWidth, 185 - 70, 0x3399abab);
}
GlHelper.drawString(20, 80, progressFormWidth - 40, 180 - 80, 16,
secondaryProgress, 0xff222222, false, true);

boolean monospace = !auxilaryInfo.isEmpty() && auxilaryInfo.charAt(0)== ':';
GlHelper.drawString(20, 180, progressFormWidth - 40, 30, 18,
monospace ? auxilaryInfo.substring(1) : auxilaryInfo, 0xff222222, monospace, false);
boolean monospace = !auxiliaryInfo.isEmpty() && auxiliaryInfo.charAt(0)== ':';
GlHelper.drawString(20, 195, progressFormWidth - 40, 30, 18,
monospace ? auxiliaryInfo.substring(1) : auxiliaryInfo, 0xff222222, monospace, false);

String escBtnHint = ResourcePackUpdater.CONFIG.sourceList.value.size() > 1 ? "Cancel / Use Another Source" : "Cancel";
GlHelper.drawString(20, progressFormHeight - 20, progressFormWidth - 40, 16, 16, escBtnHint + ": Hold ESC", 0xff222222, false, true);
GlHelper.drawString(20, progressFormHeight - 30, progressFormWidth - 40, 16, 16, "(" + escBtnHint + ": Hold ESC)", 0xff222222, false, true);

GlHelper.end();
}
Expand All @@ -61,7 +73,7 @@ public boolean shouldStopPausing() {
@Override
public void reset() {
primaryInfo = "";
auxilaryInfo = "";
auxiliaryInfo = "";
primaryProgress = 0;
secondaryProgress = "";
}
Expand All @@ -84,7 +96,7 @@ public void setProgress(float primary, float secondary) throws GlHelper.Minecraf
@Override
public void setInfo(String secondary, String textValue) throws GlHelper.MinecraftStoppingException {
this.secondaryProgress = secondary;
this.auxilaryInfo = textValue;
this.auxiliaryInfo = textValue;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Matrix4f;
import com.mojang.math.Vector3f;
import com.mojang.math.Vector4f;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;

public class GlHelper {

Expand Down Expand Up @@ -108,7 +111,9 @@ public static void drawString(float x1, float y1, float width, float height, flo
} else if (chr == '\r') {
// Ignore CR
} else if (chr == '\t') {
x += (preloadFont.spaceWidthPl * 4 + CHAR_SPACING) * fontSize;
// Align to 8 spaces
float alignToPixels = (preloadFont.spaceWidthPl + CHAR_SPACING) * 8 * fontSize;
x = (float) (Math.ceil((x - x1) / alignToPixels) * alignToPixels + x1);
} else if (chr == ' ') {
x += (preloadFont.spaceWidthPl + CHAR_SPACING) * fontSize;
} else {
Expand All @@ -135,6 +140,31 @@ public static void drawString(float x1, float y1, float width, float height, flo
}
}

public static float getStringWidth(String text, float fontSize) {
float CHAR_SPACING = 0f;

float width = 0;
float x = 0;
for (char chr : text.toCharArray()) {
if (chr == '\n') {
width = Math.max(width, x);
x = 0;
} else if (chr == '\r') {
// Ignore CR
} else if (chr == '\t') {
// Align to 8 spaces
float alignToPixels = (preloadFont.spaceWidthPl + CHAR_SPACING) * 8 * fontSize;
x = (float) (Math.ceil(x / alignToPixels) * alignToPixels);
} else if (chr == ' ') {
x += (preloadFont.spaceWidthPl + CHAR_SPACING) * fontSize;
} else {
SimpleFont.GlyphProperty glyph = preloadFont.getGlyph(chr);
x += glyph.advancePl * fontSize + CHAR_SPACING * fontSize;
}
}
return Math.max(width, x);
}

public static void setMatIdentity() {
RenderSystem.getModelViewStack().setIdentity();
}
Expand All @@ -159,6 +189,23 @@ public static void setMatScaledPixel() {
RenderSystem.setProjectionMatrix(matrix);
}

public static void enableScissor(float x, float y, float width, float height) {
Matrix4f posMap = RenderSystem.getProjectionMatrix();
Vector4f bottomLeft = new Vector4f(x, y + height, 0, 1);
bottomLeft.transform(posMap);
Vector4f topRight = new Vector4f(x + width, y, 0, 1);
topRight.transform(posMap);
float x1 = Mth.map(bottomLeft.x(), -1, 1, 0, Minecraft.getInstance().getWindow().getWidth());
float y1 = Mth.map(bottomLeft.y(), -1, 1, 0, Minecraft.getInstance().getWindow().getHeight());
float x2 = Mth.map(topRight.x(), -1, 1, 0, Minecraft.getInstance().getWindow().getWidth());
float y2 = Mth.map(topRight.y(), -1, 1, 0, Minecraft.getInstance().getWindow().getHeight());
RenderSystem.enableScissor((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1));
}

public static void disableScissor() {
RenderSystem.disableScissor();
}

public static int getWidth() {
int rawWidth = Minecraft.getInstance().getWindow().getWidth();
if (rawWidth < 854) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public boolean runSync(String baseDir, Config.SourceProperty source, ProgressRec
while (!downloadDispatcher.tasksFinished()) {
downloadDispatcher.updateSummary();
((GlProgressScreen)cb).redrawScreen(true);
Thread.sleep(250);
Thread.sleep(1000 / 30);
}
remoteMetadata.downloadedBytes += downloadDispatcher.downloadedBytes;
downloadDispatcher.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public interface ProgressReceiver {

void printLog(String line) throws GlHelper.MinecraftStoppingException;
void printLogOutsidePolling(String line) throws GlHelper.MinecraftStoppingException;
void amendLastLog(String postfix) throws GlHelper.MinecraftStoppingException;
void setProgress(float primary, float secondary) throws GlHelper.MinecraftStoppingException;
void setInfo(String aux1, String aux2) throws GlHelper.MinecraftStoppingException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DownloadDispatcher(ProgressReceiver progressReceiver) {
this.progressReceiver = progressReceiver;
}

private final int MAX_RETRIES = 3;
private final int MAX_RETRIES = 8;

public void dispatch(DownloadTask task, Supplier<OutputStream> target) {
totalBytes += task.expectedSize;
Expand All @@ -43,14 +43,20 @@ public void dispatch(DownloadTask task, Supplier<OutputStream> target) {
while (true) {
try {
task.runBlocking(target.get());
if (task.failedAttempts > 0) {
delayedProgresses.add(() -> {
progressReceiver.printLogOutsidePolling(String.format("Downloading files ... (Retry %d succeed)",
task.failedAttempts));
});
}
break;
} catch (Exception ex) {
task.failedAttempts++;
if (task.failedAttempts < MAX_RETRIES) {
delayedProgresses.add(() -> {
progressReceiver.printLog(String.format("Retry (%d/%d) for %s due to error:",
progressReceiver.printLogOutsidePolling(String.format("Retry (%d/%d) for %s due to error:",
task.failedAttempts, MAX_RETRIES, task.fileName));
progressReceiver.printLog(ex.toString());
progressReceiver.printLogOutsidePolling(String.format("Retry %d: %s", task.failedAttempts, ex.toString()));
});
} else {
throw ex;
Expand All @@ -59,7 +65,7 @@ public void dispatch(DownloadTask task, Supplier<OutputStream> target) {
}
} catch (Exception e) {
taskException = e;
executor.shutdown();
executor.shutdownNow();
runningTasks.clear();
incompleteTasks.clear();
} finally {
Expand All @@ -79,22 +85,24 @@ public void updateSummary() {
} else {
long currentTime = System.currentTimeMillis();
long deltaTime = currentTime - lastSummaryTime;
if (deltaTime > 1000) {
summaryBytesPerSecond = (downloadedBytes - lastSummaryBytes) * 1000 / deltaTime;
lastSummaryTime = currentTime;
lastSummaryBytes = downloadedBytes;
}
double SMOOTH_FACTOR = 0.05;
double instantSpeed = (downloadedBytes - lastSummaryBytes) * 1000.0 / deltaTime;
summaryBytesPerSecond = (long) (summaryBytesPerSecond * (1 - SMOOTH_FACTOR) + instantSpeed * SMOOTH_FACTOR);
lastSummaryTime = currentTime;
lastSummaryBytes = downloadedBytes;
}
String message = String.format(": %5d KiB / %5d KiB; %5d KiB/s",
downloadedBytes / 1024, totalBytes / 1024, summaryBytesPerSecond / 1024);
String message = String.format(": % 5.2f MiB / % 5.2f MiB; %5d KiB/s",
downloadedBytes / 1048576.0, totalBytes / 1048576.0, summaryBytesPerSecond / 1024);
progressReceiver.setProgress(downloadedBytes * 1f / totalBytes, 0);

String runningProgress = incompleteTasks.size() + " Files Remaining\n" +
String.join("; ", runningTasks.stream()
.map(task -> task.fileName + ":" + (
task.totalBytes == 0 ? "WAIT" :
String.join("\n", runningTasks.stream()
.map(task -> " " + (
task.totalBytes == 0 ? "WAIT" :
String.format("%.1f%%", task.downloadedBytes * 100f / task.totalBytes)
))
) + "\t"
+ (task.failedAttempts > 0 ? "(RETRY " + task.failedAttempts + ") " : "")
+ task.fileName)
.toList());
progressReceiver.setInfo(runningProgress, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static HttpResponse<InputStream> sendHttpRequest(URI requestUri) throws I
*/

HttpRequest httpRequest = HttpRequest.newBuilder(requestUri)
.timeout(Duration.ofSeconds(10))
.timeout(Duration.ofSeconds(20))
.setHeader("User-Agent", "ResourcePackUpdater/" + ResourcePackUpdater.MOD_VERSION + " +https://www.zbx1425.cn")
.setHeader("Accept-Encoding", "gzip")
.GET()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void handleCustomPayload(ServerboundCustomPayloadPacket packet, CallbackInfo ci)
ci.cancel();
} else if (identifier.equals(ClientboundCustomPayloadPacket.BRAND)) {
if (((RPUClientVersionSupplier)player).getRPUClientVersion() == null && ResourcePackUpdater.CONFIG.clientEnforceInstall.value) {
disconnect(Text.literal(new MismatchingVersionException(ResourcePackUpdater.MOD_VERSION, "N/A").getMessage().trim()));
disconnect(Text.literal(new MismatchingVersionException(ResourcePackUpdater.MOD_VERSION, "未安裝 NOT INSTALLED").getMessage().trim()));
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private MtrVersion(int[] parts) {
public static MtrVersion parse(String src) {
ArrayList<Integer> parts = new ArrayList<>();
String[] strParts = src.split("-");
for (int i = 1; i < strParts.length; i++) {
for (int i = (strParts.length > 1 ? 1 : 0); i < strParts.length; i++) {
String[] subParts = strParts[i].split("\\.");
for (String subPart : subParts) {
if (subPart.matches("\\d+")) {
Expand Down Expand Up @@ -46,7 +46,7 @@ public boolean matches(String criteria) {
case '<':
return this.compareTo(MtrVersion.parse(criteria.substring(1))) < 0;
default:
return this.compareTo(MtrVersion.parse(criteria.substring(1))) >= 0;
return this.compareTo(MtrVersion.parse(criteria)) >= 0;
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a10671e

Please sign in to comment.