public void LoadAllVoteFilesFromDisk()
        {
            if (UseLocalFiles && VoteFilesDirectory != null)
            {
                VoteFiles.Clear();
                foreach (FileInfo item in VoteFilesDirectory.GetFiles())
                {
                    if (item.Name.EndsWith(".json"))
                    {
                        VoteFile newVF = null;
                        try { newVF = VoteFile.FromLoad(item.FullName, this); }
                        catch (Exception e) { App.Log(e.Message, this); }

                        if (newVF != null)
                        {
                            if (newVF.Validate(this))
                            {
                                VoteFiles.Add(newVF);
                            }
                        }
                    }
                }
                VoteFilesLoaded      = true;
                LastVoteFileLoadTime = DateTime.Now;
            }
        }
Beispiel #2
0
        public bool Equals(VoteFile p)
        {
            // If parameter is null, return false.
            if (Object.ReferenceEquals(p, null))
            {
                return(false);
            }

            // Optimization for a common success case.
            if (Object.ReferenceEquals(this, p))
            {
                return(true);
            }

            // If run-time types are not exactly the same, return false.
            if (this.GetType() != p.GetType())
            {
                return(false);
            }

            // Return true if the fields match.
            // Note that the base class is not invoked because it is
            // System.Object, which defines Equals as reference equality.
            return(this.Path == p.Path);
        }
Beispiel #3
0
 private static VoteFile LoadOrCreate(string path, ServerSettings settings)
 {
     //if file exists - load
     if (File.Exists(path))
     {
         try
         {
             string   json  = File.ReadAllText(path);
             VoteFile newVF = JsonConvert.DeserializeObject <VoteFile>(json);
             if (!newVF.IsValid)
             {
                 try
                 {
                     newVF = JsonConvert.DeserializeObject <VoteFile>(BackupVotingJson1);
                 }
                 catch (Exception e)
                 {
                     App.Log("Failed to serialize default vote file from backup json | " + path + " | " + e.Message, settings);
                 }
                 newVF.Validate(settings);
                 if (newVF != null && newVF.IsValid)
                 {
                     newVF.Path = path;
                     return(newVF);
                 }
                 else
                 {
                     return(null);
                 }
             }
             else
             {
                 return(newVF);
             }
         }
         catch (Exception e)
         {
             App.Log("Failed to load vote file \"" + path + "\" | " + e.Message, settings);
             return(null);
         }
     }
     //if not, create
     else
     {
         if (Directory.Exists(System.IO.Path.GetDirectoryName(path)))
         {
             VoteFile newVF = new VoteFile()
             {
                 Path = path
             };
             newVF.WriteToDisk();
             return(newVF);
         }
         else
         {
             return(null);
         }
     }
 }
        public VoteFile GetDynamicVoteFileObject()
        {
            if (!UseLocalFiles)
            {
                throw new Exception("Attempted to retrieve dynamic voting file, but dynamic voting file management is disabled.");
            }
            if (VoteFilesDirectory == null)
            {
                throw new Exception("Attempted to retrieve dynamic voting file, but voting files directory could not be located.");
            }

            VoteFile voteFile = null;

            if (VoteFiles.Count != 0)
            {
                voteFile = VoteFiles.DefaultIfEmpty(null)?.FirstOrDefault(x => x.Name == "dynamic.json");
            }
            if (voteFile == null)
            {
                // Not validating this one because it's intentionally blank at this point
                voteFile = VoteFile.Create(VoteFilesDirectory.FullName, "dynamic", this);
                if (voteFile == null)
                {
                    throw new Exception("Failed to write or retrieve dynamic voting file.");
                }
                else
                {
                    VoteFiles.Add(voteFile);
                    return(voteFile);
                }
            }
            else
            {
                return(voteFile);
            }
        }
 public void DeleteVoteFile(VoteFile file)
 {
     try { file.DeleteFromDisk(); }
     catch (Exception e) { App.Log(e.Message, this); }
     VoteFiles.Remove(file);
 }