Skip to content

Commit

Permalink
Fixed issue #331
Browse files Browse the repository at this point in the history
  • Loading branch information
SDClowen committed Jul 25, 2022
1 parent 7fdff38 commit e14636a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 71 deletions.
6 changes: 3 additions & 3 deletions Application/RSBot/Helper/ProfilePathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ internal static class ProfilePathHelper
{
public static string GetProfileConfigFileName()
{
return Path.Combine(Environment.CurrentDirectory, "User", "Profiles.rs");
return Path.Combine(Environment.CurrentDirectory, "Data", "User", "Profiles.rs");
}

public static string GetProfileFile(string profileName)
{
return Path.Combine(Environment.CurrentDirectory, "User", $"{profileName}.rs");
return Path.Combine(Environment.CurrentDirectory, "Data", "User", $"{profileName}.rs");
}

public static string GetProfileDirectory(string profileName)
{
return Path.Combine(Environment.CurrentDirectory, "User", profileName);
return Path.Combine(Environment.CurrentDirectory, "Data", "User", profileName);
}
}
}
59 changes: 39 additions & 20 deletions Application/RSBot/Views/Dialog/ProfileSelectionDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 32 additions & 37 deletions Application/RSBot/Views/Dialog/ProfileSelectionDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,21 @@ private void LoadProfiles()
//Add the default profile if no profiles have been saved by the user
if (profiles.Count == 0)
{
profiles.Add("Default"); //Always points to Data\Default.rs
profiles.Add("Default");

_profileConfig.SetArray("RSBot.Profiles", profiles); //Save default to profiles
_profileConfig.Save();
}

foreach (var profile in profiles)
comboProfiles.Items.Add(profile);

comboProfiles.Items.Add("New..."); //The last item is ALWAYS new

comboProfiles.SelectedItem = _profileConfig.Get("RSBot.SelectedProfile", "Default");

if (comboProfiles.SelectedItem == null)
comboProfiles.SelectedItem = "Default";
}

#endregion Methods

#region Events

private void comboProfiles_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboProfiles.SelectedItem == null)
return;

if (comboProfiles.SelectedIndex != comboProfiles.Items.Count - 1) //New...?
{
SelectedProfile = (string)comboProfiles.SelectedItem;

_profileConfig.Set("RSBot.SelectedProfile", SelectedProfile);

_profileConfig.Save();
}
else //New profile...
{
SelectedProfile = CreateNewProfile();

LoadProfiles();
}
}

private string CreateNewProfile()
{
var inputDialog = new InputDialog("New profile", "New profile", "Please enter a profile name");
Expand Down Expand Up @@ -114,7 +87,9 @@ private string CreateNewProfile()
existingProfiles.Add($"{profileName}");

var newProfileDirectory = ProfilePathHelper.GetProfileDirectory(profileName);
Directory.CreateDirectory(newProfileDirectory);

if(!Directory.Exists(newProfileDirectory))
Directory.CreateDirectory(newProfileDirectory);

_profileConfig.SetArray("RSBot.Profiles", existingProfiles, "|");
_profileConfig.Set("RSBot.SelectedProfile", profileName);
Expand Down Expand Up @@ -154,19 +129,30 @@ private static void CopyOldProfileData(string profileName)
}
}

#endregion Events
#endregion Methods

#region Events

private void comboProfiles_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboProfiles.SelectedItem == null)
return;

SelectedProfile = (string)comboProfiles.SelectedItem;

_profileConfig.Set("RSBot.SelectedProfile", SelectedProfile);

_profileConfig.Save();
}

private void checkSaveSelection_CheckedChanged(object sender, EventArgs e)
{
_profileConfig.Set("RSBot.ShowProfileDialog", !checkSaveSelection.Checked);
_profileConfig.Save();
}

private void linkDeleteProfile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
private void buttonDeleteProfile_Click(object sender, EventArgs e)
{
if (comboProfiles.SelectedIndex == comboProfiles.Items.Count - 1) //New...
return;

if (comboProfiles.SelectedIndex == 0) //Default
{
MessageBox.Show("You can not delete the default profile!", "Default profile",
Expand All @@ -175,11 +161,11 @@ private void linkDeleteProfile_LinkClicked(object sender, LinkLabelLinkClickedEv
return;
}

if (Kernel.Profile == (string) comboProfiles.SelectedItem) //Active profile?
if (Kernel.Profile == (string)comboProfiles.SelectedItem) //Active profile?
{
MessageBox.Show("You can not delete the active profile!", "Profile active",
MessageBoxButtons.OK, MessageBoxIcon.Error);

return;
}

Expand All @@ -189,12 +175,21 @@ private void linkDeleteProfile_LinkClicked(object sender, LinkLabelLinkClickedEv
return;

var profiles = _profileConfig.GetArray<string>("RSBot.Profiles", '|').ToList();
profiles.Remove((string) comboProfiles.SelectedItem);
profiles.Remove((string)comboProfiles.SelectedItem);

_profileConfig.SetArray("RSBot.Profiles", profiles, "|");
_profileConfig.Save();

LoadProfiles();
}

private void buttonCreateProfile_Click(object sender, EventArgs e)
{
SelectedProfile = CreateNewProfile();

LoadProfiles();
}

#endregion Events
}
}
16 changes: 10 additions & 6 deletions Application/RSBot/Views/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,24 +665,28 @@ private void coloredToolStripMenuItem_Click(object sender, EventArgs e)

private void menuSelectProfile_Click(object sender, EventArgs e)
{
var diag = new ProfileSelectionDialog();
diag.StartPosition = FormStartPosition.CenterParent;
if (diag.ShowDialog() != DialogResult.OK)
var dialog = new ProfileSelectionDialog();
dialog.StartPosition = FormStartPosition.CenterParent;
dialog.ShowInTaskbar = false;
if (dialog.ShowDialog() != DialogResult.OK)
return;

if (dialog.SelectedProfile == Kernel.Profile)
return;

var oldSroPath = GlobalConfig.Get("RSBot.SilkroadDirectory", "");

//We need this to check if the sro directories are different
var tempNewConfig = new Config(ProfilePathHelper.GetProfileFile(diag.SelectedProfile));
var tempNewConfig = new Config(ProfilePathHelper.GetProfileFile(dialog.SelectedProfile));

if (oldSroPath != tempNewConfig.Get("RSBot.SilkroadDirectory", ""))
{
if (MessageBox.Show("This profile references to a different client, do you want to restart the bot?", "Restart bot?", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
Environment.Exit(0);
Application.Restart();

}

Kernel.Profile = diag.SelectedProfile;
Kernel.Profile = dialog.SelectedProfile;
GlobalConfig.Load();

EventManager.FireEvent("OnProfileChanged");
Expand Down
2 changes: 1 addition & 1 deletion Library/RSBot.Core/Config/GlobalConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class GlobalConfig
/// </summary>
public static void Load()
{
var path = Path.Combine(Environment.CurrentDirectory, "User", Kernel.Profile + ".rs");
var path = Path.Combine(Environment.CurrentDirectory, "Data", "User", Kernel.Profile + ".rs");

_config = new Config(path);

Expand Down
7 changes: 3 additions & 4 deletions Plugins/RSBot.General/Components/Accounts.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using RSBot.General.Models;
using RSBot.Core;
using RSBot.General.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using RSBot.Core;

namespace RSBot.General.Components
{
Expand All @@ -25,7 +24,7 @@ internal class Accounts
/// <summary>
/// Get the data file path
/// </summary>
private static string _filePath => Path.Combine(Environment.CurrentDirectory, "User", Kernel.Profile, "autologin.data");
private static string _filePath => Path.Combine(Environment.CurrentDirectory, "Data", "User", Kernel.Profile, "autologin.data");

/// <summary>
/// Check the saving directory
Expand Down

0 comments on commit e14636a

Please sign in to comment.