show white page when the image files in jar are missing

This commit is contained in:
huangyuhui 2017-02-25 19:14:08 +08:00
parent b4658463e1
commit 0dfd8af7d5
6 changed files with 113 additions and 149 deletions

View File

@ -144,7 +144,7 @@ public final class Main implements Runnable {
try {
PluginManager.getPlugin(Class.forName(c));
} catch (ClassNotFoundException ex) {
LOGGER.log(Level.WARNING, "Class: " + c + " not found, please add your plugin jar to class path.", ex);
HMCLog.warn("Class: " + c + " not found, please add your plugin jar to class path.", ex);
}
} else if (s.startsWith("--help")) {
System.out.println("HMCL command line help");

View File

@ -38,6 +38,7 @@ import java.util.jar.Pack200;
import java.util.logging.Level;
import java.util.zip.GZIPInputStream;
import org.jackhuang.hmcl.Main;
import org.jackhuang.hmcl.api.HMCLog;
import org.jackhuang.hmcl.api.event.SimpleEvent;
import org.jackhuang.hmcl.util.C;
import org.jackhuang.hmcl.core.MCUtils;
@ -98,7 +99,7 @@ public class AppDataUpgrader extends IUpgrader {
} catch (JsonSyntaxException ex) {
f.delete();
} catch (IOException | PrivilegedActionException t) {
Main.LOGGER.log(Level.SEVERE, "Failed to execute newer version application", t);
HMCLog.err("Failed to execute newer version application", t);
}
}
@ -131,7 +132,7 @@ public class AppDataUpgrader extends IUpgrader {
System.exit(0);
}
} catch (IOException ex) {
Main.LOGGER.log(Level.SEVERE, "Failed to create upgrader", ex);
HMCLog.err("Failed to create upgrader", ex);
}
else {
String url = C.URL_PUBLISH;
@ -145,7 +146,7 @@ public class AppDataUpgrader extends IUpgrader {
try {
java.awt.Desktop.getDesktop().browse(new URI(url));
} catch (URISyntaxException | IOException e) {
Main.LOGGER.log(Level.WARNING, "Failed to browse uri: " + url, e);
HMCLog.err("Failed to browse uri: " + url, e);
Utils.setClipborad(url);
MessageBox.show(C.i18n("update.no_browser"));
}

View File

@ -5,6 +5,7 @@
*/
package org.jackhuang.hmcl.laf.utils;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import org.jb2011.ninepatch4j.NinePatch;
@ -13,6 +14,8 @@ import org.jb2011.ninepatch4j.NinePatch;
* @author huang
*/
public class Icon9Factory extends RawCache<NinePatch> {
private static final NinePatch EMPTY = NinePatch.load(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), false, true);
/**
* 相对路径根默认是相对于本类的相对物理路径.
@ -50,7 +53,13 @@ public class Icon9Factory extends RawCache<NinePatch> {
}
public NinePatch get(String key) {
return icons.get(ns + ":" + key);
NinePatch p = icons.get(ns + ":" + key);
if (p == null) {
System.err.println("Could not find 9patch: " + key);
icons.put(ns + ":" + key, EMPTY);
return EMPTY;
}
return p;
}
public NinePatch get(String key, String state) {

View File

@ -5,12 +5,8 @@ package org.jb2011.ninepatch4j;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ImageObserver;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.io.InputStream;
@ -18,14 +14,13 @@ import java.net.URL;
import javax.imageio.ImageIO;
public class GraphicsUtilities {
public static BufferedImage loadCompatibleImage(URL resource) throws IOException {
BufferedImage image = ImageIO.read(resource);
return GraphicsUtilities.toCompatibleImage(image);
return GraphicsUtilities.toCompatibleImage(ImageIO.read(resource));
}
public static BufferedImage loadCompatibleImage(InputStream stream) throws IOException {
BufferedImage image = ImageIO.read(stream);
return GraphicsUtilities.toCompatibleImage(image);
return GraphicsUtilities.toCompatibleImage(ImageIO.read(stream));
}
public static BufferedImage createCompatibleImage(int width, int height) {
@ -33,12 +28,10 @@ public class GraphicsUtilities {
}
public static BufferedImage toCompatibleImage(BufferedImage image) {
if (GraphicsUtilities.isHeadless()) {
if (GraphicsUtilities.isHeadless())
return image;
}
if (image.getColorModel().equals(GraphicsUtilities.getGraphicsConfiguration().getColorModel())) {
if (image.getColorModel().equals(GraphicsUtilities.getGraphicsConfiguration().getColorModel()))
return image;
}
BufferedImage compatibleImage = GraphicsUtilities.getGraphicsConfiguration().createCompatibleImage(image.getWidth(), image.getHeight(), image.getTransparency());
Graphics g = compatibleImage.getGraphics();
g.drawImage(image, 0, 0, null);
@ -64,20 +57,17 @@ public class GraphicsUtilities {
}
public static int[] getPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) {
if (w == 0 || h == 0) {
if (w == 0 || h == 0)
return new int[0];
}
if (pixels == null) {
if (pixels == null)
pixels = new int[w * h];
} else if (pixels.length < w * h) {
else if (pixels.length < w * h)
throw new IllegalArgumentException("Pixels array must have a length >= w * h");
}
int imageType = img.getType();
if (imageType == 2 || imageType == 1) {
WritableRaster raster = img.getRaster();
return (int[])raster.getDataElements(x, y, w, h, pixels);
return (int[]) raster.getDataElements(x, y, w, h, pixels);
}
return img.getRGB(x, y, w, h, pixels, 0, w);
}
}

View File

@ -3,21 +3,19 @@
*/
package org.jb2011.ninepatch4j;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.jb2011.ninepatch4j.GraphicsUtilities;
import org.jb2011.ninepatch4j.NinePatchChunk;
public class NinePatch {
public static final String EXTENSION_9PATCH = ".9.png";
private BufferedImage mImage;
private NinePatchChunk mChunk;
private final BufferedImage mImage;
private final NinePatchChunk mChunk;
public BufferedImage getImage() {
return this.mImage;
@ -28,26 +26,20 @@ public class NinePatch {
}
public static NinePatch load(URL fileUrl, boolean convert) throws IOException {
BufferedImage image = null;
try {
image = GraphicsUtilities.loadCompatibleImage(fileUrl);
}
catch (MalformedURLException e) {
boolean is9Patch = fileUrl.getPath().toLowerCase().endsWith(".9.png");
return NinePatch.load(GraphicsUtilities.loadCompatibleImage(fileUrl), is9Patch, convert);
} catch (MalformedURLException e) {
return null;
}
boolean is9Patch = fileUrl.getPath().toLowerCase().endsWith(".9.png");
return NinePatch.load(image, is9Patch, convert);
}
public static NinePatch load(InputStream stream, boolean is9Patch, boolean convert) throws IOException {
BufferedImage image = null;
try {
image = GraphicsUtilities.loadCompatibleImage(stream);
}
catch (MalformedURLException e) {
return NinePatch.load(GraphicsUtilities.loadCompatibleImage(stream), is9Patch, convert);
} catch (MalformedURLException e) {
return null;
}
return NinePatch.load(image, is9Patch, convert);
}
/*
@ -56,12 +48,12 @@ public class NinePatch {
*/
public static NinePatch load(BufferedImage image, boolean is9Patch, boolean convert) {
if (!is9Patch) {
if (!convert) return null;
if (!convert)
return null;
image = NinePatch.convertTo9Patch(image);
return new NinePatch(image);
} else {
} else
NinePatch.ensure9Patch(image);
}
return new NinePatch(image);
}
@ -94,23 +86,19 @@ public class NinePatch {
int i = 0;
while (i < width) {
pixel = image.getRGB(i, 0);
if (pixel != 0 && pixel != -16777216) {
if (pixel != 0 && pixel != -16777216)
image.setRGB(i, 0, 0);
}
if ((pixel = image.getRGB(i, height - 1)) != 0 && pixel != -16777216) {
if ((pixel = image.getRGB(i, height - 1)) != 0 && pixel != -16777216)
image.setRGB(i, height - 1, 0);
}
++i;
}
i = 0;
while (i < height) {
pixel = image.getRGB(0, i);
if (pixel != 0 && pixel != -16777216) {
if (pixel != 0 && pixel != -16777216)
image.setRGB(0, i, 0);
}
if ((pixel = image.getRGB(width - 1, i)) != 0 && pixel != -16777216) {
if ((pixel = image.getRGB(width - 1, i)) != 0 && pixel != -16777216)
image.setRGB(width - 1, i, 0);
}
++i;
}
}
@ -119,6 +107,11 @@ public class NinePatch {
BufferedImage buffer = GraphicsUtilities.createTranslucentCompatibleImage(image.getWidth() + 2, image.getHeight() + 2);
Graphics2D g2 = buffer.createGraphics();
g2.drawImage(image, 1, 1, null);
g2.setColor(Color.black);
g2.fillRect(0, 1, 1, 1);
g2.fillRect(1, 0, 1, 1);
g2.fillRect(1, image.getHeight() + 1, 1, 1);
g2.fillRect(image.getWidth() + 1, 1, 1, 1);
g2.dispose();
return buffer;
}
@ -127,4 +120,3 @@ public class NinePatch {
return image.getSubimage(1, 1, image.getWidth() - 2, image.getHeight() - 2);
}
}

View File

@ -14,8 +14,8 @@ import java.util.List;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class NinePatchChunk
implements Serializable {
public class NinePatchChunk implements Serializable {
private static final long serialVersionUID = -7353439224505296217L;
private static final int[] sPaddingRect = new int[4];
private boolean mVerticalStartWithPatch;
@ -36,31 +36,28 @@ implements Serializable {
public void draw(BufferedImage image, Graphics2D graphics2D, int x, int y, int scaledWidth, int scaledHeight, int destDensity, int srcDensity) {
boolean scaling;
boolean bl = scaling = destDensity != srcDensity && destDensity != 0 && srcDensity != 0;
if (scaling) {
if (scaling)
try {
graphics2D = (Graphics2D)graphics2D.create();
float densityScale = (float)destDensity / (float)srcDensity;
graphics2D = (Graphics2D) graphics2D.create();
float densityScale = (float) destDensity / (float) srcDensity;
graphics2D.translate(x, y);
graphics2D.scale(densityScale, densityScale);
scaledWidth = (int)((float)scaledWidth / densityScale);
scaledHeight = (int)((float)scaledHeight / densityScale);
scaledWidth = (int) ((float) scaledWidth / densityScale);
scaledHeight = (int) ((float) scaledHeight / densityScale);
y = 0;
x = 0;
this.draw(image, graphics2D, x, y, scaledWidth, scaledHeight);
}
finally {
} finally {
graphics2D.dispose();
}
} else {
else
this.draw(image, graphics2D, x, y, scaledWidth, scaledHeight);
}
}
private void draw(BufferedImage image, Graphics2D graphics2D, int x, int y, int scaledWidth, int scaledHeight) {
if (scaledWidth <= 1 || scaledHeight <= 1) {
if (scaledWidth <= 1 || scaledHeight <= 1)
return;
}
Graphics2D g = (Graphics2D)graphics2D.create();
Graphics2D g = (Graphics2D) graphics2D.create();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
try {
if (this.mPatches.isEmpty()) {
@ -91,10 +88,10 @@ implements Serializable {
if (!vStretch) {
if (hStretch) {
r = this.mHorizontalPatches.get(horizontalIndex++);
extra = (float)r.width / data.mHorizontalPatchesSum;
width = (int)(extra * hRemainder / hWeightSum);
extra = (float) r.width / data.mHorizontalPatchesSum;
width = (int) (extra * hRemainder / hWeightSum);
hWeightSum -= extra;
hRemainder -= (float)width;
hRemainder -= (float) width;
g.drawImage(image, x, y, x + width, y + r.height, r.x, r.y, r.x + r.width, r.y + r.height, null);
x += width;
} else {
@ -105,18 +102,18 @@ implements Serializable {
height = r.height;
} else if (hStretch) {
r = this.mPatches.get(patchIndex++);
vExtra = (float)r.height / data.mVerticalPatchesSum;
height = (int)(vExtra * vRemainder / vWeightSum);
extra = (float)r.width / data.mHorizontalPatchesSum;
width = (int)(extra * hRemainder / hWeightSum);
vExtra = (float) r.height / data.mVerticalPatchesSum;
height = (int) (vExtra * vRemainder / vWeightSum);
extra = (float) r.width / data.mHorizontalPatchesSum;
width = (int) (extra * hRemainder / hWeightSum);
hWeightSum -= extra;
hRemainder -= (float)width;
hRemainder -= (float) width;
g.drawImage(image, x, y, x + width, y + height, r.x, r.y, r.x + r.width, r.y + r.height, null);
x += width;
} else {
r = this.mVerticalPatches.get(verticalIndex++);
vExtra = (float)r.height / data.mVerticalPatchesSum;
height = (int)(vExtra * vRemainder / vWeightSum);
vExtra = (float) r.height / data.mVerticalPatchesSum;
height = (int) (vExtra * vRemainder / vWeightSum);
g.drawImage(image, x, y, x + r.width, y + height, r.x, r.y, r.x + r.width, r.y + r.height, null);
x += r.width;
}
@ -126,12 +123,11 @@ implements Serializable {
y += height;
if (vStretch) {
vWeightSum -= vExtra;
vRemainder -= (float)height;
vRemainder -= (float) height;
}
boolean bl = vStretch = !vStretch;
}
}
finally {
} finally {
g.dispose();
}
}
@ -156,56 +152,60 @@ implements Serializable {
int remainderHorizontal = 0;
int remainderVertical = 0;
if (this.mFixed.size() > 0) {
start = this.mFixed.get((int)0).y;
start = this.mFixed.get((int) 0).y;
for (Rectangle rect : this.mFixed) {
if (rect.y > start) {
endRow = true;
measuredWidth = true;
}
if (!measuredWidth) {
if (!measuredWidth)
remainderHorizontal += rect.width;
}
if (!endRow) continue;
if (!endRow)
continue;
remainderVertical += rect.height;
endRow = false;
start = rect.y;
}
}
DrawingData.access$4(data, scaledWidth - remainderHorizontal);
DrawingData.access$5(data, scaledHeight - remainderVertical);
DrawingData.access$6(data, 0.0f);
data.mRemainderHorizontal = scaledWidth - remainderHorizontal;
data.mRemainderVertical = scaledHeight - remainderVertical;
data.mHorizontalPatchesSum = 0.0f;
if (this.mHorizontalPatches.size() > 0) {
start = -1;
for (Rectangle rect : this.mHorizontalPatches) {
if (rect.x <= start) continue;
if (rect.x <= start)
continue;
DrawingData drawingData = data;
DrawingData.access$6(drawingData, drawingData.mHorizontalPatchesSum + (float)rect.width);
drawingData.mHorizontalPatchesSum = drawingData.mHorizontalPatchesSum + (float) rect.width;
start = rect.x;
}
} else {
start = -1;
for (Rectangle rect : this.mPatches) {
if (rect.x <= start) continue;
if (rect.x <= start)
continue;
DrawingData drawingData = data;
DrawingData.access$6(drawingData, drawingData.mHorizontalPatchesSum + (float)rect.width);
drawingData.mHorizontalPatchesSum = drawingData.mHorizontalPatchesSum + (float) rect.width;
start = rect.x;
}
}
DrawingData.access$7(data, 0.0f);
data.mVerticalPatchesSum = 0.0f;
if (this.mVerticalPatches.size() > 0) {
start = -1;
for (Rectangle rect : this.mVerticalPatches) {
if (rect.y <= start) continue;
if (rect.y <= start)
continue;
DrawingData drawingData = data;
DrawingData.access$7(drawingData, drawingData.mVerticalPatchesSum + (float)rect.height);
drawingData.mVerticalPatchesSum = drawingData.mVerticalPatchesSum + (float) rect.height;
start = rect.y;
}
} else {
start = -1;
for (Rectangle rect : this.mPatches) {
if (rect.y <= start) continue;
if (rect.y <= start)
continue;
DrawingData drawingData = data;
DrawingData.access$7(drawingData, drawingData.mVerticalPatchesSum + (float)rect.height);
drawingData.mVerticalPatchesSum = drawingData.mVerticalPatchesSum + (float) rect.height;
start = rect.y;
}
}
@ -225,16 +225,16 @@ implements Serializable {
result = new boolean[1];
Pair<List<Pair<Integer>>> top = this.getPatches(row, result);
this.mHorizontalStartWithPatch = result[0];
this.mFixed = this.getRectangles((List)left.mFirst, (List)top.mFirst);
this.mPatches = this.getRectangles((List)left.mSecond, (List)top.mSecond);
this.mFixed = this.getRectangles((List) left.mFirst, (List) top.mFirst);
this.mPatches = this.getRectangles((List) left.mSecond, (List) top.mSecond);
if (this.mFixed.size() > 0) {
this.mHorizontalPatches = this.getRectangles((List)left.mFirst, (List)top.mSecond);
this.mVerticalPatches = this.getRectangles((List)left.mSecond, (List)top.mFirst);
} else if (((List)top.mFirst).size() > 0) {
this.mHorizontalPatches = this.getRectangles((List) left.mFirst, (List) top.mSecond);
this.mVerticalPatches = this.getRectangles((List) left.mSecond, (List) top.mFirst);
} else if (((List) top.mFirst).size() > 0) {
this.mHorizontalPatches = new ArrayList<>(0);
this.mVerticalPatches = this.getVerticalRectangles(height, (List)top.mFirst);
} else if (((List)left.mFirst).size() > 0) {
this.mHorizontalPatches = this.getHorizontalRectangles(width, (List)left.mFirst);
this.mVerticalPatches = this.getVerticalRectangles(height, (List) top.mFirst);
} else if (((List) left.mFirst).size() > 0) {
this.mHorizontalPatches = this.getHorizontalRectangles(width, (List) left.mFirst);
this.mVerticalPatches = new ArrayList<>(0);
} else {
this.mVerticalPatches = new ArrayList<>(0);
@ -243,9 +243,9 @@ implements Serializable {
row = GraphicsUtilities.getPixels(image, 1, height + 1, width, 1, row);
column = GraphicsUtilities.getPixels(image, width + 1, 1, 1, height, column);
top = this.getPatches(row, result);
this.mHorizontalPadding = this.getPadding((List)top.mFirst);
this.mHorizontalPadding = this.getPadding((List) top.mFirst);
left = this.getPatches(column, result);
this.mVerticalPadding = this.getPadding((List)left.mFirst);
this.mVerticalPadding = this.getPadding((List) left.mFirst);
}
private List<Rectangle> getVerticalRectangles(int imageHeight, List<Pair<Integer>> topPairs) {
@ -269,17 +269,15 @@ implements Serializable {
}
private Pair<Integer> getPadding(List<Pair<Integer>> pairs) {
if (pairs.isEmpty()) {
if (pairs.isEmpty())
return new Pair<>(0, 0);
}
if (pairs.size() == 1) {
if (pairs.get((int)0).mFirst == 0) {
return new Pair<>(pairs.get((int)0).mSecond - pairs.get((int)0).mFirst, 0);
}
return new Pair<>(0, pairs.get((int)0).mSecond - pairs.get((int)0).mFirst);
if (pairs.get((int) 0).mFirst == 0)
return new Pair<>(pairs.get((int) 0).mSecond - pairs.get((int) 0).mFirst, 0);
return new Pair<>(0, pairs.get((int) 0).mSecond - pairs.get((int) 0).mFirst);
}
int index = pairs.size() - 1;
return new Pair<>(pairs.get((int)0).mSecond - pairs.get((int)0).mFirst, pairs.get((int)index).mSecond - pairs.get((int)index).mFirst);
return new Pair<>(pairs.get((int) 0).mSecond - pairs.get((int) 0).mFirst, pairs.get((int) index).mSecond - pairs.get((int) index).mFirst);
}
private List<Rectangle> getRectangles(List<Pair<Integer>> leftPairs, List<Pair<Integer>> topPairs) {
@ -307,13 +305,11 @@ implements Serializable {
int pixel = pixels[i];
if (pixel != lastPixel) {
if (lastPixel == -16777216) {
if (first) {
if (first)
startWithPatch[0] = true;
}
patches.add(new Pair<>(lastIndex, i));
} else {
} else
fixed.add(new Pair<>(lastIndex, i));
}
first = false;
lastIndex = i;
lastPixel = pixel;
@ -321,13 +317,11 @@ implements Serializable {
++i;
}
if (lastPixel == -16777216) {
if (first) {
if (first)
startWithPatch[0] = true;
}
patches.add(new Pair<>(lastIndex, pixels.length));
} else {
} else
fixed.add(new Pair<>(lastIndex, pixels.length));
}
if (patches.isEmpty()) {
patches.add(new Pair<>(1, pixels.length));
startWithPatch[0] = true;
@ -337,36 +331,15 @@ implements Serializable {
}
static final class DrawingData {
private int mRemainderHorizontal;
private int mRemainderVertical;
private float mHorizontalPatchesSum;
private float mVerticalPatchesSum;
DrawingData() {
}
static /* synthetic */ void access$4(DrawingData drawingData, int n) {
drawingData.mRemainderHorizontal = n;
}
static /* synthetic */ void access$5(DrawingData drawingData, int n) {
drawingData.mRemainderVertical = n;
}
static /* synthetic */ void access$6(DrawingData drawingData, float f) {
drawingData.mHorizontalPatchesSum = f;
}
static /* synthetic */ void access$7(DrawingData drawingData, float f) {
drawingData.mVerticalPatchesSum = f;
}
int mRemainderHorizontal;
int mRemainderVertical;
float mHorizontalPatchesSum;
float mVerticalPatchesSum;
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
static class Pair<E>
implements Serializable {
static class Pair<E> implements Serializable {
private static final long serialVersionUID = -2204108979541762418L;
E mFirst;
E mSecond;
@ -383,4 +356,3 @@ implements Serializable {
}
}