From 13a6024644d111a2d2d5e3ad8c8e0066b3779fbb Mon Sep 17 00:00:00 2001 From: alexiscatnip Date: Thu, 27 Jan 2022 23:18:30 +0800 Subject: [PATCH] A really retarded design pattern to copy items from streaming assets to local storage... --- ...CopyStreamingAssetsToPersistentDataPath.cs | 13 --- ...CopyStreamingAssetsToPersistentDataPath.cs | 104 ++++++++++++++++++ ...reamingAssetsToPersistentDataPath.cs.meta} | 0 3 files changed, 104 insertions(+), 13 deletions(-) delete mode 100644 Assets/AndroidCopyStreamingAssetsToPersistentDataPath.cs create mode 100644 Assets/scripts/Disk/CopyStreamingAssetsToPersistentDataPath.cs rename Assets/{AndroidCopyStreamingAssetsToPersistentDataPath.cs.meta => scripts/Disk/CopyStreamingAssetsToPersistentDataPath.cs.meta} (100%) diff --git a/Assets/AndroidCopyStreamingAssetsToPersistentDataPath.cs b/Assets/AndroidCopyStreamingAssetsToPersistentDataPath.cs deleted file mode 100644 index 3a16815..0000000 --- a/Assets/AndroidCopyStreamingAssetsToPersistentDataPath.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class AndroidCopyStreamingAssetsToPersistentDataPath : MonoBehaviour -{ - - [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] - public void init() - { - - } -} diff --git a/Assets/scripts/Disk/CopyStreamingAssetsToPersistentDataPath.cs b/Assets/scripts/Disk/CopyStreamingAssetsToPersistentDataPath.cs new file mode 100644 index 0000000..eed196d --- /dev/null +++ b/Assets/scripts/Disk/CopyStreamingAssetsToPersistentDataPath.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.IO; +using OpenMetaverse.ImportExport.Collada14; +using UnityEngine; +using UnityEngine.Serialization; + +// on every boot, as soon as possible, +// copy any missing/changed items in streaming assets to the data path in user-accessible-space +namespace Disk +{ + public class CopyStreamingAssetsToPersistentDataPath : MonoBehaviour + { + [FormerlySerializedAs("doneCopy")] public bool copyIsDone = false; + + private void Awake() + { + BetterStreamingAssets.Initialize(); //fuck, this is easy to forget. + + CheckOmvDataFolderAndUpdateItIfNecessary(); + copyIsDone = true; + } + + private void CheckOmvDataFolderAndUpdateItIfNecessary() + { + List updatedFiles = ScanAndCopy_StreamingAssetsFolder( + DirectoryHelpers.GetInternalCacheDir() + ); + + if (updatedFiles.Count > 0) + { + var printString = String.Join(Environment.NewLine, updatedFiles); + Debug.Log("there were some updates to staticAssets in : " + + Environment.NewLine + + printString); + } + else + { + Debug.Log("staticAssets are already latest version in : " + + DirectoryHelpers.GetInternalCacheDir()); + } + + } + + // scan all files in streamingAssets folder, and copy any changed/missing files into userDataRoot. + // return true if some file was updated. + private List ScanAndCopy_StreamingAssetsFolder(string appRootPath) + { + List updatedFiles = new List(); + // bool someFileWasUpdated = false; + + //everything in StreamingAssets\ + Debug.Assert( BetterStreamingAssets.DirectoryExists("\\") ); + string[] paths = BetterStreamingAssets.GetFiles("\\", "*", SearchOption.AllDirectories); + + foreach (var sourcePath in paths) + { + string relativePath = ""; + + //find the equivalent path in appRootPath + string root = Directory.GetDirectoryRoot(sourcePath); // this is \StreamingAssets\, which we wish to remove. + if (root != null/*todo: check for failed get root.*/) + { + relativePath = sourcePath.Substring(root.Length - 1); // gotta remove that \ + } + + string targetPath = Path.Combine(appRootPath, relativePath); + if (!File.Exists(targetPath)) + { + updatedFiles.Add(targetPath); + DirectoryHelpers.WriteToFile( + BetterStreamingAssets.ReadAllBytes(sourcePath), + targetPath); + } + + using (Stream fs1 = File.OpenRead(targetPath)) + using (Stream fs2 = BetterStreamingAssets.OpenRead(sourcePath)) + { + if (FilesStreamsAreSame(fs1, fs2)) + { + //do nothing + } + else + { + 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); + } + } +} diff --git a/Assets/AndroidCopyStreamingAssetsToPersistentDataPath.cs.meta b/Assets/scripts/Disk/CopyStreamingAssetsToPersistentDataPath.cs.meta similarity index 100% rename from Assets/AndroidCopyStreamingAssetsToPersistentDataPath.cs.meta rename to Assets/scripts/Disk/CopyStreamingAssetsToPersistentDataPath.cs.meta