Exists() public static method

public static Exists ( string path ) : bool
path string
return bool
Example #1
0
    /// <summary>
    ///     Checks whether a certain sub-directory exists.
    /// </summary>
    /// <param name="path">The path of the directory.</param>
    /// <returns>
    ///     <see langword="true" /> if the directory exists, <see langword="false" /> otherwise.
    /// </returns>
    /// <exception cref="ArgumentNullException">
    ///     <paramref name="path" /> is <see langword="null" /> (<see langword="Nothing" />
    ///     in Visual Basic).
    /// </exception>
    public bool Exists(string path)
    {
        _ = Requires.NotNullOrWhiteSpace(
            path,
            nameof(path));

        return(FSDir.Exists(path));
    }
Example #2
0
    public bool SetWorkshopItemContentFolder(ulong updateHandle, string contentFolder)
    {
        if (!Directory.Exists(contentFolder))
        {
            throw new ArgumentException("content folder doesn't exist");
        }

        return(Steam.SetItemContent(updateHandle, Path.GetFullPath(contentFolder)));
    }
Example #3
0
 // This method decides what action to take based on the type of
 //   file we are looking at
 public static void  doFile(FileInfo f)
 {
     // If this is a directory, walk each file/dir in that directory
     if (Directory.Exists(f.FullName))
     {
         string[] files = Directory.GetFileSystemEntries(f.FullName);
         for (int i = 0; i < files.Length; i++)
         {
             doFile(new FileInfo(f.FullName + "\\" + files[i]));
         }
     }
     else if ((f.Name.Length > 5) && f.Name.Substring(f.Name.Length - 5).Equals(".java"))
     {
         Console.Error.WriteLine("   " + f.FullName);
         parseFile(f.Name, new FileStream(f.FullName, FileMode.Open, FileAccess.Read));
     }
 }
Example #4
0
    private void PopulatePluginAssemblies()
    {
        var list = this.GetNode(@"Panel/ItemList") as Godot.ItemList;

        if (Directory.Exists(PluginsDirectoryName))
        {
            var allFiles = Directory.GetFiles(PluginsDirectoryName, "*.dll");

            foreach (var fileName in allFiles)
            {
                var assembly = Assembly.LoadFile(fileName);

                var data = new PluginData(fileName);

                data.Monsters = new List <AbstractMonster>();
                var monsters = assembly.GetTypes().Where(t => t.BaseType == typeof(AbstractMonster));

                foreach (var monsterType in monsters)
                {
                    var instance = (AbstractMonster)monsterType.GetConstructor(new Type[0]).Invoke(new object[0]);
                    data.Monsters.Add(instance);
                }

                data.LevelGenerators = new List <ILevelGenerator>();
                var generators = assembly.GetTypes().Where(t => t.GetInterfaces().Any(i => i.Name.Contains("ILevelGenerator")));

                foreach (var generatorType in generators)
                {
                    var instance = (ILevelGenerator)generatorType.GetConstructor(new Type[0]).Invoke(new object[0]);
                    data.LevelGenerators.Add(instance);
                }

                list.AddItem(fileName);
                pluginData.Add(data);
            }
        }
        else
        {
            list.AddItem("Plugins directory doesn't exist.");
        }
    }
        /// <summary>
        ///     This initializes the provider registry with the list of package providers.
        ///     (currently a hardcoded list, soon, registry driven)
        /// </summary>
        /// <param name="request"></param>
        internal void LoadProviders(IHostApi request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var providerAssemblies = (_initialized ? Enumerable.Empty <string>() : _defaultProviders)
                                     .Concat(GetProvidersFromRegistry(Registry.LocalMachine, "SOFTWARE\\MICROSOFT\\PACKAGEMANAGEMENT"))
                                     .Concat(GetProvidersFromRegistry(Registry.CurrentUser, "SOFTWARE\\MICROSOFT\\PACKAGEMANAGEMENT"))
                                     .Concat(AutoloadedAssemblyLocations.SelectMany(location => {
                if (Directory.Exists(location))
                {
                    return(Directory.EnumerateFiles(location).Where(each => (each.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) || each.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))));
                }
                return(Enumerable.Empty <string>());
            }));

#if DEEP_DEBUG
            providerAssemblies = providerAssemblies.ToArray();

            foreach (var each in providerAssemblies)
            {
                request.Debug("possible assembly: {0}".format(each));
            }
#endif

            // find modules that have manifests
            // todo: expand this out to validate the assembly is ok for this instance of PackageManagement.
            providerAssemblies = providerAssemblies.Where(each => Manifest.LoadFrom(each).Any(manifest => Swidtag.IsSwidtag(manifest) && new Swidtag(manifest).IsApplicable(new Hashtable())));

            // add inbox assemblies (don't require manifests, because they are versioned with the core)

#if !COMMUNITY_BUILD
            // todo: these should just be strong-named references. for now, just load them from the same directory.
            providerAssemblies = providerAssemblies.Concat(new[] {
                Path.Combine(BaseDir, "Microsoft.PackageManagement.MetaProvider.PowerShell.dll"),
                Path.Combine(BaseDir, "Microsoft.PackageManagement.ArchiverProviders.dll"),
                Path.Combine(BaseDir, "Microsoft.PackageManagement.CoreProviders.dll"),
                Path.Combine(BaseDir, "Microsoft.PackageManagement.MsuProvider.dll"),
#if !CORE_CLR
                // can't load these providers here.
                Path.Combine(BaseDir, "Microsoft.PackageManagement.MsiProvider.dll"),
#endif
            });
#endif

#if DEEP_DEBUG
            providerAssemblies = providerAssemblies.ToArray();

            foreach (var each in providerAssemblies)
            {
                request.Debug("possible assembly with manifest: {0}".format(each));
            }
#endif

            providerAssemblies = providerAssemblies.OrderByDescending(each => {
                try {
                    // try to get a version from the file first
                    return((ulong)(FourPartVersion)FileVersionInfo.GetVersionInfo(each));
                } catch {
                    // otherwise we can't make a distinction.
                    return((ulong)0);
                }
            });
            providerAssemblies = providerAssemblies.Distinct(new PathEqualityComparer(PathCompareOption.FileWithoutExtension));

            // there is no trouble with loading providers concurrently.
#if DEEP_DEBUG
            providerAssemblies.SerialForEach(providerAssemblyName => {
#else
            providerAssemblies.ParallelForEach(providerAssemblyName => {
#endif
                TryToLoadProviderAssembly(providerAssemblyName, request);
            });
#if DEEP_DEBUG
            WaitForDebugger();
#endif
        }
Example #6
0
        private static string[] CollectPathsFromToolbox(string toolboxRiderRootPath, string dirName, string searchPattern,
                                                        bool isMac)
        {
            if (!Directory.Exists(toolboxRiderRootPath))
            {
                return(new string[0]);
            }

            var channelDirs = Directory.GetDirectories(toolboxRiderRootPath);
            var paths       = channelDirs.SelectMany(channelDir =>
            {
                try
                {
                    // use history.json - last entry stands for the active build https://jetbrains.slack.com/archives/C07KNP99D/p1547807024066500?thread_ts=1547731708.057700&cid=C07KNP99D
                    var historyFile = Path.Combine(channelDir, ".history.json");
                    if (File.Exists(historyFile))
                    {
                        var json  = File.ReadAllText(historyFile);
                        var build = ToolboxHistory.GetLatestBuildFromJson(json);
                        if (build != null)
                        {
                            var buildDir        = Path.Combine(channelDir, build);
                            var executablePaths = GetExecutablePaths(dirName, searchPattern, isMac, buildDir);
                            if (executablePaths.Any())
                            {
                                return(executablePaths);
                            }
                        }
                    }

                    var channelFile = Path.Combine(channelDir, ".channel.settings.json");
                    if (File.Exists(channelFile))
                    {
                        var json  = File.ReadAllText(channelFile).Replace("active-application", "active_application");
                        var build = ToolboxInstallData.GetLatestBuildFromJson(json);
                        if (build != null)
                        {
                            var buildDir        = Path.Combine(channelDir, build);
                            var executablePaths = GetExecutablePaths(dirName, searchPattern, isMac, buildDir);
                            if (executablePaths.Any())
                            {
                                return(executablePaths);
                            }
                        }
                    }

                    // changes in toolbox json files format may brake the logic above, so return all found Rider installations
                    return(Directory.GetDirectories(channelDir)
                           .SelectMany(buildDir => GetExecutablePaths(dirName, searchPattern, isMac, buildDir)));
                }
                catch (Exception e)
                {
                    // do not write to Debug.Log, just log it.
                    Logger.Warn($"Failed to get RiderPath from {channelDir}", e);
                }

                return(new string[0]);
            })
                              .Where(c => !string.IsNullOrEmpty(c))
                              .ToArray();

            return(paths);
        }
Example #7
0
        public Users(string dbUrl, string storageDir, string masterPassword) : base(dbUrl)
        {
            this.dbUrl          = dbUrl;
            this.masterPassword = masterPassword;

            var avatarDir = Path.Combine(storageDir, "avatar");

            if (!Dir.Exists(avatarDir))
            {
                Dir.CreateDirectory(avatarDir);
            }

            // API only available to authenticated users
            BeforeAsync = async(p, c) => await c.EnsureIsAuthenticatedAsync();

            // register a type
            Types["uid"] = (val) => (Regex.IsMatch(val, "^[A-Z0-9]+$")) ? val : null;

            GetAsync["/"] = async(p, c) =>
            {
                await RunBeforeAsync(null, c);

                var authUser = await c.GetAuthenticatedUserAsync();

                SqlFilter filterAuth;
                if (authUser.IsUser && c.Request.QueryString.ContainsKey("restrict"))
                {
                    filterAuth = User.GenerateFilterAuthUser(authUser);
                }
                else
                {
                    filterAuth = (new User()).FilterAuthUser(authUser);
                }
                using (DB db = await DB.CreateAsync(dbUrl))
                {
                    var result = await Model.SearchAsync <User>(db, c, filterAuth);

                    foreach (var item in result.Data)
                    {
                        await item.EnsureRightAsync(c, Right.Read, null);
                    }
                    c.Response.Content = result.ToJson(c);
                }
                c.Response.StatusCode = 200;
            };

            GetAsync["/current"] = async(p, c) =>
            {
                var user = await c.GetAuthenticatedUserAsync();

                if ((user == null) || !user.IsUser)
                {
                    c.Response.StatusCode = 401;
                }
                else
                {
                    c.Response.StatusCode          = 302;
                    c.Response.Headers["location"] = user.user.id;
                }
            };

            GetAsync["/current/isauthenticated"] = async(p, c) =>
            {
                var user = await c.GetAuthenticatedUserAsync();

                if (user == null)
                {
                    c.Response.StatusCode = 403;
                }
                else
                {
                    c.Response.StatusCode = 200;
                }
            };

            PostAsync["/{uid:uid}/upload/avatar"] = async(p, c) =>
            {
                var uid = (string)p["uid"];

                var oldUser = new User {
                    id = uid
                };
                using (var db = await DB.CreateAsync(dbUrl))
                {
                    if (!await oldUser.LoadAsync(db, true))
                    {
                        oldUser = null;
                    }
                }

                //var oldUser = await GetUserAsync(uid);
                if (oldUser == null)
                {
                    return;
                }

                await c.EnsureHasRightsOnUserAsync(oldUser, true, true, false);

                var           reader = c.Request.ReadAsMultipart();
                MultipartPart part;
                while ((part = await reader.ReadPartAsync()) != null)
                {
                    if (part.Headers.ContainsKey("content-disposition") && part.Headers.ContainsKey("content-type"))
                    {
                        if ((part.Headers["content-type"] != "image/jpeg") &&
                            (part.Headers["content-type"] != "image/png") &&
                            (part.Headers["content-type"] != "image/svg+xml"))
                        {
                            continue;
                        }

                        var disposition = ContentDisposition.Decode(part.Headers["content-disposition"]);
                        if (disposition.ContainsKey("name") && (disposition["name"] == "image"))
                        {
                            var dir = DirExt.CreateRecursive(Path.Combine(
                                                                 avatarDir, uid[0].ToString(), uid[1].ToString(), uid[2].ToString()));
                            var ext    = ".jpg";
                            var format = "jpeg";
                            //if ((part.Headers["content-type"] == "image/png") || (part.Headers["content-type"] == "image/svg+xml"))
                            //{
                            //	ext = ".png";
                            //	format = "png";
                            //}

                            var name = StringExt.RandomString(16) + "_" + uid + ext;

                            // crop / resize / convert the image using ImageMagick
                            var startInfo = new ProcessStartInfo("/usr/bin/convert", "- -auto-orient -strip -set option:distort:viewport \"%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]\" -distort SRT 0 +repage -quality 80 -resize 256x256 " + format + ":" + Path.Combine(dir.FullName, name));
                            startInfo.RedirectStandardOutput = false;
                            startInfo.RedirectStandardInput  = true;
                            startInfo.UseShellExecute        = false;
                            var process = new Process();
                            process.StartInfo = startInfo;
                            process.Start();

                            // read the file stream and send it to ImageMagick
                            await part.Stream.CopyToAsync(process.StandardInput.BaseStream);

                            process.StandardInput.Close();

                            process.WaitForExit();
                            process.Dispose();

                            c.Response.StatusCode = 200;
                            var userDiff = new User {
                                id = uid, avatar = name
                            };
                            using (DB db = await DB.CreateAsync(dbUrl))
                            {
                                await userDiff.UpdateAsync(db);

                                await userDiff.LoadAsync(db, true);
                            }
                            c.Response.Content = userDiff;

                            if ((oldUser.avatar != null) && (oldUser.avatar != "empty"))
                            {
                                var oldFile = Path.Combine(avatarDir, uid[0].ToString(), uid[1].ToString(), uid[2].ToString(), Path.GetFileName(oldUser.avatar));
                                if (File.Exists(oldFile))
                                {
                                    File.Delete(oldFile);
                                }
                            }
                        }
                    }
                }
            };

            GetAsync["/{uid:uid}/avatar"] = async(p, c) =>
            {
                var uid = (string)p["uid"];

                var user = new User {
                    id = uid
                };
                using (var db = await DB.CreateAsync(dbUrl))
                {
                    if (!await user.LoadAsync(db, true))
                    {
                        user = null;
                    }
                }

                if (user == null)
                {
                    return;
                }

                string avatarPath;
                if ((user.avatar == null) || (user.avatar == "empty"))
                {
                    if (user.gender == null)
                    {
                        avatarPath = "/avatar/avatar_neutre.svg";
                    }
                    else if (user.gender == Gender.M)
                    {
                        avatarPath = "/avatar/avatar_masculin.svg";
                    }
                    else
                    {
                        avatarPath = "/avatar/avatar_feminin.svg";
                    }
                }
                else
                {
                    avatarPath = "/api/avatar/user/" +
                                 uid.Substring(0, 1) + "/" + uid.Substring(1, 1) + "/" +
                                 uid.Substring(2, 1) + "/" + user.avatar;
                }

                c.Response.StatusCode          = 302;
                c.Response.Headers["location"] = avatarPath;
            };
        }
        private void process(object o)
        {
            try
            {
                while (true)
                {
                    DownloadRequest currentItem = null;

                    bool QueueEmpty = false;

                    lock (sync)
                        QueueEmpty = queue.Count == 0;
                    if (QueueEmpty && null != OnWorkerFinished)
                    {
                        OnWorkerFinished(this, EventArgs.Empty);
                    }

                    lock (sync)
                    {
                        if (queue.Count > 0)
                        {
                            currentItem = queue.Dequeue();
                        }
                        if (null == currentItem)
                        {
                            isComplete = true;
                            return;
                        }
                    }

                    currentItem.State = DownloadRequestState.Downloading;

                    if (currentItem.IsFolder)
                    {
                        length   = 0;
                        position = 0;
                        status   = "Downloading folder info for " + currentItem.FullPath;
                        //Item is a folder - Just get the folder items and add them to the queue.
                        var verb = new BrowseVerb(null);
                        verb.Path = currentItem.FullPath;
                        //Always get the latest info.
                        verb.NoCache = true;

                        var client = new Client(null);

                        if (client.Execute(verb, remoteNode))
                        {
                            currentItem.State = DownloadRequestState.Downloaded;
                            var newItems = new List <DownloadRequest>();

                            foreach (BrowsingFile item in verb.Results)
                            {
                                newItems.Add(new DownloadRequest
                                {
                                    Added       = DateTime.Now,
                                    ClientID    = remoteNode.ID,
                                    FullPath    = currentItem.FullPath + "/" + item.Name,
                                    IsFolder    = item.IsFolder,
                                    LocalPath   = currentItem.LocalPath + "\\" + currentItem.FileName,
                                    NextTryTime = 0,
                                    Nickname    = remoteNode.Nickname,
                                    Size        = item.Size,
                                    State       = DownloadRequestState.None
                                });
                            }
                            model.DownloadQueue.List.AddRange(newItems);
                        }
                        else
                        {
                            currentItem.State       = DownloadRequestState.Error;
                            currentItem.NextTryTime = Environment.TickCount + Model.DOWNLOAD_RETRY_TIME;
                        }
                    }
                    else
                    {
                        MemoryBuffer buffer = bufferService.GetBuffer();
                        buffer.SetDataLocation(0, buffer.Data.Length);
                        //Item is a file - download it
                        try
                        {
                            length            = currentItem.Size;
                            position          = 0;
                            status            = currentItem.Nickname + " - " + currentItem.FileName + " - Connecting..";
                            currentItem.State = DownloadRequestState.Downloading;

                            string mainPath       = string.Empty;
                            string mainFolder     = string.Empty;
                            string incompletePath = string.Empty;
                            ;
                            string incompleteFolder = string.Empty;

                            //Build paths
                            var mainsb = new StringBuilder();
                            mainsb.Append(model.DownloadFolder);
                            if (!string.IsNullOrEmpty(currentItem.LocalPath))
                            {
                                mainsb.Append("\\");
                                mainsb.Append(currentItem.LocalPath);
                            }
                            mainFolder = mainsb.ToString();
                            mainsb.Append("\\");
                            mainsb.Append(currentItem.FileName);
                            mainPath = mainsb.ToString();

                            var incompletesb = new StringBuilder();
                            incompletesb.Append(model.IncompleteFolder);
                            if (!string.IsNullOrEmpty(currentItem.LocalPath))
                            {
                                incompletesb.Append("\\");
                                incompletesb.Append(currentItem.LocalPath);
                            }
                            incompleteFolder = incompletesb.ToString();
                            incompletesb.Append("\\");
                            incompletesb.Append(currentItem.FileName);
                            incompletePath = incompletesb.ToString();


                            FileStream fileStream = null;

                            //Check to see if the file already exists.
                            if (File.Exists(mainPath))
                            {
                                //File exists in the download directory.
                                fileStream     = File.Open(mainPath, FileMode.Open, FileAccess.Write, FileShare.None);
                                incompletePath = mainPath;
                            }
                            else
                            {
                                if (!Directory.Exists(incompleteFolder))
                                {
                                    Directory.CreateDirectory(incompleteFolder);
                                }

                                //Else resume or just create
                                fileStream = File.Open(incompletePath, FileMode.OpenOrCreate, FileAccess.Write,
                                                       FileShare.None);
                            }

                            var req =
                                (HttpWebRequest)
                                WebRequest.Create(Multiplexor.Encode(getDownloadUrl(), "GET", currentItem.FullPath));
                            req.UserAgent = Model.AppVersion;
                            req.Headers.Add("FAP-SOURCE", model.LocalNode.ID);

                            // req.Timeout = 300000;
                            // req.ReadWriteTimeout = 3000000;
                            //If we are resuming then add range
                            long resumePoint = 0;
                            if (fileStream.Length != 0)
                            {
                                //Yes Micrsoft if you read this...  OH WHY IS ADDRANGE ONLY AN INT?? We live in an age where we might actually download more than 2gb
                                //req.AddRange(fileStream.Length);

                                //Hack
                                MethodInfo method = typeof(WebHeaderCollection).GetMethod("AddWithoutValidate",
                                                                                          BindingFlags.Instance |
                                                                                          BindingFlags.NonPublic);
                                string key = "Range";
                                string val = string.Format("bytes={0}", fileStream.Length);
                                method.Invoke(req.Headers, new object[] { key, val });
                                position    = fileStream.Length;
                                resumePoint = fileStream.Length;
                                //Seek to the end of the file
                                fileStream.Seek(fileStream.Length, SeekOrigin.Begin);
                            }

                            var resp = (HttpWebResponse)req.GetResponse();

                            if (resp.StatusCode == HttpStatusCode.OK)
                            {
                                using (Stream responseStream = resp.GetResponseStream())
                                {
                                    var tokenizer       = new StreamTokenizer(Encoding.ASCII, "|");
                                    var utilisedBuffers = new List <MemoryBuffer>();
                                    try
                                    {
                                        bool streamIncomplete = true;

                                        while (streamIncomplete)
                                        {
                                            MemoryBuffer tokenBuffer = bufferService.GetSmallBuffer();
                                            //utilisedBuffers.Add(tokenBuffer);
                                            //Receive data
                                            tokenBuffer.SetDataLocation(0,
                                                                        responseStream.Read(tokenBuffer.Data, 0,
                                                                                            tokenBuffer.DataSize));
                                            tokenizer.ReceiveData(tokenBuffer);

                                            if (tokenizer.ContainsCommand())
                                            {
                                                string data          = tokenizer.GetCommand();
                                                int    queuePosition = int.Parse(data);

                                                if (queuePosition == 0)
                                                {
                                                    if (tokenizer.Buffers.Count > 0)
                                                    {
                                                        LogManager.GetLogger("faplog").Warn(
                                                            "Queue info overlaps with file data.  File: {0}",
                                                            currentItem.FileName);
                                                        //Due to the way chunks are delivered we should never get here
                                                        //Just incase write left over data
                                                        foreach (MemoryBuffer buff in tokenizer.Buffers)
                                                        {
                                                            fileStream.Write(buff.Data, 0, buff.DataSize);
                                                        }
                                                    }

                                                    status = currentItem.Nickname + " - " + currentItem.FileName + " - " +
                                                             Utility.FormatBytes(currentItem.Size);

                                                    DateTime start = DateTime.Now;

                                                    while (true)
                                                    {
                                                        //Receive file
                                                        int read = responseStream.Read(buffer.Data, 0,
                                                                                       buffer.Data.Length);
                                                        if (read == 0)
                                                        {
                                                            streamIncomplete = false;
                                                            break;
                                                        }
                                                        else
                                                        {
                                                            fileStream.Write(buffer.Data, 0, read);
                                                            position += read;
                                                            netSpeed.PutData(read);
                                                        }
                                                    }

                                                    //Add log of transfer
                                                    double seconds = (DateTime.Now - start).TotalSeconds;
                                                    var    rxlog   = new TransferLog();
                                                    rxlog.Added     = currentItem.Added;
                                                    rxlog.Completed = DateTime.Now;
                                                    rxlog.Filename  = currentItem.FileName;
                                                    rxlog.Nickname  = currentItem.Nickname;
                                                    rxlog.Path      = currentItem.FolderPath;
                                                    rxlog.Size      = currentItem.Size - resumePoint;
                                                    if (0 != seconds)
                                                    {
                                                        rxlog.Speed = (int)(rxlog.Size / seconds);
                                                    }
                                                    model.CompletedDownloads.Add(rxlog);
                                                }
                                                else
                                                {
                                                    //Queued
                                                    status = currentItem.Nickname + " - " + currentItem.FileName +
                                                             " - Queue position " + queuePosition;
                                                }
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        foreach (MemoryBuffer buff in utilisedBuffers)
                                        {
                                            bufferService.FreeBuffer(buff);
                                        }
                                        tokenizer.Dispose();
                                    }
                                }
                            }

                            resp.Close();
                            model.DownloadQueue.List.Remove(currentItem);
                            currentItem.State = DownloadRequestState.Downloaded;
                            fileStream.Close();
                            fileStream.Dispose();
                            //Move from the incomplete folder.
                            if (mainPath != incompletePath)
                            {
                                if (!Directory.Exists(mainFolder))
                                {
                                    Directory.CreateDirectory(mainFolder);
                                }
                                File.Move(incompletePath, mainPath);
                            }
                            status = currentItem.Nickname + " - Complete: " + currentItem.FileName;
                            resp.Close();
                        }
                        catch
                        {
                            currentItem.State       = DownloadRequestState.Error;
                            currentItem.NextTryTime = Environment.TickCount + Model.DOWNLOAD_RETRY_TIME;
                        }
                        finally
                        {
                            bufferService.FreeBuffer(buffer);
                        }
                    }
                }
            }
            catch
            {
                //Something went very wrong.  Clear the queue and die.
                lock (sync)
                {
                    isComplete = true;
                    foreach (DownloadRequest v in queue)
                    {
                        v.State = DownloadRequestState.None;
                    }
                    queue.Clear();
                }
            }
        }
        public void IndexContentInFolder(string directoryPath, IFileSelector fileSelector)
        {
            if (!Directory.Exists(directoryPath))
            {
                throw new DirectoryNotFoundException($"Unable to find directory {directoryPath}");
            }

            _indexableFiles = new ConcurrentQueue <FileInfo>();
            FilesIndexed    = 0;
            TotalFilesFound = 0;

            _ioCompleted = false;
            CancellationTokenSource cancelIndexingTokenSource = new CancellationTokenSource();
            CancellationToken       cancelIndexingToken       = cancelIndexingTokenSource.Token;

            using (IndexWriter indexWriter = new IndexWriter(
                       new SimpleFSDirectory(_indexDirectory),
                       new StandardAnalyzer(Version.LUCENE_30),
                       false,
                       IndexWriter.MaxFieldLength.UNLIMITED))
                using (Searcher searcher = new IndexSearcher(indexWriter.Directory))
                {
                    Task indexDirectoryTask = Task.Run(() =>
                    {
                        cancelIndexingToken.ThrowIfCancellationRequested();
                        IndexDirectory(new DirectoryInfo(directoryPath), fileSelector);
                    }, cancelIndexingToken);

                    HashSet <string> allKnownPaths = new HashSet <string>();

                    TopDocs allTopDocs = searcher.Search(new MatchAllDocsQuery(), MaxSearchResults);
                    foreach (ScoreDoc scoreDoc in allTopDocs.ScoreDocs)
                    {
                        if (_cancel)
                        {
                            break;
                        }

                        Document doc  = searcher.Doc(scoreDoc.Doc);
                        String   path = doc.GetField("Path").StringValue;
                        if (allKnownPaths.Contains(path))
                        {
                            continue;
                        }

                        FileInfo fileInfo = new FileInfo(path);

                        if (!fileSelector.IsFileValid(fileInfo) || !fileSelector.IsDirectoryValid(fileInfo.Directory))
                        {
                            continue;
                        }

                        long lastWrite = long.Parse(doc.GetField("LastWrite").StringValue);

                        if (fileInfo.Exists)
                        {
                            IndexFile(indexWriter, searcher, fileInfo, lastWrite);
                            allKnownPaths.Add(path);
                        }
                        else
                        {
                            var query = new PhraseQuery();
                            query.Add(new Term("Path", path));
                            indexWriter.DeleteDocuments(query);
                        }

                        ++FilesIndexed;
                    }

                    // How many indexers to spawn at first
                    int initialIndexersCount = 2;
                    // How much files must be queued before spawning help
                    int indexersScalingFactor = 1000;
                    // Max amount of indexers
                    int maxIndexersCount = 5;

                    List <Thread> threads = new List <Thread>();
                    for (int i = 0; i < initialIndexersCount; i++)
                    {
                        Thread t = new Thread(() => IndexLoop(indexWriter, searcher, allKnownPaths));
                        t.Start();
                        threads.Add(t);
                    }

                    while (true)
                    {
                        if (_cancel)
                        {
                            cancelIndexingTokenSource.Cancel();
                            _ioCompleted = true;
                            break;
                        }

                        if (indexDirectoryTask.IsCompleted)
                        {
                            _ioCompleted = true;
                            break;
                        }

                        if (threads.Count < maxIndexersCount && _indexableFiles.Count / threads.Count >= indexersScalingFactor)
                        {
                            Thread t = new Thread(() => IndexLoop(indexWriter, searcher, allKnownPaths));
                            t.Start();
                            threads.Add(t);
                        }

                        Thread.Sleep(100);
                    }

                    foreach (Thread thread in threads)
                    {
                        thread.Join();
                    }
                }
        }
Example #10
0
        //Old method
        private void getFilesBtn_Click2(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder(mapPath);

            sb.Append(territoryName + @"\");

            String territoryFolder = sb.ToString();

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

            if (territory.Terrain != null)
            {
                foreach (var part in territory.Terrain.Parts)
                {
                    addToMapList("territory", part);
                    addToFileList(part.Model.File.Path);
                }
            }

            // Get the files we need
            foreach (var lgbFile in territory.LgbFiles)
            {
                foreach (var group in lgbFile.Groups)
                {
                    if (!eventCheck(group.Name))
                    {
                        addHeaderToMapList("LGBGroup: " + group.Name);
                        foreach (var entry in group.Entries)
                        {
                            var asMdl = entry as LgbModelEntry;
                            var asGim = entry as LgbGimmickEntry;

                            string path = "";

                            //Entry is model
                            if (asMdl != null && asMdl.Model != null && asMdl.Model.Model != null)
                            {
                                string header = string.Format("LgbModelEntry,{0},{1},{2},{3},{4},{5},{6},{7},{8}",
                                                              asMdl.Header.Translation.X,
                                                              asMdl.Header.Translation.Y,
                                                              asMdl.Header.Translation.Z,
                                                              asMdl.Header.Rotation.X,
                                                              asMdl.Header.Rotation.Y,
                                                              asMdl.Header.Rotation.Z,
                                                              asMdl.Header.Scale.X,
                                                              asMdl.Header.Scale.Y,
                                                              asMdl.Header.Scale.Z);
                                addHeaderToMapList(header);
                                path = asMdl.Model.Model.File.Path;
                                //MessageBox.Show(path);
                                addToMapList(group.Name, asMdl.Model);
                                addToFileList(path);
                                addHeaderToMapList("EndLgbModelEntry");
                            }

                            //Entry is gimmick
                            if (asGim != null && asGim.Gimmick != null)
                            {
                                List <String> gimmickFileList = new List <string>();
                                getGimmickPaths(asGim.Gimmick, ref gimmickFileList);

                                addGimmickInfoToMapList(asGim);
                                ExportSgbFile(group.Name, asGim.Gimmick, 0, asGim.Header.Translation,
                                              asGim.Header.Rotation, asGim.Header.Scale);
                                addHeaderToMapList("GimmickEnd");
                            }

                            addToFileList(path);
                        }
                    }
                }
            }
        }
Example #11
0
        /// <summary>
        /// 	Creates a junction point from the specified directory to the specified target directory.
        /// </summary>
        /// <remarks>
        /// 	Only works on NTFS.
        /// </remarks>
        /// <param name = "junctionPoint">The junction point path</param>
        /// <param name = "targetDir">The target directory</param>
        /// <param name = "overwrite">If true overwrites an existing reparse point or empty directory</param>
        /// <exception cref = "IOException">Thrown when the junction point could not be created or when
        /// 	an existing directory was found and <paramref name = "overwrite" /> if false</exception>
        public static void Create(Directory junctionPoint, string targetDir, bool overwrite)
        {
            //targetDir = Path.GetFullPath(targetDir);

            if (junctionPoint.Exists())
            {
                if (!overwrite)
                    throw new IOException("Directory already exists and overwrite parameter is false.");
            }
            else
                junctionPoint.Create();

            using (var handle = OpenReparsePoint(junctionPoint.Path, EFileAccess.GenericWrite))
            {
                var targetDirBytes = Encoding.Unicode.GetBytes(NonInterpretedPathPrefix + Path.GetFullPath(targetDir));

                var reparseDataBuffer = new REPARSE_DATA_BUFFER();

                reparseDataBuffer.ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
                reparseDataBuffer.ReparseDataLength = (ushort) (targetDirBytes.Length + 12);
                reparseDataBuffer.SubstituteNameOffset = 0;
                reparseDataBuffer.SubstituteNameLength = (ushort) targetDirBytes.Length;
                reparseDataBuffer.PrintNameOffset = (ushort) (targetDirBytes.Length + 2);
                reparseDataBuffer.PrintNameLength = 0;
                reparseDataBuffer.PathBuffer = new byte[0x3ff0];
                Array.Copy(targetDirBytes, reparseDataBuffer.PathBuffer, targetDirBytes.Length);

                var inBufferSize = Marshal.SizeOf(reparseDataBuffer);
                var inBuffer = Marshal.AllocHGlobal(inBufferSize);

                try
                {
                    Marshal.StructureToPtr(reparseDataBuffer, inBuffer, false);

                    int bytesReturned;
                    var result = DeviceIoControl(handle.DangerousGetHandle(), FSCTL_SET_REPARSE_POINT,
                                                 inBuffer, targetDirBytes.Length + 20, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);

                    if (!result)
                        ThrowLastWin32Error("Unable to create junction point.");
                }
                finally
                {
                    Marshal.FreeHGlobal(inBuffer);
                }
            }
        }
        private static string FindMsBuildToolsPathOnWindows()
        {
            if (!OS.IsWindows)
            {
                throw new PlatformNotSupportedException();
            }

            // Try to find 15.0 with vswhere

            var envNames = Internal.GodotIs32Bits() ? new[] { "ProgramFiles", "ProgramW6432" } : new[] { "ProgramFiles(x86)", "ProgramFiles" };

            string vsWherePath = null;

            foreach (var envName in envNames)
            {
                vsWherePath = Environment.GetEnvironmentVariable(envName);
                if (!string.IsNullOrEmpty(vsWherePath))
                {
                    vsWherePath += "\\Microsoft Visual Studio\\Installer\\vswhere.exe";
                    if (File.Exists(vsWherePath))
                    {
                        break;
                    }
                }

                vsWherePath = null;
            }

            var vsWhereArgs = new[] { "-latest", "-products", "*", "-requires", "Microsoft.Component.MSBuild" };

            var outputArray = new Godot.Collections.Array <string>();
            int exitCode    = Godot.OS.Execute(vsWherePath, vsWhereArgs,
                                               blocking: true, output: (Godot.Collections.Array)outputArray);

            if (exitCode != 0)
            {
                return(string.Empty);
            }

            if (outputArray.Count == 0)
            {
                return(string.Empty);
            }

            var lines = outputArray[0].Split('\n');

            foreach (string line in lines)
            {
                int sepIdx = line.IndexOf(':');

                if (sepIdx <= 0)
                {
                    continue;
                }

                string key = line.Substring(0, sepIdx); // No need to trim

                if (key != "installationPath")
                {
                    continue;
                }

                string value = line.Substring(sepIdx + 1).StripEdges();

                if (string.IsNullOrEmpty(value))
                {
                    throw new FormatException("installationPath value is empty");
                }

                if (!value.EndsWith("\\"))
                {
                    value += "\\";
                }

                // Since VS2019, the directory is simply named "Current"
                string msbuildDir = Path.Combine(value, "MSBuild\\Current\\Bin");

                if (Directory.Exists(msbuildDir))
                {
                    return(msbuildDir);
                }

                // Directory name "15.0" is used in VS 2017
                return(Path.Combine(value, "MSBuild\\15.0\\Bin"));
            }

            return(string.Empty);
        }
Example #13
0
 public static bool Exists(string d) => OgDir.Exists(d);
Example #14
0
        private bool InstallAssemblyProvider(Package provider, Link link, string fastPath, BootstrapRequest request, bool deleteFile = true)
        {
            request.Verbose(Resources.Messages.InstallingPackage, fastPath);

            if (!Directory.Exists(request.DestinationPath(request)))
            {
                request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.DestinationPathNotSet);
                return(false);
            }

            string targetFilename = fastPath;
            string file           = fastPath;

            //source can be from install-packageprovider or can be from the pipeline
            if (!request.LocalSource.Any() && !fastPath.IsFile() && link != null)
            {
                targetFilename = link.Attributes[Iso19770_2.Discovery.TargetFilename];

                // download the file
                file = request.DownloadAndValidateFile(provider._swidtag);
            }

            if (string.IsNullOrWhiteSpace(targetFilename))
            {
                request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.InvalidFilename);
                return(false);
            }

            targetFilename = Path.GetFileName(targetFilename);
            if (string.IsNullOrWhiteSpace(provider.Version))
            {
                request.Error(ErrorCategory.InvalidOperation, fastPath, Resources.Messages.MissingVersion);
                return(false);
            }

            //the provider is installing to like this folder: \WindowsPowerShell\Modules\PackageManagement\ProviderAssemblies\nuget\2.8.5.127
            //... providername\version\.dll
            var versionFolder = Path.Combine(request.DestinationPath(request), provider.Name, provider.Version);

            // if version folder exists, remove it
            if (Directory.Exists(versionFolder))
            {
                RemoveDirectory(versionFolder);
            }

            // create the directory if we successfully deleted it
            if (!Directory.Exists(versionFolder))
            {
                Directory.CreateDirectory(versionFolder);
            }

            var targetFile = Path.Combine(versionFolder, targetFilename);

            if (file != null)
            {
                try
                {
                    // is that file still there?
                    if (File.Exists(targetFile))
                    {
                        request.Error(ErrorCategory.InvalidOperation, fastPath, Constants.Messages.UnableToRemoveFile, targetFile);
                        return(false);
                    }

                    request.Debug("Copying file '{0}' to '{1}'", file, targetFile);
                    try {
                        if (File.Exists(file))
                        {
                            // if this is a file
                            File.Copy(file, targetFile);
                        }
                        else if (Directory.Exists(file))
                        {
                            // if this is a directory, copy items over
                            CopyDirectory(file, versionFolder);
                        }
                    }
                    catch (Exception ex) {
                        request.Debug(ex.StackTrace);
                        return(false);
                    }

                    if (File.Exists(targetFile))
                    {
                        request.Verbose(Resources.Messages.InstalledPackage, provider.Name, targetFile);
                        request.YieldFromSwidtag(provider, fastPath);

                        //Load the provider. This is needed when a provider has dependencies. For example, if Nuget provider has a dependent foobar
                        // provider and when 'Install-PackageProvider -name NuGet', we want both NuGet and Foobar provider gets loaded.
                        PackageManagementService.LoadProviderAssembly(request, targetFile, false);
                        return(true);
                    }
                }
                finally
                {
                    if (deleteFile)
                    {
                        file.TryHardToDelete();
                    }
                }
            }

            return(false);
        }
Example #15
0
    /// Rebuilds the material on the asset.
    public static void CreateMaterial()
    {
        //clear out the main material in case it is filled in from previous import
        _mainMaterial = null;

        //Make sure the materials directoy exists before adding stuff to it
        if (!Directory.Exists(MaterialsPath))
        {
            Directory.CreateDirectory(MaterialsPath);
        }

        //Check all the files in the directory for the main material
        foreach (var file in Directory.GetFiles(MaterialsPath, "*.mat", SearchOption.TopDirectoryOnly))
        {
            if (!file.Contains(_assetName))
            {
                continue;
            }
            if (file.Contains("sft"))
            {
                _shaderName   = "LaidlawFX/Soft";
                _materialName = _assetName + "_sft_mat_vat.mat";
            }
            else if (file.Contains("rgd"))
            {
                _shaderName   = "LaidlawFX/Rigid";
                _materialName = _assetName + "_rgd_mat_vat.mat";
            }
            else if (file.Contains("fld"))
            {
                _shaderName   = "LaidlawFX/Fluid";
                _materialName = _assetName + "_fld_mat_vat.mat";
            }
            else
            {
                _shaderName   = "LaidlawFX/Sprite";
                _materialName = _assetName + "_spr_mat_vat.mat";
            }

            //compare strings and check for the materialname in the file
            Match match = Regex.Match(file, _materialName, RegexOptions.IgnoreCase);

            //If the file exists, set the old material variable with the project file
            if (match.Success)
            {
                _oldMaterial = (Material)AssetDatabase.LoadAssetAtPath(file, typeof(Material));
            }
        }

        //create a new material
        var    tempMaterial = new Material(Shader.Find(_shaderName));
        string tempName     = "temp_mat_vat.mat";

        //save material to the project directory
        AssetDatabase.CreateAsset(tempMaterial, Path.Combine(MaterialsPath, tempName));

        //load up the the new material and store it
        _mainMaterial = (Material)AssetDatabase.LoadAssetAtPath(Path.Combine(MaterialsPath, tempName), typeof(Material));
        // copy the old material options to the new material
        _mainMaterial.CopyPropertiesFromMaterial(_oldMaterial);
        // destroy the old material
        AssetDatabase.DeleteAsset(Path.Combine(MaterialsPath, _materialName));
        // rename the new material
        AssetDatabase.RenameAsset(Path.Combine(MaterialsPath, tempName), _materialName);

        //Grab the position texture if it exists
        Debug.Log("Warning: Textures not being applied.");
        string texturePath = GetFilesPathNotMeta(TexturePath, _assetName + "_pos_vat").Replace("\\", "/");

        Debug.Log("this is my pos texture path : " + texturePath);
        Texture2D posTex = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));
        int       temp   = posTex.GetInstanceID();

        Debug.Log(temp);

        //if (posTex != null)
        //{
        //    _mainMaterial.SetTexture("_posTex", posTex);
        //}
        _mainMaterial.SetTexture("_posTex", posTex);


        //Grab the rotation map path if it exists
        texturePath = GetFilesPathNotMeta(TexturePath, _assetName + "_rot_vat").Replace("\\", "/");

        Texture2D rotTex = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));

        if (rotTex != null)
        {
            _mainMaterial.SetTexture("_rotTex", rotTex);
        }

        //Grab the Scale map path if it exists
        texturePath = GetFilesPathNotMeta(TexturePath, _assetName + "_scale_vat");

        Texture2D scaleTex = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));

        if (scaleTex != null)
        {
            _mainMaterial.SetTexture("_scaleTex", scaleTex);
        }

        //Grab the rotation map path if it exists
        texturePath = GetFilesPathNotMeta(TexturePath, _assetName + "_norm_vat");

        Texture2D normTex = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));

        if (normTex != null)
        {
            _mainMaterial.SetTexture("_normTex", normTex);
        }

        //Grab the Scale map path if it exists
        texturePath = GetFilesPathNotMeta(TexturePath, _assetName + "_col_vat");

        Texture2D colTex = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));

        if (colTex != null)
        {
            _mainMaterial.SetTexture("_colTex", colTex);
        }
    }
        private void InitializeLocalIndexAndDirectory(Lucene.Net.Store.Directory baseLuceneDirectory, Analyzer analyzer, string configuredPath)
        {
            lock (Locker)
            {
                if (!Directory.Exists(_tempPath))
                {
                    Directory.CreateDirectory(_tempPath);
                }
                else
                {
                    //if we are syncing storage to the main file system to temp files, then clear out whatever is
                    //currently there since we'll re-copy it over
                    if (_syncStorage)
                    {
                        //clear it!
                        Directory.Delete(_tempPath, true);
                        //recreate it
                        Directory.CreateDirectory(_tempPath);
                    }
                }


                //if we are syncing storage to the main file system to temp files, then sync from the main FS to our temp FS
                if (_syncStorage)
                {
                    //copy index

                    using (new IndexWriter(
                               //read from the underlying/default directory, not the temp codegen dir
                               baseLuceneDirectory,
                               analyzer,
                               Snapshotter,
                               IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        try
                        {
                            var basePath = IOHelper.MapPath(configuredPath);

                            var commit    = Snapshotter.Snapshot();
                            var fileNames = commit.GetFileNames();

                            foreach (var fileName in fileNames)
                            {
                                File.Copy(
                                    Path.Combine(basePath, "Index", fileName),
                                    Path.Combine(_tempPath, Path.GetFileName(fileName)), true);
                            }

                            var segments = commit.GetSegmentsFileName();
                            if (segments.IsNullOrWhiteSpace() == false)
                            {
                                File.Copy(
                                    Path.Combine(basePath, "Index", segments),
                                    Path.Combine(_tempPath, Path.GetFileName(segments)), true);
                            }
                        }
                        finally
                        {
                            Snapshotter.Release();
                        }
                    }

                    //create the custom lucene directory which will keep the main and temp FS's in sync

                    LuceneDirectory = new TempStorageDirectory(
                        new DirectoryInfo(_tempPath),
                        baseLuceneDirectory);
                }
                else
                {
                    //just return a normal lucene directory that uses the codegen folder

                    LuceneDirectory = FSDirectory.Open(new DirectoryInfo(_tempPath));
                }
            }
        }
Example #17
0
        private async void OpenFile()
        {
            Status = new AppStatusInfo {
                Status = Enums.Status.Working
            };
            try
            {
                var openDig = new OpenFolderDialog
                {
                    Title = "Choose a directory with images"
                };
                var dirName = await openDig.ShowAsync(new Window());

                if (string.IsNullOrEmpty(dirName) || !Directory.Exists(dirName))
                {
                    Status = new AppStatusInfo {
                        Status = Enums.Status.Ready
                    };
                    return;
                }
                var fileNames = Directory.GetFiles(dirName);
                _frameLoadProgressIndex = 0;
                Frames.Clear(); _frames.Clear(); GC.Collect();
                var loadingFrames = new List <Frame>();
                foreach (var fileName in fileNames)
                {
                    // TODO: Проверка IsImage вне зависимости от расширений.
                    if (!Path.HasExtension(fileName))
                    {
                        continue;
                    }
                    if (Path.GetExtension(fileName).ToLower() != ".jpg" &&
                        Path.GetExtension(fileName).ToLower() != ".jpeg" &&
                        Path.GetExtension(fileName).ToLower() != ".png" &&
                        Path.GetExtension(fileName).ToLower() != ".bmp")
                    {
                        continue;
                    }

                    var frame = new Frame();
                    frame.OnLoad += FrameLoadingProgressUpdate;
                    frame.Load(fileName, Enums.ImageLoadMode.Miniature);
                    loadingFrames.Add(frame);
                }

                if (loadingFrames.Count == 0)
                {
                    Status = new AppStatusInfo {
                        Status = Enums.Status.Ready
                    };
                    return;
                }

                Frames = loadingFrames;
                if (SelectedIndex < 0)
                {
                    SelectedIndex = 0;
                }
                UpdateFramesRepo();
                UpdateUi();
                _frames = new List <Frame>(Frames);
                Status  = new AppStatusInfo {
                    Status = Enums.Status.Ready
                };
            }
            catch (Exception ex)
            {
                Status = new AppStatusInfo
                {
                    Status       = Enums.Status.Error,
                    StringStatus = $"Error | {ex.Message.Replace('\n', ' ')}"
                };
            }
        }
Example #18
0
        //Does not support recursive sgb
        private void getFilesBtn_Click(object sender, RoutedEventArgs e)
        {
            maplist.Clear();

            StringBuilder sb = new StringBuilder(mapPath);

            sb.Append(territoryName + @"\");

            territory = new SaintCoinach.Graphics.Territory((TerritoryType)placeBox.SelectedItem);

            String territoryFolder = sb.ToString();

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

            if (territory.Terrain != null)
            {
                foreach (var part in territory.Terrain.Parts)
                {
                    addToMapList("territory", part);
                    addToFileList(part.Model.File.Path);
                }
            }

            // Get the files we need
            foreach (var lgbFile in territory.LgbFiles)
            {
                foreach (var group in lgbFile.Groups)
                {
                    if (!eventCheck(group.Name))
                    {
                        addHeaderToMapList("LGBGroup: " + group.Name);
                        foreach (var part in group.Entries)
                        {
                            var asMdl = part as SaintCoinach.Graphics.Lgb.LgbModelEntry;
                            var asGim = part as SaintCoinach.Graphics.Lgb.LgbGimmickEntry;

                            string path = "";
                            if (asMdl?.Model?.Model != null)
                            {
                                path = asMdl.Model.Model.File.Path;
                                addToMapList(group.Name, asMdl.Model);
                                addToFileList(path);
                            }

                            if (asGim?.Gimmick != null)
                            {
                                List <String> gimmickFileList = getGimmickPaths(asGim);

                                addGimmickInfoToMapList(asGim);
                                addGimmicksToMapList(group.Name, asGim);
                                addHeaderToMapList("GimmickEnd");

                                foreach (String gPath in gimmickFileList)
                                {
                                    addToFileList(gPath);
                                }
                            }

                            addToFileList(path);
                        }
                    }
                }
            }
        }
Example #19
0
        private async void ImportAll()
        {
            Status = new AppStatusInfo {
                Status = Enums.Status.Working
            };
            try
            {
                var openDig = new OpenFolderDialog
                {
                    Title = "Choose a directory with xml annotations"
                };
                var dirName = await openDig.ShowAsync(new Window());

                if (string.IsNullOrEmpty(dirName) || !Directory.Exists(dirName))
                {
                    Status = new AppStatusInfo {
                        Status = Enums.Status.Ready
                    };
                    return;
                }

                var fileNames = Directory.GetFiles(dirName);
                _frameLoadProgressIndex = 0;
                Frames.Clear(); _frames.Clear(); GC.Collect();

                var loadingFrames = new List <Frame>();
                var annotations   = fileNames.Where(i => Path.GetExtension(i).ToLower() == ".xml")
                                    .Select(Annotation.ParseFromXml).ToList();

                foreach (var ann in annotations)
                {
                    var fileName = Path.Combine(dirName, ann.Filename);
                    // TODO: Проверка IsImage вне зависимости от расширений.
                    if (!File.Exists(fileName))
                    {
                        continue;
                    }
                    if (!Path.HasExtension(fileName))
                    {
                        continue;
                    }
                    if (Path.GetExtension(fileName).ToLower() != ".jpg" &&
                        Path.GetExtension(fileName).ToLower() != ".jpeg" &&
                        Path.GetExtension(fileName).ToLower() != ".png" &&
                        Path.GetExtension(fileName).ToLower() != ".bmp")
                    {
                        continue;
                    }

                    var frame = new Frame();
                    frame.OnLoad += FrameLoadingProgressUpdate;
                    frame.Load(fileName, Enums.ImageLoadMode.Miniature);
                    frame.Rectangles = ann.Objects.Select(obj =>
                                                          new BoundBox(obj.Box.Xmin, obj.Box.Ymin, obj.Box.Ymax - obj.Box.Ymin, obj.Box.Xmax - obj.Box.Xmin));

                    if (frame.Rectangles.Any())
                    {
                        frame.IsVisible = true;
                    }
                    loadingFrames.Add(frame);
                }

                if (loadingFrames.Count == 0)
                {
                    Status = new AppStatusInfo {
                        Status = Enums.Status.Ready
                    };
                    return;
                }


                Frames = loadingFrames;
                if (SelectedIndex < 0)
                {
                    SelectedIndex = 0;
                }
                UpdateFramesRepo();
                UpdateUi();
                _frames = new List <Frame>(Frames);
                Status  = new AppStatusInfo {
                    Status = Enums.Status.Ready
                };
            }
            catch (Exception ex)
            {
                Status = new AppStatusInfo
                {
                    Status       = Enums.Status.Error,
                    StringStatus = $"Error | {ex.Message.Replace('\n', ' ')}"
                };
            }
        }
 public static Maybe <bool> Exists(Maybe <string> mayPath)
 => from path in mayPath
 select IODic.Exists(path);
        /// <summary>
        ///     Update to the current version.
        /// </summary>
        /// <param name="detectDataChanges">Boolean indicating whether the update should also look for changes in data.</param>
        /// <param name="progress">Optional object to which update progress is reported.</param>
        /// <returns>Returns the <see cref="UpdateReport" /> containing all changes.</returns>
        /// <exception cref="InvalidOperationException">Definition is up-to-date.</exception>
        public UpdateReport Update(bool detectDataChanges, IProgress <UpdateProgress> progress = null)
        {
            if (DefinitionVersion == GameVersion)
            {
                throw new InvalidOperationException();
            }

            string previousVersion = DefinitionVersion;

            PackIdentifier exdPackId          = new PackIdentifier("exd", PackIdentifier.DefaultExpansion, 0);
            Pack           exdPack            = Packs.GetPack(exdPackId);
            bool           exdOldKeepInMemory = exdPack.KeepInMemory;

            exdPack.KeepInMemory = true;

            string       tempPath = null;
            UpdateReport report;

            try {
                using (ZipFile zip = new ZipFile(StateFile.FullName, ZipEncoding)) {
                    tempPath = ExtractPacks(zip, previousVersion);
                    PackCollection previousPack = new PackCollection(Path.Combine(tempPath, previousVersion));
                    previousPack.GetPack(exdPackId).KeepInMemory = true;

                    RelationDefinition previousDefinition;
                    if (previousVersion == _GameData.Definition.Version)
                    {
                        // Override previous definition when current definition version matches.
                        // Definitions may have changed since this was recorded and we want to compare that.
                        previousDefinition = _GameData.Definition;
                    }
                    else
                    {
                        // Otherwise, read the previous definition from the zip.
                        previousDefinition = ReadDefinition(zip, previousVersion);
                    }

                    RelationUpdater updater = new RelationUpdater(previousPack, previousDefinition, Packs, GameVersion, progress);

                    System.Collections.Generic.IEnumerable <IChange> changes = updater.Update(detectDataChanges);
                    report = new UpdateReport(previousVersion, GameVersion, changes);

                    RelationDefinition definition = updater.Updated;

                    StorePacks(zip);
                    StoreDefinitionInZip(zip, definition);
                    StoreDefinitionOnFilesystem(definition, "");

                    if (Debugger.IsAttached)
                    {
                        // Little QOL path - when updating with the debugger attached,
                        // also write to the project definitions path so no need to copy
                        // them manually afterward.
                        string projectDefinitionsPath = "../../../SaintCoinach";
                        if (Directory.Exists(projectDefinitionsPath))
                        {
                            StoreDefinitionOnFilesystem(definition, projectDefinitionsPath);
                        }
                    }

                    StoreReport(zip, report);
                    UpdateVersion(zip);
                    zip.Save();

                    GameData.Definition = definition;
                    GameData.Definition.Compile();
                }
            } finally {
                if (exdPack != null)
                {
                    exdPack.KeepInMemory = exdOldKeepInMemory;
                }
                if (tempPath != null)
                {
                    try {
                        Directory.Delete(tempPath, true);
                    } catch {
                        Console.Error.WriteLine("Failed to delete temporary directory {0}.", tempPath);
                    }
                }
            }
            return(report);
        }
Example #22
0
        public async Task Start()
        {
            Instance = this;
            IInjectionService injection = Services.Get <IInjectionService>();

            string directory = injection.GamePath;

            if (!IsValidInstallation(directory))
            {
                throw new Exception("Invalid FFXIV installation");
            }

            bool forceUpdate = false;

            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (forceUpdate)
            {
                if (File.Exists("SaintCoinach.History.zip"))
                {
                    File.Delete("SaintCoinach.History.zip");
                }

                if (Directory.Exists("./Definitions/"))
                {
                    Directory.Delete("./Definitions/", true);
                }
            }

            // Unzip definitions
            if (!Directory.Exists("./Definitions/"))
            {
                ZipFile.ExtractToDirectory("./Modules/SaintCoinach/bin/Definitions.zip", "./Definitions/");
            }

            // TODO get language from language service?
            ARealmReversed realm = new ARealmReversed(directory, Language.English);

            try
            {
                if (!realm.IsCurrentVersion)
                {
                    realm.Update(true, this);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(@"Failed to update Saint Coinach", ex);
            }

            List <Task> tasks = new List <Task>();

            tasks.Add(Task.Run(() => this.Items                  = this.Load <Table <IItem>, IItem, Item, ItemWrapper>(realm)));
            tasks.Add(Task.Run(() => this.Races                  = this.Load <Table <IRace>, IRace, Race, RaceWrapper>(realm)));
            tasks.Add(Task.Run(() => this.Tribes                 = this.Load <Table <ITribe>, ITribe, Tribe, TribeWrapper>(realm)));
            tasks.Add(Task.Run(() => this.Dyes                   = this.Load <Table <IDye>, IDye, Stain, DyeWrapper>(realm)));
            tasks.Add(Task.Run(() => this.BaseNPCs               = this.Load <Table <INpcBase>, INpcBase, ENpcBase, NpcBaseWrapper>(realm)));
            tasks.Add(Task.Run(() => this.Territories            = this.Load <Table <ITerritoryType>, ITerritoryType, TerritoryType, TerritoryTypeWrapper>(realm)));
            tasks.Add(Task.Run(() => this.Weathers               = this.Load <Table <IWeather>, IWeather, Weather, WeatherWrapper>(realm)));
            tasks.Add(Task.Run(() => this.CharacterMakeCustomize = this.Load <CustomizeTable, ICharaMakeCustomize, CharaMakeCustomize, CharacterMakeCustomizeWrapper>(realm)));
            tasks.Add(Task.Run(() => this.CharacterMakeTypes     = this.Load <Table <ICharaMakeType>, ICharaMakeType, CharaMakeType, CharacterMakeTypeWrapper>(realm)));
            tasks.Add(Task.Run(() => this.ResidentNPCs           = this.Load <Table <INpcResident>, INpcResident, ENpcResident, NpcResidentWrapper>(realm)));
            tasks.Add(Task.Run(() => this.Titles                 = this.Load <Table <ITitle>, ITitle, Title, TitleWrapper>(realm)));
            tasks.Add(Task.Run(() => this.Statuses               = this.Load <Table <IStatus>, IStatus, Status, StatusWrapper>(realm)));

            foreach (Task t in tasks)
            {
                await t;
            }

            Log.Write("Initialization took " + sw.ElapsedMilliseconds + "ms", @"Saint Coinach");
        }
Example #23
0
        public Resources(string dbUrl, string storageDir) : base(dbUrl)
        {
            var resourceDir = Path.Combine(storageDir, "resource");

            if (!Dir.Exists(resourceDir))
            {
                Dir.CreateDirectory(resourceDir);
            }

            GetAsync["/{id:int}/image"] = async(p, c) =>
            {
                var id = (int)p["id"];

                var oldResource = new Resource {
                    id = id
                };
                using (var db = await DB.CreateAsync(dbUrl))
                {
                    if (!await oldResource.LoadAsync(db, true))
                    {
                        oldResource = null;
                    }
                }

                if (oldResource == null)
                {
                    return;
                }

                var fullPath = Path.Combine(resourceDir, $"{id}.jpg");

                if (File.Exists(fullPath))
                {
                    var shortName = Path.GetFileName(fullPath);
                    c.Response.Headers["content-type"] = "image/jpeg";

                    var    lastModif = File.GetLastWriteTime(fullPath);
                    string etag      = "\"" + lastModif.Ticks.ToString("X") + "\"";
                    c.Response.Headers["etag"] = etag;

                    if (c.Request.QueryString.ContainsKey("if-none-match") &&
                        (c.Request.QueryString["if-none-match"] == etag))
                    {
                        c.Response.StatusCode = 304;
                    }
                    else
                    {
                        c.Response.StatusCode    = 200;
                        c.Response.SupportRanges = true;
                        c.Response.Content       = new FileContent(fullPath);
                    }
                }
            };

            DeleteAsync["/{id:int}/image"] = async(p, c) =>
            {
                var id = (int)p["id"];

                var oldResource = new Resource {
                    id = id
                };
                using (var db = await DB.CreateAsync(dbUrl))
                {
                    if (!await oldResource.LoadAsync(db, true))
                    {
                        oldResource = null;
                    }
                }

                if (oldResource == null)
                {
                    return;
                }

                var fullPath = Path.Combine(resourceDir, $"{id}.jpg");

                if (File.Exists(fullPath))
                {
                    File.Delete(fullPath);
                    c.Response.StatusCode = 200;
                    c.Response.Content    = "";
                }
            };

            PostAsync["/{id:int}/image"] = async(p, c) =>
            {
                var id = (int)p["id"];

                var oldResource = new Resource {
                    id = id
                };
                using (var db = await DB.CreateAsync(dbUrl))
                {
                    if (!await oldResource.LoadAsync(db, true))
                    {
                        oldResource = null;
                    }
                }

                if (oldResource == null)
                {
                    return;
                }

                var           reader = c.Request.ReadAsMultipart();
                MultipartPart part;
                while ((part = await reader.ReadPartAsync()) != null)
                {
                    if (part.Headers.ContainsKey("content-disposition") && part.Headers.ContainsKey("content-type"))
                    {
                        if ((part.Headers["content-type"] != "image/jpeg") &&
                            (part.Headers["content-type"] != "image/png") &&
                            (part.Headers["content-type"] != "image/svg+xml"))
                        {
                            continue;
                        }

                        var disposition = ContentDisposition.Decode(part.Headers["content-disposition"]);
                        if (disposition.ContainsKey("name") && (disposition["name"] == "image"))
                        {
                            var dir      = DirExt.CreateRecursive(resourceDir);
                            var fullPath = Path.Combine(dir.FullName, $"{id}.jpg");

                            // crop / resize / convert the image using ImageMagick
                            var startInfo = new ProcessStartInfo("/usr/bin/convert", $"- -auto-orient -strip -distort SRT 0 +repage -quality 80 -resize 1024x1024 jpeg:{fullPath}");
                            startInfo.RedirectStandardOutput = false;
                            startInfo.RedirectStandardInput  = true;
                            startInfo.UseShellExecute        = false;
                            var process = new Process();
                            process.StartInfo = startInfo;
                            process.Start();

                            // read the file stream and send it to ImageMagick
                            await part.Stream.CopyToAsync(process.StandardInput.BaseStream);

                            process.StandardInput.Close();

                            process.WaitForExit();
                            process.Dispose();

                            c.Response.StatusCode = 200;
                        }
                    }
                }
            };
        }
Example #24
0
        public void Sort(string path, CancellationToken token)
        {
            Trace.WriteLine("Вход в папку " + path);
            int count = 0;

            string[] dirs  = null;
            string[] files = null;
            try
            {
                /* string[]*/
                dirs = System.IO.Directory.GetDirectories(path);
                /* string[]*/
                files = System.IO.Directory.GetFiles(path, "*.*").Where((s) => s.ToLower().EndsWith(".jpg") || s.ToLower().EndsWith(".jpeg")).ToArray();
            }
            catch (IOException ioe)
            {
                Trace.WriteLine(ioe.Message);
                //выясним, не извлекли ли съемный диск

                DriveInfo drive = new DriveInfo(System.IO.Path.GetPathRoot(path));
                if (!drive.IsReady)
                {
                    Trace.WriteLine("Диск " + drive.Name + " был извлечен, или содержит ошибки, мешаюшие поиску");
                    return; /*result*/;
                }
                if (dirs == null || files == null)
                {
                    Trace.WriteLine("Массивы со списком файлов или папок - null");
                    return; /*result*/;
                }
            }

            foreach (var f in files)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }
                try
                {
                    DateTime fileDateTime     = GetDateTimeForImage(f);
                    string   result_directory = "";
                    if (Parameters.format == DestinationDirectoriesFormat.YearMonth)
                    {
                        result_directory = fileDateTime.ToString("yyyy_MM");
                    }
                    else
                    {
                        result_directory = fileDateTime.ToString("yyyy_MM_dd");
                    }

                    if (!Directory.Exists(Path.Combine(DestinationDirectory, result_directory)))
                    {
                        Directory.CreateDirectory(Path.Combine(DestinationDirectory, result_directory));
                    }

                    //--------------------------------------------------------
                    string dstname = Path.Combine(DestinationDirectory, result_directory, Path.GetFileName(f));
                    foreach (string dname in DestinationFileNames(dstname))
                    {
                        if (!File.Exists(dname))
                        {
                            if (Parameters.mode == FileOperations.Copy)
                            {
                                File.Copy(f, dname);
                            }
                            else
                            {
                                File.Move(f, dname);
                            }
                            break;
                        }
                    }
                    sorterFileChangedEvent?.Invoke(new SortedEventArgs(f, fileDateTime.Date.ToShortDateString(), Path.Combine(DestinationDirectory, result_directory)));
                    //--------------------------------------------------------
                }
                catch (UnauthorizedAccessException ex)
                {
                    Trace.WriteLine("Windows Access Error : " + ex.Message);
                    sorterMetadataErrorEvent?.Invoke(new SorterErrorEventArgs(f));
                }
                catch (IOException ex)
                {
                    Trace.WriteLine("I/O Error : " + ex.Message);
                    sorterMetadataErrorEvent?.Invoke(new SorterErrorEventArgs(f));
                }
                catch (MetadataException ex)
                {
                    Trace.WriteLine("Date Extraction Error: " + ex.Message);
                    sorterMetadataErrorEvent?.Invoke(new SorterErrorEventArgs(f));
                }
                catch (NullReferenceException ex)
                {
                    Trace.WriteLine("NRE: " + ex.Message);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("Unknow error: " + ex.Message);
                }
            }

            foreach (var d in dirs)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }
                try
                {
                    Sort(d, token);
                }
                catch (UnauthorizedAccessException ex)
                {
                    Trace.WriteLine("Windows Access Error : " + ex.Message);
                }
                catch (IOException ex)
                {
                    Trace.WriteLine("I/O Error : " + ex.Message);
                }
            }
            return;
        }
Example #25
0
        // TODO Merge these, with proper logic depending on the scenario (import, force, etc)
        public (string, string) MoveWithResultString(string scriptName, bool force = false)
        {
            // TODO Make this take an argument to disable removing empty dirs. It's slow, and should only be done if needed
            if (FullServerPath == null)
            {
                logger.Error("Could not find or access the file to move: {0}",
                             VideoLocal_Place_ID);
                return(string.Empty, "ERROR: Unable to access file");
            }

            if (!File.Exists(FullServerPath))
            {
                logger.Error("Could not find or access the file to move: {0}", FullServerPath);
                // this can happen due to file locks, so retry
                return(string.Empty, "ERROR: Could not access the file");
            }

            FileInfo sourceFile = new FileInfo(FullServerPath);

            // There is a possibility of weird logic based on source of the file. Some handling should be made for it....later
            (var destImpl, string newFolderPath) = RenameFileHelper.GetDestination(this, scriptName);

            if (!(destImpl is SVR_ImportFolder destFolder))
            {
                // In this case, an error string was returned, but we'll suppress it and give an error elsewhere
                if (newFolderPath != null)
                {
                    logger.Error("Unable to find destination for: {0}", FullServerPath);
                    logger.Error("The error message was: " + newFolderPath);
                    return(string.Empty, "ERROR: " + newFolderPath);
                }
                logger.Error("Unable to find destination for: {0}", FullServerPath);
                return(string.Empty, "ERROR: There was an error but no error code returned...");
            }

            // keep the original drop folder for later (take a copy, not a reference)
            SVR_ImportFolder dropFolder = ImportFolder;

            if (string.IsNullOrEmpty(newFolderPath))
            {
                logger.Error("Unable to find destination for: {0}", FullServerPath);
                return(string.Empty, "ERROR: The returned path was null or empty");
            }

            // We've already resolved FullServerPath, so it doesn't need to be checked
            string newFilePath       = Path.Combine(newFolderPath, Path.GetFileName(FullServerPath));
            string newFullServerPath = Path.Combine(destFolder.ImportFolderLocation, newFilePath);

            var destFullTree = Path.Combine(destFolder.ImportFolderLocation, newFolderPath);

            if (!Directory.Exists(destFullTree))
            {
                try
                {
                    Directory.CreateDirectory(destFullTree);
                }
                catch (Exception e)
                {
                    logger.Error(e);
                    return(string.Empty, $"ERROR: Unable to create directory tree: {destFullTree}");
                }
            }

            // Last ditch effort to ensure we aren't moving a file unto itself
            if (newFullServerPath.Equals(FullServerPath, StringComparison.InvariantCultureIgnoreCase))
            {
                logger.Info($"The file is already at its desired location: {FullServerPath}");
                return(string.Empty, "ERROR: The file is already at its desired location");
            }

            if (File.Exists(newFullServerPath))
            {
                logger.Info($"A file already exists at the desired location for {FullServerPath}");
                return(string.Empty, "ERROR: The File already exists at the destination");
            }

            ShokoServer.PauseWatchingFiles();

            logger.Info("Moving file from {0} to {1}", FullServerPath, newFullServerPath);
            try
            {
                sourceFile.MoveTo(newFullServerPath);
            }
            catch (Exception e)
            {
                logger.Error("Unable to MOVE file: {0} to {1} error {2}", FullServerPath,
                             newFullServerPath, e);
                ShokoServer.UnpauseWatchingFiles();
                return(newFullServerPath, "ERROR: " + e);
            }

            // Save for later. Scan for subtitles while the vlplace is still set for the source location
            string originalFileName = FullServerPath;
            var    textStreams      = SubtitleHelper.GetSubtitleStreams(this);

            // Handle Duplicate Files
            var dups = RepoFactory.DuplicateFile.GetByFilePathAndImportFolder(FilePath, ImportFolderID).ToList();

            foreach (var dup in dups)
            {
                // Move source
                if (dup.FilePathFile1.Equals(FilePath) && dup.ImportFolderIDFile1 == ImportFolderID)
                {
                    dup.FilePathFile1       = newFilePath;
                    dup.ImportFolderIDFile1 = destFolder.ImportFolderID;
                }
                else if (dup.FilePathFile2.Equals(FilePath) && dup.ImportFolderIDFile2 == ImportFolderID)
                {
                    dup.FilePathFile2       = newFilePath;
                    dup.ImportFolderIDFile2 = destFolder.ImportFolderID;
                }
                // validate the dup file
                // There are cases where a dup file was not cleaned up before, so we'll do it here, too
                if (!dup.GetFullServerPath1()
                    .Equals(dup.GetFullServerPath2(), StringComparison.InvariantCultureIgnoreCase))
                {
                    RepoFactory.DuplicateFile.Save(dup);
                }
                else
                {
                    RepoFactory.DuplicateFile.Delete(dup);
                }
            }

            ImportFolderID = destFolder.ImportFolderID;
            FilePath       = newFilePath;
            RepoFactory.VideoLocalPlace.Save(this);

            try
            {
                // move any subtitle files
                foreach (TextStream subtitleFile in textStreams)
                {
                    if (string.IsNullOrEmpty(subtitleFile.Filename))
                    {
                        continue;
                    }
                    var newParent = Path.GetDirectoryName(newFullServerPath);
                    var srcParent = Path.GetDirectoryName(originalFileName);
                    if (string.IsNullOrEmpty(newParent) || string.IsNullOrEmpty(srcParent))
                    {
                        continue;
                    }
                    var subPath = Path.Combine(srcParent, subtitleFile.Filename);
                    if (!File.Exists(subPath))
                    {
                        continue;
                    }
                    var    subFile    = new FileInfo(subPath);
                    string newSubPath = Path.Combine(newParent, subFile.Name);
                    if (File.Exists(newSubPath))
                    {
                        try
                        {
                            File.Delete(newSubPath);
                        }
                        catch (Exception e)
                        {
                            logger.Warn("Unable to DELETE file: {0} error {1}", subtitleFile, e);
                        }
                    }

                    try
                    {
                        subFile.MoveTo(newSubPath);
                    }
                    catch (Exception e)
                    {
                        logger.Error("Unable to MOVE file: {0} to {1} error {2}", subtitleFile, newSubPath, e);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, ex.ToString());
            }

            // check for any empty folders in drop folder
            // only for the drop folder
            if (dropFolder.IsDropSource == 1)
            {
                RecursiveDeleteEmptyDirectories(dropFolder?.ImportFolderLocation, true);
            }
            ShokoServer.UnpauseWatchingFiles();
            return(newFolderPath, string.Empty);
        }
Example #26
0
        public static void  Main(System.String[] args)
        {
            // get command line arguments
            String prologFilename = null; // name of file "wn_s.pl"
            String indexDir       = null;

            if (args.Length == 2)
            {
                prologFilename = args[0];
                indexDir       = args[1];
            }
            else
            {
                Usage();
                Environment.Exit(1);
            }

            // ensure that the prolog file is readable
            if (!(new FileInfo(prologFilename)).Exists)
            {
                err.WriteLine("Error: cannot read Prolog file: " + prologFilename);
                Environment.Exit(1);
            }
            // exit if the target index directory already exists
            if (Directory.Exists((new FileInfo(indexDir)).FullName))
            {
                err.WriteLine("Error: index directory already exists: " + indexDir);
                err.WriteLine("Please specify a name of a non-existent directory");
                Environment.Exit(1);
            }

            o.WriteLine("Opening Prolog file " + prologFilename);
            var    fis = new FileStream(prologFilename, FileMode.Open, FileAccess.Read);
            var    br  = new StreamReader(new StreamReader(fis, System.Text.Encoding.Default).BaseStream, new StreamReader(fis, System.Text.Encoding.Default).CurrentEncoding);
            String line;

            // maps a word to all the "groups" it's in
            System.Collections.IDictionary word2Nums = new System.Collections.SortedList();
            // maps a group to all the words in it
            System.Collections.IDictionary num2Words = new System.Collections.SortedList();
            // number of rejected words
            var ndecent = 0;

            // status output
            var mod = 1;
            var row = 1;

            // parse prolog file
            o.WriteLine("[1/2] Parsing " + prologFilename);
            while ((line = br.ReadLine()) != null)
            {
                // occasional progress
                if ((++row) % mod == 0) // periodically print out line we read in
                {
                    mod *= 2;
                    o.WriteLine("\t" + row + " " + line + " " + word2Nums.Count + " " + num2Words.Count + " ndecent=" + ndecent);
                }

                // syntax check
                if (!line.StartsWith("s("))
                {
                    err.WriteLine("OUCH: " + line);
                    Environment.Exit(1);
                }

                // parse line
                line = line.Substring(2);
                var comma = line.IndexOf(',');
                var num   = line.Substring(0, comma);
                var q1    = line.IndexOf('\'');
                line = line.Substring(q1 + 1);
                var q2   = line.IndexOf('\'');
                var word = line.Substring(0, q2).ToLower().Replace("''", "'");

                // make sure is a normal word
                if (!IsDecent(word))
                {
                    ndecent++;
                    continue; // don't store words w/ spaces
                }

                // 1/2: word2Nums map
                // append to entry or add new one
                var lis = (System.Collections.IList)word2Nums[word];
                if (lis == null)
                {
                    lis = new List <String> {
                        num
                    };
                    word2Nums[word] = lis;
                }
                else
                {
                    lis.Add(num);
                }

                // 2/2: num2Words map
                lis = (System.Collections.IList)num2Words[num];
                if (lis == null)
                {
                    lis = new List <String> {
                        word
                    };
                    num2Words[num] = lis;
                }
                else
                {
                    lis.Add(word);
                }
            }

            // close the streams
            fis.Close();
            br.Close();

            // create the index
            o.WriteLine("[2/2] Building index to store synonyms, " + " map sizes are " + word2Nums.Count + " and " + num2Words.Count);
            Index(indexDir, word2Nums, num2Words);
        }
Example #27
0
        internal static string DownloadFile(string queryUrl, string destination, PackageSourceListRequest request, ProgressTracker progressTracker)
        {
            try
            {
                request.Debug(Resources.Messages.DebugInfoCallMethod, Constants.ProviderName, string.Format(System.Globalization.CultureInfo.InvariantCulture, "DownloadFile - url='{0}', destination='{1}'", queryUrl, destination));

                if (string.IsNullOrWhiteSpace(destination))
                {
                    throw new ArgumentNullException("destination");
                }

                // make sure that the parent folder is created first.
                var folder = Path.GetDirectoryName(destination);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                if (File.Exists(destination))
                {
                    destination.TryHardToDelete();
                }


                if (progressTracker == null)
                {
                    progressTracker = new ProgressTracker(request.StartProgress(0, Resources.Messages.DownloadingPackage, queryUrl));
                }

                Uri uri;

                if (!Uri.TryCreate(queryUrl, UriKind.Absolute, out uri))
                {
                    request.Error(Internal.ErrorCategory.InvalidOperation, Resources.Messages.UnsuportedUriFormat, Constants.ProviderName, queryUrl);
                    return(null);
                }

                if (uri.IsFile)
                {
                    // downloading from a file share
                    using (var input = File.OpenRead(queryUrl))
                    {
                        using (var output = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.Read))
                        {
                            request.Progress(progressTracker.ProgressID, progressTracker.StartPercent, Resources.Messages.Downloading);

                            input.CopyTo(output);
                        }
                    }

                    request.CompleteProgress(progressTracker.ProgressID, true);
                }
                else
                {
                    //Downloading from url
                    var result = DownloadDataToFileAsync(destination, queryUrl, request, PathUtility.GetNetworkCredential(request.CredentialUsername, request.CredentialPassword), progressTracker).Result;
                }

                if (File.Exists(destination))
                {
                    request.Verbose(Resources.Messages.CompletedDownload, queryUrl);
                    return(destination);
                }
                else
                {
                    request.Error(Internal.ErrorCategory.InvalidOperation, Resources.Messages.FailedToDownload, Constants.ProviderName, queryUrl, destination);
                    return(null);
                }
            }
            catch (Exception ex)
            {
                ex.Dump(request);
                request.Warning(ex.Message);
                return(null);
            }
        }
Example #28
0
 public void BaselineDirectoryExists() => Assert.IsTrue(Directory.Exists(UncHelper.GetUncFromPath(".")));
        public override void ExecuteCmdlet()
        {
            CloudFile fileToBeDownloaded;

            string[] path = NamingUtil.ValidatePath(this.Path, true);
            switch (this.ParameterSetName)
            {
            case Constants.FileParameterSetName:
                fileToBeDownloaded = this.File;
                break;

            case Constants.ShareNameParameterSetName:
                var share = this.BuildFileShareObjectFromName(this.ShareName);
                fileToBeDownloaded = share.GetRootDirectoryReference().GetFileReferenceByPath(path);
                break;

            case Constants.ShareParameterSetName:
                fileToBeDownloaded = this.Share.GetRootDirectoryReference().GetFileReferenceByPath(path);
                break;

            case Constants.DirectoryParameterSetName:
                fileToBeDownloaded = this.Directory.GetFileReferenceByPath(path);
                break;

            default:
                throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName));
            }

            string resolvedDestination = this.GetUnresolvedProviderPathFromPSPath(
                string.IsNullOrWhiteSpace(this.Destination) ? "." : this.Destination);

            FileMode mode = this.Force ? FileMode.Create : FileMode.CreateNew;
            string   targetFile;

            if (LocalDirectory.Exists(resolvedDestination))
            {
                // If the destination pointed to an existing directory, we
                // would download the file into the folder with the same name
                // on cloud.
                targetFile = LocalPath.Combine(resolvedDestination, fileToBeDownloaded.GetBaseName());
            }
            else
            {
                // Otherwise we treat the destination as a file no matter if
                // there's one existing or not. The overwrite behavior is configured
                // by FileMode.
                targetFile = resolvedDestination;
            }

            this.RunTask(async taskId =>
            {
                var downloadJob = new FileDownloadJob()
                {
                    SourceFile = fileToBeDownloaded,
                    DestPath   = targetFile
                };

                var progressRecord = new ProgressRecord(
                    this.OutputStream.GetProgressId(taskId),
                    string.Format(CultureInfo.CurrentCulture, Resources.ReceiveAzureFileActivity, fileToBeDownloaded.GetFullPath(), targetFile),
                    Resources.PrepareDownloadingFile);

                await this.RunTransferJob(downloadJob, progressRecord);

                if (this.PassThru)
                {
                    this.OutputStream.WriteObject(taskId, fileToBeDownloaded);
                }
            });
        }
Example #30
0
        // returns false if we should retry
        private bool MoveFileIfRequired(bool deleteEmpty = true)
        {
            if (!ServerSettings.Instance.Import.MoveOnImport)
            {
                logger.Trace($"Skipping move of \"{this.FullServerPath}\" as move on import is disabled");
                return(true);
            }

            // TODO Make this take an argument to disable removing empty dirs. It's slow, and should only be done if needed
            try
            {
                logger.Trace("Attempting to MOVE file: {0}", FullServerPath ?? VideoLocal_Place_ID.ToString());

                if (FullServerPath == null)
                {
                    logger.Error("Could not find or access the file to move: {0}",
                                 VideoLocal_Place_ID);
                    return(true);
                }

                // check if this file is in the drop folder
                // otherwise we don't need to move it
                if (ImportFolder.IsDropSource == 0)
                {
                    logger.Trace("Not moving file as it is NOT in the drop folder: {0}", FullServerPath);
                    return(true);
                }

                if (!File.Exists(FullServerPath))
                {
                    logger.Error("Could not find or access the file to move: {0}", FullServerPath);
                    // this can happen due to file locks, so retry
                    return(false);
                }
                var sourceFile = new FileInfo(FullServerPath);

                // find the default destination
                (var destImpl, string newFolderPath) = RenameFileHelper.GetDestination(this, null);

                if (!(destImpl is SVR_ImportFolder destFolder))
                {
                    // In this case, an error string was returned, but we'll suppress it and give an error elsewhere
                    if (newFolderPath != null)
                    {
                        return(true);
                    }
                    logger.Error("Could not find a valid destination: {0}", FullServerPath);
                    return(true);
                }

                // keep the original drop folder for later (take a copy, not a reference)
                SVR_ImportFolder dropFolder = ImportFolder;

                if (string.IsNullOrEmpty(newFolderPath))
                {
                    return(true);
                }

                // We've already resolved FullServerPath, so it doesn't need to be checked
                string newFilePath       = Path.Combine(newFolderPath, Path.GetFileName(FullServerPath));
                string newFullServerPath = Path.Combine(destFolder.ImportFolderLocation, newFilePath);

                var destFullTree = Path.Combine(destFolder.ImportFolderLocation, newFolderPath);
                if (!Directory.Exists(destFullTree))
                {
                    try
                    {
                        Directory.CreateDirectory(destFullTree);
                    }
                    catch (Exception e)
                    {
                        logger.Error(e);
                        return(true);
                    }
                }

                // Last ditch effort to ensure we aren't moving a file unto itself
                if (newFullServerPath.Equals(FullServerPath, StringComparison.InvariantCultureIgnoreCase))
                {
                    logger.Error($"Resolved to move {newFullServerPath} unto itself. NOT MOVING");
                    return(true);
                }

                var originalFileName = FullServerPath;
                var textStreams      = SubtitleHelper.GetSubtitleStreams(this);

                if (File.Exists(newFullServerPath))
                {
                    // A file with the same name exists at the destination.
                    // Handle Duplicate Files, A duplicate file record won't exist yet,
                    // so we'll check the old fashioned way
                    logger.Trace("A file already exists at the new location, checking it for duplicate");
                    var destVideoLocalPlace = RepoFactory.VideoLocalPlace.GetByFilePathAndImportFolderID(newFilePath,
                                                                                                         destFolder.ImportFolderID);
                    var destVideoLocal = destVideoLocalPlace?.VideoLocal;
                    if (destVideoLocal == null)
                    {
                        logger.Error("The existing file at the new location does not have a VideoLocal. Not moving");
                        return(true);
                    }
                    if (destVideoLocal.Hash == VideoLocal.Hash)
                    {
                        logger.Info(
                            "Not moving file as it already exists at the new location, deleting source file instead: {0} --- {1}",
                            FullServerPath, newFullServerPath);

                        // if the file already exists, we can just delete the source file instead
                        // this is safer than deleting and moving
                        try
                        {
                            sourceFile.Delete();
                        }
                        catch (Exception e)
                        {
                            logger.Warn("Unable to DELETE file: {0} error {1}", FullServerPath, e);
                            RemoveRecord(false);

                            // check for any empty folders in drop folder
                            // only for the drop folder
                            if (dropFolder.IsDropSource != 1)
                            {
                                return(true);
                            }
                            RecursiveDeleteEmptyDirectories(dropFolder?.ImportFolderLocation, true);
                            return(true);
                        }
                    }

                    // Not a dupe, don't delete it
                    logger.Trace("A file already exists at the new location, checking it for version and group");
                    var destinationExistingAniDBFile = destVideoLocal.GetAniDBFile();
                    if (destinationExistingAniDBFile == null)
                    {
                        logger.Error("The existing file at the new location does not have AniDB info. Not moving.");
                        return(true);
                    }

                    var aniDBFile = VideoLocal.GetAniDBFile();
                    if (aniDBFile == null)
                    {
                        logger.Error("The file does not have AniDB info. Not moving.");
                        return(true);
                    }

                    if (destinationExistingAniDBFile.Anime_GroupName == aniDBFile.Anime_GroupName &&
                        destinationExistingAniDBFile.FileVersion < aniDBFile.FileVersion)
                    {
                        // This is a V2 replacing a V1 with the same name.
                        // Normally we'd let the Multiple Files Utility handle it, but let's just delete the V1
                        logger.Info("The existing file is a V1 from the same group. Replacing it.");
                        // Delete the destination
                        (bool success, string _) = destVideoLocalPlace.RemoveAndDeleteFile();
                        if (!success)
                        {
                            return(false);
                        }

                        // Move
                        ShokoServer.PauseWatchingFiles();
                        logger.Info("Moving file from {0} to {1}", FullServerPath, newFullServerPath);
                        try
                        {
                            sourceFile.MoveTo(newFullServerPath);
                        }
                        catch (Exception e)
                        {
                            logger.Error("Unable to MOVE file: {0} to {1} error {2}", FullServerPath, newFullServerPath, e);
                            ShokoServer.UnpauseWatchingFiles();
                            return(false);
                        }

                        // Handle Duplicate Files
                        var dups = RepoFactory.DuplicateFile.GetByFilePathAndImportFolder(FilePath, ImportFolderID).ToList();

                        foreach (var dup in dups)
                        {
                            // Move source
                            if (dup.FilePathFile1.Equals(FilePath) && dup.ImportFolderIDFile1 == ImportFolderID)
                            {
                                dup.FilePathFile1       = newFilePath;
                                dup.ImportFolderIDFile1 = destFolder.ImportFolderID;
                            }
                            else if (dup.FilePathFile2.Equals(FilePath) && dup.ImportFolderIDFile2 == ImportFolderID)
                            {
                                dup.FilePathFile2       = newFilePath;
                                dup.ImportFolderIDFile2 = destFolder.ImportFolderID;
                            }
                            // validate the dup file
                            // There are cases where a dup file was not cleaned up before, so we'll do it here, too
                            if (!dup.GetFullServerPath1()
                                .Equals(dup.GetFullServerPath2(), StringComparison.InvariantCultureIgnoreCase))
                            {
                                RepoFactory.DuplicateFile.Save(dup);
                            }
                            else
                            {
                                RepoFactory.DuplicateFile.Delete(dup);
                            }
                        }

                        ImportFolderID = destFolder.ImportFolderID;
                        FilePath       = newFilePath;
                        RepoFactory.VideoLocalPlace.Save(this);

                        // check for any empty folders in drop folder
                        // only for the drop folder
                        if (dropFolder.IsDropSource == 1 && deleteEmpty)
                        {
                            RecursiveDeleteEmptyDirectories(dropFolder?.ImportFolderLocation, true);
                        }
                    }
                }
                else
                {
                    ShokoServer.PauseWatchingFiles();
                    logger.Info("Moving file from {0} to {1}", FullServerPath, newFullServerPath);
                    try
                    {
                        sourceFile.MoveTo(newFullServerPath);
                    }
                    catch (Exception e)
                    {
                        logger.Error("Unable to MOVE file: {0} to {1} error {2}", FullServerPath, newFullServerPath, e);
                        ShokoServer.UnpauseWatchingFiles();
                        return(false);
                    }

                    // Handle Duplicate Files
                    var dups = RepoFactory.DuplicateFile.GetByFilePathAndImportFolder(FilePath, ImportFolderID).ToList();

                    foreach (var dup in dups)
                    {
                        // Move source
                        if (dup.FilePathFile1.Equals(FilePath) && dup.ImportFolderIDFile1 == ImportFolderID)
                        {
                            dup.FilePathFile1       = newFilePath;
                            dup.ImportFolderIDFile1 = destFolder.ImportFolderID;
                        }
                        else if (dup.FilePathFile2.Equals(FilePath) && dup.ImportFolderIDFile2 == ImportFolderID)
                        {
                            dup.FilePathFile2       = newFilePath;
                            dup.ImportFolderIDFile2 = destFolder.ImportFolderID;
                        }
                        // validate the dup file
                        // There are cases where a dup file was not cleaned up before, so we'll do it here, too
                        if (!dup.GetFullServerPath1()
                            .Equals(dup.GetFullServerPath2(), StringComparison.InvariantCultureIgnoreCase))
                        {
                            RepoFactory.DuplicateFile.Save(dup);
                        }
                        else
                        {
                            RepoFactory.DuplicateFile.Delete(dup);
                        }
                    }

                    ImportFolderID = destFolder.ImportFolderID;
                    FilePath       = newFilePath;
                    RepoFactory.VideoLocalPlace.Save(this);

                    // check for any empty folders in drop folder
                    // only for the drop folder
                    if (dropFolder.IsDropSource == 1 && deleteEmpty)
                    {
                        RecursiveDeleteEmptyDirectories(dropFolder?.ImportFolderLocation, true);
                    }
                }

                try
                {
                    // move any subtitle files
                    foreach (TextStream subtitleFile in textStreams)
                    {
                        if (string.IsNullOrEmpty(subtitleFile.Filename))
                        {
                            continue;
                        }
                        var newParent = Path.GetDirectoryName(newFullServerPath);
                        var srcParent = Path.GetDirectoryName(originalFileName);
                        if (string.IsNullOrEmpty(newParent) || string.IsNullOrEmpty(srcParent))
                        {
                            continue;
                        }
                        var subPath = Path.Combine(srcParent, subtitleFile.Filename);
                        if (!File.Exists(subPath))
                        {
                            continue;
                        }
                        var    subFile    = new FileInfo(subPath);
                        string newSubPath = Path.Combine(newParent, subFile.Name);
                        if (File.Exists(newSubPath))
                        {
                            try
                            {
                                File.Delete(newSubPath);
                            }
                            catch (Exception e)
                            {
                                logger.Warn("Unable to DELETE file: {0} error {1}", subtitleFile, e);
                            }
                        }

                        try
                        {
                            subFile.MoveTo(newSubPath);
                        }
                        catch (Exception e)
                        {
                            logger.Error("Unable to MOVE file: {0} to {1} error {2}", subtitleFile, newSubPath, e);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex, ex.ToString());
                }
            }
            catch (Exception ex)
            {
                string msg = $"Could not MOVE file: {FullServerPath ?? VideoLocal_Place_ID.ToString()} -- {ex}";
                logger.Error(ex, msg);
            }
            ShokoServer.UnpauseWatchingFiles();
            return(true);
        }
Example #31
0
        /// <summary>
        /// Checks the index. If it does not exists, this will start
        /// creating a new index. This process is recoverable/restartable and
        /// can/should be called each time the Search is expected to be used.
        /// </summary>
        /// <param name="force">if set to <c>true</c> a re-indexing of all
        /// items is started.</param>
        public void CheckIndex(bool force)
        {
            if (!UseIndex)
            {
                return;
            }

            bool restartIndexing = false;
            bool fileBasedIndex  = this._settings.IndexPath != null &&
                                   Directory.Exists(this._settings.IndexPath);
            string   indexingStateFile = this.RestartIndexingStateFile;
            DateTime indexModifiedAt   = DateTime.MinValue;

            if (fileBasedIndex)
            {
                indexModifiedAt = Directory.GetLastWriteTimeUtc(this._settings.IndexPath);
                bool indexStateFileExists = (indexingStateFile != null && File.Exists(indexingStateFile));

                // if the index have to be (re-)created, we don't want to rely on old restart infos:
                if ((force || startIndexAll) && indexStateFileExists)
                {
                    FileHelper.Delete(indexingStateFile);
                }
                // restart indexing: read state and go on with the next non-indexed feed
                restartIndexing = (!startIndexAll && indexStateFileExists);
            }
            else
            {
                startIndexAll = true;
            }

            if (force || restartIndexing || startIndexAll)
            {
                IDictionary     restartInfo = null;
                DictionaryEntry lastIndexed = new DictionaryEntry();
                if (restartIndexing)
                {
                    // read indexState into restartInfo (persisted feed urls/id, that are yet indexed)
                    restartInfo = ReadIndexingRestartStateFileContent(indexingStateFile, out lastIndexed);
                }
                if (restartInfo == null)
                {
                    restartInfo = new HybridDictionary();
                }

                if (startIndexAll)
                {
                    this._indexModifier.CreateIndex();
                }

                LuceneIndexer indexer = GetIndexer();
                indexer.IndexingFinished += OnIndexingFinished;
                indexer.IndexingProgress += OnIndexingProgress;
                indexer.IndexAll(restartInfo, lastIndexed);
            }
            else if (fileBasedIndex)
            {
                // check, if we have to call OptimizeIndex():
                DateTime lastIndexModification = this._settings.LastIndexOptimization;
                int      compareResult         = indexModifiedAt.CompareTo(lastIndexModification);
                // indexModifiedAt is greater than lastIndexModification, hence index was modified:
                if (compareResult > 0)
                {
                    // attach event to get optimize index finished notification:
                    this._indexModifier.FinishedIndexOperation += OnIndexModifierFinishedIndexOperation;
                    this.IndexOptimize();
                }
                else if (compareResult != 0)
                {
                    // correct the bad persisted entry:
                    this._settings.LastIndexOptimization = Directory.GetLastWriteTimeUtc(this._settings.IndexPath);
                }
            }
        }