Subversion Repositories filter_foundry

Rev

Rev 101 | Rev 184 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  3.     Copyright (C) 2003-2019 Toby Thain, toby@telegraphics.com.au
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  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.  
  15.     You should have received a copy of the GNU General Public License
  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>
  22. #include <ctype.h>
  23.  
  24. #include "ff.h"
  25.  
  26. #include "file_compat.h"
  27.  
  28. // MoreFiles headers
  29. #include "FileCopy.h"
  30. #include "MoreFilesExtras.h"
  31.  
  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.  
  38. static OSErr doresources(FSSpec *srcplug, FSSpec *rsrccopy){
  39.         short srcrn,dstrn;
  40.         Handle hpipl,h;
  41.         long origsize,newsize,parm_type,parm_id;
  42.         OSErr e = noErr;
  43.         Str255 title;
  44.  
  45. #ifdef MACMACHO
  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))
  50.          && ((e = FSpMakeFSRef(rsrccopy,&outref))
  51.                  || (e = FSOpenResourceFile(&outref,0/*forkNameLength*/,NULL/*forkName*/,fsWrPerm,&dstrn))) )
  52.                 CloseResFile(srcrn);
  53. #else
  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();
  63. #endif
  64.  
  65.         if(!e){
  66.                 /* create a new PiPL resource for the standalone plugin,
  67.                    with updated title and category strings */
  68.  
  69.                 if( (hpipl = Get1Resource('DATA',16000))
  70.                  && (h = Get1Resource('PiPL',16000)) )
  71.                 {
  72.                         RemoveResource(h);
  73.  
  74.                         DetachResource(hpipl);
  75.  
  76.                         PLstrcpy(title,gdata->parm.title);
  77.                         if(gdata->parm.popDialog)
  78.                                 PLstrcat(title,"\pÉ");
  79.  
  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);
  86.  
  87.                         AddResource(hpipl,'PiPL',16000,"\p");
  88.  
  89.                         if( !(e = ResError()) ){
  90.                                 /* do a similar trick with the terminology resource,
  91.                                    so the scripting system can distinguish the standalone plugin */
  92.  
  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);
  100.  
  101.                                         ChangedResource(h);
  102.  
  103.                                         if( !(e = ResError()) ){
  104.                                                 /* add PARM resource */
  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.                                                 }
  118.                                         }
  119.                                 }
  120.  
  121.                         }
  122.  
  123.                 }
  124.                 if(!e)
  125.                         e = ResError();
  126.  
  127.                 CloseResFile(dstrn);
  128.                 CloseResFile(srcrn);
  129.         }
  130.  
  131.         return e;
  132. }
  133.  
  134. static int copyletters(char *dst,StringPtr src){
  135.         int i, n;
  136.  
  137.         for(i = src[0], n = 0; i--;)
  138.                 if(isalpha(*++src)){
  139.                         *dst++ = *src;
  140.                         ++n;
  141.                 }
  142.         return n;
  143. }
  144.  
  145. // Info.plist in new standalone copy needs to be edited -
  146. // at least the CFBundleIdentifier property must be unique
  147.  
  148. static OSErr copyplist(FSSpec *fss, short dstvol, long dstdir){
  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;
  155.  
  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);
  166.  
  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());
  176.                                                                 for(i = 8; i--;)
  177.                                                                         *p++ = 'a' + (rand() % 26);
  178.                                                         }
  179.                                                         strcpy(p,save);
  180.  
  181.                                                         count = strlen(buf);
  182.                                                         e = FSWrite(dstrn,&count,buf);
  183.  
  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;
  196. }
  197.  
  198. static OSErr make_bundle(StandardFileReply *sfr, short plugvol,
  199.                                                  long plugdir, StringPtr plugname, char *reason)
  200. {
  201.         short dstvol = sfr->sfFile.vRefNum;
  202.         long bundledir,contentsdir,macosdir,rsrcdir;
  203.         DInfo fndrInfo;
  204.         OSErr e;
  205.         FSSpec fss,macosfss,rsrcfss,rsrccopyfss;
  206.         char *why;
  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)) ){
  215.                                 if( !(e = DirCreate(dstvol,contentsdir,"\pResources",&rsrcdir)) ){
  216.                                         /* copy the Info.plist file, resource file, and executable */
  217.                                         if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::MacOS:FilterFoundry",&macosfss))
  218.                                          && !(e = FileCopy(macosfss.vRefNum,macosfss.parID,macosfss.name, dstvol,macosdir,NULL, NULL,NULL,0,false)) )
  219.                                         {
  220.                                                 /* add PARM resources to each binary, and edit PiPLs */
  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)) )
  227.                                                         {
  228.                                                                 e = copyplist(&fss,dstvol,contentsdir);
  229.                                                                 if(e){
  230.                                                                         FSpDelete(&rsrccopyfss);
  231.                                                                         why = "Can't copy Info.plist file.";
  232.                                                                 }
  233.                                                         }else why = "Can't copy resources.";
  234.                                                         if(e) HDelete(dstvol,macosdir,"\pFilterFoundry");
  235.                                                 }else why = "Can't copy FilterFoundry.rsrc file.";
  236.                                                 if(e) HDelete(dstvol,rsrcdir,plugname);
  237.                                         }else why = "Can't copy FilterFoundry executable.";
  238.                                         if(e) HDelete(dstvol,contentsdir,"\pResources");
  239.                                 }else why = "Can't create bundle Contents/Resources directory.";
  240.                                 if(e) HDelete(dstvol,contentsdir,"\pMacOS");
  241.                         }else why = "Can't create bundle Contents/MacOS directory.";
  242.                         if(e) HDelete(dstvol,bundledir,"\pContents");
  243.                 }else why = "Can't create bundle Contents directory.";
  244.                 if(e) FSpDelete(&sfr->sfFile);
  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.  
  252.         return e;
  253. }
  254.  
  255. static OSErr make_singlefile(StandardFileReply *sfr, short plugvol, long plugdir, StringPtr plugname){
  256.         OSErr e;
  257.         FSSpec origfss;
  258.  
  259.         e = FSpDelete(&sfr->sfFile);
  260.         if(e && e != fnfErr){
  261.                 alertuser("Can't replace the existing file. Try a different name or location.","");
  262.                 return userCanceledErr;
  263.         }
  264.  
  265.         if( !(e = FileCopy(plugvol,plugdir,plugname, sfr->sfFile.vRefNum,sfr->sfFile.parID,NULL, sfr->sfFile.name,NULL,0,false))
  266.          && !(e = FSMakeFSSpec(plugvol,plugdir,plugname,&origfss)) )
  267.                 /* add PARM resources, and edit PiPL */
  268.                 e = doresources(&origfss, &sfr->sfFile);
  269.  
  270.         return e;
  271. }
  272.  
  273. OSErr make_standalone(StandardFileReply *sfr){
  274.         OSErr e;
  275.         short plugvol;
  276.         long plugdir;
  277.         Str255 plugname;
  278.         char reason[0x100] = {0};
  279.  
  280.         if(!(e = GetFileLocation(CurResFile(),&plugvol,&plugdir,plugname))){
  281. #ifdef MACMACHO
  282.                 e = make_bundle(sfr,plugvol,plugdir,plugname,reason);
  283. #else
  284.                 e = make_singlefile(sfr,plugvol,plugdir,plugname);
  285. #endif
  286.         }
  287.  
  288.         if(e && e != userCanceledErr)
  289.                 alertuser("Could not create standalone plugin.",reason);
  290.  
  291.         return e;
  292. }
  293.