GuiPanelLiteLoaderLog.java 12.3 KB
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/*
 * This file is part of LiteLoader.
 * Copyright (C) 2012-16 Adam Mummery-Smith
 * All Rights Reserved.
 */
package com.mumfrey.liteloader.client.gui;

import static com.mumfrey.liteloader.gl.GL.*;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.lwjgl.input.Keyboard;

import com.mumfrey.liteloader.core.LiteLoader;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
import com.mumfrey.liteloader.util.net.LiteLoaderLogUpload;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.Session;

/**
 *
 * @author Adam Mummery-Smith
 */
class GuiPanelLiteLoaderLog extends GuiPanel implements ScrollPanelContent
{
    private static boolean useNativeRes = true;

    /**
     * Scroll pane
     */
    private GuiScrollPanel scrollPane;

    private List<String> logEntries = new ArrayList<String>();

    private long logIndex = -1;

    private GuiCheckbox chkScale;

    private float guiScale;

    private GuiButton btnUpload;

    private LiteLoaderLogUpload logUpload;

    private String logURL;

    private int throb;

    private boolean closeDialog;

    private GuiLiteLoaderPanel parent;

    private int debugInfoTimer = 0;

    /**
     * @param minecraft
     * @param parent
     */
    GuiPanelLiteLoaderLog(Minecraft minecraft, GuiLiteLoaderPanel parent)
    {
        super(minecraft);
        this.parent = parent;
        this.scrollPane = new GuiScrollPanel(minecraft, this, MARGIN, TOP, this.width - (MARGIN * 2), this.height - TOP - BOTTOM);
    }

    private void updateLog()
    {
        this.logEntries = LiteLoaderLogger.getLogTail();
        this.logIndex = LiteLoaderLogger.getLogIndex();
        this.scrollPane.updateHeight();
        this.scrollPane.scrollToBottom();
    }

    @Override
    public int getScrollPanelContentHeight(GuiScrollPanel source)
    {
        return (int)(this.logEntries.size() * 10 / (this.chkScale.checked ? this.guiScale : 1.0F));
    }

    /**
     * Callback from parent screen when window is resized
     * 
     * @param width
     * @param height
     */
    @Override
    void setSize(int width, int height)
    {
        super.setSize(width, height);

        this.controls.add(new GuiButton(0, this.width - 59 - MARGIN, this.height - BOTTOM + 9, 60, 20,
                I18n.format("gui.done")));
        this.controls.add(this.btnUpload = new GuiButton(1, this.width - 145 - MARGIN, this.height - BOTTOM + 9, 80, 20,
                I18n.format("gui.log.postlog")));
        this.controls.add(this.chkScale = new GuiCheckbox(2, MARGIN, this.height - BOTTOM + 15,
                I18n.format("gui.log.scalecheckbox")));

        this.chkScale.checked = GuiPanelLiteLoaderLog.useNativeRes;

        ScaledResolution res = new ScaledResolution(this.mc);
        this.guiScale = res.getScaleFactor();

        this.scrollPane.setSizeAndPosition(MARGIN, TOP, this.width - (MARGIN * 2), this.height - TOP - BOTTOM);

        this.updateLog();
    }

    /**
     * Callback from parent screen when panel is displayed
     */
    @Override
    void onShown()
    {
    }

    /**
     * Callback from parent screen when panel is hidden
     */
    @Override
    void onHidden()
    {
    }

    /**
     * Callback from parent screen every tick
     */
    @Override
    void onTick()
    {
        this.throb++;

        if (LiteLoaderLogger.getLogIndex() > this.logIndex)
        {
            this.updateLog();
        }

        if (this.logUpload != null && this.logUpload.isCompleted())
        {
            this.logURL = this.logUpload.getLogUrl().trim();
            this.logUpload = null;

            int xMid = this.width / 2;
            if (this.logURL.startsWith("http:"))
            {
                LiteLoaderLogger.info("Log file upload succeeded, url is %s", this.logURL);
                int urlWidth = this.mc.fontRenderer.getStringWidth(this.logURL);
                this.controls.add(new GuiHoverLabel(3, xMid - (urlWidth / 2), this.height / 2, this.mc.fontRenderer, "\247n" + this.logURL,
                        this.parent.getBrandColour()));
            }
            else
            {
                LiteLoaderLogger.info("Log file upload failed, reason is %s", this.logURL);
            }

            this.controls.add(new GuiButton(4, xMid - 40, this.height - BOTTOM - MARGIN - 24, 80, 20, I18n.format("gui.log.closedialog")));
        }

        if (this.closeDialog)
        {
            this.closeDialog = false;
            this.logURL = null;
            this.setSize(this.width, this.height);
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_F3))
        {
            this.debugInfoTimer++;
            if (this.debugInfoTimer == 60)
            {
                LiteLoader.dumpDebugInfo();
            }
        }
        else
        {
            this.debugInfoTimer = 0;
        }
    }

    /**
     * Draw the panel and chrome
     * 
     * @param mouseX
     * @param mouseY
     * @param partialTicks
     */
    @Override
    void draw(int mouseX, int mouseY, float partialTicks)
    {
        // Draw panel title
        this.mc.fontRenderer.drawString(I18n.format("gui.log.title"), MARGIN, TOP - 14, 0xFFFFFFFF);

        // Draw top and bottom horizontal bars
        drawRect(MARGIN, TOP - 4, this.width - MARGIN, TOP - 3, 0xFF999999);
        drawRect(MARGIN, this.height - BOTTOM + 2, this.width - MARGIN, this.height - BOTTOM + 3, 0xFF999999);

        this.scrollPane.draw(mouseX, mouseY, partialTicks);

        int xMid = this.width / 2;
        int yMid = this.height / 2;

        if (this.logUpload != null || this.logURL != null)
        {
            drawRect(MARGIN + MARGIN, TOP + MARGIN, this.width - MARGIN - MARGIN, this.height - BOTTOM - MARGIN, 0xC0000000);

            if (this.logUpload != null)
            {
                this.drawCenteredString(this.mc.fontRenderer, I18n.format("gui.log.uploading"), xMid, yMid - 10, 0xFFFFFFFF);
                this.drawThrobber(xMid - 90, yMid - 14, this.throb);
            }
            else
            {
                if (this.logURL.startsWith("http:"))
                {
                    this.drawCenteredString(this.mc.fontRenderer, I18n.format("gui.log.uploadsuccess"), xMid, yMid - 14, 0xFF55FF55);
                }
                else
                {
                    this.drawCenteredString(this.mc.fontRenderer, I18n.format("gui.log.uploadfailed"), xMid, yMid - 10, 0xFFFF5555);
                }
            }
        }

        // Draw other buttons
        super.draw(mouseX, mouseY, partialTicks);
    }

    @Override
    public void drawScrollPanelContent(GuiScrollPanel source, int mouseX, int mouseY, float partialTicks, int scrollAmount, int visibleHeight)
    {
        int yPos = 0;
        int height = this.innerHeight;

        if (this.chkScale.checked)
        {
            float scale = 1.0F / this.guiScale;
            glScalef(scale, scale, scale);

            height = (int)(height * this.guiScale);
            scrollAmount = (int)(scrollAmount * this.guiScale);
        }

        for (String logLine : this.logEntries)
        {
            if (yPos > scrollAmount - 10 && yPos <= scrollAmount + height)
            {
                this.mc.fontRenderer.drawString(logLine, 0, yPos, this.getMessageColour(logLine.toLowerCase().substring(11)));
            }
            yPos += 10;
        }
    }

    @Override
    public void scrollPanelMousePressed(GuiScrollPanel source, int mouseX, int mouseY, int mouseButton)
    {
    }

    private int getMessageColour(String logLine)
    {
        if (logLine.startsWith("liteloader")) return 0xFFFFFF;
        if (logLine.startsWith("active pack:")) return 0xFFFF55;
        if (logLine.startsWith("success")) return 0x55FF55;
        if (logLine.startsWith("discovering")) return 0xFFFF55;
        if (logLine.startsWith("searching")) return 0x00AA00;
        if (logLine.startsWith("considering")) return 0xFFAA00;
        if (logLine.startsWith("not adding")) return 0xFF5555;
        if (logLine.startsWith("mod in")) return 0xAA0000;
        if (logLine.startsWith("error")) return 0xAA0000;
        if (logLine.startsWith("adding newest")) return 0x5555FF;
        if (logLine.startsWith("found")) return 0xFFFF55;
        if (logLine.startsWith("discovered")) return 0xFFFF55;
        if (logLine.startsWith("setting up")) return 0xAA00AA;
        if (logLine.startsWith("adding \"")) return 0xAA00AA;
        if (logLine.startsWith("injecting")) return 0xFF55FF;
        if (logLine.startsWith("loading")) return 0x5555FF;
        if (logLine.startsWith("initialising")) return 0x55FFFF;
        if (logLine.startsWith("calling late")) return 0x00AAAA;
        if (logLine.startsWith("dependency check")) return 0xFFAA00;
        if (logLine.startsWith("dependency")) return 0xFF5500;
        if (logLine.startsWith("mod name collision")) return 0xAA0000;
        if (logLine.startsWith("registering discovery module")) return 0x55FF55;
        if (logLine.startsWith("registering interface provider")) return 0xFFAA00;
        if (logLine.startsWith("mod file '")) return 0xFFAA00;
        if (logLine.startsWith("classtransformer '")) return 0x5555FF;
        if (logLine.startsWith("tweakClass '")) return 0x5555FF;
        if (logLine.startsWith("baking listener list")) return 0x00AAAA;
        if (logLine.startsWith("generating new event handler")) return 0xFFFF55;
        if (logLine.startsWith("resolving mods")) return 0xFFAA00;
        if (logLine.startsWith("resolved artefact")) return 0x00AA00;
        if (logLine.startsWith("evicting")) return 0xFFAA00;

        return 0xCCCCCC;
    }

    /**
     * @param control
     */
    @Override
    public void actionPerformed(GuiButton control)
    {
        if (control.id == 0) this.close();
        if (control.id == 1) this.postLog();

        if (control.id == 2 && this.chkScale != null)
        {
            this.chkScale.checked = !this.chkScale.checked;
            GuiPanelLiteLoaderLog.useNativeRes = this.chkScale.checked;
            this.updateLog();
        }

        if (control.id == 3 && this.logURL != null)
        {
            this.openURI(URI.create(this.logURL));
        }

        if (control.id == 4)
        {
            this.closeDialog = true;
        }
    }

    @Override
    public void scrollPanelActionPerformed(GuiScrollPanel source, GuiButton control)
    {
    }

    /**
     * @param mouseWheelDelta
     */
    @Override
    void mouseWheelScrolled(int mouseWheelDelta)
    {
        this.scrollPane.mouseWheelScrolled(mouseWheelDelta);
    }

    /**
     * @param mouseX
     * @param mouseY
     * @param mouseButton
     */
    @Override
    void mousePressed(int mouseX, int mouseY, int mouseButton)
    {
        this.scrollPane.mousePressed(mouseX, mouseY, mouseButton);

        super.mousePressed(mouseX, mouseY, mouseButton);
    }

    /**
     * @param mouseX
     * @param mouseY
     * @param mouseButton
     */
    @Override
    void mouseReleased(int mouseX, int mouseY, int mouseButton)
    {
        this.scrollPane.mouseReleased(mouseX, mouseY, mouseButton);
    }

    /**
     * @param mouseX
     * @param mouseY
     */
    @Override
    void mouseMoved(int mouseX, int mouseY)
    {
    }

    /**
     * @param keyChar
     * @param keyCode
     */
    @Override
    void keyPressed(char keyChar, int keyCode)
    {
        if (keyCode == Keyboard.KEY_ESCAPE) this.close();
        if (keyCode == Keyboard.KEY_SPACE) this.actionPerformed(this.chkScale);

        this.scrollPane.keyPressed(keyChar, keyCode);
    }

    private void postLog()
    {
        this.btnUpload.enabled = false;

        StringBuilder completeLog = new StringBuilder();

        for (String logLine : this.logEntries)
        {
            completeLog.append(logLine).append("\r\n");
        }

        LiteLoaderLogger.info("Uploading log file to liteloader...");
        Session session = this.mc.getSession();
        this.logUpload = new LiteLoaderLogUpload(session.getUsername(), session.getPlayerID(), completeLog.toString());
        this.logUpload.start();
    }

    private void openURI(URI uri)
    {
        try
        {
            Class<?> desktop = Class.forName("java.awt.Desktop");
            Object instance = desktop.getMethod("getDesktop").invoke(null);
            desktop.getMethod("browse", URI.class).invoke(instance, uri);
        }
        catch (Throwable th) {}
    }
}