Tanoda
UnityFeatureHelper.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.IO;
5using System.Linq;
6using System.Reflection;
7using UnityEngine;
8using UnityEngine.SceneManagement;
9
11{
12 public static class UnityFeatureHelper
13 {
14 private static readonly Type _sceneManager = Type.GetType("UnityEngine.SceneManagement.SceneManager, UnityEngine", false);
15 private static readonly Type _scene = Type.GetType("UnityEngine.SceneManagement.Scene, UnityEngine", false);
16 private static readonly Type _vectrosity = Type.GetType("Vectrosity.VectorObject2D, Vectrosity", false);
17
18 static UnityFeatureHelper()
19 {
20 SupportsScenes = _scene != null && _sceneManager != null;
21 if (!SupportsScenes)
22 RuntimeUnityEditorCore.Logger.Log(LogLevel.Warning, "UnityEngine.SceneManager and/or UnityEngine.SceneManagement.Scene are not available, some features will be disabled");
23
24 // Todo detect properly?
25 SupportsCursorIndex = SupportsScenes;
26 if (!SupportsCursorIndex)
27 RuntimeUnityEditorCore.Logger.Log(LogLevel.Warning, "TextEditor.cursorIndex is not available, some features will be disabled");
28
29 SupportsVectrosity = _vectrosity != null;
30 if (!SupportsVectrosity)
31 RuntimeUnityEditorCore.Logger.Log(LogLevel.Warning, "Vectrosity.dll is not available, drawing gizmos will be disabled");
32 }
33
34 public static bool SupportsScenes { get; private set; }
35 public static bool SupportsCursorIndex { get; private set; }
36 public static bool SupportsVectrosity { get; private set; }
37
38 public static IEnumerable<GameObject> GetSceneGameObjects()
39 {
40 try
41 {
42 return GetSceneGameObjectsInternal();
43 }
44 catch (Exception)
45 {
46 SupportsScenes = false;
47 return Enumerable.Empty<GameObject>();
48 }
49 }
50
51 public static GameObject[] GetSceneGameObjectsInternal()
52 {
53 return SceneManager.GetActiveScene().GetRootGameObjects();
54 }
55
56 public static void OpenLog()
57 {
58 // Generated in most versions unless disabled
59 if (TryOpen(Path.Combine(Application.dataPath, "output_log.txt"))) return;
60
61 // Redirected by preloader to game root
62 if (TryOpen(Path.Combine(Path.GetDirectoryName(Application.dataPath) ?? "", "output_log.txt"))) return;
63
64 // Available since 2018.3
65 var prop = typeof(Application).GetProperty("consoleLogPath", BindingFlags.Static | BindingFlags.Public);
66 if (prop != null)
67 {
68 var path = prop.GetValue(null, null) as string;
69 if (TryOpen(path)) return;
70 }
71
72 if (Directory.Exists(Application.persistentDataPath))
73 {
74 var file = Directory.GetFiles(Application.persistentDataPath, "output_log.txt", SearchOption.AllDirectories).FirstOrDefault();
75 if (TryOpen(file)) return;
76 }
77
78 // Fall back to more aggresive brute search
79 var rootDir = Directory.GetParent(Application.dataPath);
80 if (rootDir.Exists)
81 {
82 // BepInEx 5.x log file
83 var result = rootDir.GetFiles("LogOutput.log", SearchOption.AllDirectories).FirstOrDefault();
84 if (result == null)
85 result = rootDir.GetFiles("output_log.txt", SearchOption.AllDirectories).FirstOrDefault();
86
87 if (result != null && TryOpen(result.FullName)) return;
88 }
89
90 RuntimeUnityEditorCore.Logger.Log(LogLevel.Message | LogLevel.Error, "No log files were found");
91 }
92
93 private static bool TryOpen(string path)
94 {
95 if (!File.Exists(path)) return false;
96 try
97 {
98 Process.Start(path);
99 return true;
100 }
101 catch
102 {
103 return false;
104 }
105 }
106
107 public static Texture2D LoadTexture(byte[] texData)
108 {
109 var tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
110
111 // Around Unity 2018 the LoadImage and other export/import methods got moved from Texture2D to extension methods
112 var loadMethod = typeof(Texture2D).GetMethod("LoadImage", new[] { typeof(byte[]) });
113 if (loadMethod != null)
114 {
115 loadMethod.Invoke(tex, new object[] { texData });
116 }
117 else
118 {
119 var converter = Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule");
120 if (converter == null) throw new ArgumentNullException();
121 var converterMethod = converter.GetMethod("LoadImage", new[] { typeof(Texture2D), typeof(byte[]) });
122 if (converterMethod == null) throw new ArgumentNullException();
123 converterMethod.Invoke(null, new object[] { tex, texData });
124 }
125
126 return tex;
127 }
128 }
129}
RuntimeUnityEditor.Core.LogLevel LogLevel
Definition: RUEInvoker.cs:5