mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Removal of ToArray()
This commit is contained in:
parent
9eb22da9e7
commit
9f5cf132f0
41 changed files with 184 additions and 189 deletions
|
|
@ -1,10 +1,12 @@
|
|||
namespace ServerManagerTool.Common.Interfaces
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ServerManagerTool.Common.Interfaces
|
||||
{
|
||||
public interface IIniSectionCollection
|
||||
{
|
||||
IIniValuesCollection[] Sections { get; }
|
||||
|
||||
void Add(string sectionName, string[] values);
|
||||
void Add(string sectionName, IEnumerable<string> values);
|
||||
|
||||
void Update();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ namespace ServerManagerTool.Common.Lib
|
|||
{
|
||||
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
||||
|
||||
var customAttributes = member.CustomAttributes?.ToArray() ?? new CustomAttributeData[0];
|
||||
var customAttributes = member.CustomAttributes ?? new CustomAttributeData[0];
|
||||
if (customAttributes.Any(a => a.AttributeType == typeof(System.Configuration.UserScopedSettingAttribute)))
|
||||
{
|
||||
property.ShouldSerialize = instance => { return property.PropertyType.IsValueType || property.PropertyType == typeof(string); };
|
||||
|
|
|
|||
|
|
@ -173,13 +173,13 @@ namespace ServerManagerTool.Common.Model
|
|||
|
||||
if (property.GetValue(this) is IIniValuesCollection collection)
|
||||
{
|
||||
var values = SplitCollectionValues(kvPropertyValue, DELIMITER);
|
||||
values = values.Where(v => !string.IsNullOrWhiteSpace(v)).ToArray();
|
||||
var values = SplitCollectionValues(kvPropertyValue, DELIMITER)
|
||||
.Where(v => !string.IsNullOrWhiteSpace(v));
|
||||
|
||||
if (attr?.ListValueWithinBrackets ?? false)
|
||||
{
|
||||
values = values.Select(v => v.Substring(1)).ToArray();
|
||||
values = values.Select(v => v.Substring(0, v.Length - 1)).ToArray();
|
||||
values = values.Select(v => v.Substring(1));
|
||||
values = values.Select(v => v.Substring(0, v.Length - 1));
|
||||
}
|
||||
collection.FromIniValues(values);
|
||||
}
|
||||
|
|
@ -266,7 +266,7 @@ namespace ServerManagerTool.Common.Model
|
|||
return result.ToString();
|
||||
}
|
||||
|
||||
protected string[] SplitCollectionValues(string valueString, char delimiter)
|
||||
protected IEnumerable<string> SplitCollectionValues(string valueString, char delimiter)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(valueString))
|
||||
return new string[0];
|
||||
|
|
@ -306,7 +306,7 @@ namespace ServerManagerTool.Common.Model
|
|||
|
||||
result.Add(tempString.Substring(startIndex));
|
||||
|
||||
return result.ToArray();
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Update(AggregateIniValue other)
|
||||
|
|
|
|||
|
|
@ -59,17 +59,16 @@ namespace ServerManagerTool.Common.Model
|
|||
|
||||
public virtual void FromIniValues(IEnumerable<string> iniValues)
|
||||
{
|
||||
var items = iniValues?.Select(AggregateIniValue.FromINIValue<T>).ToArray();
|
||||
var items = iniValues?.Select(AggregateIniValue.FromINIValue<T>);
|
||||
|
||||
Clear();
|
||||
AddRange(items);
|
||||
IsEnabled = (Count != 0);
|
||||
IsEnabled = (Count > 0);
|
||||
|
||||
// Add any default values which were missing
|
||||
if (_resetFunc != null)
|
||||
{
|
||||
var defaultItemsToAdd = _resetFunc().Where(r => !this.Any(v => v.IsEquivalent(r))).ToArray();
|
||||
AddRange(defaultItemsToAdd);
|
||||
AddRange(_resetFunc().Where(r => !this.Any(v => v.IsEquivalent(r))));
|
||||
}
|
||||
|
||||
Sort(AggregateIniValue.SortKeySelector);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using ServerManagerTool.Common.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace ServerManagerTool.Common.Model
|
||||
|
|
@ -14,12 +15,12 @@ namespace ServerManagerTool.Common.Model
|
|||
}
|
||||
}
|
||||
|
||||
public void Add(string sectionName, string[] values)
|
||||
public void Add(string sectionName, IEnumerable<string> values)
|
||||
{
|
||||
Add(sectionName, values, true);
|
||||
}
|
||||
|
||||
public void Add(string sectionName, string[] values, bool clearExisting)
|
||||
public void Add(string sectionName, IEnumerable<string> values, bool clearExisting)
|
||||
{
|
||||
var section = this.Items.FirstOrDefault(s => s.SectionName.Equals(sectionName, StringComparison.OrdinalIgnoreCase) && !s.IsDeleted);
|
||||
if (section == null)
|
||||
|
|
|
|||
|
|
@ -98,8 +98,7 @@ namespace ServerManagerTool.Common.Model
|
|||
// Add any default values which were missing
|
||||
if (this.ResetFunc != null)
|
||||
{
|
||||
var defaultItemsToAdd = this.ResetFunc().Where(r => !this.Any(v => this.EquivalencyFunc(v, r))).ToArray();
|
||||
this.AddRange(defaultItemsToAdd);
|
||||
this.AddRange(this.ResetFunc().Where(r => !this.Any(v => this.EquivalencyFunc(v, r))));
|
||||
this.Sort(this.SortKeySelectorFunc);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
|
|
@ -68,16 +69,15 @@ namespace ServerManagerTool.Common.Model
|
|||
|
||||
public void Remove(string steamId)
|
||||
{
|
||||
var items = this.Where(i => i.PlayerId.Equals(steamId, System.StringComparison.OrdinalIgnoreCase)).ToArray();
|
||||
foreach (var item in items)
|
||||
foreach (var item in this.Where(i => i.PlayerId.Equals(steamId, System.StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
this.Remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
public string[] ToArray()
|
||||
public IEnumerable<string> ToEnumerable()
|
||||
{
|
||||
return this.Select(i => i.PlayerId).ToArray();
|
||||
return this.Select(i => i.PlayerId);
|
||||
}
|
||||
|
||||
public string ToDelimitedString(string delimiter)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ namespace ServerManagerTool.Common.Model
|
|||
var tableValue = value as VdfTable;
|
||||
if (tableValue != null && tableValue.Count > 0)
|
||||
{
|
||||
var betaKeyItems = tableValue.Where(v => v.Name.Equals("betakey", StringComparison.OrdinalIgnoreCase)).ToArray();
|
||||
var betaKeyItems = tableValue.Where(v => v.Name.Equals("betakey", StringComparison.OrdinalIgnoreCase));
|
||||
foreach (var item in betaKeyItems)
|
||||
{
|
||||
tableValue.Remove(item);
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace ServerManagerTool.Common.Serialization
|
|||
get;
|
||||
}
|
||||
|
||||
public void Deserialize(object obj, Enum[] exclusions)
|
||||
public void Deserialize(object obj, IEnumerable<Enum> exclusions)
|
||||
{
|
||||
var iniFiles = new Dictionary<string, IniFile>();
|
||||
var fields = obj.GetType()
|
||||
|
|
@ -168,7 +168,7 @@ namespace ServerManagerTool.Common.Serialization
|
|||
}
|
||||
}
|
||||
|
||||
public void Serialize(object obj, Enum[] exclusions)
|
||||
public void Serialize(object obj, IEnumerable<Enum> exclusions)
|
||||
{
|
||||
var iniFiles = new Dictionary<string, IniFile>();
|
||||
var fields = obj.GetType()
|
||||
|
|
@ -205,7 +205,7 @@ namespace ServerManagerTool.Common.Serialization
|
|||
|
||||
if (section.IsEnabled)
|
||||
{
|
||||
WriteSection(iniFiles, attr.File, section.IniCollectionKey, section.ToIniValues().ToArray());
|
||||
WriteSection(iniFiles, attr.File, section.IniCollectionKey, section.ToIniValues());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -228,8 +228,7 @@ namespace ServerManagerTool.Common.Serialization
|
|||
{
|
||||
var section = ReadSection(iniFiles, attr.File, attr.Section);
|
||||
var filteredSection = section
|
||||
.Where(s => !s.StartsWith(collection.IniCollectionKey + (collection.IsArray ? "[" : "=")))
|
||||
.ToArray();
|
||||
.Where(s => !s.StartsWith(collection.IniCollectionKey + (collection.IsArray ? "[" : "=")));
|
||||
WriteSection(iniFiles, attr.File, attr.Section, filteredSection);
|
||||
}
|
||||
|
||||
|
|
@ -314,7 +313,7 @@ namespace ServerManagerTool.Common.Serialization
|
|||
result = result.Concat(collection.ToIniValues());
|
||||
}
|
||||
|
||||
WriteSection(iniFiles, attr.File, attr.Section, result?.ToArray());
|
||||
WriteSection(iniFiles, attr.File, attr.Section, result);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -381,29 +380,29 @@ namespace ServerManagerTool.Common.Serialization
|
|||
SaveFiles(iniFiles);
|
||||
}
|
||||
|
||||
public string[] ReadSection(Enum iniFile, Enum section)
|
||||
public IEnumerable<string> ReadSection(Enum iniFile, Enum section)
|
||||
{
|
||||
return ReadSection(iniFile, SectionNames[section]);
|
||||
}
|
||||
|
||||
public string[] ReadSection(Enum iniFile, string sectionName)
|
||||
public IEnumerable<string> ReadSection(Enum iniFile, string sectionName)
|
||||
{
|
||||
var file = Path.Combine(this.BasePath, FileNames[iniFile]);
|
||||
return IniFileUtils.ReadSection(file, sectionName);
|
||||
}
|
||||
|
||||
public void WriteSection(Enum iniFile, Enum section, string[] values)
|
||||
public void WriteSection(Enum iniFile, Enum section, IEnumerable<string> values)
|
||||
{
|
||||
WriteSection(iniFile, SectionNames[section], values);
|
||||
}
|
||||
|
||||
public void WriteSection(Enum iniFile, string sectionName, string[] values)
|
||||
public void WriteSection(Enum iniFile, string sectionName, IEnumerable<string> values)
|
||||
{
|
||||
var file = Path.Combine(this.BasePath, FileNames[iniFile]);
|
||||
IniFileUtils.WriteSection(file, sectionName, values);
|
||||
}
|
||||
|
||||
private string[] ReadCustomSectionNames(Dictionary<string, IniFile> iniFiles, Enum iniFile)
|
||||
private IEnumerable<string> ReadCustomSectionNames(Dictionary<string, IniFile> iniFiles, Enum iniFile)
|
||||
{
|
||||
if (!iniFiles.ContainsKey(FileNames[iniFile]))
|
||||
{
|
||||
|
|
@ -415,15 +414,15 @@ namespace ServerManagerTool.Common.Serialization
|
|||
}
|
||||
}
|
||||
|
||||
return iniFiles[FileNames[iniFile]].Sections.Select(s => s.SectionName).Where(s => !SectionNames.ContainsValue(s)).ToArray();
|
||||
return iniFiles[FileNames[iniFile]].Sections.Select(s => s.SectionName).Where(s => !SectionNames.ContainsValue(s));
|
||||
}
|
||||
|
||||
private string[] ReadSection(Dictionary<string, IniFile> iniFiles, Enum iniFile, Enum section)
|
||||
private IEnumerable<string> ReadSection(Dictionary<string, IniFile> iniFiles, Enum iniFile, Enum section)
|
||||
{
|
||||
return ReadSection(iniFiles, iniFile, SectionNames[section]);
|
||||
}
|
||||
|
||||
private string[] ReadSection(Dictionary<string, IniFile> iniFiles, Enum iniFile, string sectionName)
|
||||
private IEnumerable<string> ReadSection(Dictionary<string, IniFile> iniFiles, Enum iniFile, string sectionName)
|
||||
{
|
||||
if (!iniFiles.ContainsKey(FileNames[iniFile]))
|
||||
{
|
||||
|
|
@ -435,7 +434,7 @@ namespace ServerManagerTool.Common.Serialization
|
|||
}
|
||||
}
|
||||
|
||||
return iniFiles[FileNames[iniFile]].GetSection(sectionName)?.KeysToStringArray() ?? new string[0];
|
||||
return iniFiles[FileNames[iniFile]].GetSection(sectionName)?.KeysToStringEnumerable() ?? new string[0];
|
||||
}
|
||||
|
||||
private string ReadValue(Dictionary<string, IniFile> iniFiles, Enum iniFile, Enum section, string keyName)
|
||||
|
|
@ -453,12 +452,12 @@ namespace ServerManagerTool.Common.Serialization
|
|||
return iniFiles[FileNames[iniFile]].GetKey(SectionNames[section], keyName)?.KeyValue ?? string.Empty;
|
||||
}
|
||||
|
||||
private void WriteSection(Dictionary<string, IniFile> iniFiles, Enum iniFile, Enum section, string[] values)
|
||||
private void WriteSection(Dictionary<string, IniFile> iniFiles, Enum iniFile, Enum section, IEnumerable<string> values)
|
||||
{
|
||||
WriteSection(iniFiles, iniFile, SectionNames[section], values);
|
||||
}
|
||||
|
||||
private void WriteSection(Dictionary<string, IniFile> iniFiles, Enum iniFile, string sectionName, string[] values)
|
||||
private void WriteSection(Dictionary<string, IniFile> iniFiles, Enum iniFile, string sectionName, IEnumerable<string> values)
|
||||
{
|
||||
if (!iniFiles.ContainsKey(FileNames[iniFile]))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ namespace ServerManagerTool.Common.Serialization
|
|||
Sections.Remove(section);
|
||||
}
|
||||
|
||||
public bool WriteSection(string sectionName, string[] keysValuePairs)
|
||||
public bool WriteSection(string sectionName, IEnumerable<string> keysValuePairs)
|
||||
{
|
||||
if (sectionName == null)
|
||||
return false;
|
||||
|
|
@ -186,7 +186,7 @@ namespace ServerManagerTool.Common.Serialization
|
|||
{
|
||||
result.AppendLine($"[{section.SectionName}]");
|
||||
|
||||
foreach (var keyString in section.KeysToStringArray())
|
||||
foreach (var keyString in section.KeysToStringEnumerable())
|
||||
{
|
||||
result.AppendLine(keyString);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,9 +41,9 @@ namespace ServerManagerTool.Common.Serialization
|
|||
return Keys?.FirstOrDefault(s => s.KeyName.Equals(keyName, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
public string[] KeysToStringArray()
|
||||
public IEnumerable<string> KeysToStringEnumerable()
|
||||
{
|
||||
return Keys.Select(k => k.ToString()).ToArray();
|
||||
return Keys.Select(k => k.ToString());
|
||||
}
|
||||
|
||||
public void RemoveKey(string keyName)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using ServerManagerTool.Common.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
|
|
@ -13,13 +14,13 @@ namespace ServerManagerTool.Common.Utils
|
|||
/// <param name="file">The name of the initialization file.</param>
|
||||
/// <param name="sectionName">The name of the section in the initialization file.</param>
|
||||
/// <returns>A string array containing the key name and value pairs associated with the named section.</returns>
|
||||
public static string[] ReadSection(string file, string sectionName)
|
||||
public static IEnumerable<string> ReadSection(string file, string sectionName)
|
||||
{
|
||||
if (sectionName == null)
|
||||
return new string[0];
|
||||
|
||||
var iniFile = ReadFromFile(file);
|
||||
return iniFile?.GetSection(sectionName)?.KeysToStringArray() ?? new string[0];
|
||||
return iniFile?.GetSection(sectionName)?.KeysToStringEnumerable() ?? new string[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -46,7 +47,7 @@ namespace ServerManagerTool.Common.Utils
|
|||
/// <param name="sectionName">The name of the section in which data is written.</param>
|
||||
/// <param name="keysValuePairs">An array of key names and associated values that are to be written to the named section.</param>
|
||||
/// <returns>True if the function succeeds; otherwise False.</returns>
|
||||
public static bool WriteSection(string file, string sectionName, string[] keysValuePairs)
|
||||
public static bool WriteSection(string file, string sectionName, IEnumerable<string> keysValuePairs)
|
||||
{
|
||||
if (sectionName == null)
|
||||
return false;
|
||||
|
|
@ -165,7 +166,7 @@ namespace ServerManagerTool.Common.Utils
|
|||
{
|
||||
writer.WriteLine($"[{section.SectionName}]");
|
||||
|
||||
foreach (var keyString in section.KeysToStringArray())
|
||||
foreach (var keyString in section.KeysToStringEnumerable())
|
||||
{
|
||||
writer.WriteLine(keyString);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -285,7 +285,7 @@ namespace ServerManagerTool.Common.Utils
|
|||
}
|
||||
}
|
||||
|
||||
public static BigInteger[] GetProcessorAffinityList()
|
||||
public static IEnumerable<BigInteger> GetProcessorAffinityList()
|
||||
{
|
||||
var processorCount = ProcessorCount;
|
||||
var results = new List<BigInteger>(processorCount + 1);
|
||||
|
|
@ -295,7 +295,7 @@ namespace ServerManagerTool.Common.Utils
|
|||
{
|
||||
results.Add((BigInteger)Math.Pow(2, index));
|
||||
}
|
||||
return results.ToArray();
|
||||
return results;
|
||||
}
|
||||
|
||||
public static string[] GetProcessPriorityList()
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ namespace ServerManagerTool.Common.Utils
|
|||
|
||||
try
|
||||
{
|
||||
var filesToDelete = new DirectoryInfo(backupPath).GetFiles($"{settingsFileName}_*{settingsFileExt}").Where(f => f.LastWriteTimeUtc.AddDays(7) < DateTime.UtcNow).ToArray();
|
||||
var filesToDelete = new DirectoryInfo(backupPath).GetFiles($"{settingsFileName}_*{settingsFileExt}").Where(f => f.LastWriteTimeUtc.AddDays(7) < DateTime.UtcNow);
|
||||
foreach (var fileToDelete in filesToDelete)
|
||||
{
|
||||
try
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Ionic.Zip;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
|
|
@ -68,11 +69,11 @@ namespace ServerManagerTool.Common.Utils
|
|||
}
|
||||
}
|
||||
|
||||
public static void UpdateFiles(string zipFile, string[] filesToZip, string comment = "", bool preserveDirHierarchy = true, string directoryPathInArchive = "")
|
||||
public static void UpdateFiles(string zipFile, IEnumerable<string> filesToZip, string comment = "", bool preserveDirHierarchy = true, string directoryPathInArchive = "")
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(zipFile))
|
||||
throw new ArgumentNullException(nameof(zipFile));
|
||||
if (filesToZip == null || filesToZip.Length == 0)
|
||||
if (!filesToZip.Any())
|
||||
return;
|
||||
|
||||
if (!File.Exists(zipFile))
|
||||
|
|
@ -160,11 +161,11 @@ namespace ServerManagerTool.Common.Utils
|
|||
}
|
||||
}
|
||||
|
||||
public static void ZipFiles(string zipFile, string[] filesToZip, string comment = "", bool preserveDirHierarchy = true, string directoryPathInArchive = "")
|
||||
public static void ZipFiles(string zipFile, IEnumerable<string> filesToZip, string comment = "", bool preserveDirHierarchy = true, string directoryPathInArchive = "")
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(zipFile))
|
||||
throw new ArgumentNullException(nameof(zipFile));
|
||||
if (filesToZip == null || filesToZip.Length == 0)
|
||||
if (!filesToZip.Any())
|
||||
throw new ArgumentNullException(nameof(filesToZip));
|
||||
|
||||
using (var zip = new ZipFile(zipFile))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue