SqliteController.cs 2.4 KB

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