Skip to content
Merged
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*.meta
scripts/LICENSE.txt
*.sln
.vs
Expand Down
7 changes: 7 additions & 0 deletions LICENSE.txt.meta

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

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ Tool created mainly to solve old problem with reloading [native plugins](https:/

## Installation

1. Download and add unity package from [releases](https://github.com/MCpiroman/UnityNativeTool/releases), or clone this repo to your project (note that if you don't use the prebuilt package and you're willing to use [low level interface callbacks](https://docs.unity3d.com/Manual/NativePluginInterface.html), you'll need to compile the stub plugin yourself form `stubLluiPlugin.c`).
1. Download and add unity package from [releases](https://github.com/MCpiroman/UnityNativeTool/releases), or clone this repo to your project

- Clone it into the `<Project Root>/Packages` folder to use it as a local [embedded package](https://docs.unity3d.com/Manual/upm-embed.html) with [upm](https://docs.unity3d.com/Packages/com.unity.package-manager-ui@1.8/manual/index.html).

- Note that if you don't use the prebuilt package and you're willing to use [low level interface callbacks](https://docs.unity3d.com/Manual/NativePluginInterface.html), you'll need to compile the stub plugin yourself from `stubLluiPlugin.c`.

2. In project settings, set _Api Compatibility Level_ to .NET 4.x or above.
Edit > Project Settings > Player > Other Settings > Api Compatibility Level
Expand Down
7 changes: 7 additions & 0 deletions README.md.meta

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

16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "com.mcpiroman.unity-native-tool",
"displayName": "Unity Native Tool",
"version": "7.1.0",
"description": "Tool for unloading native DLLs in the editor. \nhttps://github.com/mcpiroman/UnityNativeTool",
"keywords": [
"native",
"c++",
"dll"
],
"category": "Unity",
"repository": {
"type": "git",
"url": "https://github.com/mcpiroman/UnityNativeTool.git"
}
}
7 changes: 7 additions & 0 deletions package.json.meta

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

8 changes: 8 additions & 0 deletions scripts.meta

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

11 changes: 11 additions & 0 deletions scripts/Attributes.cs.meta

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

11 changes: 11 additions & 0 deletions scripts/Detour.cs.meta

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

11 changes: 11 additions & 0 deletions scripts/DllLoadException.cs.meta

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

11 changes: 11 additions & 0 deletions scripts/DllManipulator.ReflectionData.cs.meta

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

66 changes: 38 additions & 28 deletions scripts/DllManipulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public partial class DllManipulator
public const string DLL_PATH_PATTERN_ASSETS_MACRO = "{assets}";
public const string DLL_PATH_PATTERN_PROJECT_MACRO = "{proj}";
private const string CRASH_FILE_NAME_PREFIX = "unityNativeCrash_";
public static readonly string[] DEFAULT_ASSEMBLY_NAMES = {"Assembly-CSharp"
#if UNITY_EDITOR
, "Assembly-CSharp-Editor"
#endif
};
public static readonly string[] INTERNAL_ASSEMBLY_NAMES = {"mcpiroman.UnityNativeTool"
#if UNITY_EDITOR
, "mcpiroman.UnityNativeTool.Editor"
#endif
};
public static readonly string[] IGNORED_ASSEMBLY_PREFIXES = { "UnityEngine.", "UnityEditor.", "Unity.", "com.unity.", "Mono." , "nunit."};


public static DllManipulatorOptions Options { get; set; }
private static int _unityMainThreadId;
Expand Down Expand Up @@ -48,20 +60,18 @@ internal static void Initialize(int unityMainThreadId, string assetsPath)

LowLevelPluginManager.ResetStubPlugin();

Assembly[] assemblies;
if (Options.assemblyPaths.Length == 0)
{
assemblies = new[] { Assembly.GetExecutingAssembly() };
}
else
IEnumerable<string> assemblyPathsTemp = Options.assemblyNames;
if (!assemblyPathsTemp.Any())
assemblyPathsTemp = DEFAULT_ASSEMBLY_NAMES;

assemblyPathsTemp = assemblyPathsTemp.Concat(INTERNAL_ASSEMBLY_NAMES);

var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var assemblies = allAssemblies.Where(a => !a.IsDynamic && assemblyPathsTemp.Any(p => p == Path.GetFileNameWithoutExtension(a.Location))).ToArray();
var missingAssemblies = assemblyPathsTemp.Except(assemblies.Select(a => Path.GetFileNameWithoutExtension(a.Location)));
foreach (var assembly in missingAssemblies.Except(DEFAULT_ASSEMBLY_NAMES))
{
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
assemblies = allAssemblies.Where(a => !a.IsDynamic && Options.assemblyPaths.Any(p => p == PathUtils.NormallizeSystemAssemblyPath(a.Location))).ToArray();
var missingAssemblies = Options.assemblyPaths.Except(assemblies.Select(a => PathUtils.NormallizeSystemAssemblyPath(a.Location)));
foreach (var assemblyPath in missingAssemblies)
{
Debug.LogError($"Could not find assembly at path {assemblyPath}");
}
Debug.LogError($"Could not find assembly: {assembly}");
}

foreach (var assembly in assemblies)
Expand Down Expand Up @@ -644,34 +654,34 @@ private static void WriteNativeCrashLog(NativeFunction nativeFunction, object[]

private static IntPtr SysLoadDll(string filepath)
{
#if UNITY_STANDALONE_WIN
return PInvokes_Windows.LoadLibrary(filepath);
#elif UNITY_STANDALONE_LINUX
#if UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX
return PInvokes_Linux.dlopen(filepath, (int)Options.posixDlopenFlags);
#elif UNITY_STANDALONE_OSX
#elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
return PInvokes_Osx.dlopen(filepath, (int)Options.posixDlopenFlags);
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
return PInvokes_Windows.LoadLibrary(filepath);
#endif
}

private static bool SysUnloadDll(IntPtr libHandle)
{
#if UNITY_STANDALONE_WIN
return PInvokes_Windows.FreeLibrary(libHandle);
#elif UNITY_STANDALONE_LINUX
#if UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX
return PInvokes_Linux.dlclose(libHandle) == 0;
#elif UNITY_STANDALONE_OSX
#elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
return PInvokes_Osx.dlclose(libHandle) == 0;
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
return PInvokes_Windows.FreeLibrary(libHandle);
#endif
}

private static IntPtr SysGetDllProcAddress(IntPtr libHandle, string symbol)
{
#if UNITY_STANDALONE_WIN
return PInvokes_Windows.GetProcAddress(libHandle, symbol);
#elif UNITY_STANDALONE_LINUX
#if UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX
return PInvokes_Linux.dlsym(libHandle, symbol);
#elif UNITY_STANDALONE_OSX
#elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
return PInvokes_Osx.dlsym(libHandle, symbol);
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
return PInvokes_Windows.GetProcAddress(libHandle, symbol);
#endif
}
}
Expand All @@ -680,7 +690,7 @@ private static IntPtr SysGetDllProcAddress(IntPtr libHandle, string symbol)
public class DllManipulatorOptions
{
public string dllPathPattern;
public string[] assemblyPaths; //empty means only executing assembly
public List<string> assemblyNames; // empty means only default assemblies
public DllLoadingMode loadingMode;
public PosixDlopenFlags posixDlopenFlags;
public bool threadSafe;
Expand All @@ -694,7 +704,7 @@ public class DllManipulatorOptions
public DllManipulatorOptions CloneTo(DllManipulatorOptions other)
{
other.dllPathPattern = dllPathPattern;
other.assemblyPaths = (string[]) assemblyPaths.Clone();
other.assemblyNames = assemblyNames.Select(item => (string)item.Clone()).ToList();
other.loadingMode = loadingMode;
other.posixDlopenFlags = posixDlopenFlags;
other.threadSafe = threadSafe;
Expand All @@ -710,7 +720,7 @@ public DllManipulatorOptions CloneTo(DllManipulatorOptions other)

public bool Equals(DllManipulatorOptions other)
{
return other.dllPathPattern == dllPathPattern && other.assemblyPaths.SequenceEqual(assemblyPaths) &&
return other.dllPathPattern == dllPathPattern && other.assemblyNames.SequenceEqual(assemblyNames) &&
other.loadingMode == loadingMode && other.posixDlopenFlags == posixDlopenFlags &&
other.threadSafe == threadSafe && other.enableCrashLogs == enableCrashLogs &&
other.crashLogsDir == crashLogsDir && other.crashLogsStackTrace == crashLogsStackTrace &&
Expand Down
11 changes: 11 additions & 0 deletions scripts/DllManipulator.cs.meta

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

10 changes: 5 additions & 5 deletions scripts/DllManipulatorScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class DllManipulatorScript : MonoBehaviour
public DllManipulatorOptions Options = new DllManipulatorOptions()
{
dllPathPattern =
#if UNITY_STANDALONE_WIN
"{assets}/Plugins/__{name}.dll",
#elif UNITY_STANDALONE_LINUX
#if UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX
"{assets}/Plugins/__{name}.so",
#elif UNITY_STANDALONE_OSX
#elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
"{assets}/Plugins/__{name}.dylib",
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
"{assets}/Plugins/__{name}.dll",
#endif
assemblyPaths = new string[0],
assemblyNames = new List<string>(),
loadingMode = DllLoadingMode.Lazy,
posixDlopenFlags = PosixDlopenFlags.Lazy,
threadSafe = false,
Expand Down
11 changes: 11 additions & 0 deletions scripts/DllManipulatorScript.cs.meta

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

8 changes: 8 additions & 0 deletions scripts/Editor.meta

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

Loading