home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 514b.lha / GadgetED_v2.3 / PatchGE / PatchGE.c < prev    next >
C/C++ Source or Header  |  1991-06-08  |  8KB  |  303 lines

  1. /*
  2.  *  PatchGE.c version 1.0 - © 1991 Jaba Development
  3.  *
  4.  *  written in Aztec C version 5.0a by Jan van den Baard
  5.  *
  6.  *  Since I have been so stupid as not to leave any room for improvement
  7.  *  in the binary file structure of "GE" files made with GadgetED version 2.0
  8.  *  I had to write a patch program that will upgrade these files to GadgetED
  9.  *  version 2.1 and up..... sorry !
  10.  */
  11. #include <tool.h>
  12. #include "PatchGE.h"
  13. #include <functions.h>
  14.  
  15. struct OldBin
  16. {
  17.     ULONG               FileType;
  18.     ULONG               NumGads;
  19.     ULONG               ScrDepth;
  20.     BOOL                ReqGads;
  21.     BOOL                WBScreen;
  22.     ULONG               NumTexts;
  23.     USHORT              Colors[32];
  24.  
  25.     BOOL                skip_zero_planes;
  26.     BOOL                auto_size;
  27.     BOOL                image_copy;
  28.  
  29.     struct NewWindow    new_window;
  30.  
  31.     char                window_title[80];
  32.     char                window_label[32];
  33. };
  34.  
  35. struct NewBin
  36. {
  37.     ULONG               FileType;
  38.     USHORT              Version;
  39.     USHORT              Revision;
  40.     USHORT              NumGads;
  41.     USHORT              ScrDepth;
  42.     BOOL                ReqGads;
  43.     BOOL                WBScreen;
  44.     USHORT              NumTexts;
  45.     USHORT              Colors[32];
  46.     USHORT              FPen;
  47.     USHORT              BPen;
  48.     USHORT              BackFill;
  49.     USHORT              WDBackFill;
  50.     USHORT              LightSide;
  51.     USHORT              DarkSide;
  52.     USHORT              Res[2];
  53.  
  54.     BOOL                skip_zero_planes;
  55.     BOOL                auto_size;
  56.     BOOL                image_copy;
  57.     BOOL                text_copy;
  58.     BOOL                static_structures;
  59.     BOOL                no_flags;
  60.     BOOL                Res1[2];
  61.  
  62.     struct NewWindow    new_window;
  63.  
  64.     char                window_title[80];
  65.     char                window_label[32];
  66. };
  67.  
  68. struct ToolBase      *ToolBase;
  69. struct IntuitionBase *IntuitionBase;
  70. struct GfxBase       *GfxBase;
  71.  
  72. struct Window        *window = NULL;
  73. struct IntuiMessage  *msg;
  74. struct Gadget        *gadget;
  75. struct RastPort      *rport;
  76. struct FileRequester *freq = NULL;
  77. struct OldBin        *oldbin = NULL;
  78.  
  79. struct NewBin         newbin;
  80.  
  81. ULONG                 class, filesize;
  82. USHORT                code, id;
  83.  
  84. void bail()
  85. {
  86.     if(oldbin)          FreeMem(oldbin,filesize);
  87.     if(freq)            FreeFreq(freq);
  88.     if(window)          CloseWindow(window);
  89.     if(ToolBase)        CloseLibrary(ToolBase);
  90.     exit(0L);
  91. }
  92.  
  93. void setup()
  94. {
  95.     if(NOT(ToolBase = (struct ToolBase *)
  96.         OpenLibrary("tool.library",TOOL_VERSION)))
  97.         bail();
  98.  
  99.     IntuitionBase = ToolBase->IntuitionBase;
  100.     GfxBase       = ToolBase->GfxBase;
  101.  
  102.     if(NOT(freq = AllocFreq()))
  103.         bail();
  104.  
  105.     if(NOT(window = (struct Window *)OpenWindow(NEWWINDOW)))
  106.         bail();
  107.  
  108.     rport = window->RPort;
  109. }
  110.  
  111. void do_patch()
  112. {
  113.     USHORT  i;
  114.  
  115.     SetAPen(rport,0);
  116.     SetDrMd(rport,JAM1);
  117.     RectFill(rport,212,18,373,63);
  118.     SetAPen(rport,3);
  119.  
  120.     Move(rport,212,25);
  121.     Text(rport,freq->fr_FileName,strlen(freq->fr_FileName));
  122.     Move(rport,212,34);
  123.     FormatText(rport,"%ld",oldbin->NumGads);
  124.     Move(rport,212,43);
  125.     if(oldbin->ReqGads)
  126.         Text(rport,"Requester",9L);
  127.     else
  128.         Text(rport,"Window",6L);
  129.     Move(rport,212,52);
  130.     FormatText(rport,"%ld",oldbin->NumTexts);
  131.     Move(rport,212,61);
  132.     if(oldbin->WBScreen)
  133.         Text(rport,"Workbench 4 colors",18L);
  134.     else
  135.         FormatText(rport,"Custom %ld colors",(1 << oldbin->ScrDepth));
  136.  
  137.     newbin.FileType     =   'EG2+';
  138.     newbin.Version      =   2;
  139.     newbin.Revision     =   1;
  140.     newbin.NumGads      =   oldbin->NumGads;
  141.     newbin.ScrDepth     =   oldbin->ScrDepth;
  142.     newbin.ReqGads      =   oldbin->ReqGads;
  143.     newbin.WBScreen     =   oldbin->WBScreen;
  144.     newbin.NumTexts     =   oldbin->NumTexts;
  145.  
  146.     CopyMem((char *)&oldbin->Colors[0],(char *)&newbin.Colors[0],64L);
  147.  
  148.     newbin.FPen         =   1;
  149.     newbin.BPen         =   0;
  150.     newbin.BackFill     =   1;
  151.     newbin.WDBackFill   =   0;
  152.     newbin.LightSide    =   2;
  153.     newbin.DarkSide     =   1;
  154.  
  155.     for(i=0;i<2;i++)    newbin.Res[i] = 0;
  156.  
  157.     newbin.skip_zero_planes  = oldbin->skip_zero_planes;
  158.     newbin.auto_size         = oldbin->auto_size;
  159.     newbin.image_copy        = oldbin->image_copy;
  160.     newbin.text_copy         = FALSE;
  161.     newbin.static_structures = FALSE;
  162.     newbin.no_flags          = FALSE;
  163.  
  164.     for(i=0;i<2;i++)    newbin.Res1[i] = 0;
  165.  
  166.     CopyMem((char *)&oldbin->new_window,(char *)&newbin.new_window,sizeof(struct NewWindow));
  167.  
  168.     strcpy((char *)&newbin.window_title,(char *)&oldbin->window_title);
  169.     strcpy((char *)&newbin.window_label,(char *)&oldbin->window_label);
  170. }
  171.  
  172. void inform(char *text)
  173. {
  174.     SetAPen(rport,0);
  175.     SetDrMd(rport,JAM1);
  176.     RectFill(rport,212,18,373,63);
  177.     SetAPen(rport,2);
  178.     Move(rport,294-(strlen(text) << 2),43);
  179.     Text(rport,text,strlen(text));
  180. }
  181.  
  182. void load_file()
  183. {
  184.     strcpy(freq->fr_HeadLine,"Load a 'GE' file");
  185.     freq->fr_Flags = FR_NoInfo|FR_ReturnOld;
  186.  
  187.     if(oldbin)
  188.     {
  189.         FreeMem(oldbin,filesize);
  190.         oldbin          = NULL;
  191.     }
  192.  
  193.     switch(FileRequest(freq))
  194.     {
  195.         case    FREQ_OK:
  196.             inform("LOADING");
  197.                        Seek(freq->fr_Handle,0,OFFSET_END);
  198.             filesize = Seek(freq->fr_Handle,0,OFFSET_BEGINNING);
  199.  
  200.             if(oldbin = (struct OldBin *)AllocMem(filesize,MEMF_PUBLIC))
  201.             {
  202.                 if(Read(freq->fr_Handle,(char *)oldbin,filesize) <= 0)
  203.                 {
  204.                     Close(freq->fr_Handle);
  205.                     inform("READ ERROR");
  206.                     break;
  207.                 }
  208.                 if(oldbin->FileType == 'EG2+')
  209.                 {
  210.                     FreeMem(oldbin,filesize);
  211.                     oldbin = NULL;
  212.                     Close(freq->fr_Handle);
  213.                     inform("FILE ALREADY PATCHED");
  214.                     break;
  215.                 }
  216.                 if(oldbin->FileType != 'EGAD')
  217.                 {
  218.                     FreeMem(oldbin,filesize);
  219.                     oldbin = NULL;
  220.                     Close(freq->fr_Handle);
  221.                     inform("UNKNOWN FILE");
  222.                     break;
  223.                 }
  224.                 do_patch();
  225.                 Close(freq->fr_Handle);
  226.             }
  227.             break;
  228.  
  229.         case    FREQ_CANT_OPEN:
  230.             inform("FILEREQUESTER ERROR"); break;
  231.  
  232.         case    FREQ_FILE_ERROR:
  233.             inform("CANT OPEN THE FILE"); break;
  234.  
  235.         case    FREQ_CANCELED:      break;
  236.     }
  237. }
  238.  
  239. void save_file()
  240. {
  241.     if(NOT oldbin)  return;
  242.  
  243.     strcpy(freq->fr_HeadLine,"Save 'GE' patch");
  244.     freq->fr_Flags = FR_NoInfo|FR_ReturnNew;
  245.  
  246.     switch(FileRequest(freq))
  247.     {
  248.      case    FREQ_OK:
  249.         inform("SAVING");
  250.         if(Write(freq->fr_Handle,(char *)&newbin,sizeof(struct NewBin)) < 0)
  251.         {
  252.             Close(freq->fr_Handle);
  253.             inform("WRITE ERROR");
  254.             break;
  255.         }
  256.         if(Write(freq->fr_Handle,(char *)((ULONG)oldbin+(ULONG)sizeof(struct OldBin)),filesize-sizeof(struct OldBin)) < 0)
  257.         {
  258.             Close(freq->fr_Handle);
  259.             inform("WRITE ERROR");
  260.             break;
  261.         }
  262.         Close(freq->fr_Handle);
  263.         do_patch();
  264.         break;
  265.  
  266.      case    FREQ_CANT_OPEN:
  267.         inform("FILEREQUESTER ERROR"); break;
  268.  
  269.      case    FREQ_FILE_ERROR:
  270.         inform("CANT OPEN THE FILE"); break;
  271.  
  272.      case    FREQ_CANCELED:
  273.         break;
  274.     }
  275. }
  276.  
  277. void main()
  278. {
  279.     setup();
  280.  
  281.     do
  282.     {   WaitPort(window->UserPort);
  283.         while(msg = (struct IntuiMessage *)GetMsg(window->UserPort))
  284.         {
  285.             class   =   msg->Class;
  286.             code    =   msg->Code;
  287.             gadget  =   (struct Gadget *)msg->IAddress;
  288.             id      =   gadget->GadgetID;
  289.             ReplyMsg((struct Message *)msg);
  290.  
  291.             switch(id)
  292.             {
  293.                 case    LOAD_ID:    load_file();
  294.                                     break;
  295.                 case    SAVE_ID:    save_file();
  296.                                     break;
  297.             }
  298.         }
  299.     } while(id != QUIT_ID);
  300.  
  301.     bail();
  302. }
  303.