3
3
using UnityEditor ;
4
4
using System . IO ;
5
5
6
- namespace Lachee . Tools . Editor
6
+ namespace Lachee . Tools . Editor
7
7
{
8
8
/// <summary>
9
9
/// Provides tools to convert line endings
10
10
/// </summary>
11
- public class EOLConversion
11
+ public class EOLConversion : UnityEditor . AssetModificationProcessor
12
12
{
13
+ public const string PREFS_PREFERED = "prefered_eol" ;
14
+ public const string PREFS_PROCESS = "process_eol" ;
15
+
13
16
[ MenuItem ( "Tools/EOL Conversion/Windows" ) ]
14
17
private static void ConvertToWindows ( )
15
18
{
19
+ EditorPrefs . SetString ( PREFS_PREFERED , "\r \n " ) ;
16
20
Convert ( "\r \n " ) ;
17
21
}
18
22
19
-
23
+
20
24
[ MenuItem ( "Tools/EOL Conversion/Unix" ) ]
21
25
private static void ConvertToUnix ( )
22
26
{
27
+ EditorPrefs . SetString ( PREFS_PREFERED , "\n " ) ;
23
28
Convert ( "\n " ) ;
24
29
}
25
30
31
+ [ MenuItem ( "Tools/EOL Conversion/Automaticly Process" ) ]
32
+ private static void ToggleProcessing ( )
33
+ {
34
+ bool auto = EditorPrefs . GetBool ( PREFS_PROCESS , true ) ;
35
+ EditorPrefs . SetBool ( PREFS_PROCESS , ! auto ) ;
36
+ }
37
+
38
+ [ MenuItem ( "Tools/EOL Conversion/Automaticly Process" , true ) ]
39
+ private static bool ToogleProcessingValidation ( )
40
+ {
41
+ bool auto = EditorPrefs . GetBool ( PREFS_PROCESS , true ) ;
42
+ Menu . SetChecked ( "Tools/EOL Conversion/Automaticly Process" , auto ) ;
43
+ return true ;
44
+ }
45
+
46
+ /// <summary>
47
+ /// This gets called for every .meta file created by the Editor.
48
+ /// </summary>
49
+ public static void OnWillCreateAsset ( string path )
50
+ {
51
+ bool auto = EditorPrefs . GetBool ( PREFS_PROCESS , true ) ;
52
+ if ( ! auto ) return ;
53
+
54
+ path = path . Replace ( ".meta" , string . Empty ) ;
55
+ if ( ! path . EndsWith ( ".cs" ) )
56
+ return ;
57
+
58
+ if ( ! EditorPrefs . HasKey ( PREFS_PREFERED ) )
59
+ return ;
60
+
61
+ string lineEnding = EditorPrefs . GetString ( PREFS_PREFERED ) ;
62
+ if ( ConvertFile ( path , lineEnding ) )
63
+ AssetDatabase . Refresh ( ) ;
64
+ }
65
+
26
66
/// <summary> Converts all the assets and returns a list of files that were modified </summary>
27
- public static string [ ] Convert ( string lineEnding ) {
67
+ public static string [ ] Convert ( string lineEnding )
68
+ {
28
69
List < string > assetsConverted = new List < string > ( ) ;
29
70
string [ ] assetPaths = AssetDatabase . GetAllAssetPaths ( ) ;
30
71
int progress = 0 ;
31
72
32
- foreach ( string assetPath in assetPaths )
73
+ foreach ( string assetPath in assetPaths )
33
74
{
34
75
EditorUtility . DisplayProgressBar ( "Converting Line Ending" , assetPath , ( progress ++ / ( float ) assetPaths . Length ) ) ;
76
+ if ( ConvertFile ( assetPath , lineEnding ) )
77
+ assetsConverted . Add ( assetPath ) ;
78
+ }
79
+
80
+ EditorUtility . ClearProgressBar ( ) ;
81
+ return assetsConverted . ToArray ( ) ;
82
+ }
35
83
36
- if ( ! assetPath . EndsWith ( ".cs" ) ) continue ;
37
- if ( assetPath . StartsWith ( "Packages/" ) ) continue ;
84
+ /// <summary>Converts a single file's line ending</summary>
85
+ public static bool ConvertFile ( string path )
86
+ => ConvertFile ( path , EditorPrefs . GetString ( PREFS_PREFERED , "\r \n " ) ) ;
38
87
88
+ /// <summary>Converts a single file's line ending</summary>
89
+ public static bool ConvertFile ( string path , string lineEnding )
90
+ {
91
+ if ( ! path . EndsWith ( ".cs" ) || path . StartsWith ( "Packages/" ) )
92
+ return false ;
39
93
40
- string content = File . ReadAllText ( assetPath ) ;
41
- string contentNew = Regex . Replace ( content , @"\r\n|\n\r|\n|\r" , lineEnding ) ;
94
+ string content = File . ReadAllText ( path ) ;
95
+ string contentNew = Regex . Replace ( content , @"\r\n|\n\r|\n|\r" , lineEnding ) ;
42
96
43
- if ( content != contentNew ) {
44
- File . WriteAllText ( assetPath , contentNew ) ;
45
- assetsConverted . Add ( assetPath ) ;
46
- }
97
+ if ( content != contentNew )
98
+ {
99
+ File . WriteAllText ( path , contentNew ) ;
100
+ return true ;
47
101
}
48
-
49
- EditorUtility . ClearProgressBar ( ) ;
50
- return assetsConverted . ToArray ( ) ;
102
+
103
+ return false ;
51
104
}
52
105
}
53
-
54
106
}
0 commit comments