Skip to content

Commit

Permalink
reimplement /compare using gallery loader (should be faster)
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Oct 24, 2023
1 parent 799cc33 commit 2b4d00e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/components/buttons/image/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component } from "@interfaces";
import { info } from "@helpers/logger";
import { Client, Message, ButtonInteraction, EmbedBuilder } from "@client";
import textureComparison, { comparisonTooBig } from "@functions/textureComparison";
import { InteractionEditReplyOptions, EmbedFooterData } from "discord.js";
import { InteractionEditReplyOptions } from "discord.js";

export default {
id: "compare",
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/functions/getTexture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EmbedBuilder } from "@client";
import TokenJson from "@json/tokens.json";
import { Contributor, Tokens } from "@interfaces";
import { Contributor, GalleryTexture, Tokens } from "@interfaces";
import axios from "axios";
import { APIEmbedField, AttachmentBuilder, Guild } from "discord.js";
import { magnify, magnifyToAttachment } from "@images/magnify";
Expand Down Expand Up @@ -133,7 +133,7 @@ export const getTexture = async (options: {
* @param texture texture to get paths and uses from
* @returns usable embed field data
*/
export const addPathsToEmbed = (texture: Texture): APIEmbedField[] => {
export const addPathsToEmbed = (texture: GalleryTexture | Texture): APIEmbedField[] => {
const tmp = {};
texture.uses.forEach((use) => {
texture.paths
Expand Down
27 changes: 15 additions & 12 deletions src/helpers/functions/textureComparison.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stitch from "@images/stitch";
import { magnifyToAttachment } from "@images/magnify";
import { loadImage } from "@napi-rs/canvas";
import { Image, loadImage } from "@napi-rs/canvas";
import {
Client,
EmbedBuilder,
Expand All @@ -10,7 +10,7 @@ import {
Message,
} from "@client";
import { addPathsToEmbed } from "@functions/getTexture";
import { Texture } from "@interfaces";
import { GalleryTexture } from "@interfaces";
import axios from "axios";
import { ActionRowBuilder, ButtonBuilder } from "discord.js";
import { template } from "@utility/buttons";
Expand Down Expand Up @@ -51,26 +51,29 @@ export function parseDisplay(display: string) {
*/
export default async function textureComparison(
client: Client,
id: number | string,
id: string,
display: string = "all",
): Promise<any> {
const result: Texture = (await axios.get(`${client.tokens.apiUrl}textures/${id}/all`)).data;
const displayed = parseDisplay(display);
const result: GalleryTexture = (
await axios.get(`${client.tokens.apiUrl}gallery/modal/${id}/latest`)
).data;

const defaultURL = `${client.tokens.apiUrl}textures/${id}/url/default/latest`;
const displayed = parseDisplay(display);
const defaultURL = result.urls.find((url) => url[0] == "default")?.[1];

const baseImage = await loadImage(defaultURL);
if (baseImage.width * baseImage.height * displayed.flat().length > 262144) return null;
const dimension = await loadImage(defaultURL);
if (dimension.width * dimension.height * displayed.flat().length > 262144) return null;

// get texture urls into 2d array using the parsed display
const loadedImages = [];
const loadedImages: Image[][] = [];
for (const packSet of displayed) {
// had problems with nested async mapping so this is easier for everyone
loadedImages.push([]);
for (const pack of packSet) {
const imgUrl = `${client.tokens.apiUrl}textures/${id}/url/${pack}/latest`;
const image = result.urls.find((url) => url[0] == pack)?.[1];
if (!image) continue;
try {
loadedImages.at(-1).push(await loadImage(imgUrl));
loadedImages.at(-1).push(await loadImage(image));
} catch {
// image doesn't exist yet
}
Expand All @@ -82,7 +85,7 @@ export default async function textureComparison(

const embed = new EmbedBuilder()
.setImage("attachment://magnified.png")
.setTitle(`[#${result.id}] ${result.name}`)
.setTitle(`[#${result.texture.id}] ${result.texture.name}`)
.setURL(`https://webapp.faithfulpack.net/#/gallery/java/32x/latest/all/?show=${id}`)
.addFields(addPathsToEmbed(result))
.setFooter({ text: `Displaying: ${display ?? "All"}` });
Expand Down
11 changes: 10 additions & 1 deletion src/interfaces/firestorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@ export interface Use {
edition: "java" | "bedrock" | "dungeons";
}

export interface Texture {
export interface BaseTexture {
id: string;
name: string;
tags: string[];
}

export interface Texture extends BaseTexture {
uses?: Use[];
paths?: Path[];
contributions?: Contribution[];
}

// used for comparison loader
export interface GalleryTexture extends Omit<Texture, keyof BaseTexture> {
texture: BaseTexture;
urls: [string, string];
}

export interface Contributor {
id: string;
contributions: number;
Expand Down

1 comment on commit 2b4d00e

@3vorp
Copy link
Contributor Author

@3vorp 3vorp commented on 2b4d00e Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to my incredibly scientific test of just counting how many seconds it took to load before and after the commit it went from around 4 seconds to 2 seconds (tested with dirt without display manipulation)

Please sign in to comment.