Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Has anyone written LFS substitute code for WP?
Started by spacewolf Jun 11 2015 03:37 PM

2 replies to this topic
file system lfs lua file system windows phone wp8 plugin
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

spacewolf

[GLOBAL: userInfoPane.html]
spacewolf
  • Contributor

  • 298 posts
  • Enterprise

Hi all,

 

One of my apps that I'm porting to windows phone uses the lfs module which is not supported on windows phone. Has anybody written code I could reuse that deals with this? I could write my own, but if somebody has already done it and is willing to share I'd rather just reuse and not spend the time.

 

Thanks!



[TOPIC: post.html]
#2

spacewolf

[GLOBAL: userInfoPane.html]
spacewolf
  • Contributor

  • 298 posts
  • Enterprise

Well I implemented the two methods I needed for the file system and put it in a bridge class. Posting here in case anyone wants it:

using CoronaLabs.Corona.WinRT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Windows.Storage;
using System.IO.IsolatedStorage;
using System.IO;

namespace Swl.CoronaBridges
{
    public class FileSystemCoronaBridge
    {
        private const string _coronaBaseDir = "Corona";
        private const string _coronaDocumentsDirectory = _coronaBaseDir + "/Documents";
        private const string _coronaCachesDirectory = _coronaBaseDir + "/CachedFiles";
        private const string _coronaTemporaryDirectory = _coronaBaseDir + "/TemporaryFiles";

        private Dictionary<string, string> _baseDirIdToPathDictionary;
        
        private CoronaRuntimeEventArgs _coronaRuntimeEventArgs;

        public FileSystemCoronaBridge()
        {
            _baseDirIdToPathDictionary = new Dictionary<string, string>();
            _baseDirIdToPathDictionary.Add("DocumentsDirectory", _coronaDocumentsDirectory);
            _baseDirIdToPathDictionary.Add("CachesDirectory", _coronaCachesDirectory);
            _baseDirIdToPathDictionary.Add("TemporaryDirectory", _coronaTemporaryDirectory);
        }
        
        public void Register(CoronaRuntimeEventArgs e)
        {
            _coronaRuntimeEventArgs = e;

            // Set up C# method(s) to be invoked from Corona runtime event
            // when the specified event has been dispatched from LUA                        
            _coronaRuntimeEventArgs.CoronaRuntimeEnvironment.AddEventListener("C2N_CreateDirectory", C2N_CreateDirectory_Listener);
            _coronaRuntimeEventArgs.CoronaRuntimeEnvironment.AddEventListener("C2N_DeleteDirectory", C2N_DeleteDirectory_Listener);
            

            // Set up lua methods to be invoked by Corona when the specified event has been dispatched to LUA
            // Create a custom Corona event named "requestingSum" with the following properties.
            // This will be converted into a Lua "event" table once dispatched by Corona.
        }        
        
        public ICoronaBoxedData C2N_CreateDirectory_Listener(CoronaRuntimeEnvironment sender, CoronaLuaEventArgs e)
        {
            string baseDirId = CoronaBridgeHelpers.extractString(e.Properties.Get("baseDirId"));
            string parentPath = CoronaBridgeHelpers.extractString(e.Properties.Get("parentPath"));
            string newDirectory = CoronaBridgeHelpers.extractString(e.Properties.Get("newDirectory"));

            string fullPath = _baseDirIdToPathDictionary[baseDirId];
            
            if (parentPath != string.Empty)
            {
                fullPath += "/" + parentPath;
            }
            fullPath += "/" + newDirectory;

            fullPath = fullPath.Replace("/", "\\");

            Debug.WriteLine("INFO: Calling CreateDirectory(), newDirectory=" + newDirectory);
            var result = CreateDirectory(fullPath);
            Debug.WriteLine("INFO: CreateDirectory() result=" + result);

            if (result == true) 
            {
                return CoronaBoxedBoolean.True;
            }
            else
            {
                return CoronaBoxedBoolean.False;
            }
        }

        private bool CreateDirectory(string newDirectory)
        {                                    
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {            
                try
                {
                    isoStore.CreateDirectory(newDirectory);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("ERROR: Exception on CreateDirectory(): " + e.ToString());
                    return false;
                }
            }

            return true;
        }


        public ICoronaBoxedData C2N_DeleteDirectory_Listener(CoronaRuntimeEnvironment sender, CoronaLuaEventArgs e)
        {
            string baseDirId = CoronaBridgeHelpers.extractString(e.Properties.Get("baseDirId"));
            string directory = CoronaBridgeHelpers.extractString(e.Properties.Get("directory"));
            bool deleteContentsOnly = CoronaBridgeHelpers.extractBool(e.Properties.Get("deleteContentsOnly"));

            string fullPath = _baseDirIdToPathDictionary[baseDirId];
            if (directory != string.Empty)
            {
                fullPath += "/" + directory;
            }

            fullPath = fullPath.Replace("/", "\\");

            Debug.WriteLine("INFO: Calling DeleteDirectory(), directoryPath=" + directory);
            var result = DeleteDirectory(fullPath, deleteContentsOnly);
            Debug.WriteLine("INFO: DeleteDirectory() result=" + result);

            if (result == true)
            {
                return CoronaBoxedBoolean.True;
            }
            else
            {
                return CoronaBoxedBoolean.False;
            }
        }
        
        private bool DeleteDirectory(string directory, bool deleteContentsOnly)
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    DeleteDirectoryRecursively(isoStore, directory);

                    if (deleteContentsOnly == true)
                    {
                        CreateDirectory(directory);
                    }
                }
                catch(Exception e)
                {
                    Debug.WriteLine("ERROR: Exception in DeleteDirectory(): " + e.ToString());
                    return false;
                }
            }

            return true;            
        }

        private void DeleteDirectoryRecursively(IsolatedStorageFile storageFile, String dirName)
        {
            String pattern = dirName + @"\*";
            String[] files = storageFile.GetFileNames(pattern);
            foreach (var fName in files)
            {
                storageFile.DeleteFile(Path.Combine(dirName, fName));
            }
            String[] dirs = storageFile.GetDirectoryNames(pattern);
            foreach (var dName in dirs)
            {
                DeleteDirectoryRecursively(storageFile, Path.Combine(dirName, dName));
            }
            storageFile.DeleteDirectory(dirName);
        }
    }
}


[TOPIC: post.html]
#3

doubleslashdesign

[GLOBAL: userInfoPane.html]
doubleslashdesign
  • Contributor

  • 727 posts
  • Corona SDK

Cool, thanks

 

Larry




[topic_controls]
[/topic_controls]