Subversion Repositories filter_foundry

Rev

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