引入最新版本的AxiNSApi

This commit is contained in:
sin365 2025-08-18 22:12:40 +08:00
parent 8afcbce6b1
commit 57c4947623
6 changed files with 384 additions and 214 deletions

View File

@ -15,6 +15,11 @@ public class AxiNS
} }
} }
/// <summary>
/// 延迟提交是否使用多线程
/// </summary>
public static bool usedmultithreading = false;
public AxiNSUser user; public AxiNSUser user;
public AxiNSMount mount; public AxiNSMount mount;
public AxiNSIO io; public AxiNSIO io;

View File

@ -7,20 +7,20 @@ 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
static object commitLock = new object(); static object commitLock = new object();
static bool bDirty = false; static bool bDirty = false;
bool CommitSave() bool CommitSave()
{ {
lock (commitLock) lock (commitLock)
{ {
#if UNITY_SWITCH && !UNITY_EDITOR #if UNITY_SWITCH && !UNITY_EDITOR
// 阻止用户在保存时,退出游戏 Switch 条例 0080 // 阻止用户在保存时,退出游戏 Switch 条例 0080
@ -39,41 +39,41 @@ public class AxiNSIO
bDirty = false; bDirty = false;
return true; return true;
#else #else
return false; return false;
#endif #endif
} }
} }
void SetCommitDirty() void SetCommitDirty()
{ {
lock (commitLock) lock (commitLock)
{ {
bDirty = true; bDirty = true;
} }
} }
public void ApplyAutoCommit() public void ApplyAutoCommit()
{ {
bool temp; bool temp;
lock (commitLock) lock (commitLock)
{ {
temp = bDirty; temp = bDirty;
} }
if (temp) if (temp)
{ {
CommitSave(); CommitSave();
} }
} }
/// <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
@ -86,14 +86,14 @@ public class AxiNSIO
//return nn.fs.FileSystem.ResultPathAlreadyExists.Includes(result); //return nn.fs.FileSystem.ResultPathAlreadyExists.Includes(result);
return !nn.fs.FileSystem.ResultPathNotFound.Includes(result); return !nn.fs.FileSystem.ResultPathNotFound.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
@ -102,16 +102,16 @@ 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)
{ {
lock (commitLock) lock (commitLock)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
@ -124,43 +124,43 @@ 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>
/// <param name="immediatelyCommit">是否立即Commit到物理存储</param> /// <param name="immediatelyCommit">是否立即Commit到物理存储</param>
/// <returns></returns> /// <returns></returns>
public bool FileToSaveWithCreate(string filePath, byte[] data, bool immediatelyCommit = true) public bool FileToSaveWithCreate(string filePath, byte[] data, bool immediatelyCommit = true)
{ {
lock (commitLock) lock (commitLock)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -293,42 +293,45 @@ 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); byte[] outputData;
return outputData; LoadSwitchDataFile(filename, out outputData);
} return outputData;
public bool LoadSwitchDataFile(string filename, ref System.IO.MemoryStream ms) }
{
if (LoadSwitchDataFile(filename, out byte[] outputData)) public bool LoadSwitchDataFile(string filename, ref System.IO.MemoryStream ms)
{ {
using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(ms)) byte[] outputData;
{ if (LoadSwitchDataFile(filename, out outputData))
writer.Write(outputData); {
} using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(ms))
return true; {
} writer.Write(outputData);
return false; }
} return true;
public bool LoadSwitchDataFile(string filename, out byte[] outputData) }
{ return false;
}
public bool LoadSwitchDataFile(string filename, out byte[] outputData)
{
#if !UNITY_SWITCH || UNITY_EDITOR #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)
@ -376,32 +379,34 @@ 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 || UNITY_EDITOR #if !UNITY_SWITCH || UNITY_EDITOR
entrys = null; entrys = null;
return false; 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 || UNITY_EDITOR #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)
@ -430,8 +435,9 @@ public class AxiNSIO
} }
#endif #endif
public bool GetDirectoryEntrysFullRecursion(string path, out string[] entrys)
{ public bool GetDirectoryEntrysFullRecursion(string path, out string[] entrys)
{
#if UNITY_SWITCH #if UNITY_SWITCH
nn.fs.DirectoryHandle eHandle = new nn.fs.DirectoryHandle(); nn.fs.DirectoryHandle eHandle = new nn.fs.DirectoryHandle();
@ -462,15 +468,15 @@ public class AxiNSIO
entrys = temp.ToArray(); entrys = temp.ToArray();
return true; return true;
#else #else
entrys = default; entrys = null;
return false; return false;
#endif #endif
} }
public IEnumerable<string> EnumerateFiles(string path, string searchPattern) public IEnumerable<string> EnumerateFiles(string path, string searchPattern)
{ {
#if !UNITY_SWITCH || UNITY_EDITOR #if !UNITY_SWITCH || UNITY_EDITOR
yield break; yield break;
#else #else
// 将通配符转换为正则表达式(支持*和? // 将通配符转换为正则表达式(支持*和?
var regexPattern = "^" + var regexPattern = "^" +
@ -494,10 +500,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
@ -526,15 +532,15 @@ public class AxiNSIO
return CommitSave(); return CommitSave();
#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
@ -561,15 +567,15 @@ public class AxiNSIO
#endif #endif
return CommitSave(); return CommitSave();
#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;
} }
public bool DeletePathDirRecursively(string filename) public bool DeletePathDirRecursively(string filename)
{ {
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -596,19 +602,86 @@ public class AxiNSIO
#endif #endif
return CommitSave(); return CommitSave();
#endif #endif
} }
public AxiNSWait_DeletePathDirRecursively DeletePathDirRecursivelyAsync(string filename)
{
var wait = new AxiNSWait_DeletePathDirRecursively(filename);
AxiNS.instance.wait.AddWait(wait);
return wait;
}
#if UNITY_SWITCH /// <summary>
public AxiNSWait_DeletePathDirRecursively DeletePathDirRecursivelyAsync(string filename) /// 递归删除目录
{ /// </summary>
var wait = new AxiNSWait_DeletePathDirRecursively(filename); /// <param name="filename"></param>
AxiNS.instance.wait.AddWait(wait); /// <returns></returns>
return wait; public bool DeleteRecursivelyPathDir(string filename)
} {
#if !UNITY_SWITCH
return false;
#else
#if UNITY_SWITCH && !UNITY_EDITOR
// This next line prevents the user from quitting the game while saving.
// This is required for Nintendo Switch Guideline 0080
UnityEngine.Switch.Notification.EnterExitRequestHandlingSection();
#endif #endif
public bool RenameDir(string oldpath, string newpath) if (CheckPathNotFound(filename))
{ return false;
nn.Result result;
result = nn.fs.Directory.DeleteRecursively(filename);
if (result.IsSuccess() == false)
{
UnityEngine.Debug.LogError($"nn.fs.File.DeleteRecursively 失败 {filename} : result=>{result.GetErrorInfo()}");
return false;
}
#if UNITY_SWITCH && !UNITY_EDITOR
// End preventing the user from quitting the game while saving.
UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection();
#endif
return CommitSave();
#endif
}
/// <summary>
/// 递归删除情况
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public bool CleanRecursivelyPathDir(string filename)
{
#if !UNITY_SWITCH
return false;
#else
#if UNITY_SWITCH && !UNITY_EDITOR
// This next line prevents the user from quitting the game while saving.
// This is required for Nintendo Switch Guideline 0080
UnityEngine.Switch.Notification.EnterExitRequestHandlingSection();
#endif
if (CheckPathNotFound(filename))
return false;
nn.Result result;
result = nn.fs.Directory.CleanRecursively(filename);
if (result.IsSuccess() == false)
{
UnityEngine.Debug.LogError($"nn.fs.File.DeleteRecursively 失败 {filename} : result=>{result.GetErrorInfo()}");
return false;
}
#if UNITY_SWITCH && !UNITY_EDITOR
// End preventing the user from quitting the game while saving.
UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection();
#endif
return CommitSave();
#endif
}
public bool RenameDir(string oldpath, string newpath)
{
#if !UNITY_SWITCH #if !UNITY_SWITCH
return false; return false;
#else #else
@ -637,9 +710,9 @@ public class AxiNSIO
return CommitSave(); return CommitSave();
#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
@ -715,14 +788,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
@ -760,5 +833,5 @@ public class AxiNSIO
return true; // 其他挂载点需根据实际需求实现 return true; // 其他挂载点需根据实际需求实现
} }
#endif #endif
} }
} }

View File

@ -0,0 +1,41 @@
using System;
using UnityEngine;
public class AxiNSMono : MonoBehaviour
{
Action act;
float waittime;
float lastinvokeTime;
public static void SetInvoke(Action _act, int _waitsec)
{
GameObject gobj = GameObject.Find($"[{nameof(AxiNSMono)}]");
if (gobj == null)
{
gobj = new GameObject();
gobj.name = $"[{nameof(AxiNSMono)}]";
GameObject.DontDestroyOnLoad(gobj);
}
AxiNSMono com = gobj.GetComponent<AxiNSMono>();
if (com == null)
{
com = gobj.AddComponent<AxiNSMono>();
}
com.act = _act;
com.waittime = _waitsec;
}
public void OnEnable()
{
Debug.Log("AxiNSMono Enable");
}
public void Update()
{
if (Time.time - lastinvokeTime < waittime)
return;
lastinvokeTime = Time.time;
if (act != null)
act.Invoke();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 23d5745b8989af04d8a871b5c7b65d50
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -117,3 +117,18 @@ public class AxiNSWait_DeletePathDir : AxiNSWaitBase
result = AxiNS.instance.io.DeletePathDir(req.filePath); result = AxiNS.instance.io.DeletePathDir(req.filePath);
} }
} }
public class AxiNSWait_DeletePathDirRecursively : AxiNSWaitBase
{
S_NSWAIT_Path req;
public bool result;
public AxiNSWait_DeletePathDirRecursively(string filePath)
{
req = new S_NSWAIT_Path() { filePath = filePath };
}
public override void Invoke()
{
result = AxiNS.instance.io.DeletePathDirRecursively(req.filePath);
}
}

View File

@ -4,21 +4,30 @@ using System.Threading;
public class AxiNSWaitHandle public class AxiNSWaitHandle
{ {
static AutoResetEvent autoEvent = new AutoResetEvent(false);
static Thread waitThread = new Thread(Loop);
static bool bSingleInit = false;
static Queue<AxiNSWaitBase> m_QueueReady = new Queue<AxiNSWaitBase>(); static Queue<AxiNSWaitBase> m_QueueReady = new Queue<AxiNSWaitBase>();
static Queue<AxiNSWaitBase> m_QueueWork = new Queue<AxiNSWaitBase>(); static Queue<AxiNSWaitBase> m_QueueWork = new Queue<AxiNSWaitBase>();
public void AddWait(AxiNSWaitBase wait) public void AddWait(AxiNSWaitBase wait)
{ {
InitInternalThread();
lock (m_QueueReady) lock (m_QueueReady)
{ {
m_QueueReady.Enqueue(wait); m_QueueReady.Enqueue(wait);
} }
autoEvent.Set();
if (AxiNS.usedmultithreading)
{
InitInternalThread();
autoEvent.Set();
}
else
{
InitMonoInit();
}
} }
#region 线
static AutoResetEvent autoEvent = new AutoResetEvent(false);
static Thread waitThread = new Thread(Loop);
static bool bSingleInit = false;
static void InitInternalThread() static void InitInternalThread()
{ {
if (bSingleInit) return; if (bSingleInit) return;
@ -30,27 +39,43 @@ public class AxiNSWaitHandle
{ {
while (autoEvent.WaitOne()) while (autoEvent.WaitOne())
{ {
lock (m_QueueReady) Do();
}
}
#endregion
#region 线
static bool bMonoInit = false;
static void InitMonoInit()
{
if (bMonoInit) return;
AxiNSMono.SetInvoke(Do,15);
bMonoInit = true;
}
#endregion
static void Do()
{
lock (m_QueueReady)
{
while (m_QueueReady.Count > 0)
{ {
while (m_QueueReady.Count > 0) m_QueueWork.Enqueue(m_QueueReady.Dequeue());
{
m_QueueWork.Enqueue(m_QueueReady.Dequeue());
}
} }
while (m_QueueWork.Count > 0) }
while (m_QueueWork.Count > 0)
{
AxiNSWaitBase wait = m_QueueWork.Dequeue();
try
{ {
AxiNSWaitBase wait = m_QueueWork.Dequeue(); wait.Invoke();
try
{
wait.Invoke();
}
catch (Exception ex)
{
wait.errmsg = ex.ToString();
UnityEngine.Debug.Log(ex.ToString());
}
wait.SetDone();
} }
catch (Exception ex)
{
wait.errmsg = ex.ToString();
UnityEngine.Debug.Log(ex.ToString());
}
wait.SetDone();
} }
} }
} }