gamedownloader/downloader.pas

159 lines
3.4 KiB
ObjectPascal
Raw Normal View History

2021-02-03 17:47:45 -08:00
program downloader;
uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar;
2021-02-03 17:47:45 -08:00
var
authString: String;
2021-02-03 17:47:45 -08:00
gameId: Integer;
gameData: TJSONData;
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;
2021-02-03 17:47:45 -08:00
function getGameData(var client: TFPHTTPClient; gameId: Integer; out gameData: TJSONData): Boolean;
const
url = 'https://rpi.narnian.us/games/get_game/';
var
jData: TJSONData;
begin
writeln('start get game data');
client.AllowRedirect := true;
try
jData := GetJSON(client.Get(url+IntToStr(gameId)));
gameData := jData;
getGameData := true;
Except
getGameData := false;
end;
end;
function downloadGameTar(var client: TFPHTTPClient; gameId: Integer): String;
const
url = 'https://rpi.narnian.us/games/download/windows/';
var
tarFile: TFileStream;
tarFileName: String;
begin
writeln('start download tar');
client.AllowRedirect := true;
tarFileName := gameData.FindPath('title_sanitized').AsString+'.tar';
tarFile := TFileStream.Create(tarFileName, fmCreate);
client.Get(url+IntToStr(gameId), tarFile);
tarFile.Free;
downloadGameTar := tarFileName;
end;
procedure extractTar(fileName: String);
var
tarFile: TTarArchive;
tarContent: TTarDirRec;
files: TJSONArray;
firstFile: String;
i: Integer;
2021-02-03 17:47:45 -08:00
begin
writeln('start extract tar');
tarFile := TTarArchive.Create(fileName);
while tarFile.FindNext(tarContent) do
begin
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
2021-02-03 17:47:45 -08:00
begin
createFileDir(files.Strings[i]);
copyFile(firstFile, files.Strings[i]);
2021-02-03 17:47:45 -08:00
end;
end;
tarFile.Destroy;
2021-02-03 17:47:45 -08:00
end;
function authenticate(client: TFPHTTPClient): Boolean;
const
url = 'https://rpi.narnian.us';
var
username, password: String;
begin
Write('Enter username: ');
ReadLn(username);
Write('Enter password: ');
ReadLn(password);
authString := username + ':' + password;
client.AddHeader('Authorization', 'Basic '+base64.EncodeStringBase64(authString));
client.AllowRedirect := true;
try
client.Get(url);
authenticate := true;
except
authenticate := false;
end;
end;
2021-02-03 17:47:45 -08:00
begin
if ParamCount > 0 then
begin
gameId := StrToInt(ParamStr(1));
client := TFPHTTPClient.Create(nil);
if authenticate(client) then
2021-02-03 17:47:45 -08:00
begin
if getGameData(client, gameId, gameData) then
begin
tarFile := downloadGameTar(client, gameId);
extractTar(tarFile);
DeleteFile(tarFile);
WriteLn('Download finished');
end
else
begin
WriteLn('Invalid game id given');
end;
2021-02-03 17:47:45 -08:00
end
else
begin
WriteLn('Invalid username or password');
2021-02-03 17:47:45 -08:00
end;
end
else
begin
WriteLn('No game id was given');
end;
end.