/// <summary>
    /// 获取下一个题目
    /// </summary>
    /// <param name="clientId">int 客户端编号</param>
    /// <returns>Hash 题目集合</returns>
    public static Hash Next(int clientId)
    {
        string sql = "SELECT td.*,IFNULL(tcd.result, -1) AS result " +
                     "FROM tq_question td LEFT JOIN tc_client_question tcd ON td.questionId=tcd.questionId AND clientId=@0 " +
                     "WHERE IFNULL(tcd.result, -1) IN(-1, 0, 2) " +
                     "ORDER BY IFNULL(tcd.result, -1) DESC, td.degree ASC ";

        using (MySqlADO ado = new MySqlADO())
        {
            HashCollection items = ado.GetHashCollection(sql, clientId).ToHashCollection("data");
            if (items.Count > 0)
            {
                int degree    = items[0].ToInt("degree");
                int maxDegree = 1;
                for (int i = 0; i < items.Count; i++)
                {
                    if (items[i].ToInt("degree") > degree)
                    {
                        maxDegree = i;
                        break;
                    }
                }
                if (items[0].ToInt("result") == 0 || items[0].ToInt("result") == 2)
                {
                    return(items[0]);
                }
                Random random = new Random();
                return(items[random.Next(0, maxDegree)]);
            }
            return(new Hash());
        }
    }
        /// <summary>
        /// Compute jaccard index: |A inter B| / |A union B|.
        /// </summary>
        /// <param name="s1">The first string to compare.</param>
        /// <param name="s2">The second string to compare.</param>
        /// <returns>The Jaccard index in the range [0, 1]</returns>
        /// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
        public double Similarity(string s1, string s2)
        {
            if (s1 == null)
            {
                throw new ArgumentNullException(nameof(s1));
            }

            if (s2 == null)
            {
                throw new ArgumentNullException(nameof(s2));
            }

            if (s1.Equals(s2))
            {
                return(1);
            }

            var profile1 = GetProfile(s1);
            var profile2 = GetProfile(s2);

            //var union = new HashSet<string>();
            var union = new HashCollection <string>();

            union.UnionWith(profile1.Keys);
            union.UnionWith(profile2.Keys);

            int inter = profile1.Keys.Count + profile2.Keys.Count
                        - union.Count;

            return(1.0 * inter / union.Count);
        }
Example #3
0
        /// <summary>
        /// Compute QGram distance using precomputed profiles.
        /// </summary>
        /// <param name="profile1"></param>
        /// <param name="profile2"></param>
        /// <returns></returns>
        public double Distance(IDictionary <string, int> profile1, IDictionary <string, int> profile2)
        {
            //var union = new HashSet<string>();
            var union = new HashCollection <string>();

            union.UnionWith(profile1.Keys);
            union.UnionWith(profile2.Keys);

            int agg = 0;

            foreach (var key in union)
            {
                int v1 = 0;
                int v2 = 0;

                if (profile1.TryGetValue(key, out var iv1))
                {
                    v1 = iv1;
                }

                if (profile2.TryGetValue(key, out var iv2))
                {
                    v2 = iv2;
                }

                agg += Math.Abs(v1 - v2);
            }

            return(agg);
        }
Example #4
0
        public void TestAdd()
        {
            HashCollection <int> hashCollection = new HashCollection <int>();

            Assert.IsTrue(hashCollection.Add(3));
            Assert.IsFalse(hashCollection.Add(3));
        }
Example #5
0
        public void TestICollectionSyncMembers()
        {
            ICollection a = new HashCollection <int> {
                1, 2
            };

            Assert.AreSame(a, a.SyncRoot);
            Assert.IsFalse(a.IsSynchronized);
        }
Example #6
0
        private void btnCompare_Click(object sender, EventArgs e)
        {
            _bmp1.Save("bmp1.png");
            _bmp2.Save("bmp2.png");
            var pct = HashCollection.CompareHashes(_bmp1.GetHash(_scaleSize, "bmp1"), _bmp2.GetHash(_scaleSize, "bmp2"));

            lblPercentMatch.Text    = Math.Round(pct, 3) + "%";
            lblPercentMatch.Visible = true;
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationManager"/> class.
        /// </summary>
        protected ValidationManager()
        {
            this.objects           = new HashCollection <ISupportValidation> ();
            this.validationResults = new Dictionary <ISupportValidation, HashCollection <ValidationResult> > ();
            this.validationQueue   = new HashList <ISupportValidation> ();
            this.queueResetEvent   = new ManualResetEvent(false);
            this.asyncOperation    = AsyncOperationManager.CreateOperation(null);
            this.status            = ValidationStatus.Ready;

            this.BeginValidation();
        }
    /// <summary>
    /// 获取配置信息
    /// </summary>
    /// <returns>Hash 配置信息</returns>
    public static Hash Detail()
    {
        string sql = "SELECT * FROM tz_setting";

        using (MySqlADO ado = new MySqlADO())
        {
            Hash           result = new Hash();
            HashCollection items  = ado.GetHashCollection(sql).ToHashCollection("data");
            foreach (Hash item in items)
            {
                result[item.ToString("key")] = item.ToString("value");
            }
            return(result);
        }
    }
    /// <summary>
    /// 获取下一个题目
    /// </summary>
    /// <param name="token">Hash 客户端信息</param>
    /// <returns>Hash 结果信息</returns>
    public static Hash Assign(Hash token)
    {
        Random         random = new Random();
        Hash           data   = ClientDinosaurData.Assign(token.ToInt("clientId"));
        HashCollection names  = DinosaurData.AllName(0).ToHashCollection("data");

        data["length"]   = names.Count;
        data["duration"] = token.ToInt("duration");
        data["score"]    = token.ToInt("score");
        data["cards"]    = token.ToInt("cards");
        data["status"]   = token.ToInt("status");

        int indexAt = random.Next(0, 4);

        if (data.ToInt("dinosaurId") > 0)
        {
            HashCollection options = new HashCollection();

            //  剔除当前题目
            for (int i = 0; i < names.Count; i++)
            {
                if (names[i].ToString("name") == data.ToString("name"))
                {
                    names.RemoveAt(i);
                }
            }

            //  添加选项
            for (int i = 0; i < 4; i++)
            {
                if (i == indexAt)
                {
                    options.Add(new Hash("{\"name\":\"" + data.ToString("name") + "\"}"));
                }
                else
                {
                    int index = random.Next(0, names.Count);
                    options.Add(names[i]);
                    names.RemoveAt(index);
                }
            }

            data["options"] = options;

            return(new Hash((int)CodeType.OK, "成功", data));
        }
        return(new Hash((int)CodeType.OK, "所有题目都答完了", data));
    }
Example #10
0
        public void TestIntersect()
        {
            HashCollection <int> a = new HashCollection <int> {
                1, 2
            };
            HashCollection <int> b = new HashCollection <int> {
                2, 3
            };
            ISet c = a.Intersect(b);

            Assert.IsNotNull(c);
            Assert.IsFalse(c.Contains(1));
            Assert.IsTrue(c.Contains(2));
            Assert.IsFalse(c.Contains(3));
            Assert.IsFalse(c.Contains(4));
        }
Example #11
0
        public void TestEquality()
        {
            HashCollection <int> a = new HashCollection <int> {
                1, 2
            };
            HashCollection <int> b = new HashCollection <int> {
                2, 3
            };
            HashCollection <int> c = new HashCollection <int> {
                1, 2
            };

            Assert.IsFalse(a == b);
            Assert.IsTrue(a == c);
            Assert.IsFalse(null == a);
            Assert.IsFalse(a == null);

            Assert.IsTrue(a != b);
            Assert.IsFalse(a != c);
            Assert.IsTrue(null != a);
            Assert.IsTrue(a != null);

            Assert.AreNotEqual(a, b);
            Assert.AreEqual(a, c);
            Assert.AreEqual(a.GetHashCode(), c.GetHashCode());

            Assert.IsFalse(a.Equals(b));
            Assert.IsTrue(a.Equals(c));
            Assert.IsFalse(a.Equals(null));

            Assert.IsFalse(a.Equals((ISet)b));
            Assert.IsTrue(a.Equals((ISet)c));
            Assert.IsFalse(a.Equals((ISet) new HashCollection <int> {
                1
            }));
            Assert.IsFalse(a.Equals((ISet)null));

            Assert.IsFalse(a.Equals((IEnumerable)b));
            Assert.IsTrue(a.Equals((IEnumerable)c));
            Assert.IsFalse(a.Equals((IEnumerable)null));

            Assert.IsFalse(a.Equals(new[] { 2, 3 }));
            Assert.IsTrue(a.Equals(new[] { 1, 2 }));
            Assert.IsFalse(a.Equals((IEnumerable <int>)null));
        }
Example #12
0
 private void btnWriteHashes_Click(object sender, EventArgs e)
 {
     if (saveFileDialog.ShowDialog() == DialogResult.OK)
     {
         var hashes = new HashCollection();
         var files  = Directory.GetFiles(txtImageFolder.Text);
         foreach (var file in files)
         {
             int[] hash;
             using (Bitmap bmp = new Bitmap(file)) {
                 hash = bmp.GetHash(_scaleSize);
             }
             hashes.Add(new HashEntry {
                 Filename = file, Hash = hash
             });
         }
         hashes.Write(saveFileDialog.FileName);
     }
 }
        public iTunesRatingControl(string artfolder, string[] stringsToRemove, string hashFile, string statsfile) : this()
        {
            _artfolder       = artfolder;
            _stringsToRemove = stringsToRemove;
            _hashes          = HashCollection.Load(hashFile);
            _hashFile        = hashFile;
            _statsfile       = statsfile;
            if (File.Exists(_statsfile))
            {
                var lines = File.ReadAllLines(_statsfile);
                foreach (string line in lines)
                {
                    var vals = line.Split('=');
                    if (vals.Length != 2)
                    {
                        continue;
                    }
                    switch (vals[0].Trim().ToLower())
                    {
                    case "1starcount":
                        _ratingCounters[0] = int.Parse(vals[1]);
                        break;

                    case "2starcount":
                        _ratingCounters[1] = int.Parse(vals[1]);
                        break;

                    case "3starcount":
                        _ratingCounters[2] = int.Parse(vals[1]);
                        break;

                    case "4starcount":
                        _ratingCounters[3] = int.Parse(vals[1]);
                        break;

                    case "5starcount":
                        _ratingCounters[4] = int.Parse(vals[1]);
                        break;
                    }
                }
            }
        }
Example #14
0
        /// <summary>
        /// Creates a hash collection that represents the state of all modules in this list. If
        /// a module has no current output or is missing files then a null collection will be returned
        /// </summary>
        /// <param name="Modules"></param>
        /// <returns></returns>
        private static HashCollection HashModules(IEnumerable <string> Modules, bool WarnOnFailure = true)
        {
            HashCollection Hashes = new HashCollection();

            foreach (string Module in Modules)
            {
                // this project is built by the AutomationTool project that the RunUAT script builds so
                // it will always be newer than was last built
                if (Module.Contains("AutomationUtils.Automation"))
                {
                    continue;
                }

                CsProjectInfo Proj;

                Dictionary <string, string> Properties = new Dictionary <string, string>();
                Properties.Add("Platform", "AnyCPU");
                Properties.Add("Configuration", "Development");

                FileReference ModuleFile = new FileReference(Module);

                if (!CsProjectInfo.TryRead(ModuleFile, Properties, out Proj))
                {
                    if (WarnOnFailure)
                    {
                        Log.TraceWarning("Failed to read file {0}", ModuleFile);
                    }
                    return(null);
                }

                if (!Hashes.AddCsProjectInfo(Proj, HashCollection.HashType.MetaData))
                {
                    if (WarnOnFailure)
                    {
                        Log.TraceWarning("Failed to hash file {0}", ModuleFile);
                    }
                    return(null);
                }
            }

            return(Hashes);
        }
Example #15
0
        public void TestISetMethods()
        {
            HashCollection <int> hashCollection = new HashCollection <int>();
            ISet set = hashCollection;

            set.Add(1);
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));

            set.UnionWith(new[] { 2 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsTrue(hashCollection.Contains(2));

            set.IntersectWith(new[] { 2 });
            Assert.IsFalse(hashCollection.Contains(1));
            Assert.IsTrue(hashCollection.Contains(2));

            hashCollection.Add(1);
            set.SymmetricExceptWith(new[] { 2, 3 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsTrue(hashCollection.Contains(3));

            set.ExceptWith(new[] { 2, 3 });
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsFalse(hashCollection.Contains(3));

            Assert.IsTrue(set.IsSupersetOf(Array <int> .Empty));
            Assert.IsFalse(set.IsSupersetOf(new[] { 2 }));
            Assert.IsTrue(set.IsSubsetOf(new[] { 1, 2 }));
            Assert.IsFalse(set.IsSubsetOf(new[] { 2 }));

            Assert.IsTrue(set.IsProperSupersetOf(Array <int> .Empty));
            Assert.IsFalse(set.IsProperSupersetOf(set));
            Assert.IsTrue(set.IsProperSubsetOf(new[] { 1, 2 }));
            Assert.IsFalse(set.IsProperSubsetOf(set));

            Assert.IsTrue(set.Overlaps(new[] { 1, 2 }));
            Assert.IsFalse(set.Overlaps(new[] { 3, 4 }));
        }
    /// <summary>
    /// 获取剩余题目
    /// </summary>
    /// <param name="clientId">int 客户端编号</param>
    /// <returns>Hash 题目集合</returns>
    public static Hash Assign(int clientId)
    {
        string sql = "SELECT td.*,tcd.result " +
                     "FROM td_dinosaur td LEFT JOIN tc_client_dinosaur tcd ON td.dinosaurId=tcd.dinosaurId AND clientId=@0 " +
                     "WHERE IFNULL(tcd.result, 0) in (0,2) " +
                     "ORDER BY IFNULL(tcd.result, 0) DESC, td.index ASC ";

        using (MySqlADO ado = new MySqlADO())
        {
            HashCollection items = ado.GetHashCollection(sql, clientId).ToHashCollection("data");
            if (items.Count > 0)
            {
                if (items[0].ToInt("result") == 2)
                {
                    return(items[0]);
                }
                Random random = new Random();
                return(items[random.Next(0, items.Count)]);
            }
            return(new Hash());
        }
    }
Example #17
0
        /// <summary>
        /// Removes an object from the Validation Manager.
        /// </summary>
        /// <param name="obj">The object to be removed.</param>
        protected void RemoveObject(ISupportValidation obj)
        {
            lock (this.objects)
            {
                lock (this.validationResults)
                {
                    lock (this.validationQueue)
                    {
                        if (this.objects.Contains(obj))
                        {
                            this.objects.Remove(obj);
                            ISupportValidationNotification notifyValidityAffected = obj as ISupportValidationNotification;
                            if (notifyValidityAffected != null)
                            {
                                notifyValidityAffected.ValidityAffected -= this.OnValidityAffected;
                            }

                            HashCollection <ValidationResult> validationResults = this.validationResults[obj];
                            foreach (ValidationResult validationResult in new List <ValidationResult> (validationResults))
                            {
                                validationResults.Remove(validationResult);
                                ValidationResult   currentValidationResult = validationResult;
                                SendOrPostCallback raiseRemovedEvent       = delegate
                                {
                                    this.RaiseValidationResultRemovedEvent(currentValidationResult);
                                };
                                this.asyncOperation.Post(raiseRemovedEvent, null);
                            }
                            this.validationResults.Remove(obj);

                            if (this.validationQueue.Contains(obj))
                            {
                                this.validationQueue.Remove(obj);
                            }
                        }
                    }
                }
            }
        }
Example #18
0
        public void TestICollectionCopyTo()
        {
            int[]       array      = new int[2];
            ICollection collection = new HashCollection <int> {
                1, 2
            };

            collection.CopyTo(array, 0);
            Assert.IsTrue(array[0] == 1 || array[0] == 1);
            Assert.IsTrue(array[1] == 2 || array[1] == 2);

            array      = new[] { -1, -2, -3, -4, -5 };
            collection = new HashCollection <int> {
                1, 2
            };
            collection.CopyTo(array, 2);
            Assert.IsTrue(array[2] == 1 || array[3] == 1);
            Assert.IsTrue(array[2] == 2 || array[3] == 2);
            Assert.AreEqual(-1, array[0]);
            Assert.AreEqual(-2, array[1]);
            Assert.AreEqual(-5, array[4]);
        }
    /// <summary>
    /// 获取排行榜信息
    /// </summary>
    /// <param name="token">Hash 客户端信息</param>
    /// <returns>Hash 排行榜</returns>
    public static Hash Rank(Hash token)
    {
        Hash           data   = new Hash();
        HashCollection all    = ClientData.GetRank(token.ToInt("clientId")).ToHashCollection("data");
        HashCollection global = new HashCollection();
        HashCollection friend = new HashCollection();

        for (int i = 0; i < all.Count; i++)
        {
            if (i < 100)
            {
                global.Add(all[i]);
            }
            if (all[i].ToInt("relateType") > 0 || all[i].ToInt("clientId") == token.ToInt("clientId"))
            {
                friend.Add(all[i]);
            }
        }
        data["global"] = global;
        data["friend"] = friend;
        return(new Hash((int)CodeType.OK, "成功", data));
    }
Example #20
0
        /// <summary>
        /// Pulls all dependencies from the specified module list, gathers the state of them, and
        /// compares it to the state expressed in the provided file from a previous run
        /// </summary>
        /// <param name="ModuleList"></param>
        /// <param name="DependencyFile"></param>
        /// <returns></returns>
        private static bool AreDependenciesUpToDate(IEnumerable <string> ModuleList, string DependencyFile)
        {
            bool UpToDate = false;

            if (File.Exists(DependencyFile))
            {
                Log.TraceVerbose("Read dependency file at {0}", DependencyFile);

                HashCollection OldHashes = HashCollection.CreateFromFile(DependencyFile);

                if (OldHashes != null)
                {
                    HashCollection CurrentHashes = HashModules(ModuleList, false);

                    if (OldHashes.Equals(CurrentHashes))
                    {
                        UpToDate = true;
                    }
                    else
                    {
                        if (Log.OutputLevel >= LogEventType.VeryVerbose)
                        {
                            CurrentHashes.LogDifferences(OldHashes);
                        }
                    }
                }
                else
                {
                    Log.TraceInformation("Failed to read dependency info!");
                }
            }
            else
            {
                Log.TraceVerbose("No dependency file exists at {0}. Will do full build.", DependencyFile);
            }

            return(UpToDate);
        }
        /// <summary>
        /// Similarity is computed as 2 * |A inter B| / (|A| + |B|).
        /// </summary>
        /// <param name="s1">The first string to compare.</param>
        /// <param name="s2">The second string to compare.</param>
        /// <returns>The computed Sorensen-Dice similarity.</returns>
        /// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
        public double Similarity(string s1, string s2)
        {
            if (s1 == null)
            {
                throw new ArgumentNullException(nameof(s1));
            }

            if (s2 == null)
            {
                throw new ArgumentNullException(nameof(s2));
            }

            if (s1.Equals(s2))
            {
                return(1);
            }

            var profile1 = GetProfile(s1);
            var profile2 = GetProfile(s2);

            //var union = new HashSet<string>();
            var union = new HashCollection <string>();

            union.UnionWith(profile1.Keys);
            union.UnionWith(profile2.Keys);

            int inter = 0;

            foreach (var key in union)
            {
                if (profile1.ContainsKey(key) && profile2.ContainsKey(key))
                {
                    inter++;
                }
            }

            return(2.0 * inter / (profile1.Count + profile2.Count));
        }
Example #22
0
        public void TestInitialise()
        {
            HashCollection <int> hashCollection = new HashCollection <int> {
                1
            };

            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsFalse(hashCollection.Contains(3));

            hashCollection = new HashCollection <int>(new Modulo2Comparer())
            {
                1
            };
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsTrue(hashCollection.Contains(3));

            hashCollection = new HashCollection <int>(new[] { 1 }, new Modulo2Comparer());
            Assert.IsTrue(hashCollection.Contains(1));
            Assert.IsFalse(hashCollection.Contains(2));
            Assert.IsTrue(hashCollection.Contains(3));
        }
	// Constructors
	public HashEnumerator(HashCollection hashCollection) {}
Example #24
0
        private void ProcessingThread(string[] files)
        {
            void UpdateProgBar(int c, int newmax = -1, int secondarycount = -1)
            {
                this.AsyncInvokeIfRequired(() => {
                    if (newmax >= 0)
                    {
                        progbar.Maximum = newmax;
                    }
                    progbar.Value     = c;
                    lblFileCount.Text = c + (secondarycount >= 0 ? ": " + secondarycount : "");
                });
            }

            int counter = 0;

            void UpdateProgBarCounter()
            {
                ++counter;
                if (counter % 10 == 0)
                {
                    this.AsyncInvokeIfRequired(() => {
                        progbar.Value     = counter;
                        lblFileCount.Text = counter.ToString();
                    });
                }
            }

            int started  = 0;
            int finished = 0;

            void LoadSomeFiles(object data)
            {
                ++started;
                var f        = (string[])((object[])data)[0];
                var start    = (int)((object[])data)[1];
                var len      = (int)((object[])data)[2];
                var hashcoll = (HashCollection)((object[])data)[3];

                for (int j = 0; j < len; ++j)
                {
                    if (_closed || _stop)
                    {
                        return;
                    }
                    using (Bitmap bmp = LoadBitmap(f[j + start])) {
                        var hash = bmp.GetHash(_scaleSize);
                        lock (hashcoll) {
                            hashcoll.Add(new HashEntry {
                                Filename = f[j + start], Hash = hash
                            });
                        }
                    }
                    UpdateProgBarCounter();
                    Thread.Sleep(0);
                }
                ++finished;
            }

            var hashes = new HashCollection();

            for (int k = 0; k < files.Length; k += 50)
            {
                new Thread(LoadSomeFiles).Start(new object[] { files, k, Math.Min(50, files.Length - k), hashes });
            }
            while (started == 0)
            {
                if (_closed || _stop)
                {
                    return;
                }
                Thread.Sleep(0);
            }
            while (started != finished)
            {
                if (_closed || _stop)
                {
                    return;
                }
                Thread.Sleep(0);
            }
            //int i = 0;
            //foreach (var file in files) {
            //    if (_closed || _stop) return;
            //    using (Bitmap bmp = LoadBitmap(file)) {
            //        var hash = bmp.GetHash(_scaleSize);
            //        hashes.Add(new HashEntry {Filename = file, Hash = hash});
            //        UpdateProgBar(++i);
            //    }
            //    Thread.Sleep(0);
            //}
            UpdateProgBar(0, hashes.Count);
            var minpct = (double)numMinPercent.Value - 0.0001;

            for (int i = 0; i < hashes.Count; ++i)
            {
                var hash1 = hashes[i];
                for (int j = i + 1; j < hashes.Count; ++j)
                {
                    if (_closed || _stop)
                    {
                        return;
                    }
                    var hash2 = hashes[j];
                    var pct   = HashCollection.CompareHashes(hash1.Hash, hash2.Hash);
                    if (pct >= minpct)
                    {
                        this.AsyncInvokeIfRequired(() => {
                            string s = hash1.FilenameNoPath + " = " + hash2.FilenameNoPath;
                            if (listMatches.Items.Cast <object>().ToArray().Any(o => o.ToString() == s))
                            {
                                return;
                            }
                            listMatches.Items.Add(new ListBoxEntry {
                                Text = s, Hashes = new[] { hash1, hash2 }
                            });
                        });
                    }
                    if (j % 100 == 0)
                    {
                        UpdateProgBar(i, -1, j);
                        Thread.Sleep(10);
                    }
                }
                UpdateProgBar(i + 1);
            }
            this.AsyncInvokeIfRequired(() => {
                progbar.Visible      = false;
                lblFileCount.Visible = false;
            });
        }
Example #25
0
        private IEnumerable <Hash> ParityEncoding(IEnumerable <ArraySegment <byte> > buffers, HashAlgorithm hashAlgorithm, CorrectionAlgorithm correctionAlgorithm, CancellationToken token)
        {
            lock (_parityEncodingLockObject)
            {
                if (correctionAlgorithm == CorrectionAlgorithm.ReedSolomon8)
                {
                    if (buffers.Count() > 128)
                    {
                        throw new ArgumentOutOfRangeException(nameof(buffers));
                    }

                    var createBuffers = new List <ArraySegment <byte> >();

                    try
                    {
                        var targetBuffers = new ArraySegment <byte> [buffers.Count()];
                        var parityBuffers = new ArraySegment <byte> [buffers.Count()];

                        int blockLength = buffers.Max(n => n.Count);

                        // Normalize
                        {
                            int index = 0;

                            foreach (var buffer in buffers)
                            {
                                token.ThrowIfCancellationRequested();

                                if (buffer.Count < blockLength)
                                {
                                    var tempBuffer = new ArraySegment <byte>(_bufferManager.TakeBuffer(blockLength), 0, blockLength);
                                    Unsafe.Copy(buffer.Array, buffer.Offset, tempBuffer.Array, tempBuffer.Offset, buffer.Count);
                                    Unsafe.Zero(tempBuffer.Array, tempBuffer.Offset + buffer.Count, tempBuffer.Count - buffer.Count);

                                    createBuffers.Add(tempBuffer);

                                    targetBuffers[index] = tempBuffer;
                                }
                                else
                                {
                                    targetBuffers[index] = buffer;
                                }

                                index++;
                            }
                        }

                        for (int i = 0; i < parityBuffers.Length; i++)
                        {
                            parityBuffers[i] = new ArraySegment <byte>(_bufferManager.TakeBuffer(blockLength), 0, blockLength);
                        }

                        var indexes = new int[parityBuffers.Length];

                        for (int i = 0; i < parityBuffers.Length; i++)
                        {
                            indexes[i] = targetBuffers.Length + i;
                        }

                        using (var reedSolomon = new ReedSolomon8(targetBuffers.Length, targetBuffers.Length + parityBuffers.Length, _threadCount, _bufferManager))
                        {
                            reedSolomon.Encode(targetBuffers, parityBuffers, indexes, blockLength, token).Wait();
                        }

                        token.ThrowIfCancellationRequested();

                        var parityHashes = new HashCollection();

                        for (int i = 0; i < parityBuffers.Length; i++)
                        {
                            Hash hash;

                            if (hashAlgorithm == HashAlgorithm.Sha256)
                            {
                                hash = new Hash(HashAlgorithm.Sha256, Sha256.Compute(parityBuffers[i]));
                            }
                            else
                            {
                                throw new NotSupportedException();
                            }

                            _blocksManager.Lock(hash);
                            _blocksManager.Set(hash, parityBuffers[i]);

                            parityHashes.Add(hash);
                        }

                        return(parityHashes);
                    }
                    finally
                    {
                        foreach (var buffer in createBuffers)
                        {
                            if (buffer.Array == null)
                            {
                                continue;
                            }

                            _bufferManager.ReturnBuffer(buffer.Array);
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }
Example #26
0
        /// <summary>
        /// Compiles all script modules.
        /// </summary>
        /// <param name="Modules">Module project filenames.</param>
        private static void CompileModules(List <string> Modules)
        {
            string DependencyFile = Path.Combine(CommandUtils.CmdEnv.EngineSavedFolder, "UATModuleHashes.xml");

            if (AreDependenciesUpToDate(Modules, DependencyFile) && !GlobalCommandLine.IgnoreDependencies)
            {
                Log.TraceInformation("Dependencies are up to date. Skipping compile.");
                return;
            }

            Log.TraceInformation("Dependencies are out of date. Compiling scripts....");

            // clean old assemblies
            CleanupScriptsAssemblies();

            DateTime StartTime = DateTime.Now;

            string BuildTool = CommandUtils.CmdEnv.MsBuildExe;

            // msbuild (standard on windows, in mono >=5.0 is preferred due to speed and parallel compilation)
            bool UseParallelMsBuild = Path.GetFileNameWithoutExtension(BuildTool).ToLower() == "msbuild";

            if (UseParallelMsBuild)
            {
                string ModulesList = string.Join(";", Modules);

                // Mono has an issue where arugments with semicolons or commas can't be passed through to
                // as arguments so we need to manually construct a temp file with the list of modules
                // see (https://github.com/Microsoft/msbuild/issues/471)
                var UATProjTemplate = CommandUtils.CombinePaths(CommandUtils.CmdEnv.LocalRoot, @"Engine\Source\Programs\AutomationTool\Scripts\UAT.proj");
                var UATProjFile     = Path.Combine(CommandUtils.CmdEnv.EngineSavedFolder, "UATTempProj.proj");

                string ProjContents = File.ReadAllText(UATProjTemplate);
                ProjContents = ProjContents.Replace("$(Modules)", ModulesList);

                Directory.CreateDirectory(Path.GetDirectoryName(UATProjFile));
                File.WriteAllText(UATProjFile, ProjContents);

                string MsBuildVerbosity = Log.OutputLevel >= LogEventType.Verbose ? "minimal" : "quiet";

                var CmdLine = String.Format("\"{0}\" /p:Configuration={1} /verbosity:{2} /nologo", UATProjFile, BuildConfig, MsBuildVerbosity);
                // suppress the run command because it can be long and intimidating, making the logs around this code harder to read.
                var Result = CommandUtils.Run(BuildTool, CmdLine, Options: CommandUtils.ERunOptions.Default | CommandUtils.ERunOptions.NoLoggingOfRunCommand | CommandUtils.ERunOptions.LoggingOfRunDuration);
                if (Result.ExitCode != 0)
                {
                    throw new AutomationException(String.Format("Failed to build \"{0}\":{1}{2}", UATProjFile, Environment.NewLine, Result.Output));
                }
            }
            else
            {
                // Make sure DefaultScriptsDLLName is compiled first
                var DefaultScriptsProjName = Path.ChangeExtension(DefaultScriptsDLLName, "csproj");

                // Primary modules must be built first
                List <string> PrimaryModules = Modules.Where(M => M.IndexOf(DefaultScriptsProjName, StringComparison.InvariantCultureIgnoreCase) >= 0).ToList();

                foreach (var ModuleName in PrimaryModules)
                {
                    Log.TraceInformation("Building script module: {0}", ModuleName);
                    try
                    {
                        CompileScriptModule(ModuleName);
                    }
                    catch (Exception Ex)
                    {
                        CommandUtils.LogError(LogUtils.FormatException(Ex));
                        throw new AutomationException("Failed to compile module {0}", ModuleName);
                    }
                    break;
                }

                // Second pass, compile everything else
                List <string> SecondaryModules = Modules.Where(M => !PrimaryModules.Contains(M)).ToList();

                // Non-parallel method
                foreach (var ModuleName in SecondaryModules)
                {
                    Log.TraceInformation("Building script module: {0}", ModuleName);
                    try
                    {
                        CompileScriptModule(ModuleName);
                    }
                    catch (Exception Ex)
                    {
                        CommandUtils.LogError(LogUtils.FormatException(Ex));
                        throw new AutomationException("Failed to compile module {0}", ModuleName);
                    }
                }
            }

            TimeSpan Duration = DateTime.Now - StartTime;

            Log.TraceInformation("Compiled {0} modules in {1} secs", Modules.Count, Duration.TotalSeconds);

            HashCollection NewHashes = HashModules(Modules);

            if (NewHashes == null)
            {
                Log.TraceWarning("Failed to save dependency info!");
            }
            else
            {
                NewHashes.SaveToFile(DependencyFile);
                Log.TraceVerbose("Wrote depencencies to {0}", DependencyFile);
            }
        }
        private void SaveArtworkFile(IITArtwork art, string name)
        {
            try {
                string ext;
                switch (art.Format)
                {
                case ITArtworkFormat.ITArtworkFormatJPEG:
                    ext = "jpg";
                    break;

                case ITArtworkFormat.ITArtworkFormatPNG:
                    ext = "png";
                    break;

                case ITArtworkFormat.ITArtworkFormatBMP:
                    ext = "bmp";
                    break;

                default: return;
                }
                name = name.NormalizeFilename();
                string filepath = Path.Combine(_artfolder, name + ".");
                // don't write a jpg if there's already a png, no matter how different they look to CompareImages.
                if (!File.Exists(filepath + "jpg") && !File.Exists(filepath + "png") && !File.Exists(filepath + "bmp"))
                {
                    art.SaveArtworkToFile(filepath + ext);
                    int[] hash;
                    using (Bitmap bmp = new Bitmap(filepath + ext)) {
                        hash = bmp.GetHash(10);
                    }
                    bool found = false;
                    foreach (var h in _hashes)
                    {
                        if (HashCollection.CompareHashes(hash, h.Hash) >= 97)
                        {
                            found = true;
                            File.Delete(filepath + ext);
                        }
                    }
                    if (!found)
                    {
                        _hashes.Add(new HashEntry {
                            Filename = filepath + ext, Hash = hash
                        });
                        _hashes.Write(_hashFile);
                    }
                }
                else
                {
                    string fname = File.Exists(filepath + "jpg") ? filepath + "jpg" : File.Exists(filepath + "png") ? filepath + "png" : filepath + "bmp";
                    int[]  hash;
                    using (Bitmap bmp = new Bitmap(fname)) {
                        hash = bmp.GetHash(10);
                    }
                    if (!_hashes.Any(h => HashCollection.CompareHashes(h.Hash, hash) >= 97))
                    {
                        _hashes.Add(new HashEntry {
                            Filename = fname, Hash = hash
                        });
                        _hashes.Write(_hashFile);
                    }
                }
            }
            catch (Exception ex) {
                if (!ex.Message.Contains("track has been deleted"))
                {
                    MessageBox.Show("Exception in iTunesRatingControl: " + ex.Message);
                }
            }
        }
Example #28
0
        private void DecodingThread(CancellationToken token)
        {
            for (; ;)
            {
                if (token.WaitHandle.WaitOne(1000))
                {
                    return;
                }

                DownloadItemInfo item = null;

                lock (_lockObject)
                {
                    item = CollectionUtils.Unite(_volatileDownloadItemInfoManager, _downloadItemInfoManager)
                           .Where(n => !_workingItems.Contains(n))
                           .Where(n => n.State == DownloadState.Decoding || n.State == DownloadState.ParityDecoding)
                           .OrderBy(n => (n.Depth == n.Metadata.Depth) ? 0 : 1)
                           .OrderBy(n => (n.State == DownloadState.Decoding) ? 0 : 1)
                           .FirstOrDefault();

                    if (item != null)
                    {
                        _workingItems.Add(item);
                    }
                }

                if (item == null)
                {
                    continue;
                }

                try
                {
                    if ((item.Depth == 0 && !_cacheManager.Contains(item.Metadata.Hash)) ||
                        (item.Depth > 0 && !item.Index.Groups.All(n => _existManager.GetCount(n, true) >= n.Hashes.Count() / 2)))
                    {
                        item.State = DownloadState.Downloading;
                    }
                    else
                    {
                        var hashes      = new HashCollection();
                        var totalHashes = new HashCollection();

                        if (item.Depth == 0)
                        {
                            hashes.Add(item.Metadata.Hash);
                            totalHashes.Add(item.Metadata.Hash);
                        }
                        else
                        {
                            try
                            {
                                foreach (var group in item.Index.Groups)
                                {
                                    if (item.State == DownloadState.Error)
                                    {
                                        throw new OperationCanceledException();
                                    }

                                    hashes.AddRange(_cacheManager.ParityDecoding(group, token).Result);
                                }

                                totalHashes.AddRange(item.Index.Groups.SelectMany(n => n.Hashes));
                            }
                            catch (OperationCanceledException)
                            {
                                continue;
                            }

                            item.State = DownloadState.Decoding;
                        }

                        if (item.Depth < item.Metadata.Depth)
                        {
                            Index index;

                            try
                            {
                                using (var stream = _cacheManager.Decoding(hashes))
                                    using (var progressStream = new ProgressStream(stream, null, 1024 * 1024, token))
                                    {
                                        if (item.State == DownloadState.Error)
                                        {
                                            throw new OperationCanceledException();
                                        }
                                        if (progressStream.Length > item.MaxLength)
                                        {
                                            throw new ArgumentException();
                                        }

                                        index = Index.Import(progressStream, _bufferManager);
                                    }
                            }
                            catch (OperationCanceledException)
                            {
                                continue;
                            }

                            lock (_lockObject)
                            {
                                if (item.Path != null)
                                {
                                    _protectCacheInfoManager.Add(new ProtectedCacheInfo(DateTime.UtcNow, totalHashes));
                                }

                                this.CheckState(index);
                                this.UncheckState(item.Index);

                                item.Index = index;

                                item.Depth++;

                                item.State = DownloadState.Downloading;
                            }
                        }
                        else
                        {
                            if (item.Path != null)
                            {
                                string filePath = null;

                                try
                                {
                                    token.ThrowIfCancellationRequested();

                                    string targetPath;

                                    if (Path.IsPathRooted(item.Path))
                                    {
                                        targetPath = item.Path;
                                    }
                                    else
                                    {
                                        targetPath = Path.GetFullPath(Path.Combine(_basePath, item.Path));

                                        // ディレクトリトラバーサル対策
                                        if (!targetPath.StartsWith(Path.GetFullPath(_basePath)))
                                        {
                                            targetPath = Path.GetFullPath(Path.Combine(_basePath, Path.GetFileName(item.Path)));
                                        }
                                    }

                                    Directory.CreateDirectory(Path.GetDirectoryName(targetPath));

                                    using (var inStream = _cacheManager.Decoding(hashes))
                                        using (var outStream = DownloadManager.GetUniqueFileStream(targetPath + ".tmp"))
                                            using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 1024))
                                            {
                                                filePath = outStream.Name;

                                                int readLength;

                                                while ((readLength = inStream.Read(safeBuffer.Value, 0, safeBuffer.Value.Length)) > 0)
                                                {
                                                    if (item.State == DownloadState.Error)
                                                    {
                                                        throw new OperationCanceledException();
                                                    }
                                                    token.ThrowIfCancellationRequested();

                                                    outStream.Write(safeBuffer.Value, 0, readLength);
                                                }
                                            }

                                    File.Move(filePath, DownloadManager.GetUniqueFilePath(targetPath));
                                }
                                catch (OperationCanceledException)
                                {
                                    if (filePath != null)
                                    {
                                        File.Delete(filePath);
                                    }

                                    continue;
                                }
                            }

                            lock (_lockObject)
                            {
                                if (item.Path != null)
                                {
                                    _protectCacheInfoManager.Add(new ProtectedCacheInfo(DateTime.UtcNow, totalHashes));
                                }

                                item.ResultHashes.AddRange(hashes);

                                item.State = DownloadState.Completed;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    item.State = DownloadState.Error;

                    Log.Error(e);
                }
                finally
                {
                    _workingItems.Remove(item);
                }
            }
        }
Example #29
0
 public bool AddAll(HashCollection c)
 {
     throw new RuntimeException();
 }
Example #30
0
 // Constructors
 public HashEnumerator(HashCollection hashCollection)
 {
 }
Example #31
0
        public HashCollection Values()
        {
            if (values == null)
            {
                values = new ValuesClass(this);
            }

            return values;
        }
Example #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EditorValidationManager&lt;T&gt;"/> class.
 /// </summary>
 /// <param name="model">The model to which the validation manager will be bound.</param>
 public EditorValidationManager(T model)
 {
     this.registeredObjects = new HashCollection <ModelObject> ();
     this.Model             = model;
 }
Example #33
0
        private void BeginValidation()
        {
            ThreadStart beginValidation = delegate
            {
                while (true)
                {
                    // Wait if there is no work to do
                    this.queueResetEvent.WaitOne();

                    ISupportValidation obj = null;
                    lock (this.validationQueue)
                    {
                        if (this.validationQueue.Count == 0)
                        {
                            this.Status = ValidationStatus.Ready;
                            this.queueResetEvent.Reset();
                            continue;
                        }
                        else
                        {
                            obj = this.validationQueue[0];
                            this.validationQueue.RemoveAt(0);
                        }
                    }

                    lock (this.validationResults)
                    {
                        // Try to get the validation results (they may have been removed by a different
                        // thread calling RemoveObject)
                        HashCollection <ValidationResult> oldValidationResults;
                        if (this.validationResults.TryGetValue(obj, out oldValidationResults))
                        {
                            HashCollection <ValidationResult> newValidationResults = new HashCollection <ValidationResult> ();
                            foreach (ValidationResult validationResult in obj.Validate())
                            {
                                if (!newValidationResults.Contains(validationResult))
                                {
                                    newValidationResults.Add(validationResult);
                                }
                            }

                            foreach (ValidationResult validationResult in new List <ValidationResult> (oldValidationResults))
                            {
                                if (!newValidationResults.Contains(validationResult))
                                {
                                    oldValidationResults.Remove(validationResult);
                                    ValidationResult   currentValidationResult = validationResult;
                                    SendOrPostCallback raiseRemovedEvent       = delegate
                                    {
                                        this.RaiseValidationResultRemovedEvent(currentValidationResult);
                                    };
                                    this.asyncOperation.Post(raiseRemovedEvent, null);
                                }
                            }

                            foreach (ValidationResult validationResult in newValidationResults)
                            {
                                if (!oldValidationResults.Contains(validationResult))
                                {
                                    oldValidationResults.Add(validationResult);
                                    ValidationResult   currentValidationResult = validationResult;
                                    SendOrPostCallback raiseAddedEvent         = delegate
                                    {
                                        this.RaiseValidationResultAddedEvent(currentValidationResult);
                                    };
                                    this.asyncOperation.Post(raiseAddedEvent, null);
                                }
                            }
                        }
                    }
                }
            };

            Thread thread = new Thread(beginValidation);

            thread.Name         = "Validation Manager";
            thread.IsBackground = true;
            thread.Priority     = ThreadPriority.BelowNormal;
            thread.Start();
        }