private static void UpdateSQLite3Version(string InPath) { FileInfo fileInfo = new FileInfo(InPath); string dbName = fileInfo.Name; bool needReset = true; SQLite3Data data = Resources.Load <SQLite3Data>("Sqlite3Data"); if (data.AllData != null) { int count = data.AllData.Count; for (int i = 0; i < count; i++) { if (data.AllData[i].Name == dbName) { needReset = false; break; } } } if (needReset) { string dir = fileInfo.DirectoryName; if (!string.IsNullOrEmpty(dir)) { if (dir.IndexOf('\\') != -1) { dir = dir.Replace('\\', '/'); } } string streamPath = Application.streamingAssetsPath; if (streamPath.IndexOf('\\') != -1) { streamPath = streamPath.Replace('\\', '/'); } // ReSharper disable once PossibleNullReferenceException dir = dir == streamPath ? string.Empty : dir.Replace(streamPath + "/", string.Empty); SQLite3SingleData singleData = new SQLite3SingleData { Directory = dir, Name = dbName }; SQLite3Data newData = ScriptableObject.CreateInstance <SQLite3Data>(); if (data.AllData == null) { newData.AllData = new List <SQLite3SingleData>(2) { singleData }; } else { newData.AllData = new List <SQLite3SingleData>(data.AllData.Count + 2); newData.AllData.AddRange(data.AllData); newData.AllData.Add(singleData); } AssetDatabase.CreateAsset(newData, AssetDatabase.GetAssetPath(data)); } }
public void OnPreprocessBuild(BuildTarget InTarget, string InPath) { string streamingAssetsPath = Application.streamingAssetsPath; if (!Directory.Exists(streamingAssetsPath)) { return; } List <FileInfo> fileInfos = FileTools.GetAllFileInfos(streamingAssetsPath); if (null == fileInfos) { return; } List <FileInfo> dbFileInfos = new List <FileInfo>(); int count = fileInfos.Count; for (int i = 0; i < count; i++) { if (fileInfos[i].Extension == ".db") { dbFileInfos.Add(fileInfos[i]); } } SQLite3Data data = Resources.Load <SQLite3Data>("Sqlite3Data"); string dataPath = AssetDatabase.GetAssetPath(data); data = ScriptableObject.CreateInstance <SQLite3Data>(); int dbCount = dbFileInfos.Count; data.AllData = new List <SQLite3SingleData>(dbCount); for (int i = 0; i < dbCount; i++) { SQLite3SingleData singleData = new SQLite3SingleData { Name = dbFileInfos[i].Name, LocalName = MD5Tools.GetStringMd5(dbFileInfos[i].Name), Md5 = MD5Tools.GetFileMd5(dbFileInfos[i].FullName) }; string dirPath = dbFileInfos[i].DirectoryName; singleData.Directory = string.IsNullOrEmpty(dirPath) || dirPath == streamingAssetsPath ? string.Empty : dirPath.Replace('\\', '/').Replace(streamingAssetsPath, string.Empty); data.AllData.Add(singleData); } AssetDatabase.CreateAsset(data, dataPath); AssetDatabase.SaveAssets(); }
/// <summary> /// If there is no database in the persistentDataPath directory, or database in the persistentDataPath directory md5 not match original, /// Then copy the database from the streamingAssetsPath directory to the persistentDataPath directory and open the database in read-only mode /// </summary> /// <param name="InDbName">The name of the SQLite3 database.</param> /// <returns>Operation SQLite3 database handle.</returns> public static SQLite3Operate OpenToRead(string InDbName) { SQLite3Data data = Resources.Load <SQLite3Data>("Sqlite3Data"); if (null == data || null == data.AllData) { throw new Exception("Not found sqlite3 data file in '/SQLite3Helper/Resources/Sqlite3Data.asset'"); } int count = data.AllData.Count; SQLite3SingleData singleData = null; for (int i = 0; i < count; i++) { if (data.AllData[i].Name == InDbName) { singleData = data.AllData[i]; break; } } if (singleData == null) { throw new Exception("Not found sqlite3 data named + " + InDbName); } #if UNITY_EDITOR string dbPath = Application.streamingAssetsPath; if (!string.IsNullOrEmpty(singleData.Directory)) { dbPath = Path.Combine(dbPath, singleData.Directory); } dbPath = Path.Combine(dbPath, singleData.Name); if (!File.Exists(dbPath)) { throw new Exception("Not found sqlite3 file named + " + InDbName); } #else string dbPath = string.Format("{0}/{1}/", Application.persistentDataPath, singleData.Directory); if (!Directory.Exists(dbPath)) { Directory.CreateDirectory(dbPath); } dbPath = string.Format("{0}{1}.png", dbPath, singleData.LocalName); Debug.LogError(dbPath); bool needUpdate = true; if (File.Exists(dbPath)) { if (Szn.Framework.UtilPackage.MD5Tools.GetFileMd5(dbPath) == singleData.Md5) { needUpdate = false; } } if (needUpdate) { #if UNITY_ANDROID string streamPath = string.IsNullOrEmpty(singleData.Directory) ? string.Format("jar:file://{0}!/assets/{1}", Application.dataPath, singleData.Name) : string.Format("jar:file://{0}!/assets{1}/{2}", Application.dataPath, singleData.Directory, singleData.Name); using (WWW www = new WWW(streamPath)) { while (!www.isDone) { } if (string.IsNullOrEmpty(www.error)) { File.WriteAllBytes(dbPath, www.bytes); } else { Debug.LogError("www error " + www.error); } } #elif UNITY_IOS string streamPath = string.IsNullOrEmpty(singleData.Directory) ? string.Format("{0}/{1}", Application.streamingAssetsPath, singleData.Name) : string.Format("{0}{1}/{2}", Application.streamingAssetsPath, singleData.Directory, singleData.Name); File.Copy(streamPath, dbPath, true); #endif } #endif return(new SQLite3Operate(dbPath, SQLite3OpenFlags.ReadOnly)); }