Rev 189 | Rev 194 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | toby | 1 | /* |
18 | toby | 2 | This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop |
192 | daniel-mar | 3 | Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au |
4 | Copyright (C) 2018-2019 Daniel Marschall, ViaThinkSoft |
||
2 | toby | 5 | |
6 | This program is free software; you can redistribute it and/or modify |
||
171 | dmarschall | 7 | it under the terms of the GNU General Public License as published by |
2 | toby | 8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
||
10 | |||
11 | This program is distributed in the hope that it will be useful, |
||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | GNU General Public License for more details. |
||
15 | |||
171 | dmarschall | 16 | You should have received a copy of the GNU General Public License |
2 | toby | 17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
19 | */ |
||
20 | |||
21 | #include <plstringfuncs.h> |
||
22 | #include <ASRegistry.h> |
||
13 | toby | 23 | #include <ctype.h> |
2 | toby | 24 | |
25 | #include "ff.h" |
||
26 | |||
27 | #include "file_compat.h" |
||
28 | |||
29 | // MoreFiles headers |
||
171 | dmarschall | 30 | #include "FileCopy.h" |
2 | toby | 31 | #include "MoreFilesExtras.h" |
32 | |||
13 | toby | 33 | // prototype for a function included in Carbon's stdlib and declared in /usr/include/string.h |
34 | // but missing from MPW Universal header string.h |
||
35 | #ifndef _STRING_H_ |
||
36 | char *strnstr(const char *, const char *, size_t); |
||
37 | #endif |
||
38 | |||
99 | toby | 39 | static OSErr doresources(FSSpec *srcplug, FSSpec *rsrccopy){ |
2 | toby | 40 | short srcrn,dstrn; |
41 | Handle hpipl,h; |
||
85 | toby | 42 | long origsize,newsize,parm_type,parm_id; |
2 | toby | 43 | OSErr e = noErr; |
44 | Str255 title; |
||
45 | |||
11 | toby | 46 | #ifdef MACMACHO |
62 | toby | 47 | FSRef inref,outref; |
48 | // work with resources in data fork |
||
49 | if( !(e = FSpMakeFSRef(srcplug,&inref)) |
||
50 | && !(e = FSOpenResourceFile(&inref,0/*forkNameLength*/,NULL/*forkName*/,fsRdPerm,&srcrn)) |
||
101 | toby | 51 | && ((e = FSpMakeFSRef(rsrccopy,&outref)) |
52 | || (e = FSOpenResourceFile(&outref,0/*forkNameLength*/,NULL/*forkName*/,fsWrPerm,&dstrn))) ) |
||
62 | toby | 53 | CloseResFile(srcrn); |
11 | toby | 54 | #else |
62 | toby | 55 | // ordinary resource fork files |
56 | srcrn = FSpOpenResFile(srcplug,fsRdPerm); |
||
57 | if(srcrn != -1){ |
||
58 | dstrn = FSpOpenResFile(rsrccopy,fsWrPerm); |
||
59 | if(dstrn == -1){ |
||
60 | e = ResError(); |
||
61 | CloseResFile(srcrn); |
||
62 | } |
||
63 | }else e = ResError(); |
||
11 | toby | 64 | #endif |
2 | toby | 65 | |
11 | toby | 66 | if(!e){ |
67 | /* create a new PiPL resource for the standalone plugin, |
||
68 | with updated title and category strings */ |
||
171 | dmarschall | 69 | |
70 | if( (hpipl = Get1Resource('DATA',16000)) |
||
57 | toby | 71 | && (h = Get1Resource('PiPL',16000)) ) |
72 | { |
||
11 | toby | 73 | RemoveResource(h); |
171 | dmarschall | 74 | |
11 | toby | 75 | DetachResource(hpipl); |
2 | toby | 76 | |
11 | toby | 77 | PLstrcpy(title,gdata->parm.title); |
171 | dmarschall | 78 | if(gdata->parm.popDialog) |
11 | toby | 79 | PLstrcat(title,"\pÉ"); |
2 | toby | 80 | |
11 | toby | 81 | origsize = GetHandleSize(hpipl); |
82 | SetHandleSize(hpipl,origsize+0x300); /* some slop for fixup to work with */ |
||
83 | HLock(hpipl); |
||
84 | newsize = fixpipl((PIPropertyList*) *hpipl,origsize,title); |
||
85 | HUnlock(hpipl); |
||
86 | SetHandleSize(hpipl,newsize); |
||
171 | dmarschall | 87 | |
11 | toby | 88 | AddResource(hpipl,'PiPL',16000,"\p"); |
171 | dmarschall | 89 | |
57 | toby | 90 | if( !(e = ResError()) ){ |
91 | /* do a similar trick with the terminology resource, |
||
92 | so the scripting system can distinguish the standalone plugin */ |
||
171 | dmarschall | 93 | |
57 | toby | 94 | if( (h = Get1Resource(typeAETE,AETE_ID)) ){ |
184 | dmarschall | 95 | SetHandleSize(h,4096); |
57 | toby | 96 | HLock(h); |
184 | dmarschall | 97 | newsize = aete_generate((unsigned char*)*h, &gdata->parm); |
57 | toby | 98 | HUnlock(h); |
99 | SetHandleSize(h,newsize); |
||
171 | dmarschall | 100 | |
57 | toby | 101 | ChangedResource(h); |
171 | dmarschall | 102 | |
57 | toby | 103 | if( !(e = ResError()) ){ |
104 | /* add PARM resource */ |
||
85 | toby | 105 | if( !(e = PtrToHand(&gdata->parm,&h,sizeof(PARM_T))) ){ |
106 | if(gdata->obfusc){ |
||
107 | HLock(h); |
||
108 | obfusc((unsigned char*)*h,sizeof(PARM_T)); |
||
109 | HUnlock(h); |
||
110 | parm_type = 'DATA'; |
||
111 | parm_id = OBFUSCDATA_ID; |
||
112 | }else{ |
||
113 | parm_type = 'PARM'; |
||
114 | parm_id = PARM_ID; |
||
115 | } |
||
116 | AddResource(h,parm_type,parm_id,"\p"); |
||
117 | } |
||
57 | toby | 118 | } |
119 | } |
||
171 | dmarschall | 120 | |
57 | toby | 121 | } |
171 | dmarschall | 122 | |
11 | toby | 123 | } |
57 | toby | 124 | if(!e) |
125 | e = ResError(); |
||
126 | |||
11 | toby | 127 | CloseResFile(dstrn); |
2 | toby | 128 | CloseResFile(srcrn); |
11 | toby | 129 | } |
171 | dmarschall | 130 | |
2 | toby | 131 | return e; |
132 | } |
||
133 | |||
99 | toby | 134 | static int copyletters(char *dst,StringPtr src){ |
98 | toby | 135 | int i, n; |
62 | toby | 136 | |
98 | toby | 137 | for(i = src[0], n = 0; i--;) |
62 | toby | 138 | if(isalpha(*++src)){ |
139 | *dst++ = *src; |
||
140 | ++n; |
||
141 | } |
||
142 | return n; |
||
11 | toby | 143 | } |
8 | toby | 144 | |
98 | toby | 145 | // Info.plist in new standalone copy needs to be edited - |
11 | toby | 146 | // at least the CFBundleIdentifier property must be unique |
147 | |||
99 | toby | 148 | static OSErr copyplist(FSSpec *fss, short dstvol, long dstdir){ |
62 | toby | 149 | static char *key = "com.telegraphics.FilterFoundry"; |
150 | static unsigned char *fname="\pInfo.plist"; |
||
151 | char *buf,*save,*p; |
||
152 | short rn,dstrn,i,n,m; |
||
189 | dmarschall | 153 | FILEPOS eof; |
154 | FILECOUNT count; |
||
62 | toby | 155 | OSErr e; |
171 | dmarschall | 156 | |
62 | toby | 157 | if( !(e = HCreate(dstvol,dstdir,fname,'pled','TEXT')) ){ |
158 | if( !(e = HOpenDF(dstvol,dstdir,fname,fsWrPerm,&dstrn)) ){ |
||
159 | if( !(e = FSpOpenDF(fss,fsRdPerm,&rn)) ){ |
||
160 | if( !(e = GetEOF(rn,&eof)) && (buf = malloc(eof+1024)) ){ |
||
161 | if( !(e = FSRead(rn,&eof,buf)) ){ |
||
162 | buf[eof] = 0; |
||
163 | if( (p = strnstr(buf,key,eof)) && (save = malloc(eof-(p-buf)+1)) ){ |
||
164 | p += strlen(key); |
||
165 | // store text after matched string |
||
166 | strcpy(save,p); |
||
171 | dmarschall | 167 | |
62 | toby | 168 | *p++ = '.'; |
169 | n = copyletters(p,gdata->parm.category); |
||
170 | p += n; |
||
171 | if(n) *p++ = '.'; |
||
172 | m = copyletters(p,gdata->parm.title); |
||
173 | p += m; |
||
174 | if(!m){ |
||
175 | // generate a random ASCII identifier |
||
176 | srand(TICKCOUNT()); |
||
98 | toby | 177 | for(i = 8; i--;) |
178 | *p++ = 'a' + (rand() % 26); |
||
62 | toby | 179 | } |
180 | strcpy(p,save); |
||
171 | dmarschall | 181 | |
62 | toby | 182 | count = strlen(buf); |
183 | e = FSWrite(dstrn,&count,buf); |
||
171 | dmarschall | 184 | |
62 | toby | 185 | free(save); |
186 | }else e = paramErr; // not found?? shouldn't happen |
||
187 | } |
||
188 | free(buf); |
||
189 | } |
||
190 | FSClose(rn); |
||
191 | } |
||
192 | FSClose(dstrn); |
||
193 | } |
||
194 | if(e) HDelete(dstvol,dstdir,fname); |
||
195 | } |
||
196 | return e; |
||
11 | toby | 197 | } |
198 | |||
99 | toby | 199 | static OSErr make_bundle(StandardFileReply *sfr, short plugvol, |
200 | long plugdir, StringPtr plugname, char *reason) |
||
201 | { |
||
2 | toby | 202 | short dstvol = sfr->sfFile.vRefNum; |
11 | toby | 203 | long bundledir,contentsdir,macosdir,rsrcdir; |
2 | toby | 204 | DInfo fndrInfo; |
205 | OSErr e; |
||
11 | toby | 206 | FSSpec fss,macosfss,rsrcfss,rsrccopyfss; |
99 | toby | 207 | char *why; |
2 | toby | 208 | |
209 | if( !(e = FSpDirCreate(&sfr->sfFile,sfr->sfScript,&bundledir)) ){ |
||
210 | if(!(e = FSpGetDInfo(&sfr->sfFile,&fndrInfo)) ){ |
||
211 | fndrInfo.frFlags |= kHasBundle; |
||
212 | FSpSetDInfo(&sfr->sfFile,&fndrInfo); |
||
213 | } |
||
214 | if( !(e = DirCreate(dstvol,bundledir,"\pContents",&contentsdir)) ){ |
||
215 | if( !(e = DirCreate(dstvol,contentsdir,"\pMacOS",&macosdir)) ){ |
||
11 | toby | 216 | if( !(e = DirCreate(dstvol,contentsdir,"\pResources",&rsrcdir)) ){ |
99 | toby | 217 | /* copy the Info.plist file, resource file, and executable */ |
11 | toby | 218 | if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::MacOS:FilterFoundry",&macosfss)) |
63 | toby | 219 | && !(e = FileCopy(macosfss.vRefNum,macosfss.parID,macosfss.name, dstvol,macosdir,NULL, NULL,NULL,0,false)) ) |
220 | { |
||
98 | toby | 221 | /* add PARM resources to each binary, and edit PiPLs */ |
63 | toby | 222 | if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::Resources:FilterFoundry.rsrc",&rsrcfss)) |
223 | && !(e = FileCopy(rsrcfss.vRefNum,rsrcfss.parID,rsrcfss.name, dstvol,rsrcdir,NULL, NULL,NULL,0,false)) |
||
224 | && !(e = FSMakeFSSpec(dstvol,rsrcdir,"\pFilterFoundry.rsrc",&rsrccopyfss)) ) |
||
225 | { |
||
226 | if( !(e = doresources(&rsrcfss, &rsrccopyfss)) |
||
227 | && !(e = FSMakeFSSpec(plugvol,plugdir,"\p::Info.plist",&fss)) ) |
||
62 | toby | 228 | { |
63 | toby | 229 | e = copyplist(&fss,dstvol,contentsdir); |
99 | toby | 230 | if(e){ |
231 | FSpDelete(&rsrccopyfss); |
||
232 | why = "Can't copy Info.plist file."; |
||
233 | } |
||
234 | }else why = "Can't copy resources."; |
||
62 | toby | 235 | if(e) HDelete(dstvol,macosdir,"\pFilterFoundry"); |
99 | toby | 236 | }else why = "Can't copy FilterFoundry.rsrc file."; |
11 | toby | 237 | if(e) HDelete(dstvol,rsrcdir,plugname); |
99 | toby | 238 | }else why = "Can't copy FilterFoundry executable."; |
11 | toby | 239 | if(e) HDelete(dstvol,contentsdir,"\pResources"); |
99 | toby | 240 | }else why = "Can't create bundle Contents/Resources directory."; |
11 | toby | 241 | if(e) HDelete(dstvol,contentsdir,"\pMacOS"); |
99 | toby | 242 | }else why = "Can't create bundle Contents/MacOS directory."; |
11 | toby | 243 | if(e) HDelete(dstvol,bundledir,"\pContents"); |
99 | toby | 244 | }else why = "Can't create bundle Contents directory."; |
2 | toby | 245 | if(e) FSpDelete(&sfr->sfFile); |
99 | toby | 246 | }else why = "Can't create new bundle directory."; |
247 | |||
248 | if(e) |
||
249 | sprintf(reason, "%s (%d)", why, e); |
||
250 | else |
||
251 | reason[0] = 0; |
||
252 | |||
2 | toby | 253 | return e; |
254 | } |
||
255 | |||
99 | toby | 256 | static OSErr make_singlefile(StandardFileReply *sfr, short plugvol, long plugdir, StringPtr plugname){ |
2 | toby | 257 | OSErr e; |
258 | FSSpec origfss; |
||
259 | |||
260 | e = FSpDelete(&sfr->sfFile); |
||
261 | if(e && e != fnfErr){ |
||
11 | toby | 262 | alertuser("Can't replace the existing file. Try a different name or location.",""); |
2 | toby | 263 | return userCanceledErr; |
264 | } |
||
265 | |||
171 | dmarschall | 266 | if( !(e = FileCopy(plugvol,plugdir,plugname, sfr->sfFile.vRefNum,sfr->sfFile.parID,NULL, sfr->sfFile.name,NULL,0,false)) |
62 | toby | 267 | && !(e = FSMakeFSSpec(plugvol,plugdir,plugname,&origfss)) ) |
98 | toby | 268 | /* add PARM resources, and edit PiPL */ |
11 | toby | 269 | e = doresources(&origfss, &sfr->sfFile); |
2 | toby | 270 | |
271 | return e; |
||
272 | } |
||
273 | |||
274 | OSErr make_standalone(StandardFileReply *sfr){ |
||
275 | OSErr e; |
||
276 | short plugvol; |
||
277 | long plugdir; |
||
278 | Str255 plugname; |
||
99 | toby | 279 | char reason[0x100] = {0}; |
171 | dmarschall | 280 | |
98 | toby | 281 | if(!(e = GetFileLocation(CurResFile(),&plugvol,&plugdir,plugname))){ |
11 | toby | 282 | #ifdef MACMACHO |
99 | toby | 283 | e = make_bundle(sfr,plugvol,plugdir,plugname,reason); |
11 | toby | 284 | #else |
98 | toby | 285 | e = make_singlefile(sfr,plugvol,plugdir,plugname); |
11 | toby | 286 | #endif |
98 | toby | 287 | } |
2 | toby | 288 | |
184 | dmarschall | 289 | if(e && e != userCanceledErr) { |
99 | toby | 290 | alertuser("Could not create standalone plugin.",reason); |
184 | dmarschall | 291 | } else { |
292 | showmessage("Filter was sucessfully created"); |
||
293 | } |
||
171 | dmarschall | 294 | |
2 | toby | 295 | return e; |
296 | } |