|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net; |
| 6 | +using UnityEditor; |
| 7 | +using UnityEngine; |
| 8 | +using System.IO.Compression; |
| 9 | + |
| 10 | +namespace UnityVolumeRendering |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Manager for the SimpleITK integration. |
| 14 | + /// Since SimpleITK is a native library that requires binaries to be built for your target platform, |
| 15 | + /// SimpleITK will be disabled by default and can be enabled through this class. |
| 16 | + /// The binaries will be downloaded automatically. |
| 17 | + /// </summary> |
| 18 | + public class SimpleITKManager |
| 19 | + { |
| 20 | + private static string SimpleITKDefinition = "UVR_USE_SIMPLEITK"; |
| 21 | + |
| 22 | + public static bool IsSITKEnabled() |
| 23 | + { |
| 24 | + BuildTarget target = EditorUserBuildSettings.activeBuildTarget; |
| 25 | + BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target); |
| 26 | + |
| 27 | + HashSet<string> defines = new HashSet<string>(PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';')); |
| 28 | + return defines.Contains(SimpleITKDefinition); |
| 29 | + } |
| 30 | + |
| 31 | + public static void EnableSITK(bool enable) |
| 32 | + { |
| 33 | + if (!HasDownloadedBinaries()) |
| 34 | + { |
| 35 | + EditorUtility.DisplayDialog("Missing SimpleITK binaries", "You need to download the SimpleITK binaries before you can enable SimpleITK.", "Ok"); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Enable the UVR_USE_SIMPLEITK preprocessor definition for standalone target |
| 40 | + List<BuildTargetGroup> buildTargetGroups = new List<BuildTargetGroup> (){ BuildTargetGroup.Standalone }; |
| 41 | + foreach (BuildTargetGroup group in buildTargetGroups) |
| 42 | + { |
| 43 | + List<string> defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').ToList(); |
| 44 | + defines.Remove(SimpleITKDefinition); |
| 45 | + if (enable) |
| 46 | + defines.Add(SimpleITKDefinition); |
| 47 | + PlayerSettings.SetScriptingDefineSymbolsForGroup(group, String.Join(";", defines)); |
| 48 | + } |
| 49 | + |
| 50 | + // Save project and recompile scripts |
| 51 | + AssetDatabase.SaveAssets(); |
| 52 | + AssetDatabase.Refresh(); |
| 53 | +#if UNITY_2019_3_OR_NEWER |
| 54 | + UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation(); |
| 55 | +#endif |
| 56 | + } |
| 57 | + |
| 58 | + public static bool HasDownloadedBinaries() |
| 59 | + { |
| 60 | + string binDir = GetBinaryDirectoryPath(); |
| 61 | + return Directory.Exists(binDir) && Directory.GetFiles(binDir).Length > 0; // TODO: Check actual files? |
| 62 | + } |
| 63 | + |
| 64 | + public static void DownloadBinaries() |
| 65 | + { |
| 66 | + string extractDirPath = GetBinaryDirectoryPath(); |
| 67 | + string zipPath = Path.Combine(Directory.GetParent(extractDirPath).FullName, "SimpleITK.zip"); |
| 68 | + if (HasDownloadedBinaries()) |
| 69 | + { |
| 70 | + if (!EditorUtility.DisplayDialog("Download SimpleITK binaries", "SimpleITK has already been downloaded. Do you want to delete it and download again?", "Yes", "No")) |
| 71 | + { |
| 72 | + return; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + EditorUtility.DisplayProgressBar("Downloading SimpleITK", "Downloading SimpleITK binaries.", 0); |
| 77 | + |
| 78 | + // Downlaod binaries zip |
| 79 | + using (var client = new WebClient()) |
| 80 | + { |
| 81 | + string downloadURL = "https://sourceforge.net/projects/simpleitk/files/SimpleITK/1.2.4/CSharp/SimpleITK-1.2.4-CSharp-win64-x64.zip/download"; |
| 82 | + client.DownloadFile(downloadURL, zipPath); |
| 83 | + |
| 84 | + EditorUtility.DisplayProgressBar("Downloading SimpleITK", "Downloading SimpleITK binaries.", 70); |
| 85 | + |
| 86 | + if (!File.Exists(zipPath)) |
| 87 | + { |
| 88 | + Debug.Log(zipPath); |
| 89 | + EditorUtility.DisplayDialog("Error downloadig SimpleITK binaries.", "Failed to download SimpleITK binaries. Please check your internet connection.", "Close"); |
| 90 | + 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."); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + try |
| 95 | + { |
| 96 | + ExtractZip(zipPath, extractDirPath); |
| 97 | + } |
| 98 | + catch (Exception ex) |
| 99 | + { |
| 100 | + string errorString = $"Extracting binaries failed with error: {ex.Message}\n" |
| 101 | + + $"Please try downloading the zip from: {downloadURL}\nAnd extract it somewhere in the Assets folder.\n\n" |
| 102 | + + "The download URL can be copied from the error log (console)."; |
| 103 | + Debug.LogError(ex.ToString()); |
| 104 | + Debug.LogError(errorString); |
| 105 | + EditorUtility.DisplayDialog("Failed to extract binaries.", errorString, "Close"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + File.Delete(zipPath); |
| 110 | + |
| 111 | + EditorUtility.ClearProgressBar(); |
| 112 | + } |
| 113 | + |
| 114 | + private static void ExtractZip(string zipPath, string extractDirPath) |
| 115 | + { |
| 116 | + // Extract zip |
| 117 | + using (FileStream zipStream = new FileStream(zipPath, FileMode.Open)) |
| 118 | + { |
| 119 | + using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update)) |
| 120 | + { |
| 121 | + if (!Directory.Exists(extractDirPath)) |
| 122 | + Directory.CreateDirectory(extractDirPath); |
| 123 | + |
| 124 | + foreach (ZipArchiveEntry entry in archive.Entries) |
| 125 | + { |
| 126 | + if (entry.Name != "" && !entry.Name.EndsWith("/")) |
| 127 | + { |
| 128 | + string destFilePath = Path.Combine(extractDirPath, entry.Name); |
| 129 | + //TextAsset destAsset = new TextAsset("abc"); |
| 130 | + //AssetDatabase.CreateAsset(destAsset, extractDirRelPath + "/" + entry.Name); |
| 131 | + Stream inStream = entry.Open(); |
| 132 | + |
| 133 | + using (Stream outStream = File.OpenWrite(destFilePath)) |
| 134 | + { |
| 135 | + inStream.CopyTo(outStream); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static string GetBinaryDirectoryPath() |
| 144 | + { |
| 145 | + string dataPath = Application.dataPath; |
| 146 | + foreach (string file in Directory.EnumerateFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)) |
| 147 | + { |
| 148 | + // Search for magic file stored in Assets directory. |
| 149 | + // This is necessary for cases where the UVR plugin is stored in a subfolder (thatæs the case for the asset store version) |
| 150 | + if (Path.GetFileName(file) == "DONOTREMOVE-PathSearchFile.txt") |
| 151 | + { |
| 152 | + dataPath = Path.GetDirectoryName(file); |
| 153 | + } |
| 154 | + } |
| 155 | + return Path.Combine(dataPath, "3rdparty", "SimpleITK"); // TODO: What is UVR is in a subfolder? |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments