private static bool ReplaceGUIDInFile(string path, string replacementSearchID) { // try to read file, assume it's a text file for now bool modified = false; try { var tmpFile = Path.GetTempFileName(); if (string.IsNullOrEmpty(tmpFile)) { return(false); } using (var sr = new StreamReader(path)){ // verify that this is a text file var firstLine = ""; if (sr.Peek() > -1) { firstLine = sr.ReadLine(); if (!firstLine.StartsWith("%YAML")) { return(false); } } using (var sw = new StreamWriter(tmpFile, false)){ if (!string.IsNullOrEmpty(firstLine)) { sw.WriteLine(firstLine); } while (sr.Peek() > -1) { var line = sr.ReadLine(); SearchIDsToReplace.ForEach(searchId => modified |= ReplaceID(searchId, replacementSearchID, ref line) ); sw.WriteLine(line); } } } if (modified) { File.Delete(path); File.Move(tmpFile, path); Debug.LogFormat("Updated FbxPrefab components in file {0}", path); return(true); } else { File.Delete(tmpFile); } } catch (IOException e) { Debug.LogError(string.Format("Failed to replace GUID in file {0} (error={1})", path, e)); } return(false); }
private static bool AssetNeedsRepair(string filePath) { try{ using (var sr = new StreamReader(filePath)){ if (sr.Peek() > -1) { var firstLine = sr.ReadLine(); if (!firstLine.StartsWith("%YAML")) { return(false); } } var contents = sr.ReadToEnd(); if (SearchIDsToReplace.Exists(searchId => contents.Contains(searchId))) { return(true); } } } catch (IOException e) { Debug.LogError(string.Format("Failed to check file for component update: {0} (error={1})", filePath, e)); } return(false); }