aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kubatech/api/utils/ModUtils.java
blob: 60ce5cbb2e3b1e6146181de14cbdb6793fa5d3a1 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * KubaTech - Gregtech Addon
 * Copyright (C) 2022  kuba6000
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <https://www.gnu.org/licenses/>.
 *
 */

package kubatech.api.utils;

import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
import javax.xml.bind.DatatypeConverter;
import net.minecraft.launchwrapper.Launch;

public class ModUtils {
    public static final boolean isDeobfuscatedEnvironment =
            (boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment");
    public static boolean isClientSided = false;
    private static final HashMap<String, String> classNamesToModIDs = new HashMap<>();
    private static final Map.Entry<String, String> emptyEntry = new AbstractMap.SimpleEntry<>("", "");

    public static String getModNameFromClassName(String classname) {
        if (classNamesToModIDs.size() == 0) {
            classNamesToModIDs.put("net.minecraft", "Minecraft");
            Loader.instance().getActiveModList().forEach(m -> {
                Object Mod = m.getMod();
                if (Mod != null)
                    classNamesToModIDs.put(Mod.getClass().getPackage().getName(), m.getName());
            });
        }
        return classNamesToModIDs.entrySet().stream()
                .filter(e -> classname.startsWith(e.getKey()))
                .findAny()
                .orElse(emptyEntry)
                .getValue();
    }

    private static String modListVersion = null;

    public static String getModListVersion() {
        if (modListVersion != null) return modListVersion;
        @SuppressWarnings("unchecked")
        ArrayList<ModContainer> modlist = (ArrayList<ModContainer>)
                ((ArrayList<ModContainer>) Loader.instance().getActiveModList()).clone();
        String sortedList = modlist.stream()
                .filter(m -> m.getMod() != null)
                .sorted(Comparator.comparing(ModContainer::getModId))
                .collect(
                        StringBuilder::new,
                        (a, b) -> a.append(b.getModId()).append(b.getVersion()),
                        (a, b) -> a.append(", ").append(b))
                .toString();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            modListVersion = DatatypeConverter.printHexBinary(md.digest(sortedList.getBytes(StandardCharsets.UTF_8)))
                    .toUpperCase();
            return modListVersion;
        } catch (Exception e) {
            modListVersion = sortedList;
            return sortedList;
        }
    }

    private static String modListVersionIgnoringModVersions = null;

    public static String getModListVersionIgnoringModVersions() {
        if (modListVersionIgnoringModVersions != null) return modListVersionIgnoringModVersions;
        @SuppressWarnings("unchecked")
        ArrayList<ModContainer> modlist = (ArrayList<ModContainer>)
                ((ArrayList<ModContainer>) Loader.instance().getActiveModList()).clone();
        String sortedList = modlist.stream()
                .filter(m -> m.getMod() != null)
                .sorted(Comparator.comparing(ModContainer::getModId))
                .collect(StringBuilder::new, (a, b) -> a.append(b.getModId()), (a, b) -> a.append(", ")
                        .append(b))
                .toString();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            modListVersionIgnoringModVersions = DatatypeConverter.printHexBinary(
                            md.digest(sortedList.getBytes(StandardCharsets.UTF_8)))
                    .toUpperCase();
            return modListVersionIgnoringModVersions;
        } catch (Exception e) {
            modListVersionIgnoringModVersions = sortedList;
            return sortedList;
        }
    }
}