Skip to content

Commit 417002e

Browse files
jared-marsauGitHub Enterprise
authored andcommitted
Availability Attribute additions and clean-up. (#6)
* Added one more availability attribute - the Unavailable attribute for indicating runtime operating systems that might not support a particular API. * Updated version parsing code to use a regular expressions; some Unity builds were not deducing overloads of String.Split correctly. * Small fixes to prevent bad input and addressing of typos.
1 parent 5ef57c5 commit 417002e

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/AvailabilityAttributes.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Apple.Core
45
{
@@ -11,25 +12,46 @@ namespace Apple.Core
1112
public class IntroducedAttribute : Attribute
1213
{
1314
// Access by property
14-
public RuntimeVersion? iOS { get => _osVersionNumbers[(int)RuntimeOperatingSystem.iOS]; }
15-
public RuntimeVersion? macOS { get => _osVersionNumbers[(int)RuntimeOperatingSystem.macOS]; }
16-
public RuntimeVersion? tvOS { get => _osVersionNumbers[(int)RuntimeOperatingSystem.tvOS]; }
15+
public RuntimeVersion? iOS { get => _osVersions[RuntimeOperatingSystem.iOS]; }
16+
public RuntimeVersion? macOS { get => _osVersions[RuntimeOperatingSystem.macOS]; }
17+
public RuntimeVersion? tvOS { get => _osVersions[RuntimeOperatingSystem.tvOS]; }
1718

1819
// Access by OperatingSystem
1920
public RuntimeVersion? OperatingSystemVersion(RuntimeOperatingSystem operatingSystem)
2021
{
21-
return _osVersionNumbers[(int)operatingSystem];
22+
if (!Enum.IsDefined(typeof(RuntimeOperatingSystem), operatingSystem) || (operatingSystem == RuntimeOperatingSystem.Unknown) || (operatingSystem == RuntimeOperatingSystem.RuntimeOperatingSystemCount))
23+
{
24+
return null;
25+
}
26+
27+
return _osVersions[operatingSystem];
2228
}
2329

24-
protected RuntimeVersion?[] _osVersionNumbers;
30+
protected SortedList<RuntimeOperatingSystem, RuntimeVersion?> _osVersions;
2531

2632
public IntroducedAttribute(string iOS = "", string macOS = "", string tvOS = "")
2733
{
28-
_osVersionNumbers = new RuntimeVersion?[(int)RuntimeOperatingSystem.RuntimeOperatingSystemCount];
34+
_osVersions = new SortedList<RuntimeOperatingSystem, RuntimeVersion?>();
2935

30-
_osVersionNumbers[(int)RuntimeOperatingSystem.iOS] = RuntimeVersion.FromString(iOS);
31-
_osVersionNumbers[(int)RuntimeOperatingSystem.macOS] = RuntimeVersion.FromString(macOS);
32-
_osVersionNumbers[(int)RuntimeOperatingSystem.tvOS] = RuntimeVersion.FromString(tvOS);
36+
_osVersions[RuntimeOperatingSystem.iOS] = RuntimeVersion.FromString(iOS);
37+
_osVersions[RuntimeOperatingSystem.macOS] = RuntimeVersion.FromString(macOS);
38+
_osVersions[RuntimeOperatingSystem.tvOS] = RuntimeVersion.FromString(tvOS);
39+
}
40+
}
41+
42+
/// <summary>
43+
/// UnavailableAttribute is used to indicate operating systems for which an API is *not* available.
44+
/// Example: [Apple.Core.Unavailable(RuntimeOperatingSystem.tvOS)] means that a given API is not available for tvOS, but is available for all other supported OSes.
45+
/// </summary>
46+
[System.AttributeUsage(System.AttributeTargets.All)]
47+
public class UnavailableAttribute : Attribute
48+
{
49+
public RuntimeOperatingSystem[] UnavailableOperatingSystems { get; protected set; }
50+
51+
public UnavailableAttribute(params RuntimeOperatingSystem[] unavailableOperatingSystems)
52+
{
53+
UnavailableOperatingSystems = new RuntimeOperatingSystem[unavailableOperatingSystems.Length];
54+
unavailableOperatingSystems.CopyTo(UnavailableOperatingSystems, 0);
3355
}
3456
}
3557

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/RuntimeEnvironment.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using System.Text.RegularExpressions;
4+
using UnityEngine;
35

46
namespace Apple.Core
57
{
@@ -26,8 +28,10 @@ public RuntimeVersion(int major, int minor)
2628
Minor = minor;
2729
}
2830

29-
public override string ToString() => $"{Major}.{Minor}";
31+
public override string ToString() => (Minor == 0) ? $"{Major}" : $"{Major}.{Minor}";
3032

33+
static readonly Regex _versionStringFormat = new Regex(@"^((?:\d+)(?:\.\d+)?)$");
34+
3135
/// <summary>
3236
/// Accepted string formats are "Major.Minor" or "Major" where Major and Minor are represented by integer values.
3337
/// Minor value is assumed to be 0 when no Minor value is provided in the format string.
@@ -44,39 +48,35 @@ public RuntimeVersion(int major, int minor)
4448
return null;
4549
}
4650

47-
string[] vStrings = versionString.Split('.', 3);
48-
49-
// String provided in the format: "Major.Minor"
50-
if (vStrings.Length >= 2)
51+
// Ensure strings are formatted as "Major.Minor" or "Major" where Major and Minor are strings of numbers, e.g. "12.5" or "14"
52+
if (_versionStringFormat.IsMatch(versionString))
5153
{
5254
int major = 0, minor = 0;
5355

54-
if (Int32.TryParse(vStrings[0], out major) && Int32.TryParse(vStrings[1], out minor))
55-
{
56-
return new RuntimeVersion(major, minor);
57-
}
58-
else
59-
{
60-
return null;
61-
}
62-
}
63-
// String provided in the format: "Major"
64-
else if (vStrings.Length == 1)
65-
{
66-
int major = 0;
67-
68-
if (Int32.TryParse(vStrings[0], out major))
56+
// Get first string of numerals and try to parse
57+
Match currMatch = Regex.Match(versionString, @"\d+");
58+
if (currMatch.Success && Int32.TryParse(currMatch.Value, out major))
6959
{
70-
return new RuntimeVersion(major, 0);
60+
// Get the string of numerals, if they exist, and try to parse
61+
currMatch = currMatch.NextMatch();
62+
if (currMatch.Success && Int32.TryParse(currMatch.Value, out minor))
63+
{
64+
return new RuntimeVersion(major, minor);
65+
}
66+
else
67+
{
68+
return new RuntimeVersion(major, 0);
69+
}
7170
}
7271
else
7372
{
73+
Debug.Log($"[Apple.Core Plug-In] RuntimeEnvironment failed to parse \"{currMatch.Value}\" as Int32.");
7474
return null;
7575
}
7676
}
77-
// Unsupported formatting.
78-
else
77+
else
7978
{
79+
Debug.Log($"[Apple.Core Plug-In] RuntimeEnvironment failed to recognize \"{versionString}\" as a valid version string.");
8080
return null;
8181
}
8282
}

0 commit comments

Comments
 (0)