跳到主要内容

客户端接入流程

1. 获取文件列表​

GET https://tmplistoss.bgydook.cn/api/tmp/files

2. 对比本地文件​

建议先比较文件大小,再对大小不一致或不确定的文件计算 MD5。

var response = await http.GetFromJsonAsync<ApiResponse<List<TmpFile>>>(
"https://tmplistoss.bgydook.cn/api/tmp/files"
);

foreach (var file in response.Data)
{
var localPath = Path.Combine(
gameDirectory,
file.FilePath.TrimStart('/').Replace('/', Path.DirectorySeparatorChar)
);
}

3. 拼接下载地址​

优先直接使用接口返回的 downloadUrl:

var url = file.DownloadUrl.StartsWith("http")
? file.DownloadUrl
: "https://tmplistoss.bgydook.cn" + file.DownloadUrl;

如果客户端自行拼接,必须追加 MD5:

var url =
$"https://tmplistoss.bgydook.cn/files{file.FilePath}?v={file.Md5}";

:::warning 必须保留 MD5 不要拼接成:

https://tmplistoss.bgydook.cn/files{filePath}

否则可能命中旧缓存。 :::

4. 下载并校验​

using var response = await http.GetAsync(
url,
HttpCompletionOption.ResponseHeadersRead
);
response.EnsureSuccessStatusCode();

var tempPath = localPath + ".part";

await using (var input = await response.Content.ReadAsStreamAsync())
await using (var output = File.Create(tempPath))
{
await input.CopyToAsync(output);
}

var actualMd5 = await GetMd5Async(tempPath);
if (!actualMd5.Equals(file.Md5, StringComparison.OrdinalIgnoreCase))
{
File.Delete(tempPath);
throw new Exception($"MD5 校验失败: {file.FilePath}");
}

File.Move(tempPath, localPath, true);

5. 保存路径​

filePath 是相对路径,不要直接作为 Windows 根路径。

filePath保存位置
/core_ets2mp.dll<客户端目录>/core_ets2mp.dll
/data/ets2/ui/logo.png<客户端目录>/data/ets2/ui/logo.png

错误处理​

状态码说明
200成功
404文件不存在或尚未同步
429请求过于频繁
403当前 IP 被限制

遇到 404 时,应重新请求文件列表,不要一直重试同一个旧地址。