123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace YooAsset
- {
- internal static class CacheSystem
- {
- private readonly static Dictionary<string, PatchBundle> _cachedDic = new Dictionary<string, PatchBundle>(1000);
-
-
-
- public static EVerifyLevel InitVerifyLevel { private set; get; }
- public static void Initialize(EVerifyLevel initVerifyLevel)
- {
- InitVerifyLevel = initVerifyLevel;
- }
- public static void DestroyAll()
- {
- _cachedDic.Clear();
- }
-
-
-
-
- public static bool IsCached(PatchBundle patchBundle)
- {
- string fileHash = patchBundle.FileHash;
- if (_cachedDic.ContainsKey(fileHash))
- {
- string filePath = patchBundle.CachedFilePath;
- if (File.Exists(filePath))
- {
- return true;
- }
- else
- {
- _cachedDic.Remove(fileHash);
- YooLogger.Error($"Cache file is missing : {filePath}");
- return false;
- }
- }
- else
- {
- return false;
- }
- }
-
-
-
- public static void CacheBundle(PatchBundle patchBundle)
- {
- string fileHash = patchBundle.FileHash;
- if (_cachedDic.ContainsKey(fileHash) == false)
- {
- string filePath = patchBundle.CachedFilePath;
- YooLogger.Log($"Cache verify file : {filePath}");
- _cachedDic.Add(fileHash, patchBundle);
- }
- }
-
-
-
- public static EVerifyResult VerifyBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
- {
- return VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
- }
-
-
-
- public static EVerifyResult VerifyAndCacheBundle(PatchBundle patchBundle, EVerifyLevel verifyLevel)
- {
- var verifyResult = VerifyContentInternal(patchBundle.CachedFilePath, patchBundle.FileSize, patchBundle.FileCRC, verifyLevel);
- if (verifyResult == EVerifyResult.Succeed)
- CacheBundle(patchBundle);
- return verifyResult;
- }
-
-
-
- public static EVerifyResult VerifyContentInternal(string filePath, long fileSize, string fileCRC, EVerifyLevel verifyLevel)
- {
- try
- {
- if (File.Exists(filePath) == false)
- return EVerifyResult.FileNotExisted;
-
- long size = FileUtility.GetFileSize(filePath);
- if (size < fileSize)
- return EVerifyResult.FileNotComplete;
- else if (size > fileSize)
- return EVerifyResult.FileOverflow;
-
- if (verifyLevel == EVerifyLevel.High)
- {
- string crc = HashUtility.FileCRC32(filePath);
- if (crc == fileCRC)
- return EVerifyResult.Succeed;
- else
- return EVerifyResult.FileCrcError;
- }
- else
- {
- return EVerifyResult.Succeed;
- }
- }
- catch (Exception)
- {
- return EVerifyResult.Exception;
- }
- }
- }
- }
|