Rev 209 | Rev 255 | 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 |
206 | daniel-mar | 4 | Copyright (C) 2018-2021 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; |
||
209 | daniel-mar | 45 | long event_id; |
2 | toby | 46 | |
11 | toby | 47 | #ifdef MACMACHO |
62 | toby | 48 | FSRef inref,outref; |
49 | // work with resources in data fork |
||
50 | if( !(e = FSpMakeFSRef(srcplug,&inref)) |
||
51 | && !(e = FSOpenResourceFile(&inref,0/*forkNameLength*/,NULL/*forkName*/,fsRdPerm,&srcrn)) |
||
101 | toby | 52 | && ((e = FSpMakeFSRef(rsrccopy,&outref)) |
53 | || (e = FSOpenResourceFile(&outref,0/*forkNameLength*/,NULL/*forkName*/,fsWrPerm,&dstrn))) ) |
||
62 | toby | 54 | CloseResFile(srcrn); |
11 | toby | 55 | #else |
62 | toby | 56 | // ordinary resource fork files |
57 | srcrn = FSpOpenResFile(srcplug,fsRdPerm); |
||
58 | if(srcrn != -1){ |
||
59 | dstrn = FSpOpenResFile(rsrccopy,fsWrPerm); |
||
60 | if(dstrn == -1){ |
||
61 | e = ResError(); |
||
62 | CloseResFile(srcrn); |
||
63 | } |
||
64 | }else e = ResError(); |
||
11 | toby | 65 | #endif |
2 | toby | 66 | |
11 | toby | 67 | if(!e){ |
68 | /* create a new PiPL resource for the standalone plugin, |
||
69 | with updated title and category strings */ |
||
171 | dmarschall | 70 | |
243 | daniel-mar | 71 | if( (hpipl = Get1Resource('TpLT',16000)) |
57 | toby | 72 | && (h = Get1Resource('PiPL',16000)) ) |
73 | { |
||
11 | toby | 74 | RemoveResource(h); |
171 | dmarschall | 75 | |
11 | toby | 76 | DetachResource(hpipl); |
2 | toby | 77 | |
11 | toby | 78 | PLstrcpy(title,gdata->parm.title); |
171 | dmarschall | 79 | if(gdata->parm.popDialog) |
11 | toby | 80 | PLstrcat(title,"\pÉ"); |
2 | toby | 81 | |
11 | toby | 82 | origsize = GetHandleSize(hpipl); |
83 | SetHandleSize(hpipl,origsize+0x300); /* some slop for fixup to work with */ |
||
84 | HLock(hpipl); |
||
209 | daniel-mar | 85 | newsize = fixpipl((PIPropertyList*) *hpipl,origsize,title,&event_id); |
11 | toby | 86 | HUnlock(hpipl); |
87 | SetHandleSize(hpipl,newsize); |
||
171 | dmarschall | 88 | |
11 | toby | 89 | AddResource(hpipl,'PiPL',16000,"\p"); |
171 | dmarschall | 90 | |
57 | toby | 91 | if( !(e = ResError()) ){ |
92 | /* do a similar trick with the terminology resource, |
||
93 | so the scripting system can distinguish the standalone plugin */ |
||
171 | dmarschall | 94 | |
57 | toby | 95 | if( (h = Get1Resource(typeAETE,AETE_ID)) ){ |
184 | dmarschall | 96 | SetHandleSize(h,4096); |
57 | toby | 97 | HLock(h); |
209 | daniel-mar | 98 | newsize = aete_generate((unsigned char*)*h, &gdata->parm, event_id); |
57 | toby | 99 | HUnlock(h); |
100 | SetHandleSize(h,newsize); |
||
171 | dmarschall | 101 | |
57 | toby | 102 | ChangedResource(h); |
171 | dmarschall | 103 | |
57 | toby | 104 | if( !(e = ResError()) ){ |
105 | /* add PARM resource */ |
||
85 | toby | 106 | if( !(e = PtrToHand(&gdata->parm,&h,sizeof(PARM_T))) ){ |
107 | if(gdata->obfusc){ |
||
108 | HLock(h); |
||
109 | obfusc((unsigned char*)*h,sizeof(PARM_T)); |
||
110 | HUnlock(h); |
||
111 | parm_type = 'DATA'; |
||
112 | parm_id = OBFUSCDATA_ID; |
||
113 | }else{ |
||
114 | parm_type = 'PARM'; |
||
115 | parm_id = PARM_ID; |
||
116 | } |
||
117 | AddResource(h,parm_type,parm_id,"\p"); |
||
118 | } |
||
57 | toby | 119 | } |
120 | } |
||
171 | dmarschall | 121 | |
57 | toby | 122 | } |
171 | dmarschall | 123 | |
11 | toby | 124 | } |
57 | toby | 125 | if(!e) |
126 | e = ResError(); |
||
127 | |||
11 | toby | 128 | CloseResFile(dstrn); |
2 | toby | 129 | CloseResFile(srcrn); |
11 | toby | 130 | } |
171 | dmarschall | 131 | |
2 | toby | 132 | return e; |
133 | } |
||
134 | |||
99 | toby | 135 | static int copyletters(char *dst,StringPtr src){ |
98 | toby | 136 | int i, n; |
62 | toby | 137 | |
98 | toby | 138 | for(i = src[0], n = 0; i--;) |
62 | toby | 139 | if(isalpha(*++src)){ |
140 | *dst++ = *src; |
||
141 | ++n; |
||
142 | } |
||
143 | return n; |
||
11 | toby | 144 | } |
8 | toby | 145 | |
98 | toby | 146 | // Info.plist in new standalone copy needs to be edited - |
11 | toby | 147 | // at least the CFBundleIdentifier property must be unique |
148 | |||
99 | toby | 149 | static OSErr copyplist(FSSpec *fss, short dstvol, long dstdir){ |
62 | toby | 150 | static char *key = "com.telegraphics.FilterFoundry"; |
151 | static unsigned char *fname="\pInfo.plist"; |
||
152 | char *buf,*save,*p; |
||
153 | short rn,dstrn,i,n,m; |
||
189 | dmarschall | 154 | FILEPOS eof; |
155 | FILECOUNT count; |
||
62 | toby | 156 | OSErr e; |
171 | dmarschall | 157 | |
62 | toby | 158 | if( !(e = HCreate(dstvol,dstdir,fname,'pled','TEXT')) ){ |
159 | if( !(e = HOpenDF(dstvol,dstdir,fname,fsWrPerm,&dstrn)) ){ |
||
160 | if( !(e = FSpOpenDF(fss,fsRdPerm,&rn)) ){ |
||
161 | if( !(e = GetEOF(rn,&eof)) && (buf = malloc(eof+1024)) ){ |
||
162 | if( !(e = FSRead(rn,&eof,buf)) ){ |
||
163 | buf[eof] = 0; |
||
164 | if( (p = strnstr(buf,key,eof)) && (save = malloc(eof-(p-buf)+1)) ){ |
||
165 | p += strlen(key); |
||
166 | // store text after matched string |
||
167 | strcpy(save,p); |
||
171 | dmarschall | 168 | |
62 | toby | 169 | *p++ = '.'; |
170 | n = copyletters(p,gdata->parm.category); |
||
171 | p += n; |
||
172 | if(n) *p++ = '.'; |
||
173 | m = copyletters(p,gdata->parm.title); |
||
174 | p += m; |
||
175 | if(!m){ |
||
176 | // generate a random ASCII identifier |
||
177 | srand(TICKCOUNT()); |
||
98 | toby | 178 | for(i = 8; i--;) |
179 | *p++ = 'a' + (rand() % 26); |
||
62 | toby | 180 | } |
181 | strcpy(p,save); |
||
171 | dmarschall | 182 | |
62 | toby | 183 | count = strlen(buf); |
184 | e = FSWrite(dstrn,&count,buf); |
||
171 | dmarschall | 185 | |
62 | toby | 186 | free(save); |
187 | }else e = paramErr; // not found?? shouldn't happen |
||
188 | } |
||
189 | free(buf); |
||
190 | } |
||
191 | FSClose(rn); |
||
192 | } |
||
193 | FSClose(dstrn); |
||
194 | } |
||
195 | if(e) HDelete(dstvol,dstdir,fname); |
||
196 | } |
||
197 | return e; |
||
11 | toby | 198 | } |
199 | |||
99 | toby | 200 | static OSErr make_bundle(StandardFileReply *sfr, short plugvol, |
201 | long plugdir, StringPtr plugname, char *reason) |
||
202 | { |
||
2 | toby | 203 | short dstvol = sfr->sfFile.vRefNum; |
11 | toby | 204 | long bundledir,contentsdir,macosdir,rsrcdir; |
2 | toby | 205 | DInfo fndrInfo; |
206 | OSErr e; |
||
11 | toby | 207 | FSSpec fss,macosfss,rsrcfss,rsrccopyfss; |
99 | toby | 208 | char *why; |
2 | toby | 209 | |
210 | if( !(e = FSpDirCreate(&sfr->sfFile,sfr->sfScript,&bundledir)) ){ |
||
211 | if(!(e = FSpGetDInfo(&sfr->sfFile,&fndrInfo)) ){ |
||
212 | fndrInfo.frFlags |= kHasBundle; |
||
213 | FSpSetDInfo(&sfr->sfFile,&fndrInfo); |
||
214 | } |
||
215 | if( !(e = DirCreate(dstvol,bundledir,"\pContents",&contentsdir)) ){ |
||
216 | if( !(e = DirCreate(dstvol,contentsdir,"\pMacOS",&macosdir)) ){ |
||
11 | toby | 217 | if( !(e = DirCreate(dstvol,contentsdir,"\pResources",&rsrcdir)) ){ |
99 | toby | 218 | /* copy the Info.plist file, resource file, and executable */ |
11 | toby | 219 | if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::MacOS:FilterFoundry",&macosfss)) |
63 | toby | 220 | && !(e = FileCopy(macosfss.vRefNum,macosfss.parID,macosfss.name, dstvol,macosdir,NULL, NULL,NULL,0,false)) ) |
221 | { |
||
98 | toby | 222 | /* add PARM resources to each binary, and edit PiPLs */ |
63 | toby | 223 | if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::Resources:FilterFoundry.rsrc",&rsrcfss)) |
224 | && !(e = FileCopy(rsrcfss.vRefNum,rsrcfss.parID,rsrcfss.name, dstvol,rsrcdir,NULL, NULL,NULL,0,false)) |
||
225 | && !(e = FSMakeFSSpec(dstvol,rsrcdir,"\pFilterFoundry.rsrc",&rsrccopyfss)) ) |
||
226 | { |
||
227 | if( !(e = doresources(&rsrcfss, &rsrccopyfss)) |
||
228 | && !(e = FSMakeFSSpec(plugvol,plugdir,"\p::Info.plist",&fss)) ) |
||
62 | toby | 229 | { |
63 | toby | 230 | e = copyplist(&fss,dstvol,contentsdir); |
99 | toby | 231 | if(e){ |
232 | FSpDelete(&rsrccopyfss); |
||
233 | why = "Can't copy Info.plist file."; |
||
234 | } |
||
235 | }else why = "Can't copy resources."; |
||
62 | toby | 236 | if(e) HDelete(dstvol,macosdir,"\pFilterFoundry"); |
99 | toby | 237 | }else why = "Can't copy FilterFoundry.rsrc file."; |
11 | toby | 238 | if(e) HDelete(dstvol,rsrcdir,plugname); |
99 | toby | 239 | }else why = "Can't copy FilterFoundry executable."; |
11 | toby | 240 | if(e) HDelete(dstvol,contentsdir,"\pResources"); |
99 | toby | 241 | }else why = "Can't create bundle Contents/Resources directory."; |
11 | toby | 242 | if(e) HDelete(dstvol,contentsdir,"\pMacOS"); |
99 | toby | 243 | }else why = "Can't create bundle Contents/MacOS directory."; |
11 | toby | 244 | if(e) HDelete(dstvol,bundledir,"\pContents"); |
99 | toby | 245 | }else why = "Can't create bundle Contents directory."; |
2 | toby | 246 | if(e) FSpDelete(&sfr->sfFile); |
99 | toby | 247 | }else why = "Can't create new bundle directory."; |
248 | |||
249 | if(e) |
||
250 | sprintf(reason, "%s (%d)", why, e); |
||
251 | else |
||
252 | reason[0] = 0; |
||
253 | |||
2 | toby | 254 | return e; |
255 | } |
||
256 | |||
99 | toby | 257 | static OSErr make_singlefile(StandardFileReply *sfr, short plugvol, long plugdir, StringPtr plugname){ |
2 | toby | 258 | OSErr e; |
259 | FSSpec origfss; |
||
260 | |||
261 | e = FSpDelete(&sfr->sfFile); |
||
262 | if(e && e != fnfErr){ |
||
198 | daniel-mar | 263 | alertuser(_strdup("Can't replace the existing file. Try a different name or location."),_strdup("")); |
2 | toby | 264 | return userCanceledErr; |
265 | } |
||
266 | |||
171 | dmarschall | 267 | if( !(e = FileCopy(plugvol,plugdir,plugname, sfr->sfFile.vRefNum,sfr->sfFile.parID,NULL, sfr->sfFile.name,NULL,0,false)) |
62 | toby | 268 | && !(e = FSMakeFSSpec(plugvol,plugdir,plugname,&origfss)) ) |
98 | toby | 269 | /* add PARM resources, and edit PiPL */ |
11 | toby | 270 | e = doresources(&origfss, &sfr->sfFile); |
2 | toby | 271 | |
272 | return e; |
||
273 | } |
||
274 | |||
275 | OSErr make_standalone(StandardFileReply *sfr){ |
||
276 | OSErr e; |
||
277 | short plugvol; |
||
278 | long plugdir; |
||
279 | Str255 plugname; |
||
99 | toby | 280 | char reason[0x100] = {0}; |
171 | dmarschall | 281 | |
98 | toby | 282 | if(!(e = GetFileLocation(CurResFile(),&plugvol,&plugdir,plugname))){ |
11 | toby | 283 | #ifdef MACMACHO |
99 | toby | 284 | e = make_bundle(sfr,plugvol,plugdir,plugname,reason); |
11 | toby | 285 | #else |
98 | toby | 286 | e = make_singlefile(sfr,plugvol,plugdir,plugname); |
11 | toby | 287 | #endif |
98 | toby | 288 | } |
2 | toby | 289 | |
184 | dmarschall | 290 | if(e && e != userCanceledErr) { |
198 | daniel-mar | 291 | alertuser(_strdup("Could not create standalone plugin."),reason); |
184 | dmarschall | 292 | } else { |
198 | daniel-mar | 293 | showmessage(_strdup("Filter was sucessfully created")); |
184 | dmarschall | 294 | } |
171 | dmarschall | 295 | |
2 | toby | 296 | return e; |
297 | } |