Skip to content

Commit a5c8b1a

Browse files
[#37] Add support for index-level difference building (#44)
* [#37] Add support for index-level difference building * Remove unused loadable field
1 parent e0f7b64 commit a5c8b1a

File tree

5 files changed

+114
-40
lines changed

5 files changed

+114
-40
lines changed

.changeset/friendly-feet-reflect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@osrs-wiki/cache-mediawiki": minor
3+
---
4+
5+
Add support for index-level (PerArchiveLoadable) difference building

src/scripts/differences/builder/builder.ts

Lines changed: 91 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ import {
2020
getFieldDifferencesRow,
2121
} from "./builder.utils";
2222
import { IndexType } from "../../../utils/cache2";
23+
import {
24+
NamedPerArchiveLoadable,
25+
PerArchiveLoadable,
26+
} from "../../../utils/cache2/Loadable";
2327
import { capitalize } from "../../../utils/string";
2428
import {
2529
ArchiveDifferences,
2630
CacheDifferences,
2731
ChangedResult,
2832
Difference,
33+
IndexDifferences,
2934
Result,
3035
} from "../differences.types";
3136

@@ -61,26 +66,69 @@ const differencesBuilder = (
6166
Object.keys(differences).forEach((index) => {
6267
const indexFeatureMap = indexNameMap[index as unknown as IndexType];
6368
if (indexFeatureMap) {
64-
const archives = differences[index as unknown as number];
65-
Object.keys(archives).forEach((archive) => {
66-
const archiveNumber = archive as unknown as number;
67-
const archiveDifferences = archives[archiveNumber];
68-
const indexFeature =
69-
"name" in indexFeatureMap
70-
? indexFeatureMap
71-
: indexFeatureMap[archiveNumber];
72-
if (indexFeature) {
73-
builder.addContents(
74-
buildArchiveDifferences(archiveDifferences, indexFeature)
75-
);
76-
}
77-
});
69+
const indexDifferences = differences[index as unknown as number];
70+
if ("name" in indexFeatureMap) {
71+
const indexFeature = indexFeatureMap as IndexFeatures;
72+
builder.addContents(
73+
buildIndexDifferences(indexDifferences, indexFeature)
74+
);
75+
} else {
76+
Object.keys(indexDifferences).forEach((archive) => {
77+
const archiveNumber = archive as unknown as number;
78+
const archiveDifferences = indexDifferences[archiveNumber];
79+
const indexFeature = indexFeatureMap[archiveNumber];
80+
if (indexFeature) {
81+
builder.addContents(
82+
buildArchiveDifferences(archiveDifferences, indexFeature)
83+
);
84+
}
85+
});
86+
}
7887
}
7988
});
80-
8189
return builder;
8290
};
8391

92+
/**
93+
* Build the media wiki content for the differences in two cache index's archives.
94+
* @param indexDifferences Differences between two cache's index's archives.
95+
* @param indexFeatures Meta deta for indexes
96+
* @returns {MediaWikiContent[]}
97+
*/
98+
const buildIndexDifferences = (
99+
indexDifferences: IndexDifferences,
100+
indexFeatures: IndexFeatures
101+
): MediaWikiContent[] => {
102+
const fileDifferences = Object.values(indexDifferences).map(
103+
(archive) => archive[0]
104+
);
105+
const addedResults: Result[] = _.pluck(fileDifferences, "added").filter(
106+
(entry) => entry !== null && entry !== undefined
107+
);
108+
109+
const removedResults: Result[] = _.pluck(fileDifferences, "removed").filter(
110+
(entry) => entry !== null && entry !== undefined
111+
);
112+
113+
const changedResults: ChangedResult[] = _.pluck(
114+
fileDifferences,
115+
"changed"
116+
).filter(
117+
(entry) =>
118+
entry !== null && entry !== undefined && Object.keys(entry).length > 0
119+
);
120+
121+
const content: MediaWikiContent[] = [
122+
new MediaWikiHeader(indexFeatures.name, 2),
123+
new MediaWikiBreak(),
124+
...buildResultTable(addedResults, indexFeatures, "added"),
125+
...buildResultTable(removedResults, indexFeatures, "removed"),
126+
...buildChangedResultTable(changedResults, indexFeatures),
127+
];
128+
129+
return content;
130+
};
131+
84132
/**
85133
* Build the media wiki content for the differences in two cache archive files.
86134
* @param archiveDifferences Differences between two cache's archives
@@ -91,12 +139,30 @@ const buildArchiveDifferences = (
91139
archiveDifferences: ArchiveDifferences,
92140
indexFeatures: IndexFeatures
93141
): MediaWikiContent[] => {
142+
const addedResults: Result[] = _.pluck(
143+
Object.values(archiveDifferences),
144+
"added"
145+
).filter((entry) => entry !== null && entry !== undefined);
146+
147+
const removedResults: Result[] = _.pluck(
148+
Object.values(archiveDifferences),
149+
"removed"
150+
).filter((entry) => entry !== null && entry !== undefined);
151+
152+
const changedResults: ChangedResult[] = _.pluck(
153+
Object.values(archiveDifferences),
154+
"changed"
155+
).filter(
156+
(entry) =>
157+
entry !== null && entry !== undefined && Object.keys(entry).length > 0
158+
);
159+
94160
const content: MediaWikiContent[] = [
95161
new MediaWikiHeader(indexFeatures.name, 2),
96162
new MediaWikiBreak(),
97-
...buildFullResultTable(archiveDifferences, indexFeatures, "added"),
98-
...buildFullResultTable(archiveDifferences, indexFeatures, "removed"),
99-
...buildChangedResultTable(archiveDifferences, indexFeatures),
163+
...buildResultTable(addedResults, indexFeatures, "added"),
164+
...buildResultTable(removedResults, indexFeatures, "removed"),
165+
...buildChangedResultTable(changedResults, indexFeatures),
100166
];
101167

102168
return content;
@@ -110,22 +176,15 @@ const buildArchiveDifferences = (
110176
* @returns {MediaWikiContent[]}
111177
*/
112178
const buildChangedResultTable = (
113-
archiveDifferences: ArchiveDifferences,
179+
changedResults: ChangedResult[],
114180
indexFeatures: IndexFeatures
115181
) => {
116182
const differenceName = resultNameMap.changed;
117183
const content: MediaWikiContent[] = [];
118-
const entries: ChangedResult[] = _.pluck(
119-
Object.values(archiveDifferences),
120-
"changed"
121-
).filter(
122-
(entry) =>
123-
entry !== null && entry !== undefined && Object.keys(entry).length > 0
124-
);
125184

126185
const rows: MediaWikiTableRow[] =
127-
entries?.length > 0
128-
? entries
186+
changedResults?.length > 0
187+
? changedResults
129188
.map<MediaWikiTableRow[]>((entry) => {
130189
const diffKeys = Object.keys(entry).filter((key) => {
131190
const isIdentifier = (
@@ -214,23 +273,19 @@ const buildChangedResultTable = (
214273
* @param type Definition for added or removed content
215274
* @returns
216275
*/
217-
const buildFullResultTable = (
218-
archiveDifferences: ArchiveDifferences,
276+
const buildResultTable = (
277+
results: Result[],
219278
indexFeatures: IndexFeatures,
220279
type: Difference
221280
): MediaWikiContent[] => {
222281
const differenceName = resultNameMap[type];
223282
const tableFields = indexFeatures.fields.map((field) => field.toString());
224283
const fields = [...indexFeatures.identifiers, ...tableFields];
225284
const content: MediaWikiContent[] = [];
226-
const entries: Result[] = _.pluck(
227-
Object.values(archiveDifferences),
228-
type
229-
).filter((entry) => entry !== null && entry !== undefined);
230285

231286
const rows: MediaWikiTableRow[] =
232-
entries?.length > 0
233-
? entries.map((entry) => {
287+
results?.length > 0
288+
? results.map((entry) => {
234289
const identifierCells = indexFeatures.identifiers.map(
235290
(identifier) => ({
236291
content: formatEntryIdentifier(

src/scripts/differences/builder/builder.types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import {
1010
Sprites,
1111
Struct,
1212
} from "../../../utils/cache2";
13-
import { PerFileLoadable } from "../../../utils/cache2/Loadable";
13+
import { Loadable } from "../../../utils/cache2/Loadable";
1414
import { Difference } from "../differences.types";
1515

16-
export type IndexFeature<T extends PerFileLoadable, Name> = {
16+
export type IndexFeature<T extends Loadable, Name> = {
1717
name: Name;
1818
identifiers: (keyof T)[];
1919
fields: (keyof T)[];
@@ -120,13 +120,13 @@ export const indexNameMap: {
120120
},
121121
},
122122
},
123-
/*[IndexType.Sprites]: {
123+
[IndexType.Sprites]: {
124124
name: "Sprites",
125125
identifiers: ["id"],
126126
fields: ["width", "height"],
127127
urls: {
128128
chisel: "",
129129
abex: "https://abextm.github.io/cache2/#/viewer/sprite/",
130130
},
131-
},*/
131+
},
132132
};

src/scripts/differences/differences.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ export type ResultValue =
4646
| undefined;
4747

4848
export type CacheDifferences = {
49+
// key: index id
4950
[key in number]?: IndexDifferences;
5051
};
5152

5253
export type IndexDifferences = {
54+
// key: archive id
5355
[key: number]: ArchiveDifferences;
5456
};
5557

5658
export type ArchiveDifferences = {
59+
// key: file id
5760
[key: number]: FileDifferences;
5861
};
5962

src/utils/cache2/loaders/Sprite.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ export class Sprite {
5353

5454
return new ImageData(Reader.makeViewOf(Uint8ClampedArray, out), tw, th);
5555
}
56+
57+
toJSON() {
58+
return {
59+
offsetX: this.offsetX,
60+
offsetY: this.offsetY,
61+
pixelsWidth: this.pixelsWidth,
62+
pixelsHeight: this.pixelsHeight,
63+
encodingMode: this.encodingMode,
64+
pixelsLength: this.pixels.length,
65+
};
66+
}
5667
}
5768

5869
@Typed

0 commit comments

Comments
 (0)