-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUnrealPython_UPL.xml
More file actions
159 lines (143 loc) · 4.51 KB
/
Copy pathUnrealPython_UPL.xml
File metadata and controls
159 lines (143 loc) · 4.51 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
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:android="http://schemas.android.com/apk/res/android">
<gradleCopies>
<copyFile src="$S(PluginDir)/Intermediate/Android/unrealpython_bootstrap.zip" dst="$S(BuildDir)/gradle/app/src/main/assets/unrealpython_bootstrap.zip" force="true" />
</gradleCopies>
<gameActivityImportAdditions>
<insert><![CDATA[
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
]]></insert>
</gameActivityImportAdditions>
<gameActivityClassAdditions>
<insert><![CDATA[
public boolean AndroidThunkJava_ExtractUnrealPythonBootstrap(String assetName, String destinationPath, String version)
{
File destinationDir = new File(destinationPath);
File versionFile = new File(destinationDir, ".unrealpython_bootstrap_version");
try
{
if (versionFile.exists())
{
String currentVersion = UnrealPythonReadTextFile(versionFile).trim();
if (version.equals(currentVersion))
{
Log.debug("UnrealPython bootstrap is already extracted: " + destinationPath);
return true;
}
}
UnrealPythonDeleteRecursive(destinationDir);
if (!destinationDir.mkdirs() && !destinationDir.exists())
{
Log.error("Failed to create UnrealPython bootstrap directory: " + destinationPath);
return false;
}
File canonicalDestination = destinationDir.getCanonicalFile();
String canonicalRoot = canonicalDestination.getPath() + File.separator;
try (InputStream assetStream = getAssets().open(assetName);
ZipInputStream zipStream = new ZipInputStream(assetStream))
{
byte[] buffer = new byte[65536];
ZipEntry entry;
while ((entry = zipStream.getNextEntry()) != null)
{
File outputFile = new File(canonicalDestination, entry.getName());
File canonicalOutputFile = outputFile.getCanonicalFile();
String canonicalOutputPath = canonicalOutputFile.getPath();
if (!canonicalOutputPath.equals(canonicalDestination.getPath()) && !canonicalOutputPath.startsWith(canonicalRoot))
{
throw new IOException("Blocked invalid UnrealPython bootstrap entry: " + entry.getName());
}
if (entry.isDirectory())
{
if (!canonicalOutputFile.mkdirs() && !canonicalOutputFile.exists())
{
throw new IOException("Failed to create UnrealPython bootstrap directory: " + canonicalOutputPath);
}
}
else
{
File parent = canonicalOutputFile.getParentFile();
if (parent != null && !parent.mkdirs() && !parent.exists())
{
throw new IOException("Failed to create UnrealPython bootstrap parent directory: " + parent.getPath());
}
try (FileOutputStream outputStream = new FileOutputStream(canonicalOutputFile))
{
int bytesRead;
while ((bytesRead = zipStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, bytesRead);
}
}
}
zipStream.closeEntry();
}
}
UnrealPythonWriteTextFile(versionFile, version);
Log.debug("Extracted UnrealPython bootstrap to: " + destinationPath);
return true;
}
catch (Exception exception)
{
Log.error("Failed to extract UnrealPython bootstrap: " + exception.getMessage());
exception.printStackTrace();
return false;
}
}
private void UnrealPythonDeleteRecursive(File file)
{
if (file == null || !file.exists())
{
return;
}
if (file.isDirectory())
{
File[] children = file.listFiles();
if (children != null)
{
for (File child : children)
{
UnrealPythonDeleteRecursive(child);
}
}
}
if (!file.delete() && file.exists())
{
Log.debug("Failed to delete UnrealPython bootstrap path: " + file.getPath());
}
}
private String UnrealPythonReadTextFile(File file) throws IOException
{
try (FileInputStream inputStream = new FileInputStream(file))
{
byte[] bytes = new byte[(int)file.length()];
int offset = 0;
while (offset < bytes.length)
{
int bytesRead = inputStream.read(bytes, offset, bytes.length - offset);
if (bytesRead <= 0)
{
break;
}
offset += bytesRead;
}
return new String(bytes, 0, offset, StandardCharsets.UTF_8);
}
}
private void UnrealPythonWriteTextFile(File file, String text) throws IOException
{
try (FileOutputStream outputStream = new FileOutputStream(file))
{
outputStream.write(text.getBytes(StandardCharsets.UTF_8));
}
}
]]></insert>
</gameActivityClassAdditions>
</root>