南华网站建设,如何百度搜到网站,网站中竖导航栏怎么做,网站建设的实验结论打包出来的热更文件#xff0c;如果每次都要全部上传到CDN文件服务器#xff0c;不进耗费时间长#xff0c;还浪费流量。
所以让AI写了个简单的文件比较工具类#xff0c;然后修改了一下可用。记录一下。
路径可自行更改。校验算法这里使用的是MD5#xff0c;如果使用SH…打包出来的热更文件如果每次都要全部上传到CDN文件服务器不进耗费时间长还浪费流量。
所以让AI写了个简单的文件比较工具类然后修改了一下可用。记录一下。
路径可自行更改。校验算法这里使用的是MD5如果使用SHA256校验时间会长达1分钟MD5只有5秒左右。一般用MD5即可。同步拷贝的写法时间太长改为异步拷贝的写法时间减半只需30秒和3秒。项目的热更文件67个总大小在700M左右
代码如下放到Assets/Editor/路径下即可。
using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEditor;/// summary
/// 比较热更文件的差异 拷贝到另一个文件夹
/// /summary
public class HotfixComparerAsync
{
#if UNITY_ANDROIDstatic string SourceFolder Application.dataPath /../ServerData/Android; // 源文件夹路径static string TargetFolder Application.dataPath /../ServerData/Android_server; // 目标文件夹路径static string OutputFolder Application.dataPath /../ServerData/Android_hotfix; // 输出文件夹路径存放复制的文件
#endif
#if UNITY_IOSstatic string SourceFolder Application.dataPath /../ServerData/iOS; // 源文件夹路径static string TargetFolder Application.dataPath /../ServerData/iOS_server; // 目标文件夹路径static string OutputFolder Application.dataPath /../ServerData/iOS_hotfix; // 输出文件夹路径存放复制的文件
#endif
#if UNITY_STANDALONE_WINstatic string SourceFolder Application.dataPath /../ServerData/StandaloneWindows64; // 源文件夹路径static string TargetFolder Application.dataPath /../ServerData/StandaloneWindows64_server; // 目标文件夹路径static string OutputFolder Application.dataPath /../ServerData/StandaloneWindows64_hotfix; // 输出文件夹路径存放复制的文件
#endif[MenuItem(Tools/比较并拷贝热更文件Async)]public static void CompareAndCopyFiles(){DateTime startTime DateTime.Now;if (!Directory.Exists(OutputFolder)){Directory.CreateDirectory(OutputFolder);}var sourceFiles new HashSetstring(Directory.GetFiles(SourceFolder, *.*, SearchOption.AllDirectories));var targetFilesHashes new Dictionarystring, string();foreach (var file in Directory.GetFiles(TargetFolder, *.*, SearchOption.AllDirectories)){string relativePath MakeRelativePath(file, TargetFolder);targetFilesHashes[relativePath] FileHash(file);}var tasks new ListTask();foreach (var sourceFile in sourceFiles){string relativePath MakeRelativePath(sourceFile, SourceFolder);tasks.Add(Task.Run(() ProcessFile(sourceFile, relativePath, targetFilesHashes)));}Task.WhenAll(tasks.ToArray()).Wait();TimeSpan diff DateTime.Now - startTime;Debug.Log($比较完成耗费{diff.Minutes}分{diff.Seconds}秒);}private static void ProcessFile(string sourceFile, string relativePath, Dictionarystring, string targetFilesHashes){if (targetFilesHashes.ContainsKey(relativePath)){string hash FileHash(sourceFile);if (targetFilesHashes[relativePath] ! hash){CopyFile(sourceFile, Path.Combine(OutputFolder, relativePath));}}else{CopyFile(sourceFile, Path.Combine(OutputFolder, relativePath));}}private static string MakeRelativePath(string path, string rootDirectory){return path.Substring(rootDirectory.Length 1);}private static string FileHash(string file){using (var stream File.OpenRead(file)){using (var hash MD5.Create()){byte[] hashBytes hash.ComputeHash(stream);return BitConverter.ToString(hashBytes).Replace(-, ).ToLowerInvariant();}}}private static void CopyFile(string sourceFile, string targetFile){//File.Copy(sourceFile, targetFile, true);// 获取目标文件的目录路径string targetDirectory Path.GetDirectoryName(targetFile);// 如果目标目录不存在则创建它if (!Directory.Exists(targetDirectory)){Directory.CreateDirectory(targetDirectory);}try{// 尝试复制文件File.Copy(sourceFile, targetFile, true);}catch (Exception e){// 处理可能的异常Debug.LogError($Failed to copy file from {sourceFile} to {targetFile}. Error: {e.Message});}}
}