private async void ReadFB2File(string path) { string fileName = FileBrowserHelpers.GetFilename(path); string text = FileBrowserHelpers.ReadTextFromFile(path); FB2File file = await new FB2Reader().ReadAsync(text); var lines = await _fB2SampleConverter.ConvertAsync(file); string finalText = _fB2SampleConverter.GetLinesAsText(); byte[] imageData = _fB2SampleConverter.GetCoverImageData(); string imagePath = Application.persistentDataPath + _targetFolder + fileName + ".jpg";; try { File.WriteAllBytes(imagePath, imageData); Debug.Log("Image is saved. Path: " + imagePath); } catch { Debug.LogError("Loading image is error!"); } imagePath = imagePath.Replace("/", "\\"); string filePath = (Application.persistentDataPath + _targetFolder + fileName).Replace("/", "\\"); FileData newBook = new FileData(fileName, filePath, imagePath, FileData.FileType.Book); SaveFile(newBook, finalText); }
IEnumerator ShowLoadDialogCoroutine() { // Show a load file dialog and wait for a response from user // Load file/folder: file, Initial path: default (Documents), Title: "Load File", submit button text: "Load" yield return(FileBrowser.WaitForLoadDialog(false, @"Assets\Resources\Animations\", "Load Animation", "Load")); // Dialog is closed // Print whether a file is chosen (FileBrowser.Success) // and the path to the selected file (FileBrowser.Result) (null, if FileBrowser.Success is false) Debug.Log(FileBrowser.Success + " " + FileBrowser.Result); if (FileBrowser.Success) { // If a file was chosen, read its bytes via FileBrowserHelpers // Contrary to File.ReadAllBytes, this function works on Android 10+, as well //byte[] bytes = FileBrowserHelpers.ReadBytesFromFile(FileBrowser.Result) string code = FileBrowserHelpers.ReadTextFromFile(FileBrowser.Result); Anim loadedAnim = new Anim(FileBrowserHelpers.GetFilename(FileBrowser.Result).Replace(".txt", ""), code); //loadedAnim.Code = GetCleanCode(loadedAnim.Code); AnimationData.Instance.AddAnim(loadedAnim); AnimationData.Instance.selectedAnim = loadedAnim; MenuManager.Instance.UpdateAnimations(); MenuManager.Instance.SetSelectedAnimation(loadedAnim.AnimationName); } }
void ReadFile(string path) { if (path != "") { //byte[] bt = FileBrowserHelpers.ReadBytesFromFile(path); //Designer.PGdata = System.Text.Encoding.ASCII.GetString(bt); Designer.PGdata = FileBrowserHelpers.ReadTextFromFile(path); Designer.LoadFromString(); } }
private IEnumerator ReadV5File(string filename, Action <string> callback = null) { string url; if (Application.platform == RuntimePlatform.Android) { if (useEmbeddedData) { url = System.IO.Path.Combine(Application.streamingAssetsPath, filename); if (url.Contains("://")) { var www = new UnityWebRequest(url); www.downloadHandler = new DownloadHandlerBuffer(); yield return(www.SendWebRequest()); v5file = www.downloadHandler.text; } else { v5file = FileBrowserHelpers.ReadTextFromFile(url); } } else { url = filename; v5file = FileBrowserHelpers.ReadTextFromFile(url); } debugString += url + '\n'; // UIPanel.transform.Find("DebugText").GetComponent<Text>().text = debugString; } else { if (useEmbeddedData) { url = System.IO.Path.Combine(Application.streamingAssetsPath, filename); } else { url = "file://" + filename; } var www = UnityWebRequest.Get(url); yield return(www.SendWebRequest()); #if UNITY_2020_2_OR_NEWER if (www.result == UnityWebRequest.Result.ProtocolError || www.result == UnityWebRequest.Result.ConnectionError) #else if (www.isHttpError || www.isNetworkError) #endif { Debug.Log(www.error); } else { v5file = www.downloadHandler.text; if (callback != null) { callback(www.downloadHandler.text); } } } }
private async void ImportFiles() { string path = Application.persistentDataPath + _sourceFolder; try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } catch (IOException ex) { Debug.Log(ex.Message); return; } var files = FileBrowserHelpers.GetEntriesInDirectory(path); foreach (var file in files) { string fileName = string.Empty; string filePath = string.Empty; string imagePath = string.Empty; string text = string.Empty; string finalText = string.Empty; byte[] imageData = null; FileData newBook = null; if (!file.IsDirectory) { switch (file.Extension) { case ".fb2": fileName = FileBrowserHelpers.GetFilename(file.Path); text = FileBrowserHelpers.ReadTextFromFile(file.Path); FB2File fb2File = await new FB2Reader().ReadAsync(text); var lines = await _fB2SampleConverter.ConvertAsync(fb2File); finalText = _fB2SampleConverter.GetLinesAsText(); imageData = _fB2SampleConverter.GetCoverImageData(); if (imageData != null) { imagePath = Application.persistentDataPath + _targetFolder + fileName + ".jpg";; try { File.WriteAllBytes(imagePath, imageData); Debug.Log("Image is saved. Path: " + imagePath); } catch { Debug.LogError("Loading image is error!"); } imagePath = imagePath.Replace("/", "\\"); } filePath = (Application.persistentDataPath + _targetFolder + fileName).Replace("/", "\\"); newBook = new FileData(fileName, filePath, imagePath, FileData.FileType.Book); SaveFile(newBook, finalText); FileBrowserHelpers.DeleteFile(file.Path); break; case ".epub": EpubBook epubFile = EpubReader.ReadBook(file.Path); fileName = epubFile.Title + " (" + epubFile.Author + ")"; foreach (EpubTextContentFile textContentFile in epubFile.ReadingOrder) { HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(textContentFile.Content); StringBuilder sb = new StringBuilder(); foreach (HtmlNode node in htmlDocument.DocumentNode.SelectNodes("//text()")) { sb.AppendLine(node.InnerText.Replace("\n", "").Replace("\r", "")); } finalText += sb.ToString(); } imageData = epubFile.CoverImage; var imageName = string.Empty; if (imageData == null) { imageData = epubFile.Content.Images.FirstOrDefault().Value.Content; imageName = epubFile.Content.Images.FirstOrDefault().Key; } else { imageName = epubFile.Content.Cover.FileName; } if (imageData != null) { imagePath = Application.persistentDataPath + _targetFolder + imageName; try { File.WriteAllBytes(imagePath, imageData); Debug.Log("Image is saved. Path: " + imagePath); } catch { Debug.LogError("Loading image is error!"); } imagePath = imagePath.Replace("/", "\\"); } filePath = (Application.persistentDataPath + _targetFolder + fileName).Replace("/", "\\"); newBook = new FileData(fileName, filePath, imagePath, FileData.FileType.Book); SaveFile(newBook, finalText); FileBrowserHelpers.DeleteFile(file.Path); break; case ".pdf": var document = PdfiumViewer.PdfDocument.Load(file.Path); var title = document.GetInformation().Title; if (FilesData.Files.Any(x => x.Name == title)) { return; } fileName = FileBrowserHelpers.GetFilename(file.Path); filePath = Application.persistentDataPath + _targetFolder + fileName; imagePath = Application.persistentDataPath + _targetFolder + fileName.Remove(fileName.Length - 4) + ".png"; var image = document.Render(0, 72, 72, false); image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png); imagePath = imagePath.Replace("/", "\\"); filePath = filePath.Replace("/", "\\"); document.Dispose(); FileBrowserHelpers.MoveFile(file.Path, filePath); newBook = new FileData(title, filePath, imagePath, FileData.FileType.PdfFile); SaveFile(newBook); break; } } else { FileBrowserHelpers.DeleteDirectory(file.Path); } } }
public string LoadFile(string path) { string text = FileBrowserHelpers.ReadTextFromFile(path); return(text); }