Subversion Repositories filter_foundry

Rev

Rev 189 | Rev 194 | 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-2009 Toby Thain, toby@telegraphics.com.au
  4.     Copyright (C) 2018-2019 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_
  36.         char    *strnstr(const char *, const char *, size_t);
  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.  
  46. #ifdef MACMACHO
  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))
  51.          && ((e = FSpMakeFSRef(rsrccopy,&outref))
  52.                  || (e = FSOpenResourceFile(&outref,0/*forkNameLength*/,NULL/*forkName*/,fsWrPerm,&dstrn))) )
  53.                 CloseResFile(srcrn);
  54. #else
  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();
  64. #endif
  65.  
  66.         if(!e){
  67.                 /* create a new PiPL resource for the standalone plugin,
  68.                    with updated title and category strings */
  69.  
  70.                 if( (hpipl = Get1Resource('DATA',16000))
  71.                  && (h = Get1Resource('PiPL',16000)) )
  72.                 {
  73.                         RemoveResource(h);
  74.  
  75.                         DetachResource(hpipl);
  76.  
  77.                         PLstrcpy(title,gdata->parm.title);
  78.                         if(gdata->parm.popDialog)
  79.                                 PLstrcat(title,"\pÉ");
  80.  
  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);
  87.  
  88.                         AddResource(hpipl,'PiPL',16000,"\p");
  89.  
  90.                         if( !(e = ResError()) ){
  91.                                 /* do a similar trick with the terminology resource,
  92.                                    so the scripting system can distinguish the standalone plugin */
  93.  
  94.                                 if( (h = Get1Resource(typeAETE,AETE_ID)) ){
  95.                                         SetHandleSize(h,4096);
  96.                                         HLock(h);
  97.                                         newsize = aete_generate((unsigned char*)*h, &gdata->parm);
  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.         FILEPOS eof;
  154.         FILECOUNT count;
  155.         OSErr e;
  156.  
  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);
  167.  
  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());
  177.                                                                 for(i = 8; i--;)
  178.                                                                         *p++ = 'a' + (rand() % 26);
  179.                                                         }
  180.                                                         strcpy(p,save);
  181.  
  182.                                                         count = strlen(buf);
  183.                                                         e = FSWrite(dstrn,&count,buf);
  184.  
  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;
  197. }
  198.  
  199. static OSErr make_bundle(StandardFileReply *sfr, short plugvol,
  200.                                                  long plugdir, StringPtr plugname, char *reason)
  201. {
  202.         short dstvol = sfr->sfFile.vRefNum;
  203.         long bundledir,contentsdir,macosdir,rsrcdir;
  204.         DInfo fndrInfo;
  205.         OSErr e;
  206.         FSSpec fss,macosfss,rsrcfss,rsrccopyfss;
  207.         char *why;
  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)) ){
  216.                                 if( !(e = DirCreate(dstvol,contentsdir,"\pResources",&rsrcdir)) ){
  217.                                         /* copy the Info.plist file, resource file, and executable */
  218.                                         if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::MacOS:FilterFoundry",&macosfss))
  219.                                          && !(e = FileCopy(macosfss.vRefNum,macosfss.parID,macosfss.name, dstvol,macosdir,NULL, NULL,NULL,0,false)) )
  220.                                         {
  221.                                                 /* add PARM resources to each binary, and edit PiPLs */
  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)) )
  228.                                                         {
  229.                                                                 e = copyplist(&fss,dstvol,contentsdir);
  230.                                                                 if(e){
  231.                                                                         FSpDelete(&rsrccopyfss);
  232.                                                                         why = "Can't copy Info.plist file.";
  233.                                                                 }
  234.                                                         }else why = "Can't copy resources.";
  235.                                                         if(e) HDelete(dstvol,macosdir,"\pFilterFoundry");
  236.                                                 }else why = "Can't copy FilterFoundry.rsrc file.";
  237.                                                 if(e) HDelete(dstvol,rsrcdir,plugname);
  238.                                         }else why = "Can't copy FilterFoundry executable.";
  239.                                         if(e) HDelete(dstvol,contentsdir,"\pResources");
  240.                                 }else why = "Can't create bundle Contents/Resources directory.";
  241.                                 if(e) HDelete(dstvol,contentsdir,"\pMacOS");
  242.                         }else why = "Can't create bundle Contents/MacOS directory.";
  243.                         if(e) HDelete(dstvol,bundledir,"\pContents");
  244.                 }else why = "Can't create bundle Contents directory.";
  245.                 if(e) FSpDelete(&sfr->sfFile);
  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.  
  253.         return e;
  254. }
  255.  
  256. static OSErr make_singlefile(StandardFileReply *sfr, short plugvol, long plugdir, StringPtr plugname){
  257.         OSErr e;
  258.         FSSpec origfss;
  259.  
  260.         e = FSpDelete(&sfr->sfFile);
  261.         if(e && e != fnfErr){
  262.                 alertuser("Can't replace the existing file. Try a different name or location.","");
  263.                 return userCanceledErr;
  264.         }
  265.  
  266.         if( !(e = FileCopy(plugvol,plugdir,plugname, sfr->sfFile.vRefNum,sfr->sfFile.parID,NULL, sfr->sfFile.name,NULL,0,false))
  267.          && !(e = FSMakeFSSpec(plugvol,plugdir,plugname,&origfss)) )
  268.                 /* add PARM resources, and edit PiPL */
  269.                 e = doresources(&origfss, &sfr->sfFile);
  270.  
  271.         return e;
  272. }
  273.  
  274. OSErr make_standalone(StandardFileReply *sfr){
  275.         OSErr e;
  276.         short plugvol;
  277.         long plugdir;
  278.         Str255 plugname;
  279.         char reason[0x100] = {0};
  280.  
  281.         if(!(e = GetFileLocation(CurResFile(),&plugvol,&plugdir,plugname))){
  282. #ifdef MACMACHO
  283.                 e = make_bundle(sfr,plugvol,plugdir,plugname,reason);
  284. #else
  285.                 e = make_singlefile(sfr,plugvol,plugdir,plugname);
  286. #endif
  287.         }
  288.  
  289.         if(e && e != userCanceledErr) {
  290.                 alertuser("Could not create standalone plugin.",reason);
  291.         } else {
  292.                 showmessage("Filter was sucessfully created");
  293.         }
  294.  
  295.         return e;
  296. }
  297.