Skip to content

Commit 8b62476

Browse files
author
AJubrey
committed
[FIXED] the unit test to be more comprehensive
[FIXED] the way ties are decided to be more quick [ADDED] a helper function to get the first part of a string, for comparison purposes
1 parent bcd9452 commit 8b62476

File tree

2 files changed

+69
-41
lines changed

2 files changed

+69
-41
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public override void OnInspectorGUI() {
9898
var options = ExportSettings.GetDCCOptions();
9999
// make sure we never initially have browse selected
100100
if (exportSettings.selectedDCCApp == options.Length - 1) {
101-
exportSettings.selectedDCCApp = exportSettings.FindPreferredProgram();
101+
exportSettings.selectedDCCApp = exportSettings.GetPreferredDCCApp();
102102
}
103103

104104
int oldValue = exportSettings.selectedDCCApp;
@@ -226,7 +226,9 @@ private static string TryFindDCC(string dccPath, string ext, ExportSettings.DCCT
226226
public class ExportSettings : ScriptableSingleton<ExportSettings>
227227
{
228228
public const string kDefaultSavePath = ".";
229-
private static List<string> preferenceList = new List<string>() {"maya", "mayalt", "3ds", "blender" };
229+
private static List<string> s_PreferenceList = new List<string>() {"maya", "mayalt", "3ds", "blender" };
230+
public static string s_MaxName = "3ds Max ";
231+
public static string s_MayaName = "Maya ";
230232

231233
/// <summary>
232234
/// The path where all the different versions of Maya are installed
@@ -285,7 +287,7 @@ protected override void LoadDefaults()
285287
/// </summary>
286288
/// <returns>The unique name.</returns>
287289
/// <param name="name">Name.</param>
288-
private static string GetUniqueName(string name){
290+
public static string GetUniqueName(string name){
289291
if (!instance.dccOptionNames.Contains(name)) {
290292
return name;
291293
}
@@ -307,7 +309,7 @@ private static string GetUniqueName(string name){
307309
do {
308310
uniqueName = string.Format (format, index, name);
309311
index++;
310-
} while (instance.dccOptionNames.Contains(name));
312+
} while (instance.dccOptionNames.Contains(uniqueName));
311313

312314
return uniqueName;
313315
}
@@ -317,16 +319,20 @@ private static string GetUniqueName(string name){
317319
/// Find the latest program available and make that the default choice.
318320
/// Will always take any Maya version over any 3ds Max version.
319321
///
320-
/// Returns the index of the most recent program in the list of dccOptionPaths
322+
/// Returns the index of the most recent program in the list of dccOptionNames
321323
///
322324
/// </summary>
323-
public int FindPreferredProgram()
325+
public int GetPreferredDCCApp()
324326
{
327+
if (dccOptionNames == null)
328+
{
329+
return -1;
330+
}
325331

326-
int newestDCCVersionIndex = 0;
327-
int newestDCCVersionNumber = 0;
332+
int newestDCCVersionIndex = -1;
333+
int newestDCCVersionNumber = -1;
328334

329-
for (int i = 0; i < dccOptionPaths.Count; i++)
335+
for (int i = 0; i < dccOptionNames.Count; i++)
330336
{
331337
int versionToCheck = FindDCCVersion(dccOptionNames[i]);
332338
if (versionToCheck > newestDCCVersionNumber)
@@ -336,42 +342,36 @@ public int FindPreferredProgram()
336342
}
337343
else if (versionToCheck == newestDCCVersionNumber)
338344
{
339-
string selection = PickPrefferedName(dccOptionNames[newestDCCVersionIndex], dccOptionNames[i]);
340-
if (selection == dccOptionNames[i])
345+
int selection = ChoosePreferredDCCApp(newestDCCVersionIndex, i);
346+
if (selection == i)
341347
{
342348
newestDCCVersionIndex = i;
343349
newestDCCVersionNumber = FindDCCVersion(dccOptionNames[i]);
344350
}
345351
}
346352
}
347-
Debug.Assert(newestDCCVersionIndex > -1 && newestDCCVersionIndex < dccOptionPaths.Count);
353+
Debug.Assert(newestDCCVersionIndex >= -1 && newestDCCVersionIndex < dccOptionNames.Count);
348354
return newestDCCVersionIndex;
349355
}
350356
/// <summary>
351-
/// Gives our preffered program name from two options. ACCEPTS ENTRIES FROM DCCOptionNames LIST
357+
/// Takes the index of two program names from dccOptionNames and chooses our preferred one based on the preference list
358+
/// This happens in case of a tie between two programs with the same release year / version
352359
/// </summary>
353360
/// <param name="optionA"></param>
354361
/// <param name="optionB"></param>
355362
/// <returns></returns>
356-
private string PickPrefferedName(string optionA, string optionB)
363+
private int ChoosePreferredDCCApp(int optionA, int optionB)
357364
{
358-
string[] optionArray = new string[2];
359-
optionArray[0] = optionA.ToLower().Split(' ')[0];
360-
optionArray[1] = optionB.ToLower().Split(' ')[0];
361-
int[] optionScores = new int[2];
365+
int scoreA = s_PreferenceList.IndexOf(GetAppNameFromFolderName(dccOptionNames[optionA]));
366+
int scoreB = s_PreferenceList.IndexOf(GetAppNameFromFolderName(dccOptionNames[optionB]));
362367

363-
for (int i = 0; i < 2; i++)
364-
{
365-
for (int j = 0; j < preferenceList.Count; j++)
366-
{
367-
if (optionArray[i] == preferenceList[j])
368-
{
369-
optionScores[i] = j;
370-
}
371-
}
372-
}
368+
return scoreA < scoreB ? optionA : optionB;
369+
}
373370

374-
return optionScores[0] < optionScores[1] ? optionA : optionB;
371+
///
372+
private string GetAppNameFromFolderName(string originalString)
373+
{
374+
return originalString.ToLower().Split(' ')[0];
375375
}
376376

377377
/// <summary>
@@ -381,8 +381,17 @@ private string PickPrefferedName(string optionA, string optionB)
381381
/// <returns> the year/version OR -1 if the year could not be parsed </returns>
382382
private static int FindDCCVersion(string AppName)
383383
{
384+
if (AppName == null)
385+
{
386+
return -1;
387+
}
388+
384389
int version;
385390
string[] piecesArray = AppName.Split(' ');
391+
if (piecesArray.Length == 0)
392+
{
393+
return -1;
394+
}
386395
//Get the number, which is always the last chunk separated by a space.
387396
string number = piecesArray[piecesArray.Length - 1];
388397

@@ -439,7 +448,7 @@ private static void FindDCCInstalls() {
439448
}
440449
string version = product.Substring ("maya".Length);
441450
dccOptionPath.Add (GetMayaExePath (productDir.FullName.Replace ("\\", "/")));
442-
dccOptionName.Add (GetUniqueName ("Maya " + version));
451+
dccOptionName.Add (GetUniqueName (s_MayaName + version));
443452
}
444453

445454
if (product.StartsWith ("3ds max", StringComparison.InvariantCultureIgnoreCase)) {
@@ -449,10 +458,10 @@ private static void FindDCCInstalls() {
449458
}
450459
string version = product.Substring ("3ds max ".Length);
451460
dccOptionPath.Add (exePath);
452-
dccOptionName.Add (GetUniqueName ("3ds Max " + version));
461+
dccOptionName.Add (GetUniqueName (s_MaxName + version));
453462
}
454463
}
455-
instance.selectedDCCApp = instance.FindPreferredProgram();
464+
instance.selectedDCCApp = instance.GetPreferredDCCApp();
456465
}
457466

458467
/// <summary>
@@ -499,7 +508,7 @@ public static GUIContent[] GetDCCOptions(){
499508
var dccPath = instance.dccOptionPaths [i];
500509
if (!File.Exists (dccPath)) {
501510
if (i == instance.selectedDCCApp) {
502-
instance.selectedDCCApp = instance.FindPreferredProgram();
511+
instance.selectedDCCApp = instance.GetPreferredDCCApp();
503512
}
504513
namesToDelete.Add (instance.dccOptionNames [i]);
505514
pathsToDelete.Add (dccPath);

Assets/FbxExporters/Editor/UnitTests/FbxExportSettingsTest.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,40 @@ public void TestGetSetFields()
181181

182182
[Test]
183183
public void TestFindPreferredProgram()
184-
{
185-
//Putting Maya 2017 and Max 2017 will also test our PickPreferredName function
186-
ExportSettings.instance.dccOptionNames = new List<string> { "3ds Max 2000", "Maya 2016", "Maya 2017", "3ds max 2017" };
187-
ExportSettings.instance.dccOptionPaths = new List<string> { "3ds Max 2000 path", "maya 2016 path", "maya 2017 path", "3ds max 2017 path"};
188-
189-
int preferred = ExportSettings.instance.FindPreferredProgram();
184+
{
185+
//Add a number of fake programs to the list, including some garbage ones
186+
ExportSettings.instance.dccOptionNames = new List<string>();
187+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName(ExportSettings.s_MaxName + "2000"));
188+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName(ExportSettings.s_MayaName + "2016"));
189+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName(ExportSettings.s_MayaName + "2017"));
190+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName(ExportSettings.s_MaxName + "2017"));
191+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName("Blender " + "2.67"));
192+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName(""));
193+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName(null));
194+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName(ExportSettings.s_MayaName + "lt"));
195+
ExportSettings.instance.dccOptionNames.Add(ExportSettings.GetUniqueName(ExportSettings.s_MayaName + "2017"));
196+
197+
int preferred = ExportSettings.instance.GetPreferredDCCApp();
190198
//While Maya 2017 and 3ds Max 2017 are tied for most recent, Maya 2017 should win because we prefer Maya.
191199
Assert.AreEqual(preferred, 2);
192200

193201
ExportSettings.instance.dccOptionNames = new List<string> { "Blender 2.67", "Blender 3.0" };
194-
ExportSettings.instance.dccOptionPaths = new List<string> { "Blender 2.67 path", "Blender 3.0 path" };
195202

196-
preferred = ExportSettings.instance.FindPreferredProgram();
203+
preferred = ExportSettings.instance.GetPreferredDCCApp();
197204
//The function should be able to deal with floats well enough to give us a preferred version of soemthing like blender, which does not use the year.
198205
Assert.AreEqual(preferred, 1);
206+
207+
ExportSettings.instance.dccOptionNames.Clear();
208+
//Try running it with an empty list
209+
preferred = ExportSettings.instance.GetPreferredDCCApp();
210+
211+
Assert.AreEqual(preferred, -1);
212+
213+
ExportSettings.instance.dccOptionNames = null;
214+
//Try running it with a null list
215+
preferred = ExportSettings.instance.GetPreferredDCCApp();
216+
217+
Assert.AreEqual(preferred, -1);
199218
}
200219

201220
}

0 commit comments

Comments
 (0)