Skip to content

Commit

Permalink
头像修复
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoye97 committed Oct 17, 2022
1 parent 1bb9347 commit 37460f6
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 31 deletions.
6 changes: 3 additions & 3 deletions VTS_XYPlugin/BilibiliHeadCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void Update()
{
HeadLinkDict[userID] = url;
// 如果不存在此文件,则下载
if (!File.Exists($"{XYPaths.BiliHeadDirPath}/{url}"))
if (!File.Exists($"{XYPaths.BiliHeadDirPath}/{url.RemoveHttp()}"))
{
NetHelper.DownloadHead(url);
}
Expand Down Expand Up @@ -188,7 +188,7 @@ private UserHead LoadAndCacheHead(int userID, string path)
/// <param name="message"></param>
public void OnRecvGift(BGiftMessage message)
{
string url = message.头像图片链接.Replace("http://", "");
string url = message.头像图片链接;
if (HeadLinkDict.ContainsKey(message.用户ID))
{
// 如果缓存的数据和传入的数据不一致,说明需要更新
Expand All @@ -204,7 +204,7 @@ public void OnRecvGift(BGiftMessage message)
if (XYPlugin.Instance.GlobalConfig.DownloadHeadOnRecvGift)
{
// 如果不存在此文件,则加入下载队列
if (!File.Exists($"{XYPaths.BiliHeadDirPath}/{url}"))
if (!File.Exists($"{XYPaths.BiliHeadDirPath}/{url.RemoveHttp()}"))
{
waitDownloadHeads.Enqueue(url);
}
Expand Down
68 changes: 53 additions & 15 deletions VTS_XYPlugin/Component/DropItemBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,71 @@ private void Update()
switch (state)
{
case DropItemState.正在掉落:
lifeCD -= Time.deltaTime;
if (lifeCD < 0)
try
{
state = DropItemState.正在销毁;
destroyCD = 1.5f;
Collider.enabled = false;
lifeCD -= Time.deltaTime;
if (lifeCD < 0)
{
state = DropItemState.正在销毁;
destroyCD = 1.5f;
Collider.enabled = false;
}
}
catch
{
Debug.LogError("DropItemBehaviour Update Error 1");
}
break;

case DropItemState.正在销毁:
destroyCD -= Time.deltaTime;
if (destroyCD < 0)
try
{
destroyCD -= Time.deltaTime;
if (destroyCD < 0)
{
// GameObject.Destroy(gameObject);
Lean.Pool.LeanPool.Despawn(gameObject);
}
SR.color = new Color(1, 1, 1, destroyCD / 1.5f);
}
catch
{
// GameObject.Destroy(gameObject);
Lean.Pool.LeanPool.Despawn(gameObject);
Debug.LogError("DropItemBehaviour Update Error 2");
}
SR.color = new Color(1, 1, 1, destroyCD / 1.5f);
break;
}
if (itemData.UseUserHead)
{
if (userHead.ImageType == ImageType.GIF && gif != null)
try
{
if (userHead != null)
{
if (!userHead.IsHeadReadError)
{
if (userHead.ImageType == ImageType.GIF && gif != null)
{
SR.sprite = gif.GetNowSprite();
}
}
}
}
catch
{
SR.sprite = gif.GetNowSprite();
Debug.LogError("DropItemBehaviour Update Error 3");
}
}
else
{
if (itemData.ImageType == ImageType.GIF && gif != null)
try
{
SR.sprite = gif.GetNowSprite();
if (itemData.ImageType == ImageType.GIF && gif != null)
{
SR.sprite = gif.GetNowSprite();
}
}
catch
{
Debug.LogError("DropItemBehaviour Update Error 4");
}
}
}
Expand Down Expand Up @@ -94,7 +128,11 @@ public void StartDrop(WaitDropItemData waitData)
if (head != null)
{
userHead = head;
if (head.ImageType == ImageType.PNG)
if (userHead.IsHeadReadError)
{
SR.sprite = XYDropManager.Instance.UserHeadNormalSprite;
}
else if (head.ImageType == ImageType.PNG)
{
SR.sprite = head.sprite;
}
Expand Down
11 changes: 2 additions & 9 deletions VTS_XYPlugin/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,8 @@ public static void SaveBiliHeadCache()
public static Texture2D LoadTexture2D(string path)
{
Texture2D tex = new Texture2D(2, 2);
try
{
var bytes = File.ReadAllBytes(path);
tex.LoadImage(bytes);
}
catch (Exception ex)
{
XYLog.LogError($"{ex}");
}
var bytes = File.ReadAllBytes(path);
tex.LoadImage(bytes);
return tex;
}

Expand Down
10 changes: 9 additions & 1 deletion VTS_XYPlugin/NetHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
Expand Down Expand Up @@ -44,6 +45,8 @@ public static int GetAvailablePort(string ip)
return port;
}

private static List<string> downloadUrlCache = new List<string>();

/// <summary>
/// 下载头像
/// </summary>
Expand All @@ -52,6 +55,10 @@ public static void DownloadHead(string url, bool overrideFile = false)
{
try
{
if (downloadUrlCache.Contains(url))
{
return;
}
string savePath = $"{XYPaths.BiliHeadDirPath}/{url.RemoveHttp()}";
FileInfo file = new FileInfo(savePath);
if (file.Exists)
Expand All @@ -66,7 +73,8 @@ public static void DownloadHead(string url, bool overrideFile = false)
{
file.Directory.Create();
}
XYLog.LogMessage($"开始下载头像 {url}");
XYLog.LogMessage($"开始下载头像 {url} savePath:{savePath}");
downloadUrlCache.Add(url);
using (var web = new WebClient())
{
web.DownloadFile($"{url}", savePath);
Expand Down
19 changes: 17 additions & 2 deletions VTS_XYPlugin/UserHead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class UserHead
public int UserID;
public ImageType ImageType;
public string FilePath;
public bool IsHeadReadError;

public UserHead()
{
Expand Down Expand Up @@ -41,13 +42,27 @@ public void LoadImage()
{
ImageType = ImageType.GIF;
gif = new Gif();
gif.LoadGif(FilePath, true);
try
{
gif.LoadGif(FilePath, true);
}
catch
{
IsHeadReadError = true;
}
//XYLog.LogMessage($"加载gif完毕,共{gif.frameDelays.Count}帧");
}
else
{
ImageType = ImageType.PNG;
sprite = FileHelper.LoadSprite(FilePath, true);
try
{
sprite = FileHelper.LoadSprite(FilePath, true);
}
catch
{
IsHeadReadError = true;
}
//XYLog.LogMessage($"加载sprite完毕, pixelsPerUnit:{sprite.pixelsPerUnit}");
}
}
Expand Down
2 changes: 1 addition & 1 deletion VTS_XYPlugin/XYPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class XYPlugin : BaseUnityPlugin
{
public const string GUID = "me.xiaoye97.plugin.VTubeStudio.VTS_XYPlugin";
public const string PluginName = "VTS_XYPlugin";
public const string VERSION = "2.1.0";
public const string VERSION = "2.0.0";

public static List<string> CmdArgs;

Expand Down

0 comments on commit 37460f6

Please sign in to comment.