Subversion Repositories filter_foundry

Rev

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