miduo_client/Assets/Scripts/Bridge/VersionManager.cs

233 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameCore;
using ResUpdate;
using System.Linq;
namespace JLLogic
{
public class Version
{
Hashtable info;
public Version(string json)
{
info = MiniJSON.jsonDecode(json) as Hashtable;
}
public string ToJson()
{
return MiniJSON.jsonEncode(info);
}
public string GetInfo(string key)
{
if (info.ContainsKey(key))
{
return info[key] as string;
}
return null;
}
public void RemoveInfo(string key)
{
if (info.ContainsKey(key))
{
info.Remove(key);
}
}
public void SetInfo(string key, string value)
{
info[key] = value;
}
}
/// <summary>
/// 版本管理器
/// </summary>
public class VersionManager : Singleton<VersionManager>
{
/// <summary>
/// 版本号文件
/// </summary>
const string VersionsFile = "version";
/// <summary>
/// 外部版本号
/// </summary>
Version externalVersion;
/// <summary>
/// 流媒体目录中的游戏及其版本号
/// </summary>
Version internalVersion;
public void Initialize()
{
InitVersions();
CheckApkVersionInfo();
}
/// <summary>
/// 初始化资源版本号
/// </summary>
public void InitVersions()
{
try
{
if (File.Exists(VersionsFilePath))
{
externalVersion = new Version(File.ReadAllText(VersionsFilePath, System.Text.Encoding.UTF8));
}
}
catch (Exception e) {
Debug.LogError(e);
}
try
{
internalVersion = new Version(Resources.Load<TextAsset>(VersionsFile).text);
}
catch(Exception e)
{
Debug.LogError(e);
}
}
/// <summary>
/// 保存版本号
/// </summary>
/// <param name="version"></param>
public void SaveVersion(string version)
{
if (externalVersion != null)
{
externalVersion.SetInfo("version", version);
SaveToFiles(externalVersion);
return;
}
if (internalVersion != null)
{
internalVersion.SetInfo("version", version);
SaveToFiles(internalVersion);
return;
}
}
/// <summary>
/// 获取版本号
/// 1.优先从外部拿
/// 2.拿不到返回-1
/// </summary>
/// <returns></returns>
public string GetLocalVersion()
{
string extVersion = externalVersion != null ? externalVersion.GetInfo("version") : "";
string intVersion = internalVersion != null ? internalVersion.GetInfo("version") : "";
int aV, rV;
string[] extVersions = extVersion.Split('.');
if (extVersion.Length != intVersion.Length || !int.TryParse(extVersions[1], out aV) || !int.TryParse(extVersions[2], out rV))
{
Debug.Log("VersionManager GetLocalVersion version: " + intVersion);
return intVersion;
}
Debug.Log("VersionManager GetLocalVersion version: " + extVersion);
return extVersion;
}
/// <summary>
/// 检测是否apk信息
/// </summary>
private void CheckApkVersionInfo()
{
string inteVer = internalVersion != null ? internalVersion.GetInfo("version") : "";
string extVer = externalVersion != null ? externalVersion.GetInfo("version") : "";
ResourcesUpdateState state = VersionManager.VersionCompare(inteVer, extVer);
if (state == ResourcesUpdateState.OldPackageNeedChange)
{
ResourcesManager.Instance.ClearFilesCache();
}
}
/// <summary>
/// 对比版本号,返回不一样的位数 -1表示对比异常0表示相同其余表示按位不同
/// </summary>
/// <param name="ver1"> serverversion </param>
/// <param name="ver2"> localversion </param>
/// <returns></returns>
public static ResourcesUpdateState VersionCompare(string ver1, string ver2)
{
string[] vs1 = ver1.Split('.');
string[] vs2 = ver2.Split('.');
if (vs1.Length != vs2.Length || vs1.Length < 3 || vs2.Length < 3)
{
Debug.LogError("VersionManager VersionCompare vs1.Length != vs2.Length! vs1 count: " + vs1.Length + " vs2 count: " + vs2.Length);
return ResourcesUpdateState.Err;
}
int v1, v2;
// 对比大版本
if (!int.TryParse(vs1[1], out v1) || !int.TryParse(vs2[1], out v2))
{
Debug.LogError(string.Format("VersionManager VersionCompare vs1:{0} or vs2: {1} pos 1 Conver to int err!!", vs1, vs2));
return ResourcesUpdateState.Err;
}
if (v1 != v2) return ResourcesUpdateState.OldPackageNeedChange;
// 对比小版本
if (!int.TryParse(vs1[2], out v1) || !int.TryParse(vs2[2], out v2))
{
Debug.LogError(string.Format("VersionManager VersionCompare vs1:{0} or vs2: {1} pos 2 Conver to int err!!", vs1, vs2));
return ResourcesUpdateState.Err;
}
if (v1 != v2) return ResourcesUpdateState.PackageNeedRes;
return ResourcesUpdateState.Normal;
}
/// <summary>
/// 获取版本内容,优先拿外部
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetVersionInfo(string key)
{
if (externalVersion != null)
{
return externalVersion.GetInfo(key);
}
if (internalVersion != null)
{
return internalVersion.GetInfo(key);
}
return null;
}
/// <summary>
/// 保存到文件
/// </summary>
void SaveToFiles(Version version)
{
string directoryPath = Path.GetDirectoryName(VersionsFilePath);
if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
File.WriteAllText(VersionsFilePath, version.ToJson(), System.Text.Encoding.UTF8);
}
/// <summary>
/// 版本文件路径
/// </summary>
string VersionsFilePath
{
get
{
return AppConst.PersistentDataPath + VersionsFile + ".txt";
}
}
}
}