From ef2aa1b264ff9eeb769974e161152a728287031c Mon Sep 17 00:00:00 2001 From: Allen Kinzalow Date: Tue, 6 Aug 2024 22:45:30 -0400 Subject: [PATCH 1/2] [#37] Add support for index-level difference building --- .changeset/friendly-feet-reflect.md | 5 + src/scripts/differences/builder/builder.ts | 127 +++++++++++++----- .../differences/builder/builder.types.ts | 17 ++- src/scripts/differences/differences.types.ts | 3 + src/utils/cache2/loaders/Sprite.ts | 11 ++ 5 files changed, 123 insertions(+), 40 deletions(-) create mode 100644 .changeset/friendly-feet-reflect.md diff --git a/.changeset/friendly-feet-reflect.md b/.changeset/friendly-feet-reflect.md new file mode 100644 index 0000000..6d9cc95 --- /dev/null +++ b/.changeset/friendly-feet-reflect.md @@ -0,0 +1,5 @@ +--- +"@osrs-wiki/cache-mediawiki": minor +--- + +Add support for index-level (PerArchiveLoadable) difference building diff --git a/src/scripts/differences/builder/builder.ts b/src/scripts/differences/builder/builder.ts index 55a5d60..1a62205 100644 --- a/src/scripts/differences/builder/builder.ts +++ b/src/scripts/differences/builder/builder.ts @@ -20,12 +20,17 @@ import { getFieldDifferencesRow, } from "./builder.utils"; import { IndexType } from "../../../utils/cache2"; +import { + NamedPerArchiveLoadable, + PerArchiveLoadable, +} from "../../../utils/cache2/Loadable"; import { capitalize } from "../../../utils/string"; import { ArchiveDifferences, CacheDifferences, ChangedResult, Difference, + IndexDifferences, Result, } from "../differences.types"; @@ -61,26 +66,69 @@ const differencesBuilder = ( Object.keys(differences).forEach((index) => { const indexFeatureMap = indexNameMap[index as unknown as IndexType]; if (indexFeatureMap) { - const archives = differences[index as unknown as number]; - Object.keys(archives).forEach((archive) => { - const archiveNumber = archive as unknown as number; - const archiveDifferences = archives[archiveNumber]; - const indexFeature = - "name" in indexFeatureMap - ? indexFeatureMap - : indexFeatureMap[archiveNumber]; - if (indexFeature) { - builder.addContents( - buildArchiveDifferences(archiveDifferences, indexFeature) - ); - } - }); + const indexDifferences = differences[index as unknown as number]; + if ("name" in indexFeatureMap) { + const indexFeature = indexFeatureMap as IndexFeatures; + builder.addContents( + buildIndexDifferences(indexDifferences, indexFeature) + ); + } else { + Object.keys(indexDifferences).forEach((archive) => { + const archiveNumber = archive as unknown as number; + const archiveDifferences = indexDifferences[archiveNumber]; + const indexFeature = indexFeatureMap[archiveNumber]; + if (indexFeature) { + builder.addContents( + buildArchiveDifferences(archiveDifferences, indexFeature) + ); + } + }); + } } }); - return builder; }; +/** + * Build the media wiki content for the differences in two cache index's archives. + * @param indexDifferences Differences between two cache's index's archives. + * @param indexFeatures Meta deta for indexes + * @returns {MediaWikiContent[]} + */ +const buildIndexDifferences = ( + indexDifferences: IndexDifferences, + indexFeatures: IndexFeatures +): MediaWikiContent[] => { + const fileDifferences = Object.values(indexDifferences).map( + (archive) => archive[0] + ); + const addedResults: Result[] = _.pluck(fileDifferences, "added").filter( + (entry) => entry !== null && entry !== undefined + ); + + const removedResults: Result[] = _.pluck(fileDifferences, "removed").filter( + (entry) => entry !== null && entry !== undefined + ); + + const changedResults: ChangedResult[] = _.pluck( + fileDifferences, + "changed" + ).filter( + (entry) => + entry !== null && entry !== undefined && Object.keys(entry).length > 0 + ); + + const content: MediaWikiContent[] = [ + new MediaWikiHeader(indexFeatures.name, 2), + new MediaWikiBreak(), + ...buildResultTable(addedResults, indexFeatures, "added"), + ...buildResultTable(removedResults, indexFeatures, "removed"), + ...buildChangedResultTable(changedResults, indexFeatures), + ]; + + return content; +}; + /** * Build the media wiki content for the differences in two cache archive files. * @param archiveDifferences Differences between two cache's archives @@ -91,12 +139,30 @@ const buildArchiveDifferences = ( archiveDifferences: ArchiveDifferences, indexFeatures: IndexFeatures ): MediaWikiContent[] => { + const addedResults: Result[] = _.pluck( + Object.values(archiveDifferences), + "added" + ).filter((entry) => entry !== null && entry !== undefined); + + const removedResults: Result[] = _.pluck( + Object.values(archiveDifferences), + "removed" + ).filter((entry) => entry !== null && entry !== undefined); + + const changedResults: ChangedResult[] = _.pluck( + Object.values(archiveDifferences), + "changed" + ).filter( + (entry) => + entry !== null && entry !== undefined && Object.keys(entry).length > 0 + ); + const content: MediaWikiContent[] = [ new MediaWikiHeader(indexFeatures.name, 2), new MediaWikiBreak(), - ...buildFullResultTable(archiveDifferences, indexFeatures, "added"), - ...buildFullResultTable(archiveDifferences, indexFeatures, "removed"), - ...buildChangedResultTable(archiveDifferences, indexFeatures), + ...buildResultTable(addedResults, indexFeatures, "added"), + ...buildResultTable(removedResults, indexFeatures, "removed"), + ...buildChangedResultTable(changedResults, indexFeatures), ]; return content; @@ -110,22 +176,15 @@ const buildArchiveDifferences = ( * @returns {MediaWikiContent[]} */ const buildChangedResultTable = ( - archiveDifferences: ArchiveDifferences, + changedResults: ChangedResult[], indexFeatures: IndexFeatures ) => { const differenceName = resultNameMap.changed; const content: MediaWikiContent[] = []; - const entries: ChangedResult[] = _.pluck( - Object.values(archiveDifferences), - "changed" - ).filter( - (entry) => - entry !== null && entry !== undefined && Object.keys(entry).length > 0 - ); const rows: MediaWikiTableRow[] = - entries?.length > 0 - ? entries + changedResults?.length > 0 + ? changedResults .map((entry) => { const diffKeys = Object.keys(entry).filter((key) => { const isIdentifier = ( @@ -214,8 +273,8 @@ const buildChangedResultTable = ( * @param type Definition for added or removed content * @returns */ -const buildFullResultTable = ( - archiveDifferences: ArchiveDifferences, +const buildResultTable = ( + results: Result[], indexFeatures: IndexFeatures, type: Difference ): MediaWikiContent[] => { @@ -223,14 +282,10 @@ const buildFullResultTable = ( const tableFields = indexFeatures.fields.map((field) => field.toString()); const fields = [...indexFeatures.identifiers, ...tableFields]; const content: MediaWikiContent[] = []; - const entries: Result[] = _.pluck( - Object.values(archiveDifferences), - type - ).filter((entry) => entry !== null && entry !== undefined); const rows: MediaWikiTableRow[] = - entries?.length > 0 - ? entries.map((entry) => { + results?.length > 0 + ? results.map((entry) => { const identifierCells = indexFeatures.identifiers.map( (identifier) => ({ content: formatEntryIdentifier( diff --git a/src/scripts/differences/builder/builder.types.ts b/src/scripts/differences/builder/builder.types.ts index 9f1bd29..b92ddd9 100644 --- a/src/scripts/differences/builder/builder.types.ts +++ b/src/scripts/differences/builder/builder.types.ts @@ -10,13 +10,14 @@ import { Sprites, Struct, } from "../../../utils/cache2"; -import { PerFileLoadable } from "../../../utils/cache2/Loadable"; +import { Loadable } from "../../../utils/cache2/Loadable"; import { Difference } from "../differences.types"; -export type IndexFeature = { +export type IndexFeature = { name: Name; identifiers: (keyof T)[]; fields: (keyof T)[]; + loadable: Loadable; urls?: IndexURLs; }; @@ -48,6 +49,7 @@ export const indexNameMap: { name: "Objects", identifiers: ["name", "id"], fields: ["actions"], + loadable: Obj, urls: { chisel: "https://chisel.weirdgloop.org/moid/object_id.html#", abex: "https://abextm.github.io/cache2/#/viewer/obj/", @@ -57,6 +59,7 @@ export const indexNameMap: { name: "Enums", identifiers: ["id"], fields: ["defaultValue", "map"], + loadable: Enum, urls: { chisel: "https://chisel.weirdgloop.org/structs/index.html?type=enums&id=", @@ -67,6 +70,7 @@ export const indexNameMap: { name: "Npcs", identifiers: ["name", "id"], fields: ["combatLevel", "actions"], + loadable: NPC, urls: { chisel: "https://chisel.weirdgloop.org/moid/npc_id.html#", abex: "https://abextm.github.io/cache2/#/viewer/npc/", @@ -86,6 +90,7 @@ export const indexNameMap: { "price", "weight", ], + loadable: Item, urls: { chisel: "https://chisel.weirdgloop.org/moid/item_id.html#", abex: "https://abextm.github.io/cache2/#/viewer/item/", @@ -95,6 +100,7 @@ export const indexNameMap: { name: "Params", identifiers: ["id"], fields: ["type", "defaultInt", "defaultString", "isMembers"], + loadable: Param, urls: { chisel: "https://chisel.weirdgloop.org/structs/index.html?type=enums&id=", @@ -105,6 +111,7 @@ export const indexNameMap: { name: "Structs", identifiers: ["id"], fields: ["params"], + loadable: Struct, urls: { chisel: "https://chisel.weirdgloop.org/structs/index.html?type=structs&id=", @@ -115,18 +122,20 @@ export const indexNameMap: { name: "Database Rows", identifiers: ["id"], fields: ["table", "values"], + loadable: DBRow, urls: { abex: "https://abextm.github.io/cache2/#/viewer/dbrow/", }, }, }, - /*[IndexType.Sprites]: { + [IndexType.Sprites]: { name: "Sprites", identifiers: ["id"], fields: ["width", "height"], + loadable: Sprites, urls: { chisel: "", abex: "https://abextm.github.io/cache2/#/viewer/sprite/", }, - },*/ + }, }; diff --git a/src/scripts/differences/differences.types.ts b/src/scripts/differences/differences.types.ts index 65fa646..a6ce29a 100644 --- a/src/scripts/differences/differences.types.ts +++ b/src/scripts/differences/differences.types.ts @@ -46,14 +46,17 @@ export type ResultValue = | undefined; export type CacheDifferences = { + // key: index id [key in number]?: IndexDifferences; }; export type IndexDifferences = { + // key: archive id [key: number]: ArchiveDifferences; }; export type ArchiveDifferences = { + // key: file id [key: number]: FileDifferences; }; diff --git a/src/utils/cache2/loaders/Sprite.ts b/src/utils/cache2/loaders/Sprite.ts index 08a25ae..f587463 100644 --- a/src/utils/cache2/loaders/Sprite.ts +++ b/src/utils/cache2/loaders/Sprite.ts @@ -53,6 +53,17 @@ export class Sprite { return new ImageData(Reader.makeViewOf(Uint8ClampedArray, out), tw, th); } + + toJSON() { + return { + offsetX: this.offsetX, + offsetY: this.offsetY, + pixelsWidth: this.pixelsWidth, + pixelsHeight: this.pixelsHeight, + encodingMode: this.encodingMode, + pixelsLength: this.pixels.length, + }; + } } @Typed From c55a4b84182ce3d4ea10464c775f22693546e813 Mon Sep 17 00:00:00 2001 From: Allen Kinzalow Date: Tue, 6 Aug 2024 22:48:39 -0400 Subject: [PATCH 2/2] Remove unused loadable field --- src/scripts/differences/builder/builder.types.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/scripts/differences/builder/builder.types.ts b/src/scripts/differences/builder/builder.types.ts index b92ddd9..a1b79ff 100644 --- a/src/scripts/differences/builder/builder.types.ts +++ b/src/scripts/differences/builder/builder.types.ts @@ -17,7 +17,6 @@ export type IndexFeature = { name: Name; identifiers: (keyof T)[]; fields: (keyof T)[]; - loadable: Loadable; urls?: IndexURLs; }; @@ -49,7 +48,6 @@ export const indexNameMap: { name: "Objects", identifiers: ["name", "id"], fields: ["actions"], - loadable: Obj, urls: { chisel: "https://chisel.weirdgloop.org/moid/object_id.html#", abex: "https://abextm.github.io/cache2/#/viewer/obj/", @@ -59,7 +57,6 @@ export const indexNameMap: { name: "Enums", identifiers: ["id"], fields: ["defaultValue", "map"], - loadable: Enum, urls: { chisel: "https://chisel.weirdgloop.org/structs/index.html?type=enums&id=", @@ -70,7 +67,6 @@ export const indexNameMap: { name: "Npcs", identifiers: ["name", "id"], fields: ["combatLevel", "actions"], - loadable: NPC, urls: { chisel: "https://chisel.weirdgloop.org/moid/npc_id.html#", abex: "https://abextm.github.io/cache2/#/viewer/npc/", @@ -90,7 +86,6 @@ export const indexNameMap: { "price", "weight", ], - loadable: Item, urls: { chisel: "https://chisel.weirdgloop.org/moid/item_id.html#", abex: "https://abextm.github.io/cache2/#/viewer/item/", @@ -100,7 +95,6 @@ export const indexNameMap: { name: "Params", identifiers: ["id"], fields: ["type", "defaultInt", "defaultString", "isMembers"], - loadable: Param, urls: { chisel: "https://chisel.weirdgloop.org/structs/index.html?type=enums&id=", @@ -111,7 +105,6 @@ export const indexNameMap: { name: "Structs", identifiers: ["id"], fields: ["params"], - loadable: Struct, urls: { chisel: "https://chisel.weirdgloop.org/structs/index.html?type=structs&id=", @@ -122,7 +115,6 @@ export const indexNameMap: { name: "Database Rows", identifiers: ["id"], fields: ["table", "values"], - loadable: DBRow, urls: { abex: "https://abextm.github.io/cache2/#/viewer/dbrow/", }, @@ -132,7 +124,6 @@ export const indexNameMap: { name: "Sprites", identifiers: ["id"], fields: ["width", "height"], - loadable: Sprites, urls: { chisel: "", abex: "https://abextm.github.io/cache2/#/viewer/sprite/",