1
+ using System . Collections ;
2
+ using System . Collections . Generic ;
3
+ using UnityEngine ;
4
+ using UnityEditor ;
5
+ using System . IO ;
6
+
7
+ namespace FbxExporters . Editor
8
+ {
9
+ public class RepairMissingScripts
10
+ {
11
+ private const string m_forumPackageGUID = "2d81c55c4d9d85146b1d2de96e084b63" ;
12
+ private const string m_currentPackageGUID = "628ffbda3fdf4df4588770785d91a698" ;
13
+
14
+ private const string m_fbxPrefabDLLFileId = "69888640" ;
15
+
16
+ private const string m_idFormat = "{{fileID: {0}, guid: {1}, type:" ;
17
+
18
+
19
+ private static string m_forumPackageSearchID ;
20
+
21
+ private static string ForumPackageSearchID {
22
+ get {
23
+ if ( string . IsNullOrEmpty ( m_forumPackageSearchID ) ) {
24
+ m_forumPackageSearchID = string . Format ( m_idFormat , m_fbxPrefabDLLFileId , m_forumPackageGUID ) ;
25
+ }
26
+ return m_forumPackageSearchID ;
27
+ }
28
+ }
29
+
30
+ private static string m_currentPackageSearchID ;
31
+
32
+ private static string CurrentPackageSearchID {
33
+ get {
34
+ if ( string . IsNullOrEmpty ( m_currentPackageSearchID ) ) {
35
+ m_currentPackageSearchID = string . Format ( m_idFormat , m_fbxPrefabDLLFileId , m_currentPackageGUID ) ;
36
+ }
37
+ return m_currentPackageSearchID ;
38
+ }
39
+ }
40
+
41
+ [ MenuItem ( "File/Repair Missing Scripts" , false ) ]
42
+ public static void Menu ( )
43
+ {
44
+ ReplaceGUIDInTextAssets ( ) ;
45
+ }
46
+
47
+ private static bool ReplaceGUIDInTextAssets ( )
48
+ {
49
+ // search project for assets containing old GUID
50
+
51
+ // ignore if forced binary
52
+ if ( UnityEditor . EditorSettings . serializationMode == SerializationMode . ForceBinary ) {
53
+ return false ;
54
+ }
55
+
56
+ // check all scenes and prefabs
57
+ string [ ] searchFilePatterns = new string [ ] { "*.prefab" , "*.unity" } ;
58
+
59
+ bool replacedGUID = false ;
60
+ foreach ( string searchPattern in searchFilePatterns ) {
61
+ foreach ( string file in Directory . GetFiles ( Application . dataPath , searchPattern , SearchOption . AllDirectories ) ) {
62
+ replacedGUID |= ReplaceGUIDInFile ( file ) ;
63
+ }
64
+ }
65
+ if ( replacedGUID ) {
66
+ AssetDatabase . Refresh ( ) ;
67
+ }
68
+ return replacedGUID ;
69
+ }
70
+
71
+ private static bool ReplaceGUIDInFile ( string path )
72
+ {
73
+ // try to read file, assume it's a text file for now
74
+ int modified = 0 ;
75
+
76
+ try {
77
+ var sr = new StreamReader ( path ) ;
78
+ var sw = new StreamWriter ( path + ".remap" , false ) ;
79
+
80
+ while ( sr . Peek ( ) > - 1 ) {
81
+ var line = sr . ReadLine ( ) ;
82
+
83
+ if ( line . Contains ( ForumPackageSearchID ) ) {
84
+ modified ++ ;
85
+ line = line . Replace ( ForumPackageSearchID , CurrentPackageSearchID ) ;
86
+ }
87
+
88
+ sw . WriteLine ( line ) ;
89
+ }
90
+
91
+ sr . Close ( ) ;
92
+ sw . Close ( ) ;
93
+
94
+ if ( modified > 0 ) {
95
+ File . Delete ( path ) ;
96
+ File . Move ( path + ".remap" , path ) ;
97
+ return true ;
98
+ } else {
99
+ File . Delete ( path + ".remap" ) ;
100
+ }
101
+ } catch ( IOException e ) {
102
+ Debug . LogError ( string . Format ( "Failed to replace GUID in file {0} (error={1})" , path , e ) ) ;
103
+ }
104
+
105
+ return false ;
106
+ }
107
+ }
108
+ }
0 commit comments