aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/CMake/CMakeProject.Records.cs
blob: eebfcb94c71d7888eae397811d1696eb45601e05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace QtVsTools.Core.CMake
{
    public partial class CMakeProject : Concurrent<CMakeProject>
    {
        private static JProperty RecordInfo(JObject record)
        {
            return record?["vendor"]?
                .Children<JProperty>()
                .FirstOrDefault(x => x.Name.StartsWith("qt-project.org"));
        }

        private IEnumerable<JObject> GetRecords(JObject root, string recordType = null)
        {
            return root
                .Descendants()
                .Select(x => new
                {
                    Record = x as JObject,
                    Info = RecordInfo(x as JObject)
                })
                .Where(x => x.Record != null && x.Info != null
                    && (recordType == null || x.Info.Name == recordType))
                .Select(x => x.Record);
        }

        private string EvalChecksum(JObject record)
        {
            record = record?.DeepClone() as JObject;
            if (RecordInfo(record)?.Value is not JObject info)
                return string.Empty;
            info.Remove("checksum");
            var json = record.ToString(Formatting.Indented);
            var jsonUtf8 = Encoding.UTF8.GetBytes(json);
            using var sha1 = SHA1.Create();
            var sha1Data = sha1.ComputeHash(jsonUtf8);
            return System.Convert.ToBase64String(sha1Data);
        }

        private void VerifyChecksums()
        {
            Presets ??= new JObject();
            UserPresets ??= new JObject();

            var records = Presets.Descendants()
                .Union(UserPresets.Descendants())
                .Append(Presets)
                .Append(UserPresets)
                .Select(x => new
                {
                    Self = x as JObject,
                    Info = RecordInfo(x as JObject)
                })
                .Select(x => new
                {
                    x.Self,
                    x.Info,
                    Checksum = x.Info?.Value["checksum"]
                })
                .Where(x => x.Info != null)
                .ToList();
            foreach (var record in records) {
                if (record.Checksum?.Value<string>() == EvalChecksum(record.Self))
                    continue;
                if (record.Self != Presets && record.Self != UserPresets)
                    record.Self.Remove();
            }
        }
    }
}