mkv

IPS.PAS

mkv
Sep 25th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 8.88 KB | None | 0 0
  1. {$M 8192,0,0}
  2.  
  3. Program IPS_Translate;
  4.  
  5. uses Dos, Crt;
  6.  
  7. type
  8.   ScreenType = array[0..3999] of Byte;
  9.  
  10. const
  11.   IPSHeader = 'PATCH';
  12.   AddressBytes = 3;
  13.   MaxArraySize =  50000;
  14.  
  15. var
  16.   Screen         : ScreenType absolute $B800:0000;
  17.   SourceName     : string;
  18.   PatchName      : string;
  19.   PatchFile      : file;
  20.   SourceFile     : file;
  21.   f              : file;
  22.   LogFile        : text;
  23.   header         : array[1..5] of char;
  24.   AddArray       : array[1..3] of byte;
  25.   RLEArray       : array[1..3] of byte;
  26.   BytesArray     : array[1..2] of byte;
  27.   ChunkArray     : array[1..MaxArraySize] of byte;
  28.   Address        : longint;
  29.   NumBytes       : longint;
  30.   RLEBytes       : longint;
  31.   RLEByte        : byte;
  32.   Count          : longint;
  33.   TotalChunks    : integer;
  34.   a1,a2,a3       : longint;
  35.   h1,h2,h3       : string;
  36.   s              : string;
  37.   message        : string;
  38.   Verbose,Backup : boolean;
  39.   Exist          : boolean;
  40.   Overload       : boolean;
  41.  
  42.  
  43. Procedure CloseFiles;
  44.   begin
  45.     if verbose = true then
  46.       begin
  47.         WriteLn(LogFile, 'ÀÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÙ');
  48.         Writeln(LogFile);
  49.         Writeln(LogFile,'Total Chunks:  ',totalchunks);
  50.       end;
  51.     Close(SourceFile);
  52.     Close(PatchFile);
  53.     If Verbose = True then
  54.       Close(LogFile);
  55.     GotoXY(53,13);
  56.     TextColor (White);
  57.     WriteLn('Patching Complete !!!');
  58.     GotoXY(1,20);
  59.     If Overload = True then
  60.       begin
  61.         TextColor(LightGray);
  62.         Writeln('The patch file has requested a patch size greater then the');
  63.         WriteLn('maximum patch size this version can handle.  The maximum');
  64.         WriteLn('patch size is currently set to ',MaxArraySize);
  65.         WriteLn;
  66.       end;
  67.     halt;
  68.   end;
  69.  
  70. Procedure exit_program;
  71.   {$I IPS.SCR}
  72.   begin
  73.     Move(ImageData,Screen,4000);
  74.     GotoXY(1,19);
  75.     If Exist = False then
  76.     begin
  77.       WriteLn (s,' Not Found!');
  78.     end;
  79.     If Message <> '' then
  80.       writeln(message);
  81.     halt;
  82.   end;
  83.  
  84. Function Hex(the_Byte:byte):String;
  85.   var
  86.     code       : integer;
  87.     low,high   : integer;
  88.     lowH,highH : string[1];
  89.   begin
  90.     low := (the_byte and 15);
  91.     high := (the_byte and 240);
  92.     high := high div 16;
  93.     case low of
  94.       10 : lowH := 'A';
  95.       11 : lowH := 'B';
  96.       12 : lowH := 'C';
  97.       13 : lowH := 'D';
  98.       14 : lowH := 'E';
  99.       15 : lowH := 'F'
  100.     else
  101.       str(low,lowH);
  102.     end;
  103.  
  104.     case high of
  105.       10 : highH := 'A';
  106.       11 : highH := 'B';
  107.       12 : highH := 'C';
  108.       13 : highH := 'D';
  109.       14 : highH := 'E';
  110.       15 : highH := 'F'
  111.     else
  112.       str(high,highH);
  113.     end;
  114.  
  115.     Hex := highH+lowH;
  116.   end;
  117.  
  118. Procedure Display_Stuff;
  119.   begin
  120.     TextColor(LightGreen);
  121.     GotoXY(70,11);
  122.     Write(TotalChunks:4);
  123.     GotoXY (22,13);
  124.     Write (Address:8);
  125.     GotoXY (46,11);
  126.     If NumBytes <> 0
  127.     then
  128.       Write (NumBytes:5)
  129.     else
  130.       Write (RLEBytes:5);
  131.     if Verbose = True
  132.     then
  133.       if NumBytes <> 0
  134.       then
  135.         Writeln(LogFile, NumBytes:5, ' ³          ³')
  136.       else
  137.         Writeln;
  138.   end;
  139.  
  140. Procedure IsItAnIPS;
  141.   var
  142.     checksum : integer;
  143.   begin
  144.     Blockread(PatchFile,Header,SizeOf(Header));
  145.     If Header <> IPSHeader then
  146.       begin
  147.         Writeln;
  148.         Writeln;
  149.         Writeln('Your patch file is not a valid IPS');
  150.         Writeln;
  151.         halt;
  152.       end;
  153.   end;
  154.  
  155. Procedure GetAddress;
  156.   begin
  157.     BlockRead(PatchFile,AddArray,3);
  158.     if (AddArray[1] = 69) and (AddArray[2] = 79) and (AddArray[3] = 70) then
  159.       CloseFiles;
  160.     Inc(TotalChunks,1);
  161.     h1:=''; h2:=''; h3:='';
  162.     h1 := hex(AddArray[1]);
  163.     h2 := hex(AddArray[2]);
  164.     h3 := hex(AddArray[3]);
  165.     a1 := AddArray[1];
  166.     a2 := AddArray[2];
  167.     a3 := AddArray[3];
  168.     Address := a3;
  169.     Address := Address + (a2 * 256);
  170.     Address := Address + (a1 * 65536);
  171.     TextColor(LightGreen);
  172.     GotoXY (22,11);
  173.     s := h1+h2+h3;
  174.     Write(s:6);
  175.     If Verbose = true then
  176.       begin
  177.         Write (LogFile,'³',Address:8,'  ³ ');
  178.         Write (LogFile,s:7,'  ³ ');
  179.       end;
  180.   end;
  181.  
  182. Procedure GetRLE;
  183.   var
  184.     r1,r2 : longint;
  185.   begin
  186.     BlockRead(PatchFile,RLEArray,3);
  187.     r1 := RLEArray[1];
  188.     r2 := RLEArray[2];
  189.     RLEBytes := r2;
  190.     RLEBytes := RLEBytes + r1 * 256;
  191.     RLEByte  := RLEArray[3];
  192.     if Verbose = true then
  193.       begin
  194.         Write(LogFile, RLEBytes:5, ' ³ ');
  195.         WriteLn(LogFile, '   ',RLEByte:3, '   ³');
  196.       end;
  197.   end;
  198.  
  199. Procedure GetNumBytes;
  200.   var
  201.     b1,b2 : longint;
  202.   begin
  203.     BlockRead(PatchFile,BytesArray,2);
  204.     b1 := BytesArray[1];
  205.     b2 := BytesArray[2];
  206.     NumBytes := b2;
  207.     NumBytes := NumBytes + b1 * 256;
  208.     if NumBytes = 0 then
  209.       GetRLE;
  210.     if NumBytes > MaxArraySize then
  211.       begin
  212.         Overload := True;
  213.         Display_Stuff;
  214.         CloseFiles
  215.       end;
  216.   end;
  217.  
  218. Procedure GetChunk;
  219.   begin
  220.     BlockRead(PatchFile,ChunkArray,NumBytes);
  221.   end;
  222.  
  223. Procedure ApplyPatch;
  224.   begin
  225.     Seek(SourceFile,Address);
  226.     BlockWrite (SourceFile, ChunkArray, NumBytes);
  227.   end;
  228.  
  229. Procedure ApplyRLE;
  230.   begin
  231.     Seek(SourceFile,Address);
  232.     if RLEBytes > MaxArraySize then
  233.       begin
  234.         for count := 1 to MaxArraySize do
  235.           ChunkArray[count] := RLEByte;
  236.         BlockWrite (SourceFile, ChunkArray, MaxArraySize);
  237.         RLEBytes := RLEBytes - MaxArraySize;
  238.       end;
  239.     for count := 1 to RLEBytes do
  240.       ChunkArray[count] := RLEByte;
  241.     BlockWrite (SourceFile, ChunkArray, RLEBytes);
  242.   end;
  243.  
  244. Procedure Backup_Source;
  245.   begin
  246.     TextColor(Yellow);
  247.     GotoXY(53,13);
  248.     Write('Copying Source');
  249.     SwapVectors;
  250.     Exec(GetEnv('COMSPEC'), '/c copy ' + SourceName + ' SRC_BAK.TMP > nul');
  251.     SwapVectors;
  252.   end;
  253.  
  254. Procedure Display_Main;
  255.   {$I IPSMAIN.SCR}
  256.   begin
  257.     Move(MainData,Screen,4000);
  258.     TextBackground(Black);
  259.     TextColor (LightCyan);
  260.     GotoXY(22,7);
  261.     Write(SourceName);
  262.     GotoXY(22,9);
  263.     Write(PatchName);
  264.   end;
  265.  
  266. (**************************************************************************)
  267.  
  268. begin
  269.   Exist := True;
  270.   Verbose := False;
  271.   Backup  := False;
  272.   TotalChunks := 0;
  273.   Overload := False;
  274.   message := '';
  275.  
  276.   If (ParamCount < 2) or (ParamCount > 4) then
  277.     exit_program;
  278.  
  279.   SourceName := ParamStr(1);
  280.   for count := 1 to length(SourceName) do
  281.     SourceName[count] := Upcase(SourceName[count]);
  282.   {$I-}
  283.   Assign(f, SourceName);
  284.   Reset(f);
  285.   Close(f);
  286.   {$I+}
  287.   if IOResult <> 0 then
  288.     begin
  289.       Exist := False;
  290.       s := SourceName;
  291.       exit_Program
  292.     end;
  293.   If SourceName[1] = '/' then
  294.     exit_program;
  295.   Assign (SourceFile, SourceName);
  296.   Reset (SourceFile,1);
  297.   PatchName := ParamStr(2);
  298.   for count := 1 to length(PatchName) do
  299.     PatchName[count] := Upcase(PatchName[count]);
  300.   {$I-}
  301.   Assign(f, PatchName);
  302.   Reset(f);
  303.   Close(f);
  304.   {$I+}
  305.   if IOResult <> 0 then
  306.     begin
  307.       Exist := False;
  308.       s:=PatchName;
  309.       exit_Program
  310.     end;
  311.   If PatchName[1] = '/' then
  312.     exit_program;
  313.   If SourceName = PatchName then
  314.     begin
  315.       message := 'Source file and Patch file MUST be different!!';
  316.       exit_program
  317.     end;
  318.   Assign  (PatchFile,  PatchName);
  319.   Reset   (PatchFile,1);
  320.  
  321.   If ParamCount >= 3 then
  322.     begin
  323.       s := ParamStr(3);
  324.       if (s = '/l') or (s = '/L') then
  325.         Verbose := True;
  326.       if (s = '/b') or (s = '/B') then
  327.         Backup := True;
  328.     end;
  329.  
  330.   If ParamCount = 4 then
  331.     begin
  332.       s := ParamStr(4);
  333.       if (s = '/l') or (s = '/L') then
  334.         Verbose := True;
  335.       if (s = '/b') or (s = '/B') then
  336.         Backup := True;
  337.     end;
  338.  
  339.   IsItAnIPS;
  340.  
  341.   Display_Main;
  342.  
  343.   If Backup = True then
  344.     Backup_Source;
  345.  
  346.   If Verbose = True then
  347.     begin
  348.       TextColor(LightMagenta);
  349.       gotoXY(53,13);
  350.       Write('Creating log file');
  351.       Assign(LogFile,'LOGFILE.TXT');
  352.       ReWrite(LogFile);
  353.       Writeln(LogFile);
  354.       Writeln(LogFile,'Created with International Patch Translator PLUS v1.2');
  355.       WriteLn(LogFile,'Presented by the WaveMaster.  $CePTiC, 1993');
  356.       WriteLn(LogFile,'Call Spatula City: 707-451-2835');
  357.       WriteLn(LogFile);
  358.       Write(LogFile,'Source File: ');
  359.       WriteLn(LogFile,SourceName);
  360.       Write(LogFile,'Patch File : ');
  361.       WriteLn(LogFile,PatchName);
  362.       WriteLn(LogFile);
  363.       WriteLn(LogFile, 'ÚÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄ¿');
  364.       WriteLn(LogFile, '³ ADDR dec ³ ADDR hex ³ Bytes ³ RLE byte ³');
  365.       WriteLn(LogFile, 'ÃÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄ´');
  366.     end;
  367.  
  368.   While not EOF(PatchFile) do
  369.   begin
  370.     GetAddress;
  371.     GetNumBytes;
  372.     Display_Stuff;
  373.     If NumBytes <> 0
  374.     then
  375.       GetChunk;
  376.     If NumBytes <> 0
  377.     then
  378.       ApplyPatch
  379.     else
  380.       ApplyRLE;
  381.   end;
  382.  
  383.   CloseFiles;
  384.  
  385. end.
Advertisement
Add Comment
Please, Sign In to add comment