This commit is contained in:
sin365 2025-04-29 00:07:14 +08:00
parent 4430b23fdf
commit 05ae1163e3
2 changed files with 230 additions and 202 deletions

View File

@ -8,18 +8,18 @@ using System.Text.RegularExpressions;
public class AxiNSIO public class AxiNSIO
{ {
string save_name => AxiNS.instance.mount.SaveMountName; string save_name => AxiNS.instance.mount.SaveMountName;
public string save_path => $"{save_name}:/"; public string save_path => $"{save_name}:/";
#if UNITY_SWITCH #if UNITY_SWITCH
private FileHandle fileHandle = new nn.fs.FileHandle(); private FileHandle fileHandle = new nn.fs.FileHandle();
#endif #endif
/// <summary> /// <summary>
/// 检查Path是否存在 /// 检查Path是否存在
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <returns></returns> /// <returns></returns>
public bool CheckPathExists(string filePath) public bool CheckPathExists(string filePath)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -29,14 +29,14 @@ public class AxiNSIO
//这个异常捕获。真的别扭 //这个异常捕获。真的别扭
return nn.fs.FileSystem.ResultPathAlreadyExists.Includes(result); return nn.fs.FileSystem.ResultPathAlreadyExists.Includes(result);
#endif #endif
} }
/// <summary> /// <summary>
/// 检查Path是否不存在 /// 检查Path是否不存在
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <returns></returns> /// <returns></returns>
public bool CheckPathNotFound(string filePath) public bool CheckPathNotFound(string filePath)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -45,14 +45,14 @@ public class AxiNSIO
//这个异常捕获。真的别扭 //这个异常捕获。真的别扭
return nn.fs.FileSystem.ResultPathNotFound.Includes(result); return nn.fs.FileSystem.ResultPathNotFound.Includes(result);
#endif #endif
} }
/// <summary> /// <summary>
/// 创建目录目录存在也会返回true /// 创建目录目录存在也会返回true
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <returns></returns> /// <returns></returns>
public bool CreateDir(string filePath) public bool CreateDir(string filePath)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -64,38 +64,38 @@ public class AxiNSIO
} }
return true; return true;
#endif #endif
} }
/// <summary> /// <summary>
/// 保存并创建文件(如果目录不存在回先自动创建目录) /// 保存并创建文件(如果目录不存在回先自动创建目录)
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <param name="bw"></param> /// <param name="bw"></param>
/// <returns></returns> /// <returns></returns>
public bool FileToSaveWithCreate(string filePath, System.IO.MemoryStream ms) public bool FileToSaveWithCreate(string filePath, System.IO.MemoryStream ms)
{ {
return FileToSaveWithCreate(filePath, ms.ToArray()); return FileToSaveWithCreate(filePath, ms.ToArray());
} }
/// <summary> /// <summary>
/// 保存并创建文件(如果目录不存在回先自动创建目录) /// 保存并创建文件(如果目录不存在回先自动创建目录)
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <param name="data"></param> /// <param name="data"></param>
/// <returns></returns> /// <returns></returns>
public AxiNSWait_FileToSaveByMSWithCreate FileToSaveWithCreateAsync(string filePath, System.IO.MemoryStream ms) public AxiNSWait_FileToSaveByMSWithCreate FileToSaveWithCreateAsync(string filePath, System.IO.MemoryStream ms)
{ {
var wait = new AxiNSWait_FileToSaveByMSWithCreate(filePath, ms); var wait = new AxiNSWait_FileToSaveByMSWithCreate(filePath, ms);
AxiNS.instance.wait.AddWait(wait); AxiNS.instance.wait.AddWait(wait);
return wait; return wait;
} }
/// <summary> /// <summary>
/// 保存并创建文件(如果目录不存在回先自动创建目录) /// 保存并创建文件(如果目录不存在回先自动创建目录)
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <param name="data"></param> /// <param name="data"></param>
/// <returns></returns> /// <returns></returns>
public bool FileToSaveWithCreate(string filePath, byte[] data) public bool FileToSaveWithCreate(string filePath, byte[] data)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -199,42 +199,42 @@ public class AxiNSIO
return true; return true;
#endif #endif
} }
/// <summary> /// <summary>
/// 保存并创建文件(如果目录不存在回先自动创建目录) /// 保存并创建文件(如果目录不存在回先自动创建目录)
/// </summary> /// </summary>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <param name="data"></param> /// <param name="data"></param>
/// <returns></returns> /// <returns></returns>
public AxiNSWait_FileToSaveWithCreate FileToSaveWithCreateAsync(string filePath, byte[] data) public AxiNSWait_FileToSaveWithCreate FileToSaveWithCreateAsync(string filePath, byte[] data)
{ {
var wait = new AxiNSWait_FileToSaveWithCreate(filePath, data); var wait = new AxiNSWait_FileToSaveWithCreate(filePath, data);
AxiNS.instance.wait.AddWait(wait); AxiNS.instance.wait.AddWait(wait);
return wait; return wait;
} }
public byte[] LoadSwitchDataFile(string filename) public byte[] LoadSwitchDataFile(string filename)
{ {
LoadSwitchDataFile(filename, out byte[] outputData); LoadSwitchDataFile(filename, out byte[] outputData);
return outputData; return outputData;
} }
public bool LoadSwitchDataFile(string filename, ref System.IO.MemoryStream ms) public bool LoadSwitchDataFile(string filename, ref System.IO.MemoryStream ms)
{ {
if (LoadSwitchDataFile(filename, out byte[] outputData)) if (LoadSwitchDataFile(filename, out byte[] outputData))
{ {
using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(ms)) using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(ms))
{ {
writer.Write(outputData); writer.Write(outputData);
} }
return true; return true;
} }
return false; return false;
} }
public bool LoadSwitchDataFile(string filename, out byte[] outputData) public bool LoadSwitchDataFile(string filename, out byte[] outputData)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH || UNITY_EDITOR
outputData = null; outputData = null;
return false; return false;
#else #else
outputData = null; outputData = null;
if (!AxiNS.instance.mount.SaveIsMount) if (!AxiNS.instance.mount.SaveIsMount)
@ -282,67 +282,100 @@ public class AxiNSIO
outputData = loadedData; outputData = loadedData;
return true; return true;
#endif #endif
} }
public AxiNSWait_LoadSwitchDataFile LoadSwitchDataFileAsync(string filename) public AxiNSWait_LoadSwitchDataFile LoadSwitchDataFileAsync(string filename)
{ {
var wait = new AxiNSWait_LoadSwitchDataFile(filename); var wait = new AxiNSWait_LoadSwitchDataFile(filename);
AxiNS.instance.wait.AddWait(wait); AxiNS.instance.wait.AddWait(wait);
return wait; return wait;
} }
public bool GetDirectoryFiles(string path, out string[] entrys) public bool GetDirectoryFiles(string path, out string[] entrys)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH || UNITY_EDITOR
entrys = null;
return false; entrys = null;
return false;
#else #else
return GetDirectoryEntrys(path,nn.fs.OpenDirectoryMode.File,out entrys); return GetDirectoryEntrys(path,nn.fs.OpenDirectoryMode.File,out entrys);
#endif #endif
} }
public bool GetDirectoryDirs(string path, out string[] entrys) public bool GetDirectoryDirs(string path, out string[] entrys)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH || UNITY_EDITOR
entrys = null; entrys = null;
return false; return false;
#else #else
return GetDirectoryEntrys(path, nn.fs.OpenDirectoryMode.Directory, out entrys); return GetDirectoryEntrys(path, nn.fs.OpenDirectoryMode.Directory, out entrys);
#endif #endif
} }
#if UNITY_SWITCH #if UNITY_SWITCH
public bool GetDirectoryEntrys(string path, nn.fs.OpenDirectoryMode type, out string[] entrys) public bool GetDirectoryEntrys(string path, nn.fs.OpenDirectoryMode type, out string[] entrys)
{ {
entrys = null; nn.fs.DirectoryHandle eHandle = new nn.fs.DirectoryHandle();
return false; nn.Result result = nn.fs.Directory.Open(ref eHandle, path, type);
nn.fs.DirectoryHandle dirHandle = new nn.fs.DirectoryHandle(); if (nn.fs.FileSystem.ResultPathNotFound.Includes(result))
nn.Result result = nn.fs.Directory.Open(ref dirHandle, path, type); {
if (nn.fs.FileSystem.ResultPathNotFound.Includes(result)) UnityEngine.Debug.Log($"目录 {path} 不存在");
{ entrys = null;
UnityEngine.Debug.Log($"目录 {path} 不存在"); return false;
entrys = null; }
return false; long entryCount = 0;
} nn.fs.Directory.GetEntryCount(ref entryCount, eHandle);
long entryCount = 0; nn.fs.DirectoryEntry[] entries = new nn.fs.DirectoryEntry[entryCount];
nn.fs.Directory.GetEntryCount(ref entryCount, dirHandle); long actualEntries = 0;
nn.fs.DirectoryEntry[] dirEntries = new nn.fs.DirectoryEntry[entryCount]; nn.fs.Directory.Read(ref actualEntries, entries, eHandle, entryCount);
long actualEntries = 0;
nn.fs.Directory.Read(ref actualEntries, dirEntries, dirHandle, entryCount);
entrys = new string[actualEntries]; entrys = new string[actualEntries];
for (int i = 0; i < actualEntries; i++) for (int i = 0; i < actualEntries; i++)
{ {
entrys[i] = dirEntries[i].name; entrys[i] = System.IO.Path.Combine(path, entries[i].name);
} }
nn.fs.Directory.Close(dirHandle); nn.fs.Directory.Close(eHandle);
return true; return true;
} }
#endif #endif
public IEnumerable<string> EnumerateFiles(string path, string searchPattern)
{ #if UNITY_SWITCH
#if !UNITY_SWITCH public bool GetDirectoryEntrysFullRecursion(string path, out string[] entrys)
yield break; {
nn.fs.DirectoryHandle eHandle = new nn.fs.DirectoryHandle();
nn.Result result = nn.fs.Directory.Open(ref eHandle, path, OpenDirectoryMode.All);
if (nn.fs.FileSystem.ResultPathNotFound.Includes(result))
{
UnityEngine.Debug.Log($"目录 {path} 不存在");
entrys = null;
return false;
}
long entryCount = 0;
nn.fs.Directory.GetEntryCount(ref entryCount, eHandle);
nn.fs.DirectoryEntry[] entries = new nn.fs.DirectoryEntry[entryCount];
long actualEntries = 0;
nn.fs.Directory.Read(ref actualEntries, entries, eHandle, entryCount);
List<string> temp = new List<string>();
for (int i = 0; i < actualEntries; i++)
{
string singlePath = System.IO.Path.Combine(path, entries[i].name);
temp.Add(singlePath);
if (entries[i].entryType == EntryType.Directory && GetDirectoryEntrysFullRecursion(singlePath, out string[] singleEntryList))
{
temp.AddRange(singleEntryList);
}
}
nn.fs.Directory.Close(eHandle);
entrys = temp.ToArray();
return true;
}
#endif
public IEnumerable<string> EnumerateFiles(string path, string searchPattern)
{
#if !UNITY_SWITCH || UNITY_EDITOR
yield break;
#else #else
// 将通配符转换为正则表达式(支持*和? // 将通配符转换为正则表达式(支持*和?
var regexPattern = "^" + var regexPattern = "^" +
@ -366,10 +399,10 @@ public class AxiNSIO
} }
} }
#endif #endif
} }
public bool DeletePathFile(string filename) public bool DeletePathFile(string filename)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -404,15 +437,15 @@ public class AxiNSIO
#endif #endif
#endif #endif
} }
public AxiNSWait_DeletePathFile DeletePathFileAsync(string filename) public AxiNSWait_DeletePathFile DeletePathFileAsync(string filename)
{ {
var wait = new AxiNSWait_DeletePathFile(filename); var wait = new AxiNSWait_DeletePathFile(filename);
AxiNS.instance.wait.AddWait(wait); AxiNS.instance.wait.AddWait(wait);
return wait; return wait;
} }
public bool DeletePathDir(string filename) public bool DeletePathDir(string filename)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -445,21 +478,21 @@ public class AxiNSIO
UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection(); UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection();
#endif #endif
#endif #endif
} }
public AxiNSWait_DeletePathDir DeletePathDirAsync(string filename) public AxiNSWait_DeletePathDir DeletePathDirAsync(string filename)
{ {
var wait = new AxiNSWait_DeletePathDir(filename); var wait = new AxiNSWait_DeletePathDir(filename);
AxiNS.instance.wait.AddWait(wait); AxiNS.instance.wait.AddWait(wait);
return wait; return wait;
} }
/// <summary> /// <summary>
/// 递归删除目录 /// 递归删除目录
/// </summary> /// </summary>
/// <param name="filename"></param> /// <param name="filename"></param>
/// <returns></returns> /// <returns></returns>
public bool DeleteRecursivelyPathDir(string filename) public bool DeleteRecursivelyPathDir(string filename)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -492,15 +525,15 @@ public class AxiNSIO
UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection(); UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection();
#endif #endif
#endif #endif
} }
/// <summary> /// <summary>
/// 递归删除情况 /// 递归删除情况
/// </summary> /// </summary>
/// <param name="filename"></param> /// <param name="filename"></param>
/// <returns></returns> /// <returns></returns>
public bool CleanRecursivelyPathDir(string filename) public bool CleanRecursivelyPathDir(string filename)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -533,10 +566,10 @@ public class AxiNSIO
UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection(); UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection();
#endif #endif
#endif #endif
} }
public bool RenameDir(string oldpath, string newpath) public bool RenameDir(string oldpath, string newpath)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -570,9 +603,9 @@ public class AxiNSIO
UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection(); UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection();
#endif #endif
#endif #endif
} }
bool EnsureParentDirectory(string filePath, bool bAutoCreateDir = true) bool EnsureParentDirectory(string filePath, bool bAutoCreateDir = true)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -648,14 +681,14 @@ public class AxiNSIO
return true; return true;
#endif #endif
} }
/// <summary> /// <summary>
/// 检查指定挂载点是否可访问 /// 检查指定挂载点是否可访问
/// </summary> /// </summary>
/// <param name="pathPrefix">路径前缀,例如 "save:/" 或 "sd:/"</param> /// <param name="pathPrefix">路径前缀,例如 "save:/" 或 "sd:/"</param>
/// <returns>挂载点是否可访问</returns> /// <returns>挂载点是否可访问</returns>
bool IsMountPointAccessible(string pathPrefix) bool IsMountPointAccessible(string pathPrefix)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -693,5 +726,5 @@ public class AxiNSIO
return true; // 其他挂载点需根据实际需求实现 return true; // 其他挂载点需根据实际需求实现
} }
#endif #endif
} }
} }

View File

@ -129,28 +129,26 @@ public class MainTest : MonoBehaviour
UnityEngine.Debug.Log($"result =>{result}"); UnityEngine.Debug.Log($"result =>{result}");
else else
{ {
UnityEngine.Debug.Log($"====EntrysList====");
foreach (var e in elist) foreach (var e in elist)
{ UnityEngine.Debug.Log(e);
UnityEngine.Debug.Log($"result =>{result}");
}
} }
} }
); text17.text = $"AxiNS.instance.io.GetDirectoryEntrys({"save:/"},nn.fs.OpenDirectoryMode.All,out var elist)"; ); text17.text = $"AxiNS.instance.io.GetDirectoryEntrys({"save:/"},nn.fs.OpenDirectoryMode.All,out var elist)";
btn18.onClick.AddListener(() => btn18.onClick.AddListener(() =>
{ {
bool result = AxiNS.instance.io.GetDirectoryEntrys("save:/axibug", nn.fs.OpenDirectoryMode.All, out var elist); bool result = AxiNS.instance.io.GetDirectoryEntrysFullRecursion("save:/",out var elist);
if (!result) if (!result)
UnityEngine.Debug.Log($"result =>{result}"); UnityEngine.Debug.Log($"result =>{result}");
else else
{ {
UnityEngine.Debug.Log($"==== FullRecursion Entrys List====");
foreach (var e in elist) foreach (var e in elist)
{ UnityEngine.Debug.Log(e);
UnityEngine.Debug.Log($"result =>{result}");
}
} }
} }
); text18.text = $"AxiNS.instance.io.GetDirectoryEntrys({"save:/axibug"},nn.fs.OpenDirectoryMode.All,out var elist)"; ); text18.text = $"AxiNS.instance.io.GetDirectoryEntrysFullRecursion({"save:/"},out var elist)";
btn19.onClick.AddListener(() => btn19.onClick.AddListener(() =>
{ {
@ -159,10 +157,9 @@ public class MainTest : MonoBehaviour
UnityEngine.Debug.Log($"result =>{result}"); UnityEngine.Debug.Log($"result =>{result}");
else else
{ {
UnityEngine.Debug.Log($"====EntrysList====");
foreach (var e in elist) foreach (var e in elist)
{ UnityEngine.Debug.Log(e);
UnityEngine.Debug.Log($"result =>{result}");
}
} }
} }
); text19.text = $"AxiNS.instance.io.GetDirectoryEntrys({"save:/axibug/"},nn.fs.OpenDirectoryMode.All,out var elist)"; ); text19.text = $"AxiNS.instance.io.GetDirectoryEntrys({"save:/axibug/"},nn.fs.OpenDirectoryMode.All,out var elist)";
@ -174,10 +171,9 @@ public class MainTest : MonoBehaviour
UnityEngine.Debug.Log($"result =>{result}"); UnityEngine.Debug.Log($"result =>{result}");
else else
{ {
UnityEngine.Debug.Log($"====EntrysList====");
foreach (var e in elist) foreach (var e in elist)
{ UnityEngine.Debug.Log(e);
UnityEngine.Debug.Log($"result =>{result}");
}
} }
} }
); text20.text = $"AxiNS.instance.io.GetDirectoryDirs({"save:/"},out var elist)"; ); text20.text = $"AxiNS.instance.io.GetDirectoryDirs({"save:/"},out var elist)";
@ -189,10 +185,9 @@ public class MainTest : MonoBehaviour
UnityEngine.Debug.Log($"result =>{result}"); UnityEngine.Debug.Log($"result =>{result}");
else else
{ {
UnityEngine.Debug.Log($"====EntrysList====");
foreach (var e in elist) foreach (var e in elist)
{ UnityEngine.Debug.Log(e);
UnityEngine.Debug.Log($"result =>{result}");
}
} }
} }
); text21.text = $"AxiNS.instance.io.GetDirectoryFiles({"save:/"},out var elist)"; ); text21.text = $"AxiNS.instance.io.GetDirectoryFiles({"save:/"},out var elist)";