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 { decompressSync } from "https://cdn.skypack.dev/fflate@0.8.2?min" ;
11+ import * as tiny from "./tiny_mappings.js" ;
12+
13+ ( async ( ) => {
14+ const minecraftStableVersions = await getMinecraftStableVersions ( ) ;
15+ const minecraftAllVersions = await getMinecraftVersions ( ) ;
16+
17+ let possibleVersions ;
18+
19+ const versionSelectorInput = document . getElementById ( "mc-version" ) ;
20+ const versionListElement = document . getElementById ( "version-list" ) ;
21+ const allowSnapshotsCheck = document . getElementById ( "allow-snapshots" ) ;
22+
23+ const buildSourceElement = document . getElementById ( "build-source" ) ;
24+ const buildTargetElement = document . getElementById ( "build-target" ) ;
25+ const diffViewerElement = document . getElementById ( "diff-viewer" ) ;
26+
27+ async function updateFeatherBuilds ( ) {
28+ if (
29+ possibleVersions . some ( ( version ) => versionSelectorInput . value === version )
30+ ) {
31+ await getFeatherVersionMeta ( versionSelectorInput . value ) . then (
32+ ( featherVersionMeta ) => {
33+ buildSourceElement . innerHTML = "" ;
34+ buildTargetElement . innerHTML = "" ;
35+ for ( const featherVersion of featherVersionMeta ) {
36+ const featherVersionElement = document . createElement ( "option" ) ;
37+ featherVersionElement . innerText = "Build " + featherVersion . build ;
38+ featherVersionElement . value = featherVersion . version ;
39+ buildSourceElement . appendChild ( featherVersionElement ) ;
40+ buildTargetElement . appendChild (
41+ featherVersionElement . cloneNode ( true ) ,
42+ ) ;
43+ }
44+ } ,
45+ ) ;
46+
47+ // Hide the diff viewer bc the source and target builds are the same
48+ diffViewerElement . style . display = "none" ;
49+ }
50+ }
51+
52+ async function getTinyMappings ( version ) {
53+ let arrayBuf = await getFeatherBuildMaven ( version )
54+ . then ( ( response ) => response . blob ( ) ) // Get response as a Blob
55+ . then ( async ( blob ) => {
56+ const arrayBuffer = await blob . arrayBuffer ( ) ; // Convert Blob to ArrayBuffer
57+ // Convert to Uint8Array
58+ return new Uint8Array ( arrayBuffer ) ;
59+ } ) ;
60+ const decompressed = decompressSync ( arrayBuf ) ;
61+ const decoder = new TextDecoder ( "utf-8" ) ;
62+ const file = decoder . decode ( decompressed ) ;
63+ return tiny . parseTiny ( file ) ;
64+ }
65+
66+ async function updateFeatherDiff ( ) {
67+ const source = buildSourceElement . value ;
68+ const target = buildTargetElement . value ;
69+
70+ if ( source === target ) {
71+ console . log ( "Source and target builds are the same" ) ;
72+ // Hide the diff viewer
73+ diffViewerElement . style . display = "none" ;
74+ return ;
75+ }
76+ // Display the diff viewer
77+ diffViewerElement . style . display = "inline" ;
78+
79+ const sourceMappings = await getTinyMappings ( source ) ;
80+ const targetMappings = await getTinyMappings ( target ) ;
81+
82+ diffMappings ( sourceMappings , targetMappings ) ;
83+ }
84+
85+ function diffMappings ( source , target ) {
86+ printDiff ( diffMemberArray ( source . classes , target ) , "classes-diff" )
87+ printDiff ( diffMemberArray ( source . methods , target ) , "methods-diff" )
88+ printDiff ( diffMemberArray ( source . fields , target ) , "fields-diff" )
89+ }
90+
91+ function printDiff ( diff , elementID ) {
92+ document . getElementById ( elementID ) . innerText = diff . map ( value => `${ value . source } -> ${ value . target } ` ) . join ( "\n" )
93+ }
94+
95+ function diffMemberArray ( source , targetMappings ) {
96+ let diff = [ ]
97+
98+ source . forEach ( source => {
99+ let target = targetMappings . find ( source . calamus )
100+
101+ if ( target !== undefined && source . feather !== target . feather ) {
102+ diff . push ( {
103+ source : source . feather ,
104+ target : target . feather
105+ } )
106+ }
107+ } )
108+ return diff
109+ }
110+
111+ versionSelectorInput . addEventListener (
112+ "input" ,
113+ async ( _ ) => await updateFeatherBuilds ( ) ,
114+ ) ;
115+
116+ allowSnapshotsCheck . addEventListener ( "change" , ( _ ) => {
117+ if ( allowSnapshotsCheck . checked ) {
118+ possibleVersions = minecraftAllVersions ;
119+ } else {
120+ possibleVersions = minecraftStableVersions ;
121+ }
122+ updateVersionList ( ) ;
123+ } ) ;
124+
125+ buildSourceElement . addEventListener (
126+ "change" ,
127+ async ( _ ) => await updateFeatherDiff ( ) ,
128+ ) ;
129+
130+ buildTargetElement . addEventListener (
131+ "change" ,
132+ async ( _ ) => await updateFeatherDiff ( ) ,
133+ ) ;
134+
135+ function updateVersionList ( ) {
136+ const list = possibleVersions ;
137+ while ( versionListElement . firstChild )
138+ versionListElement . removeChild ( versionListElement . lastChild ) ;
139+ list . forEach ( ( e ) => {
140+ const opt = new Option ( ) ;
141+ opt . value = e ;
142+ versionListElement . appendChild ( opt ) ;
143+ } ) ;
144+ }
145+
146+ possibleVersions = minecraftStableVersions ;
147+ updateVersionList ( ) ;
148+ await updateFeatherBuilds ( ) ;
149+ } ) ( ) ;
0 commit comments