Ejemplo n.º 1
0
 private async Task <bool> assertFile(FileInfo fi, string dir, string targetPath)
 {
     if (fi != null && YU.stringHasText(fi.Path) && YU.stringHasText(fi.Url))
     {
         if (!FileChecker.CheckFileMD5(dir, fi))
         {
             Directory.CreateDirectory(dir);
             await loadFile(fi.Url, targetPath);
         }
         return(true);
     }
     return(false);
 }
Ejemplo n.º 2
0
        private async void DownloadFile(FileInfo fileInfo)
        {
            if (!YU.stringHasText(fileInfo.UploadAlias))
            {
                fileInfo.UploadAlias = fileInfo.Hashes.Count > 0 ? fileInfo.Hashes[0] : null;
                if (!YU.stringHasText(fileInfo.UploadAlias))
                {
                    int lios = Math.Max(fileInfo.Path.LastIndexOf('\\'), fileInfo.Path.LastIndexOf('/'));
                    fileInfo.UploadAlias = lios > -1 ? fileInfo.Path.Substring(lios + 1, fileInfo.Path.Length) : fileInfo.Path;
                }
            }
            string uploadFilename = PreloaderForm.UPDPATH + fileInfo.UploadAlias;

            if (File.Exists(uploadFilename))
            {
                if (FileChecker.CheckFileMD5(PreloaderForm.UPDPATH, fileInfo))
                {
                    if (UpdateInProgress_)
                    {
                        DownloadNext();
                    }
                    else
                    {
                        DownloadNextMod();
                    }
                    return;
                }
                else
                {
                    File.Delete(uploadFilename);
                }
            }
            try {
                updateProgressBar.Value = 0;
                updateLabelText.Text    = string.Format(
                    Locale.Get("DLRate")
                    , FormatBytes(0)
                    , FormatBytes(fileInfo.Size)
                    , ""
                    , currentFile_.Value.Description
                    );
                await wc_.DownloadFileTaskAsync(new Uri(fileInfo.Url), uploadFilename);
            }
            catch (Exception ex) {
                ShowDownloadError(string.Format(Locale.Get("CannotDownloadFile"), fileInfo.Path) + "\r\n" + ex.Message);
            }
        }
Ejemplo n.º 3
0
        private async void Initialize()
        {
            _progressBar1.Value = 0;
            Program.OfflineMode = false;
            long startingTicks = DateTime.Now.Ticks;
            long lastTicks     = startingTicks;

            void logDeltaTicks(string point)
            {
                long current = DateTime.Now.Ticks;

                YU.Log(point + ": " + (current - lastTicks) + " (" + (current - startingTicks) + ')');
                lastTicks = current;
            }

            try {
                if (!Directory.Exists(IMGPATH))
                {
                    Directory.CreateDirectory(IMGPATH);
                }
                if (!Directory.Exists(UPDPATH))
                {
                    Directory.CreateDirectory(UPDPATH);
                }
                //WebBrowserHelper.FixBrowserVersion();
                //ErrorAndKill("Cannot get Images:\r\n");
                string settingsJson = (await wc_.DownloadStringTaskAsync(Program.SETTINGS_URL));
                logDeltaTicks("settings");
                incProgress(5);
                try {
                    Program.LoncherSettings = new LauncherData(settingsJson);
                    try {
                        File.WriteAllText(SETTINGSPATH, settingsJson, Encoding.UTF8);
                    }
                    catch { }
                    incProgress(5);
                    try {
                        if (Program.LoncherSettings.RAW.Localization != null)
                        {
                            FileInfo locInfo = Program.LoncherSettings.RAW.Localization;
                            if (YU.stringHasText(locInfo.Url))
                            {
                                locInfo.Path = LOCPATH;
                                if (!FileChecker.CheckFileMD5("", locInfo))
                                {
                                    string loc = await wc_.DownloadStringTaskAsync(locInfo.Url);

                                    File.WriteAllText(LOCPATH, loc, Encoding.UTF8);
                                    Locale.LoadCustomLoc(loc.Replace("\r\n", "\n").Split('\n'));
                                }
                                Locale.LoadCustomLoc(File.ReadAllLines(LOCPATH, Encoding.UTF8));
                            }
                            else if (File.Exists(LOCPATH))
                            {
                                Locale.LoadCustomLoc(File.ReadAllLines(LOCPATH, Encoding.UTF8));
                            }
                        }
                        else if (File.Exists(LOCPATH))
                        {
                            Locale.LoadCustomLoc(File.ReadAllLines(LOCPATH, Encoding.UTF8));
                        }
                        incProgress(5);
                        logDeltaTicks("locales");
                    }
                    catch (Exception ex) {
                        YobaDialog.ShowDialog(Locale.Get("CannotGetLocaleFile") + ":\r\n" + ex.Message);
                    }
                    try {
                        downloadProgressTracker_     = new DownloadProgressTracker(50, TimeSpan.FromMilliseconds(500));
                        wc_.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
#if DEBUG
#else
                        if (YU.stringHasText(Program.LoncherSettings.LoncherHash))
                        {
                            string selfHash = FileChecker.GetFileMD5(Application.ExecutablePath);

                            if (!Program.LoncherSettings.LoncherHash.ToUpper().Equals(selfHash))
                            {
                                if (YU.stringHasText(Program.LoncherSettings.LoncherExe))
                                {
                                    string newLoncherPath = Application.ExecutablePath + ".new";
                                    string appname        = Application.ExecutablePath;
                                    appname = appname.Substring(appname.LastIndexOf('\\') + 1);
                                    await loadFile(Program.LoncherSettings.LoncherExe, newLoncherPath, Locale.Get("UpdatingLoncher"));

                                    string newHash = FileChecker.GetFileMD5(newLoncherPath);

                                    if (selfHash.Equals(Program.PreviousVersionHash))
                                    {
                                        YU.ErrorAndKill(Locale.Get("LoncherOutOfDate2"));
                                    }
                                    else if (newHash.Equals(Program.PreviousVersionHash))
                                    {
                                        YU.ErrorAndKill(Locale.Get("LoncherOutOfDate3"));
                                    }
                                    else
                                    {
                                        Process.Start(new ProcessStartInfo {
                                            Arguments = String.Format("/C choice /C Y /N /D Y /T 1 & Del \"{0}\" & Rename \"{1}\" \"{2}\" & \"{0}\" -oldhash {3}"
                                                                      , Application.ExecutablePath, newLoncherPath, appname, selfHash)
                                            ,
                                            FileName = "cmd"
                                            ,
                                            WindowStyle = ProcessWindowStyle.Hidden
                                        });
                                        Application.Exit();
                                    }
                                    return;
                                }
                                else
                                {
                                    YU.ErrorAndKill(Locale.Get("LoncherOutOfDate1"));
                                    return;
                                }
                            }
                        }
#endif
                    }
                    catch (Exception ex) {
                        YU.ErrorAndKill(Locale.Get("CannotUpdateLoncher") + ":\r\n" + ex.Message + "\r\n" + ex.StackTrace);
                        return;
                    }
                    LauncherData.LauncherDataRaw launcherDataRaw = Program.LoncherSettings.RAW;
                    try {
                        if (await assertFile(launcherDataRaw.Icon, IMGPATH, ICON_FILE))
                        {
                            Bitmap bm = YU.readBitmap(ICON_FILE);
                            if (bm != null)
                            {
                                Program.LoncherSettings.Icon = Icon.FromHandle(bm.GetHicon());
                                this.Icon = Program.LoncherSettings.Icon;
                            }
                        }
                        if (Program.LoncherSettings.Icon == null)
                        {
                            Program.LoncherSettings.Icon = this.Icon;
                        }
                        if (await assertFile(launcherDataRaw.PreloaderBackground, IMGPATH, BG_FILE))
                        {
                            this.BackgroundImage = YU.readBitmap(BG_FILE);
                        }
                        bool gotRandomBG = false;
                        if (launcherDataRaw.RandomBackgrounds != null && launcherDataRaw.RandomBackgrounds.Count > 0)
                        {
                            int randomBGRoll = new Random().Next(0, 1000);
                            int totalRoll    = 0;
                            foreach (RandomBgImageInfo rbgi in launcherDataRaw.RandomBackgrounds)
                            {
                                if (await assertFile(rbgi.Background, IMGPATH))
                                {
                                    totalRoll += rbgi.Chance;
                                    if (totalRoll > randomBGRoll)
                                    {
                                        Program.LoncherSettings.Background = YU.readBitmap(IMGPATH + rbgi.Background.Path);
                                        gotRandomBG = true;
                                        break;
                                    }
                                }
                            }
                        }
                        if (!gotRandomBG && await assertFile(launcherDataRaw.Background, IMGPATH))
                        {
                            Program.LoncherSettings.Background = YU.readBitmap(IMGPATH + launcherDataRaw.Background.Path);
                        }

                        if (Program.LoncherSettings.UI.Count > 0)
                        {
                            string[] keys = Program.LoncherSettings.UI.Keys.ToArray();
                            foreach (string key in keys)
                            {
                                if (!(await assertFile(Program.LoncherSettings.UI[key].BgImage, IMGPATH)))
                                {
                                    Program.LoncherSettings.UI[key].BgImage = null;
                                }
                                if (!(await assertFile(Program.LoncherSettings.UI[key].BgImageClick, IMGPATH)))
                                {
                                    Program.LoncherSettings.UI[key].BgImageClick = null;
                                }
                                if (!(await assertFile(Program.LoncherSettings.UI[key].BgImageHover, IMGPATH)))
                                {
                                    Program.LoncherSettings.UI[key].BgImageHover = null;
                                }
                            }
                        }
                        if (Program.LoncherSettings.Buttons.Count > 0)
                        {
                            foreach (LinkButton lbtn in Program.LoncherSettings.Buttons)
                            {
                                if (!(await assertFile(lbtn.BgImage, IMGPATH)))
                                {
                                    lbtn.BgImage = null;
                                }
                                if (!(await assertFile(lbtn.BgImageClick, IMGPATH)))
                                {
                                    lbtn.BgImageClick = null;
                                }
                                if (!(await assertFile(lbtn.BgImageHover, IMGPATH)))
                                {
                                    lbtn.BgImageHover = null;
                                }
                            }
                        }

                        /*await assertFile(new FileInfo() {
                         *      Url = "https://drive.google.com/uc?export=download&confirm=-MpP&id=1fUW0NfP2EYUG6K2hOg6hgajRi59pCBBy"
                         *      , Path = "legends"
                         * }, IMGPATH);*/

                        logDeltaTicks("images");
                    }
                    catch (Exception ex) {
                        YU.ErrorAndKill(Locale.Get("CannotGetImages") + ":\r\n" + ex.Message);
                        return;
                    }
                    if (Program.LoncherSettings.Fonts != null)
                    {
                        List <string> keys = Program.LoncherSettings.Fonts.Keys.ToList();
                        if (keys.Count > 0)
                        {
                            try {
                                if (!Directory.Exists(FNTPATH))
                                {
                                    Directory.CreateDirectory(FNTPATH);
                                }
                                foreach (string key in keys)
                                {
                                    using (Font fontTester = new Font(key, 12, FontStyle.Regular, GraphicsUnit.Pixel)) {
                                        if (fontTester.Name == key)
                                        {
                                            Program.LoncherSettings.Fonts[key] = "win";
                                        }
                                        else if (File.Exists(FNTPATH + key))
                                        {
                                            Program.LoncherSettings.Fonts[key] = "local";
                                        }
                                        else
                                        {
                                            string status   = "none";
                                            string src      = Program.LoncherSettings.Fonts[key];
                                            string filename = FNTPATH + key;
                                            if (YU.stringHasText(src))
                                            {
                                                await loadFile(src, filename);

                                                if (File.Exists(filename))
                                                {
                                                    status = "local";
                                                }
                                            }
                                            Program.LoncherSettings.Fonts[key] = status;
                                        }
                                    }
                                }
                                logDeltaTicks("fonts");
                            }
                            catch (Exception ex) {
                                YU.ErrorAndKill(Locale.Get("CannotGetFonts") + ":\r\n" + ex.Message);
                                return;
                            }
                        }
                    }
                    try {
                        loadingLabel.Text = Locale.Get("PreparingToLaunch");
                        //await Program.LoncherSettings.InitChangelogOnline();
                        Program.LoncherSettings.Changelog = await getStaticTabData("Changelog", launcherDataRaw.Changelog, launcherDataRaw.QuoteToEscape, "[[[CHANGELOG]]]");

                        Program.LoncherSettings.FAQ = await getStaticTabData("FAQ", launcherDataRaw.FAQFile, launcherDataRaw.QuoteToEscape, "[[[FAQTEXT]]]");

                        incProgress(5);
                        logDeltaTicks("changelog and etc");
                        //loadingLabel.Text = Locale.Get("PreparingToLaunch");
                        try {
                            if (findGamePath())
                            {
                                try {
                                    updateGameVersion();
                                    if (oldMainForm_ != null)
                                    {
                                        oldMainForm_.Dispose();
                                    }
                                    int progressBarPerFile = 100 - _progressBar1.Value;
                                    if (progressBarPerFile < Program.LoncherSettings.Files.Count)
                                    {
                                        progressBarPerFile  = 88;
                                        _progressBar1.Value = 6;
                                    }
                                    progressBarPerFile = progressBarPerFile / Program.LoncherSettings.Files.Count;
                                    if (progressBarPerFile < 1)
                                    {
                                        progressBarPerFile = 1;
                                    }

                                    Program.GameFileCheckResult = await FileChecker.CheckFiles(
                                        Program.LoncherSettings.Files
                                        , new EventHandler <FileCheckedEventArgs>((object o, FileCheckedEventArgs a) => {
                                        _progressBar1.Value += progressBarPerFile;
                                        if (_progressBar1.Value > 100)
                                        {
                                            _progressBar1.Value = 40;
                                        }
                                    })
                                        );

                                    foreach (ModInfo mi in Program.LoncherSettings.Mods)
                                    {
                                        if (mi.ModConfigurationInfo != null)
                                        {
                                            await FileChecker.CheckFiles(
                                                mi.CurrentVersionFiles
                                                , new EventHandler <FileCheckedEventArgs>((object o, FileCheckedEventArgs a) => {
                                                _progressBar1.Value += progressBarPerFile;
                                                if (_progressBar1.Value > 100)
                                                {
                                                    _progressBar1.Value = 40;
                                                }
                                            })
                                                );
                                        }
                                    }
                                    logDeltaTicks("filecheck");
                                    showMainForm();
                                }
                                catch (Exception ex) {
                                    YU.ErrorAndKill(Locale.Get("CannotCheckFiles") + ":\r\n" + ex.Message);
                                }
                            }
                        }
                        catch (Exception ex) {
                            YU.ErrorAndKill(Locale.Get("CannotParseConfig") + ":\r\n" + ex.Message);
                        }
                    }
                    catch (Exception ex) {
                        YU.ErrorAndKill(Locale.Get("CannotLoadIcon") + ":\r\n" + ex.Message);
                    }
                }
                catch (Exception ex) {
                    YU.ErrorAndKill(Locale.Get("CannotParseSettings") + ":\r\n" + ex.Message);
                }
            }
            catch (Exception ex) {
                UIElement[] btns;
                UIElement   btnQuit = new UIElement();
                btnQuit.Caption = Locale.Get("Quit");
                btnQuit.Result  = DialogResult.Abort;
                UIElement btnRetry = new UIElement();
                btnRetry.Caption = Locale.Get("Retry");
                btnRetry.Result  = DialogResult.Retry;
                string msg;
                if (File.Exists(SETTINGSPATH))
                {
                    msg = Locale.Get("WebClientErrorOffline");
                    UIElement btnOffline = new UIElement();
                    btnOffline.Caption = Locale.Get("RunOffline");
                    btnOffline.Result  = DialogResult.Ignore;
                    btns = new UIElement[] { btnQuit, btnRetry, btnOffline };
                }
                else
                {
                    msg  = Locale.Get("WebClientError");
                    btns = new UIElement[] { btnQuit, btnRetry };
                }
                YobaDialog yobaDialog = new YobaDialog(msg, btns);
                yobaDialog.Icon = Program.LoncherSettings != null ? (Program.LoncherSettings.Icon ?? this.Icon) : this.Icon;
                DialogResult result = yobaDialog.ShowDialog(this);
                switch (result)
                {
                case DialogResult.Retry:
                    Initialize();
                    break;

                case DialogResult.Ignore:
                    InitializeOffline();
                    break;

                case DialogResult.Abort: {
                    Application.Exit();
                    return;
                }
                }
            }
        }