@@ -315,53 +315,104 @@ public static async Task<long> GetFileSizeAsyncAsLong(Uri? url)
315315 return 0 ;
316316 }
317317
318+
319+ public struct Version : IComparable
320+ {
321+ public static readonly Version Null = new ( - 1 , - 1 , - 1 , - 1 ) ;
322+
323+ public readonly int Major ;
324+ public readonly int Minor ;
325+ public readonly int Patch ;
326+ public readonly int Remainder ;
327+
328+ public Version ( int major , int minor = 0 , int patch = 0 , int remainder = 0 )
329+ {
330+ Major = major ;
331+ Minor = minor ;
332+ Patch = patch ;
333+ Remainder = remainder ;
334+ }
335+
336+ public int CompareTo ( object ? other_ )
337+ {
338+ if ( other_ is not Version other ) return 0 ;
339+
340+ int major = Major . CompareTo ( other . Major ) ;
341+ if ( major != 0 ) return major ;
342+
343+ int minor = Minor . CompareTo ( other . Minor ) ;
344+ if ( minor != 0 ) return minor ;
345+
346+ int patch = Patch . CompareTo ( other . Patch ) ;
347+ if ( patch != 0 ) return patch ;
348+
349+ return Remainder . CompareTo ( other . Remainder ) ;
350+ }
351+
352+ public static bool operator == ( Version left , Version right )
353+ => left . CompareTo ( right ) == 0 ;
354+
355+ public static bool operator != ( Version left , Version right )
356+ => left . CompareTo ( right ) != 0 ;
357+
358+ public static bool operator >= ( Version left , Version right )
359+ => left . CompareTo ( right ) >= 0 ;
360+
361+ public static bool operator <= ( Version left , Version right )
362+ => left . CompareTo ( right ) <= 0 ;
363+
364+ public static bool operator > ( Version left , Version right )
365+ => left . CompareTo ( right ) > 0 ;
366+
367+ public static bool operator < ( Version left , Version right )
368+ => left . CompareTo ( right ) < 0 ;
369+
370+ public bool Equals ( Version other )
371+ => Major == other . Major && Minor == other . Minor && Patch == other . Patch && Remainder == other . Remainder ;
372+
373+ public override bool Equals ( object ? obj )
374+ => obj is Version other && Equals ( other ) ;
375+
376+ public override int GetHashCode ( )
377+ => HashCode . Combine ( Major , Minor , Patch , Remainder ) ;
378+ }
379+
318380 /// <summary>
319381 /// Converts a string into a double floating-point number.
320382 /// </summary>
321383 /// <param name="Version">Any string</param>
322384 /// <returns>The best approximation of the string as a Version</returns>
323- public static double GetVersionStringAsFloat ( string Version )
385+ public static Version VersionStringToStruct ( string Version )
324386 {
325387 try
326388 {
327- string _ver = "" ;
328- bool _dotAdded = false ;
329- foreach ( char _char in Version )
389+ char [ ] separators = [ '.' , '-' , '/' , '#' ] ;
390+ string [ ] versionItems = [ "" , "" , "" , "" ] ;
391+
392+ int dotCount = 0 ;
393+ bool first = true ;
394+
395+ foreach ( char c in Version )
330396 {
331- if ( char . IsDigit ( _char ) )
332- {
333- _ver += _char ;
334- }
335- else if ( _char == '.' )
336- {
337- if ( ! _dotAdded )
338- {
339- _ver += _char ;
340- _dotAdded = true ;
341- }
342- }
397+ if ( char . IsDigit ( c ) ) versionItems [ dotCount ] += c ;
398+ else if ( ! first && separators . Contains ( c ) ) if ( dotCount < 3 ) dotCount ++ ;
399+ first = false ;
343400 }
344401
345- double res = - 1 ;
346- if ( _ver is not "" and not "." )
402+ int [ ] numbers = { 0 , 0 , 0 , 0 } ;
403+ for ( int i = 0 ; i < 4 ; i ++ )
347404 {
348- try
349- {
350- double val = double . Parse ( _ver , CultureInfo . InvariantCulture ) ;
351- return val ;
352- }
353- catch
354- {
355- // ignored
356- }
405+ if ( int . TryParse ( versionItems [ i ] , out int val ) )
406+ numbers [ i ] = val ;
357407 }
358408
359- return res ;
409+ var ver = new Version ( numbers [ 0 ] , numbers [ 1 ] , numbers [ 2 ] , numbers [ 3 ] ) ;
410+ return ver ;
360411 }
361412 catch
362413 {
363414 Logger . Warn ( $ "Failed to parse version { Version } to float") ;
364- return - 1 ;
415+ return CoreTools . Version . Null ;
365416 }
366417 }
367418
0 commit comments