Skip to content

Commit baf109f

Browse files
committed
code review fixes
- use LINQ when possible - TryGetSourceCodeSearchID -> return string instead of bool
1 parent af4ad4c commit baf109f

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

Assets/com.unity.formats.fbx/Editor/Scripts/FbxExporterRepairMissingScripts.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ public class RepairMissingScripts
1616
private const string m_idFormat = "{{fileID: {0}, guid: {1}, type:";
1717

1818
private static List<string> m_searchIDsToReplace;
19-
private static string[] SearchIDsToReplace
19+
private static List<string> SearchIDsToReplace
2020
{
2121
get
2222
{
23-
if(m_searchIDsToReplace == null || m_searchIDsToReplace.Count <= 0)
23+
if (m_searchIDsToReplace == null || m_searchIDsToReplace.Count <= 0)
2424
{
25-
m_searchIDsToReplace = new List<string>();
26-
m_searchIDsToReplace.Add(string.Format(m_idFormat, m_fbxPrefabDLLFileId, m_forumPackageGUID));
27-
m_searchIDsToReplace.Add(string.Format(m_idFormat, m_fbxPrefabDLLFileId, m_assetStorePackageGUID));
25+
m_searchIDsToReplace = new List<string>() {
26+
string.Format(m_idFormat, m_fbxPrefabDLLFileId, m_forumPackageGUID),
27+
string.Format(m_idFormat, m_fbxPrefabDLLFileId, m_assetStorePackageGUID)
28+
};
2829
}
29-
return m_searchIDsToReplace.ToArray();
30+
return m_searchIDsToReplace;
3031
}
3132
}
3233

@@ -40,18 +41,17 @@ private string[] AssetsToRepair{
4041
}
4142
}
4243

43-
public static bool TryGetSourceCodeSearchID(out string searchID)
44+
public static string GetSourceCodeSearchID()
4445
{
4546
var fbxPrefabObj = AssetDatabase.LoadMainAssetAtPath(FbxExporters.FbxPrefabAutoUpdater.FindFbxPrefabAssetPath());
46-
searchID = null;
47+
string searchID = null;
4748
string guid;
4849
long fileId;
4950
if(AssetDatabase.TryGetGUIDAndLocalFileIdentifier(fbxPrefabObj, out guid, out fileId))
5051
{
5152
searchID = string.Format(m_idFormat, fileId, guid);
52-
return true;
5353
}
54-
return false;
54+
return searchID;
5555
}
5656

5757
public int GetAssetsToRepairCount(){
@@ -98,13 +98,10 @@ private static bool AssetNeedsRepair(string filePath)
9898
}
9999

100100
var contents = sr.ReadToEnd();
101-
foreach(var searchId in SearchIDsToReplace)
101+
if (SearchIDsToReplace.Exists(searchId => contents.Contains(searchId)))
102102
{
103-
if (contents.Contains(searchId))
104-
{
105-
sr.Close();
106-
return true;
107-
}
103+
sr.Close();
104+
return true;
108105
}
109106
}
110107
}
@@ -116,8 +113,8 @@ private static bool AssetNeedsRepair(string filePath)
116113

117114
public bool ReplaceGUIDInTextAssets ()
118115
{
119-
string sourceCodeSearchID;
120-
if(!TryGetSourceCodeSearchID(out sourceCodeSearchID))
116+
string sourceCodeSearchID = GetSourceCodeSearchID();
117+
if(string.IsNullOrEmpty(sourceCodeSearchID))
121118
{
122119
return false;
123120
}
@@ -131,6 +128,16 @@ public bool ReplaceGUIDInTextAssets ()
131128
return replacedGUID;
132129
}
133130

131+
private static bool ReplaceID(string searchId, string replacementId, ref string line)
132+
{
133+
if (line.Contains(searchId))
134+
{
135+
line = line.Replace(searchId, replacementId);
136+
return true;
137+
}
138+
return false;
139+
}
140+
134141
private static bool ReplaceGUIDInFile (string path, string replacementSearchID)
135142
{
136143
// try to read file, assume it's a text file for now
@@ -160,15 +167,9 @@ private static bool ReplaceGUIDInFile (string path, string replacementSearchID)
160167

161168
while (sr.Peek () > -1) {
162169
var line = sr.ReadLine ();
163-
164-
foreach(var searchId in SearchIDsToReplace)
165-
{
166-
if (line.Contains(searchId))
167-
{
168-
line = line.Replace(searchId, replacementSearchID);
169-
modified = true;
170-
}
171-
}
170+
SearchIDsToReplace.ForEach(searchId =>
171+
modified |= ReplaceID(searchId, replacementSearchID, ref line)
172+
);
172173

173174
sw.WriteLine (line);
174175
}

0 commit comments

Comments
 (0)