Commit 2bdbbbf3 authored by hex's avatar hex

remove HttpDldFile.cs

parent 19910d74
Pipeline #39193 failed
......@@ -177,26 +177,4 @@ public class MyCardHelper {
onComplete(null, e.Message);
}
}
public static string DownloadFace(string name)
{
try
{
string face = "textures/face/" + name + ".png";
HttpDldFile df = new HttpDldFile();
df.Download("http://api.moestart.com/avatar/avatar/" + name + "/100/koishipro2.png", face);
if (File.Exists(face))
{
Texture2D Face = UIHelper.getTexture2D(face);
UIHelper.faces.Remove(name);
UIHelper.faces.Add(name, Face);
return null;
}
return "Not downloaded";
}
catch (Exception e)
{
return e.Message;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using UnityEngine;
public class HttpDldFile
{
public bool Download(string url, string filename)
{
bool flag = false;
try
{
if (!Directory.Exists(Path.GetDirectoryName(filename)))
{
Directory.CreateDirectory(Path.GetDirectoryName(filename));
}
if (File.Exists(filename + ".tmp"))
{
File.Delete(filename + ".tmp");
}
using (var client = new TimeoutWebClient())
{
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
if (Path.GetExtension(filename).Contains("png"))
{
client.Timeout = 6500;
}
if (Path.GetExtension(filename).Contains("jpg"))
{
client.Timeout = 6500;
}
if (Path.GetExtension(filename).Contains("cdb"))
{
client.Timeout = 30000;
}
if (Path.GetExtension(filename).Contains("conf"))
{
client.Timeout = 3000;
}
client.DownloadFile(new Uri(url), filename + ".tmp");
}
flag = true;
if (File.Exists(filename))
{
File.Delete(filename);
}
File.Move(filename + ".tmp", filename);
}
catch (Exception)
{
if (File.Exists(filename + ".tmp"))
{
File.Delete(filename + ".tmp");
}
flag = false;
}
return flag;
}
public static bool MyRemoteCertificateValidationCallback(System.Object sender,
X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
bool isOk = true;
// If there are errors in the certificate chain,
// look at each error to determine the cause.
if (sslPolicyErrors != SslPolicyErrors.None)
{
for (int i = 0; i < chain.ChainStatus.Length; i++)
{
if (chain.ChainStatus[i].Status == X509ChainStatusFlags.RevocationStatusUnknown)
{
continue;
}
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
bool chainIsValid = chain.Build((X509Certificate2)certificate);
if (!chainIsValid)
{
isOk = false;
break;
}
}
}
return isOk;
}
}
public class TimeoutWebClient : WebClient
{
public int Timeout { get; set; }
public TimeoutWebClient()
{
Timeout = 10000;
}
public TimeoutWebClient(int timeout)
{
Timeout = timeout;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
request.Timeout = Timeout;
return request;
}
}
fileFormatVersion: 2
guid: df98823729c9cbf4ba006cb67248ce13
timeCreated: 1534496729
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -166,66 +166,11 @@ public class Room : WindowServantSP
RoomPlayer player = new RoomPlayer();
player.name = name;
player.prep = false;
// if (Program.I().mycard.isMatching && name != "********") // athletic match name mask
// {
// (
// new Thread(() =>
// {
// string errorMessage = MyCardHelper.DownloadFace(name);
// if (errorMessage != null)
// Program.PrintToChat(InterString.Get("头像加载失败: ") + errorMessage);
// else if (isShowed)
// realize();
// else if (Program.I().ocgcore.isShowed && Program.I().ocgcore.gameInfo)
// Program.I().ocgcore.gameInfo.realize();
// })
// ).Start();
// }
// roomPlayers[pos] = player;
// realize();
// First, add the player and do an initial UI refresh.
// This way, the player's name appears immediately in the UI.
roomPlayers[pos] = player;
realize();
// 注释掉,不下载头像
// Now, if we need to download the face, start the non-blocking coroutine.
// if (Program.I().mycard.isMatching && name != "********")
// {
// // We start a Coroutine from a MonoBehaviour instance, like Program.I()
// Program.I().StartCoroutine(DownloadFaceAndUpdateUI(name));
// }
UIHelper.Flash();
}
private IEnumerator DownloadFaceAndUpdateUI(string playerName)
{
// --- Step 1: Prepare for the background task ---
string downloadErrorMessage = null;
Thread downloadThread = new Thread(() =>
{
// This is the ONLY thing the background thread does: the blocking download.
downloadErrorMessage = MyCardHelper.DownloadFace(playerName);
});
// --- Step 2: Start the background thread and wait for it to finish ---
downloadThread.Start();
// 'yield return' here pauses the coroutine without freezing the game.
// The code will resume only after the downloadThread has completed.
yield return new WaitUntil(() => !downloadThread.IsAlive);
// --- Step 3: We are now back on the MAIN THREAD ---
// It is now safe to use the result and call Unity APIs.
if (downloadErrorMessage != null)
{
Program.PrintToChat(InterString.Get("头像加载失败: ") + downloadErrorMessage);
}
else
{
// The face has downloaded successfully. Now refresh the UI to show it.
if (isShowed)
realize();
else if (Program.I().ocgcore.isShowed && Program.I().ocgcore.gameInfo != null)
Program.I().ocgcore.gameInfo.realize();
}
}
public void StocMessage_Chat(BinaryReader r)
{
int player = r.ReadInt16();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment