Skip to content

Commit

Permalink
feat: Speed up minifier and add icon sequences (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelBreslaw authored May 5, 2024
1 parent 6e3606d commit 88628d1
Show file tree
Hide file tree
Showing 4 changed files with 964 additions and 36 deletions.
Binary file modified minifier/bun.lockb
Binary file not shown.
138 changes: 104 additions & 34 deletions minifier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,85 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
[RepeatStringsName.TalentGridHash]: [],
};

type RepeatStringsName =
| "Descriptions"
| "DisplaySources"
| "ExpirationTooltip"
| "ItemTypeDisplayName"
| "ExpiredInActivityMessage"
| "IconWaterMark"
| "TraitIds"
| "UiItemDisplayStyle"
| "PlugCategoryIdentifier"
| "UiPlugLabel"
| "InsertionMaterialRequirementHash"
| "StackUniqueLabel"
| "BucketTypeHash"
| "Versions"
| "StatHash"
| "StatGroupHash"
| "DamageTypeHashes"
| "ItemValue"
| "TooltipNotifications"
| "ReusablePlugSetHash"
| "SingleInitialItemHash"
| "SocketCategoryHash"
| "SocketIndexes"
| "SocketCategories"
| "PlugCategoryHash"
| "SocketEntries"
| "SocketTypeHash"
| "TalentGridHash";

const repeatStringsMap: Record<RepeatStringsName, Map<string, number>> = {
Descriptions: new Map(),
DisplaySources: new Map(),
ExpirationTooltip: new Map(),
ItemTypeDisplayName: new Map(),
ExpiredInActivityMessage: new Map(),
IconWaterMark: new Map(),
TraitIds: new Map(),
UiItemDisplayStyle: new Map(),
PlugCategoryIdentifier: new Map(),
UiPlugLabel: new Map(),
InsertionMaterialRequirementHash: new Map(),
StackUniqueLabel: new Map(),
BucketTypeHash: new Map(),
Versions: new Map(),
StatHash: new Map(),
StatGroupHash: new Map(),
DamageTypeHashes: new Map(),
ItemValue: new Map(),
TooltipNotifications: new Map(),
ReusablePlugSetHash: new Map(),
SingleInitialItemHash: new Map(),
SocketCategoryHash: new Map(),
SocketIndexes: new Map(),
SocketCategories: new Map(),
PlugCategoryHash: new Map(),
SocketEntries: new Map(),
SocketTypeHash: new Map(),
TalentGridHash: new Map(),
};

// Send a repeat string and get a index value back
function getRepeatStringIndex(name: RepeatStringsName, s: string): number {
const index = repeatStrings[name].indexOf(s);
if (index === -1) {
// function getRepeatStringIndexMap(name: RepeatStringsName, s: string): number {
// const index = repeatStrings[name].indexOf(s);
// if (index === -1) {
// repeatStrings[name].push(s);
// return getRepeatStringIndexMap(name, s);
// }

// return index;
// }

function getRepeatStringIndexMap(name: RepeatStringsName, s: string): number {
if (!repeatStringsMap[name].has(s)) {
repeatStringsMap[name].set(s, repeatStringsMap[name].size);
repeatStrings[name].push(s);
return getRepeatStringIndex(name, s);
}

return index;
return repeatStringsMap[name].get(s)!;
}

const processedData: ProcessedData = { helpers: {}, items: {}, version: 3, id: uniqueKey };
Expand All @@ -189,7 +259,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD

const description = displayProperties.description;
if (description) {
item.d = getRepeatStringIndex(RepeatStringsName.Descriptions, description);
item.d = getRepeatStringIndexMap(RepeatStringsName.Descriptions, description);
}

const icon = displayProperties.icon;
Expand Down Expand Up @@ -239,7 +309,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD

const displaySource = jsonData[key].displaySource;
if (displaySource) {
item.ds = getRepeatStringIndex(RepeatStringsName.DisplaySources, displaySource);
item.ds = getRepeatStringIndexMap(RepeatStringsName.DisplaySources, displaySource);
}

const itemType = jsonData[key].itemType;
Expand All @@ -259,7 +329,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD

const itemTypeDisplayName = jsonData[key].itemTypeDisplayName;
if (itemTypeDisplayName) {
item.itd = getRepeatStringIndex(RepeatStringsName.ItemTypeDisplayName, itemTypeDisplayName);
item.itd = getRepeatStringIndexMap(RepeatStringsName.ItemTypeDisplayName, itemTypeDisplayName);
}

/// Values
Expand All @@ -276,7 +346,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
if (itemHash === 0) {
continue;
}
val.ih = getRepeatStringIndex(RepeatStringsName.ItemValue, itemHash);
val.ih = getRepeatStringIndexMap(RepeatStringsName.ItemValue, itemHash);
if (itemValue.quantity > 0) {
val.q = itemValue.quantity;
}
Expand All @@ -301,22 +371,22 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD

const bucketTypeHash = inventory?.bucketTypeHash;
if (bucketTypeHash) {
item.b = getRepeatStringIndex(RepeatStringsName.BucketTypeHash, bucketTypeHash);
item.b = getRepeatStringIndexMap(RepeatStringsName.BucketTypeHash, bucketTypeHash);
}

const stackUniqueLabel = inventory?.stackUniqueLabel;
if (stackUniqueLabel) {
item.su = getRepeatStringIndex(RepeatStringsName.StackUniqueLabel, stackUniqueLabel);
item.su = getRepeatStringIndexMap(RepeatStringsName.StackUniqueLabel, stackUniqueLabel);
}

const expirationTooltip = inventory?.expirationTooltip;
if (expirationTooltip) {
item.et = getRepeatStringIndex(RepeatStringsName.ExpirationTooltip, expirationTooltip);
item.et = getRepeatStringIndexMap(RepeatStringsName.ExpirationTooltip, expirationTooltip);
}

const expiredInActivityMessage = inventory?.expiredInActivityMessage;
if (expiredInActivityMessage) {
item.em = getRepeatStringIndex(RepeatStringsName.ExpiredInActivityMessage, expiredInActivityMessage);
item.em = getRepeatStringIndexMap(RepeatStringsName.ExpiredInActivityMessage, expiredInActivityMessage);
}

const maxStackSize = inventory?.maxStackSize;
Expand Down Expand Up @@ -349,7 +419,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
const dt: any[] = [];

for (const damageHash of damageTypeHashes) {
dt.push(getRepeatStringIndex(RepeatStringsName.DamageTypeHashes, damageHash));
dt.push(getRepeatStringIndexMap(RepeatStringsName.DamageTypeHashes, damageHash));
}

if (dt.length > 0) {
Expand All @@ -371,17 +441,17 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
/// Is this needed any more?
const talentGridHash = jsonData[key].talentGrid?.talentGridHash;
if (talentGridHash && talentGridHash !== 0) {
item.th = getRepeatStringIndex(RepeatStringsName.TalentGridHash, talentGridHash);
item.th = getRepeatStringIndexMap(RepeatStringsName.TalentGridHash, talentGridHash);
}

const uiItemDisplayStyle = jsonData[key].uiItemDisplayStyle;
if (uiItemDisplayStyle) {
item.ids = getRepeatStringIndex(RepeatStringsName.UiItemDisplayStyle, uiItemDisplayStyle);
item.ids = getRepeatStringIndexMap(RepeatStringsName.UiItemDisplayStyle, uiItemDisplayStyle);
}

const iconWatermark = jsonData[key].iconWatermark;
if (iconWatermark) {
item.iw = getRepeatStringIndex(RepeatStringsName.IconWaterMark, stripImageUrl(iconWatermark));
item.iw = getRepeatStringIndexMap(RepeatStringsName.IconWaterMark, stripImageUrl(iconWatermark));
}

// Quality
Expand All @@ -392,7 +462,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
if (versions) {
const qv: any[] = [];
for (const version of versions) {
qv.push(getRepeatStringIndex(RepeatStringsName.Versions, version.powerCapHash));
qv.push(getRepeatStringIndexMap(RepeatStringsName.Versions, version.powerCapHash));
}
if (qv.length > 0) {
item.qv = qv;
Expand All @@ -407,7 +477,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
if (!watermark) {
continue;
}
dvwi.push(getRepeatStringIndex(RepeatStringsName.IconWaterMark, stripImageUrl(watermark)));
dvwi.push(getRepeatStringIndexMap(RepeatStringsName.IconWaterMark, stripImageUrl(watermark)));
}

if (dvwi.length > 0) {
Expand All @@ -426,7 +496,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
const s: any = {};
const sortedStatKeys = Object.keys(itemStats).sort((a, b) => parseFloat(a) - parseFloat(b));
for (const key of sortedStatKeys) {
s[getRepeatStringIndex(RepeatStringsName.StatHash, key)] = itemStats[key].value;
s[getRepeatStringIndexMap(RepeatStringsName.StatHash, key)] = itemStats[key].value;
}

if (Object.keys(s).length > 0) {
Expand All @@ -435,7 +505,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD

var statGroupHash = stats.statGroupHash;
if (statGroupHash) {
st.sgs = getRepeatStringIndex(RepeatStringsName.StatGroupHash, statGroupHash);
st.sgs = getRepeatStringIndexMap(RepeatStringsName.StatGroupHash, statGroupHash);
}

if (Object.keys(st).length > 0) {
Expand Down Expand Up @@ -475,7 +545,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
const ttString = tt.displayString;

if (ttString) {
ttn.push(getRepeatStringIndex(RepeatStringsName.TooltipNotifications, ttString));
ttn.push(getRepeatStringIndexMap(RepeatStringsName.TooltipNotifications, ttString));
}

/// NOTE!!! Ishtar only uses the first tooltip so no need to keep the others?
Expand Down Expand Up @@ -520,7 +590,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD

const plugCategoryHash = plug?.plugCategoryHash;
if (plugCategoryHash) {
p.p = getRepeatStringIndex(RepeatStringsName.PlugCategoryHash, plugCategoryHash);
p.p = getRepeatStringIndexMap(RepeatStringsName.PlugCategoryHash, plugCategoryHash);
}

/// NOTE: This change breaks the existing app. All it needs to do to get the correct
Expand All @@ -529,17 +599,17 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
const plugCategoryIdentifier = plug.plugCategoryIdentifier;
if (plugCategoryIdentifier) {
/// Intentionally call the function but don't save the result here. The p.p index will be the same.
getRepeatStringIndex(RepeatStringsName.PlugCategoryIdentifier, plugCategoryIdentifier);
getRepeatStringIndexMap(RepeatStringsName.PlugCategoryIdentifier, plugCategoryIdentifier);
}

var uiPlugLabel = plug.uiPlugLabel;
if (uiPlugLabel) {
p.pl = getRepeatStringIndex(RepeatStringsName.UiPlugLabel, uiPlugLabel);
p.pl = getRepeatStringIndexMap(RepeatStringsName.UiPlugLabel, uiPlugLabel);
}

const insertionMaterialRequirementHash = plug?.insertionMaterialRequirementHash;
if (insertionMaterialRequirementHash && insertionMaterialRequirementHash !== 0) {
p.im = getRepeatStringIndex(
p.im = getRepeatStringIndexMap(
RepeatStringsName.InsertionMaterialRequirementHash,
insertionMaterialRequirementHash,
);
Expand All @@ -555,7 +625,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
const ti: any[] = [];

for (const traitId of traitIds) {
ti.push(getRepeatStringIndex(RepeatStringsName.TraitIds, traitId));
ti.push(getRepeatStringIndexMap(RepeatStringsName.TraitIds, traitId));
}

if (ti.length > 0) {
Expand All @@ -581,17 +651,17 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD

const st = socketEntry?.socketTypeHash;
if (st) {
socEntry.st = getRepeatStringIndex(RepeatStringsName.SocketTypeHash, st);
socEntry.st = getRepeatStringIndexMap(RepeatStringsName.SocketTypeHash, st);
}

const rp = socketEntry.reusablePlugSetHash;
if (rp) {
socEntry.r = getRepeatStringIndex(RepeatStringsName.ReusablePlugSetHash, rp);
socEntry.r = getRepeatStringIndexMap(RepeatStringsName.ReusablePlugSetHash, rp);
}

const s = socketEntry.singleInitialItemHash;
if (s && s !== 0) {
socEntry.s = getRepeatStringIndex(RepeatStringsName.SingleInitialItemHash, s);
socEntry.s = getRepeatStringIndexMap(RepeatStringsName.SingleInitialItemHash, s);
}

if (socEntry) {
Expand All @@ -600,7 +670,7 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD
}

if (se.length > 0) {
sk.se = getRepeatStringIndex(RepeatStringsName.SocketEntries, JSON.stringify(se));
sk.se = getRepeatStringIndexMap(RepeatStringsName.SocketEntries, JSON.stringify(se));
}

const scJson: any[] = [];
Expand All @@ -609,19 +679,19 @@ function createMiniDefinition(jsonData: JsonData, uniqueKey: string): ProcessedD

var h = socketCategory?.socketCategoryHash;
if (h) {
socCatEntry.h = getRepeatStringIndex(RepeatStringsName.SocketCategoryHash, h);
socCatEntry.h = getRepeatStringIndexMap(RepeatStringsName.SocketCategoryHash, h);
}

/// NOTE: In ishtar you want to Json.parse the string you get to turn it into a json array.
var socketIndexes = socketCategory?.socketIndexes;
if (socketIndexes) {
socCatEntry.i = getRepeatStringIndex(RepeatStringsName.SocketIndexes, JSON.stringify(socketIndexes));
socCatEntry.i = getRepeatStringIndexMap(RepeatStringsName.SocketIndexes, JSON.stringify(socketIndexes));
scJson.push(socCatEntry);
}
}
if (scJson.length > 0) {
/// NOTE: In ishtar you want to Json.parse the string you get to turn it into a json array.
sk.sc = getRepeatStringIndex(RepeatStringsName.SocketCategories, JSON.stringify(scJson));
sk.sc = getRepeatStringIndexMap(RepeatStringsName.SocketCategories, JSON.stringify(scJson));
}

if (Object.keys(sk).length > 0) {
Expand Down
Loading

0 comments on commit 88628d1

Please sign in to comment.