CmlLib / CmlLib.Core.Auth.Microsoft

Minecraft Login with Microsoft Xbox account
MIT License
22 stars 4 forks source link

This does not work on Windows Forms. #11

Closed voidZiAD closed 2 years ago

voidZiAD commented 2 years ago

I've tried this on a .NET Windows Forms app and it didnt do anything when I clicked button1.

using CmlLib.Core;
using CmlLib.Core.Auth;
using CmlLib.Core.Auth.Microsoft;
using CmlLib.Core.Auth.Microsoft.UI.WinForm;
using Microsoft.Extensions.Logging;
using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XboxAuthNet.OAuth;
using XboxAuthNet.XboxLive;

namespace ElixirClient
{
    public partial class Form1 : Form
    {
        MSession Session;

        public Form1()
        {
            InitializeComponent();
        }

        private async Task<MicrosoftLoginForm> CreateForm()
        {
            var loginHandler = new LoginHandler();
            MicrosoftLoginForm form = new MicrosoftLoginForm(loginHandler);

            //var dataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CmlLib.Core.Auth.Microsoft.UI.WinForm.TestApp");
            //form.WebView2Environment = await CoreWebView2Environment.CreateAsync(userDataFolder: dataPath);

            return form;
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            var form = await CreateForm();

            try
            {
                MSession session = await form.ShowLoginDialog(); // show login form
                this.Session = session;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private async void btnLogout_Click(object sender, EventArgs e)
        {
            MicrosoftLoginForm form = await CreateForm();
            form.ShowLogoutDialog(); // show logout form

            MessageBox.Show("Done");
        }

        private async void btnStart_Click(object sender, EventArgs e)
        {

            var path = new MinecraftPath(); // default game path
            var launcher = new CMLauncher(path);

            // register event handlers
            launcher.FileChanged += Launcher_FileChanged;
            launcher.ProgressChanged += Launcher_ProgressChanged;

            // check and download game files
            var process = await launcher.CreateProcessAsync("1.18.2", new MLaunchOption
            {
                Session = this.Session,
                MaximumRamMb = 4096
            });

            // start game
            process.Start();
        }

        private void Launcher_FileChanged(CmlLib.Core.Downloader.DownloadFileChangedEventArgs e)
        {

        }

        private void Launcher_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
        }
    }
}
voidZiAD commented 2 years ago

Nevermind, I got an error image

No Idea what to do

Line 194: MSession session = await form.ShowLoginDialog();

voidZiAD commented 2 years ago

I think it doesn't support .NET, not Windows Forms but not 100% sure. I need the fix asap!

AlphaBs commented 2 years ago

this may related to WebView2 issue.

https://github.com/MicrosoftEdge/WebView2Feedback/issues/2447

voidZiAD commented 2 years ago

I just checked that issue and I'm still not sure what to do...

voidZiAD commented 2 years ago

I've tried reverting back to an older version (1.0.1210.39) and got a different error this time

  System.StackOverflowException
  HResult=0x800703E9
  Message=Exception of type 'System.StackOverflowException' was thrown.
AlphaBs commented 2 years ago

I've seen very similar issues on another project.

it was made on .net framework and uses Fody.Costura.

StackOverflowException was related to Fody.Costura. Costura was not able to load webview2 library and it retry infinitely.

System.ArgumentException was releated to wrong webview2 library version. (not sure)Screenshot_20220818-200310_Discord.jpg

As I remember, I solved this by removing all nuget package and reinstall only one package: CmlLib.Core.Auth.Microsoft.UI.Winform (or Wpf), and I removed app.config file in my project.

btw webview2 is so buggy actually i don't want to maintain UI.Winform and UI.Wpf package. CmlLib.Core.Auth.Microsoft.MsalClient provides more options to login without any webview2 related bugs.

voidZiAD commented 2 years ago

Oh, then that explains it. I use Costura.Fody and I guess that's why it's happening. Anyways, i've found another way to authenticate accounts via Microsoft Login by using the MSALClient, but thank you for letting me know what the issue is from. I recommend you switch from WebView2 to some other package cause from what i've heard and what you said it's really buggy.