1-
21/*
32 _ ____ _ _ __ _ ___
43 | | _ \ / \ | |/ / __ _/ | / _ \
@@ -11,6 +10,13 @@ By: Lucas Teske
1110https://github.com/racerxdl/jpak
1211*/
1312
13+
14+ /**
15+ * JPAK Base Class
16+ * @expose
17+ */
18+ var JPAK = function ( ) { } ;
19+
1420/**
1521 * Base64 Enconding Base
1622 * @const
@@ -39,30 +45,22 @@ if(!ArrayBuffer.prototype.slice) {
3945 }
4046 return arr ;
4147 } ;
42- } ;
48+ }
4349
4450/**
4551 * Clean all deletedValue from array
4652 * @expose
4753 */
4854Array . prototype . clean = function ( deleteValue ) {
4955 for ( var i = 0 ; i < this . length ; i ++ ) {
50- if ( this [ i ] == deleteValue ) {
56+ if ( this [ i ] === deleteValue ) {
5157 this . splice ( i , 1 ) ;
5258 i -- ;
5359 }
5460 }
5561 return this ;
5662} ;
5763
58- /**
59- * JPAK Base Class
60- * @expose
61- */
62- var JPAK = function ( ) { } ;
63-
64- window [ "JPAK" ] = JPAK ;
65-
6664/**
6765 * Convert Unsigned Int8 ArrayBuffer to String
6866 * @param {Uint8Array } uintArray
@@ -73,7 +71,7 @@ JPAK.Uint8ArrayToString = function(uintArray) {
7371 for ( var i = 0 ; i < uintArray . byteLength ; i ++ )
7472 o += String . fromCharCode ( uintArray [ i ] ) ;
7573 return o ;
76- }
74+ } ;
7775
7876/**
7977 * Provided for retro-compatibility.
@@ -103,54 +101,54 @@ JPAK.String2ArrayBuffer = function(str) {
103101 * @return {string } base64
104102 */
105103JPAK . ArrayBufferToBase64 = function ( arrayBuffer ) {
106- var base64 = ''
104+ var base64 = '' ;
107105
108- var bytes = new Uint8Array ( arrayBuffer )
109- var byteLength = bytes . byteLength
110- var byteRemainder = byteLength % 3
111- var mainLength = byteLength - byteRemainder
106+ var bytes = new Uint8Array ( arrayBuffer ) ;
107+ var byteLength = bytes . byteLength ;
108+ var byteRemainder = byteLength % 3 ;
109+ var mainLength = byteLength - byteRemainder ;
112110
113- var a , b , c , d
114- var chunk
111+ var a , b , c , d ;
112+ var chunk ;
115113
116114 // Main loop deals with bytes in chunks of 3
117115 for ( var i = 0 ; i < mainLength ; i = i + 3 ) {
118116 // Combine the three bytes into a single integer
119- chunk = ( bytes [ i ] << 16 ) | ( bytes [ i + 1 ] << 8 ) | bytes [ i + 2 ]
117+ chunk = ( bytes [ i ] << 16 ) | ( bytes [ i + 1 ] << 8 ) | bytes [ i + 2 ] ;
120118
121119 // Use bitmasks to extract 6-bit segments from the triplet
122- a = ( chunk & 16515072 ) >> 18 // 16515072 = (2^6 - 1) << 18
123- b = ( chunk & 258048 ) >> 12 // 258048 = (2^6 - 1) << 12
124- c = ( chunk & 4032 ) >> 6 // 4032 = (2^6 - 1) << 6
125- d = chunk & 63 // 63 = 2^6 - 1
120+ a = ( chunk & 16515072 ) >> 18 ; // 16515072 = (2^6 - 1) << 18
121+ b = ( chunk & 258048 ) >> 12 ; // 258048 = (2^6 - 1) << 12
122+ c = ( chunk & 4032 ) >> 6 ; // 4032 = (2^6 - 1) << 6
123+ d = chunk & 63 ; // 63 = 2^6 - 1
126124
127125 // Convert the raw binary segments to the appropriate ASCII encoding
128- base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + JPAK . Base64_Encoding [ c ] + JPAK . Base64_Encoding [ d ]
126+ base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + JPAK . Base64_Encoding [ c ] + JPAK . Base64_Encoding [ d ] ;
129127 }
130128
131129 // Deal with the remaining bytes and padding
132- if ( byteRemainder == 1 ) {
133- chunk = bytes [ mainLength ]
130+ if ( byteRemainder === 1 ) {
131+ chunk = bytes [ mainLength ] ;
134132
135- a = ( chunk & 252 ) >> 2 // 252 = (2^6 - 1) << 2
133+ a = ( chunk & 252 ) >> 2 ; // 252 = (2^6 - 1) << 2
136134
137135 // Set the 4 least significant bits to zero
138- b = ( chunk & 3 ) << 4 // 3 = 2^2 - 1
136+ b = ( chunk & 3 ) << 4 ; // 3 = 2^2 - 1
139137
140- base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + '=='
141- } else if ( byteRemainder == 2 ) {
142- chunk = ( bytes [ mainLength ] << 8 ) | bytes [ mainLength + 1 ]
138+ base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + '==' ;
139+ } else if ( byteRemainder === 2 ) {
140+ chunk = ( bytes [ mainLength ] << 8 ) | bytes [ mainLength + 1 ] ;
143141
144- a = ( chunk & 64512 ) >> 10 // 64512 = (2^6 - 1) << 10
145- b = ( chunk & 1008 ) >> 4 // 1008 = (2^6 - 1) << 4
142+ a = ( chunk & 64512 ) >> 10 ; // 64512 = (2^6 - 1) << 10
143+ b = ( chunk & 1008 ) >> 4 ; // 1008 = (2^6 - 1) << 4
146144
147145 // Set the 2 least significant bits to zero
148- c = ( chunk & 15 ) << 2 // 15 = 2^4 - 1
146+ c = ( chunk & 15 ) << 2 ; // 15 = 2^4 - 1
149147
150- base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + JPAK . Base64_Encoding [ c ] + '='
148+ base64 += JPAK . Base64_Encoding [ a ] + JPAK . Base64_Encoding [ b ] + JPAK . Base64_Encoding [ c ] + '=' ;
151149 }
152150
153- return base64
151+ return base64 ;
154152} ;
155153/**
156154 * Logs a message, if enabled
@@ -160,7 +158,7 @@ JPAK.ArrayBufferToBase64 = function(arrayBuffer) {
160158JPAK . log = function ( msg ) {
161159 if ( JPAK . ShowMessages )
162160 console . log ( msg ) ;
163- }
161+ } ;
164162
165163/**
166164 * Constructor of JPAKLoader
@@ -185,7 +183,7 @@ JPAK.jpakloader = function(parameters) {
185183 */
186184JPAK . jpakloader . prototype . CacheLoad = function ( path ) {
187185 for ( var i = 0 ; i < this . filecache . length ; i ++ ) {
188- if ( this . filecache [ i ] . path == path )
186+ if ( this . filecache [ i ] . path === path )
189187 return this . filecache [ i ] ;
190188 }
191189 return undefined ;
@@ -208,39 +206,39 @@ JPAK.jpakloader.prototype.Load = function() {
208206
209207 // The On Progress request. For now it only does stuff if there is a hooked onprogress jpakloader event.
210208 xhr . onprogress = function ( e ) {
211- if ( e . lengthComputable && _this . onprogress != undefined ) {
209+ if ( e . lengthComputable && _this . onprogress !== undefined ) {
212210 var percentComplete = ( ( ( e . loaded / e . total ) * 10000 ) >> 0 ) / 100 ; // Rounded percent to two decimal
213211 _this . onprogress ( { "loaded" :e . loaded , "total" :e . total , "percent" : percentComplete } ) ;
214212 }
215- }
213+ } ;
216214
217215 // The onload function. This parses the JPAK and after loading the filetable it calls the onload event of jpakloader
218216 xhr . onload = function ( e ) {
219- if ( this . status == 200 ) {
217+ if ( this . status === 200 ) {
220218 var data = this . response ;
221219 var MagicNumber = u8as ( new Uint8Array ( data . slice ( 0 , 5 ) ) ) ;
222- if ( MagicNumber == "JPAK1" ) {
220+ if ( MagicNumber === "JPAK1" ) {
223221 JPAK . log ( "JPAK::jpakloader - Loaded file " + _this . jpakfile + " successfully. JPAK1 Format" ) ;
224222 var filetableoffset = new DataView ( data . slice ( data . byteLength - 4 , data . byteLength ) ) . getUint32 ( 0 , true ) ;
225223 var filetable = new Uint8Array ( data . slice ( filetableoffset , data . byteLength - 4 ) ) ;
226224 filetable = JSON . parse ( u8as ( filetable ) ) ;
227225 _this . filetable = filetable ;
228226 _this . jpakdata = data ;
229227 _this . dataloaded = true ;
230- if ( _this . onload != undefined )
228+ if ( _this . onload !== undefined )
231229 _this . onload ( ) ;
232230 } else {
233231 JPAK . log ( "JPAK::jpakloader - Error loading file " + _this . jpakfile + " (8000): Wrong File Magic. Expected JPAK1 got " + MagicNumber ) ;
234- if ( _this . onerror != undefined )
232+ if ( _this . onerror !== undefined )
235233 _this . onerror ( { "text" : "Wrong File Magic. Expected JPAK1 got " + MagicNumber , "errorcode" : 8000 } ) ;
236234 }
237235 }
238236 } ;
239237 xhr . onreadystatechange = function ( aEvt ) {
240- if ( this . readyState == 4 ) {
241- if ( this . status != 200 ) {
238+ if ( this . readyState === 4 ) {
239+ if ( this . status !== 200 ) {
242240 JPAK . log ( "JPAK::jpakloader - Error loading file " + _this . jpakfile + " (" + this . status + "): " + this . statusText ) ;
243- if ( _this . onerror != undefined )
241+ if ( _this . onerror !== undefined )
244242 _this . onerror ( { "text" : this . statusText , "errorcode" : this . status } ) ;
245243 }
246244 }
@@ -260,7 +258,7 @@ JPAK.jpakloader.prototype.Load = function() {
260258JPAK . jpakloader . prototype . FindDirectoryEntry = function ( path ) {
261259 var base = this . filetable ;
262260 if ( this . dataloaded ) {
263- if ( path != "/" ) {
261+ if ( path !== "/" ) {
264262 path = path . split ( "/" ) . clean ( "" ) ;
265263 var dir = "" , ok = true ;
266264 for ( var i = 0 ; i < path . length ; i ++ ) {
@@ -290,7 +288,7 @@ JPAK.jpakloader.prototype.FindFileEntry = function(path) {
290288 var filename = pathblock [ pathblock . length - 1 ] ;
291289 path = path . replace ( filename , "" ) ;
292290 var base = this . FindDirectoryEntry ( path ) ;
293- if ( base != undefined )
291+ if ( base !== undefined )
294292 if ( filename in base . files )
295293 return base . files [ filename ] ;
296294 return undefined ;
@@ -306,10 +304,10 @@ JPAK.jpakloader.prototype.ls = function(path) {
306304 var out = { "files" : [ ] , "dirs" : [ ] } ;
307305 if ( this . dataloaded ) {
308306 var base = this . FindDirectoryEntry ( path ) ;
309- if ( base != undefined ) {
307+ if ( base !== undefined ) {
310308 for ( var i in base . files )
311309 out . files . push ( base . files [ i ] ) ;
312- for ( var i in base . directories )
310+ for ( i in base . directories )
313311 out . dirs . push ( { "name" : base . directories [ i ] . name , "numfiles" : base . directories [ i ] . numfiles } ) ;
314312 } else
315313 out . error = "Directory not found!" ;
@@ -331,15 +329,15 @@ JPAK.jpakloader.prototype.GetFile = function(path, type) {
331329 type = type || 'application/octet-binary' ;
332330 var cache = this . CacheLoad ( path ) ;
333331
334- if ( file != undefined && cache == undefined ) {
332+ if ( file !== undefined && cache = == undefined ) {
335333 // Add it to file cache
336334 var dataslice = this . jpakdata . slice ( file . offset , file . offset + file . size ) ;
337335 if ( file . compressed !== undefined && file . compressed )
338336 dataslice = JPAK . GZIP . decompress ( dataslice ) ;
339337 var blob = new Blob ( [ new Uint8Array ( dataslice ) . buffer ] , { "type" :type } ) ;
340338 this . filecache . push ( { "path" :path , "type" :type , "blob" :blob , "url" :URL . createObjectURL ( blob ) , "arraybuffer" : dataslice } ) ;
341339 return blob ;
342- } else if ( cache != undefined )
340+ } else if ( cache !== undefined )
343341 return cache . blob ;
344342
345343 return undefined ;
@@ -354,9 +352,9 @@ JPAK.jpakloader.prototype.GetFile = function(path, type) {
354352 */
355353JPAK . jpakloader . prototype . GetFileURL = function ( path , type ) {
356354 var cache = this . CacheLoad ( path ) ;
357- if ( cache == undefined ) {
355+ if ( cache === undefined ) {
358356 var blob = this . GetFile ( path , type ) ;
359- if ( blob != undefined )
357+ if ( blob !== undefined )
360358 return URL . createObjectURL ( blob ) ;
361359 else {
362360 JPAK . log ( "Error: Cannot find file \"" + path + "\"" ) ;
@@ -378,15 +376,15 @@ JPAK.jpakloader.prototype.GetFileArrayBuffer = function(path, type) {
378376 type = type || 'application/octet-binary' ;
379377 var cache = this . CacheLoad ( path ) ;
380378
381- if ( file != undefined && cache == undefined ) {
379+ if ( file !== undefined && cache = == undefined ) {
382380 // Add it to file cache
383381 var dataslice = this . jpakdata . slice ( file . offset , file . offset + file . size ) ;
384382 if ( file . compressed !== undefined && file . compressed )
385383 dataslice = JPAK . GZIP . decompress ( dataslice ) ;
386384 var blob = new Blob ( [ new Uint8Array ( dataslice ) . buffer ] , { "type" :type } ) ;
387385 this . filecache . push ( { "path" :path , "type" :type , "blob" :blob , "url" :URL . createObjectURL ( blob ) , "arraybuffer" : dataslice } ) ;
388386 return dataslice ;
389- } else if ( cache != undefined )
387+ } else if ( cache !== undefined )
390388 return cache . arraybuffer ;
391389
392390 JPAK . log ( "Error: Cannot find file \"" + path + "\"" ) ;
@@ -403,7 +401,7 @@ JPAK.jpakloader.prototype.GetFileArrayBuffer = function(path, type) {
403401 */
404402JPAK . jpakloader . prototype . GetBase64File = function ( path , type ) {
405403 var filedata = this . GetFileArrayBuffer ( path , type ) ;
406- if ( filedata == undefined )
404+ if ( filedata === undefined )
407405 return undefined ;
408406
409407 return JPAK . ArrayBufferToBase64 ( filedata ) ;
@@ -422,10 +420,13 @@ JPAK.jpakloader.prototype.GetHTMLDataURIFile = function(path, type, encoding) {
422420 var b64 = this . GetBase64File ( path , type ) ;
423421 // HTML Data URI Format: data:[<MIME-type>][;charset=<encoding>][;base64],<data>
424422 if ( b64 === undefined )
425- return undefined
423+ return undefined ;
426424
427425 if ( encoding !== undefined )
428426 return "data:" + type + ";charset=" + encoding + ";base64," + b64 ;
429427 else
430428 return "data:" + type + ";base64," + b64 ;
431429} ;
430+
431+
432+ window . JPAK = JPAK ;
0 commit comments