Subversion Repositories filter_foundry

Rev

Rev 171 | Rev 185 | 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 "ff.h"
  21.  
  22. #include "file_compat.h"
  23. #include "compat_string.h"
  24. #include "versioninfo_modify_win.h"
  25. #include "version.h"
  26.  
  27. extern HANDLE hDllInstance;
  28.  
  29. Boolean doresources(HMODULE srcmod,char *dstname);
  30.  
  31. void dbglasterror(char *func){
  32.         char s[0x100];
  33.  
  34.         strcpy(s,func);
  35.         strcat(s," failed: ");
  36.         FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),0,s+strlen(s),0x100,NULL );
  37.         dbg(s);
  38. }
  39.  
  40. /*
  41. BOOL CALLBACK enumfunc(HMODULE hModule,LPCTSTR lpszType,LPCTSTR lpszName,WORD wIDLanguage,LONG lParam){
  42.         char s[0x100];
  43.         sprintf(s,"EnumResourceLanguages callback: module=%#x type=%s name=%s lang=%d",
  44.                 hModule,lpszType,lpszName,wIDLanguage);
  45.         dbg(s);
  46.         return TRUE;
  47. }
  48. */
  49.  
  50. Boolean doresources(HMODULE srcmod,char *dstname){
  51.         HRSRC datarsrc,aetersrc;
  52.         HANDLE datah,aeteh,hupdate;
  53.         Ptr newpipl = NULL,newaete = NULL,datap,aetep;
  54.         PARM_T *pparm = NULL;
  55.         long piplsize,aetesize,origsize;
  56.         Str255 title;
  57.         LPCTSTR parm_type;
  58.         int i,parm_id;
  59.         Boolean discard = true;
  60.         LPWSTR changeRequestStr, tmp;
  61.         char* soleFilename;
  62.  
  63.         if (!isWin32NT()) {
  64.                 HMODULE hLib;
  65.  
  66.                 hLib = LoadLibraryA("UNICOWS.DLL");
  67.                 if (!hLib) {
  68.                         char* sysdir;
  69.  
  70.                         sysdir = (char*)malloc(MAX_PATH);
  71.                         GetSystemDirectoryA(sysdir, MAX_PATH);
  72.                         alertuser("To build standalone plugins using this version of\nWindows, you need to install UNICOWS.DLL\n\nPlease download it from the Internet\nand place it into following directory:",sysdir);
  73.                         free(sysdir);
  74.  
  75.                         return false;
  76.                 } else {
  77.                         FreeLibrary(hLib);
  78.                 }
  79.         }
  80.  
  81. //      if(!EnumResourceLanguages(srcmod,"PiPL",MAKEINTRESOURCE(16000),enumfunc,0))
  82. //              dbglasterror("EnumResourceLanguages");
  83.  
  84.         if( (hupdate = _BeginUpdateResource(dstname,false)) ){
  85.                 DBG("BeginUpdateResource OK");
  86.                 if( (datarsrc = FindResource(srcmod,MAKEINTRESOURCE(16000),RT_RCDATA))
  87.                         && (datah = LoadResource(srcmod,datarsrc))
  88.                         && (datap = LockResource(datah))
  89.                         && (aetersrc = FindResource(srcmod,MAKEINTRESOURCE(16000),"AETE"))
  90.                         && (aeteh = LoadResource(srcmod,aetersrc))
  91.                         && (aetep = LockResource(aeteh)) )
  92.                 {
  93.                         DBG("loaded DATA, PiPL");
  94.  
  95.                         PLstrcpy(title,gdata->parm.title);
  96.                         if(gdata->parm.popDialog)
  97.                                 PLstrcat(title,(StringPtr)"\003...");
  98.  
  99.                         origsize = SizeofResource(srcmod,datarsrc);
  100.  
  101.                         if( (newpipl = malloc(origsize+0x300))
  102.                          && (newaete = malloc(4096))
  103.                          && (pparm = malloc(sizeof(PARM_T))) )
  104.                         {
  105.                                 /* add user-specified title and category to new PiPL */
  106.                                 memcpy(newpipl,datap,origsize);
  107.                                 /* note that Windows PiPLs have 2 byte version datum in front
  108.                                    that isn't reflected in struct definition or Mac resource template: */
  109.                                 piplsize = fixpipl((PIPropertyList*)(newpipl+2),origsize-2,title) + 2;
  110.  
  111.                                 /* set up the PARM resource with saved parameters */
  112.                                 memcpy(pparm,&gdata->parm,sizeof(PARM_T));
  113.  
  114.                                 /* convert to C strings for Windows PARM resource */
  115.                                 myp2cstr(pparm->category);
  116.                                 myp2cstr(pparm->title);
  117.                                 myp2cstr(pparm->copyright);
  118.                                 myp2cstr(pparm->author);
  119.                                 for(i=0;i<4;++i)
  120.                                         myp2cstr(pparm->map[i]);
  121.                                 for(i=0;i<8;++i)
  122.                                         myp2cstr(pparm->ctl[i]);
  123.  
  124.                                 /* Generate 'aete' resource (contains names of the parameters for the "Actions" tab in Photoshop) */
  125.                                 aetesize = aete_generate(newaete, pparm);
  126.  
  127.                                 if(gdata->obfusc){
  128.                                         parm_type = RT_RCDATA;
  129.                                         parm_id = OBFUSCDATA_ID;
  130.                                         obfusc((unsigned char*)pparm,sizeof(PARM_T));
  131.                                 }else{
  132.                                         parm_type = "PARM";
  133.                                         parm_id = PARM_ID;
  134.                                 }
  135.  
  136.                                 /* Attention: The resource we have found using FindResource() might have a different
  137.                                    language than the resource we are saving (Neutral), so we might end up having
  138.                                    multiple languages for the same resource. Therefore, the language "Neutral" was
  139.                                    set in the PIPL.RC file, for the resources PIPL and AETE. */
  140.                                 if( _UpdateResource(hupdate,"PIPL" /* note: caps!! */,MAKEINTRESOURCE(16000),
  141.                                                                    MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),newpipl,piplsize)
  142.                                  && _UpdateResource(hupdate,"AETE" /* note: caps!! */,MAKEINTRESOURCE(16000),
  143.                                                                    MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),newaete,aetesize)
  144.                                  && _UpdateResource(hupdate,parm_type,MAKEINTRESOURCE(parm_id),
  145.                                                                    MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),pparm,sizeof(PARM_T)) )
  146.                                         discard = false;
  147.                                 else
  148.                                         dbglasterror("UpdateResource");
  149.  
  150.                                 if (soleFilename = strrchr(dstname, '\\')) {
  151.                                     ++soleFilename;
  152.                                 } else {
  153.                                     soleFilename = dstname;
  154.                                 }
  155.  
  156.                                 // Format of argument "PCWSTR changes" is "<name>\0<value>\0<name>\0<value>\0....."
  157.                                 // You can CHANGE values for any given name
  158.                                 // You can DELETE entries by setting the value to "\b" (0x08 backspace character)
  159.                                 // You cannot (yet) ADD entries.
  160.                                 changeRequestStr = (LPWSTR)malloc(6*2*100+1);
  161.  
  162.                                 tmp = changeRequestStr ;
  163.  
  164.                                 tmp += mbstowcs(tmp, "Comments", 100);
  165.                                 tmp++;
  166.                                 tmp += mbstowcs(tmp, "Built using Filter Foundry " VERSION_STR, 100);
  167.                                 tmp++;
  168.  
  169.                                 tmp += mbstowcs(tmp, "CompanyName", 100);
  170.                                 tmp++;
  171.                                 tmp += mbstowcs(tmp, pparm->author, 100);
  172.                                 tmp++;
  173.  
  174.                                 tmp += mbstowcs(tmp, "LegalCopyright", 100);
  175.                                 tmp++;
  176.                                 tmp += mbstowcs(tmp, pparm->copyright, 100);
  177.                                 tmp++;
  178.  
  179.                                 tmp += mbstowcs(tmp, "FileDescription", 100);
  180.                                 tmp++;
  181.                                 tmp += mbstowcs(tmp, pparm->title, 100);
  182.                                 tmp++;
  183.  
  184.                                 tmp += mbstowcs(tmp, "OriginalFilename", 100);
  185.                                 tmp++;
  186.                                 tmp += mbstowcs(tmp, soleFilename, 100);
  187.                                 tmp++;
  188.  
  189.                                 tmp += mbstowcs(tmp, "License", 100);
  190.                                 tmp++;
  191.                                 tmp += mbstowcs(tmp, "\b", 100); // \b = remove, since filter is standalone and might have its own license
  192.                                 tmp++;
  193.  
  194.                                 tmp += mbstowcs(tmp, "", 1);
  195.  
  196.                                 if (UpdateVersionInfoWithHandle(dstname, hupdate, changeRequestStr) != NOERROR) {
  197.                                         alertuser("UpdateVersionInfoWithHandle failed","");
  198.                                 }
  199.  
  200.                                 free(changeRequestStr);
  201.                         }
  202.  
  203.                 }else dbglasterror("Find-, Load- or LockResource");
  204.  
  205.                 if(!_EndUpdateResource(hupdate,discard))
  206.                         dbglasterror("EndUpdateResource");
  207.  
  208.                 if(pparm) free(pparm);
  209.                 if(newpipl) free(newpipl);
  210.                 if(newaete) free(newaete);
  211.         }else
  212.                 dbglasterror("BeginUpdateResource");
  213.         return !discard;
  214. }
  215.  
  216. OSErr make_standalone(StandardFileReply *sfr){
  217.         Boolean res;
  218.         char dstname[0x100],srcname[MAX_PATH+1];
  219.  
  220.         //FSpDelete(&sfr->sfFile);
  221.         myp2cstrcpy(dstname,sfr->sfFile.name);
  222.         res = GetModuleFileName(hDllInstance,srcname,MAX_PATH)
  223.                   && CopyFile(srcname,dstname,false)
  224.                   && doresources(hDllInstance,dstname);
  225.  
  226.         if(!res) {
  227.                 alertuser("Could not create standalone plugin.","");
  228.         } else {
  229.                 showmessage("Filter was sucessfully created");
  230.         }
  231.  
  232.         return res ? ioErr : noErr;
  233. }
  234.