44using System ;
55using System . Collections . Generic ;
66using System . IO ;
7+ using System . Runtime . CompilerServices ;
78using UnityEditor . PackageManager ;
89using UnityEngine ;
10+ using UnityEngine . Networking ;
11+ using Version = System . Version ;
912
13+ [ assembly: InternalsVisibleTo ( "Microsoft.MixedReality.Toolkit.Tests.EditModeTests" ) ]
1014namespace Microsoft . MixedReality . Toolkit . Utilities . Editor
1115{
1216 /// <summary>
@@ -19,7 +23,7 @@ internal static class PackageManifestUpdater
1923 private static string [ ] MSBuildRegistryScopes = new string [ ] { "com.microsoft" } ;
2024
2125 internal const string MSBuildPackageName = "com.microsoft.msbuildforunity" ;
22- internal const string MSBuildPackageVersion = "0.9.1" ;
26+ internal const string MSBuildPackageVersion = "0.9.1-20200131.12 " ;
2327
2428 /// <summary>
2529 /// Finds and returns the fully qualified path to the Unity Package Manager manifest
@@ -42,10 +46,102 @@ private static string GetPackageManifestFilePath()
4246 return manifestPath ;
4347 }
4448
49+ internal static bool TryGetVersionComponents (
50+ string packageVersion ,
51+ out Version version ,
52+ out float prerelease )
53+ {
54+ char [ ] trimChars = new char [ ] { ' ' , '\" ' , ',' } ;
55+
56+ // Note: The version is in the following format Major.Minor.Revision[-Date.Build]
57+
58+ // Attempt to split the version string into version and float components
59+ string [ ] versionComponents = packageVersion . Split ( new char [ ] { '-' } , 2 ) ;
60+
61+ // Parse the version component.
62+ string versionString = versionComponents [ 0 ] . Trim ( trimChars ) ;
63+ if ( Version . TryParse ( versionString , out version ) )
64+ {
65+ if ( versionComponents . Length == 2 )
66+ {
67+ // Parse the float component
68+ string prereleaseString = versionComponents [ 1 ] . Trim ( trimChars ) ;
69+ if ( float . TryParse ( prereleaseString , out prerelease ) )
70+ {
71+ return true ;
72+ }
73+ }
74+ else
75+ {
76+ prerelease = 0f ;
77+ return true ;
78+ }
79+ }
80+
81+ version = null ;
82+ prerelease = float . NaN ;
83+ return false ;
84+ }
85+
4586 /// <summary>
4687 /// Reports whether or not the appropriate version of MSBuild for Unity is specified
4788 /// in the Unity Package Manager manifest.
4889 /// </summary>
90+ /// <param name="minPackageVersion">The minimum version of the package, as listed in the manifest.</param>
91+ /// <param name="packageVersion">The version of the package, as listed in the manifest.</param>
92+ /// <returns>
93+ /// True if an appropriate verson of MS Build for Unity is configured in the manifest, otherwise false.
94+ /// </returns>
95+ internal static bool IsAppropriateMBuildVersion ( string minPackageVersion , string packageVersion )
96+ {
97+ // Get the version of the package.
98+ // Note: The version is in the following format Major.Minor.Revision[-Date.Build]
99+
100+ Version minVersion ;
101+ float minPrerelease ;
102+
103+ // Get the min version
104+ if ( ! TryGetVersionComponents ( minPackageVersion , out minVersion , out minPrerelease ) )
105+ {
106+ return false ;
107+ }
108+
109+ // Get the current version from the manifest
110+ Version currentVersion ;
111+ float currentPrerelease ;
112+ if ( ! TryGetVersionComponents ( packageVersion , out currentVersion , out currentPrerelease ) )
113+ {
114+ return false ;
115+ }
116+
117+ // Evaluate the results.
118+ // * (currentVersion > minVersion) return true;
119+ // * (currentVersion == minVersion && currentPrerelease == minPrerelease) return true;
120+ // * (currentVersion == minVersion && minPrerelease != 0 && currentPrerelease >= minPrerelease) return true;
121+ // * all other combinatons return false;
122+ if ( currentVersion > minVersion )
123+ {
124+ return true ;
125+ }
126+ else if ( currentVersion == minVersion )
127+ {
128+ // The current and minumum versions are the same, check the prerelease portion
129+ if ( currentPrerelease == minPrerelease )
130+ {
131+ return true ;
132+ }
133+ else if ( ( minPrerelease != 0f ) && ( currentPrerelease >= minPrerelease ) )
134+ {
135+ return true ;
136+ } ;
137+ }
138+
139+ return false ;
140+ }
141+
142+ /// <summary>
143+ /// Reports whether or not the MSBuild for Unity is properly enabled in the Unity Package Manager manifest.
144+ /// </summary>
49145 /// <returns>
50146 /// True if an appropriate verson of MS Build for Unity is configured in the manifest, otherwise false.
51147 /// </returns>
@@ -65,9 +161,6 @@ internal static bool IsMSBuildForUnityEnabled()
65161 }
66162
67163 // Read the package manifest a line at a time.
68- bool msBuildFound = false ;
69- bool isAppropriateVersion = false ;
70- Version minVersion = Version . Parse ( MSBuildPackageVersion ) ;
71164 using ( FileStream manifestStream = new FileStream ( manifestPath , FileMode . Open , FileAccess . Read ) )
72165 {
73166 using ( StreamReader reader = new StreamReader ( manifestStream ) )
@@ -78,38 +171,16 @@ internal static bool IsMSBuildForUnityEnabled()
78171 string line = reader . ReadLine ( ) ;
79172 if ( line . Contains ( MSBuildPackageName ) )
80173 {
81- msBuildFound = true ;
82-
83- // Next, check the version.
84- string [ ] splitLine = line . Split ( new char [ ] { ':' } ) ;
85- if ( splitLine . Length == 2 )
86- {
87- // Ensure correct formatting of the version string, before we attempt to parse it.
88- string versionString = splitLine [ 1 ] . Trim ( new char [ ] { ' ' , '\" ' , ',' } ) ;
89- bool replaceOnEquals = false ;
90- if ( versionString . Contains ( "-" ) )
91- {
92- // The string references a preview version. Truncate at the '-'.
93- versionString = versionString . Substring ( 0 , versionString . IndexOf ( '-' ) ) ;
94-
95- // We want to update preview versions to the final.
96- replaceOnEquals = true ;
97- }
98-
99- Version version ;
100- if ( Version . TryParse ( versionString , out version ) )
101- {
102- isAppropriateVersion = replaceOnEquals ? ( version > minVersion ) : ( version >= minVersion ) ;
103- }
104- }
105-
106- break ;
174+ // Split the line into packageName : packageVersion
175+ string [ ] lineComponents = line . Split ( new char [ ] { ':' } , 2 ) ;
176+
177+ return IsAppropriateMBuildVersion ( MSBuildPackageVersion , lineComponents [ 1 ] ) ;
107178 }
108179 }
109180 }
110181 }
111182
112- return ( msBuildFound && isAppropriateVersion ) ;
183+ return false ;
113184 }
114185
115186 /// <summary>
@@ -189,10 +260,14 @@ internal static void EnsureMSBuildForUnity()
189260 int scopedRegistriesEndIndex = - 1 ;
190261 int packageLine = - 1 ;
191262
263+ // Presume that we need to add the MSBuild for Unity package. If this value is false,
264+ // we will check to see if the currently configured version meets or exceeds the
265+ // minimum requirements.
266+ bool needToAddPackage = true ;
267+
192268 // Attempt to find the MSBuild for Unity package entry in the dependencies collection
193269 // This loop also identifies the dependecies collection line and the start / end of a
194270 // pre-existing scoped registries collections
195- bool addPackage = true ;
196271 for ( int i = 0 ; i < manifestFileLines . Count ; i ++ )
197272 {
198273 if ( manifestFileLines [ i ] . Contains ( "\" scopedRegistries\" :" ) )
@@ -210,12 +285,12 @@ internal static void EnsureMSBuildForUnity()
210285 if ( manifestFileLines [ i ] . Contains ( MSBuildPackageName ) )
211286 {
212287 packageLine = i ;
213- addPackage = false ;
288+ needToAddPackage = false ;
214289 }
215290 }
216291
217292 // If no package was found add it to the dependencies collection.
218- if ( addPackage )
293+ if ( needToAddPackage )
219294 {
220295 // Add the package to the collection (pad the entry with four spaces)
221296 manifestFileLines . Insert ( dependenciesStartIndex + 1 , $ " \" { MSBuildPackageName } \" : \" { MSBuildPackageVersion } \" ,") ;
0 commit comments