-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathSimpleITKManager.cs
More file actions
183 lines (161 loc) · 7.9 KB
/
Copy pathSimpleITKManager.cs
File metadata and controls
183 lines (161 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using UnityEditor;
using UnityEngine;
using System.IO.Compression;
namespace UnityVolumeRendering
{
/// <summary>
/// Manager for the SimpleITK integration.
/// Since SimpleITK is a native library that requires binaries to be built for your target platform,
/// SimpleITK will be disabled by default and can be enabled through this class.
/// The binaries will be downloaded automatically.
/// </summary>
public class SimpleITKManager
{
private static string SimpleITKDefinition = "UVR_USE_SIMPLEITK";
public static bool IsSITKEnabled()
{
HashSet<string> defines = new HashSet<string>(PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone).Split(';'));
return defines.Contains(SimpleITKDefinition);
}
public static void EnableSITK(bool enable)
{
BuildTarget activeTarget = EditorUserBuildSettings.activeBuildTarget;
BuildTargetGroup activeGroup = BuildPipeline.GetBuildTargetGroup(activeTarget);
if (enable && activeGroup != BuildTargetGroup.Standalone
&& !EditorUtility.DisplayDialog("Build target does not support SimpleITK.",
$"SimpleITK is only supported in standalone builds and editor, and will not work on your selected build target ({activeTarget.ToString()}).\n"
+ "Enable SimpleITK for standalone (Windows, Linux, MacOS) and editor?", "Yes", "No"))
{
return;
}
if (!HasDownloadedBinaries())
{
EditorUtility.DisplayDialog("Missing SimpleITK binaries", "You need to download the SimpleITK binaries before you can enable SimpleITK.", "Ok");
return;
}
// Enable the UVR_USE_SIMPLEITK preprocessor definition for standalone target
List<BuildTargetGroup> buildTargetGroups = new List<BuildTargetGroup> (){ BuildTargetGroup.Standalone };
foreach (BuildTargetGroup group in buildTargetGroups)
{
List<string> defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').ToList();
defines.Remove(SimpleITKDefinition);
if (enable)
defines.Add(SimpleITKDefinition);
PlayerSettings.SetScriptingDefineSymbolsForGroup(group, String.Join(";", defines));
}
// Save project and recompile scripts
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
#if UNITY_2019_3_OR_NEWER
UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
#endif
}
public static bool HasDownloadedBinaries()
{
string binDir = GetBinaryDirectoryPath();
return Directory.Exists(binDir) && Directory.GetFiles(binDir).Length > 0; // TODO: Check actual files?
}
public static void DownloadBinaries()
{
string extractDirPath = GetBinaryDirectoryPath();
string zipPath = Path.Combine(Directory.GetParent(extractDirPath).FullName, "SimpleITK.zip");
if (HasDownloadedBinaries())
{
if (!EditorUtility.DisplayDialog("Download SimpleITK binaries", "SimpleITK has already been downloaded. Do you want to delete it and download again?", "Yes", "No"))
{
return;
}
}
EditorUtility.DisplayProgressBar("Downloading SimpleITK", "Downloading SimpleITK binaries.", 0);
// Downlaod binaries zip
using (var client = new WebClient())
{
#if UNITY_EDITOR_WIN
string downloadURL = "https://github.com/SimpleITK/SimpleITK/releases/download/v2.2.0/SimpleITK-2.2.0-CSharp-win64-x64.zip";
#elif UNITY_EDITOR_LINUX
string downloadURL = "https://github.com/SimpleITK/SimpleITK/releases/download/v2.2.0/SimpleITK-2.2.0-CSharp-linux.zip";
#else
string downloadURL = "https://github.com/SimpleITK/SimpleITK/releases/download/v2.2.0/SimpleITK-2.2.0-CSharp-macosx-10.9-anycpu.zip";
#endif
client.DownloadFile(downloadURL, zipPath);
EditorUtility.DisplayProgressBar("Downloading SimpleITK", "Extracting SimpleITK.", 70);
if (!File.Exists(zipPath))
{
Debug.Log(zipPath);
EditorUtility.DisplayDialog("Error downloadig SimpleITK binaries.", "Failed to download SimpleITK binaries. Please check your internet connection.", "Close");
Debug.Log($"Failed to download SimpleITK binaries. You can also try to manually download from {downloadURL} and extract it to some folder inside the Assets folder.");
return;
}
try
{
ExtractZip(zipPath, extractDirPath);
}
catch (Exception ex)
{
string errorString = $"Extracting binaries failed with error: {ex.Message}\n"
+ $"Please try downloading the zip from: {downloadURL}\nAnd extract it somewhere in the Assets folder.\n\n"
+ "The download URL can be copied from the error log (console).";
Debug.LogError(ex.ToString());
Debug.LogError(errorString);
EditorUtility.DisplayDialog("Failed to extract binaries.", errorString, "Close");
}
}
File.Delete(zipPath);
EditorUtility.ClearProgressBar();
}
private static void ExtractZip(string zipPath, string extractDirPath)
{
// Extract zip
using (FileStream zipStream = new FileStream(zipPath, FileMode.Open))
{
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update))
{
if (!Directory.Exists(extractDirPath))
Directory.CreateDirectory(extractDirPath);
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.Name != "" && !entry.Name.EndsWith("/"))
{
string destFilePath = Path.Combine(extractDirPath, entry.Name);
//TextAsset destAsset = new TextAsset("abc");
//AssetDatabase.CreateAsset(destAsset, extractDirRelPath + "/" + entry.Name);
Stream inStream = entry.Open();
using (Stream outStream = File.OpenWrite(destFilePath))
{
inStream.CopyTo(outStream);
}
}
}
}
}
}
private static string GetPluginRootPath()
{
// Try UPM package resolution first
var packageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(SimpleITKManager).Assembly);
if (packageInfo != null)
{
return packageInfo.resolvedPath;
}
// Fallback: search for magic file in Assets (for .unitypackage import).
// The file is at the plugin root, so its directory is the plugin root path.
foreach (string file in Directory.EnumerateFiles(Application.dataPath, "*.*", SearchOption.AllDirectories))
{
if (Path.GetFileName(file) == "DONOTREMOVE-PathSearchFile.txt")
{
return Path.GetDirectoryName(file);
}
}
return Application.dataPath;
}
private static string GetBinaryDirectoryPath()
{
return Path.Combine(GetPluginRootPath(), "ThirdParty", "SimpleITK");
}
}
}