Nuke.Unreal
Build Unreal apps in Style.
Loading...
Searching...
No Matches
UnrealPlatform.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading.Tasks;
5using System.ComponentModel;
6using Nuke.Common.Tooling;
7using Newtonsoft.Json;
8using Nuke.Cola;
9using Nuke.Common;
10using Nuke.Common.Utilities;
11
12namespace Nuke.Unreal
13{
14 /// <summary>
15 /// Bit-field representation of Unreal platforms and platform-families
16 /// </summary>
17 [Flags]
18 public enum UnrealPlatformFlag : UInt16
19 {
20 Win64 = 1 << 0,
21 Win32 = 1 << 1,
22 // TODO: Windows ARM64?
23 HoloLens = 1 << 2,
24 Mac = 1 << 3,
25 Linux = 1 << 4,
26 LinuxArm64 = 1 << 5,
27 Android = 1 << 6,
28 IOS = 1 << 7,
29 TVOS = 1 << 8,
30 VisionOS = 1 << 9,
31 AllWin = Win32 | Win64,
32 AllLinux = Linux | LinuxArm64,
33 AllDesktop = AllWin | AllLinux | Mac,
34 Independent = 0xFFFF
35
36 // TODO: rest of the platforms
37 // Engine/Source/Programs/UnrealBuildTool/Configuration/UEBuildTarget.cs
38 }
39
40 public class UnrealPlatformJsonConverter : JsonConverter
41 {
42 public override bool CanConvert(Type objectType)
43 {
44 return objectType.IsClass && objectType.IsAssignableTo(typeof(UnrealPlatform));
45 }
46
47 public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
48 {
49 if (reader.Value is string value)
50 {
51 return UnrealPlatform.Parse(value);
52 }
53 Assert.True(reader.TokenType == JsonToken.None
54 || reader.TokenType == JsonToken.Null
55 || reader.TokenType == JsonToken.Undefined,
56 $"JSON value was neither a string, nor one of the 'not-existing' types. Token type: {reader.TokenType}; Object type: {objectType.Name};"
57 );
58 return null;
59 }
60
61 public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
62 {
63 if (value is UnrealPlatform platform)
64 {
65 writer.WriteValue(platform.UserDefinedName ?? platform.ToString());
66 }
67 }
68 }
69
70 /// <summary>
71 /// High level representation of common platforms supported by Unreal Engine (NDA ones excluded)
72 /// and extra metadata about platform specific intricacies.
73 /// </summary>
74 [TypeConverter(typeof(TypeConverter<UnrealPlatform>))]
75 [JsonConverter(typeof(UnrealPlatformJsonConverter))]
76 public class UnrealPlatform : Enumeration, ICloneable<UnrealPlatform>
77 {
78 /// <summary>
79 /// Any platform name containing 'Windows' is also mapped to this platform
80 /// </summary>
81 public static readonly UnrealPlatform Win64 = new()
82 {
83 Value = nameof(Win64),
84 Flag = UnrealPlatformFlag.Win64,
85 DllExtension = "dll"
86 };
87 public static readonly UnrealPlatform Win32 = new()
88 {
89 Value = nameof(Win32),
90 Flag = UnrealPlatformFlag.Win32,
92 DllExtension = "dll",
93 };
94 public static readonly UnrealPlatform HoloLens = new()
95 {
96 Value = nameof(HoloLens),
97 Flag = UnrealPlatformFlag.HoloLens,
99 DllExtension = "dll",
100 };
101
102 /// <summary>
103 /// Any platform name containing 'Mac' is also mapped to this platform
104 /// </summary>
105 public static readonly UnrealPlatform Mac = new()
106 {
107 Value = nameof(Mac),
108 Flag = UnrealPlatformFlag.Mac,
109 DllExtension = "dylib",
110 };
111 public static readonly UnrealPlatform Linux = new()
112 {
113 Value = nameof(Linux),
114 Flag = UnrealPlatformFlag.Linux
115 };
116 public static readonly UnrealPlatform LinuxArm64 = new()
117 {
118 Value = nameof(LinuxArm64),
119 Flag = UnrealPlatformFlag.LinuxArm64
120 };
121 public static readonly UnrealPlatform Android = new()
122 {
123 Value = nameof(Android),
124 Flag = UnrealPlatformFlag.Android
125 };
126 public static readonly UnrealPlatform IOS = new()
127 {
128 Value = nameof(IOS),
129 Flag = UnrealPlatformFlag.IOS,
130 DllExtension = "dylib",
131 };
132 public static readonly UnrealPlatform TVOS = new()
133 {
134 Value = nameof(TVOS),
135 Flag = UnrealPlatformFlag.TVOS,
136 DllExtension = "dylib",
137 };
138 public static readonly UnrealPlatform VisionOS = new()
139 {
140 Value = nameof(VisionOS),
141 Flag = UnrealPlatformFlag.VisionOS,
142 Compatibility = UnrealCompatibility.UE_5_5.AndLater(),
143 DllExtension = "dylib",
144 };
145
146 /// <summary>
147 /// Meta-platform indicating that its context of operation is platform-independent or has
148 /// (should have) the same results on all platforms.
149 /// </summary>
150 public static readonly UnrealPlatform Independent = new()
151 {
152 Value = nameof(Independent),
153 Flag = UnrealPlatformFlag.Independent,
154 DllExtension = "",
155 _platformText = ""
156 };
157
158 /// <summary>
159 /// Get the high-level platform from a bit-field platform flag
160 /// </summary>
162 {
163 return flag switch
164 {
165 UnrealPlatformFlag.Win64 => Win64,
166 UnrealPlatformFlag.Win32 => Win32,
167 // TODO: Windows ARM64?
168 UnrealPlatformFlag.HoloLens => HoloLens,
169 UnrealPlatformFlag.Mac => Mac,
170 UnrealPlatformFlag.Linux => Linux,
171 UnrealPlatformFlag.LinuxArm64 => LinuxArm64,
172 UnrealPlatformFlag.Android => Android,
173 UnrealPlatformFlag.IOS => IOS,
174 UnrealPlatformFlag.TVOS => TVOS,
175 UnrealPlatformFlag.VisionOS => VisionOS,
176 UnrealPlatformFlag.Independent => Independent,
177 _ => throw new Exception($"UnrealPlatformFlag {flag} didn't have matching UnrealPlatform"),
178 };
179 }
180
181 /// <summary>
182 /// List of all "real" platforms
183 /// </summary>
184 public static readonly List<UnrealPlatform> Platforms =
185 [
186 Win64,
187 Mac,
188 Linux,
189 LinuxArm64,
190 Android,
191 IOS,
192 TVOS,
193 VisionOS,
194 ];
195
196 /// <summary>
197 /// List of platforms which can support the Unreal Editor
198 /// </summary>
199 public static readonly List<UnrealPlatform> DevelopmentPlatforms =
200 [
201 Win64,
202 Mac,
203 Linux,
204 ];
205
206 public UnrealPlatformFlag Flag {get; private set; } = UnrealPlatformFlag.Win64;
207
208 /// <summary>
209 /// Which Unreal version this platform is compatible with.
210 /// </summary>
211 public UnrealCompatibility Compatibility {get; private set; } = UnrealCompatibility.All;
212
213 /// <summary>
214 /// The platform specific file extension used for dynamic libraries
215 /// </summary>
216 public string DllExtension {get; private set; } = "so";
217
218 /// <summary>
219 /// In some cases platforms can have multiple names which can be used by project~ and plugin
220 /// descriptors. In order to maintain their value during file operations, we maintain these
221 /// aliases here, if they're recognized by the parser.
222 /// </summary>
223 public string? UserDefinedName { get; private set; }
224
225 private string? _platformText = null;
226
227 /// <summary>
228 /// This property is used for serialization contexts where maintaining a platform name alias
229 /// is important. Use `ToString` or `Value` otherwise.
230 /// </summary>
231 public string PlatformText => _platformText ?? Value;
232
233 /// <summary>
234 /// Is given platform a traditional desktop computer?
235 /// </summary>
236 public bool IsDesktop => (Flag & UnrealPlatformFlag.AllDesktop) > 0;
237
238 /// <summary>
239 /// Can this platform support Unreal Editor?
240 /// </summary>
241 public bool IsDevelopment => DevelopmentPlatforms.Contains(this);
242
243 /// <summary>
244 /// Can this platform support Unreal Editor?
245 /// </summary>
246 public bool IsHost => IsDevelopment;
247
248 public bool IsLinux => (Flag & UnrealPlatformFlag.AllLinux) > 0;
249 public bool IsWindows => (Flag & UnrealPlatformFlag.AllWin) > 0;
250
251 /// <summary>
252 /// Given platform is for handheld devices and/or standalone XR headsets.
253 /// </summary>
254 public bool IsMobile => (Flag & (UnrealPlatformFlag.Android | UnrealPlatformFlag.IOS | UnrealPlatformFlag.HoloLens | UnrealPlatformFlag.VisionOS)) > 0;
255
256 public override string ToString()
257 {
258 return Value;
259 }
260
261 public UnrealPlatform Clone()
262 {
263 return new()
264 {
265 Flag = Flag,
269 _platformText = _platformText
270 };
271 }
272
273 object ICloneable.Clone()
274 {
275 return Clone();
276 }
277
278 public static implicit operator string(UnrealPlatform configuration)
279 {
280 return configuration.ToString();
281 }
282
283 /// <summary>
284 /// Attempt to convert a piece of text into a high-level platform representation.
285 /// Platform name aliases/variations are supported:
286 /// <list type="bullet">
287 /// <item>Anything containing 'Windows' will be mapped to `Win64`</item>
288 /// <item>Anything containing 'Mac' will be mapped to `Mac` (like MacOS, MacOSX)</item>
289 /// </list>
290 /// </summary>
291 /// <exception cref="AggregateException">
292 /// Thrown when the input text cannot be mapped to any of the known platforms.
293 /// </exception>
294 /// <returns>
295 /// The determined static high-level platform, or a clone of a high-level platform if
296 /// the input name was a platform alias.
297 /// </returns>
298 public static UnrealPlatform Parse(string platformText)
299 {
300 if (platformText.ContainsOrdinalIgnoreCase("Windows"))
301 return Win64.Clone().With(p => p.UserDefinedName = platformText);
302 if (platformText.ContainsOrdinalIgnoreCase("Mac") && platformText != "Mac")
303 return Mac.Clone().With(p => p.UserDefinedName = platformText);
304 try
305 {
306 var result = TypeDescriptor.GetConverter(typeof(UnrealPlatform)).ConvertFrom(platformText!);
307 Assert.NotNull(result, $"'{platformText}' is not a valid UnrealPlatform");
308 return (UnrealPlatform)result!;
309 }
310 catch (Exception e)
311 {
312 throw new AggregateException($"'{platformText}' is not a valid UnrealPlatform", e);
313 }
314 }
315 }
316}
High level representation of common platforms supported by Unreal Engine (NDA ones excluded) and extr...
string DllExtension
The platform specific file extension used for dynamic libraries.
bool IsMobile
Given platform is for handheld devices and/or standalone XR headsets.
static readonly UnrealPlatform Win64
Any platform name containing 'Windows' is also mapped to this platform.
static readonly UnrealPlatform Mac
Any platform name containing 'Mac' is also mapped to this platform.
bool IsDevelopment
Can this platform support Unreal Editor?
static readonly UnrealPlatform Independent
Meta-platform indicating that its context of operation is platform-independent or has (should have) t...
static readonly List< UnrealPlatform > Platforms
List of all "real" platforms.
UnrealCompatibility Compatibility
Which Unreal version this platform is compatible with.
bool IsHost
Can this platform support Unreal Editor?
bool IsDesktop
Is given platform a traditional desktop computer?
string? UserDefinedName
In some cases platforms can have multiple names which can be used by project~ and plugin descriptors....
string PlatformText
This property is used for serialization contexts where maintaining a platform name alias is important...
static UnrealPlatform Parse(string platformText)
Attempt to convert a piece of text into a high-level platform representation. Platform name aliases/v...
static readonly List< UnrealPlatform > DevelopmentPlatforms
List of platforms which can support the Unreal Editor.
static UnrealPlatform FromFlag(UnrealPlatformFlag flag)
Get the high-level platform from a bit-field platform flag.
UnrealPlatformFlag
Bit-field representation of Unreal platforms and platform-families.
UnrealCompatibility
A flag enum representation for checking the Unreal version compatibility of various features....