forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.as
More file actions
147 lines (132 loc) · 5.09 KB
/
Server.as
File metadata and controls
147 lines (132 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Scratch Project Editor and Player
* Copyright (C) 2014 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// ServerOffline.as
// John Maloney, June 2013
//
// Interface to the Scratch website API's for Offline Editor.
//
// Note: All operations call the whenDone function with the result
// if the operation succeeded or null if it failed.
package util {
import flash.events.ErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.geom.Matrix;
import flash.net.SharedObject;
import flash.net.URLLoader;
public class Server implements IServer {
// -----------------------------
// Asset API
//------------------------------
public function fetchAsset(url:String, whenDone:Function):URLLoader {
// Make a GET or POST request to the given URL (do a POST if the data is not null).
// The whenDone() function is called when the request is done, either with the
// data returned by the server or with a null argument if the request failed.
function completeHandler(e:Event):void {
loader.removeEventListener(Event.COMPLETE, completeHandler);
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
loader.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
whenDone(loader.data);
}
function errorHandler(err:ErrorEvent):void {
loader.removeEventListener(Event.COMPLETE, completeHandler);
loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
loader.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
Scratch.app.logMessage('Failed server request for '+url);
whenDone(null);
}
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
var request:URLRequest = new URLRequest(url);
try {
loader.load(request);
} catch(e:*){
// Local sandbox exception?
whenDone(null);
}
return loader;
}
public function getAsset(md5:String, whenDone:Function):URLLoader {
// if (BackpackPart.localAssets[md5] && BackpackPart.localAssets[md5].length > 0) {
// whenDone(BackpackPart.localAssets[md5]);
// return null;
// }
return fetchAsset('media/' + md5, whenDone);
}
public function getMediaLibrary(whenDone:Function):URLLoader {
return getAsset('mediaLibrary.json', whenDone);
}
public function getThumbnail(md5:String, w:int, h:int, whenDone:Function):URLLoader {
function gotAsset(data:ByteArray):void {
if (data) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
try { loader.loadBytes(data) } catch (e:*) {}
}
}
function imageLoaded(e:Event):void {
whenDone(makeThumbnail(e.target.content.bitmapData));
}
var ext:String = md5.slice(-3);
if (['gif', 'png', 'jpg'].indexOf(ext) > -1) getAsset(md5, gotAsset);
return null;
}
private function makeThumbnail(bm:BitmapData):BitmapData {
const tnWidth:int = 120;
const tnHeight:int = 90;
var result:BitmapData = new BitmapData(tnWidth, tnHeight, true, 0);
if ((bm.width == 0) || (bm.height == 0)) return result;
var scale:Number = Math.min(tnWidth/ bm.width, tnHeight / bm.height);
var m:Matrix = new Matrix();
m.scale(scale, scale);
m.translate((tnWidth - (scale * bm.width)) / 2, (tnHeight - (scale * bm.height)) / 2);
result.draw(bm, m);
return result;
}
// -----------------------------
// Translation Support
//------------------------------
public function getLanguageList(whenDone:Function):void {
fetchAsset('locale/lang_list.txt', whenDone);
}
public function getPOFile(lang:String, whenDone:Function):void {
fetchAsset('locale/' + lang + '.po', whenDone);
}
public function getSelectedLang(whenDone:Function):void {
// Get the language setting.
var sharedObj:SharedObject = SharedObject.getLocal('Scratch');
if (sharedObj.data.lang) whenDone(sharedObj.data.lang);
}
public function setSelectedLang(lang:String):void {
// Record the language setting.
var sharedObj:SharedObject = SharedObject.getLocal('Scratch');
if (lang == '') lang = 'en';
sharedObj.data.lang = lang;
sharedObj.flush();
}
}}