@@ -26,38 +26,6 @@ import { MISSING_PROJECT_ID } from "./tw-missing-project";
2626import VM from "scratch-vm" ;
2727import * as progressMonitor from "../components/loader/tw-progress-monitor" ;
2828
29- // TW: Temporary hack for project tokens
30- const fetchProjectToken = ( projectId ) => {
31- if ( projectId === "0" ) {
32- return Promise . resolve ( null ) ;
33- }
34- // Parse ?token=abcdef
35- const searchParams = new URLSearchParams ( location . search ) ;
36- if ( searchParams . has ( "token" ) ) {
37- return Promise . resolve ( searchParams . get ( "token" ) ) ;
38- }
39- // Parse #1?token=abcdef
40- const hashParams = new URLSearchParams ( location . hash . split ( "?" ) [ 1 ] ) ;
41- if ( hashParams . has ( "token" ) ) {
42- return Promise . resolve ( hashParams . get ( "token" ) ) ;
43- }
44- return fetch (
45- `https://projects.penguinmod.com/api/v1/projects/getproject?projectID=${ projectId } &requestType=metadata` ,
46- )
47- . then ( ( r ) => {
48- if ( ! r . ok ) return null ;
49- return r . json ( ) ;
50- } )
51- . then ( ( dataOrNull ) => {
52- const token = dataOrNull ? dataOrNull . id : null ;
53- return token ;
54- } )
55- . catch ( ( err ) => {
56- log . error ( err ) ;
57- return null ;
58- } ) ;
59- } ;
60-
6129/* Higher Order Component to provide behavior for loading projects by id. If
6230 * there's no id, the default project is loaded.
6331 * @param {React.Component } WrappedComponent component to receive projectData prop
@@ -110,6 +78,7 @@ const ProjectFetcherHOC = function (WrappedComponent) {
11078 this . props . onActivateTab ( BLOCKS_TAB_INDEX ) ;
11179 }
11280 }
81+
11382 fetchProject ( projectId , loadingState ) {
11483 // tw: clear and stop the VM before fetching
11584 // these will also happen later after the project is fetched, but fetching may take a while and
@@ -145,6 +114,7 @@ const ProjectFetcherHOC = function (WrappedComponent) {
145114 ) {
146115 projectUrl = `https://${ projectUrl } ` ;
147116 }
117+
148118 assetPromise = progressMonitor
149119 . fetchWithProgress ( projectUrl )
150120 . then ( ( r ) => {
@@ -172,58 +142,19 @@ const ProjectFetcherHOC = function (WrappedComponent) {
172142 storage . DataFormat . JSON ,
173143 ) ;
174144 } else {
145+ assetPromise = storage . load (
146+ storage . AssetType . Project ,
147+ projectId ,
148+ storage . DataFormat . JSON ,
149+ ) ;
175150 storage . setProjectID ( projectId ) ;
176- projectUrl = `https://projects.penguinmod.com/api/v1/projects/getprojectwrapper?safe=true&projectId=${ projectId } &assets=false` ;
177- assetPromise = progressMonitor
178- . fetchWithProgress ( projectUrl )
179- . then ( async ( r ) => {
180- if (
181- this . props . vm . runtime . renderer
182- ?. setPrivateSkinAccess
183- )
184- this . props . vm . runtime . renderer . setPrivateSkinAccess (
185- false ,
186- ) ;
187- if ( ! r . ok ) {
188- throw new Error (
189- `Request returned status ${ r . status } ` ,
190- ) ;
191- }
192- const project = await r . json ( ) ;
193-
194- const json = protobufToJson (
195- new Uint8Array ( project . project . data ) ,
196- ) ;
197-
198- // now get the assets
199- let zip = new JSZip ( ) ;
200- zip . file ( "project.json" , JSON . stringify ( json ) ) ;
201-
202- /*
203- // we will fetch assets later now
204- for (const asset of project.assets) {
205- zip.file(
206- asset.id,
207- new Uint8Array(asset.buffer.data).buffer,
208- );
209- }
210- */
211-
212- const arrayBuffer = await zip . generateAsync ( {
213- type : "arraybuffer" ,
214- } ) ;
215-
216- return arrayBuffer ;
217- } )
218- . then ( ( buffer ) => ( { data : buffer } ) )
219- . catch ( ( error ) => {
220- console . log ( error ) ;
221- } ) ;
222151 }
223152 }
224153
225154 return assetPromise
226155 . then ( ( projectAsset ) => {
156+ console . log ( `project asset: ${ projectAsset } ` ) ;
157+
227158 // tw: If the project data appears to be HTML, then the result is probably an nginx 404 page,
228159 // and the "missing project" project should be loaded instead.
229160 // See: https://projects.scratch.mit.edu/9999999999999999999999
@@ -249,57 +180,7 @@ const ProjectFetcherHOC = function (WrappedComponent) {
249180 loadingState ,
250181 ) ;
251182 } else {
252- // pm: Failed to grab data, use the "fetch" API as a backup
253- // we shouldnt be interrupted by the fetch replacement in tw-progress-monitor
254- // as it uses projects.scratch.mit.edu still
255- fetch ( projectUrl )
256- . then ( async ( res ) => {
257- if ( ! res . ok ) {
258- // Treat failure to load as an error
259- // Throw to be caught by catch later on
260- throw new Error (
261- "Could not find project; " + projectUrl ,
262- ) ;
263- }
264-
265- const project = await res . json ( ) ;
266- const json = protobufToJson (
267- new Uint8Array ( project . project . data ) ,
268- ) ;
269-
270- // now get the assets
271- let zip = new JSZip ( ) ;
272- zip . file ( "project.json" , JSON . stringify ( json ) ) ;
273-
274- if ( typeof project . assets !== "object" ) {
275- alert (
276- "No assets were returned. This error is temporary and should not be reported." ,
277- ) ;
278- throw new TypeError (
279- "Invalid type given inside the assets list" ,
280- ) ;
281- }
282- for ( const asset of project . assets ) {
283- zip . file (
284- asset . id ,
285- new Uint8Array ( asset . buffer . data )
286- . buffer ,
287- ) ;
288- }
289-
290- const arrayBuffer = await zip . generateAsync ( {
291- type : "arraybuffer" ,
292- } ) ;
293- this . props . onFetchedProjectData (
294- arrayBuffer ,
295- loadingState ,
296- ) ;
297- } )
298- . catch ( ( err ) => {
299- throw new Error (
300- "Could not find project; " + err ,
301- ) ;
302- } ) ;
183+ throw new Error ( "Failed to load project." ) ;
303184 }
304185 } )
305186 . catch ( ( err ) => {
@@ -360,7 +241,8 @@ const ProjectFetcherHOC = function (WrappedComponent) {
360241 ProjectFetcherComponent . defaultProps = {
361242 assetHost :
362243 "https://asset-cdn.penguinmod.com/file/penguinmod-warm-tier-s2-cf" ,
363- projectHost : "https://projects.scratch.mit.edu" ,
244+ projectHost :
245+ "https://projects.penguinmod.com/api/v1/projects/getProject?requestType=protobuf&safe=true&projectID" ,
364246 } ;
365247
366248 const mapStateToProps = ( state ) => ( {
0 commit comments