Question How to read file in vam plugin?

dkavmdk98

New member
Messages
2
Reactions
1
Points
3
I understand that System.IO is not available when creating plugin VAM. So what should I do if I want to read .txt or .png files from the plugin? Or if you let me know the plugin to read the text file or image file, I will look at the code myself and refer to it.
 
Solution
There are various safe-wrappers you can use. These restrict file access to within the VaM directory and read-only. Writing is possible only in a very short list of folders:
  • Saves\PluginData
  • Saves\screenshots
  • Custom\PluginData
In addition you can write to other subfolders under Custom and Saves, but it triggers a confirmation dialog.

On SuperController you find those:
public void SaveJSON(JSONClass jc, string saveName) public void SaveJSON(JSONClass jc, string saveName, UserActionCallback confirmCallback, UserActionCallback denyCallback, ExceptionCallback exceptionCallback) public JSONNode LoadJSON(string saveName)
These also exist as convenience methods on MVRScript, which are just calling SuperController.

There...
There are various safe-wrappers you can use. These restrict file access to within the VaM directory and read-only. Writing is possible only in a very short list of folders:
  • Saves\PluginData
  • Saves\screenshots
  • Custom\PluginData
In addition you can write to other subfolders under Custom and Saves, but it triggers a confirmation dialog.

On SuperController you find those:
public void SaveJSON(JSONClass jc, string saveName) public void SaveJSON(JSONClass jc, string saveName, UserActionCallback confirmCallback, UserActionCallback denyCallback, ExceptionCallback exceptionCallback) public JSONNode LoadJSON(string saveName)
These also exist as convenience methods on MVRScript, which are just calling SuperController.

There are also various static file management functions on FileManagerSecure in namespace MVR.FileManagementSecure
public static string GetFullPath(string path) public static string NormalizeLoadPath(string path) public static string NormalizePath(string path) public static string GetDirectoryName(string path, bool returnSlashPath = false) public static string GetFileName(string path) public static bool FileExists(string path, bool onlySystemFiles = false) public static bool IsFileInPackage(string path) public static DateTime FileLastWriteTime(string path, bool onlySystemFiles = false) public static DateTime FileCreationTime(string path, bool onlySystemFiles = false) public static void RegisterRefreshHandler(OnRefresh refreshHandler) public static void UnregisterRefreshHandler(OnRefresh refreshHandler) public static bool PackageExists(string packageUid) public static int GetPackageVersion(string packageUid) public static List<ShortCut> GetShortCutsForDirectory(string dir, bool allowNavigationAboveRegularDirectories = false, bool useFullPaths = false, bool generateAllFlattenedShortcut = false, bool includeRegularDirsInFlattenedShortcut = false) public static bool DirectoryExists(string path, bool onlySystemDirectories = false) public static bool IsDirectoryInPackage(string path) public static DateTime DirectoryLastWriteTime(string path, bool onlySystemDirectories = false) public static DateTime DirectoryCreationTime(string path, bool onlySystemDirectories = false) public static string[] GetDirectories(string dir, string pattern = null) public static string[] GetFiles(string dir, string pattern = null) public static void CreateDirectory(string path) public static void CreateDirectory(string path, UserActionCallback confirmCallback, UserActionCallback denyCallback, ExceptionCallback exceptionCallback) public static byte[] ReadAllBytes(string path) public static string ReadAllText(string path) public static void WriteAllText(string path, string text) public static void WriteAllText(string path, string text, UserActionCallback confirmCallback, UserActionCallback denyCallback, ExceptionCallback exceptionCallback) public static void WriteAllBytes(string path, byte[] bytes) public static void WriteAllBytes(string path, byte[] bytes, UserActionCallback confirmCallback, UserActionCallback denyCallback, ExceptionCallback exceptionCallback) public static void DeleteFile(string path) public static void DeleteFile(string path, UserActionCallback confirmCallback, UserActionCallback denyCallback, ExceptionCallback exceptionCallback) public static void CopyFile(string oldPath, string newPath) public static void CopyFile(string oldPath, string newPath, UserActionCallback confirmCallback, UserActionCallback denyCallback, ExceptionCallback exceptionCallback) public static void MoveFile(string oldPath, string newPath, bool overwrite = true) public static void MoveFile(string oldPath, string newPath, UserActionCallback confirmCallback, UserActionCallback denyCallback, ExceptionCallback exceptionCallback, bool overwrite = true)

For writing images you can use something like this (copy&paste from SuperShot):
C#:
Texture2D texture = new Texture2D(myOutputTexture.width, myOutputTexture.height, TextureFormat.RGB24, false);

// ... copy image data into the texture ...

byte[] bytes = (fileFormat == FORMAT_JPG) ? texture.EncodeToJPG(100) : texture.EncodeToPNG();
FileManagerSecure.CreateDirectory(SCREENSHOT_DIRECTORY);
FileManagerSecure.WriteAllBytes(SCREENSHOT_DIRECTORY + filename, bytes);

See Unity docs for more detail. The ImageConversion class provides extension methods for the Texture2D class:

There is also a function to load JPG/PNG in a similar way:
texture.LoadImage(bytes);
Out of my head, not 100% sure this works, though. For example my PostMagic loads textures in a different way via VaM's own image loader. Wrote that too long ago, not sure why it was needed. Possibly to give more control what is loaded, like mipmaps and other options. See MacGruber.Utils.QueueLoadTexture for details.
 
Last edited:
Upvote 0
Solution
I checked it late because I had something to do. Thank you so much for your kind response. I will test it again based on the information you gave me. Thank you so much :)
 
Upvote 0
Back
Top Bottom