Skip to content

Commit bcd9452

Browse files
author
AJubrey
committed
[ADDED] unit test for this function
[CHANGED] the name of this function [ADDED] function to deal with ties for best default program [CHANGED] the function to be more modular overall
1 parent 19d4241 commit bcd9452

File tree

2 files changed

+76
-52
lines changed

2 files changed

+76
-52
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 56 additions & 52 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.FindMostRecentProgram();
101+
exportSettings.selectedDCCApp = exportSettings.FindPreferredProgram();
102102
}
103103

104104
int oldValue = exportSettings.selectedDCCApp;
@@ -226,6 +226,7 @@ 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" };
229230

230231
/// <summary>
231232
/// The path where all the different versions of Maya are installed
@@ -265,10 +266,10 @@ public static string kDefaultAdskRoot {
265266

266267
// List of names in order that they appear in option list
267268
[SerializeField]
268-
private List<string> dccOptionNames;
269+
public List<string> dccOptionNames;
269270
// List of paths in order that they appear in the option list
270271
[SerializeField]
271-
private List<string> dccOptionPaths;
272+
public List<string> dccOptionPaths;
272273

273274
protected override void LoadDefaults()
274275
{
@@ -316,84 +317,87 @@ private static string GetUniqueName(string name){
316317
/// Find the latest program available and make that the default choice.
317318
/// Will always take any Maya version over any 3ds Max version.
318319
///
319-
/// Returns the position of the most recent program in the list of dccOptionPaths
320+
/// Returns the index of the most recent program in the list of dccOptionPaths
320321
///
321322
/// </summary>
322-
public int FindMostRecentProgram()
323+
public int FindPreferredProgram()
323324
{
324-
int newestMayaVersion = -1;
325-
int savedMayaVersionNumber = 0;
326-
int newestMaxVersion = -1;
327-
int savedMaxVersionNumber = 0;
328325

329-
for (int i = 0; i < instance.dccOptionPaths.Count; i++)
326+
int newestDCCVersionIndex = 0;
327+
int newestDCCVersionNumber = 0;
328+
329+
for (int i = 0; i < dccOptionPaths.Count; i++)
330330
{
331-
if (instance.dccOptionPaths[i].ToLower().Contains("maya"))
331+
int versionToCheck = FindDCCVersion(dccOptionNames[i]);
332+
if (versionToCheck > newestDCCVersionNumber)
332333
{
333-
if (newestMayaVersion == -1)
334-
{
335-
newestMayaVersion = 0;
336-
savedMayaVersionNumber = FindDCCVersion(dccOptionNames[i]);
337-
continue;
338-
}
339-
340-
//Check if the path we are considering is newer than the previously saved one
341-
int versionToCheck = FindDCCVersion(dccOptionNames[i]);
342-
if (versionToCheck > savedMayaVersionNumber)
343-
{
344-
newestMayaVersion = i;
345-
savedMayaVersionNumber = versionToCheck;
346-
}
334+
newestDCCVersionIndex = i;
335+
newestDCCVersionNumber = versionToCheck;
347336
}
348-
else if (newestMayaVersion == -1 && instance.dccOptionPaths[i].ToLower().Contains("max"))
337+
else if (versionToCheck == newestDCCVersionNumber)
349338
{
350-
if (newestMaxVersion == -1)
339+
string selection = PickPrefferedName(dccOptionNames[newestDCCVersionIndex], dccOptionNames[i]);
340+
if (selection == dccOptionNames[i])
351341
{
352-
newestMaxVersion = 0;
353-
savedMaxVersionNumber = FindDCCVersion(dccOptionNames[newestMaxVersion]);
354-
continue;
342+
newestDCCVersionIndex = i;
343+
newestDCCVersionNumber = FindDCCVersion(dccOptionNames[i]);
355344
}
356-
357-
//Check if the path we are considering is newer than the previously saved one
358-
int versionToCheck = FindDCCVersion(dccOptionNames[i]);
359-
if (versionToCheck > savedMaxVersionNumber)
360-
{
361-
newestMaxVersion = i;
362-
savedMaxVersionNumber = versionToCheck;
363-
}
364-
365345
}
366-
367346
}
347+
Debug.Assert(newestDCCVersionIndex > -1 && newestDCCVersionIndex < dccOptionPaths.Count);
348+
return newestDCCVersionIndex;
349+
}
350+
/// <summary>
351+
/// Gives our preffered program name from two options. ACCEPTS ENTRIES FROM DCCOptionNames LIST
352+
/// </summary>
353+
/// <param name="optionA"></param>
354+
/// <param name="optionB"></param>
355+
/// <returns></returns>
356+
private string PickPrefferedName(string optionA, string optionB)
357+
{
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];
368362

369-
//We prefer to use the latest Maya version, if one exists
370-
if (newestMayaVersion != -1)
363+
for (int i = 0; i < 2; i++)
371364
{
372-
return newestMayaVersion;
373-
}
374-
else if (newestMaxVersion != -1)
375-
{
376-
return newestMaxVersion;
365+
for (int j = 0; j < preferenceList.Count; j++)
366+
{
367+
if (optionArray[i] == preferenceList[j])
368+
{
369+
optionScores[i] = j;
370+
}
371+
}
377372
}
378373

379-
return 0;
374+
return optionScores[0] < optionScores[1] ? optionA : optionB;
380375
}
381376

382377
/// <summary>
383-
/// Finds the 3ds Max version based off of the title of the application
378+
/// Finds the version based off of the title of the application
384379
/// </summary>
385380
/// <param name="path"></param>
386381
/// <returns> the year/version OR -1 if the year could not be parsed </returns>
387382
private static int FindDCCVersion(string AppName)
388383
{
389384
int version;
385+
string[] piecesArray = AppName.Split(' ');
386+
//Get the number, which is always the last chunk separated by a space.
387+
string number = piecesArray[piecesArray.Length - 1];
390388

391-
if (int.TryParse(AppName, out version))
389+
if (int.TryParse(number, out version))
392390
{
393391
return version;
394392
}
395393
else
396394
{
395+
float fVersion;
396+
//In case we are looking at a Blender version- the int parse will fail so we'll need to parse it as a float.
397+
if (float.TryParse(number, out fVersion))
398+
{
399+
return (int)fVersion;
400+
}
397401
return -1;
398402
}
399403
}
@@ -448,7 +452,7 @@ private static void FindDCCInstalls() {
448452
dccOptionName.Add (GetUniqueName ("3ds Max " + version));
449453
}
450454
}
451-
instance.selectedDCCApp = instance.FindMostRecentProgram();
455+
instance.selectedDCCApp = instance.FindPreferredProgram();
452456
}
453457

454458
/// <summary>
@@ -495,7 +499,7 @@ public static GUIContent[] GetDCCOptions(){
495499
var dccPath = instance.dccOptionPaths [i];
496500
if (!File.Exists (dccPath)) {
497501
if (i == instance.selectedDCCApp) {
498-
instance.selectedDCCApp = instance.FindMostRecentProgram();
502+
instance.selectedDCCApp = instance.FindPreferredProgram();
499503
}
500504
namesToDelete.Add (instance.dccOptionNames [i]);
501505
pathsToDelete.Add (dccPath);

Assets/FbxExporters/Editor/UnitTests/FbxExportSettingsTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,25 @@ public void TestGetSetFields()
178178
var platformPath = Path.Combine("a", Path.Combine("b", "c"));
179179
Assert.AreEqual(Path.Combine(appDataPath, platformPath), ExportSettings.GetAbsoluteSavePath());
180180
}
181+
182+
[Test]
183+
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();
190+
//While Maya 2017 and 3ds Max 2017 are tied for most recent, Maya 2017 should win because we prefer Maya.
191+
Assert.AreEqual(preferred, 2);
192+
193+
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" };
195+
196+
preferred = ExportSettings.instance.FindPreferredProgram();
197+
//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.
198+
Assert.AreEqual(preferred, 1);
199+
}
200+
181201
}
182202
}

0 commit comments

Comments
 (0)