summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverMetadata.kt
blob: 511a4171a1cf06cd190986d4f50c57865a56de08 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.utils

import java.io.IOException
import org.json.JSONException
import org.json.JSONObject
import java.io.File
import java.io.InputStream

class GpuDriverMetadata {
    /**
     * Tries to get driver metadata information from a meta.json [File]
     *
     * @param metadataFile meta.json file provided with a GPU driver
     */
    constructor(metadataFile: File) {
        if (metadataFile.length() > MAX_META_SIZE_BYTES) {
            return
        }

        try {
            val json = JSONObject(FileUtil.getStringFromFile(metadataFile))
            name = json.getString("name")
            description = json.getString("description")
            author = json.getString("author")
            vendor = json.getString("vendor")
            version = json.getString("driverVersion")
            minApi = json.getInt("minApi")
            libraryName = json.getString("libraryName")
        } catch (e: JSONException) {
            // JSON is malformed, ignore and treat as unsupported metadata.
        } catch (e: IOException) {
            // File is inaccessible, ignore and treat as unsupported metadata.
        }
    }

    /**
     * Tries to get driver metadata information from an input stream that's intended to be
     * from a zip file
     *
     * @param metadataStream ZipEntry input stream
     * @param size Size of the file in bytes
     */
    constructor(metadataStream: InputStream, size: Long) {
        if (size > MAX_META_SIZE_BYTES) {
            return
        }

        try {
            val json = JSONObject(FileUtil.getStringFromInputStream(metadataStream))
            name = json.getString("name")
            description = json.getString("description")
            author = json.getString("author")
            vendor = json.getString("vendor")
            version = json.getString("driverVersion")
            minApi = json.getInt("minApi")
            libraryName = json.getString("libraryName")
        } catch (e: JSONException) {
            // JSON is malformed, ignore and treat as unsupported metadata.
        } catch (e: IOException) {
            // File is inaccessible, ignore and treat as unsupported metadata.
        }
    }

    /**
     * Creates an empty metadata instance
     */
    constructor()

    override fun equals(other: Any?): Boolean {
        if (other !is GpuDriverMetadata) {
            return false
        }

        return other.name == name &&
            other.description == description &&
            other.author == author &&
            other.vendor == vendor &&
            other.version == version &&
            other.minApi == minApi &&
            other.libraryName == libraryName
    }

    override fun hashCode(): Int {
        var result = name?.hashCode() ?: 0
        result = 31 * result + (description?.hashCode() ?: 0)
        result = 31 * result + (author?.hashCode() ?: 0)
        result = 31 * result + (vendor?.hashCode() ?: 0)
        result = 31 * result + (version?.hashCode() ?: 0)
        result = 31 * result + minApi
        result = 31 * result + (libraryName?.hashCode() ?: 0)
        return result
    }

    override fun toString(): String =
        """
            Name - $name
            Description - $description
            Author - $author
            Vendor - $vendor
            Version - $version
            Min API - $minApi
            Library Name - $libraryName
        """.trimMargin().trimIndent()

    var name: String? = null
    var description: String? = null
    var author: String? = null
    var vendor: String? = null
    var version: String? = null
    var minApi = 0
    var libraryName: String? = null

    companion object {
        private const val MAX_META_SIZE_BYTES = 500000
    }
}