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