Skip to content

Commit b055a2f

Browse files
committed
fix ConvertToModel.cs warnings
- exception too general - argument not validated
1 parent 4ef3353 commit b055a2f

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

Packages/com.unity.formats.fbx/Editor/Scripts/ConvertToModel.cs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,32 @@
33
using UnityEngine;
44
using System.Linq;
55
using UnityEngine.Formats.Fbx.Exporter;
6+
using System.Runtime.Serialization;
67

78
namespace UnityEditor.Formats.Fbx.Exporter
89
{
10+
public class ConvertToLinkedPrefabException : System.Exception
11+
{
12+
public ConvertToLinkedPrefabException()
13+
{
14+
}
15+
16+
public ConvertToLinkedPrefabException(string message)
17+
: base(message)
18+
{
19+
}
20+
21+
public ConvertToLinkedPrefabException(string message, System.Exception inner)
22+
: base(message, inner)
23+
{
24+
}
25+
26+
protected ConvertToLinkedPrefabException(SerializationInfo info, StreamingContext context)
27+
: base(info, context)
28+
{
29+
}
30+
}
31+
932
public static class ConvertToModel
1033
{
1134
const string GameObjectMenuItemName = "GameObject/Convert To Linked Prefab Instance...";
@@ -149,6 +172,11 @@ public static GameObject Convert (
149172
string prefabFullPath = null,
150173
ConvertToPrefabSettingsSerialize exportOptions = null)
151174
{
175+
if (toConvert == null)
176+
{
177+
throw new System.ArgumentNullException("toConvert");
178+
}
179+
152180
// If we selected the something that's already backed by an
153181
// FBX, don't export.
154182
var mainAsset = GetOrCreateFbxAsset (toConvert, fbxDirectoryFullPath, fbxFullPath, exportOptions);
@@ -214,11 +242,16 @@ public static bool WillExportFbx(GameObject toConvert) {
214242
/// file. Overrides fbxDirectoryFullPath. Ignored if toConvert is an
215243
/// fbx asset or an instance of an fbx.</param>
216244
/// <returns>The root of a model prefab asset.</returns>
217-
public static GameObject GetOrCreateFbxAsset(GameObject toConvert,
245+
internal static GameObject GetOrCreateFbxAsset(GameObject toConvert,
218246
string fbxDirectoryFullPath = null,
219247
string fbxFullPath = null,
220248
ConvertToPrefabSettingsSerialize exportOptions = null)
221249
{
250+
if(toConvert == null)
251+
{
252+
throw new System.ArgumentNullException("toConvert");
253+
}
254+
222255
var mainAsset = GetFbxAssetOrNull(toConvert);
223256
if (mainAsset) {
224257
return mainAsset;
@@ -253,15 +286,15 @@ public static GameObject GetOrCreateFbxAsset(GameObject toConvert,
253286
exportOptions != null ? exportOptions : new ConvertToPrefabSettingsSerialize()
254287
);
255288
if (fbxActualPath != fbxFullPath) {
256-
throw new System.Exception ("Failed to convert " + toConvert.name);
289+
throw new ConvertToLinkedPrefabException("Failed to convert " + toConvert.name);
257290
}
258291
}
259292

260293
// Replace w Model asset. LoadMainAssetAtPath wants a path
261294
// relative to the project, not relative to the assets folder.
262295
var unityMainAsset = AssetDatabase.LoadMainAssetAtPath (projectRelativePath) as GameObject;
263296
if (!unityMainAsset) {
264-
throw new System.Exception ("Failed to convert " + toConvert.name);
297+
throw new ConvertToLinkedPrefabException ("Failed to convert " + toConvert.name);
265298
}
266299

267300
// Copy the mesh/materials from the FBX
@@ -327,6 +360,11 @@ public static GameObject ApplyOrCreatePrefab(GameObject instance,
327360
string prefabDirectoryFullPath = null,
328361
string prefabFullPath = null)
329362
{
363+
if(instance == null)
364+
{
365+
throw new System.ArgumentNullException("instance");
366+
}
367+
330368
if(PrefabUtility.GetPrefabType(instance) == PrefabType.PrefabInstance) {
331369
return PrefabUtility.ReplacePrefab(instance, PrefabUtility.GetCorrespondingObjectFromSource(instance));
332370
}
@@ -351,7 +389,7 @@ public static GameObject ApplyOrCreatePrefab(GameObject instance,
351389

352390
var prefab = PrefabUtility.CreatePrefab(prefabFileName, instance, ReplacePrefabOptions.ConnectToPrefab);
353391
if (!prefab) {
354-
throw new System.Exception(string.Format("Failed to create prefab asset in [{0}]", prefabFileName));
392+
throw new ConvertToLinkedPrefabException(string.Format("Failed to create prefab asset in [{0}]", prefabFileName));
355393
}
356394
return prefab;
357395
}
@@ -365,6 +403,11 @@ public static GameObject ApplyOrCreatePrefab(GameObject instance,
365403
/// <param name="unityMainAsset">Main asset in the FBX.</param>
366404
public static void SetupFbxPrefab(GameObject toSetUp, GameObject unityMainAsset)
367405
{
406+
if(toSetUp == null)
407+
{
408+
throw new System.ArgumentNullException("toSetUp");
409+
}
410+
368411
// Set up the FbxPrefab component so it will auto-update.
369412
// Make sure to delete whatever FbxPrefab history we had.
370413
var fbxPrefab = toSetUp.GetComponent<FbxPrefab>();
@@ -385,7 +428,7 @@ public static void SetupFbxPrefab(GameObject toSetUp, GameObject unityMainAsset)
385428
/// </summary>
386429
/// <returns>The root of a model prefab asset, or null.</returns>
387430
/// <param name="go">A gameobject either in the scene or in the assets folder.</param>
388-
public static GameObject GetFbxAssetOrNull(GameObject go) {
431+
internal static GameObject GetFbxAssetOrNull(GameObject go) {
389432
// Children of model prefab instances will also have "model prefab instance"
390433
// as their prefab type, so it is important that it is the root that is selected.
391434
//
@@ -497,7 +540,7 @@ public static void EnforceUniqueNames(IEnumerable<GameObject> exportSet)
497540
/// </summary>
498541
/// <param name="dest">GameObject to update.</param>
499542
/// <param name="source">Source to update from.</param>
500-
public static void UpdateFromSourceRecursive(GameObject dest, GameObject source)
543+
internal static void UpdateFromSourceRecursive(GameObject dest, GameObject source)
501544
{
502545
// recurse over orig, for each transform finding the corresponding transform in the FBX
503546
// and copying the meshes and materials over from the FBX
@@ -524,7 +567,7 @@ public static void UpdateFromSourceRecursive(GameObject dest, GameObject source)
524567
/// <returns>Dictionary containing the name to source game object.</returns>
525568
/// <param name="dest">Destination GameObject.</param>
526569
/// <param name="source">Source GameObject.</param>
527-
public static Dictionary<string,GameObject> MapNameToSourceRecursive(GameObject dest, GameObject source){
570+
internal static Dictionary<string,GameObject> MapNameToSourceRecursive(GameObject dest, GameObject source){
528571
var nameToGO = new Dictionary<string,GameObject> ();
529572

530573
var q = new Queue<Transform> ();
@@ -569,7 +612,7 @@ public static Dictionary<string,GameObject> MapNameToSourceRecursive(GameObject
569612
///
570613
/// The 'from' hierarchy is not modified.
571614
/// </summary>
572-
public static void CopyComponents(GameObject to, GameObject from){
615+
internal static void CopyComponents(GameObject to, GameObject from){
573616
var originalComponents = new List<Component>(to.GetComponents<Component> ());
574617

575618
// UNI-27534: This fixes the issue where the mesh collider would not update to point to the mesh in the fbx after export

0 commit comments

Comments
 (0)