using System; using System.Collections.Generic; using System.IO; using Disk; using OpenMetaverse; using SearchOption = System.IO.SearchOption; // on every boot, as soon as possible, // copy any missing/changed items in streaming assets to the data path in user-accessible-space // CopyStreamingAssetsToPersistentDataPath namespace UnityScripts.Disk { public class StaticFilesCopier { private StaticFilesCopier() { } private static StaticFilesCopier _instance; public static StaticFilesCopier GetInstance() { if (_instance == null) { _instance = new StaticFilesCopier(); } return _instance; } public bool CopyIsDoneAndNoErrors = false; // do copy (scan the files in device, copy if not same as staticassets.). public int Work() { BetterStreamingAssets.Initialize(); //fuck, this is easy to forget. int res = CheckOmvDataFolderAndUpdateItIfNecessary(); if (res != -1) CopyIsDoneAndNoErrors = true; return res; } //returns -1 if failed. private int CheckOmvDataFolderAndUpdateItIfNecessary() { List updatedFiles = null; try { updatedFiles = CopyIfRequired_StreamingAssetsFolder( DirectoryHelpers.GetInternalStorageDir() ); } catch (Exception e) { return -1; } if (updatedFiles.Count > 0) { var printString = String.Join(Environment.NewLine, updatedFiles); OpenMetaverse.Logger.Log( "These files were copied from staticAssets : " + Environment.NewLine + printString, Helpers.LogLevel.Info ); } else { OpenMetaverse.Logger.Log( "staticAssets are already latest version in : " + DirectoryHelpers.GetInternalStorageDir(), Helpers.LogLevel.Info ); } return 0; } // scan all files in streamingAssets folder, and copy any changed/missing files into userDataRoot. // return some files that were updated. private List CopyIfRequired_StreamingAssetsFolder(string appRootPath) { List updatedFiles = new List(); //everything in StreamingAssets\ string slash = Path.DirectorySeparatorChar.ToString(); if (!BetterStreamingAssets.DirectoryExists(slash)) { throw new Exception("StreamingAssets root not accessible."); } string[] paths = BetterStreamingAssets.GetFiles(slash, "*", SearchOption.AllDirectories); if (paths.Length == 0) { throw new Exception("StreamingAssets has no files in it."); } foreach (var sourcePath in paths) { string relativePath = ""; //find the equivalent path in appRootPath string root = Directory.GetDirectoryRoot(sourcePath); if (root != null/*todo: check for failed get root.*/) { // gotta remove that root slash character '\' relativePath = sourcePath.Substring(root.Length - 1); } //1. check for missing file string targetPath = Path.Combine(appRootPath, relativePath); if (!File.Exists(targetPath)) { updatedFiles.Add(targetPath); DirectoryHelpers.WriteToFile( BetterStreamingAssets.ReadAllBytes(sourcePath), targetPath); } //2. check for diffs in the file. bool fileIsDifferent = false; using (Stream fs1 = File.OpenRead(targetPath)) using (Stream fs2 = BetterStreamingAssets.OpenRead(sourcePath)) { if (FilesStreamsAreSame(fs1, fs2)) { //do nothing } else { fileIsDifferent = true; } } if (fileIsDifferent) { updatedFiles.Add(targetPath); DirectoryHelpers.WriteToFile( BetterStreamingAssets.ReadAllBytes(sourcePath), targetPath); } } return updatedFiles; } // true if same data in both files. // false if either source or dest file is missing private bool FilesStreamsAreSame(Stream fs1, Stream fs2) { return FileHelpers.FileStreamsAreEqual(fs1, fs2); } } }