Skip to content

打包时支持将多个文件打包到二进制文件系统中 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 76 additions & 23 deletions Scripts/Editor/ResourceBuilder/ResourceBuilderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public sealed partial class ResourceBuilderController
private IBuildEventHandler m_BuildEventHandler;
private IFileSystemManager m_FileSystemManager;

private class PackageFileSystemInfo
{
public byte[] fileBytes;
public string fileName;
public PackageFileSystemInfo(string name,byte[] bytes)
{
fileName = name;
fileBytes = bytes;
}
}



public ResourceBuilderController()
{
m_ConfigurationPath = Type.GetConfigurationPath<ResourceBuilderConfigPathAttribute>() ?? Utility.Path.GetRegularPath(Path.Combine(Application.dataPath, "GameFramework/Configs/ResourceBuilder.xml"));
Expand Down Expand Up @@ -1009,33 +1022,45 @@ private bool ProcessAssetBundle(Platform platform, string workingPath, string ou
bytes = Utility.Encryption.GetXorBytes(bytes, hashBytes);
}

return ProcessOutput(platform, outputPackagePath, outputFullPath, outputPackedPath, additionalCompressionSelected, name, variant, fileSystem, resourceData, bytes, length, hashCode, compressedLength, compressedHashCode);
return ProcessOutput(platform, outputPackagePath, outputFullPath, outputPackedPath, additionalCompressionSelected, name, variant, fileSystem, resourceData, bytes, length, hashCode, compressedLength, compressedHashCode, null);
}

private bool ProcessBinary(Platform platform, string workingPath, string outputPackagePath, string outputFullPath, string outputPackedPath, bool additionalCompressionSelected, string name, string variant, string fileSystem)
{
string fullName = GetResourceFullName(name, variant);
ResourceData resourceData = m_ResourceDatas[fullName];
string assetName = resourceData.GetAssetNames()[0];
string assetPath = Utility.Path.GetRegularPath(Application.dataPath.Substring(0, Application.dataPath.Length - AssetsStringLength) + assetName);

// 取出首个文件,利用首个文件的内容生成hashcode后进行加密
string firstAssetName = resourceData.GetAssetNames()[0];
string firstAssetPath = Utility.Path.GetRegularPath(Application.dataPath.Substring(0, Application.dataPath.Length - AssetsStringLength) + firstAssetName);

byte[] firstBytes = File.ReadAllBytes(firstAssetPath);
int firstLength = firstBytes.Length;
int hashCode = Utility.Verifier.GetCrc32(firstBytes);
byte[] hashBytes = Utility.Converter.GetBytes(hashCode);

byte[] bytes = File.ReadAllBytes(assetPath);
int length = bytes.Length;
int hashCode = Utility.Verifier.GetCrc32(bytes);
int compressedLength = length;
int iLength = 0;
int compressedHashCode = hashCode;

byte[] hashBytes = Utility.Converter.GetBytes(hashCode);
if (resourceData.LoadType == LoadType.LoadFromBinaryAndQuickDecrypt)
List<PackageFileSystemInfo> infos = new List<PackageFileSystemInfo>();
foreach (string resourceAsset in resourceData.GetAssetNames())
{
bytes = Utility.Encryption.GetQuickXorBytes(bytes, hashBytes);
}
else if (resourceData.LoadType == LoadType.LoadFromBinaryAndDecrypt)
{
bytes = Utility.Encryption.GetXorBytes(bytes, hashBytes);
}
string assetPath = Utility.Path.GetRegularPath(Application.dataPath.Substring(0, Application.dataPath.Length - AssetsStringLength) + resourceAsset);
byte[] bytes = File.ReadAllBytes(assetPath);
iLength += bytes.Length;
if (resourceData.LoadType == LoadType.LoadFromBinaryAndQuickDecrypt)
{
bytes = Utility.Encryption.GetQuickXorBytes(bytes, hashBytes);
}
else if (resourceData.LoadType == LoadType.LoadFromBinaryAndDecrypt)
{
bytes = Utility.Encryption.GetXorBytes(bytes, hashBytes);
}

return ProcessOutput(platform, outputPackagePath, outputFullPath, outputPackedPath, additionalCompressionSelected, name, variant, fileSystem, resourceData, bytes, length, hashCode, compressedLength, compressedHashCode);
infos.Add(new PackageFileSystemInfo(resourceAsset, bytes));
}
int iCompressedLength = iLength;
return ProcessOutput(platform, outputPackagePath, outputFullPath, outputPackedPath, additionalCompressionSelected, name, variant, fileSystem, resourceData, null, firstLength, hashCode, iCompressedLength, compressedHashCode, infos);
}

private void ProcessPackageVersionList(string outputPackagePath, Platform platform)
Expand Down Expand Up @@ -1272,6 +1297,19 @@ private int[] GetResourceIndexesFromFileSystem(IEnumerable<ResourceData> resourc
return resourceIndexes.ToArray();
}

private int GetResourceFileCountFromFileSystem(IEnumerable<ResourceData> resourceDatas, string fileSystemName)
{
int index = 0;
foreach (ResourceData resourceData in resourceDatas)
{
if (resourceData.FileSystem == fileSystemName)
{
index += resourceData.AssetCount;
}
}
return index;
}

private string[] GetResourceGroupNames(IEnumerable<ResourceData> resourceDatas)
{
HashSet<string> resourceGroupNames = new HashSet<string>();
Expand Down Expand Up @@ -1320,7 +1358,8 @@ private void CreateFileSystems(IEnumerable<ResourceData> resourceDatas, string o

foreach (string fileSystemName in fileSystemNames)
{
int fileCount = GetResourceIndexesFromFileSystem(resourceDatas, fileSystemName).Length;
//int fileCount = GetResourceIndexesFromFileSystem(resourceDatas, fileSystemName).Length;
int fileCount = GetResourceFileCountFromFileSystem(resourceDatas, fileSystemName);
string fullPath = Utility.Path.GetRegularPath(Path.Combine(outputPath, Utility.Text.Format("{0}.{1}", fileSystemName, DefaultExtension)));
string directory = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(directory))
Expand All @@ -1333,7 +1372,7 @@ private void CreateFileSystems(IEnumerable<ResourceData> resourceDatas, string o
}
}

private bool ProcessOutput(Platform platform, string outputPackagePath, string outputFullPath, string outputPackedPath, bool additionalCompressionSelected, string name, string variant, string fileSystem, ResourceData resourceData, byte[] bytes, int length, int hashCode, int compressedLength, int compressedHashCode)
private bool ProcessOutput(Platform platform, string outputPackagePath, string outputFullPath, string outputPackedPath, bool additionalCompressionSelected, string name, string variant, string fileSystem, ResourceData resourceData, byte[] bytes, int length, int hashCode, int compressedLength, int compressedHashCode, List<PackageFileSystemInfo> fileBytesInFileSystems)
{
string fullNameWithExtension = Utility.Text.Format("{0}.{1}", GetResourceFullName(name, variant), GetExtension(resourceData));

Expand All @@ -1352,9 +1391,15 @@ private bool ProcessOutput(Platform platform, string outputPackagePath, string o
}
else
{
if (!m_OutputPackageFileSystems[fileSystem].WriteFile(fullNameWithExtension, bytes))
if (fileBytesInFileSystems != null && fileBytesInFileSystems.Count > 0)
{
return false;
foreach (PackageFileSystemInfo info in fileBytesInFileSystems)
{
if (!m_OutputPackageFileSystems[fileSystem].WriteFile(info.fileName, info.fileBytes))
{
return false;
}
}
}
}
}
Expand All @@ -1374,14 +1419,20 @@ private bool ProcessOutput(Platform platform, string outputPackagePath, string o
}
else
{
if (!m_OutputPackedFileSystems[fileSystem].WriteFile(fullNameWithExtension, bytes))
if (fileBytesInFileSystems != null && fileBytesInFileSystems.Count > 0)
{
return false;
foreach (PackageFileSystemInfo info in fileBytesInFileSystems)
{
if (!m_OutputPackedFileSystems[fileSystem].WriteFile(info.fileName, info.fileBytes))
{
return false;
}
}
}
}
}

if (OutputFullSelected)
if (OutputFullSelected && bytes != null)
{
string fullNameWithCrc32AndExtension = variant != null ? Utility.Text.Format("{0}.{1}.{2:x8}.{3}", name, variant, hashCode, DefaultExtension) : Utility.Text.Format("{0}.{1:x8}.{2}", name, hashCode, DefaultExtension);
string fullPath = Utility.Path.GetRegularPath(Path.Combine(outputFullPath, fullNameWithCrc32AndExtension));
Expand All @@ -1408,6 +1459,8 @@ private bool ProcessOutput(Platform platform, string outputPackagePath, string o
return true;
}



private BuildAssetBundleOptions GetBuildAssetBundleOptions()
{
BuildAssetBundleOptions buildOptions = BuildAssetBundleOptions.DeterministicAssetBundle;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Editor/ResourceCollection/ResourceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public bool AssignAsset(string guid, string name, string variant)
}

Asset asset = GetAsset(guid);
if (resource.IsLoadFromBinary && assetsInResource.Length > 0 && asset != assetsInResource[0])
if (resource.IsLoadFromBinary && assetsInResource.Length > 0 && asset != null && asset != assetsInResource[0])
{
return false;
}
Expand Down