Guest User

Aika j2d

a guest
Nov 30th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.57 KB | None | 0 0
  1. // *
  2. // * @package         Aika j2d
  3. // * @copyright (c)   Dwar, 2010
  4. // * @link            ProGamerCity.net
  5. // * @author          Dwar
  6. // * @license         Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  7. // * @desc            Threads
  8. // *
  9.  
  10. unit thMain;
  11.  
  12. interface
  13.  
  14. uses
  15.   Classes, Windows,SysUtils,Forms, ShellApi;
  16.  
  17. type
  18.   TThreadMain = class(TThread)
  19.   private
  20.     FAct : short;
  21.     { Private declarations }
  22.   protected
  23.     procedure Execute; override;
  24.     procedure Sync;
  25.   public
  26.     property Act : Short read FAct write FAct;
  27.   end;
  28.  
  29. //threads var
  30. var FCount : integer;       //files count
  31.     CCount : integer;       //current progress
  32.     error     : boolean;    //if we have errors
  33.     StatusStr : string;     //string for status message
  34.     status    : short;      //status code
  35.     CurFile  : string;      //current file
  36.  
  37. implementation
  38. uses dMainForm, func, StrUtils;
  39.  
  40. procedure TThreadMain.Execute;
  41. var
  42.   i,j : integer;
  43.  
  44.   inFile, outFile  : TFileStream;
  45.   buff : array of byte;
  46.   fNAme : string;
  47.  
  48.   //header data
  49.   jHeader       : array [0..3] of AnsiChar;
  50.   jHeight       : dword;
  51.   jWidth        : dword;
  52.   fHeader       : string;
  53.   fHeaderDXT    : array [0..107] of byte;
  54.   fHeaderDXT1   : array [0..$B] of byte;
  55.   bufTmp        : array of byte;
  56.   fSize         : dword;
  57.   destDir       : string;
  58. begin
  59.   while not Terminated do
  60.     begin
  61.      FCount := 0;
  62.      CCount := 0;
  63.  
  64.      // dxt5 header
  65.      fHeader := '000004000000000009000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000040000004458543500000000000000000000000000000000000000000810400000000000000000000000000000000000';
  66.  
  67.      //converting string into array
  68.      for I := 0 to 103 do
  69.        begin
  70.          fHeaderDXT[i] := StrToInt('$'+copy(fHeader,i*2 + 1,2));
  71.        end;
  72.  
  73.      fHeader := '444453207C00000007100A00';
  74.  
  75.      for I := 0 to $B do
  76.        begin
  77.          fHeaderDXT1[i] := StrToInt('$'+copy(fHeader,i*2 + 1,2));
  78.        end;
  79.  
  80.       CCount := 0;
  81.  
  82.       //set file count for displaying in progress status on main form
  83.       FCount := MainForm.FileList.Count;
  84.  
  85.       //process all files from our list
  86.       for I := 0 to MainForm.FileList.Count - 1 do
  87.         begin
  88.           status  := 0;                                           //
  89.           error   := false;                                       //no error
  90.           fName   := MainForm.FileList.Strings[i];                //get file name from list
  91.           CurFile := ExtractFileNameEx(fNAme, true);              //get file name with extension
  92.           inFile  := TFileStream.Create(fNAme, fmShareDenyNone);  //create new file stream
  93.  
  94.           //create directory for converted files
  95.           destDir := IncludeTrailingBackslash(IncludeTrailingBackslash(ExtractFilePath(fNAme))+ 'Converted');
  96.           if not DirectoryExists(destDir) then
  97.             {$I+}MkDir(destDir);{$I-}
  98.  
  99.           //stream for output file
  100.           outFile := TFileStream.Create(destDir + ExtractFileNameEx(CurFile,false)+'.dds', fmCreate);
  101.  
  102.           //perform sync with main form
  103.           Synchronize(Sync);
  104.  
  105.           //get dword from opened file
  106.           inFile.read(jHeader, 4);
  107.  
  108.           //check header: JT31, JT33, JT35
  109.           //set some header data for output file
  110.           case AnsiIndexStr(string(jHeader),['JT31','JT33','JT35']) of
  111.                     0  : // dxt1
  112.                       begin
  113.                         fHeaderDXT[2]  := 4;
  114.                         fHeaderDXT[67] := $31;
  115.                       end;
  116.                     1  : // dxt3
  117.                       begin
  118.                         fHeaderDXT[67] := $33;
  119.                       end;
  120.                     2  :  // dxt5
  121.                       begin
  122.                         fHeaderDXT[67] := $35;
  123.                       end;
  124.           end;
  125.  
  126.           // write header 1
  127.           outFile.Position := 0;
  128.           outFile.WriteBuffer(fHeaderDXT1[0],length(fHeaderDXT1));
  129.  
  130.           // r-w dds size 8b
  131.           setLength(bufTmp, 8);
  132.           inFile.ReadBuffer   (jWidth,4);
  133.           inFile.ReadBuffer   (jHeight,4);
  134.  
  135.           outFile.WriteBuffer (jHeight,4);
  136.           outFile.WriteBuffer (jWidth,4);
  137.           fSize := jHeight*jWidth*2;
  138.  
  139.           // write header 2
  140.           outFile.WriteBuffer (fHeaderDXT[0],length(fHeaderDXT));
  141.           outFile.CopyFrom(inFile, inFile.Size - $c);
  142.  
  143.           if (outFile.Size - 128) < fSize  then
  144.             begin
  145.               setLength(bufTmp, fSize-(outFile.Size - 128));
  146.               outFile.WriteBuffer(bufTmp[0], length(bufTmp));
  147.             end;
  148.  
  149.           inFile.Free;
  150.           outFile.Free;
  151.  
  152.           Synchronize(Sync);
  153.         end;   //  for I := 0 to MainForm.FileList.Count
  154.       status := 2;
  155.       Synchronize(Sync);
  156.       //wait another operation
  157.       suspend;
  158.     end;
  159. end;
  160.  
  161.  
  162. procedure TThreadMain.Sync;
  163. var StrSig  : PWideChar;
  164. begin
  165.   with MainForm do
  166.     begin
  167.       if error then
  168.           begin
  169.             LblStatus.Caption := StatusStr;
  170.             DragAcceptFiles( Handle, True );
  171.           end
  172.       else
  173.       case status of
  174.         0:
  175.           begin
  176.             ProgressStatus.TotalParts := FCount;
  177.             ProgressStatus.PartsComplete := CCount;
  178.             LblStatus.Caption := CurFile; //wrong file
  179.           end;
  180.         2:
  181.           begin
  182.             LblStatus.Caption := 'Complete';
  183.             ProgressStatus.PartsComplete := 0;
  184.             fileList.Clear;
  185.             DragAcceptFiles( Handle, True );
  186.           end;
  187.       end;
  188.  
  189.  
  190.     end;
  191. end;
  192.  
  193. end.
Add Comment
Please, Sign In to add comment