7
7
using System ;
8
8
using System . Collections . Generic ;
9
9
using System . Linq ;
10
+ using System . Text . RegularExpressions ;
10
11
using UnityEngine ;
11
12
using Unity . MLAgents . Analytics ;
12
13
using Unity . MLAgents . CommunicatorObjects ;
@@ -99,6 +100,41 @@ internal static bool CheckCommunicationVersionsAreCompatible(
99
100
}
100
101
101
102
internal static bool CheckPythonPackageVersionIsCompatible ( string pythonLibraryVersion )
103
+ {
104
+ // Try to extract the numerical values from the pythonLibraryVersion, e.g. remove the ".dev0" suffix
105
+ var versionMatch = Regex . Match ( pythonLibraryVersion , @"[0-9]+\.[0-9]+\.[0-9]" ) ;
106
+ if ( versionMatch . Success )
107
+ {
108
+ pythonLibraryVersion = versionMatch . Value ;
109
+ }
110
+
111
+ Version pythonVersion ;
112
+ try
113
+ {
114
+ pythonVersion = new Version ( pythonLibraryVersion ) ;
115
+ }
116
+ catch
117
+ {
118
+ // Unparseable version.
119
+ // Anything like 0.42.0.dev0 should have been caught with the regex above, so anything here
120
+ // is totally bogus. For now, ignore these and let CheckPythonPackageVersionIsSupported handle it.
121
+ return true ;
122
+ }
123
+
124
+ if ( pythonVersion > PythonTrainerVersions . s_MaxCompatibleVersion )
125
+ {
126
+ return false ;
127
+ }
128
+ return true ;
129
+ }
130
+
131
+ /// <summary>
132
+ /// Check if the package is in the supported range. Note that some versions might be unsupported but
133
+ /// still compatible.
134
+ /// </summary>
135
+ /// <param name="pythonLibraryVersion"></param>
136
+ /// <returns></returns>
137
+ internal static bool CheckPythonPackageVersionIsSupported ( string pythonLibraryVersion )
102
138
{
103
139
Version pythonVersion ;
104
140
try
@@ -107,7 +143,7 @@ internal static bool CheckPythonPackageVersionIsCompatible(string pythonLibraryV
107
143
}
108
144
catch
109
145
{
110
- // Unparseable - this also catches things like "0.20.0- dev0" which we don't want to support
146
+ // Unparseable - this also catches things like "0.20.0. dev0" which we don't want to support
111
147
return false ;
112
148
}
113
149
@@ -182,7 +218,21 @@ public UnityRLInitParameters Initialize(CommunicatorInitParameters initParameter
182
218
throw new UnityAgentsException ( "ICommunicator.Initialize() failed." ) ;
183
219
}
184
220
185
- var packageVersionSupported = CheckPythonPackageVersionIsCompatible ( pythonPackageVersion ) ;
221
+ var packageVersionCompatible = CheckPythonPackageVersionIsCompatible ( pythonPackageVersion ) ;
222
+ if ( ! packageVersionCompatible )
223
+ {
224
+ Debug . LogErrorFormat (
225
+ "Python package version ({0}) will produce model files that are incompatible with this " +
226
+ "version of the com.unity.ml-agents Unity package. Please downgrade to a Python package " +
227
+ "between {1} and {2}, or update to a new version of com.unity.ml-agents." ,
228
+ pythonPackageVersion ,
229
+ PythonTrainerVersions . s_MinSupportedVersion ,
230
+ PythonTrainerVersions . s_MaxSupportedVersion
231
+ ) ;
232
+ throw new UnityAgentsException ( "Incompatible trainer version." ) ;
233
+ }
234
+
235
+ var packageVersionSupported = CheckPythonPackageVersionIsSupported ( pythonPackageVersion ) ;
186
236
if ( ! packageVersionSupported )
187
237
{
188
238
Debug . LogWarningFormat (
0 commit comments