1- using System . Collections . Generic ;
2- using System . Threading . Tasks ;
1+ using System ;
32using Sandbox . Engine . Networking ;
4- using Torch . API ;
53using VRage . Game ;
64
75namespace Torch . Utils
86{
97 public static class ModItemUtils
108 {
9+ /// <summary>
10+ /// Creates <see cref="MyObjectBuilder_Checkpoint.ModItem"/> from mod id and optionally service name.
11+ /// </summary>
12+ /// <param name="modId">Mod id.</param>
13+ /// <param name="serviceType">Service name, will be default if null passed.</param>
14+ /// <returns><see cref="MyObjectBuilder_Checkpoint.ModItem"/></returns>
1115 public static MyObjectBuilder_Checkpoint . ModItem Create ( ulong modId , string serviceType = null )
1216 {
1317 return new MyObjectBuilder_Checkpoint . ModItem ( modId , serviceType ?? GetDefaultServiceName ( ) ) ;
1418 }
1519
20+ /// <summary>
21+ /// Creates <see cref="MyObjectBuilder_Checkpoint.ModItem"/> from <see cref="string"/> containing mod id and the service name.
22+ /// </summary>
23+ /// <param name="str">String containing mod id and the service name.</param>
24+ /// <returns><see cref="MyObjectBuilder_Checkpoint.ModItem"/></returns>
25+ /// <exception cref="FormatException" />
26+ /// <exception cref="OverflowException" />
27+ /// <exception cref="ArgumentOutOfRangeException" />
1628 public static MyObjectBuilder_Checkpoint . ModItem Create ( string str )
1729 {
1830 var arr = str . Split ( '-' ) ;
19- // backward compat
20- return new MyObjectBuilder_Checkpoint . ModItem ( ulong . Parse ( arr [ 0 ] ) , arr . Length > 1 ? arr [ 1 ] : GetDefaultServiceName ( ) ) ;
31+ return new MyObjectBuilder_Checkpoint . ModItem ( ulong . Parse ( arr [ 0 ] ) , arr [ 1 ] ) ;
32+ }
33+
34+ /// <summary>
35+ /// Tries to parse mod id <see cref="string"/> with or without the service name.
36+ /// </summary>
37+ /// <param name="str"><see cref="string"/> containing mod id and optionally the service name.</param>
38+ /// <param name="item">Parsed <see cref="MyObjectBuilder_Checkpoint.ModItem"/> or default if parsing was unsuccessful.</param>
39+ /// <returns>Parsing operation was successful or not.</returns>
40+ public static bool TryParse ( string str , out MyObjectBuilder_Checkpoint . ModItem item )
41+ {
42+ item = default ;
43+
44+ var arr = str . Split ( '-' ) ;
45+
46+ if ( arr . Length == 0 || arr . Length > 2 )
47+ return false ;
48+
49+ if ( ! ulong . TryParse ( arr [ 0 ] , out var id ) )
50+ return false ;
51+
52+ if ( arr . Length == 1 || ! TryParseServiceName ( arr [ 1 ] , out var serviceName ) )
53+ serviceName = GetDefaultServiceName ( ) ;
54+
55+ item = new MyObjectBuilder_Checkpoint . ModItem ( id , serviceName ) ;
56+ return true ;
57+ }
58+
59+ /// <summary>
60+ /// Tries to format service name to standardized and recognizable by game.
61+ /// </summary>
62+ /// <param name="str">Raw service name.</param>
63+ /// <param name="serviceName">Parsed service name or null if parsing was unsuccessful.</param>
64+ /// <returns>Parsing operation was successful or not.</returns>
65+ public static bool TryParseServiceName ( string str , out string serviceName )
66+ {
67+ if ( str . Equals ( "steam" , StringComparison . OrdinalIgnoreCase ) )
68+ {
69+ serviceName = "Steam" ;
70+ return true ;
71+ }
72+ if ( str . Equals ( "mod.io" , StringComparison . OrdinalIgnoreCase ) ||
73+ str . Equals ( "eos" , StringComparison . OrdinalIgnoreCase ) )
74+ {
75+ serviceName = "mod.io" ;
76+ return true ;
77+ }
78+
79+ serviceName = null ;
80+ return false ;
2181 }
2282
23- //because KEEEN!
83+ /// <summary>
84+ /// Default service name according to current configuration.
85+ /// </summary>
86+ /// <returns>UGC Service name.</returns>
2487 public static string GetDefaultServiceName ( )
2588 {
2689 try
@@ -29,7 +92,9 @@ public static string GetDefaultServiceName()
2992 }
3093 catch
3194 {
95+ #pragma warning disable CS0618
3296 return TorchBase . Instance . Config . UgcServiceType . ToString ( ) ;
97+ #pragma warning restore CS0618
3398 }
3499 }
35100 }
0 commit comments