Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit 10f9911

Browse files
authored
Merge pull request #183 from Unity-Technologies/fix_potential_crash
fix crash when font file doesn't exist
2 parents 11d6441 + 36e7cb6 commit 10f9911

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

com.unity.uiwidgets/Runtime/engine/AndroidPlatformUtil.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ public static class UIWidgetsAndroidConfiguration{
1717

1818
internal delegate string UnpackFileCallback(string file);
1919

20+
internal static bool FileExists(string file)
21+
{
22+
var path = "jar:file://" + Application.dataPath + "!/assets/" + file;
23+
using (var unpackerWWW = UnityWebRequest.Get(path)) {
24+
unpackerWWW.SendWebRequest();
25+
while (!unpackerWWW.isDone) {
26+
} // This will block in the webplayer.
27+
28+
if (unpackerWWW.isNetworkError || unpackerWWW.isHttpError) {
29+
return false;
30+
}
31+
}
32+
return true;
33+
}
34+
2035
[MonoPInvokeCallback(typeof(UnpackFileCallback))]
2136
internal static string unpackFile(string file) {
2237
var dir = Application.temporaryCachePath + "/";

com.unity.uiwidgets/Runtime/engine/UIWidgetsPanel.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.IO;
45
using Unity.UIWidgets.external.simplejson;
56
using Unity.UIWidgets.foundation;
67
using Unity.UIWidgets.rendering;
@@ -11,6 +12,7 @@
1112
using UnityEngine.Profiling;
1213
using UnityEngine.Scripting;
1314
using UnityEngine.UI;
15+
using Path = System.IO.Path;
1416
using RawImage = UnityEngine.UI.RawImage;
1517

1618
namespace Unity.UIWidgets.engine {
@@ -66,19 +68,37 @@ public object fontsToObject() {
6668
foreach (var setting in settings) {
6769
var font = new Dictionary<string, object>();
6870
font.Add("family", value: setting.Key);
69-
var dic = new Dictionary<string, object>[setting.Value.fonts.Length];
71+
var dic = new List<Dictionary<string, object>>();
7072
for (var j = 0; j < setting.Value.fonts.Length; j++) {
71-
dic[j] = new Dictionary<string, object>();
73+
var fontDic = new Dictionary<string, object>();
74+
var fileExist = false;
75+
7276
if (setting.Value.fonts[j].asset.Length > 0) {
73-
dic[j].Add("asset", value: setting.Value.fonts[j].asset);
77+
var assetPath = setting.Value.fonts[j].asset;
78+
var assetAbsolutePath = Path.Combine(Application.streamingAssetsPath, assetPath);
79+
#if !UNITY_EDITOR && UNITY_ANDROID
80+
if (!AndroidPlatformUtil.FileExists(assetPath)) {
81+
#else
82+
if (!File.Exists(assetAbsolutePath)) {
83+
#endif
84+
Debug.LogError($"The font asset (family: \"{setting.Key}\", path: \"{assetPath}\") is not found");
85+
}
86+
else {
87+
fileExist = true;
88+
}
89+
fontDic.Add("asset", value: setting.Value.fonts[j].asset);
7490
}
7591

7692
if (setting.Value.fonts[j].weight > 0) {
77-
dic[j].Add("weight", value: setting.Value.fonts[j].weight);
93+
fontDic.Add("weight", value: setting.Value.fonts[j].weight);
94+
}
95+
96+
if (fileExist) {
97+
dic.Add(fontDic);
7898
}
7999
}
80100

81-
font.Add("fonts", value: dic);
101+
font.Add("fonts", value: dic.ToArray());
82102
result[i] = font;
83103
i++;
84104
}

0 commit comments

Comments
 (0)