Subversion Repositories filter_foundry

Rev

Rev 375 | Rev 511 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
259 daniel-mar 1
/*
2
    This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
3
    Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
4
    Copyright (C) 2018-2021 Daniel Marschall, ViaThinkSoft
5
 
6
    This program is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
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
 
16
    You should have received a copy of the GNU General Public License
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>
23
#include <ctype.h>
24
 
25
#include "ff.h"
26
 
27
#include "file_compat.h"
28
 
29
// MoreFiles headers
30
#include "FileCopy.h"
31
#include "MoreFilesExtras.h"
32
 
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_
268 daniel-mar 36
        char *strnstr(const char *, const char *, size_t);
259 daniel-mar 37
#endif
38
 
39
static OSErr doresources(FSSpec *srcplug, FSSpec *rsrccopy){
40
        short srcrn,dstrn;
41
        Handle hpipl,h;
42
        long origsize,newsize,parm_type,parm_id;
43
        OSErr e = noErr;
44
        Str255 title;
45
        long event_id;
46
 
268 daniel-mar 47
        #ifdef MACMACHO
259 daniel-mar 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))
52
         && ((e = FSpMakeFSRef(rsrccopy,&outref))
53
                 || (e = FSOpenResourceFile(&outref,0/*forkNameLength*/,NULL/*forkName*/,fsWrPerm,&dstrn))) )
54
                CloseResFile(srcrn);
268 daniel-mar 55
        #else
259 daniel-mar 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();
268 daniel-mar 65
        #endif
259 daniel-mar 66
 
67
        if(!e){
68
                /* create a new PiPL resource for the standalone plugin,
69
                   with updated title and category strings */
70
 
373 daniel-mar 71
                if( (hpipl = Get1Resource('tpLT',16000))
259 daniel-mar 72
                 && (h = Get1Resource('PiPL',16000)) )
73
                {
74
                        RemoveResource(h);
75
 
76
                        DetachResource(hpipl);
77
 
393 daniel-mar 78
                        myc2pstrcpy(title,gdata->parm.szTitle);
259 daniel-mar 79
                        if(gdata->parm.popDialog)
80
                                PLstrcat(title,"\pÉ");
81
 
82
                        origsize = GetHandleSize(hpipl);
83
                        SetHandleSize(hpipl,origsize+0x300); /* some slop for fixup to work with */
84
                        HLock(hpipl);
85
                        newsize = fixpipl((PIPropertyList*) *hpipl,origsize,title,&event_id);
86
                        HUnlock(hpipl);
87
                        SetHandleSize(hpipl,newsize);
88
 
89
                        AddResource(hpipl,'PiPL',16000,"\p");
90
 
393 daniel-mar 91
                        /* convert C strings to Pascal strings */
92
                        PARM_T pascal_parm = (PARM_T)malloc(sizeof(PARM_T));
93
                        memcpy(pascal_parm, gdata->parm, sizeof(PARM_T));
94
                        myc2pstr(pascal_parm->szCategory);
95
                        myc2pstr(pascal_parm->szTitle);
96
                        myc2pstr(pascal_parm->szCopyright);
97
                        myc2pstr(pascal_parm->szAuthor);
98
                        for (i = 0; i < 4; ++i)
99
                                myc2pstr(pascal_parm->szMap[i]);
100
                        for (i = 0; i < 8; ++i)
101
                                myc2pstr(pascal_parm->szCtl[i]);
102
 
259 daniel-mar 103
                        if( !(e = ResError()) ){
104
                                /* do a similar trick with the terminology resource,
105
                                   so the scripting system can distinguish the standalone plugin */
106
 
107
                                if( (h = Get1Resource(typeAETE,AETE_ID)) ){
108
                                        SetHandleSize(h,4096);
109
                                        HLock(h);
393 daniel-mar 110
                                        newsize = aete_generate((unsigned char*)*h, &pascal_parm, event_id);
259 daniel-mar 111
                                        HUnlock(h);
112
                                        SetHandleSize(h,newsize);
113
 
114
                                        ChangedResource(h);
115
 
116
                                        if( !(e = ResError()) ){
117
                                                /* add PARM resource */
393 daniel-mar 118
                                                if( !(e = PtrToHand(&pascal_parm,&h,sizeof(PARM_T))) ){
259 daniel-mar 119
                                                        if(gdata->obfusc){
120
                                                                HLock(h);
292 daniel-mar 121
                                                                obfusc((PARM_T*)*h);
259 daniel-mar 122
                                                                HUnlock(h);
375 daniel-mar 123
                                                                parm_type = OBFUSCDATA_TYPE_NEW;
124
                                                                parm_id = OBFUSCDATA_ID_NEW;
259 daniel-mar 125
                                                        }else{
375 daniel-mar 126
                                                                parm_type = PARM_TYPE;
127
                                                                parm_id = PARM_ID_NEW;
259 daniel-mar 128
                                                        }
129
                                                        AddResource(h,parm_type,parm_id,"\p");
130
                                                }
131
                                        }
132
                                }
133
 
134
                        }
135
 
136
                }
137
                if(!e)
138
                        e = ResError();
139
 
140
                CloseResFile(dstrn);
141
                CloseResFile(srcrn);
142
        }
143
 
144
        return e;
145
}
146
 
393 daniel-mar 147
static int copyletters(char *dst,char* src){
259 daniel-mar 148
        int i, n;
149
 
393 daniel-mar 150
        for(i=0; i<strlen(src); ++i) {
151
                if(isalpha(src[i])){
152
                        *dst++ = src[i];
259 daniel-mar 153
                        ++n;
154
                }
393 daniel-mar 155
        }
156
 
259 daniel-mar 157
        return n;
158
}
159
 
160
// Info.plist in new standalone copy needs to be edited -
161
// at least the CFBundleIdentifier property must be unique
162
 
163
static OSErr copyplist(FSSpec *fss, short dstvol, long dstdir){
164
        static char *key = "com.telegraphics.FilterFoundry";
165
        static unsigned char *fname="\pInfo.plist";
166
        char *buf,*save,*p;
167
        short rn,dstrn,i,n,m;
168
        FILEPOS eof;
169
        FILECOUNT count;
170
        OSErr e;
171
 
172
        if( !(e = HCreate(dstvol,dstdir,fname,'pled','TEXT')) ){
173
                if( !(e = HOpenDF(dstvol,dstdir,fname,fsWrPerm,&dstrn)) ){
174
                        if( !(e = FSpOpenDF(fss,fsRdPerm,&rn)) ){
175
                                if( !(e = GetEOF(rn,&eof)) && (buf = malloc(eof+1024)) ){
176
                                        if( !(e = FSRead(rn,&eof,buf)) ){
177
                                                buf[eof] = 0;
178
                                                if( (p = strnstr(buf,key,eof)) && (save = malloc(eof-(p-buf)+1)) ){
179
                                                        p += strlen(key);
180
                                                        // store text after matched string
181
                                                        strcpy(save,p);
182
 
183
                                                        *p++ = '.';
393 daniel-mar 184
                                                        n = copyletters(p,gdata->parm.szCategory);
259 daniel-mar 185
                                                        p += n;
186
                                                        if(n) *p++ = '.';
393 daniel-mar 187
                                                        m = copyletters(p,gdata->parm.szTitle);
259 daniel-mar 188
                                                        p += m;
189
                                                        if(!m){
190
                                                                // generate a random ASCII identifier
191
                                                                srand(TICKCOUNT());
192
                                                                for(i = 8; i--;)
193
                                                                        *p++ = 'a' + (rand() % 26);
194
                                                        }
195
                                                        strcpy(p,save);
196
 
197
                                                        count = strlen(buf);
198
                                                        e = FSWrite(dstrn,&count,buf);
199
 
200
                                                        free(save);
201
                                                }else e = paramErr; // not found?? shouldn't happen
202
                                        }
203
                                        free(buf);
204
                                }
205
                                FSClose(rn);
206
                        }
207
                        FSClose(dstrn);
208
                }
209
                if(e) HDelete(dstvol,dstdir,fname);
210
        }
211
        return e;
212
}
213
 
214
static OSErr make_bundle(StandardFileReply *sfr, short plugvol,
215
                                                 long plugdir, StringPtr plugname, char *reason)
216
{
217
        short dstvol = sfr->sfFile.vRefNum;
218
        long bundledir,contentsdir,macosdir,rsrcdir;
219
        DInfo fndrInfo;
220
        OSErr e;
221
        FSSpec fss,macosfss,rsrcfss,rsrccopyfss;
222
        char *why;
223
 
224
        if( !(e = FSpDirCreate(&sfr->sfFile,sfr->sfScript,&bundledir)) ){
225
                if(!(e = FSpGetDInfo(&sfr->sfFile,&fndrInfo)) ){
226
                        fndrInfo.frFlags |= kHasBundle;
227
                        FSpSetDInfo(&sfr->sfFile,&fndrInfo);
228
                }
229
                if( !(e = DirCreate(dstvol,bundledir,"\pContents",&contentsdir)) ){
230
                        if( !(e = DirCreate(dstvol,contentsdir,"\pMacOS",&macosdir)) ){
231
                                if( !(e = DirCreate(dstvol,contentsdir,"\pResources",&rsrcdir)) ){
232
                                        /* copy the Info.plist file, resource file, and executable */
233
                                        if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::MacOS:FilterFoundry",&macosfss))
234
                                         && !(e = FileCopy(macosfss.vRefNum,macosfss.parID,macosfss.name, dstvol,macosdir,NULL, NULL,NULL,0,false)) )
235
                                        {
236
                                                /* add PARM resources to each binary, and edit PiPLs */
237
                                                if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::Resources:FilterFoundry.rsrc",&rsrcfss))
238
                                                 && !(e = FileCopy(rsrcfss.vRefNum,rsrcfss.parID,rsrcfss.name, dstvol,rsrcdir,NULL, NULL,NULL,0,false))
239
                                                 && !(e = FSMakeFSSpec(dstvol,rsrcdir,"\pFilterFoundry.rsrc",&rsrccopyfss)) )
240
                                                {
241
                                                        if( !(e = doresources(&rsrcfss, &rsrccopyfss))
242
                                                         && !(e = FSMakeFSSpec(plugvol,plugdir,"\p::Info.plist",&fss)) )
243
                                                        {
244
                                                                e = copyplist(&fss,dstvol,contentsdir);
245
                                                                if(e){
246
                                                                        FSpDelete(&rsrccopyfss);
247
                                                                        why = "Can't copy Info.plist file.";
248
                                                                }
249
                                                        }else why = "Can't copy resources.";
250
                                                        if(e) HDelete(dstvol,macosdir,"\pFilterFoundry");
251
                                                }else why = "Can't copy FilterFoundry.rsrc file.";
252
                                                if(e) HDelete(dstvol,rsrcdir,plugname);
253
                                        }else why = "Can't copy FilterFoundry executable.";
254
                                        if(e) HDelete(dstvol,contentsdir,"\pResources");
255
                                }else why = "Can't create bundle Contents/Resources directory.";
256
                                if(e) HDelete(dstvol,contentsdir,"\pMacOS");
257
                        }else why = "Can't create bundle Contents/MacOS directory.";
258
                        if(e) HDelete(dstvol,bundledir,"\pContents");
259
                }else why = "Can't create bundle Contents directory.";
260
                if(e) FSpDelete(&sfr->sfFile);
261
        }else why = "Can't create new bundle directory.";
262
 
263
        if(e)
264
                sprintf(reason, "%s (%d)", why, e);
265
        else
266
                reason[0] = 0;
267
 
268
        return e;
269
}
270
 
271
static OSErr make_singlefile(StandardFileReply *sfr, short plugvol, long plugdir, StringPtr plugname){
272
        OSErr e;
273
        FSSpec origfss;
274
 
275
        e = FSpDelete(&sfr->sfFile);
276
        if(e && e != fnfErr){
277
                alertuser(_strdup("Can't replace the existing file. Try a different name or location."),_strdup(""));
278
                return userCanceledErr;
279
        }
280
 
281
        if( !(e = FileCopy(plugvol,plugdir,plugname, sfr->sfFile.vRefNum,sfr->sfFile.parID,NULL, sfr->sfFile.name,NULL,0,false))
282
         && !(e = FSMakeFSSpec(plugvol,plugdir,plugname,&origfss)) )
283
                /* add PARM resources, and edit PiPL */
284
                e = doresources(&origfss, &sfr->sfFile);
285
 
286
        return e;
287
}
288
 
289
OSErr make_standalone(StandardFileReply *sfr){
290
        OSErr e;
291
        short plugvol;
292
        long plugdir;
293
        Str255 plugname;
294
        char reason[0x100] = {0};
295
 
296
        if(!(e = GetFileLocation(CurResFile(),&plugvol,&plugdir,plugname))){
268 daniel-mar 297
                #ifdef MACMACHO
259 daniel-mar 298
                e = make_bundle(sfr,plugvol,plugdir,plugname,reason);
268 daniel-mar 299
                #else
259 daniel-mar 300
                e = make_singlefile(sfr,plugvol,plugdir,plugname);
268 daniel-mar 301
                #endif
259 daniel-mar 302
        }
303
 
304
        if(e && e != userCanceledErr) {
305
                alertuser(_strdup("Could not create standalone plugin."),reason);
306
        } else {
307
                showmessage(_strdup("Filter was sucessfully created"));
308
        }
309
 
310
        return e;
311
}