266 lines
8.8 KiB
C#
266 lines
8.8 KiB
C#
using System.Collections.Generic;
|
||
using System;
|
||
using UnityEngine;
|
||
using System.IO;
|
||
using System.IO.Compression;
|
||
using System.Text;
|
||
|
||
namespace BF
|
||
{
|
||
[Serializable]
|
||
public class AssetBundleConfigCollection
|
||
{
|
||
public string version = "";
|
||
public List<AssetBundleConfig> mainConfigs = new List<AssetBundleConfig>();
|
||
public List<LanguageConfig> languageConfigs = new List<LanguageConfig>();
|
||
|
||
[NonSerialized]
|
||
public List<AssetBundleConfig> allConfigs = new List<AssetBundleConfig>();
|
||
|
||
Dictionary<string, LanguageConfig> configsKeyLangaugeName = new Dictionary<string, LanguageConfig>();
|
||
|
||
Dictionary<string, AssetBundleConfig> configsKeyABPath;
|
||
Dictionary<string, AssetBundleConfig> configsKeyAssetPath;
|
||
Dictionary<string, AssetBundleConfig> configsKeyMd5;
|
||
|
||
Dictionary<string, AssetBundleConfig> configsKeyMainABPath;
|
||
|
||
public AssetBundleConfig GetConfigByBundlePath(string bundlePath)
|
||
{
|
||
if (!configsKeyABPath.TryGetValue(bundlePath, out AssetBundleConfig config))
|
||
{
|
||
return null;
|
||
}
|
||
return config;
|
||
}
|
||
|
||
public AssetBundleConfig GetConfigByMainBundlePath(string bundlePath)
|
||
{
|
||
if (!configsKeyMainABPath.TryGetValue(bundlePath, out AssetBundleConfig config))
|
||
{
|
||
return null;
|
||
}
|
||
return config;
|
||
}
|
||
|
||
public AssetBundleConfig GetConfigByAssetPath(string assetPath)
|
||
{
|
||
if (!configsKeyAssetPath.TryGetValue(assetPath, out AssetBundleConfig config))
|
||
{
|
||
BFLog.Log("[ResMgr] assetpath : \"{0}\" config missing!", assetPath);
|
||
return null;
|
||
}
|
||
return config;
|
||
}
|
||
|
||
public AssetBundleConfig GetConfigByMd5(string md5)
|
||
{
|
||
if (!configsKeyMd5.TryGetValue(md5, out AssetBundleConfig config))
|
||
{
|
||
return null;
|
||
}
|
||
return config;
|
||
}
|
||
|
||
public string ToJson()
|
||
{
|
||
return JsonUtility.ToJson(this);
|
||
}
|
||
|
||
public void AddAssetBundleConfig(AssetBundleConfig cfg)
|
||
{
|
||
mainConfigs.Add(cfg);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加多语言包资源
|
||
/// </summary>
|
||
public void AddLangAssetBundleConfig(string languageName, AssetBundleConfig cfg)
|
||
{
|
||
if (GetLanguageConfigByName(languageName, out LanguageConfig languageConfig))
|
||
{
|
||
languageConfig.AddAssetBundleConfig(cfg);
|
||
}
|
||
else
|
||
{
|
||
var newLanguageConfig = new LanguageConfig();
|
||
newLanguageConfig.languageName = languageName;
|
||
newLanguageConfig.AddAssetBundleConfig(cfg);
|
||
languageConfigs.Add(newLanguageConfig);
|
||
}
|
||
}
|
||
|
||
public bool GetLanguageConfigByName(string name, out LanguageConfig languageConfig)
|
||
{
|
||
if (configsKeyLangaugeName.TryGetValue(name, out LanguageConfig result))
|
||
{
|
||
languageConfig = result;
|
||
return true;
|
||
}
|
||
for (var i = 0; i < languageConfigs.Count; i++)
|
||
{
|
||
var temp = languageConfigs[i];
|
||
if (temp.languageName == name)
|
||
{
|
||
languageConfig = temp;
|
||
configsKeyLangaugeName.Add(name, temp);
|
||
return true;
|
||
}
|
||
}
|
||
languageConfig = null;
|
||
return false;
|
||
}
|
||
|
||
public static AssetBundleConfigCollection Create(string json)
|
||
{
|
||
var cfg = JsonUtility.FromJson<AssetBundleConfigCollection>(json);
|
||
var bp = new Dictionary<string, AssetBundleConfig>();
|
||
var mbp = new Dictionary<string, AssetBundleConfig>();
|
||
var p = new Dictionary<string, AssetBundleConfig>();
|
||
var m = new Dictionary<string, AssetBundleConfig>();
|
||
var allConfigs = new List<AssetBundleConfig>();
|
||
|
||
var mainConfigs = cfg.mainConfigs;
|
||
for (var i = 0; i < mainConfigs.Count; i++)
|
||
{
|
||
var config = mainConfigs[i];
|
||
bp.Add(config.assetBundlePath, config);
|
||
mbp.Add(config.assetBundlePath, config);
|
||
m.Add(config.md5, config);
|
||
|
||
if (config.assetsPath == null || config.assetsPath.Length == 0)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
for (var j = 0; j < config.assetsPath.Length; j++)
|
||
{
|
||
p.Add(config.assetsPath[j], config);
|
||
}
|
||
}
|
||
allConfigs.AddRange(mainConfigs);
|
||
|
||
var languageConfigs = cfg.languageConfigs;
|
||
for (var i = 0; i < languageConfigs.Count; i++)
|
||
{
|
||
var langConfig = languageConfigs[i];
|
||
allConfigs.AddRange(langConfig.configs);
|
||
for (var j = 0; j < langConfig.configs.Count; j++)
|
||
{
|
||
var config = langConfig.configs[j];
|
||
bp.Add(config.assetBundlePath, config);
|
||
m.Add(config.md5, config);
|
||
|
||
if (config.assetsPath == null || config.assetsPath.Length == 0)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
for (var k = 0; k < config.assetsPath.Length; k++)
|
||
{
|
||
p.Add(config.assetsPath[k], config);
|
||
}
|
||
}
|
||
}
|
||
|
||
var abcc = new AssetBundleConfigCollection();
|
||
abcc.version = cfg.version;
|
||
abcc.configsKeyABPath = bp;
|
||
abcc.configsKeyMainABPath = mbp;
|
||
abcc.configsKeyAssetPath = p;
|
||
abcc.mainConfigs = mainConfigs;
|
||
abcc.languageConfigs = languageConfigs;
|
||
abcc.configsKeyMd5 = m;
|
||
abcc.allConfigs = allConfigs;
|
||
return abcc;
|
||
}
|
||
|
||
public static byte[] Compress(string json)
|
||
{
|
||
var bytes = Encoding.Default.GetBytes(json);
|
||
var compressStream = new MemoryStream();
|
||
var zipStream = new GZipStream(compressStream, CompressionMode.Compress);
|
||
zipStream.Write(bytes, 0, bytes.Length);
|
||
zipStream.Close();
|
||
var result = compressStream.ToArray();
|
||
compressStream.Close();
|
||
return result;
|
||
}
|
||
|
||
public static string Decompress(byte[] bytes)
|
||
{
|
||
var compressStream = new MemoryStream(bytes);
|
||
var zipStream = new GZipStream(compressStream, CompressionMode.Decompress);
|
||
var resultStream = new MemoryStream();
|
||
zipStream.CopyTo(resultStream);
|
||
zipStream.Close();
|
||
var result = Encoding.Default.GetString(resultStream.ToArray());
|
||
resultStream.Close();
|
||
return result;
|
||
}
|
||
}
|
||
|
||
[Serializable]
|
||
public class LanguageConfig
|
||
{
|
||
public string languageName;
|
||
public List<AssetBundleConfig> configs = new List<AssetBundleConfig>();
|
||
|
||
public void AddAssetBundleConfig(AssetBundleConfig cfg)
|
||
{
|
||
configs.Add(cfg);
|
||
}
|
||
}
|
||
|
||
[Serializable]
|
||
public class AssetBundleConfig
|
||
{
|
||
public string assetBundlePath; // ab路径,提前存储好,避免运行时进行字符串拼接
|
||
public string[] assetsPath; // 资源路径,多个资源可能对应一个bundle
|
||
public string md5; // 资源md5,每次启动更新流程时对下载完成的资源以及本地存储的资源进行检查
|
||
public string zipPath; // 如果ab划分比较细,需要进行压缩来节省下载时间。
|
||
public long rawSize; // 文件大小
|
||
|
||
[NonSerialized]
|
||
public byte location; // 0 = streamAsset, 1 = persistentDataPath 只在内存中更新
|
||
|
||
public override string ToString()
|
||
{
|
||
var sb = new StringBuilder();
|
||
sb.Append("[ ");
|
||
sb.Append("assetBundlePath : ");
|
||
sb.Append(assetBundlePath);
|
||
sb.Append(". ");
|
||
|
||
sb.Append("assetPath : ");
|
||
if (assetsPath != null)
|
||
{
|
||
for (int i = 0; i < assetsPath.Length; i++)
|
||
{
|
||
sb.Append(assetsPath[i]);
|
||
sb.Append(", ");
|
||
}
|
||
}
|
||
|
||
sb.Append(". ");
|
||
|
||
sb.Append("md5 : ");
|
||
sb.Append(md5);
|
||
sb.Append(". ");
|
||
|
||
sb.Append("zipPath : ");
|
||
sb.Append(zipPath);
|
||
sb.Append(". ");
|
||
|
||
sb.Append("location : ");
|
||
sb.Append(location);
|
||
sb.Append(". ");
|
||
|
||
sb.Append("rawSize : ");
|
||
sb.Append(rawSize);
|
||
sb.Append(". ");
|
||
|
||
return sb.ToString();
|
||
}
|
||
}
|
||
} |