Advertisement
mixster

mixster

Oct 19th, 2009
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.14 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 = '';
  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.baseloc.local.path := SRLDirectory;
  174.   if SCARDirectory = '' then
  175.     Result.baseloc.local.path := AppPath + Result.baseloc.local.path;
  176.   Result.filter.exp := '<';
  177.   Result.filter.dir := 'dir';
  178.   Result.filter.fil := 'file';
  179.   Result.filter.local.start := 'name="';
  180.   Result.filter.local.finish := '"';
  181.   Result.filter.remote.start := 'href="';
  182.   Result.filter.remote.finish := '"';
  183.   Result.rev.start := 'rev="';
  184.   Result.rev.finish := '"';
  185.   Result.skip := [];
  186. end;
  187.  
  188. function ReturnSCARSVN: TSVN;
  189. begin
  190.   Result.name := 'SCAR';
  191.   Result.baseloc.remote.path := 'http://freddy1990.com/svn/scarprerelease/';
  192.   if SCARDirectory <> '' then
  193.     Result.baseloc.local.path := SCARDirectory
  194.   else
  195.     Result.baseloc.local.path := AppPath;
  196.   Result.filter.exp := '<a';
  197.   Result.filter.dir := '/"';
  198.   Result.filter.fil := '.';
  199.   Result.filter.local.start := '">';
  200.   Result.filter.local.finish := '<';
  201.   Result.filter.remote.start := 'href="';
  202.   Result.filter.remote.finish := '"';
  203.   Result.rev.start := 'Revision ';
  204.   Result.rev.finish := ':';
  205.   Result.skip := ['http', '..'];
  206. end;
  207.  
  208. function GetSVNVersion(svn: TSVN): Integer;
  209. var
  210.   input: string;
  211. begin
  212.   input := GetPage(svn.baseloc.remote.path);
  213.   Result := StrToIntDef(Between(svn.rev.start, svn.rev.finish, input), 0);
  214. end;
  215.  
  216. procedure WriteSVNVersion(svn: TSVN);
  217. var
  218.   rev: Integer;
  219. begin
  220.   rev := GetSVNVersion(svn);
  221.   if rev = 0 then
  222.   begin
  223.     Writeln('Collecting rev number failed');
  224.     Writeln('You can manually set svn_settings.ini if this results in a problem');
  225.     exit;
  226.   end;
  227.  
  228.   WriteINI(svn.name, 'REVISION', IntToStr(rev), SCARDirectory + 'svn_settings.ini');
  229. end;
  230.  
  231. function IsSVNOld(svn: TSVN): Boolean;
  232. var
  233.   curRev, locRev: Integer;
  234. begin
  235.   locRev := StrToIntDef(ReadINI(svn.name, 'REVISION', AppPath + 'svn_settings.ini'), 0);
  236.   curRev := GetSVNVersion(svn);
  237.   if curRev = 0 then
  238.   begin
  239.     Writeln('Collecting current rev number failed');
  240.     Writeln('Assuming newer SVN is on local system to avoid problems');
  241.     Result := False;
  242.     exit;
  243.   end;
  244.  
  245.   Result := locRev < curRev;
  246.  
  247.   if Result then
  248.     Writeln(svn.name + ' has the latest rev downloaded')
  249.   else
  250.     Writeln(svn.name + ' is rev ' + IntToStr(locRev) + ' and latest is ' + IntToStr(curRev));
  251. end;
  252.  
  253. procedure MovePlugins(svn: TSVN; dest: string);
  254. var
  255.   i, f: Integer;
  256.   s: string;
  257.   o: Boolean;
  258. begin
  259.   for i := 0 to High(svn.files) do
  260.     if ExtractFileExt(svn.files[i].local.filename) = '.dll' then
  261.       try
  262.         Writeln('Found plugin "' + svn.files[i].local.filename);
  263.         o := False;
  264.         f := OpenFile(svn.baseloc.local.path + svn.files[i].local.path + svn.files[i].local.filename, False);
  265.         ReadFileString(f, s, FileSize(f));
  266.         CloseFile(f);
  267.         o := True;
  268.         f := RewriteFile(dest + svn.files[i].local.filename, False);
  269.         WriteFileString(f, s);
  270.         CloseFile(f);
  271.       except
  272.         Writeln('Failed to save plugin.');
  273.         if o then
  274.           Writeln('Problem involved rewriting the file at target folder')
  275.         else
  276.           Writeln('Problem involved opening the sauce file to read');
  277.       end;
  278. end;
  279.  
  280. var
  281.   scar, srl: TSVN;
  282.  
  283. begin
  284.   Writeln('Begin');
  285.  
  286.   scar := ReturnSCARSVN;
  287.   srl := ReturnSRLSVN;
  288.  
  289.   if DownloadSCAR then
  290.   begin
  291.     if (not CheckVersion) or (CheckVersion and IsSVNOld(scar)) then
  292.     begin
  293.       CollectSVN(scar);
  294.       DownloadSVN(scar);
  295.     end;
  296.   end;
  297.  
  298.   if DownloadSRL then
  299.   begin
  300.     if (not CheckVersion) or (CheckVersion and IsSVNOld(srl)) then
  301.     begin
  302.       CollectSVN(srl);
  303.       DownloadSVN(srl);
  304.  
  305.       if MoveSRLPlugins then
  306.         MovePlugins(srl, scar.baseloc.local.path + 'Plugins\');
  307.     end;
  308.   end else if MoveSRLPlugins then
  309.   begin
  310.     CollectSVN(srl);
  311.     MovePlugins(srl, scar.baseloc.local.path + 'Plugins\');
  312.   end;
  313.  
  314.   Writeln('End');
  315. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement