[SKIP_DEPLOY] Support account alias in configs

This commit is contained in:
BruceChen 2022-11-06 16:20:38 +08:00
parent c0266685a8
commit 0b5a562f7f
17 changed files with 213 additions and 157 deletions

View file

@ -9,7 +9,7 @@ body:
attributes: attributes:
label: Prerequisites label: Prerequisites
options: options:
- label: I made sure I am running the latest [development build](https://ci.appveyor.com/project/ORelio/minecraft-console-client/build/artifacts) - label: I made sure I am running the latest [development build](https://github.com/MCCTeam/Minecraft-Console-Client/releases/latest)
required: true required: true
- label: I tried to [look for similar issues](https://github.com/MCCTeam/Minecraft-Console-Client/issues?q=is%3Aissue) before opening a new one - label: I tried to [look for similar issues](https://github.com/MCCTeam/Minecraft-Console-Client/issues?q=is%3Aissue) before opening a new one
required: true required: true

View file

@ -10,9 +10,9 @@ body:
attributes: attributes:
label: Prerequisites label: Prerequisites
options: options:
- label: I have read and understood the [user manual](https://github.com/MCCTeam/Minecraft-Console-Client/tree/master/MinecraftClient/config) - label: I have read and understood the [user manual](https://mccteam.github.io/guide/)
required: true required: true
- label: I made sure I am running the latest [development build](https://ci.appveyor.com/project/ORelio/Minecraft-Console-Client/build/artifacts) - label: I made sure I am running the latest [development build](https://github.com/MCCTeam/Minecraft-Console-Client/releases/latest)
required: true required: true
- label: I tried to [look for similar feature requests](https://github.com/MCCTeam/Minecraft-Console-Client/issues?q=is%3Aissue) before opening a new one - label: I tried to [look for similar feature requests](https://github.com/MCCTeam/Minecraft-Console-Client/issues?q=is%3Aissue) before opening a new one
required: true required: true

View file

@ -111,6 +111,7 @@ namespace MinecraftClient
} }
//Process ini configuration file //Process ini configuration file
{
bool loadSucceed, needWriteDefaultSetting, newlyGenerated = false; bool loadSucceed, needWriteDefaultSetting, newlyGenerated = false;
if (args.Length >= 1 && File.Exists(args[0]) && Settings.ToLowerIfNeed(Path.GetExtension(args[0])) == ".ini") if (args.Length >= 1 && File.Exists(args[0]) && Settings.ToLowerIfNeed(Path.GetExtension(args[0])) == ".ini")
{ {
@ -183,12 +184,6 @@ namespace MinecraftClient
ConsoleIO.WriteLine(string.Format(Translations.mcc_help_us_translate, Settings.TranslationProjectUrl)); ConsoleIO.WriteLine(string.Format(Translations.mcc_help_us_translate, Settings.TranslationProjectUrl));
WriteBackSettings(true); // format WriteBackSettings(true); // format
} }
bool needPromptUpdate = true;
if (Settings.CheckUpdate(Config.Head.CurrentVersion, Config.Head.LatestVersion))
{
needPromptUpdate = false;
ConsoleIO.WriteLineFormatted(string.Format(Translations.mcc_has_update, Settings.GithubReleaseUrl));
} }
//Other command-line arguments //Other command-line arguments
@ -280,20 +275,22 @@ namespace MinecraftClient
Console.WriteLine(string.Format(Translations.mcc_generator_done, dataGenerator, dataPath)); Console.WriteLine(string.Format(Translations.mcc_generator_done, dataGenerator, dataPath));
return; return;
} }
}
try if (Config.Main.Advanced.ConsoleTitle != "")
{ {
Settings.LoadArguments(args); InternalConfig.Username = "New Window";
} Console.Title = Config.AppVar.ExpandVars(Config.Main.Advanced.ConsoleTitle);
catch (ArgumentException e)
{
InternalConfig.InteractiveMode = false;
HandleFailure(e.Message);
return;
}
} }
// Check for updates // Check for updates
{
bool needPromptUpdate = true;
if (Settings.CheckUpdate(Config.Head.CurrentVersion, Config.Head.LatestVersion))
{
needPromptUpdate = false;
ConsoleIO.WriteLineFormatted(string.Format(Translations.mcc_has_update, Settings.GithubReleaseUrl));
}
Task.Run(() => Task.Run(() =>
{ {
HttpClientHandler httpClientHandler = new() { AllowAutoRedirect = false }; HttpClientHandler httpClientHandler = new() { AllowAutoRedirect = false };
@ -327,11 +324,21 @@ namespace MinecraftClient
httpClient.Dispose(); httpClient.Dispose();
httpClientHandler.Dispose(); httpClientHandler.Dispose();
}); });
}
if (Config.Main.Advanced.ConsoleTitle != "") // Load command-line arguments
if (args.Length >= 1)
{ {
InternalConfig.Username = "New Window"; try
Console.Title = Config.AppVar.ExpandVars(Config.Main.Advanced.ConsoleTitle); {
Settings.LoadArguments(args);
}
catch (ArgumentException e)
{
InternalConfig.InteractiveMode = false;
HandleFailure(e.Message);
return;
}
} }
//Test line to troubleshoot invisible colors //Test line to troubleshoot invisible colors
@ -382,26 +389,22 @@ namespace MinecraftClient
//Asking the user to type in missing data such as Username and Password //Asking the user to type in missing data such as Username and Password
bool useBrowser = Config.Main.General.AccountType == LoginType.microsoft && Config.Main.General.Method == LoginMethod.browser; bool useBrowser = Config.Main.General.AccountType == LoginType.microsoft && Config.Main.General.Method == LoginMethod.browser;
if (string.IsNullOrEmpty(InternalConfig.Login) && !useBrowser) if (string.IsNullOrWhiteSpace(InternalConfig.Account.Login) && !useBrowser)
{ {
ConsoleIO.WriteLine(ConsoleIO.BasicIO ? Translations.mcc_login_basic_io : Translations.mcc_login); ConsoleIO.WriteLine(ConsoleIO.BasicIO ? Translations.mcc_login_basic_io : Translations.mcc_login);
InternalConfig.Login = ConsoleIO.ReadLine().Trim(); InternalConfig.Account.Login = ConsoleIO.ReadLine().Trim();
if (string.IsNullOrEmpty(InternalConfig.Login)) if (string.IsNullOrWhiteSpace(InternalConfig.Account.Login))
{ {
HandleFailure(Translations.error_login_blocked, false, ChatBot.DisconnectReason.LoginRejected); HandleFailure(Translations.error_login_blocked, false, ChatBot.DisconnectReason.LoginRejected);
return; return;
} }
} }
InternalConfig.Username = InternalConfig.Login; InternalConfig.Username = InternalConfig.Account.Login;
if (string.IsNullOrEmpty(Config.Main.General.Account.Password) && string.IsNullOrEmpty(InternalConfig.Password) && !useBrowser && if (string.IsNullOrWhiteSpace(InternalConfig.Account.Password) && !useBrowser &&
(Config.Main.Advanced.SessionCache == CacheType.none || !SessionCache.Contains(Settings.ToLowerIfNeed(InternalConfig.Login)))) (Config.Main.Advanced.SessionCache == CacheType.none || !SessionCache.Contains(ToLowerIfNeed(InternalConfig.Account.Login))))
{ {
RequestPassword(); RequestPassword();
} }
else if (string.IsNullOrEmpty(InternalConfig.Password))
{
InternalConfig.Password = Config.Main.General.Account.Password;
}
startupargs = args; startupargs = args;
InitializeClient(); InitializeClient();
@ -412,10 +415,12 @@ namespace MinecraftClient
/// </summary> /// </summary>
private static void RequestPassword() private static void RequestPassword()
{ {
ConsoleIO.WriteLine(ConsoleIO.BasicIO ? string.Format(Translations.mcc_password_basic_io, InternalConfig.Login) + "\n" : Translations.mcc_password); ConsoleIO.WriteLine(ConsoleIO.BasicIO ? string.Format(Translations.mcc_password_basic_io, InternalConfig.Account.Login) + "\n" : Translations.mcc_password);
string? password = ConsoleIO.BasicIO ? Console.ReadLine() : ConsoleIO.ReadPassword(); string? password = ConsoleIO.BasicIO ? Console.ReadLine() : ConsoleIO.ReadPassword();
if (password == null || password == string.Empty) { password = "-"; } if (string.IsNullOrWhiteSpace(password))
InternalConfig.Password = password; InternalConfig.Account.Password = "-";
else
InternalConfig.Account.Password = password;
} }
/// <summary> /// <summary>
@ -430,8 +435,8 @@ namespace MinecraftClient
ProtocolHandler.LoginResult result = ProtocolHandler.LoginResult.LoginRequired; ProtocolHandler.LoginResult result = ProtocolHandler.LoginResult.LoginRequired;
string loginLower = Settings.ToLowerIfNeed(InternalConfig.Login); string loginLower = ToLowerIfNeed(InternalConfig.Account.Login);
if (InternalConfig.Password == "-") if (InternalConfig.Account.Password == "-")
{ {
ConsoleIO.WriteLineFormatted(Translations.mcc_offline, acceptnewlines: true); ConsoleIO.WriteLineFormatted(Translations.mcc_offline, acceptnewlines: true);
result = ProtocolHandler.LoginResult.Success; result = ProtocolHandler.LoginResult.Success;
@ -463,8 +468,8 @@ namespace MinecraftClient
} }
if (result != ProtocolHandler.LoginResult.Success if (result != ProtocolHandler.LoginResult.Success
&& InternalConfig.Password == "" && string.IsNullOrWhiteSpace(InternalConfig.Account.Password)
&& Config.Main.General.AccountType == LoginType.mojang) && !(Config.Main.General.AccountType == LoginType.microsoft && Config.Main.General.Method == LoginMethod.browser))
RequestPassword(); RequestPassword();
} }
else ConsoleIO.WriteLineFormatted(string.Format(Translations.mcc_session_valid, session.PlayerName)); else ConsoleIO.WriteLineFormatted(string.Format(Translations.mcc_session_valid, session.PlayerName));
@ -473,7 +478,7 @@ namespace MinecraftClient
if (result != ProtocolHandler.LoginResult.Success) if (result != ProtocolHandler.LoginResult.Success)
{ {
ConsoleIO.WriteLine(string.Format(Translations.mcc_connecting, Config.Main.General.AccountType == LoginType.mojang ? "Minecraft.net" : "Microsoft")); ConsoleIO.WriteLine(string.Format(Translations.mcc_connecting, Config.Main.General.AccountType == LoginType.mojang ? "Minecraft.net" : "Microsoft"));
result = ProtocolHandler.GetLogin(InternalConfig.Login, InternalConfig.Password, Config.Main.General.AccountType, out session); result = ProtocolHandler.GetLogin(InternalConfig.Account.Login, InternalConfig.Account.Password, Config.Main.General.AccountType, out session);
} }
if (result == ProtocolHandler.LoginResult.Success && Config.Main.Advanced.SessionCache != CacheType.none) if (result == ProtocolHandler.LoginResult.Success && Config.Main.Advanced.SessionCache != CacheType.none)
@ -582,8 +587,10 @@ namespace MinecraftClient
} }
} }
if (Config.Main.General.AccountType == LoginType.microsoft && InternalConfig.Password != "-" && if (Config.Main.General.AccountType == LoginType.microsoft
Config.Signature.LoginWithSecureProfile && protocolversion >= 759 /* 1.19 and above */) && (InternalConfig.Account.Password != "-" || Config.Main.General.Method == LoginMethod.browser)
&& Config.Signature.LoginWithSecureProfile
&& protocolversion >= 759 /* 1.19 and above */)
{ {
// Load cached profile key from disk if necessary // Load cached profile key from disk if necessary
if (Config.Main.Advanced.ProfileKeyCache == CacheType.disk) if (Config.Main.Advanced.ProfileKeyCache == CacheType.disk)

View file

@ -535,7 +535,7 @@ namespace MinecraftClient.Protocol.Handlers
{ {
session.ServerIDhash = serverIDhash; session.ServerIDhash = serverIDhash;
session.ServerPublicKey = serverPublicKey; session.ServerPublicKey = serverPublicKey;
SessionCache.Store(InternalConfig.Login.ToLower(), session); SessionCache.Store(InternalConfig.Account.Login.ToLower(), session);
} }
else else
{ {

View file

@ -2004,7 +2004,7 @@ namespace MinecraftClient.Protocol.Handlers
{ {
session.ServerIDhash = serverIDhash; session.ServerIDhash = serverIDhash;
session.ServerPublicKey = serverPublicKey; session.ServerPublicKey = serverPublicKey;
SessionCache.Store(InternalConfig.Login.ToLower(), session); SessionCache.Store(InternalConfig.Account.Login.ToLower(), session);
} }
else else
{ {

View file

@ -570,11 +570,12 @@ namespace MinecraftClient.Protocol
Microsoft.OpenBrowser(Microsoft.SignInUrl); Microsoft.OpenBrowser(Microsoft.SignInUrl);
else else
Microsoft.OpenBrowser(Microsoft.GetSignInUrlWithHint(loginHint)); Microsoft.OpenBrowser(Microsoft.GetSignInUrlWithHint(loginHint));
ConsoleIO.WriteLine("Your browser should open automatically. If not, open the link below in your browser."); ConsoleIO.WriteLine(Translations.mcc_browser_open);
ConsoleIO.WriteLine("\n" + Microsoft.SignInUrl + "\n"); ConsoleIO.WriteLine("\n" + Microsoft.SignInUrl + "\n");
ConsoleIO.WriteLine("Paste your code here"); ConsoleIO.WriteLine(Translations.mcc_browser_login_code);
string code = ConsoleIO.ReadLine(); string code = ConsoleIO.ReadLine();
ConsoleIO.WriteLine(string.Format(Translations.mcc_connecting, "Microsoft"));
var msaResponse = Microsoft.RequestAccessToken(code); var msaResponse = Microsoft.RequestAccessToken(code);
return MicrosoftLogin(msaResponse, out session); return MicrosoftLogin(msaResponse, out session);
@ -604,7 +605,7 @@ namespace MinecraftClient.Protocol
session.PlayerID = profile.UUID; session.PlayerID = profile.UUID;
session.ID = accessToken; session.ID = accessToken;
session.RefreshToken = msaResponse.RefreshToken; session.RefreshToken = msaResponse.RefreshToken;
InternalConfig.Login = msaResponse.Email; InternalConfig.Account.Login = msaResponse.Email;
return LoginResult.Success; return LoginResult.Success;
} }
else else

View file

@ -6901,6 +6901,24 @@ namespace MinecraftClient {
} }
} }
/// <summary>
/// Looks up a localized string similar to Paste your code here:.
/// </summary>
internal static string mcc_browser_login_code {
get {
return ResourceManager.GetString("mcc.browser_login_code", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Your browser should open automatically. If not, open the link below in your browser..
/// </summary>
internal static string mcc_browser_open {
get {
return ResourceManager.GetString("mcc.browser_open", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Connecting to {0}.... /// Looks up a localized string similar to Connecting to {0}....
/// </summary> /// </summary>

View file

@ -59,7 +59,7 @@
: using a System.ComponentModel.TypeConverter : using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding. : and then encoded with base64 encoding.
--> -->
<xsd:schema xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="root"> <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true"> <xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType> <xsd:complexType>
@ -2670,4 +2670,10 @@ Logging in...</value>
<data name="config.Main.Advanced.temporary_fix_badpacket" xml:space="preserve"> <data name="config.Main.Advanced.temporary_fix_badpacket" xml:space="preserve">
<value>Temporary fix for Badpacket issue on some servers.</value> <value>Temporary fix for Badpacket issue on some servers.</value>
</data> </data>
<data name="mcc.browser_login_code" xml:space="preserve">
<value>Paste your code here:</value>
</data>
<data name="mcc.browser_open" xml:space="preserve">
<value>Your browser should open automatically. If not, open the link below in your browser.</value>
</data>
</root> </root>

View file

@ -19,6 +19,7 @@ using static MinecraftClient.Settings.ChatFormatConfigHelper;
using static MinecraftClient.Settings.HeadCommentHealper; using static MinecraftClient.Settings.HeadCommentHealper;
using static MinecraftClient.Settings.LoggingConfigHealper; using static MinecraftClient.Settings.LoggingConfigHealper;
using static MinecraftClient.Settings.MainConfigHealper; using static MinecraftClient.Settings.MainConfigHealper;
using static MinecraftClient.Settings.MainConfigHealper.MainConfig;
using static MinecraftClient.Settings.MainConfigHealper.MainConfig.AdvancedConfig; using static MinecraftClient.Settings.MainConfigHealper.MainConfig.AdvancedConfig;
using static MinecraftClient.Settings.MCSettingsConfigHealper; using static MinecraftClient.Settings.MCSettingsConfigHealper;
using static MinecraftClient.Settings.SignatureConfigHelper; using static MinecraftClient.Settings.SignatureConfigHelper;
@ -47,12 +48,10 @@ namespace MinecraftClient
public static ushort ServerPort = 25565; public static ushort ServerPort = 25565;
public static string Login = string.Empty; public static AccountInfoConfig Account = new();
public static string Username = string.Empty; public static string Username = string.Empty;
public static string Password = string.Empty;
public static string MinecraftVersion = string.Empty; public static string MinecraftVersion = string.Empty;
public static bool InteractiveMode = true; public static bool InteractiveMode = true;
@ -252,6 +251,7 @@ namespace MinecraftClient
public static void LoadArguments(string[] args) public static void LoadArguments(string[] args)
{ {
int positionalIndex = 0; int positionalIndex = 0;
bool skipPassword = false;
foreach (string argument in args) foreach (string argument in args)
{ {
@ -272,11 +272,20 @@ namespace MinecraftClient
switch (positionalIndex) switch (positionalIndex)
{ {
case 0: case 0:
InternalConfig.Login = argument; if (Config.Main.Advanced.AccountList.TryGetValue(argument, out AccountInfoConfig accountInfo))
{
InternalConfig.Account = accountInfo;
skipPassword = true;
}
else
{
InternalConfig.Account.Login = argument;
}
InternalConfig.KeepAccountSettings = true; InternalConfig.KeepAccountSettings = true;
break; break;
case 1: case 1:
InternalConfig.Password = argument; if (!skipPassword)
InternalConfig.Account.Password = argument;
break; break;
case 2: case 2:
Config.Main.SetServerIP(new MainConfig.ServerInfoConfig(argument), true); Config.Main.SetServerIP(new MainConfig.ServerInfoConfig(argument), true);
@ -388,7 +397,12 @@ namespace MinecraftClient
General.Account.Login ??= string.Empty; General.Account.Login ??= string.Empty;
General.Account.Password ??= string.Empty; General.Account.Password ??= string.Empty;
if (!InternalConfig.KeepAccountSettings) if (!InternalConfig.KeepAccountSettings)
InternalConfig.Login = General.Account.Login; {
if (Advanced.AccountList.TryGetValue(General.Account.Login, out AccountInfoConfig account))
InternalConfig.Account = account;
else
InternalConfig.Account = General.Account;
}
General.Server.Host ??= string.Empty; General.Server.Host ??= string.Empty;
@ -597,8 +611,7 @@ namespace MinecraftClient
{ {
if (AccountList.TryGetValue(accountAlias, out AccountInfoConfig accountInfo)) if (AccountList.TryGetValue(accountAlias, out AccountInfoConfig accountInfo))
{ {
InternalConfig.Login = accountInfo.Login; InternalConfig.Account = accountInfo;
InternalConfig.Password = accountInfo.Password;
return true; return true;
} }
else else
@ -894,7 +907,7 @@ namespace MinecraftClient
switch (varname_lower) switch (varname_lower)
{ {
case "username": result.Append(InternalConfig.Username); break; case "username": result.Append(InternalConfig.Username); break;
case "login": result.Append(InternalConfig.Login); break; case "login": result.Append(InternalConfig.Account.Login); break;
case "serverip": result.Append(InternalConfig.ServerIP); break; case "serverip": result.Append(InternalConfig.ServerIP); break;
case "serverport": result.Append(InternalConfig.ServerPort); break; case "serverport": result.Append(InternalConfig.ServerPort); break;
case "datetime": case "datetime":

View file

@ -1,4 +1,4 @@
Minecraft Console Client User Manual Minecraft Console Client User Manual
====== ======
This page has been moved to appropriate sections on our new [Documentation website](https://mccteam.github.io/docs/). This page has been moved to appropriate sections on our new [Documentation website](https://mccteam.github.io/guide/configuration.html).

View file

@ -1,6 +1,7 @@
--- ---
title: About & Features title: About & Features
--- ---
# Introduction # Introduction
- [About](#about) - [About](#about)

View file

@ -1,7 +1,10 @@
--- ---
title: Chat Bots title: Chat Bots
redirectFrom: ["/g/bots/index.html", "/g/bots.html"] redirectFrom:
- "/g/bots/index.html"
- "/g/bots.html"
--- ---
# Chat Bots # Chat Bots
- [About](#about) - [About](#about)

View file

@ -1,7 +1,10 @@
--- ---
title: Configuration title: Configuration
redirectFrom: ["/g/conf/index.html", "/g/conf.html"] redirectFrom:
- "/g/conf/index.html"
- "/g/conf.html"
--- ---
# Configuration # Configuration
**Minecraft Console Client** can be both configured by the [command line parameters](usage.md#command-line-parameters) and the configuration file. **Minecraft Console Client** can be both configured by the [command line parameters](usage.md#command-line-parameters) and the configuration file.

View file

@ -1,6 +1,7 @@
--- ---
title: Contributing title: Contributing
--- ---
# Contributing # Contributing
At this moment this page needs to be created. At this moment this page needs to be created.

View file

@ -1,6 +1,7 @@
--- ---
title: Creating Chat Bots title: Creating Chat Bots
--- ---
# Creating Chat Bots # Creating Chat Bots
- [Notes](#notes) - [Notes](#notes)

View file

@ -1,6 +1,7 @@
--- ---
title: Installation title: Installation
--- ---
# Installation # Installation
- [YouTube Tutorials](#youtube-tutorials) - [YouTube Tutorials](#youtube-tutorials)

View file

@ -1,6 +1,7 @@
--- ---
title: Usage title: Usage
--- ---
# Usage # Usage
How to run the program: How to run the program: