Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#37] Add support for index-level difference building #44

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/friendly-feet-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osrs-wiki/cache-mediawiki": minor
---

Add support for index-level (PerArchiveLoadable) difference building
127 changes: 91 additions & 36 deletions src/scripts/differences/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
getFieldDifferencesRow,
} from "./builder.utils";
import { IndexType } from "../../../utils/cache2";
import {
NamedPerArchiveLoadable,

Check warning on line 24 in src/scripts/differences/builder/builder.ts

View workflow job for this annotation

GitHub Actions / Lint, Built, & Test / Lint, Built, & Test

'NamedPerArchiveLoadable' is defined but never used
PerArchiveLoadable,

Check warning on line 25 in src/scripts/differences/builder/builder.ts

View workflow job for this annotation

GitHub Actions / Lint, Built, & Test / Lint, Built, & Test

'PerArchiveLoadable' is defined but never used
} from "../../../utils/cache2/Loadable";
import { capitalize } from "../../../utils/string";
import {
ArchiveDifferences,
CacheDifferences,
ChangedResult,
Difference,
IndexDifferences,
Result,
} from "../differences.types";

Expand Down Expand Up @@ -61,26 +66,69 @@
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
Expand All @@ -91,12 +139,30 @@
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;
Expand All @@ -110,22 +176,15 @@
* @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<MediaWikiTableRow[]>((entry) => {
const diffKeys = Object.keys(entry).filter((key) => {
const isIdentifier = (
Expand Down Expand Up @@ -214,23 +273,19 @@
* @param type Definition for added or removed content
* @returns
*/
const buildFullResultTable = (
archiveDifferences: ArchiveDifferences,
const buildResultTable = (
results: Result[],
indexFeatures: IndexFeatures,
type: Difference
): MediaWikiContent[] => {
const differenceName = resultNameMap[type];
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(
Expand Down
8 changes: 4 additions & 4 deletions src/scripts/differences/builder/builder.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ 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<T extends PerFileLoadable, Name> = {
export type IndexFeature<T extends Loadable, Name> = {
name: Name;
identifiers: (keyof T)[];
fields: (keyof T)[];
Expand Down Expand Up @@ -120,13 +120,13 @@ export const indexNameMap: {
},
},
},
/*[IndexType.Sprites]: {
[IndexType.Sprites]: {
name: "Sprites",
identifiers: ["id"],
fields: ["width", "height"],
urls: {
chisel: "",
abex: "https://abextm.github.io/cache2/#/viewer/sprite/",
},
},*/
},
};
3 changes: 3 additions & 0 deletions src/scripts/differences/differences.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
11 changes: 11 additions & 0 deletions src/utils/cache2/loaders/Sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading