Skip to content

Commit a31732d

Browse files
Merge pull request #5 from thdaele/master
Feather diff viewer
2 parents 40d036c + cc9c64a commit a31732d

8 files changed

Lines changed: 502 additions & 122 deletions

File tree

public/develop.js

Lines changed: 12 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,15 @@
1-
(async () => {
2-
3-
const URL = "ornithemc.net";
4-
const META = "meta." + URL;
5-
const VERSION = "v3";
6-
7-
8-
function makeMetaUrl(...pathComponents) {
9-
let url = META + "/" + VERSION;
10-
for (const pathComponent of pathComponents) {
11-
url += "/";
12-
url += pathComponent;
13-
}
14-
return "https://" + url;
15-
}
16-
17-
async function getFromMeta(...pathComponents) {
18-
const response = await fetch(makeMetaUrl(...pathComponents));
19-
return response.json();
20-
}
21-
22-
async function getMinecraftVersionsMeta() {
23-
return await getFromMeta("versions", "game");
24-
}
25-
26-
async function getFeatherVersionMeta(mcVersion) {
27-
return await getFromMeta("versions", "feather", mcVersion);
28-
}
29-
30-
async function getRavenVersionMeta(mcVersion) {
31-
return await getFromMeta("versions", "raven", mcVersion);
32-
}
33-
34-
async function getSparrowVersionMeta(mcVersion) {
35-
return await getFromMeta("versions", "sparrow", mcVersion);
36-
}
37-
38-
async function getNestsVersionMeta(mcVersion) {
39-
return await getFromMeta("versions", "nests", mcVersion);
40-
}
41-
42-
async function getLoaderVersionsMeta(loader) {
43-
return await getFromMeta("versions", loader + "-loader");
44-
}
45-
46-
async function getOslVersionsMeta() {
47-
return await getFromMeta("versions", "osl");
48-
}
49-
50-
function compareVersion(sv1, sv2) {
51-
function rec(v1, v2) {
52-
if (v1.length === 0 && v2.length === 0) return 0;
53-
if (v1.length === 0) return 1;
54-
if (v2.length === 0) return -1;
55-
const [head1, ...tail1] = v1;
56-
const [head2, ...tail2] = v2;
57-
const ih1 = parseInt(head1);
58-
const ih2 = parseInt(head2);
59-
if (ih1 < ih2) return 1;
60-
if (ih1 > ih2) return -1;
61-
return rec(tail1, tail2);
62-
}
63-
64-
return rec(sv1.split("."), sv2.split("."));
65-
}
66-
67-
async function getMinecraftVersions() {
68-
return await getMinecraftVersionsMeta()
69-
.then(l => l.map(v => v.version));
70-
}
71-
72-
async function getMinecraftStableVersions() {
73-
return await getMinecraftVersionsMeta()
74-
.then(l => l.filter(v => v.stable))
75-
.then(l => l.map(v => v.version));
76-
}
77-
78-
async function getLatestFeatherBuild(mcVersion) {
79-
return await getFeatherVersionMeta(mcVersion)
80-
.then(l => l.sort((e1, e2) => e2.build - e1.build))
81-
.then(s => { console.log(s); return s; })
82-
.then(([head, ..._]) => head)
83-
.then(e => e !== undefined ? e.build : null);
84-
}
85-
86-
async function getLatestRavenBuild(mcVersion) {
87-
return await getRavenVersionMeta(mcVersion)
88-
.then(l => l.sort((e1, e2) => e2.build - e1.build))
89-
.then(([head, ..._]) => head)
90-
.then(e => e !== undefined ? e.build : null);
91-
}
92-
93-
async function getLatestSparrowBuild(mcVersion) {
94-
return await getSparrowVersionMeta(mcVersion)
95-
.then(l => l.sort((e1, e2) => e2.build - e1.build))
96-
.then(([head, ..._]) => head)
97-
.then(e => e !== undefined ? e.build : null);
98-
}
99-
100-
async function getLatestNestsBuild(mcVersion) {
101-
return await getNestsVersionMeta(mcVersion)
102-
.then(l => l.sort((e1, e2) => e2.build - e1.build))
103-
.then(([head, ..._]) => head)
104-
.then(e => e !== undefined ? e.build : null);
105-
}
106-
107-
async function getLatestLoader(loader) {
108-
return await getLoaderVersionsMeta(loader)
109-
.then(l => l.filter(e => e.stable))
110-
.then(([head, ..._]) => head)
111-
.then(e => e.version);
112-
}
113-
114-
async function getLatestOsl() {
115-
return await getOslVersionsMeta()
116-
.then(l => l.sort((e1, e2) => compareVersion(e1.version, e2.version)))
117-
.then(([head, ..._]) => head)
118-
.then(e => e.version);
119-
}
1+
import {
2+
getMinecraftStableVersions,
3+
getMinecraftVersions,
4+
getLatestLoader,
5+
getLatestOsl,
6+
getLatestFeatherBuild,
7+
getLatestRavenBuild,
8+
getLatestSparrowBuild,
9+
getLatestNestsBuild
10+
} from "./meta_maven_utils.js";
12011

12+
(async () => {
12113
const minecraftStableVersions = await getMinecraftStableVersions();
12214
const minecraftAllVersions = await getMinecraftVersions();
12315

@@ -281,6 +173,6 @@
281173

282174
possibleVersions = minecraftStableVersions;
283175
updateVersionList()
284-
updateOrnitheDependencies()
176+
await updateOrnitheDependencies()
285177

286178
})()

public/feather_diff.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// diffMappings, printDiff, diffMemberArray from https://github.com/modmuss50/YarnDiff/tree/master
2+
3+
import {
4+
getFeatherBuildMaven,
5+
getFeatherVersionMeta,
6+
getMinecraftStableVersions,
7+
getMinecraftVersions,
8+
} from "./meta_maven_utils.js";
9+
10+
import * as tiny from "./tiny_mappings.js";
11+
12+
(async () => {
13+
const minecraftStableVersions = await getMinecraftStableVersions();
14+
const minecraftAllVersions = await getMinecraftVersions();
15+
16+
let possibleVersions;
17+
18+
const versionSelectorInput = document.getElementById("mc-version");
19+
const versionListElement = document.getElementById("version-list");
20+
const allowSnapshotsCheck = document.getElementById("allow-snapshots");
21+
22+
const buildSourceElement = document.getElementById("build-source");
23+
const buildTargetElement = document.getElementById("build-target");
24+
const diffViewerElement = document.getElementById("diff-viewer");
25+
26+
const hidePackage = document.getElementById("hide-package");
27+
28+
async function updateFeatherBuilds() {
29+
if (
30+
possibleVersions.some((version) => versionSelectorInput.value === version)
31+
) {
32+
await getFeatherVersionMeta(versionSelectorInput.value).then(
33+
(featherVersionMeta) => {
34+
buildSourceElement.innerHTML = "";
35+
buildTargetElement.innerHTML = "";
36+
for (const featherVersion of featherVersionMeta) {
37+
const featherVersionElement = document.createElement("option");
38+
featherVersionElement.innerText = "Build " + featherVersion.build;
39+
featherVersionElement.value = featherVersion.version;
40+
buildSourceElement.appendChild(featherVersionElement);
41+
buildTargetElement.appendChild(
42+
featherVersionElement.cloneNode(true),
43+
);
44+
}
45+
},
46+
);
47+
48+
// Hide the diff viewer bc the source and target builds are the same
49+
diffViewerElement.style.display = "none";
50+
}
51+
}
52+
53+
async function getTinyMappings(version) {
54+
let arrayBuf = await getFeatherBuildMaven(version)
55+
.then((response) => response.blob()) // Get response as a Blob
56+
.then(async (blob) => {
57+
const arrayBuffer = await blob.arrayBuffer(); // Convert Blob to ArrayBuffer
58+
// Convert to Uint8Array
59+
return new Uint8Array(arrayBuffer);
60+
});
61+
62+
const readableStream = new ReadableStream({
63+
start(controller) {
64+
controller.enqueue(arrayBuf);
65+
controller.close();
66+
}
67+
});
68+
const ds = new DecompressionStream("gzip");
69+
const tds = new TextDecoderStream("utf-8")
70+
71+
let file = '';
72+
const reader = readableStream.pipeThrough(ds).pipeThrough(tds).getReader();
73+
while (true) {
74+
const { done, value } = await reader.read();
75+
if (done) {
76+
break;
77+
}
78+
file += value;
79+
}
80+
81+
return tiny.parseTiny(file);
82+
}
83+
84+
async function updateFeatherDiff() {
85+
const source = buildSourceElement.value;
86+
const target = buildTargetElement.value;
87+
88+
if (source === target) {
89+
console.log("Source and target builds are the same");
90+
// Hide the diff viewer
91+
diffViewerElement.style.display = "none";
92+
return;
93+
}
94+
// Display the diff viewer
95+
diffViewerElement.style.display = "inline";
96+
97+
const sourceMappings = await getTinyMappings(source);
98+
const targetMappings = await getTinyMappings(target);
99+
100+
diffMappings(sourceMappings, targetMappings);
101+
}
102+
103+
function diffMappings(source, target) {
104+
printDiff(diffMemberArray(source.classes, target, hidePackage.checked), "classes-diff")
105+
printDiff(diffMemberArray(source.methods, target), "methods-diff")
106+
printDiff(diffMemberArray(source.fields, target), "fields-diff")
107+
}
108+
109+
function printDiff(diff, elementID) {
110+
document.getElementById(elementID).innerText = diff.map(value => `${value.source} -> ${value.target}`).join("\n")
111+
}
112+
113+
function diffMemberArray(source, targetMappings, stripPath = false) {
114+
let diff = []
115+
116+
source.forEach(source => {
117+
let target = targetMappings.find(source.calamus)
118+
119+
if (target !== undefined && source.feather !== target.feather) {
120+
let sourceFeather = source.feather;
121+
let targetFeather = target.feather;
122+
123+
if (stripPath) {
124+
if (sourceFeather.substring(0, sourceFeather.lastIndexOf('/')) === targetFeather.substring(0, targetFeather.lastIndexOf('/'))) {
125+
sourceFeather = sourceFeather.split('/').pop();
126+
targetFeather = targetFeather.split('/').pop();
127+
}
128+
}
129+
130+
diff.push({
131+
source: sourceFeather,
132+
target: targetFeather
133+
})
134+
}
135+
})
136+
return diff
137+
}
138+
139+
versionSelectorInput.addEventListener(
140+
"input",
141+
async (_) => await updateFeatherBuilds(),
142+
);
143+
144+
allowSnapshotsCheck.addEventListener("change", (_) => {
145+
if (allowSnapshotsCheck.checked) {
146+
possibleVersions = minecraftAllVersions;
147+
} else {
148+
possibleVersions = minecraftStableVersions;
149+
}
150+
updateVersionList();
151+
});
152+
153+
buildSourceElement.addEventListener(
154+
"change",
155+
async (_) => await updateFeatherDiff(),
156+
);
157+
158+
buildTargetElement.addEventListener(
159+
"change",
160+
async (_) => await updateFeatherDiff(),
161+
);
162+
163+
hidePackage.addEventListener("change", async (_) => {
164+
await updateFeatherDiff();
165+
});
166+
167+
function updateVersionList() {
168+
const list = possibleVersions;
169+
while (versionListElement.firstChild)
170+
versionListElement.removeChild(versionListElement.lastChild);
171+
list.forEach((e) => {
172+
const opt = new Option();
173+
opt.value = e;
174+
versionListElement.appendChild(opt);
175+
});
176+
}
177+
178+
possibleVersions = minecraftStableVersions;
179+
updateVersionList();
180+
await updateFeatherBuilds();
181+
})();

0 commit comments

Comments
 (0)