Skip to content

Commit

Permalink
Merge pull request #2151 from DuckySoLucky/perfImprovePacks
Browse files Browse the repository at this point in the history
perf: improve packs even futher - if it breaks it's cupcake's fault
  • Loading branch information
Shiiyu authored Jan 8, 2024
2 parents fa4b89b + 6b95a42 commit a15f751
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
type=item
items=minecraft:diamond
weight=1
nbt.display.Name=ipattern:*mining speed*
nbt.display.Lore.*=iregex:.*Level 50.*/.*
nbt.display.Name=ipattern:*mining speed*
138 changes: 81 additions & 57 deletions src/custom-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,11 @@ async function loadResourcePacks() {
}

if (property == "items" || property == "matchItems") {
const item = mcData.itemsByName[properties[property].replace("minecraft:", "")];
const item = mcData.itemsByName[properties[property].trim().replace("minecraft:", "")];

if (item) {
texture.id = item.id;
texture.damage = item.damage ?? 0;
}
}

Expand Down Expand Up @@ -522,24 +523,40 @@ export function getCompletePacks() {

const textureMap = new Map();
const allTextures = new Map();
setTimeout(async () => {
if (resourcePacks.length > 0) {
if (!resourcesReady) {
await readyPromise;
}
const textureIdDamageMap = new Map();
const allTexturesIdDamage = new Map();
const timeoutId = setTimeout(async () => {
if (!resourcesReady) {
await readyPromise;
}

for (const pack of resourcePacks) {
for (const texture of pack.textures) {
if ("skyblock_id" in texture === false) {
continue;
for (const pack of resourcePacks) {
for (const texture of pack.textures) {
if ("skyblock_id" in texture === false) {
if (pack.config.id === "RNBW_PLUS" && texture.file.startsWith("heart_")) {
// console.log(texture);
}

const damage = texture.damage ?? 0;
const itemId = texture.id;

if (textureIdDamageMap.has(`${pack.config.id}:${itemId}:${damage}`) === false) {
textureIdDamageMap.set(`${pack.config.id}:${itemId}:${damage}`, []);
}

textureMap.set(`${pack.config.id}:${texture.skyblock_id}`, texture);
allTextures.set(texture.skyblock_id, true);
textureIdDamageMap.get(`${pack.config.id}:${itemId}:${damage}`).push(texture);
allTexturesIdDamage.set(`${itemId}:${damage}`, true);

continue;
}

textureMap.set(`${pack.config.id}:${texture.skyblock_id}`, texture);
allTextures.set(texture.skyblock_id, true);
}
}
}, 1000);

clearTimeout(timeoutId);
}, 100);

/**
* Processes all textures that could potentially be connected to an item, then throws the one with biggest priority
Expand All @@ -550,8 +567,12 @@ setTimeout(async () => {
* @param {boolean} [options.debug]
* @returns {object} Item's texture
*/
export async function getTexture(item, { ignore_id = false, pack_ids = [], debug = false } = {}) {
if (allTextures.has(getId(item)) === false || getId(item) === "") {
export function getTexture(item, { ignore_id = false, pack_ids = [], debug = false, hotm = false } = {}) {

Check warning on line 570 in src/custom-resources.js

View workflow job for this annotation

GitHub Actions / check linting (es-lint)

Parameter name `ignore_id` must match one of the following formats: camelCase

Check warning on line 570 in src/custom-resources.js

View workflow job for this annotation

GitHub Actions / check linting (es-lint)

Parameter name `pack_ids` must match one of the following formats: camelCase
if (
((allTextures.has(getId(item)) === false && allTexturesIdDamage.has(`${item.id}:${item.Damage ?? 0}`) === false) ||
getId(item) === "") &&
hotm === false
) {
return null;
}

Expand Down Expand Up @@ -591,63 +612,66 @@ export async function getTexture(item, { ignore_id = false, pack_ids = [], debug
break;
}

for (const texture of pack.textures) {
if (!ignore_id && texture.id != item.id) {
continue;
}

if (!ignore_id && "damage" in texture && texture.damage != item.Damage) {
continue;
}

if (!ignore_id && texture.match === undefined && !("skyblock_id" in texture)) {
continue;
}

let matches = 0;
const cachedtextureIdDamageMap = textureIdDamageMap.get(`${pack.config.id}:${item.id}:${item.Damage ?? 0}`);
if (cachedtextureIdDamageMap) {
for (const texture of cachedtextureIdDamageMap) {
if (
texture.weight < outputTexture.weight ||
(texture.weight == outputTexture.weight && texture.file < outputTexture.file)
) {
continue;
}

let matchValues = [];
for (const match of texture.match) {
let { value, regex } = match;
if (!ignore_id && texture.id != item.id) {
continue;
}

if (value.endsWith(".*")) {
value = value.slice(0, -2);
if (!ignore_id && "damage" in texture && texture.damage != item.Damage) {
continue;
}

if (hasPath(item, "tag", ...value.split(".")) == false) {
if (!ignore_id && texture.match === undefined && !("skyblock_id" in texture)) {
continue;
}

matchValues = getPath(item, "tag", ...value.split("."));
matchValues = Array.isArray(matchValues) ? matchValues : [matchValues];
let matches = 0;

const slash = regex.lastIndexOf("/");
regex = new RegExp(regex.slice(1, slash), regex.slice(slash + 1));
let matchValues = [];
for (const match of texture.match) {
let { value, regex } = match;

if (matchValues.some((matchValue) => regex.test(matchValue.toString().replace(removeFormatting, "")))) {
matches++;
}
}
if (value.endsWith(".*")) {
value = value.slice(0, -2);
}

debugStats.found_matches += matches;
debugStats.processed_textures++;
if (hasPath(item, "tag", ...value.split(".")) == false) {
continue;
}

if (matches == texture.match.length) {
if (
texture.weight < outputTexture.weight ||
(texture.weight == outputTexture.weight && texture.file < outputTexture.file)
) {
continue;
matchValues = getPath(item, "tag", ...value.split("."));
matchValues = Array.isArray(matchValues) ? matchValues : [matchValues];

const slash = regex.lastIndexOf("/");
regex = new RegExp(regex.slice(1, slash), regex.slice(slash + 1));

if (matchValues.some((matchValue) => regex.test(matchValue.toString().replace(removeFormatting, "")))) {
matches++;
}
}

outputTexture = Object.assign(
{ pack: { base_path: pack.base_path ?? pack.basePath, config: pack.config } },
texture
);
debugStats.found_matches += matches;
debugStats.processed_textures++;

if (matches == texture.match.length) {
outputTexture = Object.assign(
{ pack: { base_path: pack.base_path ?? pack.basePath, config: pack.config } },
texture
);
}
}
}

debugStats.processed_packs++;
debugStats.processed_packs++;
}
}

if (!("path" in outputTexture)) {
Expand Down
2 changes: 1 addition & 1 deletion src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ export function addToItemLore(item, lore) {
* @returns {Promise<Item>} A Promise that resolves with the modified item.
*/
export async function applyResourcePack(item, packs) {
const customTexture = await getTexture(item, {
const customTexture = getTexture(item, {
ignore_id: false,
pack_ids: packs,
});
Expand Down
2 changes: 1 addition & 1 deletion src/routes/texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ router.all("/:textureId", cors(), async (req, res, next) => {
return;
}

const texture = await getTexture(textureId);
const texture = getTexture(textureId);

if (!texture) {
handleError(res, new Error("texture not found"), 404);
Expand Down
5 changes: 3 additions & 2 deletions src/stats/items/mining.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ export function getHotmItems(userProfile, packs) {
}

// Processing textures
output.forEach(async (item) => {
const customTexture = await getTexture(item, {
output.forEach((item) => {
const customTexture = getTexture(item, {
ignore_id: false,
pack_ids: packs,
hotm: true,
});

if (customTexture) {
Expand Down
2 changes: 1 addition & 1 deletion src/stats/items/processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export async function processItems(base64, source, customTextures = false, packs
}

if (item.tag?.ExtraAttributes?.skin == undefined && customTextures) {
const customTexture = await getTexture(item, {
const customTexture = getTexture(item, {
ignore_id: false,
pack_ids: packs,
});
Expand Down

0 comments on commit a15f751

Please sign in to comment.