zip压缩加载测试

This commit is contained in:
2025-05-23 18:26:47 +08:00
parent ada423ac91
commit cbd48e8411
147 changed files with 7855 additions and 6 deletions

View File

@@ -0,0 +1,288 @@
using UnityEditor;
using System.Linq;
using System;
using System.IO;
static class BuildCommand
{
private const string KEYSTORE_PASS = "KEYSTORE_PASS";
private const string KEY_ALIAS_PASS = "KEY_ALIAS_PASS";
private const string KEY_ALIAS_NAME = "KEY_ALIAS_NAME";
private const string KEYSTORE = "keystore.keystore";
private const string BUILD_OPTIONS_ENV_VAR = "BuildOptions";
private const string ANDROID_BUNDLE_VERSION_CODE = "VERSION_BUILD_VAR";
private const string ANDROID_APP_BUNDLE = "BUILD_APP_BUNDLE";
private const string SCRIPTING_BACKEND_ENV_VAR = "SCRIPTING_BACKEND";
private const string VERSION_NUMBER_VAR = "VERSION_NUMBER_VAR";
private const string VERSION_iOS = "VERSION_BUILD_VAR";
static string GetArgument(string name)
{
string[] args = Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length; i++)
{
if (args[i].Contains(name))
{
return args[i + 1];
}
}
return null;
}
static string[] GetEnabledScenes()
{
return (
from scene in EditorBuildSettings.scenes
where scene.enabled
where !string.IsNullOrEmpty(scene.path)
select scene.path
).ToArray();
}
static BuildTarget GetBuildTarget()
{
string buildTargetName = GetArgument("customBuildTarget");
Console.WriteLine(":: Received customBuildTarget " + buildTargetName);
if (buildTargetName.ToLower() == "android")
{
#if !UNITY_5_6_OR_NEWER
// https://issuetracker.unity3d.com/issues/buildoptions-dot-acceptexternalmodificationstoplayer-causes-unityexception-unknown-project-type-0
// Fixed in Unity 5.6.0
// side effect to fix android build system:
EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Internal;
#endif
}
if (buildTargetName.TryConvertToEnum(out BuildTarget target))
return target;
Console.WriteLine($":: {nameof(buildTargetName)} \"{buildTargetName}\" not defined on enum {nameof(BuildTarget)}, using {nameof(BuildTarget.NoTarget)} enum to build");
return BuildTarget.NoTarget;
}
static string GetBuildPath()
{
string buildPath = GetArgument("customBuildPath");
Console.WriteLine(":: Received customBuildPath " + buildPath);
if (buildPath == "")
{
throw new Exception("customBuildPath argument is missing");
}
return buildPath;
}
static string GetBuildName()
{
string buildName = GetArgument("customBuildName");
Console.WriteLine(":: Received customBuildName " + buildName);
if (buildName == "")
{
throw new Exception("customBuildName argument is missing");
}
return buildName;
}
static string GetFixedBuildPath(BuildTarget buildTarget, string buildPath, string buildName)
{
if (buildTarget.ToString().ToLower().Contains("windows")) {
buildName += ".exe";
} else if (buildTarget == BuildTarget.Android) {
#if UNITY_2018_3_OR_NEWER
buildName += EditorUserBuildSettings.buildAppBundle ? ".aab" : ".apk";
#else
buildName += ".apk";
#endif
}
return buildPath + buildName;
}
static BuildOptions GetBuildOptions()
{
if (TryGetEnv(BUILD_OPTIONS_ENV_VAR, out string envVar)) {
string[] allOptionVars = envVar.Split(',');
BuildOptions allOptions = BuildOptions.None;
BuildOptions option;
string optionVar;
int length = allOptionVars.Length;
Console.WriteLine($":: Detecting {BUILD_OPTIONS_ENV_VAR} env var with {length} elements ({envVar})");
for (int i = 0; i < length; i++) {
optionVar = allOptionVars[i];
if (optionVar.TryConvertToEnum(out option)) {
allOptions |= option;
}
else {
Console.WriteLine($":: Cannot convert {optionVar} to {nameof(BuildOptions)} enum, skipping it.");
}
}
return allOptions;
}
return BuildOptions.None;
}
// https://stackoverflow.com/questions/1082532/how-to-tryparse-for-enum-value
static bool TryConvertToEnum<TEnum>(this string strEnumValue, out TEnum value)
{
if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
{
value = default;
return false;
}
value = (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
return true;
}
static bool TryGetEnv(string key, out string value)
{
value = Environment.GetEnvironmentVariable(key);
return !string.IsNullOrEmpty(value);
}
static void SetScriptingBackendFromEnv(BuildTarget platform) {
var targetGroup = BuildPipeline.GetBuildTargetGroup(platform);
if (TryGetEnv(SCRIPTING_BACKEND_ENV_VAR, out string scriptingBackend)) {
if (scriptingBackend.TryConvertToEnum(out ScriptingImplementation backend)) {
Console.WriteLine($":: Setting ScriptingBackend to {backend}");
PlayerSettings.SetScriptingBackend(targetGroup, backend);
} else {
string possibleValues = string.Join(", ", Enum.GetValues(typeof(ScriptingImplementation)).Cast<ScriptingImplementation>());
throw new Exception($"Could not find '{scriptingBackend}' in ScriptingImplementation enum. Possible values are: {possibleValues}");
}
} else {
var defaultBackend = PlayerSettings.GetDefaultScriptingBackend(targetGroup);
Console.WriteLine($":: Using project's configured ScriptingBackend (should be {defaultBackend} for targetGroup {targetGroup}");
}
}
static void PerformBuild()
{
var buildTarget = GetBuildTarget();
Console.WriteLine(":: Performing build");
if (TryGetEnv(VERSION_NUMBER_VAR, out var bundleVersionNumber))
{
if (buildTarget == BuildTarget.iOS)
{
bundleVersionNumber = GetIosVersion();
}
Console.WriteLine($":: Setting bundleVersionNumber to '{bundleVersionNumber}' (Length: {bundleVersionNumber.Length})");
PlayerSettings.bundleVersion = bundleVersionNumber;
}
if (buildTarget == BuildTarget.Android) {
HandleAndroidAppBundle();
HandleAndroidBundleVersionCode();
HandleAndroidKeystore();
}
var buildPath = GetBuildPath();
var buildName = GetBuildName();
var buildOptions = GetBuildOptions();
var fixedBuildPath = GetFixedBuildPath(buildTarget, buildPath, buildName);
SetScriptingBackendFromEnv(buildTarget);
var buildReport = BuildPipeline.BuildPlayer(GetEnabledScenes(), fixedBuildPath, buildTarget, buildOptions);
if (buildReport.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
throw new Exception($"Build ended with {buildReport.summary.result} status");
Console.WriteLine(":: Done with build");
}
private static void HandleAndroidAppBundle()
{
if (TryGetEnv(ANDROID_APP_BUNDLE, out string value))
{
#if UNITY_2018_3_OR_NEWER
if (bool.TryParse(value, out bool buildAppBundle))
{
EditorUserBuildSettings.buildAppBundle = buildAppBundle;
Console.WriteLine($":: {ANDROID_APP_BUNDLE} env var detected, set buildAppBundle to {value}.");
}
else
{
Console.WriteLine($":: {ANDROID_APP_BUNDLE} env var detected but the value \"{value}\" is not a boolean.");
}
#else
Console.WriteLine($":: {ANDROID_APP_BUNDLE} env var detected but does not work with lower Unity version than 2018.3");
#endif
}
}
private static void HandleAndroidBundleVersionCode()
{
if (TryGetEnv(ANDROID_BUNDLE_VERSION_CODE, out string value))
{
if (int.TryParse(value, out int version))
{
PlayerSettings.Android.bundleVersionCode = version;
Console.WriteLine($":: {ANDROID_BUNDLE_VERSION_CODE} env var detected, set the bundle version code to {value}.");
}
else
Console.WriteLine($":: {ANDROID_BUNDLE_VERSION_CODE} env var detected but the version value \"{value}\" is not an integer.");
}
}
private static string GetIosVersion()
{
if (TryGetEnv(VERSION_iOS, out string value))
{
if (int.TryParse(value, out int version))
{
Console.WriteLine($":: {VERSION_iOS} env var detected, set the version to {value}.");
return version.ToString();
}
else
Console.WriteLine($":: {VERSION_iOS} env var detected but the version value \"{value}\" is not an integer.");
}
throw new ArgumentNullException(nameof(value), $":: Error finding {VERSION_iOS} env var");
}
private static void HandleAndroidKeystore()
{
#if UNITY_2019_1_OR_NEWER
PlayerSettings.Android.useCustomKeystore = false;
#endif
if (!File.Exists(KEYSTORE)) {
Console.WriteLine($":: {KEYSTORE} not found, skipping setup, using Unity's default keystore");
return;
}
PlayerSettings.Android.keystoreName = KEYSTORE;
string keystorePass;
string keystoreAliasPass;
if (TryGetEnv(KEY_ALIAS_NAME, out string keyaliasName)) {
PlayerSettings.Android.keyaliasName = keyaliasName;
Console.WriteLine($":: using ${KEY_ALIAS_NAME} env var on PlayerSettings");
} else {
Console.WriteLine($":: ${KEY_ALIAS_NAME} env var not set, using Project's PlayerSettings");
}
if (!TryGetEnv(KEYSTORE_PASS, out keystorePass)) {
Console.WriteLine($":: ${KEYSTORE_PASS} env var not set, skipping setup, using Unity's default keystore");
return;
}
if (!TryGetEnv(KEY_ALIAS_PASS, out keystoreAliasPass)) {
Console.WriteLine($":: ${KEY_ALIAS_PASS} env var not set, skipping setup, using Unity's default keystore");
return;
}
#if UNITY_2019_1_OR_NEWER
PlayerSettings.Android.useCustomKeystore = true;
#endif
PlayerSettings.Android.keystorePass = keystorePass;
PlayerSettings.Android.keyaliasPass = keystoreAliasPass;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7c1e435333ed4df8b2e136d0be8c6843
timeCreated: 1743490492

View File

@@ -0,0 +1,71 @@
using UnityEditor.Callbacks;
using UnityEditor;
using System.IO;
using UnityEngine;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
public static class BuildPostProcess
{
private const string PLIST_FILE = "Info.plist";
private const string EXIST_ON_SUSPEND_KEY = "UIApplicationExitsOnSuspend";
private const string VERSIONING_SYSTEM_KEY = "VERSIONING_SYSTEM";
private const string CURRENT_PROJECT_VERSION_KEY = "CURRENT_PROJECT_VERSION";
private const string APPLE_GENERIC_VALUE = "apple-generic";
private const string ENABLE_BITCODE_KEY = "ENABLE_BITCODE";
private const string CODE_SIGN_STYLE_KEY = "CODE_SIGN_STYLE";
private const string PROVISIONING_PROFILE_SPECIFIER_KEY = "PROVISIONING_PROFILE_SPECIFIER";
private const string PROVISIONING_PROFILE_KEY = "PROVISIONING_PROFILE";
[PostProcessBuild(1)]
public static void IOSBuildPostProcess(BuildTarget target, string pathToBuiltProject)
{
RemoveDeprecatedInfoPListKeys(pathToBuiltProject);
string projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
var pbxProject = new PBXProject();
pbxProject.ReadFromFile(projectPath);
#if UNITY_2020_1_OR_NEWER
var guidProject = pbxProject.GetUnityMainTargetGuid();
#else
var guidProject = pbxProject.TargetGuidByName(pbxProject.GetUnityMainTargetGuid());
#endif
Debug.Log("Setting Versioning system to Apple Generic...");
pbxProject.SetBuildProperty(guidProject, VERSIONING_SYSTEM_KEY, APPLE_GENERIC_VALUE);
pbxProject.SetBuildProperty(guidProject, CURRENT_PROJECT_VERSION_KEY, "1");
Debug.Log("Disabling bitcode...");
pbxProject.SetBuildProperty(guidProject, ENABLE_BITCODE_KEY, "NO");
Debug.Log("Setting Code sign style to manual and setup provisioning profile specifier...");
pbxProject.SetBuildProperty(guidProject, CODE_SIGN_STYLE_KEY, "Manual");
pbxProject.SetBuildProperty(guidProject, PROVISIONING_PROFILE_SPECIFIER_KEY, pbxProject.GetBuildPropertyForAnyConfig(guidProject, PROVISIONING_PROFILE_KEY));
pbxProject.WriteToFile(projectPath);
}
private static void RemoveDeprecatedInfoPListKeys(string pathToBuiltProject)
{
string plistPath = Path.Combine(pathToBuiltProject, PLIST_FILE);
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
if (rootDict.values.ContainsKey(EXIST_ON_SUSPEND_KEY))
{
Debug.LogFormat("Removing deprecated key \"{0}\" on \"{1}\" file", EXIST_ON_SUSPEND_KEY, PLIST_FILE);
rootDict.values.Remove(EXIST_ON_SUSPEND_KEY);
}
File.WriteAllText(plistPath, plist.WriteToString());
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e84dd163f51449218c7e6680b682e7e3
timeCreated: 1743490492

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0dfec6660d6d5d34c9a5b4ba8a2e3498
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,163 @@
using System.IO;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
namespace YooAsset.Editor
{
public class PackageComparatorWindow : EditorWindow
{
static PackageComparatorWindow _thisInstance;
[MenuItem("Evo/YooAsset/补丁包比对工具", false, 102)]
static void ShowWindow()
{
if (_thisInstance == null)
{
_thisInstance = EditorWindow.GetWindow(typeof(PackageComparatorWindow), false, "补丁包比对工具", true) as PackageComparatorWindow;
_thisInstance.minSize = new Vector2(800, 600);
}
_thisInstance.Show();
}
private string _manifestPath1 = string.Empty;
private string _manifestPath2 = string.Empty;
private readonly List<PackageBundle> _changeList = new List<PackageBundle>();
private readonly List<PackageBundle> _newList = new List<PackageBundle>();
private readonly List<PackageBundle> _deleteList = new List<PackageBundle>();
private Vector2 _scrollPos1;
private Vector2 _scrollPos2;
private Vector2 _scrollPos3;
private void OnGUI()
{
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("选择补丁包1", GUILayout.MaxWidth(150)))
{
string resultPath = EditorUtility.OpenFilePanel("Find", "Assets/", "bytes");
if (string.IsNullOrEmpty(resultPath))
return;
_manifestPath1 = resultPath;
}
EditorGUILayout.LabelField(_manifestPath1);
EditorGUILayout.EndHorizontal();
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("选择补丁包2", GUILayout.MaxWidth(150)))
{
string resultPath = EditorUtility.OpenFilePanel("Find", "Assets/", "bytes");
if (string.IsNullOrEmpty(resultPath))
return;
_manifestPath2 = resultPath;
}
EditorGUILayout.LabelField(_manifestPath2);
EditorGUILayout.EndHorizontal();
if (string.IsNullOrEmpty(_manifestPath1) == false && string.IsNullOrEmpty(_manifestPath2) == false)
{
if (GUILayout.Button("比对差异", GUILayout.MaxWidth(150)))
{
ComparePackage(_changeList, _newList);
}
}
EditorGUILayout.Space();
using (new EditorGUI.DisabledScope(false))
{
int totalCount = _changeList.Count;
EditorGUILayout.Foldout(true, $"差异列表 ( {totalCount} )");
EditorGUI.indentLevel = 1;
_scrollPos1 = EditorGUILayout.BeginScrollView(_scrollPos1);
{
foreach (var bundle in _changeList)
{
EditorGUILayout.LabelField($"{bundle.BundleName} | {(bundle.FileSize / 1024)}K");
}
}
EditorGUILayout.EndScrollView();
EditorGUI.indentLevel = 0;
}
EditorGUILayout.Space();
using (new EditorGUI.DisabledScope(false))
{
int totalCount = _newList.Count;
EditorGUILayout.Foldout(true, $"新增列表 ( {totalCount} )");
EditorGUI.indentLevel = 1;
_scrollPos2 = EditorGUILayout.BeginScrollView(_scrollPos2);
{
foreach (var bundle in _newList)
{
EditorGUILayout.LabelField($"{bundle.BundleName}");
}
}
EditorGUILayout.EndScrollView();
EditorGUI.indentLevel = 0;
}
EditorGUILayout.Space();
using (new EditorGUI.DisabledScope(false))
{
int totalCount = _deleteList.Count;
EditorGUILayout.Foldout(true, $"删除列表 ( {totalCount} )");
EditorGUI.indentLevel = 1;
_scrollPos3 = EditorGUILayout.BeginScrollView(_scrollPos3);
{
foreach (var bundle in _deleteList)
{
EditorGUILayout.LabelField($"{bundle.BundleName}");
}
}
EditorGUILayout.EndScrollView();
EditorGUI.indentLevel = 0;
}
}
private void ComparePackage(List<PackageBundle> changeList, List<PackageBundle> newList)
{
changeList.Clear();
newList.Clear();
// 加载补丁清单1
byte[] bytesData1 = FileUtility.ReadAllBytes(_manifestPath1);
PackageManifest manifest1 = ManifestTools.DeserializeFromBinary(bytesData1);
// 加载补丁清单1
byte[] bytesData2 = FileUtility.ReadAllBytes(_manifestPath2);
PackageManifest manifest2 = ManifestTools.DeserializeFromBinary(bytesData2);
// 拷贝文件列表
foreach (var bundle2 in manifest2.BundleList)
{
if (manifest1.TryGetPackageBundleByBundleName(bundle2.BundleName, out PackageBundle bundle1))
{
if (bundle2.FileHash != bundle1.FileHash)
{
changeList.Add(bundle2);
}
else
{
//检测是否新增
}
}
else
{
newList.Add(bundle2);
}
}
// 按字母重新排序
changeList.Sort((x, y) => string.Compare(x.BundleName, y.BundleName));
newList.Sort((x, y) => string.Compare(x.BundleName, y.BundleName));
Debug.Log("资源包差异比对完成!");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6ff3c700b7f108b48998aa1630a769e1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: