首次提交
This commit is contained in:
61
Assets/Scripts/NBC/Asset/Runtime/Address/Data/AssetData.cs
Normal file
61
Assets/Scripts/NBC/Asset/Runtime/Address/Data/AssetData.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
[Serializable]
|
||||
public class AssetData
|
||||
{
|
||||
public string Name;
|
||||
public int Bundle;
|
||||
|
||||
/// <summary>
|
||||
/// 所属目录
|
||||
/// </summary>
|
||||
public int Dir;
|
||||
|
||||
// /// <summary>
|
||||
// /// 依赖的bundle
|
||||
// /// </summary>
|
||||
// public List<int> Deps = new List<int>();
|
||||
|
||||
/// <summary>
|
||||
/// 资源可寻址地址
|
||||
/// </summary>
|
||||
public string Address;
|
||||
|
||||
/// <summary>
|
||||
/// 资源真实地址
|
||||
/// </summary>
|
||||
public string Path { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 资源Bundle
|
||||
/// </summary>
|
||||
public string BundleName { get; set; }
|
||||
|
||||
|
||||
public string[] Tags { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否包含Tag
|
||||
/// </summary>
|
||||
public bool HasTag(string[] tags)
|
||||
{
|
||||
if (tags == null || tags.Length == 0)
|
||||
return false;
|
||||
if (Tags == null || Tags.Length == 0)
|
||||
return false;
|
||||
|
||||
foreach (var tag in tags)
|
||||
{
|
||||
if (Tags.Contains(tag))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 591cdedd5ee84c1c98d1d80d4c8f44fc
|
||||
timeCreated: 1677230741
|
||||
114
Assets/Scripts/NBC/Asset/Runtime/Address/Data/BundleData.cs
Normal file
114
Assets/Scripts/NBC/Asset/Runtime/Address/Data/BundleData.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
[Serializable]
|
||||
public class BundleData
|
||||
{
|
||||
public string Name;
|
||||
public string Hash;
|
||||
public int Size;
|
||||
|
||||
// /// <summary>
|
||||
// /// 加载方法
|
||||
// /// </summary>
|
||||
// public byte LoadMethod;
|
||||
|
||||
/// <summary>
|
||||
/// 资源包的分类标签
|
||||
/// </summary>
|
||||
public string[] Tags;
|
||||
|
||||
/// <summary>
|
||||
/// 依赖的bundleId
|
||||
/// </summary>
|
||||
public List<int> Deps = new List<int>();
|
||||
|
||||
/// <summary>
|
||||
/// 所属的包裹名称
|
||||
/// </summary>
|
||||
public string PackageName { set; get; }
|
||||
|
||||
public List<string> DependBundles { get; private set; } = new List<string>();
|
||||
|
||||
private string _nameHash = string.Empty;
|
||||
|
||||
public string NameHash
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_nameHash)) return _nameHash;
|
||||
_nameHash = Util.NameAddHash(Name, Hash);
|
||||
return _nameHash;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 内置文件路径
|
||||
/// </summary>
|
||||
private string _streamingFilePath;
|
||||
|
||||
public string StreamingFilePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(_streamingFilePath) == false)
|
||||
return _streamingFilePath;
|
||||
|
||||
_streamingFilePath = Const.GetStreamingPath(NameHash);
|
||||
return _streamingFilePath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存的数据文件路径
|
||||
/// </summary>
|
||||
private string _cachedDataFilePath;
|
||||
|
||||
public string CachedDataFilePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(_cachedDataFilePath) == false)
|
||||
return _cachedDataFilePath;
|
||||
_cachedDataFilePath = Const.GetCachePath(NameHash);
|
||||
return _cachedDataFilePath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 远程的数据文件路径
|
||||
/// </summary>
|
||||
private string _remoteDataFilePath;
|
||||
|
||||
public string RemoteDataFilePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(_remoteDataFilePath) == false)
|
||||
return _remoteDataFilePath;
|
||||
_remoteDataFilePath = Const.GetRemotePath(NameHash);
|
||||
return _remoteDataFilePath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 临时的数据文件路径
|
||||
/// </summary>
|
||||
private string _tempDataFilePath;
|
||||
|
||||
public string TempDataFilePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(_tempDataFilePath) == false)
|
||||
return _tempDataFilePath;
|
||||
|
||||
_tempDataFilePath = $"{CachedDataFilePath}.temp";
|
||||
return _tempDataFilePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de6f2556c6e147afb0269b9ca02c6260
|
||||
timeCreated: 1677230723
|
||||
15
Assets/Scripts/NBC/Asset/Runtime/Address/Data/PackageData.cs
Normal file
15
Assets/Scripts/NBC/Asset/Runtime/Address/Data/PackageData.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
[Serializable]
|
||||
public class PackageData
|
||||
{
|
||||
public string Name;
|
||||
public int Def;
|
||||
public List<string> Dirs;
|
||||
public List<AssetData> Assets;
|
||||
public List<BundleData> Bundles;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10936edd968649ecbf4b7a1bb20e84cf
|
||||
timeCreated: 1678156381
|
||||
63
Assets/Scripts/NBC/Asset/Runtime/Address/Data/VersionData.cs
Normal file
63
Assets/Scripts/NBC/Asset/Runtime/Address/Data/VersionData.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
// [Serializable]
|
||||
// public class VersionPackagesData
|
||||
// {
|
||||
// public string Ver;
|
||||
// public string Name;
|
||||
// public string Hash;
|
||||
// public int Size;
|
||||
// public int Def;
|
||||
// public string NameHash => $"{Name}_{Hash}.json";
|
||||
// }
|
||||
//
|
||||
// [Serializable]
|
||||
// public class VersionData
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// app版本号
|
||||
// /// </summary>
|
||||
// public string AppVer;
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 版本包
|
||||
// /// </summary>
|
||||
// public List<VersionPackagesData> Packages = new List<VersionPackagesData>();
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 导出时间
|
||||
// /// </summary>
|
||||
// public long BuildTime;
|
||||
// }
|
||||
|
||||
[Serializable]
|
||||
public class VersionPackageData
|
||||
{
|
||||
public List<PackageData> Packages = new List<PackageData>();
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class VersionData
|
||||
{
|
||||
/// <summary>
|
||||
/// app版本号
|
||||
/// </summary>
|
||||
public string AppVer;
|
||||
|
||||
/// <summary>
|
||||
/// 版本包hash
|
||||
/// </summary>
|
||||
public string Hash;
|
||||
|
||||
public long Size;
|
||||
/// <summary>
|
||||
/// 导出时间
|
||||
/// </summary>
|
||||
public long BuildTime;
|
||||
|
||||
public string NameHash => $"packages_{Hash}.json";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 671aac8f73924f1bae2ef7753fb6b239
|
||||
timeCreated: 1675820403
|
||||
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
internal class VersionDataReader
|
||||
{
|
||||
private readonly Dictionary<string, BundleData> _bundles = new Dictionary<string, BundleData>();
|
||||
|
||||
private readonly Dictionary<string, AssetData> _assets = new Dictionary<string, AssetData>();
|
||||
private readonly Dictionary<string, string> _addressablePath = new Dictionary<string, string>();
|
||||
public VersionData VersionData { get; private set; }
|
||||
|
||||
public List<PackageData> PackageDataList { get; private set; } = new List<PackageData>();
|
||||
|
||||
public VersionDataReader()
|
||||
{
|
||||
ReadVersionData();
|
||||
}
|
||||
|
||||
public string GetAssetRealPath(string path)
|
||||
{
|
||||
return _addressablePath.TryGetValue(path, out var realPath) ? realPath : path;
|
||||
}
|
||||
|
||||
public AssetData GetAsset(string path)
|
||||
{
|
||||
return _assets.TryGetValue(path, out var assetRef) ? assetRef : null;
|
||||
}
|
||||
|
||||
public List<BundleData> GetAllBundle()
|
||||
{
|
||||
List<BundleData> list = new List<BundleData>(_bundles.Count);
|
||||
list.AddRange(_bundles.Values);
|
||||
return list;
|
||||
}
|
||||
|
||||
public BundleData GetBundle(string name)
|
||||
{
|
||||
return _bundles.TryGetValue(name, out var bundleRef) ? bundleRef : null;
|
||||
}
|
||||
|
||||
public BundleData GetBundleByAsset(string assetPath)
|
||||
{
|
||||
var asset = GetAsset(assetPath);
|
||||
return asset == null ? null : GetBundle(asset.BundleName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源所需的所有bundle信息(下标0为所在包)
|
||||
/// </summary>
|
||||
/// <param name="path">资源原始地址</param>
|
||||
/// <returns></returns>
|
||||
public BundleData[] GetAllDependBundle(string path)
|
||||
{
|
||||
if (_assets.TryGetValue(path, out var assetData))
|
||||
{
|
||||
if (_bundles.TryGetValue(assetData.BundleName, out var bundleData))
|
||||
{
|
||||
var needBundle = new List<BundleData> { bundleData };
|
||||
needBundle.AddRange(bundleData.DependBundles.Select(bundle => _bundles[bundle]));
|
||||
return needBundle.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
return Array.Empty<BundleData>();
|
||||
}
|
||||
|
||||
private void ReadVersionData()
|
||||
{
|
||||
VersionData = ReadJson<VersionData>(Const.VersionFileName);
|
||||
if (VersionData != null)
|
||||
{
|
||||
var packageData =
|
||||
ReadJson<VersionPackageData>(VersionData.NameHash);
|
||||
if (packageData != null)
|
||||
{
|
||||
foreach (var package in packageData.Packages)
|
||||
{
|
||||
ReadPackage(package);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("version data is null");
|
||||
}
|
||||
}
|
||||
|
||||
private void ReadPackage(PackageData packageData)
|
||||
{
|
||||
if (packageData != null)
|
||||
{
|
||||
foreach (var bundle in packageData.Bundles)
|
||||
{
|
||||
foreach (var dep in bundle.Deps)
|
||||
{
|
||||
var depBundle = packageData.Bundles[dep];
|
||||
if (depBundle != null)
|
||||
{
|
||||
bundle.DependBundles.Add(depBundle.Name);
|
||||
}
|
||||
}
|
||||
|
||||
bundle.PackageName = packageData.Name;
|
||||
_bundles[bundle.Name] = bundle;
|
||||
}
|
||||
|
||||
foreach (var asset in packageData.Assets)
|
||||
{
|
||||
if (asset.Dir < 0 || asset.Dir >= packageData.Dirs.Count) continue;
|
||||
if (asset.Bundle < 0 || asset.Bundle >= packageData.Bundles.Count) continue;
|
||||
var dir = packageData.Dirs[asset.Dir];
|
||||
var bundle = packageData.Bundles[asset.Bundle];
|
||||
asset.Path = $"{dir}/{asset.Name}";
|
||||
asset.BundleName = bundle.Name;
|
||||
_assets[asset.Path] = asset;
|
||||
var filePath = $"{dir}/{Path.GetFileNameWithoutExtension(asset.Name)}";
|
||||
//去除后缀后,默认加入寻址
|
||||
_addressablePath[filePath] = asset.Path;
|
||||
if (asset.Address != asset.Path)
|
||||
{
|
||||
_addressablePath[asset.Address] = asset.Path;
|
||||
}
|
||||
}
|
||||
|
||||
PackageDataList.Add(packageData);
|
||||
}
|
||||
}
|
||||
|
||||
private T ReadJson<T>(string fileName) where T : new()
|
||||
{
|
||||
return Util.ReadJson<T>(Const.GetCachePath(fileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df6f17d0c94e4f9ca2145fab20c9052f
|
||||
timeCreated: 1678083955
|
||||
Reference in New Issue
Block a user