JScratch
Loading...
Searching...
No Matches
ProjectManager.java
Go to the documentation of this file.
1
package
com.jscratch;
2
3
import
com.google.gson.Gson;
4
import
com.google.gson.GsonBuilder;
5
import
java.io.*;
6
import
java.nio.file.*;
7
import
java.util.*;
8
import
java.util.zip.*;
9
13
public
class
ProjectManager
{
14
private
static
final
Gson
gson
=
new
GsonBuilder().setPrettyPrinting().create();
15
16
public
static
void
saveProject
(
Project
project, File targetFile)
throws
IOException {
17
try
(ZipOutputStream zos =
new
ZipOutputStream(
new
FileOutputStream(targetFile))) {
18
// Write project.json
19
ZipEntry jsonEntry =
new
ZipEntry(
"project.json"
);
20
zos.putNextEntry(jsonEntry);
21
zos.write(
gson
.toJson(project).getBytes());
22
zos.closeEntry();
23
24
// Assets will be handled here (copying files into ZIP)
25
// ... logic to include costumes/sounds ...
26
}
27
}
28
29
public
static
Project
loadProject
(File sourceFile)
throws
IOException {
30
try
(ZipFile zipFile =
new
ZipFile(sourceFile)) {
31
ZipEntry jsonEntry = zipFile.getEntry(
"project.json"
);
32
if
(jsonEntry ==
null
)
throw
new
IOException(
"Not a valid .jscratch project."
);
33
34
try
(InputStream is = zipFile.getInputStream(jsonEntry);
35
InputStreamReader reader =
new
InputStreamReader(is)) {
36
return
gson
.fromJson(reader,
Project
.class);
37
}
38
}
39
}
40
}
com.jscratch.Project
Definition
Project.java:11
com.jscratch.ProjectManager
Definition
ProjectManager.java:13
com.jscratch.ProjectManager.gson
static final Gson gson
Definition
ProjectManager.java:14
com.jscratch.ProjectManager.saveProject
static void saveProject(Project project, File targetFile)
Definition
ProjectManager.java:16
com.jscratch.ProjectManager.loadProject
static Project loadProject(File sourceFile)
Definition
ProjectManager.java:29
src
main
java
com
jscratch
ProjectManager.java
Generated by
1.16.1