123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using UnityEngine;
- using System.IO;
- namespace GFGGame
- {
- public class SqliteController : SingletonBase<SqliteController>
- {
- /// <summary>
- /// 数据库文件文件夹路径
- /// </summary>
- [HideInInspector]
- public string dirPath;
- public void Init(bool logable, string resPath)
- {
- string dbPath = null;
- if (string.IsNullOrEmpty(dirPath))
- dirPath = $"{Application.persistentDataPath}";
- dbPath = $"{dirPath}/{LauncherConfig.SQL_FILE_NAME}";
- var connectionPath = "data source=" + dbPath;
- #if !UNITY_EDITOR && UNITY_ANDROID
- connectionPath = "uri=file:" + dbPath;
- #endif
- //Debug.Log($"connectionPath {connectionPath}");
- if(resPath != null)
- {
- CheckSqlFile(dbPath, resPath);
- }
- SQLiteHelper.Instance.Init(logable, connectionPath);
- }
- /// <summary>
- /// 检查数据库文件是否存在并且是最新版
- /// </summary>
- private void CheckSqlFile(string dbPath, string resPath)
- {
- #if UNITY_EDITOR
- if(!VEngine.Versions.OfflineMode)
- {
- #endif
- bool needWrite = true;
- var crcKey = "crc" + resPath;
- string crc = "";
- VEngine.ManifestBundle version;
- if (File.Exists(dbPath))
- {
- version = VEngine.Versions.GetBundle(resPath);
- crc = PlayerPrefs.GetString(crcKey, "");
- needWrite = crc != version.crc.ToString();
- }
- if(needWrite)
- {
- var asset = GFGAsset.Load<TextAsset>(resPath);
- byte[] bytes = asset.bytes;
- File.WriteAllBytes(dbPath, bytes);
- GFGAsset.Release(resPath);
- version = VEngine.Versions.GetBundle(resPath);
- PlayerPrefs.SetString(crcKey, version.crc.ToString());
- }
- #if UNITY_EDITOR
- }
- #endif
- }
- }
- }
|