mirror of
https://github.com/kuroppoi/entralinked.git
synced 2026-04-26 08:37:37 -05:00
Add option to preview C-Gear & Pokédex skins
This commit is contained in:
parent
0d964ec129
commit
8849265805
|
|
@ -1,10 +1,21 @@
|
|||
package entralinked.network.http.dashboard;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import entralinked.Entralinked;
|
||||
|
|
@ -20,6 +31,7 @@ import entralinked.model.player.PlayerManager;
|
|||
import entralinked.model.player.PlayerStatus;
|
||||
import entralinked.network.http.HttpHandler;
|
||||
import entralinked.utility.GsidUtility;
|
||||
import entralinked.utility.TiledImageReader;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.config.JavalinConfig;
|
||||
import io.javalin.http.Context;
|
||||
|
|
@ -32,20 +44,41 @@ import io.javalin.json.JavalinJackson;
|
|||
*/
|
||||
public class DashboardHandler implements HttpHandler {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
private final Set<Integer> availableBlackAndWhiteSpecies = Set.of(
|
||||
505, 507, 510, 511, 513, 515, 519, 523, 525, 527, 529, 531, 533, 535, 538, 539, 542, 545, 546, 548,
|
||||
550, 553, 556, 558, 559, 561, 564, 569, 572, 575, 578, 580, 583, 587, 588, 594, 596, 600, 605, 607,
|
||||
610, 613, 616, 618, 619, 621, 622, 624, 626, 628, 630, 631, 632);
|
||||
private final Map<String, BufferedImage> skinPreviewCache = new HashMap<>();
|
||||
private final DlcList dlcList;
|
||||
private final PlayerManager playerManager;
|
||||
|
||||
public DashboardHandler(Entralinked entralinked) {
|
||||
this.dlcList = entralinked.getDlcList();
|
||||
this.playerManager = entralinked.getPlayerManager();
|
||||
|
||||
// Load & cache skin previews
|
||||
logger.info("Loading C-Gear and Pokédex skin previews ...");
|
||||
List<Dlc> skins = dlcList.getDlcList(dlc -> dlc.type().startsWith("CGEAR") || dlc.type().equals("ZUKAN"));
|
||||
|
||||
for(Dlc skin : skins) {
|
||||
try(FileInputStream inputStream = new FileInputStream(skin.path())) {
|
||||
BufferedImage image =
|
||||
skin.type().equals("ZUKAN") ? TiledImageReader.readDexSkin(inputStream) :
|
||||
skin.type().equals("CGEAR") ? TiledImageReader.readCGearSkin(inputStream, true) :
|
||||
TiledImageReader.readCGearSkin(inputStream, false); // CGEAR2
|
||||
skinPreviewCache.put(skin.name(), image);
|
||||
} catch(IOException | IndexOutOfBoundsException e) {
|
||||
logger.error("Could not load image for skin {} of type {}", skin.name(), skin.type(), e);
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("Cached {} skin previews", skinPreviewCache.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addHandlers(Javalin javalin) {
|
||||
javalin.get("/dashboard/previewskin", this::handlePreviewSkin);
|
||||
javalin.get("/dashboard/dlc", this::handleRetrieveDlcList);
|
||||
javalin.get("/dashboard/profile", this::handleRetrieveProfile);
|
||||
javalin.post("/dashboard/profile", this::handleUpdateProfile);
|
||||
|
|
@ -73,6 +106,22 @@ public class DashboardHandler implements HttpHandler {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET request handler for {@code /dashboard/previewskin}
|
||||
*/
|
||||
private void handlePreviewSkin(Context ctx) throws IOException {
|
||||
// Make sure that the name is present and exists
|
||||
String name = ctx.queryParam("name");
|
||||
|
||||
if(name == null || !skinPreviewCache.containsKey(name)) {
|
||||
ctx.status(404);
|
||||
return;
|
||||
}
|
||||
|
||||
// Write cached image data
|
||||
ImageIO.write(skinPreviewCache.get(name), "png", ctx.outputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* GET request handler for {@code /dashboard/dlc}
|
||||
*/
|
||||
|
|
|
|||
130
src/main/java/entralinked/utility/TiledImageReader.java
Normal file
130
src/main/java/entralinked/utility/TiledImageReader.java
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
package entralinked.utility;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Utility class for reading tiled images (C-Gear & Pokédex skin data) into a usable {@link BufferedImage}.
|
||||
*/
|
||||
public class TiledImageReader {
|
||||
|
||||
public static final int TILE_WIDTH = 8;
|
||||
public static final int TILE_HEIGHT = 8;
|
||||
public static final int TILE_SIZE = TILE_WIDTH * TILE_HEIGHT;
|
||||
public static final int COLOR_PALETTE_SIZE = 16;
|
||||
public static final int SCREEN_WIDTH = 256;
|
||||
public static final int SCREEN_HEIGHT = 192;
|
||||
public static final int SCREEN_TILE_COUNT = SCREEN_WIDTH * SCREEN_HEIGHT / TILE_SIZE;
|
||||
|
||||
/**
|
||||
* Calls {@link #readTiledImage(InputStream, int, boolean)} with a tile count of 255.
|
||||
*
|
||||
* @param normalizeIndices Should be {@code true} if the provided C-Gear skin data is from the original Black & White.
|
||||
* @return A {@link BufferedImage} representing the read C-Gear skin data.
|
||||
*/
|
||||
public static BufferedImage readCGearSkin(InputStream inputStream, boolean normalizeIndices) throws IOException {
|
||||
return readTiledImage(inputStream, 255, normalizeIndices);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link #readTiledImage(InputStream, int, boolean)} with a tile count of 768 and index normalization disabled.
|
||||
*
|
||||
* @return A {@link BufferedImage} representing the read Pokédex skin data.
|
||||
*/
|
||||
public static BufferedImage readDexSkin(InputStream inputStream) throws IOException {
|
||||
return readTiledImage(inputStream, 768, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads tiled image data from the provided {@link InputStream} and returns a {@link BufferedImage} representing the read image data.
|
||||
*
|
||||
* @param tileCount The number of tiles to be read. This should be equal to the maximum number of tiles for this image.
|
||||
* @param normalizedIndices Indicates that tile indices are not linear (Black & White C-Gear skins) and should be normalized.
|
||||
* @return A {@link BufferedImage} representing the read image data.
|
||||
*/
|
||||
public static BufferedImage readTiledImage(InputStream inputStream, int tileCount, boolean normalizeIndices) throws IOException {
|
||||
BufferedImage image = new BufferedImage(SCREEN_WIDTH, SCREEN_HEIGHT, BufferedImage.TYPE_INT_RGB); // Result image
|
||||
int[] tileData = new int[tileCount * TILE_SIZE];
|
||||
int[] tileIndices = new int[tileCount]; // Tile index lookup table
|
||||
int[] colorPalette = new int[COLOR_PALETTE_SIZE];
|
||||
|
||||
// Read tile data.
|
||||
for(int i = 0; i < tileCount; i++) {
|
||||
for(int j = 0; j < TILE_SIZE / 2; j++) {
|
||||
int paletteIndices = inputStream.read(); // Contains color palette indices for 2 adjacent pixels.
|
||||
tileData[i * TILE_SIZE + j * 2] = paletteIndices & (COLOR_PALETTE_SIZE - 1);
|
||||
tileData[i * TILE_SIZE + j * 2 + 1] = (paletteIndices >> 4) & (COLOR_PALETTE_SIZE - 1);
|
||||
}
|
||||
|
||||
// Store the index of the tile as it would be in memory so we can look it up later when we're mapping the tiles.
|
||||
tileIndices[i] = normalizeIndices ? i + i / 17 * 15 + 0xA0A0 : i;
|
||||
}
|
||||
|
||||
// Read color data.
|
||||
// Pokédex skins contain room for 240 extra colors, 64 of which are defined and appear to be used as the
|
||||
// 'background' colors in cases where the skin is only an overlay that is displayed on top of the 'true' Pokédex.
|
||||
for(int i = 0; i < COLOR_PALETTE_SIZE; i++) {
|
||||
int color = inputStream.read() | inputStream.read() << 8;
|
||||
|
||||
// Convert BGR555 to RGB888
|
||||
int red = (color & 0x1F) << 3;
|
||||
int green = ((color & 0x3E0) >> 5) << 3;
|
||||
int blue = ((color & 0x7C00) >> 10) << 3;
|
||||
colorPalette[i] = (red << 16) | (green << 8) | blue;
|
||||
}
|
||||
|
||||
// Map tiles to the resulting image.
|
||||
// In cases where the tile count is 768 or greater, which is exactly enough tiles to fill the entire screen,
|
||||
// the tiles will be applied in the order they are provided and no additional mapping data will be read.
|
||||
// This is always the case for Pokédex skins, and never the case for C-Gear skins.
|
||||
if(tileCount < SCREEN_TILE_COUNT) {
|
||||
// Not enough tiles -- read additional mapping data to figure out their placement.
|
||||
for(int i = 0; i < SCREEN_TILE_COUNT; i++) {
|
||||
int x = i * TILE_WIDTH % SCREEN_WIDTH;
|
||||
int y = i * TILE_WIDTH / SCREEN_WIDTH * TILE_HEIGHT;
|
||||
int leftBits = inputStream.read();
|
||||
int rightBits = inputStream.read();
|
||||
int memoryIndex = leftBits | (rightBits & ~12) << 8;
|
||||
int flipBits = rightBits & 12;
|
||||
|
||||
// The normalized tile index is the index of the in-memory tile index in the lookup table.
|
||||
int tileIndex = 0;
|
||||
|
||||
for(int k = 0; k < tileIndices.length; k++) {
|
||||
if(memoryIndex == tileIndices[k]) {
|
||||
tileIndex = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the pixels of this tile to the resulting image.
|
||||
for(int j = 0; j < TILE_SIZE; j++) {
|
||||
// Get the color index for this pixel based on how the tile is flipped.
|
||||
int tilePixelIndex = switch(flipBits) {
|
||||
case 4 -> (TILE_WIDTH * (j / TILE_WIDTH) + TILE_WIDTH) - j % TILE_WIDTH - 1; // Flip horizontally
|
||||
case 8 -> TILE_SIZE - (TILE_WIDTH * (j / TILE_WIDTH) + TILE_WIDTH) + j % TILE_WIDTH; // Flip vertically
|
||||
case 12 -> TILE_SIZE - j - 1; // Flip horizontally & vertically
|
||||
default -> j; // Don't flip
|
||||
};
|
||||
|
||||
// Finally, set the pixel!
|
||||
int paletteIndex = tileData[tileIndex * TILE_SIZE + tilePixelIndex];
|
||||
image.setRGB(x + j % TILE_WIDTH, y + j / TILE_WIDTH, colorPalette[paletteIndex]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// There are enough tiles to fill up the entire screen, so let's just place them in order.
|
||||
for(int i = 0; i < SCREEN_TILE_COUNT; i++) {
|
||||
int x = i * TILE_WIDTH % SCREEN_WIDTH;
|
||||
int y = i * TILE_WIDTH / SCREEN_WIDTH * TILE_WIDTH;
|
||||
|
||||
for(int j = 0; j < TILE_SIZE; j++) {
|
||||
image.setRGB(x + j % TILE_WIDTH, y + j / TILE_WIDTH, colorPalette[tileData[i * TILE_SIZE + j]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
|
@ -124,13 +124,13 @@
|
|||
<!-- Misc Configurations -->
|
||||
<div class="grid-container">
|
||||
<div>
|
||||
<label>C-Gear Skin</label>
|
||||
<label>C-Gear Skin | </label><a href="#" onclick="return previewSkin('cgear-skin')">Preview</a>
|
||||
<select id="cgear-skin">
|
||||
<option value="none">Do not change</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label>Pokédex Skin</label>
|
||||
<label>Pokédex Skin | </label><a href="#" onclick="return previewSkin('dex-skin')">Preview</a>
|
||||
<select id="dex-skin">
|
||||
<option value="none">Do not change</option>
|
||||
</select>
|
||||
|
|
|
|||
|
|
@ -325,6 +325,18 @@ function closeItemForm() {
|
|||
window.location.href = "#";
|
||||
}
|
||||
|
||||
function previewSkin(inputElementId) {
|
||||
let value = document.getElementById(inputElementId).value;
|
||||
|
||||
if(value == "none") {
|
||||
window.alert("Please select a skin to preview it.");
|
||||
return false;
|
||||
}
|
||||
|
||||
window.open("/dashboard/previewskin?name=" + value);
|
||||
return false;
|
||||
}
|
||||
|
||||
async function fetchData(path) {
|
||||
return fetchData(path, "GET", null);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user