SqliteController.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using UnityEngine;
  2. using System.IO;
  3. namespace GFGGame
  4. {
  5. public class SqliteController : SingletonBase<SqliteController>
  6. {
  7. /// <summary>
  8. /// 数据库文件文件夹路径
  9. /// </summary>
  10. [HideInInspector]
  11. public string dirPath;
  12. public void Init(bool logable, string resPath)
  13. {
  14. string dbPath = null;
  15. if (string.IsNullOrEmpty(dirPath))
  16. dirPath = $"{Application.persistentDataPath}";
  17. dbPath = $"{dirPath}/{LauncherConfig.SQL_FILE_NAME}";
  18. var connectionPath = "data source=" + dbPath;
  19. #if !UNITY_EDITOR && UNITY_ANDROID
  20. connectionPath = "uri=file:" + dbPath;
  21. #endif
  22. //Debug.Log($"connectionPath {connectionPath}");
  23. if(resPath != null)
  24. {
  25. CheckSqlFile(dbPath, resPath);
  26. }
  27. SQLiteHelper.Instance.Init(logable, connectionPath);
  28. }
  29. /// <summary>
  30. /// 检查数据库文件是否存在并且是最新版
  31. /// </summary>
  32. private void CheckSqlFile(string dbPath, string resPath)
  33. {
  34. #if UNITY_EDITOR
  35. if(!VEngine.Versions.OfflineMode)
  36. {
  37. #endif
  38. bool needWrite = true;
  39. var crcKey = "crc" + resPath;
  40. string crc = "";
  41. VEngine.ManifestBundle version;
  42. if (File.Exists(dbPath))
  43. {
  44. version = VEngine.Versions.GetBundle(resPath);
  45. crc = PlayerPrefs.GetString(crcKey, "");
  46. needWrite = crc != version.crc.ToString();
  47. }
  48. if(needWrite)
  49. {
  50. var asset = GFGAsset.Load<TextAsset>(resPath);
  51. byte[] bytes = asset.bytes;
  52. File.WriteAllBytes(dbPath, bytes);
  53. GFGAsset.Release(resPath);
  54. version = VEngine.Versions.GetBundle(resPath);
  55. PlayerPrefs.SetString(crcKey, version.crc.ToString());
  56. }
  57. #if UNITY_EDITOR
  58. }
  59. #endif
  60. }
  61. }
  62. }