Esempio n. 1
0
        public PhysicalDirectoryHelper(string rootPath)
        {
            _compatDirectory = new DirectoryInfo(Path.Combine(rootPath, "Physical-" + Guid.NewGuid()));
            _compatDirectory.Create();

            var pfs = new PhysicalFileSystem();

            PhysicalFileSystem = new SubFileSystem(pfs, pfs.ConvertPathFromInternal(_compatDirectory.FullName));
        }
Esempio n. 2
0
        private static CodeWriter GetCodeWriter(string subPath)
        {
            var fs         = new PhysicalFileSystem();
            var destFolder = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, SrcFolderRelative, subPath));
            var subfs      = new SubFileSystem(fs, fs.ConvertPathFromInternal(destFolder));
            var codeWriter = new CodeWriter(new CodeWriterOptions(subfs));

            return(codeWriter);
        }
Esempio n. 3
0
        public TestMountFileSystemCompatSub()
        {
            // Check that MountFileSystem is working with a mount with the compat test
            var mountfs = new MountFileSystem();
            mountfs.Mount("/customMount", new MemoryFileSystem());

            // Use a SubFileSystem to fake the mount to a root folder
            fs = new SubFileSystem(mountfs, "/customMount");
        }
Esempio n. 4
0
        public void TestFileSystemInvalidDriveLetter()
        {
            var driverLetter = SystemPath[0];

            Assert.Throws <DirectoryNotFoundException>(() => new SubFileSystem(new PhysicalFileSystem(), $"/mnt/{driverLetter}"));
            using (var fs = new SubFileSystem(new PhysicalFileSystem(), $"/mnt/{char.ToLowerInvariant(driverLetter)}"))
            {
            }
        }
        /// <summary>
        /// Determine what kind of filesystem to use.
        /// After this point we *should theoretically* not need to use System.IO.Path.
        /// </summary>
        public static (IFileSystem, DirectoryEntry) DetermineFileSystem(string path, bool readOnly = false)
        {
            var emptyPath = string.IsNullOrWhiteSpace(path);
            var extension = Path.GetExtension(path);

            Log.Debug($"Determining file system for {path}");

            IFileSystem    fileSystem;
            DirectoryEntry baseEntry;

            if (emptyPath)
            {
                fileSystem = new PhysicalFileSystem();
                baseEntry  = fileSystem.GetDirectoryEntry(
                    fileSystem.ConvertPathFromInternal(Directory.GetCurrentDirectory()));
            }
            else
            {
                // resolve path (relative to absolute)
                path = Path.GetFullPath(path);

                if (Directory.Exists(path) || extension == string.Empty)
                {
                    var physicalFileSystem = new PhysicalFileSystem();
                    var internalPath       = physicalFileSystem.ConvertPathFromInternal(path);
                    physicalFileSystem.CreateDirectory(internalPath);
                    fileSystem = new SubFileSystem(physicalFileSystem, internalPath);
                    baseEntry  = fileSystem.GetDirectoryEntry(UPath.Root);
                }
                else
                {
                    // ensure parent directory exists on disk
                    Directory.CreateDirectory(Path.GetDirectoryName(path));

                    switch (extension)
                    {
                    case "." + SqlitePattern:
                        fileSystem = new SqliteFileSystem(
                            path,
                            readOnly ? OpenMode.ReadOnly : OpenMode.ReadWriteCreate);
                        break;

                    default:
                        throw new NotSupportedException(
                                  $"Cannot determine file system for given extension {extension}");
                    }

                    baseEntry = new DirectoryEntry(fileSystem, UPath.Root);
                }
            }

            Log.Debug($"Filesystem for {path} is {fileSystem.GetType().Name}");

            return(fileSystem, baseEntry);
        }
Esempio n. 6
0
        /// <summary> Returns a root dir for an existing directory entry </summary>
        public static DirectoryEntry ToRootDirectoryEntry(this DirectoryInfo localDir)
        {
            if (!localDir.Exists)   // The SubFileSystem cant handle non existing roots
            {
                throw new DirectoryNotFoundException("ToRootDirectoryEntry on non-existing dir: " + localDir);
            }
            var pfs = new PhysicalFileSystemV2(ExtractDiscPrefix(localDir));
            var fs  = new SubFileSystem(pfs, pfs.ConvertPathFromInternal(localDir.FullName));

            return(fs.GetDirectoryEntry(UPath.Root));
        }
Esempio n. 7
0
        /// <summary>
        /// Clone a <see cref="IFileSystem"/> with a new sub path.
        /// </summary>
        /// <param name="fileSystem"><see cref="IFileSystem"/> to clone.</param>
        /// <param name="path">The sub path of the cloned file system.</param>
        /// <param name="streamManager">The stream manager for this file system.</param>
        /// <returns>The cloned <see cref="IFileSystem"/>.</returns>
        public static IFileSystem CloneFileSystem(IFileSystem fileSystem, UPath path, IStreamManager streamManager)
        {
            var newFileSystem = fileSystem.Clone(streamManager);

            if (path != UPath.Empty)
            {
                newFileSystem = new SubFileSystem(newFileSystem, path);
            }

            return(newFileSystem);
        }
Esempio n. 8
0
        /// <summary>
        /// Create a <see cref="AfiFileSystem"/> based on the given <see cref="IArchiveState"/>.
        /// </summary>
        /// <param name="stateInfo"><see cref="IStateInfo"/> to create the file system from.</param>
        /// <param name="path">The path of the virtual file system.</param>
        /// <param name="streamManager">The stream manager for this file system.</param>
        /// <returns>The created <see cref="IFileSystem"/> for this state.</returns>
        public static IFileSystem CreateAfiFileSystem(IStateInfo stateInfo, UPath path, IStreamManager streamManager)
        {
            var fileSystem = (IFileSystem) new AfiFileSystem(stateInfo, streamManager);

            if (path != UPath.Empty && path != UPath.Root)
            {
                fileSystem = new SubFileSystem(fileSystem, path);
            }

            return(fileSystem);
        }
Esempio n. 9
0
        public SiteObject(ILoggerFactory loggerFactory = null)
        {
            var sharedFolder = Path.Combine(Path.GetDirectoryName(typeof(SiteObject).GetTypeInfo().Assembly.Location), SharedFolderName);

            _contentFileSystems = new List <IFileSystem>();
            var sharedPhysicalFileSystem = new PhysicalFileSystem();

            // Make sure that SharedFileSystem is a read-only filesystem
            SharedFileSystem     = new ReadOnlyFileSystem(new SubFileSystem(sharedPhysicalFileSystem, sharedPhysicalFileSystem.ConvertPathFromInternal(sharedFolder)));
            SharedMetaFileSystem = SharedFileSystem.GetOrCreateSubFileSystem(LunetFolder);

            _fileSystem = new AggregateFileSystem(SharedFileSystem);

            MetaFileSystem = new SubFileSystem(_fileSystem, LunetFolder);

            ConfigFile = new FileEntry(_fileSystem, UPath.Root / DefaultConfigFileName);

            StaticFiles  = new PageCollection();
            Pages        = new PageCollection();
            DynamicPages = new PageCollection();

            // Create the logger
            LoggerFactory = loggerFactory ?? Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
            {
                builder.AddProvider(new LoggerProviderIntercept(this))
                .AddFilter(LogFilter)
                .AddConsole();
            });

            Log          = LoggerFactory.CreateLogger("lunet");
            ContentTypes = new ContentTypeManager();

            DefaultPageExtension = DefaultPageExtensionValue;

            Html = new HtmlObject(this);
            SetValue(SiteVariables.Html, Html, true);

            CommandLine = new LunetCommandLine(this);

            Statistics = new SiteStatistics();

            Scripts = new ScriptingPlugin(this);

            Content = new ContentPlugin(this);

            Plugins = new OrderedList <ISitePlugin>();

            _pluginBuilders = new ContainerBuilder();
            _pluginBuilders.RegisterInstance(LoggerFactory).As <ILoggerFactory>();
            _pluginBuilders.RegisterInstance(this);
        }
Esempio n. 10
0
        /// <summary>
        /// Replace files in destination file system.
        /// </summary>
        /// <param name="stateInfo">The state to save in the destination.</param>
        /// <param name="sourceFileSystem">The file system to take the files from.</param>
        /// <param name="destinationFileSystem">The file system to replace the files in.</param>
        private async Task <SaveResult> MoveFiles(IStateInfo stateInfo, IFileSystem sourceFileSystem, IFileSystem destinationFileSystem)
        {
            if (stateInfo.HasParent)
            {
                // Put source filesystem into final destination
                destinationFileSystem = new SubFileSystem(destinationFileSystem, stateInfo.FilePath.ToAbsolute().GetDirectory());

                var replaceResult = await TryReplaceFiles(sourceFileSystem, destinationFileSystem, stateInfo.ParentStateInfo.StreamManager);

                return(replaceResult);
            }

            // Put source filesystem into final destination
            var copyResult = await TryCopyFiles(sourceFileSystem, destinationFileSystem);

            return(copyResult);
        }
Esempio n. 11
0
        public DefaultSiteOptions(string rootDirectory = null)
        {
            rootDirectory = Path.GetFullPath(rootDirectory ?? Directory.GetCurrentDirectory());

            if (!Directory.Exists(rootDirectory))
            {
                throw new DirectoryNotFoundException($"The directory `{rootDirectory}` was not found");
            }

            var basefs = new PhysicalFileSystem();

            BaseFileSystem = new SubFileSystem(basefs, basefs.ConvertPathFromInternal(rootDirectory));

            TempFileSystem = new SubFileSystem(BaseFileSystem, UPath.Root / DefaultTempFolder);

            OutputFileSystem = new SubFileSystem(BaseFileSystem, UPath.Root / DefaultOutputFolder);
        }
Esempio n. 12
0
        /// <summary>
        /// Checks the and initialize.
        /// </summary>
        /// <exception cref="bS.Sked2.Structure.Base.Exceptions.StorageException">
        /// Error initializing storage. Invalid 'RootPath' provided. - 1
        /// or
        /// Error initializing storage. {e.Message} - 2
        /// </exception>
        private void CheckAndInit()
        {
            // Check if config is valid
            if (string.IsNullOrWhiteSpace(this.config.RootPath))
            {
                throw new StorageException(logger, $"Error initializing storage. Invalid 'RootPath' provided.", 1);
            }

            // Init the virtual file system
            try
            {
                var fileSystem = new PhysicalFileSystem();
                var rootUpath  = fileSystem.ConvertPathFromInternal(this.config.RootPath);
                workSpaceFileSystem = new SubFileSystem(fileSystem, rootUpath);
            }
            catch (Exception e)
            {
                throw new StorageException(logger, $"Error initializing storage. {e.Message}", e, 2);
            }
        }
Esempio n. 13
0
        private static void JoinPersonalCloud()
        {
            var t2 = new SimpleConfigStorage(
                Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                             "TestConsoleApp", Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture)));

            var fs    = new PhysicalFileSystem();
            var subfs = new SubFileSystem(fs, fs.ConvertPathFromInternal(t2.RootPath), true);

            pcservice = new PCLocalService(t2, loggerFactory, subfs, null);
            //pcservice.SetUdpPort(2330, new[] { 2330 });
            pcservice.StartService();

            Console.Write("Input share code:");
            var input = Console.ReadLine();

            Console.WriteLine();

            pc = pcservice.JoinPersonalCloud(int.Parse(input), "test2").Result;
        }
        public void MountWorkspace(string name)
        {
            var filePath = WorkspacePath.Root.AppendDirectory("User");

            // Make sure that the user directory exits
            if (Exists(filePath))
            {
                filePath = filePath.AppendDirectory(name);

                // If the filesystem doesn't exit, we want to create it
                if (!Exists(filePath))
                {
                    CreateDirectory(filePath);
                }
            }

            var workspaceDisk = new SubFileSystem(this, filePath);

            Mounts.Add(
                new KeyValuePair <WorkspacePath, IFileSystem>(WorkspacePath.Root.AppendDirectory("Workspace"),
                                                              workspaceDisk));
        }
Esempio n. 15
0
        public void TestCopyFileCross()
        {
            // TODO: Add more tests
            var from = new MemoryFileSystem();

            from.WriteAllText("/test.txt", "test");
            var fs         = new PhysicalFileSystem();
            var outputfs   = new SubFileSystem(fs, fs.ConvertPathFromInternal(SystemPath));
            var outputPath = (UPath)"/test.txt";

            try
            {
                outputfs.WriteAllText(outputPath, "toto");
                from.CopyFileCross("/test.txt", outputfs, outputPath, true);
                var content = outputfs.ReadAllText(outputPath);
                Assert.Equal("test", content);
            }
            finally
            {
                outputfs.DeleteFile(outputPath);
            }
        }
        public static void AddJsonConfigurationService(
            this ICoconaLiteServiceCollection services,
            string jsonConfigurationPath)
        {
            string jsonConfigurationDirectory = Directory.GetParent(jsonConfigurationPath).FullName;

            if (!Directory.Exists(jsonConfigurationDirectory))
            {
                Directory.CreateDirectory(jsonConfigurationDirectory);
            }

            var pfs        = new PhysicalFileSystem();
            var fileSystem = new SubFileSystem(
                pfs,
                pfs.ConvertPathFromInternal(jsonConfigurationDirectory),
                owned: true);

            services.AddTransient <IConfigurationService <ToolConfiguration> >(
                _ => new JsonConfigurationService(
                    fileSystem,
                    Path.GetFileName(jsonConfigurationPath)));
        }
Esempio n. 17
0
        public void TestBasic()
        {
            var fs   = new PhysicalFileSystem();
            var path = fs.ConvertPathFromInternal(SystemPath);

            // Create a filesystem / on the current folder of this assembly
            var subfs = new SubFileSystem(fs, path);

            // This test is basically testing the two methods (ConvertPathToDelegate and ConvertPathFromDelegate) in SubFileSystem

            var files         = subfs.EnumeratePaths("/").Select(info => info.GetName()).ToList();
            var expectedFiles = fs.EnumeratePaths(path).Select(info => info.GetName()).ToList();

            Assert.True(files.Count > 0);
            Assert.Equal(expectedFiles, files);

            // Check that SubFileSystem is actually checking that the directory exists in the delegate filesystem
            Assert.Throws <DirectoryNotFoundException>(() => new SubFileSystem(fs, path / "does_not_exist"));

            Assert.Throws <InvalidOperationException>(() => subfs.ConvertPathFromInternal(@"C:\"));
            // TODO: We could add another test just to make sure that files can be created...etc. But the test above should already cover the code provided in SubFileSystem
        }
Esempio n. 18
0
        public void Setup(string inputDirectory, string outputDirectory, params string[] defines)
        {
            var rootFolder = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, inputDirectory ?? "."));

            var diskfs = new PhysicalFileSystem();

            var siteFileSystem = new SubFileSystem(diskfs, diskfs.ConvertPathFromInternal(rootFolder));

            SiteFileSystem = siteFileSystem;

            // Add defines
            foreach (var value in defines)
            {
                AddDefine(value);
            }

            var outputFolder = outputDirectory != null
                ? Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, outputDirectory))
                : Path.Combine(rootFolder, LunetFolderName + "/build/" + DefaultOutputFolderName);

            var outputFolderForFs = diskfs.ConvertPathFromInternal(outputFolder);

            OutputFileSystem = diskfs.GetOrCreateSubFileSystem(outputFolderForFs);
        }
Esempio n. 19
0
        /// <summary>
        /// Create a <see cref="PhysicalFileSystem"/> based on the directory in <paramref name="path"/>.
        /// </summary>
        /// <param name="path">The path of the physical file system.</param>
        /// <param name="streamManager">The stream manager for this file system.</param>
        /// <returns>The created <see cref="PhysicalFileSystem"/> for this folder.</returns>
        public static IFileSystem CreatePhysicalFileSystem(UPath path, IStreamManager streamManager)
        {
            IFileSystem fileSystem;

            if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                fileSystem = new PhysicalFileSystem(streamManager);
                return(new SubFileSystem(fileSystem, path));
            }

            if (!IsRooted(path))
            {
                throw new InvalidOperationException($"Path {path} is not rooted to a drive.");
            }

            var internalPath = ReplaceWindowsDrive(path);
            var physicalPath = ReplaceMountPoint(path);

            if (!Directory.Exists(physicalPath.FullName))
            {
                Directory.CreateDirectory(physicalPath.FullName);
            }

            fileSystem = new PhysicalFileSystem(streamManager);
            if (IsOnlyDrive(internalPath))
            {
                fileSystem = new SubFileSystem(fileSystem, internalPath.FullName.Substring(0, 6));
            }
            else
            {
                fileSystem = new SubFileSystem(fileSystem, internalPath.FullName.Substring(0, 7));
                fileSystem = new SubFileSystem(fileSystem, internalPath.FullName.Substring(6));
            }

            return(fileSystem);
        }
Esempio n. 20
0
        /// <summary>
        /// Creates a new <seealso cref="DefaultStore"/>.
        /// </summary>
        /// <param name="path">The path of the directory where the storage files will be saved.
        /// If the path is <c>null</c>, the database is created in memory.</param>
        /// <param name="compress">Whether to compress data.  Does not compress by default.</param>
        /// <param name="journal">
        /// Enables or disables double write check to ensure durability.
        /// </param>
        /// <param name="indexCacheSize">Max number of pages in the index cache.</param>
        /// <param name="blockCacheSize">The capacity of the block cache.</param>
        /// <param name="txCacheSize">The capacity of the transaction cache.</param>
        /// <param name="statesCacheSize">The capacity of the states cache.</param>
        /// <param name="flush">Writes data direct to disk avoiding OS cache.  Turned on by default.
        /// </param>
        /// <param name="readOnly">Opens database readonly mode. Turned off by default.</param>
        public DefaultStore(
            string path,
            bool compress       = false,
            bool journal        = true,
            int indexCacheSize  = 50000,
            int blockCacheSize  = 512,
            int txCacheSize     = 1024,
            int statesCacheSize = 10000,
            bool flush          = true,
            bool readOnly       = false
            )
        {
            _logger = Log.ForContext <DefaultStore>();

            if (path is null)
            {
                _root         = new MemoryFileSystem();
                _memoryStream = new MemoryStream();
                _db           = new LiteDatabase(_memoryStream);
            }
            else
            {
                path = Path.GetFullPath(path);

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var pfs = new PhysicalFileSystem();
                _root = new SubFileSystem(
                    pfs,
                    pfs.ConvertPathFromInternal(path),
                    owned: true
                    );

                var connectionString = new ConnectionString
                {
                    Filename  = Path.Combine(path, "index.ldb"),
                    Journal   = journal,
                    CacheSize = indexCacheSize,
                    Flush     = flush,
                };

                if (readOnly)
                {
                    connectionString.Mode = FileMode.ReadOnly;
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
                         Type.GetType("Mono.Runtime") is null)
                {
                    // macOS + .NETCore doesn't support shared lock.
                    connectionString.Mode = FileMode.Exclusive;
                }

                _db = new LiteDatabase(connectionString);
            }

            lock (_db.Mapper)
            {
                _db.Mapper.RegisterType(
                    hash => hash.ToByteArray(),
                    b => new HashDigest <SHA256>(b));
                _db.Mapper.RegisterType(
                    txid => txid.ToByteArray(),
                    b => new TxId(b));
                _db.Mapper.RegisterType(
                    address => address.ToByteArray(),
                    b => new Address(b.AsBinary));
            }

            _root.CreateDirectory(TxRootPath);
            _txs = new SubFileSystem(_root, TxRootPath, owned: false);
            _root.CreateDirectory(BlockRootPath);
            _blocks = new SubFileSystem(_root, BlockRootPath, owned: false);

            _compress    = compress;
            _txCache     = new LruCache <TxId, object>(capacity: txCacheSize);
            _blockCache  = new LruCache <HashDigest <SHA256>, BlockDigest>(capacity: blockCacheSize);
            _statesCache = new LruCache <HashDigest <SHA256>, IImmutableDictionary <Address, IValue> >(
                capacity: statesCacheSize
                );
            _lastStateRefCaches =
                new Dictionary <Guid, LruCache <Address, Tuple <HashDigest <SHA256>, long> > >();

            _codec = new Codec();
        }
Esempio n. 21
0
        /// <summary>
        /// Generates the PInvoke layer from Monocypher C header files.
        /// - Raw functions
        /// - The same functions using Span&lt;T&gt; when possible
        /// </summary>
        public void GeneratePInvoke()
        {
            var srcFolder  = Path.Combine(MonocypherFolder, "src");
            var destFolder = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\..\..\Monocypher"));

            if (!Directory.Exists(srcFolder))
            {
                throw new DirectoryNotFoundException($"The source folder `{srcFolder}` doesn't exist");
            }
            if (!Directory.Exists(destFolder))
            {
                throw new DirectoryNotFoundException($"The destination folder `{destFolder}` doesn't exist");
            }


            var marshalNoFreeNative = new CSharpMarshalAttribute(CSharpUnmanagedKind.CustomMarshaler)
            {
                MarshalTypeRef = "typeof(UTF8MarshallerNoFree)"
            };

            var csOptions = new CSharpConverterOptions()
            {
                DefaultClassLib                  = "Monocypher",
                DefaultNamespace                 = "Monocypher",
                DefaultOutputFilePath            = "/Monocypher.generated.cs",
                DefaultDllImportNameAndArguments = "MonocypherDll",
                GenerateAsInternal               = false,
                DispatchOutputPerInclude         = false,

                MappingRules =
                {
                    e => e.Map <CppField>("crypto_blake2b_vtable").Discard(),
                    e => e.Map <CppField>("crypto_sha512_vtable").Discard(),
                }
            };

            csOptions.Plugins.Insert(0, new FixedArrayTypeConverter());
            csOptions.IncludeFolders.Add(srcFolder);
            var files = new List <string>()
            {
                Path.Combine(srcFolder, "monocypher.h"),
                Path.Combine(srcFolder, "optional", "monocypher-ed25519.h"),
            };

            var csCompilation = CSharpConverter.Convert(files, csOptions);

            if (csCompilation.HasErrors)
            {
                foreach (var message in csCompilation.Diagnostics.Messages)
                {
                    Console.Error.WriteLine(message);
                }
                Console.Error.WriteLine("Unexpected parsing errors");
                Environment.Exit(1);
            }


            var monocypher = csCompilation.Members.OfType <CSharpGeneratedFile>().First().Members.OfType <CSharpNamespace>().First().Members.OfType <CSharpClass>().First();

            ProcessInvokeFunctions(monocypher);

            var fs = new PhysicalFileSystem();

            {
                var subfs      = new SubFileSystem(fs, fs.ConvertPathFromInternal(destFolder));
                var codeWriter = new CodeWriter(new CodeWriterOptions(subfs));
                csCompilation.DumpTo(codeWriter);
            }
        }
Esempio n. 22
0
        public ExtendObject TryInstall(string extendName, bool isPrivate = false)
        {
            if (extendName == null)
            {
                throw new ArgumentNullException(nameof(extendName));
            }
            var extend = extendName;

            string version        = null;
            var    indexOfVersion = extend.IndexOf('@');

            if (indexOfVersion > 0)
            {
                extend  = extend.Substring(0, indexOfVersion);
                version = extend.Substring(indexOfVersion + 1);
            }

            //var themePrivatePath = PrivateExtendsFolder / extendName;
            var         themeSiteDir = new DirectoryEntry(Site.MetaFileSystem, ExtendsFolder / extendName);
            var         themeTempDir = new DirectoryEntry(Site.TempMetaFileSystem, ExtendsFolder / extendName);
            IFileSystem extendPath   = null;

            if (themeSiteDir.Exists)
            {
                extendPath = new SubFileSystem(themeSiteDir.FileSystem, themeSiteDir.Path);
            }
            else if (themeTempDir.Exists)
            {
                extendPath = new SubFileSystem(themeTempDir.FileSystem, themeSiteDir.Path);
            }

            if (extendPath != null)
            {
                return(new ExtendObject(Site, extendName, extend, version, null, null, extendPath));
            }

            extendPath = isPrivate
                ? themeTempDir.FileSystem.GetOrCreateSubFileSystem(themeSiteDir.Path)
                : themeSiteDir.FileSystem.GetOrCreateSubFileSystem(themeSiteDir.Path);

            if (Providers.Count == 0)
            {
                Site.Error($"Unable to find the extension/theme [{extend}]. No provider list installed.");
                return(null);
            }

            foreach (var extendDesc in FindAll())
            {
                if (extendDesc.Name == extend)
                {
                    if (extendDesc.Provider.TryInstall(Site, extend, version, extendPath))
                    {
                        return(new ExtendObject(Site, extendName, extend, version, extendDesc.Description, extendDesc.Url, extendPath));
                    }
                    return(null);
                }
            }

            Site.Error($"Unable to find the extension/theme [{extend}] locally from [{themeSiteDir}] or [{themeTempDir}] or from the provider list [{string.Join(",", Providers.Select(t => t.Name))}]");
            return(null);
        }
Esempio n. 23
0
        public static void ProcessFile(string inputFile, string outputPath, string outputFile, string defaultNamespace, string defaultClass)
        {
            inputFile = Path.GetFullPath(inputFile);

            Console.WriteLine($"Processing file '{Path.GetFileName(inputFile)}'...");

            //Writing
            var converterOptions = new CSharpConverterOptions()
            {
                DefaultNamespace                 = defaultNamespace,
                DefaultClassLib                  = defaultClass,
                DefaultOutputFilePath            = outputFile,
                DefaultDllImportNameAndArguments = "Library",
                DispatchOutputPerInclude         = false,
                GenerateEnumItemAsFields         = false,
                TypedefCodeGenKind               = CppTypedefCodeGenKind.NoWrap,

                MappingRules =
                {
                    //Remove prefixes from elements' names.
                    e => e.MapAll <CppElement>().CppAction((converter,                                                        element) => {
                        if (element is ICppMember member)
                        {
                            string prefix = member switch {
                                CppType _ => "IPL",
                                CppEnumItem _ => "IPL_",
                                _ => null
                            };

                            if (prefix != null)
                            {
                                member.Name = StringUtils.Capitalize(StringUtils.RemovePrefix(member.Name,                    prefix));
                            }
                        }
                    }).CSharpAction((converter,                                                                               element) => {
                        if (element is CSharpMethod method)
                        {
                            const string Prefix = "ipl";

                            string oldName = method.Name;
                            string newName = StringUtils.Capitalize(StringUtils.RemovePrefix(oldName,                         Prefix));

                            //Add an EntryPoint parameter to the DllImportAttribute, so that this rename doesn't break anything.
                            if (method.Attributes.FirstOrDefault(attrib => attrib is CSharpDllImportAttribute) is CSharpDllImportAttribute dllImportAttribute)
                            {
                                dllImportAttribute.EntryPoint = $@"""{oldName}""";
                            }

                            method.Name = newName;
                        }
                    }),

                    //Replace the bool enum with an actual bool.
                    e => e.Map <CppEnum>("Bool").Discard(),
                    e => e.MapAll <CppDeclaration>().CSharpAction((converter,                                                 element) => {
                        CSharpType type;
                        Action <CSharpType> setType;

                        if (element is CSharpField field)
                        {
                            type    = field.FieldType;
                            setType = value => field.FieldType = value;
                        }
                        else if (element is CSharpParameter parameter)
                        {
                            type    = parameter.ParameterType;
                            setType = value => parameter.ParameterType = value;
                        }
                        else
                        {
                            return;
                        }

                        if (type is CSharpFreeType freeType && freeType.Text == "unsupported_type /* enum Bool {...} */")
                        {
                            var boolean = converter.GetCSharpType(CppPrimitiveType.Bool,                                      element);

                            setType(boolean);

                            if (boolean is CSharpTypeWithAttributes typeWithAttributes)
                            {
                                foreach (CSharpMarshalAttribute attribute in typeWithAttributes.Attributes.Where(a => a is CSharpMarshalAttribute))
                                {
                                    attribute.UnmanagedType = CSharpUnmanagedKind.U4;
                                }
                            }
                        }
                    }),

                    //Rename enum elements from SCREAMING_SNAKECASE to LameupperCamelcase. There are manual fixes below, for cases where words aren't separated.
                    e => e.MapAll <CppEnumItem>().CppAction((converter,                                                       element) => {
                        var enumItem = (CppEnumItem)element;

                        string name     = enumItem.Name;
                        string[] splits = name.Split('_');

                        if (splits.Length > 1)
                        {
                            string prefix = splits[0];

                            //Remove (potentially partial) prefixes of enum's name on its items' names.
                            if (name.Length > prefix.Length + 1 && name.StartsWith(prefix,                                    StringComparison.InvariantCultureIgnoreCase))
                            {
                                name   = name.Substring(prefix.Length + 1);
                                splits = name.Split('_');
                            }

                            //Capitalize each part
                            for (int i = 0; i < splits.Length; i++)
                            {
                                string split = splits[i];
                                char[] chars = split.ToCharArray();

                                for (int j = 0; j < chars.Length; j++)
                                {
                                    chars[j] = j == 0 ? char.ToUpper(chars[j]) : char.ToLower(chars[j]);
                                }

                                splits[i] = new string(chars);
                            }

                            name = string.Join(string.Empty,                                                                  splits);
                        }

                        enumItem.Name = name;
                    }),

                    //Fix weird 'ref void' parameters.
                    e => e.MapAll <CppParameter>().CSharpAction((converter,                                                   element) => {
                        var parameter     = (CSharpParameter)element;
                        var parameterType = parameter.ParameterType;

                        if (parameterType is CSharpRefType refType && refType.ElementType is CSharpPrimitiveType primitiveType && primitiveType.Kind == CSharpPrimitiveKind.Void)
                        {
                            parameter.ParameterType = CSharpPrimitiveType.IntPtr;
                        }
                    }),

                    //Turn some 'ref' parameters to 'out' or 'in' based on \param documentation.
                    e => e.MapAll <CppParameter>().CSharpAction((converter,                                                   element) => {
                        var parameter = (CSharpParameter)element;

                        if (!(parameter.ParameterType is CSharpRefType refParameterType))
                        {
                            return;
                        }

                        if (!(element.Parent is CSharpMethod method) || !(method.CppElement is CppFunction function))
                        {
                            return;
                        }

                        if (!(function.Comment?.Children?.FirstOrDefault(c => c is CppCommentParamCommand pc && pc.ParamName == parameter.Name) is CppCommentParamCommand parameterComment))
                        {
                            return;
                        }

                        if (!(parameterComment?.Children?.FirstOrDefault() is CppCommentParagraph paragraph))
                        {
                            return;
                        }

                        string paragraphText = paragraph.ToString().Trim();

                        if (paragraphText.StartsWith("[out]"))
                        {
                            refParameterType.Kind = CSharpRefKind.Out;
                        }
                        else if (paragraphText.StartsWith("[in]"))                            //Never actually used
                        {
                            refParameterType.Kind = CSharpRefKind.In;
                        }
                    }),

                    //Turn a 2D fixed array into an 1D one.
                    e => e.Map <CppField>("Matrix4x4::elements").Type("float",                                       16),

                    //Manually fix casing on some enum properties. This could theoretically be made automatic through crazy dictionary-based algorithms, but that's overkill.
                    e => e.Map <CppEnumItem>("Error::Outofmemory").Name("OutOfMemory"),
                    e => e.Map <CppEnumItem>("SceneType::Radeonrays").Name("RadeonRays"),
                    e => e.Map <CppEnumItem>("ConvolutionType::Trueaudionext").Name("TrueAudioNext"),
                    e => e.Map <CppEnumItem>("ChannelLayout::Fivepointone").Name("FivePointOne"),
                    e => e.Map <CppEnumItem>("ChannelLayout::Sevenpointone").Name("SevenPointOne"),
                    e => e.Map <CppEnumItem>("AmbisonicsOrdering::Fursemalham").Name("FurseMalham"),
                    e => e.Map <CppEnumItem>("AmbisonicsNormalization::Fursemalham").Name("FurseMalham"),
                    e => e.Map <CppEnumItem>("AmbisonicsNormalization::Sn3d").Name("SN3D"),
                    e => e.Map <CppEnumItem>("AmbisonicsNormalization::N3d").Name("N3D"),
                    e => e.Map <CppEnumItem>("DistanceAttenuationModelType::Inversedistance").Name("InversedDistance"),
                    e => e.Map <CppEnumItem>("DirectOcclusionMode::Notransmission").Name("NoTransmission"),
                    e => e.Map <CppEnumItem>("DirectOcclusionMode::Transmissionbyvolume").Name("TransmissionByVolume"),
                    e => e.Map <CppEnumItem>("DirectOcclusionMode::Transmissionbyfrequency").Name("TransmissionByFrequency"),
                    e => e.Map <CppEnumItem>("BakedDataType::Staticsource").Name("StaticSource"),
                    e => e.Map <CppEnumItem>("BakedDataType::Staticlistener").Name("StaticListener"),
                    e => e.Map <CppEnumItem>("BakedDataType::Uniformfloor").Name("UniformFloor"),
                }
            };

            converterOptions.IncludeFolders.Add(Path.GetDirectoryName(inputFile));

            var compilation = CSharpConverter.Convert(new List <string> {
                inputFile
            }, converterOptions);

            if (compilation.HasErrors)
            {
                foreach (var message in compilation.Diagnostics.Messages)
                {
                    if (message.Type == CppLogMessageType.Error)
                    {
                        Console.WriteLine(message);
                    }
                }

                Console.ReadKey();

                return;
            }

            using var fileSystem    = new PhysicalFileSystem();
            using var subFileSystem = new SubFileSystem(fileSystem, fileSystem.ConvertPathFromInternal(outputPath));

            var codeWriterOptions = new CodeWriterOptions(subFileSystem);
            var codeWriter        = new CodeWriter(codeWriterOptions);

            compilation.DumpTo(codeWriter);
        }
Esempio n. 24
0
        public async Task LargeFileTest()
        {
            long filesize = 10L * 1024 * 1024 * 1024;
            var  testRoot = "I:\\Personal Cloud Test\\";
            int  parts    = 128;
            var  partsize = filesize / parts;


            if (((filesize / parts) % 256) != 0)
            {
#pragma warning disable CA1303                                           // Do not pass literals as localized parameters
                Assert.Fail("filesize/parts must be a multiple of 256"); //otherwise you have to rewrite TestStream
#pragma warning restore CA1303                                           // Do not pass literals as localized parameters
            }
            Directory.CreateDirectory(testRoot);

#pragma warning disable CA2000 // Dispose objects before losing scope
            var fs = new PhysicalFileSystem();
#pragma warning restore CA2000 // Dispose objects before losing scope
            var subfs = new SubFileSystem(fs, fs.ConvertPathFromInternal(testRoot), true);

            using var server = new HttpProvider(100, subfs);
            server.Start();

            using var client = new TopFolderClient($"http://localhost:100", new byte[32], "");

            //if(false)
            {
                try
                {
                    await client.DeleteAsync("test.txt").ConfigureAwait(false);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch
                {
                }
                using var teststrm = new TestStream(filesize);
                await client.WriteFileAsync("test.txt", teststrm).ConfigureAwait(false);
                await TestRead("test.txt", filesize, parts, client).ConfigureAwait(false);

                await Task.Delay(1000).ConfigureAwait(false);

                try
                {
                    await client.DeleteAsync("test.txt").ConfigureAwait(false);
                }
                catch
                {
                }
            }

            //if (false)
            {
                try
                {
                    await client.DeleteAsync("test1.txt").ConfigureAwait(false);
                }
                catch
                {
                }
                var origpart = parts;
                if ((filesize % parts) != 0)
                {
                    parts++;
                }
                {
                    using var teststrm = new TestStream(partsize);
                    await client.WriteFileAsync("test1.txt", teststrm).ConfigureAwait(false);
                }
                for (int i = 1; i < origpart; i++)
                {
                    using var teststrm = new TestStream(partsize);
                    await client.WritePartialFileAsync("test1.txt", partsize *i, partsize, teststrm).ConfigureAwait(false);
                }
                if (origpart < parts)
                {
                    partsize           = filesize % origpart;
                    using var teststrm = new TestStream(partsize);
                    await client.WritePartialFileAsync("test1.txt", partsize *origpart, partsize, teststrm).ConfigureAwait(false);
                }
                await TestRead("test1.txt", filesize, parts, client).ConfigureAwait(false);

                await Task.Delay(1000).ConfigureAwait(false);

                try
                {
                    await client.DeleteAsync("test1.txt").ConfigureAwait(false);
                }
                catch
#pragma warning restore CA1031 // Do not catch general exception types
                {
                }
            }
        }