420 lines
17 KiB
C#
420 lines
17 KiB
C#
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||
////////////////////////////////////定时检查资源版本,更新资源//////////////////////////////////////////
|
||
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
|
||
public class RealtimeCheck : MonoBehaviour
|
||
{
|
||
List<Assets> assetsDatas;//读取本地的资源信息
|
||
List<Assets> serverDatas = new List<Assets>();//服务器最新的资源信息
|
||
|
||
|
||
private void Start()
|
||
{
|
||
assetsDatas = ReadAssetsInfo();
|
||
|
||
InvokeRepeating("CheckAesset", 1, 30);
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 读取本地资源信息
|
||
/// </summary>
|
||
public List<Assets> ReadAssetsInfo()
|
||
{
|
||
List<Assets> assetsDatas = new List<Assets>();
|
||
List<string> assetsInfos = Directory.GetFiles(Config.AssetsInfoPath, "*.json", SearchOption.AllDirectories).ToList();
|
||
if (assetsInfos.Count > 0)
|
||
{
|
||
foreach (var item in assetsInfos)
|
||
{
|
||
byte[] bytes = GameTools.ReadFile(item);
|
||
string fileContents = Encoding.UTF8.GetString(bytes);
|
||
Assets assetsData = JsonUtility.FromJson<Assets>(fileContents);
|
||
assetsDatas.Add(assetsData);
|
||
}
|
||
}
|
||
return assetsDatas;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查资源更新
|
||
/// </summary>
|
||
public void CheckAesset()
|
||
{
|
||
Debug.Log("检查资源更新");
|
||
StartCoroutine(GetAssets());
|
||
}
|
||
IEnumerator GetAssets()
|
||
{
|
||
using (UnityWebRequest request = UnityWebRequest.Get("http://www.baidu.com"))
|
||
{
|
||
yield return request.SendWebRequest();
|
||
if (request.result != UnityWebRequest.Result.Success)
|
||
{
|
||
Debug.Log("网络不可用");
|
||
yield break;
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("网络可用");
|
||
}
|
||
}
|
||
|
||
WWWForm form = new WWWForm();
|
||
WebNet.Encrypt(Config.SN);
|
||
//Debug.Log(WebNet.s);//加密前字符串
|
||
//Debug.Log(WebNet.Authorization);//加密后token
|
||
form.AddField("deviceSN", Config.SN);
|
||
form.AddField("password", WebNet.password);
|
||
using (UnityWebRequest req = UnityWebRequest.Post(WebNet.assetsUrl, form))
|
||
{
|
||
yield return req.SendWebRequest();
|
||
if (req.result != UnityWebRequest.Result.Success)
|
||
{
|
||
Debug.Log($"获取资源失败:{req.error}");
|
||
yield break;
|
||
}
|
||
else
|
||
{
|
||
JObject jsonData = JsonConvert.DeserializeObject<JObject>(req.downloadHandler.text);
|
||
//Debug.Log(jsonData["code"]);
|
||
//Debug.Log(jsonData["msg"]);
|
||
if (jsonData["code"].ToString() != "200")
|
||
{
|
||
Debug.Log($"获取资源失败:{jsonData["msg"]}");
|
||
yield break;
|
||
}
|
||
JToken data = jsonData["data"];
|
||
JToken host = data["host"];//资源主地址
|
||
WebNet.DownAssetsUrl = host.ToString();
|
||
JToken video = data["video"];
|
||
//foreach (var item in data["video"])
|
||
//{
|
||
// Debug.LogError(item.ToString());
|
||
//}
|
||
|
||
//更新本地资源信息
|
||
for (int i = 0; i < video.Count(); i++)
|
||
{
|
||
//Debug.LogError("资源id:" + video[i]["resource_id"].ToString());
|
||
//Debug.LogError("资源名字:" + video[i]["resource_name"].ToString());
|
||
//Debug.LogError("资源地址:" + video[i]["url"].ToString());
|
||
//Debug.LogError("资源简介:" + video[i]["brief"].ToString());
|
||
//Debug.LogError("资源缩略图:" + video[i]["icon"].ToString());
|
||
//Debug.LogError("资源大小:" + video[i]["file_size"].ToString());
|
||
//Debug.LogError("资源分组id:" + video[i]["group_id"].ToString());
|
||
//Debug.LogError("资源组名称:" + video[i]["group_name"].ToString());
|
||
//Debug.LogError("设备id:" + video[i]["device_id"].ToString());
|
||
//Debug.LogError("播放次数:" + video[i]["play_time"].ToString());
|
||
//Debug.LogError("分组所属省份:" + video[i]["province"].ToString());
|
||
//Debug.LogError("分组所属城市:" + video[i]["city"].ToString());
|
||
//Debug.LogError("更新日期:" + video[i]["updated_at"].ToString());
|
||
//Debug.LogError("资源类型:" + video[i]["type"].ToString());
|
||
Assets assets = new Assets();
|
||
assets.resource_id = int.Parse(video[i]["resource_id"].ToString());
|
||
assets.resource_name = video[i]["resource_name"].ToString();
|
||
assets.url = video[i]["url"].ToString();
|
||
assets.brief = video[i]["brief"].ToString();
|
||
assets.icon = video[i]["icon"].ToString();
|
||
assets.file_size = int.Parse(video[i]["file_size"].ToString());
|
||
assets.group_id = int.Parse(video[i]["group_id"].ToString());
|
||
assets.group_name = video[i]["group_name"].ToString();
|
||
assets.device_id = int.Parse(video[i]["device_id"].ToString());
|
||
assets.play_time = int.Parse(video[i]["play_time"].ToString());
|
||
assets.province = video[i]["province"].ToString();
|
||
assets.city = video[i]["city"].ToString();
|
||
assets.updated_at = video[i]["updated_at"].ToString();
|
||
assets.type = int.Parse(video[i]["type"].ToString());
|
||
|
||
bool isContainId = assetsDatas.Any(v => v.resource_id == assets.resource_id);//是否包含此id
|
||
if (!isContainId)//不包含此id属于新增
|
||
{
|
||
Debug.Log($"新增资源:{assets.resource_name}");
|
||
Download(assets);
|
||
assetsDatas.Add(assets);
|
||
}
|
||
else
|
||
{
|
||
Assets oldAssets = assetsDatas.FirstOrDefault(v => v.resource_id == assets.resource_id);//上版本的资源信息
|
||
if (oldAssets.updated_at != assets.updated_at)//资源有更新
|
||
{
|
||
bool isurl = false;
|
||
Debug.Log($"资源更新:{assets.resource_name}");
|
||
if (oldAssets.resource_name != assets.resource_name)//名字改变
|
||
{
|
||
RenameAsset(assets, oldAssets);
|
||
oldAssets.resource_name = assets.resource_name;
|
||
}
|
||
if (oldAssets.icon != assets.icon)//封面地址不一致
|
||
{
|
||
UpdateAssets(assets, oldAssets, 0);//更新封面
|
||
}
|
||
if (oldAssets.url != assets.url)//资源地址不一致
|
||
{
|
||
isurl = true;
|
||
UpdateAssets(assets, oldAssets, 1);//更新资源
|
||
}
|
||
if (oldAssets.brief != assets.brief || oldAssets.file_size != assets.file_size ||
|
||
oldAssets.group_id != assets.group_id || oldAssets.group_name != assets.group_name ||
|
||
oldAssets.device_id != assets.device_id || oldAssets.play_time != assets.play_time||
|
||
oldAssets.province != assets.province || oldAssets.city != assets.city)//其他信息改变
|
||
{
|
||
|
||
}
|
||
if (!isurl)//如果不是资源地址更改,则记录信息
|
||
UpdateRecordInfo(oldAssets, assets);
|
||
}
|
||
|
||
}
|
||
|
||
serverDatas.Add(assets);
|
||
}
|
||
|
||
CheakDeleteLocalAssets();
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新记录信息
|
||
/// </summary>
|
||
void UpdateRecordInfo(Assets oldAssets, Assets assets)
|
||
{
|
||
assetsDatas.Remove(oldAssets);//移除旧版资源信息
|
||
assetsDatas.Add(assets);//添加新版资源信息
|
||
DownloadAssets.Instance.RecordDownloaddoneAssetsinfo(assets);//重新记录资源信息
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查删除本地资源
|
||
/// </summary>
|
||
void CheakDeleteLocalAssets()
|
||
{
|
||
deleteAssets(Config.AppPath);
|
||
deleteAssets(Config.VideoPath);
|
||
deleteAssets(Config.ImagePath);
|
||
deleteAssets(Config.FilePath);
|
||
deleteAssets(Config.MapPath);
|
||
deleteAssets(Config.PCappPath);
|
||
deleteAssets(Config.OtherPath);
|
||
serverDatas.Clear();
|
||
//删除资源
|
||
void deleteAssets(string path)
|
||
{
|
||
//获取文件夹内所有文件名,含扩展名和不含扩展名形成字典
|
||
var assetsNmaeDic = Directory.GetFiles(path, "*", SearchOption.AllDirectories)
|
||
.Select(file => new { withExtension = Path.GetFileName(file), withoutExtension = Path.GetFileNameWithoutExtension(file) })
|
||
.ToDictionary(item => item.withExtension, item => item.withoutExtension);
|
||
var diffassetsNames = assetsNmaeDic.Keys.Where(x => !serverDatas.Any(y => assetsNmaeDic[x] == y.resource_name)).ToList();//本地存在,服务器不存在资源文件
|
||
if (diffassetsNames.Count() > 0)
|
||
{
|
||
foreach (var assetsName in diffassetsNames)
|
||
{
|
||
string resource_name = assetsNmaeDic[assetsName];
|
||
string urlextension = Path.GetExtension(assetsName).ToLower();
|
||
string icon;
|
||
var asset = assetsDatas.FirstOrDefault(v => v.resource_name == resource_name);
|
||
if (asset == null)
|
||
continue;
|
||
switch (asset.type)
|
||
{
|
||
case 1:
|
||
icon = Directory.EnumerateFiles(Config.AppIconPath).FirstOrDefault(file => Path.GetFileNameWithoutExtension(file) == resource_name);
|
||
break;
|
||
case 2:
|
||
icon = Directory.EnumerateFiles(Config.VideoIconPath).FirstOrDefault(file => Path.GetFileNameWithoutExtension(file) == resource_name);
|
||
break;
|
||
case 6:
|
||
icon = Directory.EnumerateFiles(Config.PCappIconPath).FirstOrDefault(file => Path.GetFileNameWithoutExtension(file) == resource_name);
|
||
break;
|
||
default:
|
||
icon = string.Empty;
|
||
break;
|
||
}
|
||
if (!string.IsNullOrEmpty(icon))
|
||
File.Delete(icon);//删除缩略图
|
||
File.Delete($"{Config.AssetsInfoPath}/{resource_name}.json");//删除json
|
||
|
||
string filename = $"{asset.resource_name}{urlextension}";
|
||
if (DownloadAssets.Instance.AssetsUWR.ContainsKey(filename))
|
||
{
|
||
DownloadAssets.Instance.AssetsUWR[filename].Abort();
|
||
Debug.Log($"{resource_name}:中断下载");
|
||
}
|
||
|
||
if (asset.type != 5)//地图文件不删
|
||
File.Delete($"{path}/{assetsName}");//删除资源
|
||
|
||
assetsDatas.Remove(asset);
|
||
DownloadAssets.Instance.DeleteAssetcell.Invoke(asset, assetsName);
|
||
|
||
Debug.Log($"删除资源{assetsName}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重命名文件
|
||
/// </summary>
|
||
void RenameAsset(Assets assets, Assets oldAssets)
|
||
{
|
||
Debug.Log("资源重命名");
|
||
string iconextension = Path.GetExtension(assets.icon);
|
||
string urlextension = Path.GetExtension(assets.url);
|
||
string iconPath;
|
||
string fileTypePath;
|
||
Judgment(assets.type, out iconPath, out fileTypePath);
|
||
if (!string.IsNullOrEmpty(iconPath))
|
||
rename(iconPath, iconextension);
|
||
rename(fileTypePath, urlextension);
|
||
rename(Config.AssetsInfoPath, ".json");
|
||
|
||
//修改UI上的文件名
|
||
//string extension = Path.GetExtension(oldAssets.url);
|
||
//Transform tran = NetlyPad1.Instance.gamesContent.Find($"{oldAssets.resource_name}{extension}");
|
||
//if (tran)
|
||
//{
|
||
// Game g = tran.GetComponent<Game>();
|
||
// g.apk = assets.brief;
|
||
// tran.name = g.app.text = $"{assets.resource_name}{extension}";
|
||
//}
|
||
|
||
//重命名
|
||
void rename(string fileTypePath, string extension)
|
||
{
|
||
string oldFilePath = $"{fileTypePath}/{oldAssets.resource_name}{extension}";
|
||
string newFilePath = $"{fileTypePath}/{assets.resource_name}{extension}";
|
||
if (File.Exists(oldFilePath))
|
||
File.Move(oldFilePath, newFilePath);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新资源
|
||
/// </summary>
|
||
/// <param name="assets">新版资源</param>
|
||
/// <param name="oldAssets">旧版资源</param>
|
||
/// <param name="module">更新模块:0是只更新图片,1只更新是资源</param>
|
||
void UpdateAssets(Assets assets, Assets oldAssets, int module)
|
||
{
|
||
string iconextension = Path.GetExtension(oldAssets.icon);
|
||
string urlextension = Path.GetExtension(oldAssets.url);
|
||
string iconPath;
|
||
string fileTypePath;
|
||
Judgment(assets.type, out iconPath, out fileTypePath);
|
||
update(module);
|
||
|
||
|
||
void update(int module)
|
||
{
|
||
if (module == 0)//封面更新
|
||
{
|
||
if (!string.IsNullOrEmpty(iconPath))
|
||
{
|
||
Debug.Log("资源封面更新");
|
||
string oldIconFilePath = $"{iconPath}/{oldAssets.resource_name}{iconextension}";
|
||
File.Delete(oldIconFilePath);//删除上版本图片
|
||
string savePath = $"{iconPath}/{assets.resource_name}{Path.GetExtension(assets.icon)}";
|
||
DownloadAssets.Instance.DownLoad(assets.icon, savePath, assets, false, true);
|
||
}
|
||
}
|
||
else if (module == 1)//资源更新
|
||
{
|
||
if (!string.IsNullOrEmpty(fileTypePath))
|
||
{
|
||
Debug.Log("资源文件更新");
|
||
string oldVideoFilePath = $"{fileTypePath}/{oldAssets.resource_name}{urlextension}";
|
||
File.Delete(oldVideoFilePath);//删除上版本资源
|
||
string json = $"{Config.AssetsInfoPath}/{oldAssets.resource_name}.json";
|
||
if (File.Exists(json))
|
||
{
|
||
File.Delete(json);
|
||
assetsDatas.Remove(oldAssets);
|
||
assetsDatas.Add(assets);
|
||
}
|
||
string savePath = $"{fileTypePath}/{assets.resource_name}{Path.GetExtension(assets.url)}";
|
||
DownloadAssets.Instance.DownLoad(assets.url, savePath, assets, false, false);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下载资源
|
||
/// </summary>
|
||
void Download(Assets assets)
|
||
{
|
||
if (string.IsNullOrEmpty(assets.url))
|
||
return;
|
||
|
||
string iconextension = Path.GetExtension(assets.icon);
|
||
string urlextension = Path.GetExtension(assets.url);
|
||
string iconPath;
|
||
string fileTypePath;
|
||
Judgment(assets.type, out iconPath, out fileTypePath);
|
||
downloadAsset();
|
||
|
||
//下载资源
|
||
void downloadAsset()
|
||
{
|
||
//封面图片下载
|
||
if (!string.IsNullOrEmpty(iconPath))
|
||
{
|
||
DownloadAssets.Instance.DownLoad(assets.icon, $"{iconPath}/{assets.resource_name}{iconextension}", assets, true, true);
|
||
}
|
||
//资源下载
|
||
if (!string.IsNullOrEmpty(fileTypePath))
|
||
{
|
||
string assetsSavePath = $"{fileTypePath}/{assets.resource_name}{urlextension}";
|
||
DownloadAssets.Instance.DownLoad(assets.url, assetsSavePath, assets, true, false);
|
||
}
|
||
}
|
||
}
|
||
|
||
void Judgment(int type, out string iconPath, out string fileTypePath)
|
||
{
|
||
switch (type)//1:apk 2:video 3:image 4:file 5:maps 6:pc应用
|
||
{
|
||
case 1:
|
||
iconPath = Config.AppIconPath;
|
||
fileTypePath = Config.AppPath;
|
||
break;
|
||
case 2:
|
||
iconPath = Config.VideoIconPath;
|
||
fileTypePath = Config.VideoPath;
|
||
break;
|
||
case 3:
|
||
iconPath = string.Empty;
|
||
fileTypePath = Config.ImagePath;
|
||
break;
|
||
case 4:
|
||
iconPath = string.Empty;
|
||
fileTypePath = Config.FilePath;
|
||
break;
|
||
case 5:
|
||
iconPath = string.Empty;
|
||
fileTypePath = Config.MapPath;
|
||
break;
|
||
case 6:
|
||
iconPath = Config.PCappIconPath;
|
||
fileTypePath = Config.PCappPath;
|
||
break;
|
||
default:
|
||
iconPath = string.Empty;
|
||
fileTypePath = Config.OtherPath;
|
||
break;
|
||
}
|
||
}
|
||
}
|