Skip to content

Commit 888b729

Browse files
authored
Merge pull request #214 from ManageXR/feature/eng-584-fix-nullreferenceexception-in-mxrstoragegetfullpath
ENG-584: Fix NullReferenceException in MXRStorage.GetFullPath
2 parents 0829315 + 8286306 commit 888b729

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

Assets/MXR.SDK/Runtime/Utils/MXRStorage.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ public static string ExternalStorageDirectory {
4343
public static string MXRRootDirectory => Path.Combine(ExternalStorageDirectory, "MightyImmersion");
4444

4545
/// <summary>
46-
/// Returns the full path to a sub path inside <see cref="ExternalStorageDirectory"/>
46+
/// Returns the full path to a sub path inside <see cref="ExternalStorageDirectory"/>.
47+
/// Returns null if the provided path is null or empty.
4748
/// </summary>
49+
/// <param name="path">The relative path to convert to a full path. May be null or empty.</param>
50+
/// <returns>The full path, or null if the input path is null or empty.</returns>
4851
public static string GetFullPath(string path) {
52+
if (string.IsNullOrEmpty(path))
53+
return null;
54+
4955
path = TryRemoveLeadingSpash(path);
5056
return Path.Combine(ExternalStorageDirectory, path);
5157
}

Assets/MXR.SDK/Samples/Scripts/ImageDownloader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public void Download(string url, Action<Texture2D> callback) {
3131
/// <param name="onSuccess">Callback invoked when the load is successful.</param>
3232
/// <param name="onError">Callback invoked when the load fails.</param>
3333
public void Load(string location, TextureFormat format, bool mipMap, Action<Texture2D> onSuccess, Action<Exception> onError) {
34+
if (string.IsNullOrEmpty(location)) {
35+
onError?.Invoke(new Exception("Cannot load image from null or empty location"));
36+
return;
37+
}
38+
3439
// If the location is a URL, we use the remote download method
3540
if (location.Contains("http://") || location.Contains("https://"))
3641
LoadFromURL(location, format, mipMap, onSuccess, onError);

Assets/MXR.SDK/Samples/Scripts/RuntimeAppCell.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ public class RuntimeAppCell : MonoBehaviour {
2020
public void Refresh() {
2121
title.text = runtimeApp.title;
2222

23-
// Instead of MXRStorage.GetFullPath(runtimeApp.iconPath) you can also use
24-
// runtimeApp.iconUrl to download the icon from a URL, like this:
25-
//new ImageDownloader().Load(runtimeApp.iconUrl, TextureFormat.ARGB32, true, result =>{}, error =>{});
26-
new ImageDownloader().Load(MXRStorage.GetFullPath(runtimeApp.iconPath), TextureFormat.ARGB32, true,
23+
// Try local path first, fall back to remote URL if not available
24+
string iconLocation = string.IsNullOrEmpty(runtimeApp.iconPath)
25+
? runtimeApp.iconUrl
26+
: MXRStorage.GetFullPath(runtimeApp.iconPath);
27+
28+
new ImageDownloader().Load(iconLocation, TextureFormat.ARGB32, true,
2729
result => {
2830
if (isBeingDestroyed) return;
2931

Assets/MXR.SDK/Samples/Scripts/VideoCell.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public class VideoCell : MonoBehaviour {
1818
public void Refresh() {
1919
title.text = video.title;
2020

21-
// Instead of MXRStorage.GetFullPath(video.iconPath) you can also use
22-
// video.iconUrl to download the icon from a URL, like this:
23-
//new ImageDownloader().Load(video.iconUrl, TextureFormat.ARGB32, true, result =>{}, error =>{});
24-
new ImageDownloader().Load(MXRStorage.GetFullPath(video.iconPath), TextureFormat.ARGB32, true,
21+
// Try local path first, fall back to remote URL if not available
22+
string iconLocation = string.IsNullOrEmpty(video.iconPath)
23+
? video.iconUrl
24+
: MXRStorage.GetFullPath(video.iconPath);
25+
26+
new ImageDownloader().Load(iconLocation, TextureFormat.ARGB32, true,
2527
result => {
2628
if (isBeingDestroyed) return;
2729

Assets/MXR.SDK/Samples/Scripts/WebXRAppCell.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ public class WebXRAppCell : MonoBehaviour {
1414
public void Refresh() {
1515
title.text = webXRApp.title;
1616

17-
// Instead of MXRStorage.GetFullPath(webXRApp.iconPath) you can also use
18-
// webXRApp.iconUrl to download the icon from a URL, like this:
19-
//new ImageDownloader().Load(webXRApp.iconUrl, TextureFormat.ARGB32, true, result =>{}, error =>{});
17+
// Try local path first, fall back to remote URL if not available
18+
string iconLocation = string.IsNullOrEmpty(webXRApp.iconPath)
19+
? webXRApp.iconUrl
20+
: MXRStorage.GetFullPath(webXRApp.iconPath);
2021

21-
new ImageDownloader().Load(MXRStorage.GetFullPath(webXRApp.iconPath), TextureFormat.ARGB32, true,
22+
new ImageDownloader().Load(iconLocation, TextureFormat.ARGB32, true,
2223
result => {
2324
if (isBeingDestroyed) return;
2425

0 commit comments

Comments
 (0)