From 6a9cac033707e360a8d082324fbe9a31e84873c8 Mon Sep 17 00:00:00 2001
From: ALEXTANG <574809918@qq.com>
Date: Wed, 25 May 2022 00:35:16 +0800
Subject: [PATCH] Fixed the FileSystem of Android
Fixed the FileSystem of Android
---
.../TEngine/Runtime/FileSystem/FileSystem.cs | 45 +++++++++++++++++++
.../src/TEngineCore/FileSystem/FileSystem.cs | 45 +++++++++++++++++++
2 files changed, 90 insertions(+)
diff --git a/Assets/TEngine/Runtime/FileSystem/FileSystem.cs b/Assets/TEngine/Runtime/FileSystem/FileSystem.cs
index 77ac5b70..5869fbb0 100644
--- a/Assets/TEngine/Runtime/FileSystem/FileSystem.cs
+++ b/Assets/TEngine/Runtime/FileSystem/FileSystem.cs
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
+using UnityEngine.Networking;
namespace TEngine
{
@@ -137,7 +138,19 @@ namespace TEngine
if (string.IsNullOrEmpty(filePath))
return null;
+#if UNITY_ANDROID && !UNITY_EDITOR
+ //外部目录
+ if (filePath.StartsWith(Application.persistentDataPath))
+ {
+ return ReadAllBytesFromOutFolder(filePath);
+ }
+ else //内部目录
+ {
+ return ReadAllBytesFromInnerFolder(filePath);
+ }
+#else
return ReadAllBytesFromOutFolder(filePath);
+#endif
}
private static byte[] ReadAllBytesFromOutFolder(string filePath)
@@ -149,5 +162,37 @@ namespace TEngine
}
return File.ReadAllBytes(filePath);
}
+
+ private static byte[] ReadAllBytesFromInnerFolder(string filePath)
+ {
+#if !UNITY_ANDROID || UNITY_EDITOR
+ filePath = $"file://{filePath}";
+#endif
+ UnityWebRequest www = UnityWebRequest.Get(filePath);
+ UnityWebRequestAsyncOperation request = www.SendWebRequest();
+ while (!request.isDone) ;
+ byte[] data = www.downloadHandler.data;
+ www.downloadHandler.Dispose();
+ www.Dispose();
+ www = null;
+ return data;
+ }
+
+ ///
+ /// 读取内部目录
+ ///
+ /// 文件完整路径
+ ///
+ private static string ReadTextFromInnerFolder(string filePath)
+ {
+#if !UNITY_ANDROID || UNITY_EDITOR
+ filePath = $"file://{filePath}";
+#endif
+ UnityWebRequest www = UnityWebRequest.Get(filePath);
+ UnityWebRequestAsyncOperation request = www.SendWebRequest();
+ while (!request.isDone) ;
+
+ return www.downloadHandler.text;
+ }
}
}
\ No newline at end of file
diff --git a/TEngineHotUpdate/src/TEngineCore/FileSystem/FileSystem.cs b/TEngineHotUpdate/src/TEngineCore/FileSystem/FileSystem.cs
index 2e746e56..f9cc3cde 100644
--- a/TEngineHotUpdate/src/TEngineCore/FileSystem/FileSystem.cs
+++ b/TEngineHotUpdate/src/TEngineCore/FileSystem/FileSystem.cs
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
+using UnityEngine.Networking;
namespace TEngineCore
{
@@ -137,7 +138,19 @@ namespace TEngineCore
if (string.IsNullOrEmpty(filePath))
return null;
+#if UNITY_ANDROID && !UNITY_EDITOR
+ //外部目录
+ if (filePath.StartsWith(Application.persistentDataPath))
+ {
+ return ReadAllBytesFromOutFolder(filePath);
+ }
+ else //内部目录
+ {
+ return ReadAllBytesFromInnerFolder(filePath);
+ }
+#else
return ReadAllBytesFromOutFolder(filePath);
+#endif
}
private static byte[] ReadAllBytesFromOutFolder(string filePath)
@@ -149,5 +162,37 @@ namespace TEngineCore
}
return File.ReadAllBytes(filePath);
}
+
+ private static byte[] ReadAllBytesFromInnerFolder(string filePath)
+ {
+#if !UNITY_ANDROID || UNITY_EDITOR
+ filePath = $"file://{filePath}";
+#endif
+ UnityWebRequest www = UnityWebRequest.Get(filePath);
+ UnityWebRequestAsyncOperation request = www.SendWebRequest();
+ while (!request.isDone) ;
+ byte[] data = www.downloadHandler.data;
+ www.downloadHandler.Dispose();
+ www.Dispose();
+ www = null;
+ return data;
+ }
+
+ ///
+ /// 读取内部目录
+ ///
+ /// 文件完整路径
+ ///
+ private static string ReadTextFromInnerFolder(string filePath)
+ {
+#if !UNITY_ANDROID || UNITY_EDITOR
+ filePath = $"file://{filePath}";
+#endif
+ UnityWebRequest www = UnityWebRequest.Get(filePath);
+ UnityWebRequestAsyncOperation request = www.SendWebRequest();
+ while (!request.isDone) ;
+
+ return www.downloadHandler.text;
+ }
}
}
\ No newline at end of file