Subversion Repositories filter_foundry

Rev

Rev 247 | Rev 258 | 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. int domanifest(char *newmanifest, const char *manifestp, PARM_T* pparm) {
  52.         char name[1024];
  53.         char description[1024];
  54.         size_t i;
  55.         int iname = 0;
  56.         int idescription = 0;
  57.  
  58.         // Description
  59.         for (i = 0; i < strlen((char*)pparm->category); i++) {
  60.                 char c = pparm->category[i];
  61.                 if ((c != '<') && (c != '>')) {
  62.                         description[idescription++] = c;
  63.                 }
  64.         }
  65.         description[idescription++] = ' ';
  66.         description[idescription++] = '-';
  67.         description[idescription++] = ' ';
  68.         for (i = 0; i < strlen((char*)pparm->title); i++) {
  69.                 char c = pparm->title[i];
  70.                 if ((c != '<') && (c != '>')) {
  71.                         description[idescription++] = c;
  72.                 }
  73.         }
  74.         description[idescription++] = '\0';
  75.  
  76.         // Name
  77.         strcpy(name, "Telegraphics.FilterFoundry.");
  78.         iname = strlen("Telegraphics.FilterFoundry.");
  79.         for (i = 0; i < strlen((char*)pparm->category); i++) {
  80.                 char c = pparm->category[i];
  81.                 if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
  82.                         name[iname++] = c;
  83.                 }
  84.         }
  85.         name[iname++] = '.';
  86.         for (i = 0; i < strlen((char*)pparm->title); i++) {
  87.                 char c = pparm->title[i];
  88.                 if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) {
  89.                         name[iname++] = c;
  90.                 }
  91.         }
  92.         name[iname++] = '\0';
  93.  
  94. #ifdef _WIN64
  95.         return sprintf(newmanifest, manifestp, (char*)name, "amd64", VERSION_STR, (char*)description);
  96. #else
  97.         return sprintf(newmanifest, manifestp, (char*)name, "x86", VERSION_STR, (char*)description);
  98. #endif
  99. }
  100.  
  101. Boolean doresources(HMODULE srcmod,char *dstname){
  102.         HRSRC datarsrc,aetersrc,manifestsrc;
  103.         HGLOBAL datah,aeteh,hupdate,manifesth;
  104.         Ptr newpipl = NULL, newaete = NULL;
  105.         LPVOID datap, aetep, manifestp;
  106.         PARM_T *pparm = NULL;
  107.         size_t piplsize,aetesize,origsize,manifestsize;
  108.         Str255 title;
  109.         LPCTSTR parm_type;
  110.         int i,parm_id;
  111.         Boolean discard = true;
  112.         LPWSTR changeRequestStr, tmp;
  113.         char* soleFilename;
  114.         long event_id;
  115.  
  116. //      if(!EnumResourceLanguages(srcmod,"PiPL",MAKEINTRESOURCE(16000),enumfunc,0))
  117. //            dbglasterror("EnumResourceLanguages");
  118.  
  119.         if( (hupdate = _BeginUpdateResource(dstname,false)) ){
  120.                 DBG("BeginUpdateResource OK");
  121.                 if( (datarsrc = FindResource(srcmod,MAKEINTRESOURCE(16000),"TPLT"))
  122.                         && (datah = LoadResource(srcmod,datarsrc))
  123.                         && (datap = (Ptr)LockResource(datah))
  124.                         && (aetersrc = FindResource(srcmod, MAKEINTRESOURCE(16000), "AETE"))
  125.                         && (aeteh = LoadResource(srcmod, aetersrc))
  126.                         && (aetep = (Ptr)LockResource(aeteh))
  127.                         && (manifestsrc = FindResource(srcmod, MAKEINTRESOURCE(50), "TPLT"))
  128.                         && (manifesth = LoadResource(srcmod, manifestsrc))
  129.                         && (manifestp = (Ptr)LockResource(manifesth)) )
  130.                 {
  131.                         char newmanifest[5000];
  132.  
  133.                         DBG("loaded DATA, PiPL");
  134.  
  135.                         PLstrcpy(title,gdata->parm.title);
  136.                         if(gdata->parm.popDialog)
  137.                                 PLstrcat(title,(StringPtr)"\003...");
  138.  
  139.                         origsize = SizeofResource(srcmod,datarsrc);
  140.  
  141.                         if( (newpipl = (Ptr)malloc(origsize+0x300))
  142.                          && (newaete = (Ptr)malloc(4096))
  143.                          && (pparm = (PARM_T*)malloc(sizeof(PARM_T))) )
  144.                         {
  145.                                 /* add user-specified title and category to new PiPL */
  146.                                 memcpy(newpipl,datap,origsize);
  147.                                 /* note that Windows PiPLs have 2 byte version datum in front
  148.                                    that isn't reflected in struct definition or Mac resource template: */
  149.                                 piplsize = fixpipl((PIPropertyList*)(newpipl+2),origsize-2,title, &event_id) + 2;
  150.  
  151.                                 /* set up the PARM resource with saved parameters */
  152.                                 memcpy(pparm,&gdata->parm,sizeof(PARM_T));
  153.  
  154.                                 /* Generate 'aete' resource (contains names of the parameters for the "Actions" tab in Photoshop) */
  155.                                 aetesize = aete_generate(newaete, pparm, event_id);
  156.  
  157.                                 /* convert to C strings for Windows PARM resource */
  158.                                 // Don't do it before aete_generate, because they need Pascal strings
  159.                                 myp2cstr(pparm->category);
  160.                                 myp2cstr(pparm->title);
  161.                                 myp2cstr(pparm->copyright);
  162.                                 myp2cstr(pparm->author);
  163.                                 for (i = 0; i < 4; ++i)
  164.                                         myp2cstr(pparm->map[i]);
  165.                                 for (i = 0; i < 8; ++i)
  166.                                         myp2cstr(pparm->ctl[i]);
  167.  
  168.                                 manifestsize = domanifest(newmanifest, (const char*)manifestp, pparm);
  169.  
  170.                                 if(gdata->obfusc){
  171.                                         parm_type = RT_RCDATA;
  172.                                         parm_id = OBFUSCDATA_ID;
  173.                                         obfusc((unsigned char*)pparm,sizeof(PARM_T),OBFUSC_SEED_POS);
  174.                                 }else{
  175.                                         parm_type = "PARM";
  176.                                         parm_id = PARM_ID;
  177.                                 }
  178.  
  179.                                 /* Attention: The resource we have found using FindResource() might have a different
  180.                                    language than the resource we are saving (Neutral), so we might end up having
  181.                                    multiple languages for the same resource. Therefore, the language "Neutral" was
  182.                                    set in the Scripting.rc file for the resource AETE and PIPL.rc for the resources PIPL. */
  183.                                 if( _UpdateResource(hupdate,"PIPL" /* note: caps!! */,MAKEINTRESOURCE(16000), MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),newpipl,(DWORD)piplsize)
  184.                                         && _UpdateResource(hupdate, "AETE" /* note: caps!! */, MAKEINTRESOURCE(16000), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), newaete, (DWORD)aetesize)
  185.                                         && _UpdateResource(hupdate, RT_MANIFEST, MAKEINTRESOURCE(1), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), newmanifest, (DWORD)manifestsize)
  186.                                         && _UpdateResource(hupdate,parm_type,MAKEINTRESOURCE(parm_id), MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),pparm,sizeof(PARM_T)) )
  187.                                 {
  188.                                         discard = false;
  189.                                 } else {
  190.                                         dbglasterror(_strdup("UpdateResource"));
  191.                                 }
  192.  
  193.                                 if (soleFilename = strrchr(dstname, '\\')) {
  194.                                     ++soleFilename;
  195.                                 } else {
  196.                                     soleFilename = dstname;
  197.                                 }
  198.  
  199.                                 // Format of argument "PCWSTR changes" is "<name>\0<value>\0<name>\0<value>\0....."
  200.                                 // You can CHANGE values for any given name
  201.                                 // You can DELETE entries by setting the value to "\b" (0x08 backspace character)
  202.                                 // You cannot (yet) ADD entries.
  203.                                 changeRequestStr = (LPWSTR)malloc(6*2*100+1);
  204.  
  205.                                 tmp = changeRequestStr ;
  206.  
  207.                                 tmp += mbstowcs(tmp, "Comments", 100);
  208.                                 tmp++;
  209.                                 tmp += mbstowcs(tmp, "Built using Filter Foundry " VERSION_STR, 100);
  210.                                 tmp++;
  211.  
  212.                                 tmp += mbstowcs(tmp, "CompanyName", 100);
  213.                                 tmp++;
  214.                                 if (strlen((char*)pparm->author) > 0) {
  215.                                         tmp += mbstowcs(tmp, (char*)pparm->author, 100);
  216.                                 } else {
  217.                                         tmp += mbstowcs(tmp, "\b", 100); // \b = remove
  218.                                 }
  219.                                 tmp++;
  220.  
  221.                                 tmp += mbstowcs(tmp, "LegalCopyright", 100);
  222.                                 tmp++;
  223.                                 if (strlen((char*)pparm->copyright) > 0) {
  224.                                         tmp += mbstowcs(tmp, (char*)pparm->copyright, 100);
  225.                                 } else {
  226.                                         tmp += mbstowcs(tmp, "\b", 100); // \b = remove
  227.                                 }
  228.                                 tmp++;
  229.  
  230.                                 tmp += mbstowcs(tmp, "FileDescription", 100);
  231.                                 tmp++;
  232.                                 if (strlen((char*)pparm->title) > 0) {
  233.                                         tmp += mbstowcs(tmp, (char*)pparm->title, 100);
  234.                                 } else {
  235.                                         tmp += mbstowcs(tmp, "Untitled filter", 100);
  236.                                 }
  237.                                 tmp++;
  238.  
  239.                                 tmp += mbstowcs(tmp, "OriginalFilename", 100);
  240.                                 tmp++;
  241.                                 tmp += mbstowcs(tmp, soleFilename, 100);
  242.                                 tmp++;
  243.  
  244.                                 tmp += mbstowcs(tmp, "License", 100);
  245.                                 tmp++;
  246.                                 tmp += mbstowcs(tmp, "\b", 100); // \b = remove, since filter is standalone and might have its own license
  247.                                 tmp++;
  248.  
  249.                                 tmp += mbstowcs(tmp, "", 1);
  250.  
  251.                                 if (UpdateVersionInfoWithHandle(dstname, hupdate, changeRequestStr) != NOERROR) {
  252.                                         alertuser(_strdup("UpdateVersionInfoWithHandle failed"),_strdup(""));
  253.                                 }
  254.  
  255.                                 free(changeRequestStr);
  256.                         }
  257.  
  258.                 }else dbglasterror(_strdup("Find-, Load- or LockResource"));
  259.  
  260.                 if(!_EndUpdateResource(hupdate,discard))
  261.                         dbglasterror(_strdup("EndUpdateResource"));
  262.  
  263.                 if(pparm) free(pparm);
  264.                 if(newpipl) free(newpipl);
  265.                 if(newaete) free(newaete);
  266.         }else
  267.                 dbglasterror(_strdup("BeginUpdateResource"));
  268.         return !discard;
  269. }
  270.  
  271. OSErr make_standalone(StandardFileReply *sfr){
  272.         Boolean res;
  273.         char dstname[0x100],srcname[MAX_PATH+1];
  274.  
  275.         if (!isWin32NT()) {
  276.                 HMODULE hLib;
  277.  
  278.                 hLib = LoadLibraryA("UNICOWS.DLL");
  279.                 if (!hLib) {
  280.                         char* sysdir;
  281.  
  282.                         sysdir = (char*)malloc(MAX_PATH);
  283.                         GetSystemDirectoryA(sysdir, MAX_PATH);
  284.                         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);
  285.                         free(sysdir);
  286.  
  287.                         return false;
  288.                 }
  289.                 else {
  290.                         FreeLibrary(hLib);
  291.                 }
  292.         }
  293.  
  294.         //FSpDelete(&sfr->sfFile);
  295.         myp2cstrcpy(dstname,sfr->sfFile.name);
  296.         res = GetModuleFileName(hDllInstance,srcname,MAX_PATH)
  297.                   && CopyFile(srcname,dstname,false)
  298.                   && doresources(hDllInstance,dstname);
  299.  
  300.         if(!res) {
  301.                 alertuser(_strdup("Could not create standalone plugin."),_strdup(""));
  302.         } else {
  303.                 showmessage(_strdup("Filter was sucessfully created"));
  304.         }
  305.  
  306.         return res ? ioErr : noErr;
  307. }
  308.