1
+ using UnityEngine ;
2
+ using UnityEditor ;
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+
6
+ namespace FbxExporters
7
+ {
8
+ class Integrations
9
+ {
10
+ private const string MAYA_VERSION = "2017" ;
11
+ private const string MODULE_FILENAME = "unityoneclick.mod" ;
12
+ private const string PACKAGE_NAME = "FbxExporters" ;
13
+ private const string VERSION_FILENAME = "README.txt" ;
14
+ private const string VERSION_FIELD = "**Version**" ;
15
+ private const string VERSION_TAG = "{Version}" ;
16
+ private const string PROJECT_TAG = "{UnityProject}" ;
17
+
18
+ private static Char [ ] FIELD_SEPARATORS = new Char [ ] { ':' } ;
19
+
20
+ private const string MODULE_TEMPLATE_PATH = "Integrations/Autodesk/maya" + VERSION_TAG + "/unityoneclick.mod" ;
21
+
22
+ #if UNITY_EDITOR_OSX
23
+ private const string REL_MAYA_MODULES_PATH = "Library/Preferences/Autodesk/Maya/" + VERSION_TAG + "/modules" ;
24
+ #elif UNITY_EDITOR_LINUX
25
+ private const string REL_MAYA_MODULES_PATH = "Maya/" + VERSION_TAG + "/modules" ;
26
+ #else
27
+ private const string REL_MAYA_MODULES_PATH = "maya/" + VERSION_TAG + "/modules" ;
28
+ #endif
29
+
30
+ private static string GetUserFolder ( )
31
+ {
32
+ #if UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX
33
+ return System . Environment . GetEnvironmentVariable ( "HOME" ) ;
34
+ #else
35
+ return System . Environment . GetFolderPath ( System . Environment . SpecialFolder . Personal ) ;
36
+ #endif
37
+ }
38
+
39
+ private static string GetModulePath ( string version )
40
+ {
41
+ string result = System . IO . Path . Combine ( GetUserFolder ( ) , REL_MAYA_MODULES_PATH ) ;
42
+
43
+ return result . Replace ( VERSION_TAG , version ) ;
44
+ }
45
+
46
+ private static string GetModuleTemplatePath ( string version )
47
+ {
48
+ string result = System . IO . Path . Combine ( Application . dataPath , MODULE_TEMPLATE_PATH ) ;
49
+
50
+ return result . Replace ( VERSION_TAG , version ) ;
51
+ }
52
+
53
+ private static string GetProjectPath ( )
54
+ {
55
+ return System . IO . Directory . GetParent ( Application . dataPath ) . FullName ;
56
+ }
57
+
58
+ private static string GetPackagePath ( )
59
+ {
60
+ return System . IO . Path . Combine ( Application . dataPath , PACKAGE_NAME ) ;
61
+ }
62
+
63
+ private static string GetPackageVersion ( )
64
+ {
65
+ string result = null ;
66
+
67
+ try {
68
+ string FileName = System . IO . Path . Combine ( GetPackagePath ( ) , VERSION_FILENAME ) ;
69
+
70
+ System . IO . StreamReader sr = new System . IO . StreamReader ( FileName ) ;
71
+
72
+ // Read the first line of text
73
+ string line = sr . ReadLine ( ) ;
74
+
75
+ // Continue to read until you reach end of file
76
+ while ( line != null )
77
+ {
78
+ if ( line . StartsWith ( VERSION_FIELD ) )
79
+ {
80
+ string [ ] fields = line . Split ( FIELD_SEPARATORS ) ;
81
+
82
+ if ( fields . Length > 1 )
83
+ {
84
+ result = fields [ 1 ] ;
85
+ }
86
+ break ;
87
+ }
88
+ line = sr . ReadLine ( ) ;
89
+ }
90
+ }
91
+ catch ( Exception e )
92
+ {
93
+ Debug . LogError ( string . Format ( "Exception failed to read file containing package version ({0})" , e . Message ) ) ;
94
+ }
95
+
96
+ return result ;
97
+ }
98
+
99
+ private static List < string > ParseTemplateFile ( string FileName , Dictionary < string , string > Tokens )
100
+ {
101
+ List < string > lines = new List < string > ( ) ;
102
+
103
+ try
104
+ {
105
+ // Pass the file path and file name to the StreamReader constructor
106
+ System . IO . StreamReader sr = new System . IO . StreamReader ( FileName ) ;
107
+
108
+ // Read the first line of text
109
+ string line = sr . ReadLine ( ) ;
110
+
111
+ // Continue to read until you reach end of file
112
+ while ( line != null )
113
+ {
114
+ foreach ( KeyValuePair < string , string > entry in Tokens )
115
+ {
116
+ line = line . Replace ( entry . Key , entry . Value ) ;
117
+ }
118
+ lines . Add ( line ) ;
119
+
120
+ //Read the next line
121
+ line = sr . ReadLine ( ) ;
122
+ }
123
+
124
+ //close the file
125
+ sr . Close ( ) ;
126
+ }
127
+ catch ( Exception e )
128
+ {
129
+ Debug . LogError ( string . Format ( "Exception reading module file template ({0})" , e . Message ) ) ;
130
+ }
131
+
132
+ return lines ;
133
+ }
134
+
135
+ private static void WriteFile ( string FileName , List < string > Lines )
136
+ {
137
+ try
138
+ {
139
+ //Pass the filepath and filename to the StreamWriter Constructor
140
+ System . IO . StreamWriter sw = new System . IO . StreamWriter ( FileName ) ;
141
+
142
+ foreach ( string line in Lines )
143
+ {
144
+ //Write a line of text
145
+ sw . WriteLine ( line ) ;
146
+ }
147
+
148
+ //Close the file
149
+ sw . Close ( ) ;
150
+ }
151
+ catch ( Exception e )
152
+ {
153
+ Debug . LogError ( string . Format ( "Exception while writing module file ({0})" , e . Message ) ) ;
154
+ }
155
+ }
156
+
157
+ private static void _InstallMaya2017 ( bool verbose = true , bool commandsOnly = false )
158
+ {
159
+ // check if package installed
160
+ string moduleTemplatePath = GetModuleTemplatePath ( MAYA_VERSION ) ;
161
+
162
+ if ( ! System . IO . File . Exists ( moduleTemplatePath ) )
163
+ {
164
+ Debug . LogError ( string . Format ( "FbxExporters package not installed, please install first" ) ) ;
165
+ return ;
166
+ }
167
+
168
+ // TODO: detect maya2017 installation
169
+
170
+ // TODO: if not maya2017 installed warn user
171
+
172
+ // check for {USER} modules folder
173
+ string modulePath = GetModulePath ( MAYA_VERSION ) ;
174
+
175
+ string moduleFilePath = System . IO . Path . Combine ( modulePath , MODULE_FILENAME ) ;
176
+
177
+ bool installed = false ;
178
+
179
+ if ( ! System . IO . Directory . Exists ( modulePath ) )
180
+ {
181
+ if ( verbose ) Debug . Log ( string . Format ( "Creating Maya Modules Folder {0}" , modulePath ) ) ;
182
+
183
+ try
184
+ {
185
+ System . IO . Directory . CreateDirectory ( modulePath ) ;
186
+ }
187
+ catch
188
+ {
189
+ Debug . LogError ( string . Format ( "Failed to create Maya Modules Folder {0}" , modulePath ) ) ;
190
+ return ;
191
+ }
192
+
193
+ if ( ! System . IO . Directory . Exists ( modulePath ) ) {
194
+ Debug . LogError ( string . Format ( "Failed to create Maya Modules Folder {0}" , modulePath ) ) ;
195
+ return ;
196
+ }
197
+
198
+ installed = false ;
199
+ }
200
+ else
201
+ {
202
+ // detect if unityoneclick.mod is installed
203
+ installed = System . IO . File . Exists ( moduleFilePath ) ;
204
+
205
+ if ( installed )
206
+ {
207
+ // FIXME: remove this when we support parsing existing .mod files
208
+ try {
209
+ System . IO . File . Delete ( moduleFilePath ) ;
210
+ installed = false ;
211
+ }
212
+ catch
213
+ {
214
+ Debug . LogWarning ( string . Format ( "Failed to delete plugin module file {0}" , moduleFilePath ) ) ;
215
+ }
216
+ }
217
+ }
218
+
219
+ // if not installed
220
+ if ( ! installed )
221
+ {
222
+ Dictionary < string , string > Tokens = new Dictionary < string , string > ( )
223
+ {
224
+ { VERSION_TAG , GetPackageVersion ( ) } ,
225
+ { PROJECT_TAG , GetProjectPath ( ) }
226
+ } ;
227
+
228
+ // parse template, replace "{UnityProject}" with project path
229
+ List < string > lines = ParseTemplateFile ( moduleTemplatePath , Tokens ) ;
230
+
231
+ if ( verbose ) Debug . Log ( string . Format ( "Copying plugin module file to {0}" , moduleFilePath ) ) ;
232
+
233
+ // write out .mod file
234
+ WriteFile ( moduleFilePath , lines ) ;
235
+ }
236
+ else
237
+ {
238
+ throw new NotImplementedException ( ) ;
239
+
240
+ // TODO: parse installed .mod file
241
+
242
+ // TODO: if maya version not installed add
243
+
244
+ // TODO: else check installation path
245
+
246
+ // TODO: if installation path different
247
+
248
+ // TODO: print message package already installed else where
249
+ }
250
+
251
+ if ( commandsOnly )
252
+ throw new NotImplementedException ( ) ;
253
+
254
+ // TODO: configure maya to auto-load plugin on next startup
255
+
256
+ }
257
+
258
+ public static void InstallMaya2017 ( )
259
+ {
260
+ bool verbose = true ;
261
+
262
+ Debug . Log ( string . Format ( "Installing Maya {0} Integration" , MAYA_VERSION ) ) ;
263
+
264
+ _InstallMaya2017 ( verbose ) ;
265
+
266
+ if ( verbose ) Debug . Log ( string . Format ( "Finished installing Maya {0} Integration." , MAYA_VERSION ) ) ;
267
+ }
268
+
269
+ public static void InstallMaya2017CommandsOnly ( )
270
+ {
271
+ bool verbose = true ;
272
+
273
+ Debug . Log ( string . Format ( "Installing Maya {0} Integration Commands Only" , MAYA_VERSION ) ) ;
274
+
275
+ _InstallMaya2017 ( verbose , true ) ;
276
+
277
+ if ( verbose ) Debug . Log ( string . Format ( "Finished installing Maya {0} Integration Commands Only." , MAYA_VERSION ) ) ;
278
+ }
279
+ }
280
+
281
+ namespace Editors
282
+ {
283
+ class IntegrationsUI
284
+ {
285
+ const string MenuItemName = "FbxExporters/Install Maya2017 Integration" ;
286
+
287
+ [ MenuItem ( MenuItemName , false , 0 ) ]
288
+ public static void OnMenuItem ( )
289
+ {
290
+ Integrations . InstallMaya2017 ( ) ;
291
+ }
292
+ }
293
+ }
294
+ }
0 commit comments