Exemplo n.º 1
0
        // Get total times in milliseconds that process has been running
        public static long GetProcessTotalUptime(string name)
        {
            long uptime = 0;

            JSONElement root = JSON.Parse(File.ReadAllText(ProgramData.Directory + "process-tracker\\total.json"));

            if (root.c.ContainsKey(name))
            {
                uptime = root.c[name].ToLong();
            }

            return(uptime);
        }
Exemplo n.º 2
0
        public static JSONElement Parse(string json)
        {
            json = "\"root\":" + json;

            bool Quoted         = false;
            bool SquareBrackets = false;

            Dictionary <int, JSONElement> Open = new Dictionary <int, JSONElement>();

            try {
                a : for (int i = 0; i < json.Length; i++)
                {
                    if ((json[i] == '"' && i - 1 < 0) || (json[i] == '"' && i - 1 >= 0 && json[i - 1] != '\\'))
                    {
                        Quoted = !Quoted;
                        continue;
                    }

                    if (json[i] == '[' && !Quoted)
                    {
                        SquareBrackets = true;
                        continue;
                    }
                    if (json[i] == ']' && !Quoted)
                    {
                        SquareBrackets = false;
                        continue;
                    }

                    if (json[i] == '{' && !Quoted)
                    {
                        int OpenCount  = 0;
                        int FoundCount = 0;

                        bool Quoted2 = false;

                        for (int j = i; j < json.Length; j++)
                        {
                            if (json[j] == '"' && json[j - 1] != '\\')
                            {
                                Quoted2 = !Quoted2;
                                continue;
                            }

                            if (json[j] == '{' && !Quoted2)
                            {
                                OpenCount++;
                                continue;
                            }
                            if (json[j] == '}' && !Quoted2)
                            {
                                FoundCount++;
                                if (OpenCount == FoundCount)
                                {
                                    bool Quoted3  = false;
                                    int  Brackets = 0;
                                    for (int k = j; k >= 0; k--)
                                    {
                                        if (json[k] == '"' && (k == 0 || (k - 1 >= 0 && json[k - 1] != '\\')))
                                        {
                                            Quoted3 = !Quoted3;
                                            continue;
                                        }
                                        if (json[k] == '{' && !Quoted3)
                                        {
                                            Brackets++;
                                            continue;
                                        }
                                        if (json[k] == '}' && !Quoted3)
                                        {
                                            Brackets--;
                                            continue;
                                        }
                                    }

                                    string Name = "";
                                    if (json[i - 1] == ':')
                                    {
                                        bool IsOpen = false;
                                        for (int k = i - 2; k >= 0; k--)
                                        {
                                            if (json[k] == '"' && (k == 0 || (k - 1 >= 0 && json[k - 1] != '\\')))
                                            {
                                                IsOpen = !IsOpen;
                                                if (IsOpen == false)
                                                {
                                                    Name = json.Substring(k + 1, i - 3 - k);
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    string      JsonObject  = json.Substring(i, j - i + 1);
                                    JSONElement JsonElement = new JSONElement();
                                    JsonElement.v = JsonObject;

                                    int  BracketsCount = 0;
                                    bool Quoted4       = false;
                                    for (int k = 0; k < JsonObject.Length; k++)
                                    {
                                        if (json[k] == '"' && (k == 0 || (k - 1 >= 0 && json[k - 1] != '\\')))
                                        {
                                            Quoted4 = !Quoted4;
                                            continue;
                                        }

                                        if ((JsonObject[k] == '{' || JsonObject[k] == '}') && !Quoted4)
                                        {
                                            BracketsCount++;
                                        }
                                    }

                                    if (BracketsCount <= 2)
                                    {
                                        JsonElement.CreateValues(JsonObject);
                                    }

                                    Open[Brackets] = JsonElement;
                                    if (Brackets > 0)
                                    {
                                        Open[Brackets - 1].c.Add(Name, JsonElement);
                                    }
                                    break;
                                }

                                continue;
                            }
                        }
                        continue;
                    }
                }
            } catch { }

            return(Open.Count > 0 ? Open[0] : new JSONElement());
        }
Exemplo n.º 3
0
        public void CreateValues(string s)
        {
            //Console.WriteLine("Creating values: " + s);

            string key = "", value = "";
            bool   Quoted     = false;
            int    QuoteStart = 0;

            for (int i = 1; i < s.Length - 1; i++)
            {
                if (s[i] == '"' && (i == 0 || (i - 1 >= 0 && s[i - 1] != '\\')))
                {
                    Quoted = !Quoted;
                    if (Quoted)
                    {
                        QuoteStart = i;
                    }
                    else
                    {
                        key = s.Substring(QuoteStart + 1, i - QuoteStart - 1);
                        int QuoteStart2 = 0;

                        value = "";

                        if (s[i + 1] == ':')
                        {
                            bool Quoted2 = false;
                            if (s[i + 2] == '"')
                            {
                                for (int j = i + 2; j < s.Length - 1; j++)
                                {
                                    if (s[j] == '"' && s[j - 1] != '\\')
                                    {
                                        Quoted2 = !Quoted2;
                                        if (!Quoted2)
                                        {
                                            value = s.Substring(QuoteStart2 + 1, j - QuoteStart2 - 1);
                                            i     = j;
                                            break;
                                        }
                                        else
                                        {
                                            QuoteStart2 = j;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int j = i + 2; j < s.Length - 1; j++)
                                {
                                    if (s[j] == ',' || s[j] == '{' || s[j] == '{' || s[j] == ':')
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        value += s[j];
                                    }
                                }
                            }
                        }

                        JSONElement e = new JSONElement();
                        e.v = value.Replace("\\\"", "\"");
                        try {
                            c.Add(key, e);
                        }
                        catch { }
                    }
                    continue;
                }
            }
        }
Exemplo n.º 4
0
        private static void UpdateTotalTimes()
        {
            JSONElement root = JSON.Parse(File.ReadAllText(ProgramData.Directory + "process-tracker\\total.json"));

            string[] ss = Directory.GetDirectories(ProgramData.Directory + "process-tracker");
            foreach (string s in ss)
            {
                // Get session id from directory path
                string session = s.Substring(s.LastIndexOf('\\') + 1, s.Length - s.LastIndexOf('\\') - 1);

                long shutdown       = DateTimeOffset.Now.ToUnixTimeMilliseconds(); // stores time when computer shut down, if it's still on, it will be current time
                bool CurrentSession = session.Equals(ProgramData.SystemBoot + ""); // checks if checked session is equal to current one

                if (!CurrentSession)
                {
                    shutdown = (long)(new FileInfo(s + "\\last-update.txt").LastWriteTime - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
                }

                // Read all data
                string[] fs = Directory.GetFiles(s);
                foreach (string f in fs)
                {
                    // Get process name from file path
                    string process = f.Substring(f.LastIndexOf('\\') + 1, f.Length - f.LastIndexOf('\\') - 5);

                    // Read process start and exit times, and calculate elapsed times
                    string[] Lines = File.ReadAllLines(f);

                    long startAt = 0; // stores last known start time for the process
                    long total   = 0; // stores total time that process has been up for that session

                    // If 'total.json' file already contains time for this process, then it will be used as start value
                    if (root.c.ContainsKey(process))
                    {
                        total = root.c[process].ToLong();
                    }

                    // This will be true if last line of the file starts with 'e'
                    bool Ended = false;

                    foreach (string l in Lines)
                    {
                        if (l.Length < 10)
                        {
                            continue;
                        }
                        try {
                            long time = Int64.Parse(l.Substring(1));
                            if (l.StartsWith("s"))
                            {
                                if (startAt != 0)
                                {
                                    total += time - startAt;
                                }
                                startAt = time;
                                Ended   = false;
                            }
                            else
                            {
                                total += time - startAt;
                                Ended  = true;
                            }
                        }
                        catch { }
                    }

                    // This will be 'false' if computer shut down or 'Alan' process was closed
                    if (!Ended)
                    {
                        // If 'false' then app is closed because system turned off
                        if (!CurrentSession)
                        {
                            total += shutdown - startAt;
                        }
                        else
                        {
                            if (Process.GetProcessesByName(process).Length > 0)
                            {
                                total += DateTimeOffset.Now.ToUnixTimeMilliseconds() - startAt;
                            }
                        }
                    }

                    root.c[process] = new JSONElement(total);
                    Console.WriteLine($"New total for {process} : {total} for session {session}");
                }

                // Delete session directory since all values are already saved
                foreach (string f in fs)
                {
                    File.Delete(f);
                    ;
                }
                Directory.Delete(s);
                Console.WriteLine($"Files and directory deleted @ {s}");
            }

            File.WriteAllText(ProgramData.Directory + "process-tracker\\total.json", JSON.Stringify(root));
        }