Skip to content

Commit e504aaa

Browse files
committed
migrate from node to dotnet
1 parent 2948757 commit e504aaa

File tree

12 files changed

+274
-120
lines changed

12 files changed

+274
-120
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
node_modules
2-
package-lock.json
1+
bin
2+
obj

Args.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
using System.Reflection;
3+
4+
class Args
5+
{
6+
public string id { get; private set; } = default!;
7+
public string download { get; private set; } = default!;
8+
string? _version { get; set; } = default!;
9+
bool _showHelp = false;
10+
bool _showVersion = false;
11+
public static Args Get(string[] args)
12+
{
13+
var newArgs = args.Append("\n").Select(v => v.ToLower()).ToArray();
14+
return new Args()
15+
{
16+
_version = Assembly.GetExecutingAssembly().GetName().Version?.ToString(),
17+
id = FindArgValue(newArgs, new [] { "-i", "--id" }),
18+
download = FindArgValue(newArgs, new [] { "-d", "--download" }),
19+
_showHelp = FindArgForShowInfo(newArgs, new [] { "-h", "--help" }),
20+
_showVersion = FindArgForShowInfo(newArgs, new [] { "-v", "--version" }),
21+
};
22+
}
23+
24+
public void CheckIfVariableDefine()
25+
{
26+
if (id == "")
27+
{
28+
Console.WriteLine("Game Id is not defined");
29+
Environment.Exit(0);
30+
}
31+
}
32+
33+
public void ShowInfoIfEnable()
34+
{
35+
if (_showVersion)
36+
{
37+
Console.WriteLine($"{_version}");
38+
Environment.Exit(0);
39+
}
40+
else if (_showHelp)
41+
{
42+
Console.WriteLine(@"
43+
PlayStation 3 Games Update Downloader
44+
45+
optional arguments:
46+
-h, --help show this help message and exit
47+
-v show program's version number and exit
48+
-i ID, --id ID
49+
games id
50+
-d DOWNLOAD, --download DOWNLOAD
51+
download folder
52+
");
53+
Environment.Exit(0);
54+
}
55+
56+
}
57+
static bool FindArgForShowInfo(string[] args, string[] cmd)
58+
{
59+
foreach (var v in args)
60+
{
61+
if (cmd.Contains(v))
62+
return true;
63+
}
64+
return false;
65+
66+
}
67+
static string FindArgValue(string[] args, string[] cmd)
68+
{
69+
var newArgs = args.Select((item, index) => (item, index));
70+
foreach (var (v, i) in newArgs)
71+
{
72+
if (v == "\n")
73+
return "";
74+
var next = args[i + 1];
75+
var value = !cmd.Contains(next)
76+
? next == "\n" ? "" : next
77+
: "";
78+
if (cmd.Contains(v))
79+
return value;
80+
}
81+
return "";
82+
}
83+
}

Fetch.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Fetch
2+
{
3+
public static async Task To(string downloadUrl, string destinationFilePath)
4+
{
5+
using (var client = new HttpClientDownloadWithProgress(downloadUrl, destinationFilePath))
6+
{
7+
var version = destinationFilePath.Split('/')[^3];
8+
client.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage, rate) =>
9+
{
10+
Console.Write(
11+
$"patch:{version} sie:{totalFileSize}MB/{totalBytesDownloaded}MB speed:{rate}MB/s {progressPercentage}%\r"
12+
);
13+
};
14+
15+
await client.StartDownload();
16+
}
17+
}
18+
}

GameData.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
record GameData
2+
{
3+
public string id {set; get;} = default!;
4+
public string name {set; get;} = default!;
5+
public string version {set; get;} = default!;
6+
public string url {set; get;} = default!;
7+
};

GamesDatabase.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Text.Json;
2+
class GamesDatabase
3+
{
4+
GameData[]? _db = default!;
5+
public static GamesDatabase Load(string fileName)
6+
{
7+
var file = File.ReadAllText(fileName);
8+
var json = JsonSerializer.Deserialize<GameData[]>(file);
9+
return new GamesDatabase()
10+
{
11+
_db = json
12+
};
13+
}
14+
15+
public GameData[] FindById(string id)
16+
{
17+
var items = _db?.Where(v => v.id == id.ToUpper()).ToArray() ?? default!;
18+
if (items.Length == 0)
19+
{
20+
Console.ForegroundColor = ConsoleColor.Red;
21+
Console.WriteLine($"{id} not found!");
22+
Environment.Exit(-1);
23+
}
24+
return items;
25+
}
26+
27+
}

HttpClientDownloadWithProgress.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
public class HttpClientDownloadWithProgress : IDisposable
3+
{
4+
readonly string _downloadUrl;
5+
readonly string _destinationFilePath;
6+
HttpClient? httpClient;
7+
DateTime _startedAt = DateTime.Now;
8+
9+
public delegate void ProgressChangedHandler(long? totalFileSize, long totalBytesDownloaded, double? progressPercentage, double rate);
10+
public event ProgressChangedHandler? ProgressChanged;
11+
12+
public HttpClientDownloadWithProgress(string downloadUrl, string destinationFilePath)
13+
{
14+
_downloadUrl = downloadUrl;
15+
_destinationFilePath = destinationFilePath;
16+
}
17+
18+
public async Task StartDownload()
19+
{
20+
httpClient = new HttpClient { Timeout = TimeSpan.FromDays(1) };
21+
22+
using (var response = await httpClient.GetAsync(_downloadUrl, HttpCompletionOption.ResponseHeadersRead))
23+
await DownloadFileFromHttpResponseMessage(response);
24+
}
25+
26+
async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response)
27+
{
28+
response.EnsureSuccessStatusCode();
29+
30+
var totalBytes = response.Content.Headers.ContentLength;
31+
32+
using (var contentStream = await response.Content.ReadAsStreamAsync())
33+
await ProcessContentStream(totalBytes, contentStream);
34+
}
35+
36+
async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream)
37+
{
38+
var totalBytesRead = 0L;
39+
var readCount = 0L;
40+
var buffer = new byte[8192];
41+
var isMoreToRead = true;
42+
43+
using (var fileStream = new FileStream(_destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
44+
{
45+
do
46+
{
47+
var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length);
48+
if (bytesRead == 0)
49+
{
50+
isMoreToRead = false;
51+
TriggerProgressChanged(totalDownloadSize, totalBytesRead);
52+
continue;
53+
}
54+
55+
await fileStream.WriteAsync(buffer, 0, bytesRead);
56+
57+
totalBytesRead += bytesRead;
58+
readCount += 1;
59+
60+
if (readCount % 100 == 0)
61+
TriggerProgressChanged(totalDownloadSize, totalBytesRead);
62+
}
63+
while (isMoreToRead);
64+
}
65+
}
66+
67+
void TriggerProgressChanged(long? totalDownloadSize, long totalBytesRead)
68+
{
69+
var elapsed = (DateTime.Now - _startedAt) / 1000;
70+
var rate = Math.Round((totalBytesRead / elapsed.TotalMilliseconds)/1024/1024,1);
71+
if (ProgressChanged == null)
72+
return;
73+
74+
double? progressPercentage = null;
75+
if (totalDownloadSize.HasValue)
76+
progressPercentage = Math.Round((double)totalBytesRead / totalDownloadSize.Value * 100, 0);
77+
78+
ProgressChanged(totalDownloadSize/1024/1024, totalBytesRead/1024/1024, progressPercentage, rate);
79+
}
80+
81+
public void Dispose()
82+
{
83+
httpClient?.Dispose();
84+
}
85+
}

Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
var db = GamesDatabase.Load("db.json");
3+
var arguments = Args.Get(args);
4+
arguments.ShowInfoIfEnable();
5+
arguments.CheckIfVariableDefine();
6+
if (arguments.download == "")
7+
Directory.CreateDirectory("download");
8+
var id = arguments.id;
9+
var downloadFolder = arguments.download == "" ? "download" : arguments.download;
10+
var items = db.FindById(id);
11+
var item = items.First();
12+
Console.WriteLine($"{item.id} {item.name}");
13+
foreach (var i in items)
14+
{
15+
var foldersName = $"{downloadFolder}/{i.id} {i.name}/{i.version}/";
16+
var fileName = i.url.Split("/").Last();
17+
Directory.CreateDirectory(foldersName);
18+
await Fetch.To(i.url, $"{foldersName}/{fileName}");
19+
Console.WriteLine();
20+
}

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
# PlayStation 3 game update download
22

33
Simple tool for downloading patch for games.
4-
App require NodeJS
5-
6-
### install
7-
```
8-
git clone https://github.com/3mam/ps3gut.git
9-
npm install
10-
```
11-
12-
### example
13-
```
14-
./ps3gud -d download -i BLES00246 BLES01807
15-
```
16-
This allowed to download all patch for METAL GEAR SOLID 4 and GTA V
4+
5+
## build
6+
For building app is require .net 6
7+
> git clone https://github.com/3mam/ps3gut.git
8+
9+
#### build for Linux
10+
> dotnet publish -r linux-x64 -c release --self-contained
11+
12+
#### build for Windows 10
13+
> dotnet publish -r win10-x64 -c release --self-contained
14+
15+
copy **db.json** to that same place where is app
16+
17+
18+
## example
19+
> ./ps3gud -i BLES01807
20+
21+
This allowed to download all patch for GTA V

main.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

package.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)