@@ -35,6 +35,8 @@ import android.util.Log
3535import org.godotengine.godot.Godot
3636import org.godotengine.godot.io.StorageScope
3737import org.godotengine.godot.io.directory.DirectoryAccessHandler.AccessType.ACCESS_RESOURCES
38+ import java.util.concurrent.locks.ReentrantLock
39+ import kotlin.concurrent.withLock
3840
3941/* *
4042 * Handles files and directories access and manipulation for the Android platform
@@ -145,9 +147,10 @@ class DirectoryAccessHandler(context: Context) {
145147
146148 private val assetsDirAccess = AssetsDirectoryAccess (context)
147149 private val fileSystemDirAccess = FilesystemDirectoryAccess (context, storageScopeIdentifier)
150+ private val lock = ReentrantLock ()
148151
149- fun assetsFileExists (assetsPath : String ) = assetsDirAccess.fileExists(assetsPath)
150- fun filesystemFileExists (path : String ) = fileSystemDirAccess.fileExists(path)
152+ fun assetsFileExists (assetsPath : String ) = lock.withLock { assetsDirAccess.fileExists(assetsPath) }
153+ fun filesystemFileExists (path : String ) = lock.withLock { fileSystemDirAccess.fileExists(path) }
151154
152155 private fun hasDirId (accessType : AccessType , dirId : Int ): Boolean {
153156 return when (accessType) {
@@ -156,7 +159,7 @@ class DirectoryAccessHandler(context: Context) {
156159 }
157160 }
158161
159- fun dirOpen (nativeAccessType : Int , path : String? ): Int {
162+ fun dirOpen (nativeAccessType : Int , path : String? ): Int = lock.withLock {
160163 if (path == null ) {
161164 return INVALID_DIR_ID
162165 }
@@ -176,7 +179,7 @@ class DirectoryAccessHandler(context: Context) {
176179 return dirAccessId
177180 }
178181
179- fun dirNext (dirAccessId : Int ): String {
182+ fun dirNext (dirAccessId : Int ): String = lock.withLock {
180183 val (accessType, dirId) = AccessType .fromDirAccessId(dirAccessId)
181184 if (accessType == null || ! hasDirId(accessType, dirId)) {
182185 Log .w(TAG , " dirNext: Invalid dir id: $dirId " )
@@ -189,7 +192,7 @@ class DirectoryAccessHandler(context: Context) {
189192 }
190193 }
191194
192- fun dirClose (dirAccessId : Int ) {
195+ fun dirClose (dirAccessId : Int ) = lock.withLock {
193196 val (accessType, dirId) = AccessType .fromDirAccessId(dirAccessId)
194197 if (accessType == null || ! hasDirId(accessType, dirId)) {
195198 Log .w(TAG , " dirClose: Invalid dir id: $dirId " )
@@ -202,7 +205,7 @@ class DirectoryAccessHandler(context: Context) {
202205 }
203206 }
204207
205- fun dirIsDir (dirAccessId : Int ): Boolean {
208+ fun dirIsDir (dirAccessId : Int ): Boolean = lock.withLock {
206209 val (accessType, dirId) = AccessType .fromDirAccessId(dirAccessId)
207210 if (accessType == null || ! hasDirId(accessType, dirId)) {
208211 Log .w(TAG , " dirIsDir: Invalid dir id: $dirId " )
@@ -215,7 +218,7 @@ class DirectoryAccessHandler(context: Context) {
215218 }
216219 }
217220
218- fun isCurrentHidden (dirAccessId : Int ): Boolean {
221+ fun isCurrentHidden (dirAccessId : Int ): Boolean = lock.withLock {
219222 val (accessType, dirId) = AccessType .fromDirAccessId(dirAccessId)
220223 if (accessType == null || ! hasDirId(accessType, dirId)) {
221224 return false
@@ -227,7 +230,7 @@ class DirectoryAccessHandler(context: Context) {
227230 }
228231 }
229232
230- fun dirExists (nativeAccessType : Int , path : String? ): Boolean {
233+ fun dirExists (nativeAccessType : Int , path : String? ): Boolean = lock.withLock {
231234 if (path == null ) {
232235 return false
233236 }
@@ -241,7 +244,7 @@ class DirectoryAccessHandler(context: Context) {
241244 }
242245 }
243246
244- fun fileExists (nativeAccessType : Int , path : String? ): Boolean {
247+ fun fileExists (nativeAccessType : Int , path : String? ): Boolean = lock.withLock {
245248 if (path == null ) {
246249 return false
247250 }
@@ -255,23 +258,23 @@ class DirectoryAccessHandler(context: Context) {
255258 }
256259 }
257260
258- fun getDriveCount (nativeAccessType : Int ): Int {
261+ fun getDriveCount (nativeAccessType : Int ): Int = lock.withLock {
259262 val accessType = AccessType .fromNative(nativeAccessType) ? : return 0
260263 return when (accessType) {
261264 ACCESS_RESOURCES -> assetsDirAccess.getDriveCount()
262265 else -> fileSystemDirAccess.getDriveCount()
263266 }
264267 }
265268
266- fun getDrive (nativeAccessType : Int , drive : Int ): String {
269+ fun getDrive (nativeAccessType : Int , drive : Int ): String = lock.withLock {
267270 val accessType = AccessType .fromNative(nativeAccessType) ? : return " "
268271 return when (accessType) {
269272 ACCESS_RESOURCES -> assetsDirAccess.getDrive(drive)
270273 else -> fileSystemDirAccess.getDrive(drive)
271274 }
272275 }
273276
274- fun makeDir (nativeAccessType : Int , dir : String? ): Boolean {
277+ fun makeDir (nativeAccessType : Int , dir : String? ): Boolean = lock.withLock {
275278 if (dir == null ) {
276279 return false
277280 }
@@ -285,23 +288,23 @@ class DirectoryAccessHandler(context: Context) {
285288 }
286289 }
287290
288- fun getSpaceLeft (nativeAccessType : Int ): Long {
291+ fun getSpaceLeft (nativeAccessType : Int ): Long = lock.withLock {
289292 val accessType = AccessType .fromNative(nativeAccessType) ? : return 0L
290293 return when (accessType) {
291294 ACCESS_RESOURCES -> assetsDirAccess.getSpaceLeft()
292295 else -> fileSystemDirAccess.getSpaceLeft()
293296 }
294297 }
295298
296- fun rename (nativeAccessType : Int , from : String , to : String ): Boolean {
299+ fun rename (nativeAccessType : Int , from : String , to : String ): Boolean = lock.withLock {
297300 val accessType = AccessType .fromNative(nativeAccessType) ? : return false
298301 return when (accessType) {
299302 ACCESS_RESOURCES -> assetsDirAccess.rename(from, to)
300303 else -> fileSystemDirAccess.rename(from, to)
301304 }
302305 }
303306
304- fun remove (nativeAccessType : Int , filename : String? ): Boolean {
307+ fun remove (nativeAccessType : Int , filename : String? ): Boolean = lock.withLock {
305308 if (filename == null ) {
306309 return false
307310 }
0 commit comments