From 202d91334bdf7f5eb097463eca35ef06a1b724b9 Mon Sep 17 00:00:00 2001 From: tverona1 Date: Sat, 17 Aug 2019 17:34:53 -0700 Subject: [PATCH] Lock around downloading assets - Added lock around downloading assets to ensure only one download task is running at a time --- Assets/Resources/OVRBuildConfig.asset | 2 +- Assets/Scripts/AssetsDownloader.cs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Assets/Resources/OVRBuildConfig.asset b/Assets/Resources/OVRBuildConfig.asset index f2d36f8..cf5cd6c 100644 --- a/Assets/Resources/OVRBuildConfig.asset +++ b/Assets/Resources/OVRBuildConfig.asset @@ -12,6 +12,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 20553fac56ec59645857c0732b787431, type: 3} m_Name: OVRBuildConfig m_EditorClassIdentifier: - androidSDKPath: C:\Users\tvero\AppData\Local\Android\Sdk + androidSDKPath: gradlePath: jdkPath: diff --git a/Assets/Scripts/AssetsDownloader.cs b/Assets/Scripts/AssetsDownloader.cs index 0e00784..3639df8 100644 --- a/Assets/Scripts/AssetsDownloader.cs +++ b/Assets/Scripts/AssetsDownloader.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; @@ -30,6 +31,9 @@ namespace QuestAppLauncher // Rate limit in minutes const int RateLimitInMins = 5; + // Used for mutual exclusion when loading assets + static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1); + /// /// Class that tracks each file downloaded. /// Used to determine whether an asset has been updated since last download. @@ -75,6 +79,9 @@ namespace QuestAppLauncher // Start background thread Task.Run(async () => { + // Mutual exclusion while loading assets + await AssetsDownloader.semaphoreSlim.WaitAsync(); + // Attach / detatch JNI. Required for any calls into JNI from background threads. AndroidJNI.AttachCurrentThread(); @@ -86,6 +93,7 @@ namespace QuestAppLauncher } finally { + AssetsDownloader.semaphoreSlim.Release(); AndroidJNI.DetachCurrentThread(); } }).ContinueWith((downloadedAssets) =>