188 lines
5.6 KiB
C#
188 lines
5.6 KiB
C#
using System.Collections.Generic;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 运行时使用的资源清单,代替Unity的Manifest
|
||
/// </summary>
|
||
public class AssetDependencyInfo
|
||
{
|
||
public AssetDependencyItem[] assetList;
|
||
public int version;
|
||
|
||
public static AssetDependencyInfo Create(string path)
|
||
{
|
||
var bundle = AssetBundle.LoadFromFile(path);
|
||
AssetDependencyInfo result = null;
|
||
var textAsset = bundle ? bundle.LoadAsset<TextAsset>(AssetConst.versionFile) : null;
|
||
if (bundle)
|
||
bundle.Unload(false);
|
||
if (!textAsset)
|
||
Debug.LogError(string.Format("Unable to load TextAsset from {0}!", AssetConst.versionFile));
|
||
else
|
||
{
|
||
var text = Encoding.UTF8.GetString(textAsset.bytes);
|
||
int version;
|
||
var lines = AssetUtils.TextToLines(text);
|
||
if (lines.Length < 1)
|
||
{
|
||
Debug.LogError("No data in text to create AssetDependencyInfo!");
|
||
}
|
||
else if (!int.TryParse(lines[0], out version))
|
||
{
|
||
Debug.LogError("No version number in text to create AssetDependencyInfo!");
|
||
}
|
||
else
|
||
{
|
||
var error = false;
|
||
var assetList = new List<AssetDependencyItem>();
|
||
for (var i = 1; i < lines.Length; i++)
|
||
{
|
||
var line = lines[i];
|
||
if (!string.IsNullOrEmpty(line))
|
||
{
|
||
var assetItem = AssetDependencyItem.Create(line);
|
||
if (assetItem != null)
|
||
assetList.Add(assetItem);
|
||
else
|
||
{
|
||
error = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!error)
|
||
{
|
||
result = new AssetDependencyInfo
|
||
{
|
||
version = version,
|
||
assetList = assetList.ToArray()
|
||
};
|
||
}
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public void Write(StringBuilder builder)
|
||
{
|
||
builder.Append(version);
|
||
foreach (var item in assetList)
|
||
{
|
||
builder.Append('\n');
|
||
item.Write(builder);
|
||
}
|
||
}
|
||
}
|
||
|
||
public class AssetDependencyItem
|
||
{
|
||
public const int paramLength = 6;
|
||
|
||
// 资源依赖文件
|
||
public string[] dependencies;
|
||
|
||
// 资源文件大小
|
||
public int fileSize;
|
||
|
||
// 资源后台下载排序参数
|
||
public int importance;
|
||
|
||
// 资源等级,对应AssetLevel
|
||
public int level;
|
||
|
||
// 资源hash匹配码
|
||
public string md5;
|
||
|
||
public string name;
|
||
|
||
// 资源版本号
|
||
public int version;
|
||
|
||
public static AssetDependencyItem Create(string line)
|
||
{
|
||
AssetDependencyItem result = null;
|
||
// 机器生成的数据,不会出现需要额外Trim的情况
|
||
var segments = line.Split(';');
|
||
if (segments.Length < paramLength)
|
||
{
|
||
Debug.LogError("No enough data in line: " + line);
|
||
}
|
||
else
|
||
{
|
||
// 注:第一位id仅仅用于适配重用的工具的格式
|
||
var name = segments[0];
|
||
var md5 = segments[1];
|
||
int version;
|
||
int level;
|
||
int fileSize;
|
||
int importance;
|
||
if (!int.TryParse(segments[2], out version))
|
||
{
|
||
Debug.LogError("Unable to get asset version from line: " + line);
|
||
}
|
||
else if (!int.TryParse(segments[3], out level))
|
||
{
|
||
Debug.LogError("Unable to get asset level from line: " + line);
|
||
}
|
||
else if (!int.TryParse(segments[4], out fileSize))
|
||
{
|
||
Debug.LogError("Unable to get asset fileSize from line: " + line);
|
||
}
|
||
else if (!int.TryParse(segments[5], out importance))
|
||
{
|
||
Debug.LogError("Unable to get importance from line: " + line);
|
||
}
|
||
else
|
||
{
|
||
var count = segments.Length - paramLength;
|
||
var dependencies = count > 0 ? new string[count] : new string[0];
|
||
for (var i = 0; i < count; i++)
|
||
dependencies[i] = segments[i + paramLength];
|
||
result = new AssetDependencyItem
|
||
{
|
||
name = name,
|
||
md5 = md5,
|
||
version = version,
|
||
level = level,
|
||
fileSize = fileSize,
|
||
importance = importance,
|
||
dependencies = dependencies
|
||
};
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
public void Write(StringBuilder builder)
|
||
{
|
||
builder.Append(name);
|
||
builder.Append(';');
|
||
builder.Append(md5);
|
||
builder.Append(';');
|
||
builder.Append(version);
|
||
builder.Append(';');
|
||
builder.Append(level);
|
||
builder.Append(';');
|
||
builder.Append(fileSize);
|
||
builder.Append(';');
|
||
builder.Append(importance);
|
||
if (dependencies != null)
|
||
foreach (var dependency in dependencies)
|
||
{
|
||
builder.Append(';');
|
||
builder.Append(dependency);
|
||
}
|
||
}
|
||
}
|
||
|
||
public static class AssetLevel
|
||
{
|
||
// level:0 = 使用时下载;1 = 后台持续下载;2 = 初始化下载;3 = 首包资源
|
||
public const int onUse = 0;
|
||
public const int onRunning = 1;
|
||
public const int onInit = 2;
|
||
public const int inApk = 3;
|
||
} |