aboutsummaryrefslogtreecommitdiff
path: root/loader/src/main/java/kr/syeyoung/dungeonsguide/launcher/Main.java
blob: efba137de9cb11eb5e1cff152e4bad2af73d7783 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
 * Copyright (C) 2021  cyoung06
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package kr.syeyoung.dungeonsguide.launcher;

import com.mojang.authlib.exceptions.AuthenticationUnavailableException;
import com.mojang.authlib.exceptions.InvalidCredentialsException;
import kr.syeyoung.dungeonsguide.launcher.auth.AuthManager;
import kr.syeyoung.dungeonsguide.launcher.exceptions.AuthServerException;
import kr.syeyoung.dungeonsguide.launcher.exceptions.NoSuitableLoaderFoundException;
import kr.syeyoung.dungeonsguide.launcher.exceptions.PrivacyPolicyRequiredException;
import kr.syeyoung.dungeonsguide.launcher.exceptions.ReferenceLeakedException;
import kr.syeyoung.dungeonsguide.launcher.exceptions.TokenExpiredException;
import kr.syeyoung.dungeonsguide.launcher.gui.GuiLoadingError;
import kr.syeyoung.dungeonsguide.launcher.gui.GuiPrivacyPolicy;
import kr.syeyoung.dungeonsguide.launcher.loader.IDGLoader;
import kr.syeyoung.dungeonsguide.launcher.loader.JarLoader;
import kr.syeyoung.dungeonsguide.launcher.loader.LocalLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.ProgressManager;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mod(modid = Main.MOD_ID, version = Main.VERSION)
public class Main
{
    public static final String MOD_ID = "dungeons_guide_wrapper";
    public static final String VERSION = "1.0";
    public static final String DOMAIN = "http://testmachine:8080/api";

    private static Main main;

    private static File configDir;

    private DGInterface dgInterface;

    private final List<DungeonsGuideReloadListener> listeners = new ArrayList<>();

    public static File getConfigDir() {
        return configDir;
    }

    public void addDGReloadListener(DungeonsGuideReloadListener dungeonsGuideReloadListener) {
        listeners.add(Objects.requireNonNull(dungeonsGuideReloadListener));
    }
    public void removeDGReloadListener(DungeonsGuideReloadListener dungeonsGuideReloadListener) {
        listeners.remove(dungeonsGuideReloadListener);
    }

    private IDGLoader currentLoader;

    private Throwable lastError;
    private boolean isMcLoaded;




    @EventHandler
    public void initEvent(FMLInitializationEvent initializationEvent)
    {
        MinecraftForge.EVENT_BUS.register(this);
        if (dgInterface != null) {
            try {
                dgInterface.init(configDir);

                for (DungeonsGuideReloadListener listener : listeners) {
                    listener.onLoad(dgInterface);
                }
            } catch (Exception e) {
                e.printStackTrace();
                setLastFatalError(e);
            }
        }
    }

    public void unload() throws ReferenceLeakedException {
        if (currentLoader != null && !currentLoader.isUnloadable()) {
            throw new UnsupportedOperationException("Current version is not unloadable");
        }
        dgInterface = null;
        for (DungeonsGuideReloadListener listener : listeners) {
            listener.unloadReference();
        }
        if (currentLoader != null) {
            currentLoader.unloadDungeonsGuide();
        }
        currentLoader = null;
    }
    private void load(IDGLoader newLoader) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        partialLoad(newLoader);

        dgInterface.init(configDir);

        for (DungeonsGuideReloadListener listener : listeners) {
            listener.onLoad(dgInterface);
        }
    }
    private void partialLoad(IDGLoader newLoader) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        if (dgInterface != null) throw new IllegalStateException("DG is loaded");
        dgInterface = newLoader.loadDungeonsGuide();
        currentLoader = newLoader;
    }

    public void reload(IDGLoader newLoader) {
        try {
            unload();
            load(newLoader);
        } catch (Exception e) {
            dgInterface = null;
            currentLoader = null;

            e.printStackTrace();
            setLastFatalError(e);
        }
    }

    public void tryOpenError() {
        if (isMcLoaded) Minecraft.getMinecraft().displayGuiScreen(obtainErrorGUI());
    }

    public GuiScreen obtainErrorGUI() {
        if (lastError instanceof PrivacyPolicyRequiredException) {
            return new GuiPrivacyPolicy();
        } else if (lastError instanceof TokenExpiredException) {

        } else if (lastError instanceof NoSuitableLoaderFoundException) {

        } else if (lastError instanceof ReferenceLeakedException) {

        } else if (lastError instanceof AuthServerException) {

        } else if (lastError instanceof InvalidCredentialsException) {

        } else if (lastError instanceof AuthenticationUnavailableException) {

        } else if (lastError != null){
            return new GuiLoadingError(lastError, () -> {lastError = null;});
        }
        if (lastError != null)
            lastError.printStackTrace();
        // when gets called init and stuff remove thing
        return null;
    }

    @SubscribeEvent(priority = EventPriority.LOWEST)
    public void onGuiOpen(GuiOpenEvent guiOpenEvent) {
        if (guiOpenEvent.gui instanceof GuiMainMenu) {
            isMcLoaded = true;
        }
        if (lastError != null && guiOpenEvent.gui instanceof GuiMainMenu) {
            GuiScreen gui = obtainErrorGUI();
            if (gui != null)
                guiOpenEvent.gui = gui;
        }
    }

    public String getLoaderName(Configuration configuration) {
        String loader = System.getProperty("dg.loader");
        if (loader == null) {
            loader = configuration.get("loader", "modsource", "auto").getString();
        }
        if (loader == null) loader = "auto";
        return loader;
    }


    public IDGLoader obtainLoader(Configuration configuration) {
        String loader = getLoaderName(configuration);

        if ("local".equals(loader) ||
                (loader.equals("auto") && this.getClass().getResourceAsStream("/kr/syeyoung/dungeonsguide/DungeonsGuide.class") == null)) {
            return new LocalLoader();
        } else if ("jar".equals(loader) ||
                (loader.equals("auto") && this.getClass().getResourceAsStream("/mod.jar") == null)) {
            return new JarLoader();
        } else if (loader.equals("auto") ){
                // remote load
            throw new UnsupportedOperationException(""); // yet
        } else {
            throw new NoSuitableLoaderFoundException(System.getProperty("dg.loader"), configuration.get("loader", "modsource", "auto").getString());
        }
    }

    @EventHandler
    public void preInit(FMLPreInitializationEvent preInitializationEvent) {
        // setup static variables
        main = this;
        configDir = preInitializationEvent.getModConfigurationDirectory();

        // setup preinit progress bar for well, progress bar!
        ProgressManager.ProgressBar bar = ProgressManager.push("DungeonsGuide", 1);
        try {
            // Try authenticate
            bar.step("Authenticating...");
            AuthManager.getInstance().init();


            // If authentication succeeds, obtain loader and partially load dungeons guide

            File f = new File(preInitializationEvent.getModConfigurationDirectory(), "loader.cfg");
            Configuration configuration = new Configuration(f);
            // Save config because... well to generate it
            configuration.save();
        } catch (Throwable t) {
            dgInterface = null;
            currentLoader = null;

            t.printStackTrace();
            setLastFatalError(t);
        } finally {
            while(bar.getStep() < bar.getSteps()) bar.step("");
            ProgressManager.pop(bar);
        }

        ((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(a -> {
            if (dgInterface != null) dgInterface.onResourceReload(a);
        });
    }

    public void setLastFatalError(Throwable t) {
        lastError = t;
        tryOpenError();
    }

    public static Main getMain() {
        return main;
    }
}