Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加ILoadFileServices 文件加载服务 #229

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public override void Update()
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
#if UNITY_ANDROID
if (MainBundleInfo.Bundle.Encrypted)
/// 如果文件加密了的话需要判断 是否可以读取StreamingAsset 中的文件
if (MainBundleInfo.Bundle.Encrypted && !FileUtility.EnableLoadStreamingAsset)
{
_steps = ESteps.Unpack;
FileLoadPath = MainBundleInfo.CachedDataFilePath;
Expand All @@ -62,10 +62,6 @@ public override void Update()
_steps = ESteps.LoadBundleFile;
FileLoadPath = MainBundleInfo.BuildinFilePath;
}
#else
_steps = ESteps.LoadBundleFile;
FileLoadPath = MainBundleInfo.BuildinFilePath;
#endif
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ public override void Update()
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
#if UNITY_ANDROID
_steps = ESteps.Unpack;
FileLoadPath = MainBundleInfo.CachedDataFilePath;
#else
_steps = ESteps.CheckFile;
FileLoadPath = MainBundleInfo.BuildinFilePath;
#endif
/// 判断是否可以直接从StreamingAsset 读取文件
if (FileUtility.EnableLoadStreamingAsset)
{
_steps = ESteps.CheckFile;
FileLoadPath = MainBundleInfo.BuildinFilePath;
}
else
{
_steps = ESteps.Unpack;
FileLoadPath = MainBundleInfo.CachedDataFilePath;
}
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
Expand Down Expand Up @@ -129,7 +133,7 @@ public override void Update()
DownloadProgress = 1f;
DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;

if (File.Exists(FileLoadPath))
if (FileUtility.Exists(FileLoadPath))
{
_steps = ESteps.Done;
Status = EStatus.Succeed;
Expand Down
120 changes: 120 additions & 0 deletions Assets/YooAsset/Runtime/Services/ILoadFileServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//using System.Collections;
//using System.Collections.Generic;
using System.IO;
using System.Text;
//using UnityEngine;
//using static Codice.CM.WorkspaceServer.WorkspaceTreeDataStore;

namespace YooAsset
{
public interface ILoadFileServices
{
/// <summary>
/// 判断是否可以直接从 StreamingAsset 读取文件
/// </summary>
public bool EnableLoadStreamingAsset { get; }

/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool Exists(string filePath);

/// <summary>
/// 读取文件内容 byte[]
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public byte[] ReadAllBytes(string filePath);

/// <summary>
/// 读取文件内容 string
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public string ReadAllText(string filePath);

/// <summary>
/// 写入bytes
/// </summary>
/// <param name="filePath"></param>
/// <param name="data"></param>
public void WriteAllBytes(string filePath, byte[] data);

/// <summary>
/// 写入文本
/// </summary>
/// <param name="filePath"></param>
/// <param name="text"></param>
public void WriteAllText(string filePath, string text);

/// <summary>
/// 获取文件大小
/// </summary>
/// <param name="filePath"></param>
public long GetFileSize(string filePath);
}

internal class DefaultLoadFileServices : ILoadFileServices
{
/// <summary>
/// 默认平台不支持android 直接读取streamingAsset文件
/// </summary>
public bool EnableLoadStreamingAsset
{
get
{
#if UNITY_ANDROID
return false;
#else
return true;
#endif
}
}

public bool Exists(string filePath)
{
return File.Exists(filePath);
}

public long GetFileSize(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}

public byte[] ReadAllBytes(string filePath)
{
if (File.Exists(filePath) == false)
return null;
return File.ReadAllBytes(filePath);
}

public string ReadAllText(string filePath)
{
if (File.Exists(filePath) == false)
return string.Empty;
return File.ReadAllText(filePath, Encoding.UTF8);
}

public void WriteAllBytes(string filePath, byte[] data)
{
// 创建文件夹路径
FileUtility.CreateFileDirectory(filePath);

File.WriteAllBytes(filePath, data);
}

public void WriteAllText(string filePath, string content)
{
// 创建文件夹路径
FileUtility.CreateFileDirectory(filePath);
//避免写入BOM标记
byte[] bytes = Encoding.UTF8.GetBytes(content);
File.WriteAllBytes(filePath, bytes);
}

}
}

11 changes: 11 additions & 0 deletions Assets/YooAsset/Runtime/Services/ILoadFileServices.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 49 additions & 33 deletions Assets/YooAsset/Runtime/Utility/YooUtility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -115,77 +115,93 @@ public static string Format(string format, params object[] args)
/// </summary>
internal static class FileUtility
{
/// <summary>
/// 文件加载服务
/// </summary>
static private ILoadFileServices _loadFileServices = new DefaultLoadFileServices();

/// <summary>
/// 判断是否可以直接从 StreamingAsset 读取文件
/// </summary>
public static bool EnableLoadStreamingAsset
{
get
{
return _loadFileServices.EnableLoadStreamingAsset;
}
}

/// <summary>
/// 设置文件加载服务
/// </summary>
/// <param name="loadFileServices"></param>
public static void SetLoadFileService(ILoadFileServices loadFileServices)
{
if (loadFileServices == null)
return;
_loadFileServices = loadFileServices;
}

/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static bool Exists(string filePath)
{
return _loadFileServices.Exists(filePath);
}

/// <summary>
/// 读取文件的文本数据
/// </summary>
public static string ReadAllText(string filePath)
{
if (File.Exists(filePath) == false)
return string.Empty;
return File.ReadAllText(filePath, Encoding.UTF8);
return _loadFileServices.ReadAllText(filePath);
}

/// <summary>
/// 读取文件的字节数据
/// </summary>
public static byte[] ReadAllBytes(string filePath)
{
if (File.Exists(filePath) == false)
return null;
return File.ReadAllBytes(filePath);
return _loadFileServices.ReadAllBytes(filePath);
}

/// <summary>
/// 写入文本数据(会覆盖指定路径的文件)
/// </summary>
public static void WriteAllText(string filePath, string content)
{
// 创建文件夹路径
CreateFileDirectory(filePath);

byte[] bytes = Encoding.UTF8.GetBytes(content);
File.WriteAllBytes(filePath, bytes); //避免写入BOM标记
_loadFileServices.WriteAllText(filePath, content);
}

/// <summary>
/// 写入字节数据(会覆盖指定路径的文件)
/// </summary>
public static void WriteAllBytes(string filePath, byte[] data)
{
// 创建文件夹路径
CreateFileDirectory(filePath);

File.WriteAllBytes(filePath, data);
_loadFileServices.WriteAllBytes(filePath, data);
}

/// <summary>
/// 创建文件的文件夹路径
/// 获取文件大小(字节数)
/// </summary>
public static void CreateFileDirectory(string filePath)
public static long GetFileSize(string filePath)
{
// 获取文件的文件夹路径
string directory = Path.GetDirectoryName(filePath);
CreateDirectory(directory);
return _loadFileServices.GetFileSize(filePath);
}

/// <summary>
/// 创建文件夹路径
/// 创建文件的文件夹路径
/// </summary>
public static void CreateDirectory(string directory)
public static void CreateFileDirectory(string filePath)
{
// If the directory doesn't exist, create it.
// 获取文件的文件夹路径
string directory = Path.GetDirectoryName(filePath);
if (Directory.Exists(directory) == false)
Directory.CreateDirectory(directory);
}

/// <summary>
/// 获取文件大小(字节数)
/// </summary>
public static long GetFileSize(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
}

/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions Assets/YooAsset/Runtime/YooAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,16 @@ internal static DebugReport GetDebugReport()
return report;
}
#endregion

#region 设置文件加载服务
/// <summary>
/// 设置文件加载服务
/// </summary>
/// <param name="loadFileServices"></param>
public static void SetLoadFileServices(ILoadFileServices loadFileServices)
{
FileUtility.SetLoadFileService(loadFileServices);
}
#endregion
}
}