Advertisement
mixster

mixster

Oct 19th, 2009
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.00 KB | None | 0 0
  1. const
  2.   CheckVersion = True;
  3.   DownloadSCAR = False;
  4.   DownloadSRL = True;
  5.   MoveSRLPlugins = True;
  6.   //SCARDirectory = 'C:\Program Files\SCAR 3.22 pre\';
  7.   SCARDirectory = AppPath;
  8.   SRLDirectory = SCARDirectory + 'Includes\SRL\';
  9.  
  10. type
  11.   TFile = record
  12.     path, filename: string;
  13.   end;
  14.  
  15.   TSVNFile = record
  16.     local, remote: TFile;
  17.   end;
  18.  
  19.   TMatch = record
  20.     start, finish: string;
  21.   end;
  22.  
  23.   TFilter = record
  24.     exp, dir, fil: string;
  25.     local, remote: TMatch;
  26.   end;
  27.  
  28.   TSVN = record
  29.     name: string;
  30.     skip: TStringArray;
  31.     baseloc: TSVNFile;
  32.     files: array of TSVNFile;
  33.     filter: TFilter;
  34.     rev: TMatch;
  35.     col: Boolean;
  36.   end;
  37.  
  38. function srl_Explode(str, del: string): TStringArray;
  39. {$IFNDEF SCAR320_UP}
  40. var
  41.   i, l, dL: Integer;
  42. begin
  43.   i := 0;
  44.   l := -1;
  45.   SetLength(Result, 0);
  46.   if (str = '') then
  47.     Exit;
  48.   dL := Length(del) - 1;
  49.   repeat
  50.     Inc(l);
  51.     SetLength(Result, l + 1);
  52.     i := Pos(del, str);
  53.     if i <= 0 then
  54.       Break;
  55.     Result[l] := Copy(str, 1, i - 1);
  56.     Delete(str, 1, i + dL);
  57.   until false
  58.   Result[l] := Copy(str, 1, Length(str));
  59. {$ELSE}
  60. begin
  61.   Result := Explode(del, str);
  62. {$ENDIF}
  63. end;
  64.  
  65. function ReturnPage(svn: TSVN; dir: TSVNFile): string;
  66. var
  67.   url: string;
  68. begin
  69.   url := svn.baseloc.remote.path + dir.remote.path;
  70.  
  71.   try
  72.     Result := GetPage(url);
  73.   except
  74.     Writeln('Downloading url "' + url + '" failed');
  75.     Result := '';
  76.   end;
  77. end;
  78.  
  79. procedure RawCollectSVN(var svn: TSVN; cur_dir: TSVNFile);
  80. var
  81.   input, tmpLoc, tmpRem: string;
  82.   split: TStringArray;
  83.   h, i, t: Integer;
  84.   new_dir: TSVNFile;
  85.   sk: Boolean;
  86. begin
  87.   input := ReturnPage(svn, cur_dir);
  88.   split := srl_Explode(input, svn.filter.exp);
  89.  
  90.   for i := 0 to High(split) do
  91.   begin
  92.     for h := 0 to High(svn.skip) do
  93.     begin
  94.       sk := Pos(svn.skip[h], split[i]) > 0;
  95.       if sk then
  96.         Break;
  97.     end;
  98.    
  99.     if sk then
  100.       Continue;
  101.    
  102.     tmpLoc := Between(svn.filter.local.start, svn.filter.local.finish, split[i]);
  103.     tmpRem := Between(svn.filter.remote.start, svn.filter.remote.finish, split[i]);
  104.    
  105.     if (tmpLoc = '') or (tmpRem = '') then
  106.       Continue;
  107.      
  108.     if Pos(svn.filter.dir, split[i]) > 0 then // Handle directory
  109.     begin
  110.       new_dir.local.path := cur_dir.local.path + tmpLoc + '\';
  111.       new_dir.remote.path := cur_dir.remote.path + tmpRem;
  112.       Writeln('Discovered directory "' + tmpLoc + '"');
  113.       t := GetDebugLineCount - 1;
  114.       RawCollectSVN(svn, new_dir);
  115.       ReplaceDebugLine(t, 'Finished collecting from "' + tmpLoc + '"');
  116.     end
  117.     else if Pos(svn.filter.fil, split[i]) > 0 then // Handle file
  118.     begin;
  119.       h := Length(svn.files);
  120.       SetLength(svn.files, h + 1);
  121.       svn.files[h].local.path := cur_dir.local.path;
  122.       svn.files[h].local.filename := tmpLoc;
  123.       svn.files[h].remote.path := cur_dir.remote.path;
  124.       svn.files[h].remote.filename := tmpRem;
  125.     end;
  126.   end;
  127. end;
  128.  
  129. procedure CollectSVN(var svn: TSVN);
  130. var
  131.   start: TSVNFile;
  132.   t: Integer;
  133. begin
  134.   Writeln('Now starting to collect "' + svn.name + '"');
  135.   t := GetSystemTime;
  136.   RawCollectSVN(svn, start);
  137.   svn.col := True;
  138.   t := GetSystemTime - t;
  139.   Writeln('Finished collecting "' + svn.name + '" in ' + IntToStr(t) + ' ms');
  140.   Writeln('A total of ' + IntToStr(Length(svn.files)) + ' files were collected');
  141. end;
  142.  
  143. procedure DownloadSVN(svn: TSVN);
  144. var
  145.   i, f, c, l, t, line: Integer;
  146. begin
  147.   Writeln('Now starting to download "' + svn.name + '"');
  148.   Writeln('');
  149.   line := GetDebugLineCount - 1;
  150.  
  151.   t := GetSystemTime;
  152.   c := 0;
  153.   l := Length(svn.files);
  154.   for i := 0 to l - 1 do
  155.     try
  156.       c := c + 1;
  157.       if c mod 10 = 0 then
  158.         ReplaceDebugLine(line, 'Downloading file ' + IntToStr(c) + ' of ' + IntToStr(l));
  159.       f := RewriteFile(svn.baseloc.local.path + svn.files[i].local.path + svn.files[i].local.filename, False);
  160.       WriteFileString(f, GetPage(svn.baseloc.remote.path + svn.files[i].remote.path + svn.files[i].remote.filename));
  161.       CloseFile(f);
  162.     except
  163.       Writeln('Failed to download file.');
  164.     end;
  165.   t := GetSystemTime - t;
  166.   Writeln('Finished downloading "' + svn.name + '" in ' + IntToStr(t) + ' ms');
  167. end;
  168.  
  169. function ReturnSRLSVN: TSVN;
  170. begin
  171.   Result.name := 'SRL';
  172.   Result.baseloc.remote.path := 'http://www.villavu.com/repositories/srl-opendev/';
  173.   Result.filter.exp := '<';
  174.   Result.filter.dir := 'dir';
  175.   Result.filter.fil := 'file';
  176.   Result.filter.local.start := 'name="';
  177.   Result.filter.local.finish := '"';
  178.   Result.filter.remote.start := 'href="';
  179.   Result.filter.remote.finish := '"';
  180.   Result.rev.start := 'rev="';
  181.   Result.rev.finish := '"';
  182.   Result.skip := [];
  183. end;
  184.  
  185. function ReturnSCARSVN: TSVN;
  186. begin
  187.   Result.name := 'SCAR';
  188.   Result.baseloc.remote.path := 'http://freddy1990.com/svn/scarprerelease/';
  189.   Result.filter.exp := '<a';
  190.   Result.filter.dir := '/"';
  191.   Result.filter.fil := '.';
  192.   Result.filter.local.start := '">';
  193.   Result.filter.local.finish := '<';
  194.   Result.filter.remote.start := 'href="';
  195.   Result.filter.remote.finish := '"';
  196.   Result.rev.start := 'Revision ';
  197.   Result.rev.finish := ':';
  198.   Result.skip := ['http', '..'];
  199. end;
  200.  
  201. function GetSVNVersion(svn: TSVN): Integer;
  202. var
  203.   input: string;
  204. begin
  205.   input := GetPage(svn.baseloc.remote.path);
  206.   Result := StrToIntDef(Between(svn.rev.start, svn.rev.finish, input), 0);
  207. end;
  208.  
  209. procedure WriteSVNVersion(svn: TSVN);
  210. var
  211.   rev: Integer;
  212. begin
  213.   rev := GetSVNVersion(svn);
  214.   if rev = 0 then
  215.   begin
  216.     Writeln('Collecting rev number failed');
  217.     Writeln('You can manually set svn_settings.ini if this results in a problem');
  218.     exit;
  219.   end;
  220.  
  221.   WriteINI(svn.name, 'REVISION', IntToStr(rev), SCARDirectory + 'svn_settings.ini');
  222. end;
  223.  
  224. function IsSVNOld(svn: TSVN): Boolean;
  225. var
  226.   curRev, locRev: Integer;
  227. begin
  228.   locRev := StrToIntDef(ReadINI(svn.name, 'REVISION', AppPath + 'svn_settings.ini'), 0);
  229.   curRev := GetSVNVersion(svn);
  230.   if curRev = 0 then
  231.   begin
  232.     Writeln('Collecting current rev number failed');
  233.     Writeln('Assuming newer SVN is on local system to avoid problems');
  234.     Result := False;
  235.     exit;
  236.   end;
  237.  
  238.   Result := locRev < curRev;
  239.  
  240.   if Result then
  241.     Writeln(svn.name + ' has the latest rev downloaded')
  242.   else
  243.     Writeln(svn.name + ' is rev ' + IntToStr(locRev) + ' and latest is ' + IntToStr(curRev));
  244. end;
  245.  
  246. procedure MovePlugins(svn: TSVN; dest: string);
  247. var
  248.   i, f: Integer;
  249.   s: string;
  250.   o: Boolean;
  251. begin
  252.   for i := 0 to High(svn.files) do
  253.     if ExtractFileExt(svn.files[i].local.filename) = '.dll' then
  254.       try
  255.         Writeln('Found plugin "' + svn.files[i].local.filename);
  256.         o := False;
  257.         f := OpenFile(svn.baseloc.local.path + svn.files[i].local.path + svn.files[i].local.filename, False);
  258.         ReadFileString(f, s, FileSize(f));
  259.         CloseFile(f);
  260.         o := True;
  261.         f := RewriteFile(dest + svn.files[i].local.filename, False);
  262.         WriteFileString(f, s);
  263.         CloseFile(f);
  264.       except
  265.         Writeln('Failed to save plugin.');
  266.         if o then
  267.           Writeln('Problem involved rewriting the file at target folder')
  268.         else
  269.           Writeln('Problem involved opening the sauce file to read');
  270.       end;
  271. end;
  272.  
  273. var
  274.   scar, srl: TSVN;
  275.  
  276. begin
  277.   Writeln('Begin');
  278.  
  279.   scar := ReturnSCARSVN;
  280.   scar.baseloc.local.path := SCARDirectory;
  281.   srl := ReturnSRLSVN;
  282.   srl.baseloc.local.path := SRLDirectory;
  283.  
  284.   if DownloadSCAR then
  285.   begin
  286.     if (not CheckVersion) or (CheckVersion and IsSVNOld(scar)) then
  287.     begin
  288.       CollectSVN(scar);
  289.       DownloadSVN(scar);
  290.     end;
  291.   end;
  292.  
  293.   if DownloadSRL then
  294.   begin
  295.     if (not CheckVersion) or (CheckVersion and IsSVNOld(srl)) then
  296.     begin
  297.       CollectSVN(srl);
  298.       DownloadSVN(srl);
  299.      
  300.       if MoveSRLPlugins then
  301.         MovePlugins(srl, scar.baseloc.local.path + 'Plugins\');
  302.     end;
  303.   end else if MoveSRLPlugins then
  304.   begin
  305.     CollectSVN(srl);
  306.     MovePlugins(srl, scar.baseloc.local.path + 'Plugins\');
  307.   end;
  308.  
  309.   Writeln('End');
  310. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement