Exemple #1
0
 public static byte[] ReadAllBytes(string path)
 {
     byte[] result;
     using (VirtualFileStream virtualFileStream = Open(path))
     {
         byte[] bytesBuf = new byte[virtualFileStream.Length];
         if (virtualFileStream.Read(bytesBuf, 0, bytesBuf.Length) != bytesBuf.Length)
         {
             throw new EndOfStreamException();
         }
         result = bytesBuf;
     }
     return(result);
 }
Exemple #2
0
        private static void PreloadFileToMemory(object obj)
        {
            PreloadFileToMemoryItem preloadFileToMemoryItem = (PreloadFileToMemoryItem)obj;

            try
            {
                using (VirtualFileStream virtualFileStream = VirtualFile.Open(preloadFileToMemoryItem.Path))
                {
                    byte[] bytesBuf = new byte[virtualFileStream.Length];
                    if (virtualFileStream.Read(bytesBuf, 0, bytesBuf.Length) != bytesBuf.Length)
                    {
                        throw new Exception("Unable to load all data.");
                    }
                    preloadFileToMemoryItem.data   = bytesBuf;
                    preloadFileToMemoryItem.loaded = true;
                }
            }
            catch (Exception ex)
            {
                preloadFileToMemoryItem.error = ex.Message;
            }
        }
Exemple #3
0
        public static VirtualFileStream Open(string path)
        {
            VirtualFileStream result;

            lock (VirtualFileSystem.syncVFS)
            {
                if (!VirtualFileSystem.Initialized)
                {
                    Log.Fatal("VirtualFileSystem: File system is not initialized.");
                    result = null;
                }
                else
                {
                    if (VirtualFileSystem.LoggingFileOperations)
                    {
                        Log.Info("Logging File Operations: VirtualFile.Open( \"{0}\" )", path);
                    }
                    path = VirtualFileSystem.NormalizePath(path);
                    path = VirtualFileSystem.RedirectFile(path, true);
                    bool cachingExtension = VirtualFileSystem.IsCachingExtension(path);
                    if (cachingExtension)
                    {
                        byte[] bytesCached = VirtualFileSystem.LoadCachedBytes(path);
                        if (bytesCached != null)
                        {
                            result = new MemoryVirtualFileStream(bytesCached);
                            return(result);
                        }
                    }

                    if (VirtualFileSystem.preloadItems.Count != 0)
                    {
                        string key = path.ToLower();
                        VirtualFileSystem.PreloadFileToMemoryItem preloadFileToMemoryItem;
                        if (VirtualFileSystem.preloadItems.TryGetValue(key, out preloadFileToMemoryItem) && preloadFileToMemoryItem.loaded)
                        {
                            result = new MemoryVirtualFileStream(preloadFileToMemoryItem.data);
                            return(result);
                        }
                    }
                    VirtualFileStream virtualFileStream = null;
                    string            realPathByVirtual = VirtualFileSystem.GetRealPathByVirtual(path);
                    try
                    {
                        if (PlatformInfo.Platform == PlatformInfo.PlanformType.Windows)
                        {
                            virtualFileStream = new WindowsVirtualFileStream(realPathByVirtual);
                        }
                        else if (PlatformInfo.Platform == PlatformInfo.PlanformType.MacOSX)
                        {
                            virtualFileStream = new MacOSXVirtualFileStream(realPathByVirtual);
                        }
                        else
                        {
                            virtualFileStream = new DefaultVirtualFileStream(realPathByVirtual);
                        }
                    }
                    catch (FileNotFoundException)
                    {
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    if (virtualFileStream == null)
                    {
                        virtualFileStream = ArchiveManager.Instance.FileOpen(path);
                        if (virtualFileStream == null)
                        {
                            throw new FileNotFoundException("File not found.", path);
                        }
                    }
                    if (cachingExtension)
                    {
                        byte[] array2 = new byte[virtualFileStream.Length];
                        if ((long)virtualFileStream.Read(array2, 0, (int)virtualFileStream.Length) == virtualFileStream.Length)
                        {
                            VirtualFileSystem.CacheBytes(path, array2);
                        }
                        virtualFileStream.Position = 0L;
                    }
                    result = virtualFileStream;
                }
            }
            return(result);
        }
Exemple #4
0
        private static void InitDeployment()
        {
            string userDirectory     = null;
            string deploymentCfgPath = "Base/Constants/Deployment.config";

            deployed = false;
            if (VirtualFile.Exists(deploymentCfgPath))
            {
                deployed             = true;
                deploymentParameters = new DeploymentParametersClass();
                try
                {
                    using (VirtualFileStream virtualFileStream = VirtualFile.Open(deploymentCfgPath))
                    {
                        using (StreamReader streamReader = new StreamReader(virtualFileStream))
                        {
                            while (true)
                            {
                                string line = streamReader.ReadLine();
                                if (line == null)
                                {
                                    break;
                                }

                                line = line.Trim();
                                if (!(line == "") && (line.Length < 2 || !(line.Substring(0, 2) == "//")))
                                {
                                    int eqPosition = line.IndexOf('=');
                                    if (eqPosition != -1)
                                    {
                                        string name  = line.Substring(0, eqPosition).Trim();
                                        string value = line.Substring(eqPosition + 1).Trim();
                                        if (value != "")
                                        {
                                            if (name == "userDirectory")
                                            {
                                                userDirectory = value;
                                            }
                                            if (name == "defaultLanguage")
                                            {
                                                deploymentParameters.defaultLanguage = value;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Fatal("VirtualFileSystem: Loading file failed {0} ({1}).", deploymentCfgPath, ex.Message);
                    return;
                }
            }

            if (string.IsNullOrEmpty(userDirectoryPath))
            {
                if (!string.IsNullOrEmpty(userDirectory))
                {
                    string path = null;
                    if (PlatformInfo.Platform == PlatformInfo.PlanformType.Windows)
                    {
                        path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                    }
                    else if (PlatformInfo.Platform == PlatformInfo.PlanformType.MacOSX)
                    {
                        path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library/Application Support");
                    }
                    else if (PlatformInfo.Platform == PlatformInfo.PlanformType.Android)
                    {
                        userDirectoryPath = Path.Combine(VirtualFileSystem.ExecutableDirectoryPath, "UserSettings");
                    }
                    else
                    {
                        Log.Fatal("VirtualFileSystem: InitDeploymentInfoAndUserDirectory: Unknown platform.");
                    }
                    userDirectoryPath = Path.Combine(path, userDirectory);
                    return;
                }
                userDirectoryPath = Path.Combine(ExecutableDirectoryPath, "UserSettings");
            }
        }