private async void BtnClick_MinecraftDownload(object sender, RoutedEventArgs e)
        {
            Btn_MinecraftDownload.IsEnabled = false;
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    MinecraftVersion ver = VersionsList.SelectedItem as MinecraftVersion;
                    if (ver == null)
                    {
                        return;
                    }
                    string json = await client.GetStringAsync(ver.url);

                    string      t    = ver.GetJsonPath();
                    StorageFile file = await CoreManager.WorkDir.CreateFileAsync(ver.GetJsonPath(), CreationCollisionOption.ReplaceExisting);

                    await FileIO.WriteTextAsync(file, json);

                    ValueSet valueSet = new ValueSet();
                    valueSet["type"]    = "version-url";
                    valueSet["version"] = ver.id;

                    AppServiceResponse response = await AppServiceManager.appServiceConnection.SendMessageAsync(valueSet);

                    string url = response.Message["client"]?.ToString();
                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        DownloadItem item = new DownloadItem("Minecraft" + ver.id, ver.GetJarPath(), url);
                        DownloadManager.DownloadQuene.Add(item);
                        DownloadManager.StartDownload();
                        ViewModel.isPaneOpen = true;
                    }
                }
                catch (HttpRequestException exp)
                {
                    await dialog.Show(CoreManager.GetStringFromResource("/DownloadPage/DownloadFailed"), exp.Message + exp.StackTrace);
                }
                catch (IOException exp)
                {
                    await dialog.Show(CoreManager.GetStringFromResource("/DownloadPage/DownloadFailed"), exp.Message + exp.StackTrace);
                }
                finally
                {
                    Btn_MinecraftDownload.IsEnabled = true;
                }
            }
        }
        private async void Button_WorkDirPicker_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var folderPicker = new FolderPicker();
                folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
                folderPicker.FileTypeFilter.Add("*");

                StorageFolder folder = await folderPicker.PickSingleFolderAsync();

                if (folder != null)
                {
                    // Application now has read/write access to all contents in the picked folder
                    // (including other sub-folder contents)
                    StorageApplicationPermissions.FutureAccessList.AddOrReplace(CoreManager.WorkDirToken, folder);

                    ViewModel.WorkDir   = folder.Path;
                    CoreManager.WorkDir = folder;
                    CheckAction?.Invoke();
                }
            }
            catch (Exception exp)
            {
                await _msgDialog.Show(CoreManager.GetStringFromResource("/SettingsPage/WorkDirSelectError"), exp.Message);
            }
        }
Example #3
0
        private async Task Launch(LaunchOption option)
        {
            //Check connection to launch agent
            if (AppServiceManager.appServiceConnection == null)
            {
                return;
            }

            List <MinecraftAssembly> missingLibs   = null; //include missing natives
            List <MinecraftAsset>    missingAssets = new List <MinecraftAsset>();

            #region Libraries and natives check
            ValueSet valueSet = new ValueSet();
            valueSet["type"]    = "librariesCheck";
            valueSet["version"] = option.versionId;
            AppServiceResponse response = await AppServiceManager.appServiceConnection.SendMessageAsync(valueSet);

            string responseJson = response.Message["value"].ToString();
            try
            {
                missingLibs = JsonConvert.DeserializeObject <List <MinecraftAssembly> >(responseJson);
            }
            catch (JsonException)
            { }
            #endregion

            #region Assets check
            valueSet            = new ValueSet();
            valueSet["type"]    = "assetsCheck";
            valueSet["version"] = option.versionId;
            response            = await AppServiceManager.appServiceConnection.SendMessageAsync(valueSet);

            object obj = null;
            response.Message.TryGetValue("index_path", out obj);
            // Asset index dose not exist or invalid
            if (obj != null)
            {
                string path = obj.ToString();
                string url  = response.Message["index_url"].ToString();

                try
                {
                    using (HttpClient client = new HttpClient())
                    {
                        string json = await client.GetStringAsync(url);

                        StorageFile file = await CoreManager.WorkDir.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);

                        await FileIO.WriteTextAsync(file, json);
                    }
                }
                catch (Exception e)
                {
                    await _msgDialog.Show(
                        CoreManager.GetStringFromResource("/StartPage/LaunchFailed"),
                        "Cannot fetch asset index \r\n " + e.Message + "\r\n" + e.StackTrace
                        );

                    return;
                }

                //Check again after asset index downloaded
                response = await AppServiceManager.appServiceConnection.SendMessageAsync(valueSet);

                obj = null;
                response.Message.TryGetValue("index_path", out obj);
                if (obj != null)
                {
                    await _msgDialog.Show(
                        CoreManager.GetStringFromResource("/StartPage/LaunchFailed"),
                        "Asset index validation failed");

                    return;
                }
            }

            responseJson = response.Message["missing_assets"].ToString();
            try
            {
                missingAssets = JsonConvert.DeserializeObject <List <MinecraftAsset> >(responseJson);
            }
            catch (JsonException) { }
            #endregion

            //Found missing libs, go to download.
            if ((missingLibs != null && missingLibs.Count > 0) || (missingAssets != null && missingAssets.Count > 0))
            {
                missingLibs.ForEach(lib =>
                {
                    DownloadItem item = new DownloadItem(lib.Name, lib.Path, lib.Url);
                    DownloadManager.DownloadQuene.Add(item);
                });
                missingAssets.ForEach(ass =>
                {
                    DownloadItem item = new DownloadItem(
                        string.Format("{0}: {1}", CoreManager.GetStringFromResource("/Resources/Asset"), ass.Hash),
                        ass.GetPath(),
                        ass.GetDownloadUrl()
                        );
                    DownloadManager.DownloadQuene.Add(item);
                });

                DownloadManager.StartDownload();
                await DownloadDialog.ShowAsync();

                return;
            }

            DebugWriteLine("Serializing launch message to json");

            string messageJson;
            try
            {
                LaunchOptionBase tmp = option as LaunchOptionBase;
                if (string.IsNullOrWhiteSpace(option.javaExt))
                {
                    tmp.javaExt = CoreManager.GlobalJVMPath;
                }
                messageJson = JsonConvert.SerializeObject(tmp);
            }
            catch (JsonSerializationException exp)
            {
                DebugWriteLine("ERROR: " + exp.Message);
                return;
            }

            DebugWriteLine(messageJson);

            //Check if the launch message was successfully generated
            if (!string.IsNullOrWhiteSpace(messageJson))
            {
                valueSet = new ValueSet();
                valueSet.Add("type", "launch");
                valueSet.Add("launch_option", messageJson);
                valueSet.Add("auth_type", CoreManager.AccountTypeTag);
                valueSet.Add("auth_username", CoreManager.Username);
                response = await AppServiceManager.appServiceConnection.SendMessageAsync(valueSet);

                //Display error
                obj = response.Message["result"];
                if (obj is bool && !((bool)obj))
                {
                    await _msgDialog.Show(
                        CoreManager.GetStringFromResource("/StartPage/LaunchFailed"),
                        response.Message["errorMessage"].ToString() + "\r\n" + response.Message["errorStack"]
                        );
                }
            }
        }