using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Threading.Tasks; using Ubisoft.Hotel.Device; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using static Ubisoft.Hotel.Device.StorageManager; public class DeviceTestSceneController : MonoBehaviour { #pragma warning disable IDE1006 public Dropdown fileDropdown; // To select the files to read public InputField fileInput; // To enter the path of the files to write public Text logsText; #pragma warning restore IDE1006 public static DeviceTestSceneController Instance { get; set; } private static readonly int s_maxCharsDropdown = 30; private static int s_logFileId; private static List s_filesAvailableFullPaths = new List(); private static List s_filesAvailableRelativePaths = new List(); private readonly List m_filesInAssets = new List { "small_image.png", "big_image.png", "small_pdf.pdf", "big_pdf.pdf" }; #region MonoBehaviour void Awake() { if (Instance == null) { Instance = this; } } void Start() { s_filesAvailableRelativePaths.AddRange(m_filesInAssets); #if UNITY_ANDROID && !UNITY_EDITOR StartCoroutine(InitAndroid()); #else foreach (string file in m_filesInAssets) { s_filesAvailableFullPaths.Add(Path.Combine(Application.streamingAssetsPath, file)); } AddDropdownOptions(s_filesAvailableFullPaths); #endif } #endregion #region UI Methods public void OnClickFreeDiskSpace() { Log($"Free disk space: {StorageManager.GetFreeDiskSpace()}"); } #pragma warning disable CS4014 public void OnClickMultipleFilesTest() { // Read the 4 test files, 10 times each for (int i = 0; i < 4; i++) { for (int j = 1; j < 11; j++) { ReadFileAsync(s_filesAvailableFullPaths[i]); } } // Write 10 files string filename; for (int i = 1; i < 11; i++) { filename = StorageManager.GetAbsolutePath("out_" + i + ".txt"); WriteFileAsync(filename); } } public void OnClickMultipleFilesTestWithDescriptor() { // Read the 4 test files, 10 times each for (int i = 0; i < 4; i++) { for (int j = 1; j < 11; j++) { ReadFileAsync(new FileDescriptor(s_filesAvailableFullPaths[i])); } } // Write 10 files not versioned for (int i = 1; i < 11; i++) { WriteFileAsync(new FileDescriptor("out" + i + ".txt", false)); } // Write 10 files versioned for (int i = 1; i < 11; i++) { WriteFileAsync(new FileDescriptor("outvers" + i + ".txt", true)); } } public void OnClickReadFile() { string selectedFileName = s_filesAvailableFullPaths[fileDropdown.value]; ReadFileAsync(selectedFileName); } public void OnClickReadFileWithDescriptor(bool isVersioned) { FileDescriptor selectedFile; #if !UNITY_ANDROID || UNITY_EDITOR if (fileDropdown.value < 4) { // Sample files are in StreamingAssets, not in PersistentDataPath Log("WARN: Sample files are not in data path. Reading the selected file using FD and full path (versioning will be ignored)"); selectedFile = new FileDescriptor(s_filesAvailableFullPaths[fileDropdown.value], isVersioned); } else { selectedFile = new FileDescriptor(s_filesAvailableRelativePaths[fileDropdown.value], isVersioned); } #else selectedFile = new FileDescriptor(s_filesAvailableRelativePaths[fileDropdown.value], isVersioned); #endif ReadFileAsync(selectedFile); } public void OnClickWriteFile() { if (string.IsNullOrEmpty(fileInput.text)) { Log("ERROR: Enter a file name"); } else { string filename = StorageManager.GetAbsolutePath(fileInput.text); WriteFileAsync(filename); } } public void OnClickWriteFileWithDescriptor(bool isVersioned) { if (string.IsNullOrEmpty(fileInput.text)) { Log("ERROR: Enter a file name"); } else { FileDescriptor fileDescriptor = new FileDescriptor(fileInput.text, isVersioned); WriteFileAsync(fileDescriptor); } } #pragma warning restore CS4014 public void OnClickDeleteFile() { if (fileDropdown.value < 4) { #if !UNITY_ANDROID || UNITY_EDITOR Log("ERROR: Can't delete files from StreamingAssets"); #endif } else { string selectedFileName = s_filesAvailableFullPaths[fileDropdown.value]; if (StorageManager.DeleteFile(selectedFileName, true) == OperationStatus.Ok) { fileDropdown.options.RemoveAt(fileDropdown.value); fileDropdown.value = 0; s_filesAvailableFullPaths.RemoveAt(fileDropdown.value); s_filesAvailableRelativePaths.RemoveAt(fileDropdown.value); } } } public void OnClickDeleteFileWithDescriptor(bool isVersioned) { if (fileDropdown.value < 4) { Log("ERROR: Can't delete files from StreamingAssets"); } else { string selectedFileName = s_filesAvailableRelativePaths[fileDropdown.value]; if (StorageManager.DeleteFile(new FileDescriptor(selectedFileName, isVersioned)) == OperationStatus.Ok) { fileDropdown.options.RemoveAt(fileDropdown.value); fileDropdown.value = 0; s_filesAvailableFullPaths.RemoveAt(fileDropdown.value); s_filesAvailableRelativePaths.RemoveAt(fileDropdown.value); } } } public void OnClickClearLog() { logsText.text = ""; } public async void OnClickSaveLogAsync() { s_logFileId++; string filename = "logs/log_" + s_logFileId + ".log"; WriteResult result = await StorageManager.WriteFileAsync(logsText.text, true, filename, true, false); string file = StorageManager.GetAbsolutePath(filename); Log($"==== Generated log file: {file} ({result.Bytes} bytes) ===="); } #endregion #region Private Methods private IEnumerator InitAndroid() { // It is not possible to access the StreamingAssets folder on Android platform // Instead, we will access the files using UnityWebRequest and then copy them to Application.dataPath folder List newLocations = new List(); string newLocation; foreach (string file in m_filesInAssets) { UnityWebRequest request = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, file)); yield return request.SendWebRequest(); newLocation = Path.Combine(Application.persistentDataPath, file); newLocations.Add(newLocation); #pragma warning disable CS4014 StorageManager.WriteFileAsync(request.downloadHandler.data, newLocation, false, true); #pragma warning restore CS4014 } s_filesAvailableFullPaths.AddRange(newLocations); AddDropdownOptions(newLocations); } private async Task ReadFileAsync(string filename) { string filepath = Path.Combine(Application.streamingAssetsPath, filename); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); ReadResult result = await StorageManager.ReadFileAsync(filepath, false); stopWatch.Stop(); Log($"READ {result.Bytes.Length} bytes. Elapsed time: {stopWatch.Elapsed.TotalMilliseconds} ms"); } private async Task ReadFileAsync(FileDescriptor fileDescriptor) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); ReadResult result = await StorageManager.ReadFileAsync(fileDescriptor); stopWatch.Stop(); Log($"READ {result.Bytes.Length} bytes. Elapsed time: {stopWatch.Elapsed.TotalMilliseconds} ms"); } private static async Task WriteFileAsync(string filename) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); WriteResult result = await StorageManager.WriteFileAsync("this is the file contents", true, filename, false, true); stopWatch.Stop(); Log($"[WROTE {result.Bytes} bytes. Elapsed time: {stopWatch.Elapsed.TotalMilliseconds} ms."); string relativePath = filename.Substring(StorageManager.GetDataPath().Length + 1, filename.Length - StorageManager.GetDataPath().Length - 1); s_filesAvailableRelativePaths.Add(relativePath); s_filesAvailableFullPaths.Add(filename); AddDropdownOptions(new List { filename }); } private async Task WriteFileAsync(FileDescriptor fileDescriptor) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); WriteResult result = await StorageManager.WriteFileAsync("this is the file contents", true, fileDescriptor, true); stopWatch.Stop(); Log($"[WROTE {result.Bytes} bytes. Elapsed time: {stopWatch.Elapsed.TotalMilliseconds} ms"); s_filesAvailableRelativePaths.Add(fileDescriptor.FilePath); s_filesAvailableFullPaths.Add(StorageManager.GetAbsolutePath(fileDescriptor.FilePath)); string versioned = fileDescriptor.IsVersioned ? "(versioned)" : "(unversioned)"; string newOption = $"{fileDescriptor.FilePath} {versioned}"; AddDropdownOptions(new List { newOption }); } private static void AddDropdownOptions(List newOptions) { List options = new List(); string newOption; foreach (string file in newOptions) { try { newOption = file.Length < s_maxCharsDropdown ? file : "..." + file.Substring(file.Length - s_maxCharsDropdown, s_maxCharsDropdown); if (Instance.fileDropdown.options.Find(option => option.text == newOption) == null) { options.Add(newOption); } } catch (Exception e) { Debug.LogError(e.Message); } } if (options.Count > 0) { Instance.fileDropdown.AddOptions(options); } } private static void Log(string text) { lock (Instance.logsText) { Instance.logsText.text += text + "\n"; UnityEngine.Debug.Log(text); } } #endregion }