From 7b8594adef08b8c6fa8a65cb2645847eb6c37183 Mon Sep 17 00:00:00 2001 From: Matthew Welch Date: Thu, 4 Feb 2021 17:01:29 -0800 Subject: [PATCH] Extract tar directly to game files (closes #3) No longer uses intermediate files --- downloader.pas | 101 +++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 57 deletions(-) diff --git a/downloader.pas b/downloader.pas index d5c373f..d34bfb8 100644 --- a/downloader.pas +++ b/downloader.pas @@ -11,6 +11,37 @@ var client: TFPHTTPClient; tarFile: String; +procedure createFileDir(filePath: AnsiString); +var + splitDir: TStringArray; + s: String; + newDir: String = ''; + +begin + splitDir := filePath.Split(['\', '/']); + for s in splitDir do + begin + if not (s = splitDir[Length(splitDir)-1]) then + begin + newDir := newDir + s + '/'; + CreateDir(newDir); + end; + end; +end; + +procedure copyFile(oldFile, newFile: AnsiString); +var + source, dest: TFileStream; + +begin + createFileDir(newFile); + source := TFileStream.Create(oldFile, fmOpenRead); + dest := TFileStream.Create(newFile, fmCreate); + dest.CopyFrom(source, source.Size); + source.Free; + dest.Free; +end; + function getGameData(var client: TFPHTTPClient; gameId: Integer; out gameData: TJSONData): Boolean; const url = 'https://rpi.narnian.us/games/get_game/'; @@ -52,71 +83,28 @@ procedure extractTar(fileName: String); var tarFile: TTarArchive; tarContent: TTarDirRec; + files: TJSONArray; + firstFile: String; + i: Integer; begin writeln('start extract tar'); tarFile := TTarArchive.Create(fileName); while tarFile.FindNext(tarContent) do begin - tarFile.ReadFile(tarContent.Name); + files := TJSONArray(gameData.FindPath('windows.files.'+tarContent.Name)); + firstFile := files.Strings[0]; + createFileDir(firstFile); + tarFile.ReadFile(firstFile); + for i := 1 to files.Count-1 do + begin + createFileDir(files.Strings[i]); + copyFile(firstFile, files.Strings[i]); + end; end; tarFile.Destroy; end; -procedure copyFile(oldFile, newFile: AnsiString); -var - splitDir: TStringArray; - s: String; - newDir: String = ''; - source, dest: TFileStream; - -begin - splitDir := newFile.Split(['\', '/']); - for s in splitDir do - begin - if not (s = splitDir[Length(splitDir)-1]) then - begin - newDir := newDir + s + '/'; - CreateDir(newDir); - end; - end; - source := TFileStream.Create(oldFile, fmOpenRead); - dest := TFileStream.Create(newFile, fmCreate); - dest.CopyFrom(source, source.Size); - source.Free; - dest.Free; -end; - -procedure moveFile(oldFile, newFile: AnsiString); -begin - copyFile(oldFile, newFile); - DeleteFile(oldFile); -end; - -procedure proccessHashFiles; -var - files: TJSONData; - hash: String; - hashPaths: TJSONArray; - i, j: Integer; - -begin - writeln('start proccess hash files'); - files := gameData.FindPath('windows.files'); - for i := 0 to files.count-1 do - begin - hash := TJSONObject(files).Names[i]; - hashPaths := TJSONArray(files.FindPath(hash)); - for j := 0 to hashPaths.Count-1 do - begin - if j = hashPaths.Count-1 then - moveFile(hash, hashPaths.Strings[j]) - else - copyFile(hash, hashPaths.Strings[j]); - end; - end; -end; - begin if ParamCount > 0 then begin @@ -127,8 +115,8 @@ begin begin tarFile := downloadGameTar(client, gameId); extractTar(tarFile); - proccessHashFiles; DeleteFile(tarFile); + WriteLn('Download finished'); end else begin @@ -139,6 +127,5 @@ begin begin WriteLn('No game id was given'); end; - readln; end.