-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileSave.hx
More file actions
305 lines (260 loc) · 8.36 KB
/
FileSave.hx
File metadata and controls
305 lines (260 loc) · 8.36 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import com.stencyl.Engine;
import com.stencyl.behavior.Script;
import com.stencyl.behavior.TimedTask;
import com.stencyl.models.Font;
import com.stencyl.graphics.G;
import com.stencyl.utils.Utils;
import nme.display.BitmapData;
import nme.utils.ByteArray;
import haxe.io.Bytes;
import openfl.Assets;
#if (openfl >= "4.0.0")
import openfl.geom.Rectangle;
import openfl.display.*;
#end
#if !flash
import sys.*;
import sys.io.*;
#if (openfl >= "4.0.0")
import lime.system.System.*;
#else
import nme.utils.SystemPath.*;
#end
#end
class FileSave {
public static function getText(path:String):String {
var content:String = "";
var path2:String = "";
// Flash or HTML5: This should not happen; error out
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
#elseif js
trace("ERROR: File IO cannot be accessed on HTML5.");
// iOS or Android: Attempt to get text from the external directory, then the internal directory
#elseif mobile
path2 = userDirectory + "/" + path;
if (FileSystem.exists(path2)) {
content = File.getContent(path2);
} else {
content = nme.Assets.getText(path);
if (content == null) {
trace("ERROR: File does not exist at: " + path);
content = "";
}
}
// Windows, Mac or Linux: Attempt to get text straight from the "assets/data/" folder
#else
path2 = FileSystem.fullPath(path);
if (FileSystem.exists(path2)) {
content = File.getContent(path2);
} else {
trace("ERROR: File does not exist at: " + path2);
}
#end
return content;
}
public static function getImage(path:String):BitmapData {
var image:BitmapData = new BitmapData(1,1);
var path2:String = "";
// Flash or HTML5: This should not happen; error out
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
#elseif js
trace("ERROR: File IO cannot be accessed on HTML5.");
// iOS or Android: Attempt to get image from the external directory, then the internal directory
#elseif mobile
path2 = userDirectory + "/" + path;
if (FileSystem.exists(path2)) {
#if (openfl >= "4.0.0")
image = BitmapData.fromBytes(File.getBytes(path2));
#else
image = BitmapData.loadFromHaxeBytes(File.getBytes(path2));
#end
} else {
image = nme.Assets.getBitmapData(path);
}
if (image == null) {
trace("ERROR: File does not exist at: " + path);
image = new BitmapData(1,1);
}
// Windows, Mac or Linux: Attempt to get image straight from the "assets/data/" folder
#else
path2 = FileSystem.fullPath(path);
if (FileSystem.exists(path2)) {
#if (openfl >= "4.0.0")
image = BitmapData.fromBytes(File.getBytes(path2));
#else
image = BitmapData.loadFromHaxeBytes(File.getBytes(path2));
#end
} else {
trace("ERROR: File does not exist at: " + path2);
}
if (image == null) {
trace("ERROR: File does not exist at: " + path);
image = new BitmapData(1,1);
}
#end
return image;
}
public static function saveText(path:String, content:String, ?whenDone:Bool->Void):Void {
var success:Bool = true;
var path2:String = "";
path = "/assets/data/" + path;
var a:Array<String> = DataUtils.subfold(path);
// Flash or HTML5: Not possible to save; error out
#if (flash || js)
success = false;
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
#else
trace("ERROR: File IO cannot be accessed on HTML5.");
#end
// iOS and Android: Attempt to save to the storage directory
#elseif mobile
if (!FileSystem.exists(userDirectory + "/" + a[0])) {
FileSystem.createDirectory(userDirectory + "/" + a[0]);
}
path2 = userDirectory + "/" + a[0] + "/" + a[1];
try {
File.saveContent(path2, Std.string(content));
} catch (e:Dynamic) {
success = false;
trace("ERROR: " + e);
errorify(e);
}
// Windows, Mac, and Linux: Save straight to the "assets/data/" folder
#else
if (!FileSystem.exists(FileSystem.fullPath(a[0]))) {
FileSystem.createDirectory(FileSystem.fullPath(a[0]));
}
path2 = FileSystem.fullPath(a[0] + "/" + a[1]);
try {
File.saveContent(path2, Std.string(content));
} catch (e:Dynamic) {
success = false;
trace("ERROR: " + e);
errorify(e);
}
#end
if (whenDone != null)
whenDone(success);
}
/**
* Save bytes as a file
* @param path
* @param content
* @return true if succeed, false overwise
*/
static function saveBytes(path:String, content:Bytes):Bool {
var success = true;
// Flash or HTML5: This should not happen; error out
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
success = false;
#elseif js
trace("ERROR: File IO cannot be accessed on HTML5.");
success = false;
#else
var fo:FileOutput = null;
try {
//open binary file and write bytes
fo = File.write(path, true);
fo.writeBytes(content, 0, content.length);
} catch (e:Dynamic) {
success = false;
trace("ERROR: " + e);
errorify(e);
}
//file output should be closed in any case
try {
if (fo != null)
fo.close();
} catch (e:Dynamic) {
success = false;
trace("ERROR: " + e);
errorify(e);
}
#end
return success;
}
public static function saveImage(path:String, type:String, image:BitmapData, ?whenDone:Bool->Void):Void {
// Should not happen, but we'd better handle it anyway
if (type != "png" && type != "jpg") {
trace("ERROR: Could not determine how to save image as a " + type + " file.");
whenDone(false);
return;
}
path = "/assets/data/" + path;
var path2:String = "";
var a:Array<String> = DataUtils.subfold(path);
var success = false;
// Flash or HTML5: Not possible to save; error out
#if flash
trace("ERROR: File IO cannot be accessed on Flash.");
#elseif js
trace("ERROR: File IO cannot be accessed on HTML5.");
#else
// Windows, Mac, Linux, iOS, and Android: Use the "saveBytes" function with the converted file
#if mobile
if (!FileSystem.exists(userDirectory + "/" + a[0])) {
FileSystem.createDirectory(userDirectory + "/" + a[0]);
}
path2 = userDirectory + "/" + a[0] + "/" + a[1];
#else
if (!FileSystem.exists(FileSystem.fullPath(a[0]))) {
FileSystem.createDirectory(FileSystem.fullPath(a[0]));
}
path2 = FileSystem.fullPath(a[0] + "/" + a[1]);
#end
#if (openfl >= "4.0.0")
var b:ByteArray = image.encode(image.rect, type == "jpg" ? new JPEGEncoderOptions() : new PNGEncoderOptions());
#else
var b:ByteArray = image.encode(type, 1);
#end
success = saveBytes(path2, b);
#end
if (whenDone != null)
whenDone(success);
}
public static function savePNG(path:String, image:BitmapData, ?whenDone:Bool->Void):Void {
if (path.substr(path.length - 4).toLowerCase() != ".png")
path += ".png";
saveImage(path, "png", image, whenDone);
}
public static function saveJPG(path:String, image:BitmapData, ?whenDone:Bool->Void):Void {
if (path.substr(path.length - 4).toLowerCase() != ".jpg" &&
path.substr(path.length - 5).toLowerCase() != ".jpeg")
path += ".jpg";
saveImage(path, "jpg", image, whenDone);
}
public static function errorify(e:Dynamic):Void {
var email:String = "mailto:eth3792@nycap.rr.com?subject=External Data Error"
+ "&body=Hi ETHproductions,%0a%0a"
+ "I came across this error when I was testing my game:%0a%0a"
+ e + "%0a%0a";
// When the screen is pressed, opens an link to PM ETHproductions.
Engine.engine.whenMousePressedListeners.push(function(list:Array<Dynamic>) {
Script.openURLInBrowser("http://community.stencyl.com/index.php?action=pm;sa=send;u=185723");
// Script.openURLInBrowser(email); // Would open the email instead
});
// Draws information on the screen to alert the user to the error.
// Runs after 20 milliseconds to make sure all Drawing events fire first.
Script.runLater(20, function(timeTask:TimedTask){
Engine.engine.whenDrawingListeners.push(function(g:G, x:Float, y:Float, list:Array<Dynamic>):Void {
var font:Font = new Font(-1, -1, "Default Font", true);
g.setFont(font);
g.fillColor = Utils.getColorRGB(255,255,255);
g.translateToScreen();
g.moveTo(0, 0);
g.strokeSize = 0;
g.alpha = 1;
g.fillRect(0,0,Script.getScreenWidth(),106);
g.drawString("Uh-oh! Something went really wrong", 8, 8);
g.drawString("with the External Data extension!", 8, 28);
g.drawString("Click anywhere on the screen to con-", 8, 48);
g.drawString("tact the author, ETHproductions.", 8, 68);
g.drawString("See Log Viewer for error details.", 8, 88);
});
});
}
}