Extract tar directly to game files (closes #3)
No longer uses intermediate files
This commit is contained in:
parent
da5edbeef4
commit
7b8594adef
101
downloader.pas
101
downloader.pas
@ -11,6 +11,37 @@ var
|
|||||||
client: TFPHTTPClient;
|
client: TFPHTTPClient;
|
||||||
tarFile: String;
|
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;
|
function getGameData(var client: TFPHTTPClient; gameId: Integer; out gameData: TJSONData): Boolean;
|
||||||
const
|
const
|
||||||
url = 'https://rpi.narnian.us/games/get_game/';
|
url = 'https://rpi.narnian.us/games/get_game/';
|
||||||
@ -52,71 +83,28 @@ procedure extractTar(fileName: String);
|
|||||||
var
|
var
|
||||||
tarFile: TTarArchive;
|
tarFile: TTarArchive;
|
||||||
tarContent: TTarDirRec;
|
tarContent: TTarDirRec;
|
||||||
|
files: TJSONArray;
|
||||||
|
firstFile: String;
|
||||||
|
i: Integer;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writeln('start extract tar');
|
writeln('start extract tar');
|
||||||
tarFile := TTarArchive.Create(fileName);
|
tarFile := TTarArchive.Create(fileName);
|
||||||
while tarFile.FindNext(tarContent) do
|
while tarFile.FindNext(tarContent) do
|
||||||
begin
|
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;
|
end;
|
||||||
tarFile.Destroy;
|
tarFile.Destroy;
|
||||||
end;
|
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
|
begin
|
||||||
if ParamCount > 0 then
|
if ParamCount > 0 then
|
||||||
begin
|
begin
|
||||||
@ -127,8 +115,8 @@ begin
|
|||||||
begin
|
begin
|
||||||
tarFile := downloadGameTar(client, gameId);
|
tarFile := downloadGameTar(client, gameId);
|
||||||
extractTar(tarFile);
|
extractTar(tarFile);
|
||||||
proccessHashFiles;
|
|
||||||
DeleteFile(tarFile);
|
DeleteFile(tarFile);
|
||||||
|
WriteLn('Download finished');
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
@ -139,6 +127,5 @@ begin
|
|||||||
begin
|
begin
|
||||||
WriteLn('No game id was given');
|
WriteLn('No game id was given');
|
||||||
end;
|
end;
|
||||||
readln;
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user