Server Import/Set Location Changes

- when changing the location, the profile will be updated to use the new config files.
This commit is contained in:
Brett Hewitson 2022-06-19 00:04:01 +10:00
parent a2d5c92c00
commit 7015523837
12 changed files with 100 additions and 27 deletions

View file

@ -70,7 +70,7 @@ namespace ServerManagerTool.Lib
public static Server FromPath(string path) public static Server FromPath(string path)
{ {
var loadedProfile = ServerProfile.LoadFrom(path); var loadedProfile = ServerProfile.LoadFrom(path, profile: null);
if (loadedProfile == null) if (loadedProfile == null)
return null; return null;
return new Server(loadedProfile); return new Server(loadedProfile);

View file

@ -3511,10 +3511,23 @@ namespace ServerManagerTool.Lib
#region Methods #region Methods
#region Common Methods #region Common Methods
public void ChangeInstallationFolder(string folder) public void ChangeInstallationFolder(string folder, bool reloadConfigFiles)
{ {
InstallDirectory = folder; InstallDirectory = folder;
if (reloadConfigFiles)
{
var serverConfigPath = GetProfileServerConfigDir();
if (Directory.Exists(serverConfigPath))
{
var serverConfigFile = Path.Combine(serverConfigPath, Config.Default.ServerGameUserSettingsConfigFile);
if (File.Exists(serverConfigFile))
{
LoadFromConfigFiles(serverConfigFile, this, exclusions: null);
}
}
}
LoadServerFiles(true, true, true); LoadServerFiles(true, true, true);
SetupServerFilesWatcher(); SetupServerFilesWatcher();
} }
@ -4035,12 +4048,12 @@ namespace ServerManagerTool.Lib
return LoadFromProfileFile(file, profile); return LoadFromProfileFile(file, profile);
var filePath = Path.GetDirectoryName(file); var filePath = Path.GetDirectoryName(file);
profile = LoadFromINIFiles(file, profile); profile = LoadFromConfigFiles(file, profile, exclusions: null);
if (filePath.EndsWith(Config.Default.ServerConfigRelativePath)) if (filePath.EndsWith(Config.Default.ServerConfigRelativePath))
{ {
var installDirectory = filePath.Replace(Config.Default.ServerConfigRelativePath, string.Empty).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); var installDirectory = filePath.Replace(Config.Default.ServerConfigRelativePath, string.Empty).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
profile.ChangeInstallationFolder(installDirectory); profile.ChangeInstallationFolder(installDirectory, reloadConfigFiles: false);
} }
if (profile.PlayerLevels.Count == 0) if (profile.PlayerLevels.Count == 0)
@ -4075,7 +4088,7 @@ namespace ServerManagerTool.Lib
return profile; return profile;
} }
public static ServerProfile LoadFromINIFiles(string file, ServerProfile profile, IEnumerable<Enum> exclusions = null) public static ServerProfile LoadFromConfigFiles(string file, ServerProfile profile, IEnumerable<Enum> exclusions = null)
{ {
if (string.IsNullOrWhiteSpace(file) || !File.Exists(file)) if (string.IsNullOrWhiteSpace(file) || !File.Exists(file))
return null; return null;
@ -4186,10 +4199,10 @@ namespace ServerManagerTool.Lib
return null; return null;
} }
var configIniPath = Path.Combine(GetProfileServerConfigDir(profile), Config.Default.ServerGameUserSettingsConfigFile); var serverConfigFile = Path.Combine(GetProfileServerConfigDir(profile), Config.Default.ServerGameUserSettingsConfigFile);
if (File.Exists(configIniPath)) if (File.Exists(serverConfigFile))
{ {
profile = LoadFromINIFiles(configIniPath, profile); profile = LoadFromConfigFiles(serverConfigFile, profile, exclusions: null);
} }
profile._lastSaveLocation = file; profile._lastSaveLocation = file;
@ -4460,10 +4473,10 @@ namespace ServerManagerTool.Lib
// //
string configDir = GetProfileServerConfigDir(); string configDir = GetProfileServerConfigDir();
Directory.CreateDirectory(configDir); Directory.CreateDirectory(configDir);
SaveINIFile(configDir); SaveConfigFile(configDir);
} }
public void SaveINIFile(string profileIniDir, IEnumerable<Enum> exclusions = null) public void SaveConfigFile(string profileIniDir, IEnumerable<Enum> exclusions = null)
{ {
if (exclusions == null) if (exclusions == null)
exclusions = GetExclusions(); exclusions = GetExclusions();

View file

@ -1036,10 +1036,12 @@ namespace ServerManagerTool
private void SelectInstallDirectory_Click(object sender, RoutedEventArgs e) private void SelectInstallDirectory_Click(object sender, RoutedEventArgs e)
{ {
var dialog = new CommonOpenFileDialog(); var dialog = new CommonOpenFileDialog
dialog.IsFolderPicker = true; {
dialog.Title = _globalizer.GetResourceString("ServerSettings_InstallServer_Title"); IsFolderPicker = true,
if (!String.IsNullOrWhiteSpace(Settings.InstallDirectory)) Title = _globalizer.GetResourceString("ServerSettings_InstallServer_Title")
};
if (!string.IsNullOrWhiteSpace(Settings.InstallDirectory))
{ {
dialog.InitialDirectory = Settings.InstallDirectory; dialog.InitialDirectory = Settings.InstallDirectory;
} }
@ -1047,7 +1049,10 @@ namespace ServerManagerTool
var result = dialog.ShowDialog(Window.GetWindow(this)); var result = dialog.ShowDialog(Window.GetWindow(this));
if (result == CommonFileDialogResult.Ok) if (result == CommonFileDialogResult.Ok)
{ {
Settings.ChangeInstallationFolder(dialog.FileName); Settings.ServerMap = string.Empty;
Settings.TotalConversionModId = string.Empty;
Settings.ChangeInstallationFolder(dialog.FileName, reloadConfigFiles: true);
} }
} }
@ -2047,7 +2052,7 @@ namespace ServerManagerTool
var configIniFile = Path.Combine(ServerProfile.GetProfileServerConfigDir(Settings), Config.Default.ServerGameUserSettingsConfigFile); var configIniFile = Path.Combine(ServerProfile.GetProfileServerConfigDir(Settings), Config.Default.ServerGameUserSettingsConfigFile);
// load only this section, using the full exclusion list // load only this section, using the full exclusion list
var tempServerProfile = ServerProfile.LoadFromINIFiles(configIniFile, null, exclusions); var tempServerProfile = ServerProfile.LoadFromConfigFiles(configIniFile, null, exclusions);
// perform a profile sync // perform a profile sync
Settings.SyncSettings(ServerProfileCategory.CustomGameUserSettings, tempServerProfile); Settings.SyncSettings(ServerProfileCategory.CustomGameUserSettings, tempServerProfile);
} }
@ -2208,7 +2213,7 @@ namespace ServerManagerTool
var configIniFile = Path.Combine(ServerProfile.GetProfileServerConfigDir(Settings), Config.Default.ServerGameUserSettingsConfigFile); var configIniFile = Path.Combine(ServerProfile.GetProfileServerConfigDir(Settings), Config.Default.ServerGameUserSettingsConfigFile);
// load only this section, using the full exclusion list // load only this section, using the full exclusion list
var tempServerProfile = ServerProfile.LoadFromINIFiles(configIniFile, null, exclusions); var tempServerProfile = ServerProfile.LoadFromConfigFiles(configIniFile, null, exclusions);
// perform a profile sync // perform a profile sync
Settings.SyncSettings(ServerProfileCategory.CustomGameSettings, tempServerProfile); Settings.SyncSettings(ServerProfileCategory.CustomGameSettings, tempServerProfile);
} }
@ -2369,7 +2374,7 @@ namespace ServerManagerTool
var configIniFile = Path.Combine(ServerProfile.GetProfileServerConfigDir(Settings), Config.Default.ServerGameUserSettingsConfigFile); var configIniFile = Path.Combine(ServerProfile.GetProfileServerConfigDir(Settings), Config.Default.ServerGameUserSettingsConfigFile);
// load only this section, using the full exclusion list // load only this section, using the full exclusion list
var tempServerProfile = ServerProfile.LoadFromINIFiles(configIniFile, null, exclusions); var tempServerProfile = ServerProfile.LoadFromConfigFiles(configIniFile, null, exclusions);
// perform a profile sync // perform a profile sync
Settings.SyncSettings(ServerProfileCategory.CustomEngineSettings, tempServerProfile); Settings.SyncSettings(ServerProfileCategory.CustomEngineSettings, tempServerProfile);
} }

View file

@ -25,6 +25,7 @@
<br/> <br/>
<ul> <ul>
<li>Gamedata Files - when adding, deleteing or reloading the gamedata files via the gamedata window, the server manager will reload them and update the settings window.</li> <li>Gamedata Files - when adding, deleteing or reloading the gamedata files via the gamedata window, the server manager will reload them and update the settings window.</li>
<li>Main Window - when changing the location of the server, it will reload the profile with the config settings in the new location.</li>
<li>Main Window - added discord server button.</li> <li>Main Window - added discord server button.</li>
<li>Main Window - re-organised the buttons.</li> <li>Main Window - re-organised the buttons.</li>
<li>pt-BR Translation file updated.</li> <li>pt-BR Translation file updated.</li>

View file

@ -19,6 +19,7 @@
<u style="font-size: .9em;">CHANGE</u> <u style="font-size: .9em;">CHANGE</u>
<br/> <br/>
<ul> <ul>
<li>Main Window - when changing the location of the server, it will reload the profile with the config settings in the new location.</li>
<li>Main Window - added discord server button.</li> <li>Main Window - added discord server button.</li>
<li>Main Window - re-organised the buttons.</li> <li>Main Window - re-organised the buttons.</li>
</ul> </ul>

View file

@ -70,7 +70,7 @@ namespace ServerManagerTool.Lib
public static Server FromPath(string path) public static Server FromPath(string path)
{ {
var loadedProfile = ServerProfile.LoadFrom(path); var loadedProfile = ServerProfile.LoadFrom(path, profile: null);
if (loadedProfile == null) if (loadedProfile == null)
return null; return null;
return new Server(loadedProfile); return new Server(loadedProfile);

View file

@ -213,7 +213,7 @@ namespace ServerManagerTool.Lib
set { SetValue(QueryPortProperty, value); } set { SetValue(QueryPortProperty, value); }
} }
public static readonly DependencyProperty ServerMapProperty = DependencyProperty.Register(nameof(ServerMap), typeof(string), typeof(ServerProfile), new PropertyMetadata("ConanSandbox")); public static readonly DependencyProperty ServerMapProperty = DependencyProperty.Register(nameof(ServerMap), typeof(string), typeof(ServerProfile), new PropertyMetadata(String.Empty));
[DataMember] [DataMember]
public string ServerMap public string ServerMap
{ {
@ -225,7 +225,7 @@ namespace ServerManagerTool.Lib
} }
} }
public static readonly DependencyProperty ServerMapSaveFileNameProperty = DependencyProperty.Register(nameof(ServerMapSaveFileName), typeof(string), typeof(ServerProfile), new PropertyMetadata("game.db")); public static readonly DependencyProperty ServerMapSaveFileNameProperty = DependencyProperty.Register(nameof(ServerMapSaveFileName), typeof(string), typeof(ServerProfile), new PropertyMetadata(String.Empty));
[DataMember] [DataMember]
public string ServerMapSaveFileName public string ServerMapSaveFileName
{ {
@ -710,10 +710,23 @@ namespace ServerManagerTool.Lib
#region Methods #region Methods
#region Common Methods #region Common Methods
public void ChangeInstallationFolder(string folder) public void ChangeInstallationFolder(string folder, bool reloadConfigFiles)
{ {
InstallDirectory = folder; InstallDirectory = folder;
if (reloadConfigFiles)
{
var serverConfigPath = GetProfileServerConfigDir();
if (Directory.Exists(serverConfigPath))
{
var serverConfigFile = Path.Combine(serverConfigPath, Config.Default.ServerGameConfigFile);
if (File.Exists(serverConfigFile))
{
LoadFromConfigFiles(serverConfigFile, this, exclusions: null);
}
}
}
LoadServerFiles(true, true); LoadServerFiles(true, true);
SetupServerFilesWatcher(); SetupServerFilesWatcher();
} }
@ -903,12 +916,12 @@ namespace ServerManagerTool.Lib
return LoadFromProfileFile(file, profile); return LoadFromProfileFile(file, profile);
var filePath = Path.GetDirectoryName(file); var filePath = Path.GetDirectoryName(file);
profile = LoadFromConfigFiles(file, profile); profile = LoadFromConfigFiles(file, profile, exclusions: null);
if (filePath.EndsWith(Config.Default.ServerConfigRelativePath)) if (filePath.EndsWith(Config.Default.ServerConfigRelativePath))
{ {
var installDirectory = filePath.Replace(Config.Default.ServerConfigRelativePath, string.Empty).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); var installDirectory = filePath.Replace(Config.Default.ServerConfigRelativePath, string.Empty).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
profile.ChangeInstallationFolder(installDirectory); profile.ChangeInstallationFolder(installDirectory, reloadConfigFiles: false);
} }
return profile; return profile;
@ -927,6 +940,13 @@ namespace ServerManagerTool.Lib
profile = profile ?? new ServerProfile(); profile = profile ?? new ServerProfile();
iniFile.Deserialize(profile, exclusions); iniFile.Deserialize(profile, exclusions);
if (string.IsNullOrWhiteSpace(profile.ServerModIds) && iniPath.EndsWith(Config.Default.ServerConfigRelativePath))
{
var installDirectory = iniPath.Replace(Config.Default.ServerConfigRelativePath, string.Empty).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var modIds = ModUtils.ReadModListFile(installDirectory);
profile.ServerModIds = string.Join(",", modIds);
}
return profile; return profile;
} }

View file

@ -796,7 +796,7 @@ namespace ServerManagerTool
IsFolderPicker = true, IsFolderPicker = true,
Title = _globalizer.GetResourceString("ServerSettings_InstallServer_Title") Title = _globalizer.GetResourceString("ServerSettings_InstallServer_Title")
}; };
if (!String.IsNullOrWhiteSpace(Settings.InstallDirectory)) if (!string.IsNullOrWhiteSpace(Settings.InstallDirectory))
{ {
dialog.InitialDirectory = Settings.InstallDirectory; dialog.InitialDirectory = Settings.InstallDirectory;
} }
@ -804,7 +804,10 @@ namespace ServerManagerTool
var result = dialog.ShowDialog(Window.GetWindow(this)); var result = dialog.ShowDialog(Window.GetWindow(this));
if (result == CommonFileDialogResult.Ok) if (result == CommonFileDialogResult.Ok)
{ {
Settings.ChangeInstallationFolder(dialog.FileName); Settings.ServerMap = string.Empty;
Settings.ServerMapSaveFileName = string.Empty;
Settings.ChangeInstallationFolder(dialog.FileName, reloadConfigFiles: true);
} }
} }

View file

@ -1,11 +1,11 @@
using ServerManagerTool.Common; using ServerManagerTool.Common;
using ServerManagerTool.Common.Extensions;
using ServerManagerTool.Common.Lib; using ServerManagerTool.Common.Lib;
using ServerManagerTool.Common.Utils; using ServerManagerTool.Common.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
namespace ServerManagerTool.Utils namespace ServerManagerTool.Utils
{ {
@ -170,6 +170,28 @@ namespace ServerManagerTool.Utils
} }
} }
public static List<string> ReadModListFile(string installDirectory)
{
if (string.IsNullOrWhiteSpace(installDirectory) || !Directory.Exists(installDirectory))
return new List<string>();
// get the folder/file details
var modRootFolder = GetModRootPath(installDirectory);
var modListFileName = Config.Default.ServerModListFile;
var modListFile = IOUtils.NormalizePath(Path.Combine(modRootFolder, modListFileName));
// check if the folder/file exists
if (!Directory.Exists(modRootFolder) || !File.Exists(modListFile))
return new List<string>();
var modFiles = File.ReadAllLines(modListFile);
return modFiles
.Select(m => Path.GetFileNameWithoutExtension(m))
.Where(m => m.IsNumeric())
.Where(m => !string.IsNullOrWhiteSpace(m))
.ToList();
}
public static List<string> ValidateModList(List<string> modIdList) public static List<string> ValidateModList(List<string> modIdList)
{ {
// remove all duplicate mod ids. // remove all duplicate mod ids.

View file

@ -20,6 +20,7 @@
<br/> <br/>
<ul> <ul>
<li>Gamedata Files - when adding, deleteing or reloading the gamedata files via the gamedata window, the server manager will reload them and update the settings window.</li> <li>Gamedata Files - when adding, deleteing or reloading the gamedata files via the gamedata window, the server manager will reload them and update the settings window.</li>
<li>Main Window - when changing the location of the server, it will reload the profile with the config settings in the new location.</li>
<li>Main Window - added discord server button.</li> <li>Main Window - added discord server button.</li>
<li>Main Window - re-organised the buttons.</li> <li>Main Window - re-organised the buttons.</li>
</ul> </ul>

View file

@ -19,6 +19,7 @@
<u style="font-size: .9em;">CHANGE</u> <u style="font-size: .9em;">CHANGE</u>
<br/> <br/>
<ul> <ul>
<li>Main Window - when changing the location of the server, it will reload the profile with the config settings in the new location.</li>
<li>Main Window - added discord server button.</li> <li>Main Window - added discord server button.</li>
<li>Main Window - re-organised the buttons.</li> <li>Main Window - re-organised the buttons.</li>
</ul> </ul>

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
namespace ServerManagerTool.Common.Extensions namespace ServerManagerTool.Common.Extensions
{ {
@ -13,5 +14,10 @@ namespace ServerManagerTool.Common.Extensions
return value.IndexOf(substring, comparisonType) >= 0; return value.IndexOf(substring, comparisonType) >= 0;
} }
public static bool IsNumeric(this string value)
{
return value.All(char.IsNumber);
}
} }
} }