Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  Minecraft Wiiu Mod Injector Launcher
Language: C/C++
Code:
import System;
import System.Collections.Generic;
import System.ComponentModel;
import System.Windows.Forms;
import System.Net;
import System.Diagnostics;
import System.IO;
import MetroFramework.Forms;
import Minecraft_Wii_U_Mod_Injector_Launcher.Utilities;

namespace Minecraft_Wii_U_Mod_Injector_Launcher {
    
    public class Launcher extends MetroForm {
        
        string injectorRoot = (Environment.GetEnvironmentVariable("localappdata") + "\\Minecraft Wii U Mod Injector\\");
        
        string LauncherRoot = (Application.StartupPath + "\\");
        
        public string WebsiteRoot = "https://minecraftwiiumodinjector.com/injector/Executables/";
        
        public string[] Executables;
        
        private boolean _completed;
        
        public final boolean DownloadCompleted {
            get {
                return this._completed;
            }
        }
        
        public Launcher() {
            Logger.Log("# launcher log start #");
            InitializeComponent();
            Logger.Log("Launcher UI has been Initialized");
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            Logger.Log("Security Protocol has been set to Tls12 to allow Windows 7 users to download files");
        }
        
        private final void Init(object sender, EventArgs e) {
            try {
                this.ReadArgs(false);
                if (!Directory.Exists(this.injectorRoot)) {
                    Directory.CreateDirectory(this.injectorRoot);
                    Logger.Log("Injector Directory did not exist, it has been created now.");
                }
                
                this.DownloadFile((this.WebsiteRoot + this.Executables[0]), (this.injectorRoot + this.Executables[0]));
            }
            catch (UnauthorizedAccessException ) {
                MessageBox.Show("Please close the injector before starting another instance.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
            catch (Exception Ex) {
                MessageBox.Show(("Exception occured in Init(object, EventArgs), report this in our discord server!\n" + Ex.ToString()), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
            
        }
        
        private final void ReadArgs(boolean clear) {
            try {
                if ((this.GetArgs().Contains("-clear") || clear)) {
                    Logger.Log("# -clear command start #");
                    if (this.GetArgs().Contains("-backup")) {
                        if (!Directory.Exists((this.LauncherRoot + "backup"))) {
                            Directory.CreateDirectory((this.LauncherRoot + "backup"));
                            Logger.Log("Injector Backup Directory did not exist, it has been created now.");
                        }
                        
                        for (string file : Directory.GetFiles(this.injectorRoot)) {
                            FileInfo FI = new FileInfo(file);
                            if ((FI.Name != this.Executables[0])) {
                                File.Copy(file, (this.LauncherRoot + ("backup\\" + FI.Name)), true);
                                Logger.Log(("Moving " 
                                                + (FI.Name + (" to " 
                                                + (this.LauncherRoot + ("backup\\" + FI.Name))))));
                            }
                            
                        }
                        
                        for (string directory : Directory.GetDirectories(this.injectorRoot)) {
                            if (!directory.Contains("cache")) {
                                DirectoryInfo DI1 = new DirectoryInfo(directory);
                                Directory.CreateDirectory((this.LauncherRoot + ("backup\\" + DI1.Name)));
                                for (string file : Directory.GetFiles(directory)) {
                                    FileInfo FI = new FileInfo(file);
                                    DirectoryInfo DI = new DirectoryInfo(directory);
                                    File.Copy(file, (this.LauncherRoot + ("backup\\" 
                                                    + (DI.Name + ("\\" + FI.Name)))), true);
                                    Logger.Log(("Moving " 
                                                    + (FI.Name + (" to " 
                                                    + (this.LauncherRoot + ("backup\\" + FI.Name))))));
                                }
                                
                            }
                            
                        }
                        
                    }
                    
                    Directory.Delete(this.injectorRoot, true);
                    Logger.Log("All Files cleared, exiting function");
                    Logger.Log("# -clear command end #");
                }
                
            }
            catch (Exception e) {
                MessageBox.Show(("An error occured while gatering args\n" + e.ToString()), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }
        
        final void DownloadFile(string address, string location) {
            try {
                WebClient client = new WebClient();
                Uri Uri = new Uri(address);
                this._completed = false;
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(this.DownloadCompletedEvent);
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.DownloadProgress);
                Logger.Log("Download for Injector has been started.");
                client.DownloadFileTaskAsync(Uri, location);
            }
            catch (Exception error) {
                MessageBox.Show(error.ToString());
            }
            
        }
        
        private final void DownloadProgress(object sender, DownloadProgressChangedEventArgs e) {
            LauncherProgress.Value = e.ProgressPercentage;
            ProgressLabel.Text = (LauncherProgress.Value + "%");
        }
        
        private final void launcherProgressClicked(object sender, EventArgs e) {
            Logger.Log("LauncherProgresed clicked, entering clear mode");
            Process.Start(Application.ExecutablePath, "-clear");
            Environment.Exit(0);
        }
        
        private final void DownloadCompletedEvent(object sender, AsyncCompletedEventArgs e) {
            if (e.Cancelled) {
                Logger.Log("Download for Injector has failed, exiting.");
                Environment.Exit(0);
            }
            else {
                Logger.Log("Download for Injector has been completed.");
                LauncherProgress.Value = 100;
            }
            
            this._completed = true;
            if (File.Exists((this.injectorRoot + this.Executables[0]))) {
                Logger.Log("Starting Injector.");
                (new Process() + Start());
            }
            else {
                Logger.Log("Injector file was not found, checking if directory exists.");
                if (Directory.Exists(this.injectorRoot)) {
                    Logger.Log("Directory exists, exiting.");
                }
                else {
                    Logger.Log("Directory does not exist, exiting.");
                }
                
            }
            
            Logger.Log("# launcher log end #");
            Environment.Exit(0);
        }
        
        public final List<string> GetArgs() {
            Logger.Log("Command line parameters have been gathered.");
            return Program.argsList;
        }
    }
}
Comments: