Subversion Repositories filter_foundry

Rev

Rev 255 | Rev 268 | 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-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_
  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.         long event_id;
  46.  
  47. #ifdef MACMACHO
  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);
  55. #else
  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();
  65. #endif
  66.  
  67.         if(!e){
  68.                 /* create a new PiPL resource for the standalone plugin,
  69.                    with updated title and category strings */
  70.  
  71.                 if( (hpipl = Get1Resource('TpLT',16000))
  72.                  && (h = Get1Resource('PiPL',16000)) )
  73.                 {
  74.                         RemoveResource(h);
  75.  
  76.                         DetachResource(hpipl);
  77.  
  78.                         PLstrcpy(title,gdata->parm.title);
  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.  
  91.                         if( !(e = ResError()) ){
  92.                                 /* do a similar trick with the terminology resource,
  93.                                    so the scripting system can distinguish the standalone plugin */
  94.  
  95.                                 if( (h = Get1Resource(typeAETE,AETE_ID)) ){
  96.                                         SetHandleSize(h,4096);
  97.                                         HLock(h);
  98.                                         newsize = aete_generate((unsigned char*)*h, &gdata->parm, event_id);
  99.                                         HUnlock(h);
  100.                                         SetHandleSize(h,newsize);
  101.  
  102.                                         ChangedResource(h);
  103.  
  104.                                         if( !(e = ResError()) ){
  105.                                                 /* add PARM resource */
  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),OBFUSC_SEED_POS);
  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.                                                 }
  119.                                         }
  120.                                 }
  121.  
  122.                         }
  123.  
  124.                 }
  125.                 if(!e)
  126.                         e = ResError();
  127.  
  128.                 CloseResFile(dstrn);
  129.                 CloseResFile(srcrn);
  130.         }
  131.  
  132.         return e;
  133. }
  134.  
  135. static int copyletters(char *dst,StringPtr src){
  136.         int i, n;
  137.  
  138.         for(i = src[0], n = 0; i--;)
  139.                 if(isalpha(*++src)){
  140.                         *dst++ = *src;
  141.                         ++n;
  142.                 }
  143.         return n;
  144. }
  145.  
  146. // Info.plist in new standalone copy needs to be edited -
  147. // at least the CFBundleIdentifier property must be unique
  148.  
  149. static OSErr copyplist(FSSpec *fss, short dstvol, long dstdir){
  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;
  154.         FILEPOS eof;
  155.         FILECOUNT count;
  156.         OSErr e;
  157.  
  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);
  168.  
  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());
  178.                                                                 for(i = 8; i--;)
  179.                                                                         *p++ = 'a' + (rand() % 26);
  180.                                                         }
  181.                                                         strcpy(p,save);
  182.  
  183.                                                         count = strlen(buf);
  184.                                                         e = FSWrite(dstrn,&count,buf);
  185.  
  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;
  198. }
  199.  
  200. static OSErr make_bundle(StandardFileReply *sfr, short plugvol,
  201.                                                  long plugdir, StringPtr plugname, char *reason)
  202. {
  203.         short dstvol = sfr->sfFile.vRefNum;
  204.         long bundledir,contentsdir,macosdir,rsrcdir;
  205.         DInfo fndrInfo;
  206.         OSErr e;
  207.         FSSpec fss,macosfss,rsrcfss,rsrccopyfss;
  208.         char *why;
  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)) ){
  217.                                 if( !(e = DirCreate(dstvol,contentsdir,"\pResources",&rsrcdir)) ){
  218.                                         /* copy the Info.plist file, resource file, and executable */
  219.                                         if( !(e = FSMakeFSSpec(plugvol,plugdir,"\p::MacOS:FilterFoundry",&macosfss))
  220.                                          && !(e = FileCopy(macosfss.vRefNum,macosfss.parID,macosfss.name, dstvol,macosdir,NULL, NULL,NULL,0,false)) )
  221.                                         {
  222.                                                 /* add PARM resources to each binary, and edit PiPLs */
  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)) )
  229.                                                         {
  230.                                                                 e = copyplist(&fss,dstvol,contentsdir);
  231.                                                                 if(e){
  232.                                                                         FSpDelete(&rsrccopyfss);
  233.                                                                         why = "Can't copy Info.plist file.";
  234.                                                                 }
  235.                                                         }else why = "Can't copy resources.";
  236.                                                         if(e) HDelete(dstvol,macosdir,"\pFilterFoundry");
  237.                                                 }else why = "Can't copy FilterFoundry.rsrc file.";
  238.                                                 if(e) HDelete(dstvol,rsrcdir,plugname);
  239.                                         }else why = "Can't copy FilterFoundry executable.";
  240.                                         if(e) HDelete(dstvol,contentsdir,"\pResources");
  241.                                 }else why = "Can't create bundle Contents/Resources directory.";
  242.                                 if(e) HDelete(dstvol,contentsdir,"\pMacOS");
  243.                         }else why = "Can't create bundle Contents/MacOS directory.";
  244.                         if(e) HDelete(dstvol,bundledir,"\pContents");
  245.                 }else why = "Can't create bundle Contents directory.";
  246.                 if(e) FSpDelete(&sfr->sfFile);
  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.  
  254.         return e;
  255. }
  256.  
  257. static OSErr make_singlefile(StandardFileReply *sfr, short plugvol, long plugdir, StringPtr plugname){
  258.         OSErr e;
  259.         FSSpec origfss;
  260.  
  261.         e = FSpDelete(&sfr->sfFile);
  262.         if(e && e != fnfErr){
  263.                 alertuser(_strdup("Can't replace the existing file. Try a different name or location."),_strdup(""));
  264.                 return userCanceledErr;
  265.         }
  266.  
  267.         if( !(e = FileCopy(plugvol,plugdir,plugname, sfr->sfFile.vRefNum,sfr->sfFile.parID,NULL, sfr->sfFile.name,NULL,0,false))
  268.          && !(e = FSMakeFSSpec(plugvol,plugdir,plugname,&origfss)) )
  269.                 /* add PARM resources, and edit PiPL */
  270.                 e = doresources(&origfss, &sfr->sfFile);
  271.  
  272.         return e;
  273. }
  274.  
  275. OSErr make_standalone(StandardFileReply *sfr){
  276.         OSErr e;
  277.         short plugvol;
  278.         long plugdir;
  279.         Str255 plugname;
  280.         char reason[0x100] = {0};
  281.  
  282.         if(!(e = GetFileLocation(CurResFile(),&plugvol,&plugdir,plugname))){
  283. #ifdef MACMACHO
  284.                 e = make_bundle(sfr,plugvol,plugdir,plugname,reason);
  285. #else
  286.                 e = make_singlefile(sfr,plugvol,plugdir,plugname);
  287. #endif
  288.         }
  289.  
  290.         if(e && e != userCanceledErr) {
  291.                 alertuser(_strdup("Could not create standalone plugin."),reason);
  292.         } else {
  293.                 showmessage(_strdup("Filter was sucessfully created"));
  294.         }
  295.  
  296.         return e;
  297. }
  298.