update sapce shooter

pull/326/head
何冠峰 2024-07-04 20:49:18 +08:00
parent b82ede8bde
commit d43eb821b9
17 changed files with 28 additions and 278 deletions

View File

@ -14,7 +14,7 @@ public class FileOffsetEncryption : IEncryptionServices
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
int offset = 32;
byte[] fileData = File.ReadAllBytes(fileInfo.FilePath);
byte[] fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
var encryptedData = new byte[fileData.Length + offset];
Buffer.BlockCopy(fileData, 0, encryptedData, offset, fileData.Length);
@ -41,7 +41,7 @@ public class FileStreamEncryption : IEncryptionServices
{
if (fileInfo.BundleName.Contains("_gameres_audio"))
{
var fileData = File.ReadAllBytes(fileInfo.FilePath);
var fileData = File.ReadAllBytes(fileInfo.FileLoadPath);
for (int i = 0; i < fileData.Length; i++)
{
fileData[i] ^= BundleStream.KEY;

View File

@ -1,15 +0,0 @@
using System;
using UnityEngine;
using YooAsset;
public class ApplicationQuit : MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
private void OnApplicationQuit()
{
YooAssets.Destroy();
}
}

View File

@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: 3cbcea01819f3d94ba5162624ad9c75f
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -59,7 +59,7 @@ internal class SceneBattle : MonoBehaviour
if (YooAssets.Initialized)
{
var package = YooAssets.GetPackage("DefaultPackage");
package.UnloadUnusedAssets();
package.UnloadUnusedAssetsAsync();
}
}
private void Update()

View File

@ -12,7 +12,7 @@ public class SceneHome : MonoBehaviour
private IEnumerator Start()
{
// 加载登录页面
_windowHandle = YooAssets.LoadAssetAsync<GameObject>("UIHome");
_windowHandle = YooAssets.LoadAssetSync<GameObject>("UIHome");
yield return _windowHandle;
_windowHandle.InstantiateSync(CanvasDesktop.transform);
}
@ -28,7 +28,7 @@ public class SceneHome : MonoBehaviour
if (YooAssets.Initialized)
{
var package = YooAssets.GetPackage("DefaultPackage");
package.UnloadUnusedAssets();
package.UnloadUnusedAssetsAsync();
}
}
}

View File

@ -20,7 +20,7 @@ internal class FsmClearPackageCache : IStateNode
PatchEventDefine.PatchStatesChange.SendEventMessage("清理未使用的缓存文件!");
var packageName = (string)_machine.GetBlackboardValue("PackageName");
var package = YooAssets.GetPackage(packageName);
var operation = package.ClearUnusedCacheFilesAsync();
var operation = package.ClearUnusedBundleFilesAsync();
operation.Completed += Operation_Completed;
}
void IStateNode.OnUpdate()

View File

@ -44,8 +44,9 @@ internal class FsmInitializePackage : IStateNode
InitializationOperation initializationOperation = null;
if (playMode == EPlayMode.EditorSimulateMode)
{
string simulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(buildPipeline, packageName);
var createParameters = new EditorSimulateModeParameters();
createParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(buildPipeline, packageName);
createParameters.EditorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(simulateManifestFilePath);
initializationOperation = package.InitializeAsync(createParameters);
}
@ -53,7 +54,7 @@ internal class FsmInitializePackage : IStateNode
if (playMode == EPlayMode.OfflinePlayMode)
{
var createParameters = new OfflinePlayModeParameters();
createParameters.DecryptionServices = new FileStreamDecryption();
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
initializationOperation = package.InitializeAsync(createParameters);
}
@ -62,10 +63,10 @@ internal class FsmInitializePackage : IStateNode
{
string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL();
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
var createParameters = new HostPlayModeParameters();
createParameters.DecryptionServices = new FileStreamDecryption();
createParameters.BuildinQueryServices = new GameQueryServices();
createParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
createParameters.CacheFileSystemParameters = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
initializationOperation = package.InitializeAsync(createParameters);
}
@ -74,10 +75,9 @@ internal class FsmInitializePackage : IStateNode
{
string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL();
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
var createParameters = new WebPlayModeParameters();
createParameters.DecryptionServices = new FileStreamDecryption();
createParameters.BuildinQueryServices = new GameQueryServices();
createParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
createParameters.WebFileSystemParameters = FileSystemParameters.CreateDefaultWebFileSystemParameters(remoteServices);
initializationOperation = package.InitializeAsync(createParameters);
}
@ -91,8 +91,6 @@ internal class FsmInitializePackage : IStateNode
}
else
{
var version = initializationOperation.PackageVersion;
Debug.Log($"Init resource package version : {version}");
_machine.ChangeState<FsmUpdatePackageVersion>();
}
}
@ -163,7 +161,7 @@ internal class FsmInitializePackage : IStateNode
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
managedStream = bundleStream;
return AssetBundle.LoadFromStream(bundleStream, fileInfo.ConentCRC, GetManagedReadBufferSize());
return AssetBundle.LoadFromStream(bundleStream, fileInfo.FileLoadCRC, GetManagedReadBufferSize());
}
/// <summary>
@ -174,7 +172,7 @@ internal class FsmInitializePackage : IStateNode
{
BundleStream bundleStream = new BundleStream(fileInfo.FileLoadPath, FileMode.Open, FileAccess.Read, FileShare.Read);
managedStream = bundleStream;
return AssetBundle.LoadFromStreamAsync(bundleStream, fileInfo.ConentCRC, GetManagedReadBufferSize());
return AssetBundle.LoadFromStreamAsync(bundleStream, fileInfo.FileLoadCRC, GetManagedReadBufferSize());
}
private static uint GetManagedReadBufferSize()
@ -195,7 +193,7 @@ internal class FsmInitializePackage : IStateNode
AssetBundle IDecryptionServices.LoadAssetBundle(DecryptFileInfo fileInfo, out Stream managedStream)
{
managedStream = null;
return AssetBundle.LoadFromFile(fileInfo.FileLoadPath, fileInfo.ConentCRC, GetFileOffset());
return AssetBundle.LoadFromFile(fileInfo.FileLoadPath, fileInfo.FileLoadCRC, GetFileOffset());
}
/// <summary>
@ -205,7 +203,7 @@ internal class FsmInitializePackage : IStateNode
AssetBundleCreateRequest IDecryptionServices.LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream)
{
managedStream = null;
return AssetBundle.LoadFromFileAsync(fileInfo.FileLoadPath, fileInfo.ConentCRC, GetFileOffset());
return AssetBundle.LoadFromFileAsync(fileInfo.FileLoadPath, fileInfo.FileLoadCRC, GetFileOffset());
}
private static ulong GetFileOffset()

View File

@ -34,8 +34,7 @@ public class FsmUpdatePackageManifest : IStateNode
var packageName = (string)_machine.GetBlackboardValue("PackageName");
var packageVersion = (string)_machine.GetBlackboardValue("PackageVersion");
var package = YooAssets.GetPackage(packageName);
bool savePackageVersion = true;
var operation = package.UpdatePackageManifestAsync(packageVersion, savePackageVersion);
var operation = package.UpdatePackageManifestAsync(packageVersion);
yield return operation;
if (operation.Status != EOperationStatus.Succeed)

View File

@ -33,7 +33,7 @@ internal class FsmUpdatePackageVersion : IStateNode
var packageName = (string)_machine.GetBlackboardValue("PackageName");
var package = YooAssets.GetPackage(packageName);
var operation = package.UpdatePackageVersionAsync();
var operation = package.RequestPackageVersionAsync();
yield return operation;
if (operation.Status != EOperationStatus.Succeed)

View File

@ -100,3 +100,11 @@ MonoBehaviour:
FilterRuleName: CollectAll
AssetTags:
UserData:
- CollectPath: Assets/Samples/Space Shooter/GameRes/UIAtlas
CollectorGUID: 80caf8bc3d0ac5b48a5cab7765a68205
CollectorType: 0
AddressRuleName: AddressByFileName
PackRuleName: PackDirectory
FilterRuleName: CollectAll
AssetTags:
UserData:

View File

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

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 内置资源清单
/// </summary>
public class BuildinFileManifest : ScriptableObject
{
[Serializable]
public class Element
{
public string PackageName;
public string FileName;
public string FileCRC32;
}
public List<Element> BuildinFiles = new List<Element>();
}

View File

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

View File

@ -1,8 +0,0 @@

public class StreamingAssetsDefine
{
/// <summary>
/// 根目录名称保持和YooAssets资源系统一致
/// </summary>
public const string RootFolderName = "yoo";
}

View File

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

View File

@ -1,162 +0,0 @@
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
/// <summary>
/// 资源文件查询服务类
/// </summary>
public class GameQueryServices : IBuildinQueryServices
{
/// <summary>
/// 查询内置文件的时候,是否比对文件哈希值
/// </summary>
public static bool CompareFileCRC = false;
public bool Query(string packageName, string fileName, string fileCRC)
{
// 注意fileName包含文件格式
return StreamingAssetsHelper.FileExists(packageName, fileName, fileCRC);
}
}
#if UNITY_EDITOR
public sealed class StreamingAssetsHelper
{
public static void Init() { }
public static bool FileExists(string packageName, string fileName, string fileCRC)
{
string filePath = Path.Combine(Application.streamingAssetsPath, StreamingAssetsDefine.RootFolderName, packageName, fileName);
if (File.Exists(filePath))
{
if (GameQueryServices.CompareFileCRC)
{
string crc32 = YooAsset.HashUtility.FileCRC32(filePath);
return crc32 == fileCRC;
}
else
{
return true;
}
}
else
{
return false;
}
}
}
#else
public sealed class StreamingAssetsHelper
{
private class PackageQuery
{
public readonly Dictionary<string, BuildinFileManifest.Element> Elements = new Dictionary<string, BuildinFileManifest.Element>(1000);
}
private static bool _isInit = false;
private static readonly Dictionary<string, PackageQuery> _packages = new Dictionary<string, PackageQuery>(10);
/// <summary>
/// 初始化
/// </summary>
public static void Init()
{
if (_isInit == false)
{
_isInit = true;
var manifest = Resources.Load<BuildinFileManifest>("BuildinFileManifest");
if (manifest != null)
{
foreach (var element in manifest.BuildinFiles)
{
if (_packages.TryGetValue(element.PackageName, out PackageQuery package) == false)
{
package = new PackageQuery();
_packages.Add(element.PackageName, package);
}
package.Elements.Add(element.FileName, element);
}
}
}
}
/// <summary>
/// 内置文件查询方法
/// </summary>
public static bool FileExists(string packageName, string fileName, string fileCRC32)
{
if (_isInit == false)
Init();
if (_packages.TryGetValue(packageName, out PackageQuery package) == false)
return false;
if (package.Elements.TryGetValue(fileName, out var element) == false)
return false;
if (GameQueryServices.CompareFileCRC)
{
return element.FileCRC32 == fileCRC32;
}
else
{
return true;
}
}
}
#endif
#if UNITY_EDITOR
internal class PreprocessBuild : UnityEditor.Build.IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
/// <summary>
/// 在构建应用程序前处理
/// 原理在构建APP之前搜索StreamingAssets目录下的所有资源文件然后将这些文件信息写入内置清单内置清单存储在Resources文件夹下。
/// </summary>
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
string saveFilePath = "Assets/Resources/BuildinFileManifest.asset";
if (File.Exists(saveFilePath))
{
File.Delete(saveFilePath);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
}
string folderPath = $"{Application.dataPath}/StreamingAssets/{StreamingAssetsDefine.RootFolderName}";
DirectoryInfo root = new DirectoryInfo(folderPath);
if (root.Exists == false)
{
Debug.LogWarning($"没有发现YooAsset内置目录 : {folderPath}");
return;
}
var manifest = ScriptableObject.CreateInstance<BuildinFileManifest>();
FileInfo[] files = root.GetFiles("*", SearchOption.AllDirectories);
foreach (var fileInfo in files)
{
if (fileInfo.Extension == ".meta")
continue;
if (fileInfo.Name.StartsWith("PackageManifest_"))
continue;
BuildinFileManifest.Element element = new BuildinFileManifest.Element();
element.PackageName = fileInfo.Directory.Name;
element.FileCRC32 = YooAsset.HashUtility.FileCRC32(fileInfo.FullName);
element.FileName = fileInfo.Name;
manifest.BuildinFiles.Add(element);
}
if (Directory.Exists("Assets/Resources") == false)
Directory.CreateDirectory("Assets/Resources");
UnityEditor.AssetDatabase.CreateAsset(manifest, saveFilePath);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
Debug.Log($"一共{manifest.BuildinFiles.Count}个内置文件,内置资源清单保存成功 : {saveFilePath}");
}
}
#endif

View File

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