Advertisement
mixster

mixster

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