コード例 #1
0
ファイル: Project.cs プロジェクト: rainerzufalldererste/TODO
        public Project(string name, string url, EAccessibility accessibility) : this()
        {
            Name          = name;
            URL           = url;
            Accessibility = accessibility;
            Folder        = Directory.GetParent(Environment.CurrentDirectory).FullName + "\\projects\\" + Name;

            Projects.Add(Name, this);

            new Thread(() =>
            {
                try
                {
                    Directory.CreateDirectory(Folder);
                    ExecuteInPath(Folder, Main.Configuration.GitPath, $"clone {URL} {Folder}");
                    UpdateRepository();
                    UpdateTasks();
                }
                catch (Exception e)
                {
                    Logger.LogError(e.SafeToString());
                }
            }).Start();
        }
コード例 #2
0
        public void ExecuteTestHashMap()
        {
            List <string> hashes = new List <string>();
            List <string> values = new List <string>();
            const int     size   = 1000;

            Console.WriteLine("Small Tests...");
            Assert.IsTrue(hashmap.Count == 0);
            hashmap.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(hashmap.ContainsKey("key"));
            Assert.IsFalse(hashmap.ContainsKey("value"));
            Assert.IsTrue(hashmap["key"] == "value");
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Validate();
            hashmap.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(hashmap.ContainsKey("key"));
            Assert.IsFalse(hashmap.ContainsKey("value"));
            Assert.IsTrue(hashmap["key"] == "value2");
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Validate();
            hashmap.Add(new KeyValuePair <string, string>("key", "avalue"));
            Assert.IsTrue(hashmap.ContainsKey("key"));
            Assert.IsFalse(hashmap.ContainsKey("value"));
            Assert.IsTrue(hashmap["key"] == "avalue");
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Validate();
            Assert.IsFalse(hashmap.Remove("value"));
            Assert.IsTrue(hashmap.Remove("key"));
            hashmap.Validate();
            Assert.IsTrue(hashmap.Count == 0);
            hashmap.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Clear();
            Assert.IsTrue(hashmap.Count == 0);
            hashmap.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(hashmap.ContainsKey("key"));
            Assert.IsFalse(hashmap.ContainsKey("value"));
            Assert.IsTrue(hashmap["key"] == "value");
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Validate();
            hashmap.Clear();
            Assert.IsFalse(hashmap.Remove(""));
            Assert.IsFalse(hashmap.Remove(new KeyValuePair <string, string>("", "")));

            Console.WriteLine("Adding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(hashmap.Count == i);
                hashes.Add(Hash.GetHash());
                values.Add(Hash.GetHash());
                hashmap[hashes[i]] = values[i];
                Assert.IsTrue(hashmap[hashes[i]] == values[i]);
                Assert.IsTrue(hashmap.Keys.Contains(hashes[i]));
                Assert.IsTrue(hashmap.Values.Contains(values[i]));
                hashmap.Validate();
            }

            Console.WriteLine("Overriding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(hashmap[hashes[i]] == values[i]);
                values[i]          = Hash.GetHash();
                hashmap[hashes[i]] = values[i];
                Assert.IsTrue(hashmap.Count == size);
            }

            Console.WriteLine("Checking...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(hashmap[hashes[i]] == values[i]);
                Assert.IsTrue(hashmap.Keys.Contains(hashes[i]));
                Assert.IsTrue(hashmap.Values.Contains(values[i]));
            }

            Console.WriteLine("Validating...");

            hashmap.Validate();

            Serializer.WriteXmlData(hashmap, nameof(hashmap));
            hashmap = Serializer.ReadXmlData <AVLHashMap <string, string> >(nameof(hashmap));

            hashmap.Validate();

            Console.WriteLine("Deleting...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(hashmap.Count == size - i);
                Assert.IsTrue(hashmap.ContainsKey(hashes[i]));
                Assert.IsTrue(hashmap[hashes[i]] != default(string));
                Assert.IsTrue(hashmap.Remove(hashes[i]));
                Assert.IsFalse(hashmap.Keys.Contains(hashes[i]));
                Assert.IsFalse(hashmap.Values.Contains(values[i]));

                if (true)
                {
                    for (int j = i + 1; j < size; j++)
                    {
                        Assert.IsFalse(hashmap[hashes[j]].Contains(hashes[j]));
                    }

                    for (int j = 0; j < i; j++)
                    {
                        Assert.IsFalse(hashmap.Remove(hashes[j]));
                    }
                }

                Assert.IsTrue(hashmap[hashes[i]] == default(string));
                hashmap.Validate();
            }

            Serializer.WriteXmlData(hashmap, nameof(hashmap));
            hashmap = Serializer.ReadXmlData <AVLHashMap <string, string> >(nameof(hashmap));

            hashmap.Validate();
        }
コード例 #3
0
        public void TestSerializeClassAvlHashMap()
        {
            Console.WriteLine("Building Class AVLHashMap...");

            AVLHashMap <Person, Couple> hashmap = new AVLHashMap <Person, Couple>();
            int count = 1000;

            for (int i = 0; i < count; i++)
            {
                Person a = new Person()
                {
                    age = i, name = "a" + i
                };
                Person b = new Person()
                {
                    age = i, name = "b" + i
                };
                Couple c = new Couple()
                {
                    man = a, woman = b
                };

                hashmap.Add(a, c);
                hashmap.Add(b, c);
            }

            Console.WriteLine("Serializing...");
            Serializer.WriteXmlData(hashmap, "hashmap");

            Console.WriteLine("Deserializing...");
            hashmap = Serializer.ReadXmlData <AVLHashMap <Person, Couple> >("hashmap");

            Console.WriteLine("Validating...");
            for (int i = 0; i < count; i++)
            {
                Assert.IsTrue(hashmap[new Person()
                                      {
                                          age = i, name = "a" + i
                                      }].woman.Equals(new Person()
                {
                    age = i, name = "b" + i
                }));
                Assert.IsTrue(hashmap[new Person()
                                      {
                                          age = i, name = "b" + i
                                      }].woman.Equals(new Person()
                {
                    age = i, name = "b" + i
                }));
                Assert.IsTrue(hashmap[new Person()
                                      {
                                          age = i, name = "a" + i
                                      }].man.Equals(new Person()
                {
                    age = i, name = "a" + i
                }));
                Assert.IsTrue(hashmap[new Person()
                                      {
                                          age = i, name = "b" + i
                                      }].man.Equals(new Person()
                {
                    age = i, name = "a" + i
                }));
            }
        }
コード例 #4
0
        private void ProcessFile(string file)
        {
            if (!(file.EndsWith(".dll") || file.EndsWith(".exe")))
            {
                return;
            }

            ServerHandler.LogMessage("[lwshost] [Processing File] " + file);

            try
            {
                string newFileName = Directory.GetCurrentDirectory() + "\\lwshost\\CurrentRun\\" + ID.ToHexString() + file.Replace(directoryPath, "");

                if (!Directory.Exists("lwshost\\CurrentRun\\" + ID.ToHexString()))
                {
                    Directory.CreateDirectory("lwshost\\CurrentRun\\" + ID.ToHexString());
                }

                File.Copy(file, newFileName);
                Thread.Sleep(100);
                file = newFileName;

                ServerHandler.LogMessage("[lwshost] [File Load] Created a local copy at " + newFileName);
            }
            catch (IOException e)
            {
                ServerHandler.LogMessage("[lwshost] [File Load] Failed to copy file to currentRun directory:\n" + e);
            }

            try
            {
                var assembly = Assembly.LoadFile(file);
                var types    = assembly.GetTypes();

                bool addedAnything = false;

                foreach (var type in types)
                {
                    bool ignoreDiscovery = false;

                    try
                    {
                        foreach (var attribute in type.GetCustomAttributes())
                        {
                            ignoreDiscovery |= (attribute is IgnoreDiscovery);
                        }

                        if (ignoreDiscovery)
                        {
                            ServerHandler.LogMessage($"[lwshost] [HostIgnore] Ignoring: {type.Namespace}.{type.Name}");
                            continue;
                        }
                        foreach (var interface_ in type.GetInterfaces())
                        {
                            try
                            {
                                if (interface_ == typeof(IURLIdentifyable))
                                {
                                    var constructor = type.GetConstructor(new Type[] {});

                                    if (constructor == null)
                                    {
                                        continue;
                                    }

                                    if (!addedAnything && TypesPerFile.ContainsKey(file))
                                    {
                                        foreach (var type_ in TypesPerFile[file])
                                        {
                                            foreach (var method_ in type_.GetMethods())
                                            {
                                                try
                                                {
                                                    if (method_.IsStatic && method_.IsPublic)
                                                    {
                                                        foreach (var attribute in method_.GetCustomAttributes())
                                                        {
                                                            if (attribute is ExecuteOnUnload)
                                                            {
                                                                new Thread(() =>
                                                                {
                                                                    try
                                                                    {
                                                                        ServerHandler.LogMessage(
                                                                            $"[lwshost] [File Load] Execute on Unload: {type_.Namespace}.{type_.Name}.{method_.Name} (in {file})");

                                                                        method_.Invoke(null, ((ExecuteOnUnload)attribute).Args);
                                                                    }
                                                                    catch (Exception e)
                                                                    {
                                                                        ServerHandler.LogMessage(
                                                                            $"[lwshost] [File Load] Failed to execute on unload: {type_.Namespace}.{type_.Name}.{method_.Name} (in {file})\n" +
                                                                            e);
                                                                    }
                                                                }).Start();

                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                                catch (Exception)
                                                {
                                                }
                                            }
                                        }

                                        TypesPerFile.Remove(file);
                                    }

                                    OnPageRegister(type.Namespace + "." + type.Name);

                                    new Thread(() =>
                                    {
                                        try
                                        {
                                            ServerHandler.LogMessage("[lwshost] [Adding / Updating] " + type.Namespace + "." + type.Name);
                                            constructor.Invoke(new object[0]);
                                        }
                                        catch (Exception e)
                                        {
                                            ServerHandler.LogMessage("[lwshost] [Error] Failed to add '" + type.Namespace + "." + type.Name + "'\n" + e);
                                        }
                                    }).Start();

                                    addedAnything = true;
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                foreach (var type in types)
                {
                    foreach (var method_ in type.GetMethods())
                    {
                        try
                        {
                            if (method_.IsStatic && method_.IsPublic)
                            {
                                foreach (var attribute in method_.GetCustomAttributes())
                                {
                                    if (attribute is ExecuteOnLoad)
                                    {
                                        new Thread(() =>
                                        {
                                            try
                                            {
                                                ServerHandler.LogMessage($"[lwshost] [File Load] Execute on Load: {type.Namespace}.{type.Name}.{method_.Name} (in {file})");
                                                method_.Invoke(null, ((ExecuteOnLoad)attribute).Args);
                                            }
                                            catch (Exception e)
                                            {
                                                ServerHandler.LogMessage(
                                                    $"[lwshost] [File Load] Failed to execute on Load: {type.Namespace}.{type.Name}.{method_.Name} (in {file})\n" + e);
                                            }
                                        }).Start();

                                        break;
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                }

                if (addedAnything)
                {
                    TypesPerFile.Add(file, types);
                }
            }
            catch (ReflectionTypeLoadException e)
            {
                ServerHandler.LogMessage("[lwshost] Failed to load Assembly " + file + " (" + e.LoaderExceptions.Length + " Loader Exceptions)");

                for (int i = 0; i < (e.LoaderExceptions.Length > 25 ? 20 : e.LoaderExceptions.Length); i++)
                {
                    ServerHandler.LogMessage($"[lwshost] ({(i + 1)}/{e.LoaderExceptions.Length}) {e.LoaderExceptions[i].Message}");
                }

                if (e.LoaderExceptions.Length > 25)
                {
                    ServerHandler.LogMessage(" [...] " + (e.LoaderExceptions.Length - 20) + " more Loader Exceptions.");
                }
            }
            catch (Exception e)
            {
                ServerHandler.LogMessage("[lwshost] Failed to load Assembly " + file + "\n" + e);
            }
        }
コード例 #5
0
ファイル: Project.cs プロジェクト: rainerzufalldererste/TODO
        public void UpdateTasks()
        {
            List <ProjectTask> _tasks = new List <ProjectTask>();

            Files.Clear();

            Repository repo          = new Repository(Folder);
            string     versionString = repo.Head.Tip.Id.Sha;

            var files = Directory.EnumerateFiles(Folder, "*", SearchOption.AllDirectories);

            foreach (string file in files)
            {
                bool anyContains = false;

                string fileName = file.Substring(Folder.Length);

                foreach (string ignored in IgnoredPaths)
                {
                    if (fileName.Contains(ignored))
                    {
                        anyContains = true;
                        break;
                    }
                }

                if (anyContains)
                {
                    continue;
                }

                try
                {
                    var lines = File.ReadAllLines(file);

                    int line = 1;

                    foreach (string l in lines)
                    {
                        if (l.Contains("// TODO: "))
                        {
                            _tasks.Add(new ProjectTask(fileName, line, lines, versionString));
                            Files.Add(fileName, lines);
                        }

                        line++;
                    }
                }
                catch (Exception e)
                {
                    Logger.LogError($"Failed to read file '{file}' with error '{e.Message}'.");
                }
            }

            List <UsableProjectTask> usableTasks = (from t in _tasks select new UsableProjectTask(t)).ToList();

            _tasks = null;

            Dictionary <ProjectTask, List <UsableProjectTask> > consumableTasks = new Dictionary <ProjectTask, List <UsableProjectTask> >();
            Dictionary <ProjectTask, bool> matchedTasks = new Dictionary <ProjectTask, bool>();

            foreach (var t in Tasks)
            {
                if (t.TaskState == ETaskState.Closed || t.TaskState == ETaskState.Resolved || t.TaskState == ETaskState.Dummy)
                {
                    continue;
                }

                bool nothingMatched = true;

                for (int i = usableTasks.Count - 1; i >= 0; i--)
                {
                    if (usableTasks[i].Task.Text == t.Text && usableTasks[i].Task.File == t.File)
                    {
                        if (!consumableTasks.ContainsKey(t))
                        {
                            consumableTasks[t] = new List <UsableProjectTask>();
                        }

                        usableTasks[i].LooselyMatched = true;
                        consumableTasks[t].Add(usableTasks[i]);
                        nothingMatched = false;
                    }
                }

                if (nothingMatched)
                {
                    t.Gone      = versionString;
                    t.TaskState = ETaskState.MaybeResolved;
                }
            }

            foreach (var ct in consumableTasks)
            {
                foreach (var t in ct.Value)
                {
                    if (!t.Used && t.Task.Line == ct.Key.Line)
                    {
                        ct.Key.Consume(t.Task);
                        matchedTasks.Add(ct.Key, true);
                        t.Used = true;
                        break;
                    }
                    else
                    {
                        var hunk = repo.Blame(t.Task.File, new BlameOptions()
                        {
                            StoppingAt = ct.Key.Version
                        }).HunkForLine(t.Task.Line);

                        if (hunk.InitialStartLineNumber <= ct.Key.Line && ct.Key.Line <= hunk.LineCount + hunk.InitialStartLineNumber)
                        {
                            ct.Key.Consume(t.Task);
                            matchedTasks.Add(ct.Key, true);
                            t.Used = true;
                            break;
                        }
                    }
                }
            }

            foreach (var ct in consumableTasks)
            {
                if (matchedTasks.ContainsKey(ct.Key))
                {
                    continue;
                }

                consumableTasks[ct.Key] = (from c in ct.Value where !c.Used select c).ToList();

                if (consumableTasks.Count == 1)
                {
                    ct.Key.Consume(ct.Value[0].Task);
                    matchedTasks.Add(ct.Key, true);
                    ct.Value[0].Used = true;
                }
                else if (consumableTasks.Count > 1)
                {
                    ct.Key.Gone      = versionString;
                    ct.Key.TaskState = ETaskState.MaybeResolved;
                }
            }

            foreach (var task in usableTasks)
            {
                if (!task.Used)
                {
                    Tasks.Add(task.Task);
                }
            }

            SerializeProjects();
        }
コード例 #6
0
        /// <summary>
        /// This method also creates a user if none exists!
        /// </summary>
        /// <param name="user">the current user</param>
        /// <param name="isNewSSID">has a new ssid been created or are we reusing the old one</param>
        /// <param name="userInfo">the UserInfo of the retrieved User</param>
        /// <returns></returns>
        internal static string GetSSIDforUser(string user, out bool isNewSSID, out UserInfo userInfo)
        {
            string hash = UserInfosByName[user]?.ID;

            if (hash == null)
            {
                userInfo          = new UserInfo();
                userInfo.ID       = GenerateUnusedHash();
                hash              = userInfo.ID;
                userInfo.UserName = user;

                mutex.WaitOne();

                UserInfos.Add(userInfo.ID, userInfo);
                UserInfosByName.Add(userInfo.UserName, userInfo);

                UserCleanup();

                mutex.ReleaseMutex();

                isNewSSID = true;
            }
            else
            {
                isNewSSID = false;

                if (SessionIdRereferencingMode == ESessionIdRereferencingMode.AlwaysRenew)
                {
                    mutex.WaitOne();

                    userInfo = UserInfosByName[user];
                    UserInfos.Remove(userInfo.ID);
                    userInfo.ID            = GenerateUnusedHash();
                    UserInfos[userInfo.ID] = userInfo;

                    UserCleanup();

                    mutex.ReleaseMutex();

                    isNewSSID = true;
                    hash      = userInfo.ID;
                }
                else if (SessionIdRereferencingMode == ESessionIdRereferencingMode.Keep && UserInfosByName[user].ID == null)
                {
                    mutex.WaitOne();

                    userInfo = UserInfosByName[user];
                    UserInfos.Remove(userInfo.ID);
                    userInfo.ID            = GenerateUnusedHash();
                    UserInfos[userInfo.ID] = userInfo;

                    UserCleanup();

                    mutex.ReleaseMutex();

                    isNewSSID = true;
                    hash      = userInfo.ID;
                }
                else
                {
                    userInfo = UserInfosByName[user];

                    if (userInfo != null)
                    {
                        userInfo.lastPullUtcTime = DateTime.UtcNow;
                    }
                }
            }

            return(hash);
        }