// // DllMgr.cs // // Author: // JasonXuDeveloper(傑) // // Copyright (c) 2020 JEngine // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.IO; using UnityEngine; using System.Threading.Tasks; namespace JEngine.Core { public static partial class DllMgr { /// /// Get DLL path in the editor (located at hidden file with .dll extension) /// /// /// public static string GetDllInEditorPath(string name) { Tools.EnsureEndWith(ref name, ConstMgr.DLLExtension); return Path.Combine(ConstMgr.DLLSourceFolder, name); } /// /// Get DLL path in the runtime (located in HotUpdateResources/Main/DLL) /// /// /// public static string GetDllInRuntimePath(string name) { Tools.EnsureEndWith(ref name, ConstMgr.BytesExtension); return Path.Combine(ConstMgr.DLLBytesFolder, name); } /// /// Get PDB path in the editor (located at hidden file with .pdb extension) /// /// /// public static string GetPdbInEditorPath(string name) { Tools.EnsureEndWith(ref name, ConstMgr.PdbExtension); return Path.Combine(ConstMgr.PdbSourceFolder, name); } /// /// Get PDB path in the runtime (located in HotUpdateResources/Main/DLL) /// /// /// public static string GetPdbInRuntimePath(string name) { Tools.EnsureEndWith(ref name, ConstMgr.PdbExtension); Tools.EnsureEndWith(ref name, ConstMgr.BytesExtension); return Path.Combine(ConstMgr.PdbBytesFolder, name); } /// /// Get DLL binary data /// /// dll file name with extension /// is editor mode /// /// public static async Task GetDllBytes(string name, bool editor = false) { string path; if (editor) { path = GetDllInEditorPath(name); //判断有没有dll if (File.Exists(path)) { return FileMgr.FileToBytes(path); } throw new FileNotFoundException($"DLL not found in: {path}"); } path = GetDllInRuntimePath(name); var dllFile = await AssetMgr.LoadAsync(path); if (dllFile == null) { throw new FileNotFoundException($"DLL not found in: {path}"); } return dllFile.bytes; } /// /// Get PDB binary data /// /// /// /// /// public static async Task GetPdbBytes(string name, bool editor = true) { if (editor) { //DLL文件 var dllPath = GetDllInEditorPath(name); //PDB文件 string path = GetPdbInEditorPath(name); //查看是否有PDB文件并且是最新的 if (File.Exists(path) && (File.GetLastWriteTime(dllPath) - File.GetLastWriteTime(path)).Seconds < 30) { return FileMgr.FileToBytes(path); } throw new InvalidOperationException("Pdb is invalid"); } var pdbPath = GetPdbInRuntimePath(name); var pdbFile = await AssetMgr.LoadAsync(pdbPath); if (pdbFile == null) { throw new FileNotFoundException($"Pdb not found in: {pdbPath}"); } return pdbFile.bytes; } /// /// Simulate encryption on plaintext /// /// plaintext data /// key /// public static void SimulateEncryption(ref byte[] source, string key) { if (key.Length != 16) { throw new InvalidOperationException("key to encrypt has to be length of 16"); } source = CryptoMgr.AesEncrypt(source, key); } } }