This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unitcommon.pas
182 lines (156 loc) · 4.92 KB
/
unitcommon.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
unit unitcommon;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, IniFiles, AsyncProcess, process{$IfDef Windows}, JwaWindows{$EndIf};
const
{$IfDef Windows}
ariaExecutable = 'aria2c.exe';
{$Else}
ariaExecutable = 'aria2c';
{$EndIf}
GB = 1073741824; // 1024 * 1024 * 1024
MB = 1048576; // 1024 * 1024
KB = 1024;
type
TAriaProcessManager = class
private
public
ProcessInstance: TAsyncProcess;
procedure Execute();
procedure LoadConfig();
function isRunning(): boolean;
constructor Create;
destructor Destroy; override;
end;
function FileSizeToHumanReadableString(FileSize: int64): string;
function processExists(exeFileName: string): Boolean;
var
GlobalConfig: TIniFile;
AriaProcessManager: TAriaProcessManager;
AriaParamToken: string;
implementation
constructor TAriaProcessManager.Create;
begin
Self.ProcessInstance := TAsyncProcess.Create(nil);
ProcessInstance.Executable := GetCurrentDir + PathDelim + ariaExecutable;
ProcessInstance.Options := [poNoConsole];
LoadConfig();
end;
destructor TAriaProcessManager.Destroy;
begin
ProcessInstance.Terminate(0);
ProcessInstance.Free;
end;
procedure TAriaProcessManager.Execute();
begin
ProcessInstance.Execute;
end;
procedure TAriaProcessManager.LoadConfig();
var
tmp: string;
begin
ProcessInstance.Parameters.Clear;
ProcessInstance.Parameters.Add('--enable-rpc');
if GlobalConfig.ReadBool('RPC', 'ListenAll', False) then
ProcessInstance.Parameters.Add('--rpc-listen-all=true');
ProcessInstance.Parameters.Add('--rpc-listen-port=' + IntToStr(
GlobalConfig.ReadInt64('RPC', 'Port', 6800)));
tmp := GlobalConfig.ReadString('RPC', 'Secret', '');
if tmp <> '' then
ProcessInstance.Parameters.Add('--rpc-secret=' + tmp);
tmp := GlobalConfig.ReadString('Download', 'Path', '');
if tmp <> '' then
ProcessInstance.Parameters.Add('--dir=' + tmp);
tmp := GlobalConfig.ReadString('Download', 'User-Agent', '');
if tmp <> '' then
ProcessInstance.Parameters.Add('--user-agent=' + tmp);
ProcessInstance.Parameters.Add('--input-file=' + GetCurrentDir + '/download.session');
ProcessInstance.Parameters.Add('--save-session=' + GetCurrentDir + '/download.session');
ProcessInstance.Parameters.Add('--rpc-allow-origin-all=true');
ProcessInstance.Parameters.Add('--max-concurrent-downloads='+GlobalConfig.ReadString('Download', 'MaxTasks', '64'));
ProcessInstance.Parameters.Add('--max-connection-per-server=64'+GlobalConfig.ReadString('Download', 'MaxConnections', '64'));
ProcessInstance.Parameters.Add('--min-split-size=1M');
ProcessInstance.Parameters.Add('--split=64');
ProcessInstance.Parameters.Add('--check-certificate=false');
tmp := GlobalConfig.ReadString('BT', 'Tracker', '');
if tmp <> '' then
begin
ProcessInstance.Parameters.Add('--bt-tracker=' + tmp);
end;
tmp:=GlobalConfig.ReadString('Download', 'Proxy', '');
if tmp <> '' then begin
ProcessInstance.Parameters.Add('--all-proxy=' + tmp);
end;
AriaParamToken := 'token:' + GlobalConfig.ReadString('RPC', 'Secret', '');
end;
function TAriaProcessManager.isRunning(): boolean;
begin
Result := ProcessInstance.Running;
end;
function FileSizeToHumanReadableString(FileSize: int64): string;
var
Size: double;
SizeUnit: string;
begin
if FileSize >= GB then // 如果文件大小大于等于1GB
begin
Size := FileSize / GB; // 用GB为单位
SizeUnit := 'GB';
end
else if FileSize >= MB then // 如果文件大小大于等于1MB
begin
Size := FileSize / MB; // 用MB为单位
SizeUnit := 'MB';
end
else if FileSize >= KB then begin
Size := FileSize / KB; // 用KB为单位
SizeUnit := 'KB';
end
else // 如果文件大小小于1MB
begin
Size := FileSize; // 用字节为单位
SizeUnit := 'bytes';
end;
Result:= Format('%.2f %s', [Size, SizeUnit]);// 格式化输出,保留整数部分
end;
{
function FileSizeToHumanReadableString(FileSize: int64): string;
begin
Result:=IntToStr(FileSize);
end;
}
{$IfDef WINDOWS}
function processExists(exeFileName: string): Boolean;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
Result := False;
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
begin
Result := True;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
{$Else}
function processExists(exeFileName: string): Boolean;
var
output: string;
begin
if (not RunCommand('ps -C ' + exeFileName, output)) then exit(False);
if Pos(output, exeFileName) = 0 then exit(false);
exit(true);
end;
{$EndIf}
end.