diff --git a/Editor/XericLibraryEditor.dll b/Editor/XericLibraryEditor.dll index d261cc8..ab20fe4 100644 Binary files a/Editor/XericLibraryEditor.dll and b/Editor/XericLibraryEditor.dll differ diff --git a/Runtime/Lrss3.Deconstruction.asmdef b/Runtime/Lrss3.Deconstruction.asmdef index ab246f8..225fd46 100644 --- a/Runtime/Lrss3.Deconstruction.asmdef +++ b/Runtime/Lrss3.Deconstruction.asmdef @@ -11,6 +11,12 @@ "precompiledReferences": [], "autoReferenced": true, "defineConstraints": [], - "versionDefines": [], + "versionDefines": [ + { + "name": "com.lrss3.deconstruction", + "expression": "", + "define": "XERICLIBRARY" + } + ], "noEngineReferences": false } \ No newline at end of file diff --git a/Runtime/Helper/MacroEventTrigger.cs b/Runtime/MacroLibrary/MacroEventTrigger.cs similarity index 100% rename from Runtime/Helper/MacroEventTrigger.cs rename to Runtime/MacroLibrary/MacroEventTrigger.cs diff --git a/Runtime/Helper/MacroEventTrigger.cs.meta b/Runtime/MacroLibrary/MacroEventTrigger.cs.meta similarity index 100% rename from Runtime/Helper/MacroEventTrigger.cs.meta rename to Runtime/MacroLibrary/MacroEventTrigger.cs.meta diff --git a/Runtime/Helper/MacroLayout.cs b/Runtime/MacroLibrary/MacroLayout.cs similarity index 100% rename from Runtime/Helper/MacroLayout.cs rename to Runtime/MacroLibrary/MacroLayout.cs diff --git a/Runtime/Helper/MacroLayout.cs.meta b/Runtime/MacroLibrary/MacroLayout.cs.meta similarity index 100% rename from Runtime/Helper/MacroLayout.cs.meta rename to Runtime/MacroLibrary/MacroLayout.cs.meta diff --git a/Runtime/MacroLibrary/MacroWebGLLib.cs b/Runtime/MacroLibrary/MacroWebGLLib.cs new file mode 100644 index 0000000..6b5398e --- /dev/null +++ b/Runtime/MacroLibrary/MacroWebGLLib.cs @@ -0,0 +1,132 @@ +#if UNITY_WEBGL +using System; +using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; +using UnityEngine; +using XericLibrary.Runtime.MacroLibrary; +using XericLibrary.Runtime.Type; +using XericLibrary.Runtime.Type.Base; + +namespace XericLibrary.Runtime.MacroLibrary +{ + /// + /// Web 平台文件句柄。 + /// + public class WebGLHandle : CrossPlatformFileHandle + { + public override string PlatformName => "WebGL"; + public static WebGLHandle handle = new WebGLHandle(); + public static float GlobalTimeout = 10f; + public float Timeout = -1f; + + private float timeout => Timeout >= 0 ? Timeout : GlobalTimeout; + + public WebGLHandle() : base(RuntimePlatform.WebGLPlayer) + { + if (handle == null) + handle = this; + } + + [DllImport("__Internal")] + private static extern void Download(string base64str, string fileName); + + //[DllImport("__Internal")] + //private static extern void OpenFileDialog(string gameObjectName, string callbackMethodName); + + //[DllImport("__Internal")] + //private static extern void OpenFileDialogBinary(string gameObjectName, string callbackMethodName); + + public override bool ReadTextFromFile(string absolutePathname, out string content) + { + var waitFlag = false; + var result = string.Empty; + + SingleMonoBase.GlobalInstance.DirectlyLoadFile(absolutePathname, s => result = s, s => + { + XericLogger.CCError($"WebGl文件同步加载失败:{s}"); + }, () => { waitFlag = true; }); + + // 等待文件加载完成 + while (!waitFlag) + { + // 让出控制权,避免完全阻塞 + Debug.LogWarning("不建议在WebGl端使用同步文件读取方式,当前已让渡主线程控制权"); + Thread.Sleep(0); + } + content = result; + return waitFlag; + } + + public override bool WriteTextIntoFile(string absolutePathname, string content) + { + try + { + Download(content, absolutePathname); + return true; + } + catch (Exception e) + { + Debug.LogError($"无法下载文件{absolutePathname}:{e.Message}"); + return false; + } + } + + public override async Task AsyncReadTextFromFile(string absolutePathname, Action complete, Action loadError) + { + var waitFlag = false; + var result = false; + SingleMonoBase.GlobalInstance.DirectlyLoadFile(absolutePathname, + s => + { + result = true; + complete?.Invoke(s); + }, loadError, () => { waitFlag = true; }); + await MacroAsync.WaitUntil(() => waitFlag, () => "WebGl文件读取失败", timeout); + return result; + } + + public override async Task AsyncWriteTextIntoFile(string absolutePathname, string content, Action complete, Action loadError) + { + await Task.Yield(); + if (WriteTextIntoFile(absolutePathname, content)) + { + complete?.Invoke(); + } + else + { + loadError?.Invoke("WebGL文件写入失败"); + XericLogger.CCError("WebGL文件写入失败"); + } + await Task.CompletedTask; + return false; + } + + public override string CombinePath(string pathA, string pathB) + { + try + { + if (!pathA.EndsWith("/")) // 确保路径以斜杠结尾 + pathA += "/"; + var uri = new Uri(new Uri(pathA, UriKind.Absolute), new Uri(pathB, UriKind.RelativeOrAbsolute)); + /* 不能使用uri自己的 + * 比如 uri.IsAbsoluteUri ? uri.AbsolutePath : uri.LocalPath; + * 在这种情况时: + * http://192.168.0.39:8080/DGJ/StreamingAssets/ + UriConfig.json + * 结果是 => /DGJ/StreamingAssets/UriConfig.json 用是AbsolutePath + * 所以要么直接A + B + * 要么直接Tostring。 + */ + Debug.Log($"Web路径拼接处理结果浏览:{pathA}\n{pathB}\n{uri}\n"); + return uri.ToString(); + } + catch (UriFormatException ex) + { + // 处理URI格式错误 + XericLogger.CCError($"Web路径拼接失败: {ex.Message}"); + return string.Empty; + } + } + } +} +#endif diff --git a/Runtime/MacroLibrary/MacroWebGLLib.cs.meta b/Runtime/MacroLibrary/MacroWebGLLib.cs.meta new file mode 100644 index 0000000..141436c --- /dev/null +++ b/Runtime/MacroLibrary/MacroWebGLLib.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2137fae15766b1f40a4e7c94acb5cb70 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/XericLibrary.dll b/Runtime/XericLibrary.dll index 4ea17f9..55b7bb9 100644 Binary files a/Runtime/XericLibrary.dll and b/Runtime/XericLibrary.dll differ