mirror of
				https://github.com/Nioux/AideDeJeu.git
				synced 2025-10-31 15:36:07 +00:00 
			
		
		
		
	Versioning db Android
This commit is contained in:
		
							parent
							
								
									9d83e524cd
								
							
						
					
					
						commit
						a7d83aa6f0
					
				
					 3 changed files with 54 additions and 27 deletions
				
			
		|  | @ -109,6 +109,9 @@ | |||
|     <AndroidAsset Include="..\..\Data\library.db"> | ||||
|       <Link>Assets\library.db</Link> | ||||
|     </AndroidAsset> | ||||
|     <AndroidAsset Include="..\..\Data\library.ver"> | ||||
|       <Link>Assets\library.ver</Link> | ||||
|     </AndroidAsset> | ||||
|     <None Include="Resources\AboutResources.txt" /> | ||||
|     <None Include="Assets\AboutAssets.txt" /> | ||||
|   </ItemGroup> | ||||
|  |  | |||
|  | @ -1,6 +1,5 @@ | |||
| using AideDeJeu.Tools; | ||||
| using Android.Content.PM; | ||||
| using System; | ||||
| using System.IO; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
|  | @ -30,36 +29,60 @@ namespace AideDeJeu.Droid | |||
| 
 | ||||
|         public async Task<string> GetDatabasePathAsync(string databaseName) | ||||
|         { | ||||
|             //string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); | ||||
|             //string dbPath = Path.Combine(path, databaseName); | ||||
|             //return dbPath; | ||||
|             var documentsDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); | ||||
|             var path = Path.Combine(documentsDirectoryPath, $"{databaseName}.db"); | ||||
|             if (await CheckDatabaseVersionAsync(databaseName)) | ||||
|             { | ||||
|                 await CopyOldToNewFileAsync(databaseName, "db"); | ||||
|                 await CopyOldToNewFileAsync(databaseName, "ver"); | ||||
|             } | ||||
|             return GetNewFilePath(databaseName, "db"); | ||||
|         } | ||||
| 
 | ||||
|             // This is where we copy in our pre-created database | ||||
|             if (!File.Exists(path)) | ||||
|         public Stream GetOldFileStream(string fileName, string extension) | ||||
|         { | ||||
|                 using (var inStream = Android.App.Application.Context.Assets.Open($"{databaseName}.db")) | ||||
|             return Android.App.Application.Context.Assets.Open($"{fileName}.{extension}"); | ||||
|         } | ||||
|         public string GetNewFilePath(string fileName, string extension) | ||||
|         { | ||||
|                     using (var outStream = new FileStream(path, FileMode.Create)) | ||||
|             var documentsDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); | ||||
|             return Path.Combine(documentsDirectoryPath, $"{fileName}.{extension}"); | ||||
|         } | ||||
|         public async Task CopyOldToNewFileAsync(string fileName, string extension) | ||||
|         { | ||||
|             using (var inStream = GetOldFileStream(fileName, extension)) | ||||
|             { | ||||
|                 using (var outStream = new FileStream(GetNewFilePath(fileName, extension), FileMode.Create)) | ||||
|                 { | ||||
|                     await inStream.CopyToAsync(outStream); | ||||
|                 } | ||||
|             } | ||||
|                 //using (var binaryReader = new BinaryReader(Android.App.Application.Context.Assets.Open(databaseName))) | ||||
|                 //{ | ||||
|                 //    using (var binaryWriter = new BinaryWriter(new FileStream(path, FileMode.Create))) | ||||
|                 //    { | ||||
|                 //        byte[] buffer = new byte[2048]; | ||||
|                 //        int length = 0; | ||||
|                 //        while ((length = binaryReader.Read(buffer, 0, buffer.Length)) > 0) | ||||
|                 //        { | ||||
|                 //            binaryWriter.Write(buffer, 0, length); | ||||
|                 //        } | ||||
|                 //    } | ||||
|                 //} | ||||
|         } | ||||
|             return path; | ||||
| 
 | ||||
|         public async Task<bool> CheckDatabaseVersionAsync(string databaseName) | ||||
|         { | ||||
|             var path = GetNewFilePath(databaseName, "ver"); | ||||
|             if (!File.Exists(path)) | ||||
|             { | ||||
|                 return true; | ||||
|             } | ||||
|             int newVersion = 0; | ||||
|             int oldVersion = -1; | ||||
|             using (var newStream = GetOldFileStream(databaseName, "ver")) | ||||
|             { | ||||
|                 using (var sr = new StreamReader(newStream)) | ||||
|                 { | ||||
|                     var str = await sr.ReadToEndAsync(); | ||||
|                     int.TryParse(str, out newVersion); | ||||
|                 } | ||||
|             } | ||||
|             using (var oldStream = new FileStream(path, FileMode.Open)) | ||||
|             { | ||||
|                 using (var sr = new StreamReader(oldStream)) | ||||
|                 { | ||||
|                     var str = await sr.ReadToEndAsync(); | ||||
|                     int.TryParse(str, out oldVersion); | ||||
|                 } | ||||
|             } | ||||
|             return newVersion > oldVersion; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								Data/library.ver
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Data/library.ver
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| 4 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Yan Maniez
						Yan Maniez