ModernUO/Projects/Server/Configuration/ServerConfiguration.cs
Kamron Batman 351611a23a
Updates movement packets & Fastwalk (#324)
- [X] Updates Movement packets
- [X] Adds OSI fastwalk packets. These aren't used on OSi anymore.
- [X] Adds new movement handling, but looks like the client doesn't use it. (Also leaving the 0x4000 Character List Flag off)
- [X] Adds time sync request handler, but also looks like the client doesn't use it.
- [X] Adds time sync response for time sync, just in case, but it isn't used, so not sure about the arguments.
- [X] Reimplements RunUO's fastwalk to use a circular array on the netstate instead of every mobile
- [X] Removes ClearFastwalkStack from RunUO implementation. This shouldn't be needed anymore

Changed fastwalk settings:
```cs
        public static int WalkFootDelay { get; set; } = 440;
        public static int RunFootDelay { get; set; } = 220;
        public static int WalkMountDelay { get; set; } = 220;
        public static int RunMountDelay { get; set; } = 110;

        public static bool EnableFastwalkPrevention { get; set; } = true;
        public static AccessLevel FastwalkExemptionLevel { get; set; } = AccessLevel.Counselor;

        // If this is changed during runtime, then the steps array needs resizing.
        public static int MaxSteps { get; private set; } = 4;
```

modernuo.json
```json
{
  "settings": {
    "movement.delay.runFoot": "220",
    "movement.delay.runMount": "110",
    "movement.delay.walkFoot": "440",
    "movement.delay.walkMount": "220",
    "movement.enableFastWalkPrevention": "True",
    "movement.fastwalkExemptionLevel": "Counselor",
    "movement.maxSteps": "4"
  }
}
```

Notes about OSI fastwalk:
While it does work, I can't find a benefit in using it because of the variable speeds. If players moved at a single speed then we could refill the stack every X milliseconds with 6 keys and use a naive token bucket implementation.
Unfortunately variable speeds mount/run/walk/etc means we would have to use a leaky bucket algorithm.
If we are using a leaky bucket algorithm with a variable leak, then we don't need to send tokens because we are already tracking it on the server side.

ModernUO vs OSI Fastwalk:
When the fastwalk was implemented on OSI, it used up to 6 tokens. These tokens were probably distributed every 600-750 milliseconds. To get the same effect, the new fastwalk settings might need to be adjusted. I would tweak them and feel free to let me know what worked for you!

Bumps release version
2020-11-26 23:53:47 -08:00

315 lines
10 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: ServerConfiguration.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.Json.Serialization;
using Server.Json;
namespace Server
{
public static class ServerConfiguration
{
private const string m_RelPath = "Configuration/modernuo.json";
private static readonly string m_FilePath = Path.Join(Core.BaseDirectory, m_RelPath);
private static ServerSettings m_Settings;
private static bool m_Mocked;
public static List<string> DataDirectories => m_Settings.dataDirectories;
public static List<IPEndPoint> Listeners => m_Settings.listeners;
public static string GetSetting(string key, string defaultValue)
{
m_Settings.settings.TryGetValue(key, out var value);
return value ?? defaultValue;
}
public static int GetSetting(string key, int defaultValue)
{
m_Settings.settings.TryGetValue(key, out var strValue);
return int.TryParse(strValue, out var value) ? value : defaultValue;
}
public static long GetSetting(string key, long defaultValue)
{
m_Settings.settings.TryGetValue(key, out var strValue);
return long.TryParse(strValue, out var value) ? value : defaultValue;
}
public static bool GetSetting(string key, bool defaultValue)
{
m_Settings.settings.TryGetValue(key, out var strValue);
return bool.TryParse(strValue, out var value) ? value : defaultValue;
}
public static T GetSetting<T>(string key, T defaultValue) where T : struct, Enum
{
m_Settings.settings.TryGetValue(key, out var strValue);
return Enum.TryParse(strValue, out T value) ? value : defaultValue;
}
public static string GetOrUpdateSetting(string key, string defaultValue)
{
if (m_Settings.settings.TryGetValue(key, out var value))
{
return value;
}
SetSetting(key, value = defaultValue);
return value;
}
public static int GetOrUpdateSetting(string key, int defaultValue)
{
int value;
if (m_Settings.settings.TryGetValue(key, out var strValue))
{
value = int.TryParse(strValue, out value) ? value : defaultValue;
}
else
{
SetSetting(key, (value = defaultValue).ToString());
}
return value;
}
public static long GetOrUpdateSetting(string key, long defaultValue)
{
long value;
if (m_Settings.settings.TryGetValue(key, out var strValue))
{
value = long.TryParse(strValue, out value) ? value : defaultValue;
}
else
{
SetSetting(key, (value = defaultValue).ToString());
}
return value;
}
public static bool GetOrUpdateSetting(string key, bool defaultValue)
{
bool value;
if (m_Settings.settings.TryGetValue(key, out var strValue))
{
value = bool.TryParse(strValue, out value) ? value : defaultValue;
}
else
{
SetSetting(key, (value = defaultValue).ToString());
}
return value;
}
public static TimeSpan GetOrUpdateSetting(string key, TimeSpan defaultValue)
{
TimeSpan value;
if (m_Settings.settings.TryGetValue(key, out var strValue))
{
value = TimeSpan.TryParse(strValue, out value) ? value : defaultValue;
}
else
{
SetSetting(key, (value = defaultValue).ToString());
}
return value;
}
public static T GetOrUpdateSetting<T>(string key, T defaultValue) where T : struct, Enum
{
T value;
if (m_Settings.settings.TryGetValue(key, out var strValue))
{
value = Enum.TryParse(strValue, out value) ? value : defaultValue;
}
else
{
SetSetting(key, (value = defaultValue).ToString());
}
return value;
}
public static void SetSetting(string key, string value)
{
m_Settings.settings[key] = value;
Save();
}
// If mock is enabled we skip the console readline.
public static void Load(bool mocked = false)
{
m_Mocked = mocked;
var updated = false;
if (File.Exists(m_FilePath))
{
Console.Write($"Core: Reading server configuration from {m_RelPath}...");
m_Settings = JsonConfig.Deserialize<ServerSettings>(m_FilePath);
if (m_Settings == null)
{
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine("failed");
Utility.PopColor();
throw new Exception("Core: Server configuration failed to deserialize.");
}
Console.WriteLine("done");
}
else
{
updated = true;
m_Settings = new ServerSettings();
}
if (mocked)
{
return;
}
if (m_Settings.dataDirectories.Count == 0)
{
updated = true;
Utility.PushColor(ConsoleColor.DarkYellow);
Console.WriteLine("Core: Server configuration is missing data directories.");
Utility.PopColor();
m_Settings.dataDirectories.AddRange(GetDataDirectories());
}
if (m_Settings.listeners.Count == 0)
{
updated = true;
Utility.PushColor(ConsoleColor.DarkYellow);
Console.WriteLine("Core: Server is missing socket listener IP addresses.");
Utility.PopColor();
m_Settings.listeners.AddRange(GetListeners());
}
if (updated)
{
Save();
Utility.PushColor(ConsoleColor.Green);
Console.WriteLine($"Core: Server configuration saved to {m_RelPath}.");
Utility.PopColor();
}
}
private static List<string> GetDataDirectories()
{
Console.WriteLine("Please enter the absolute path to the Ultima Online data:");
var directories = new List<string>();
do
{
Console.Write("{0}> ", directories.Count > 0 ? "[finish] " : " ");
var directory = Console.ReadLine();
if (string.IsNullOrWhiteSpace(directory))
{
break;
}
if (Directory.Exists(directory))
{
directories.Add(directory);
Console.WriteLine("Core: Path {0} added.", directory);
}
else
{
Console.WriteLine("Core: Path does not exist. ({0})");
}
} while (true);
return directories;
}
private static List<IPEndPoint> GetListeners()
{
Console.WriteLine("Please enter the IP and ports to listen:");
Console.WriteLine(" - Only enter IP addresses directly bound to this machine");
Console.WriteLine(" - To listen to all IP addresses enter 0.0.0.0");
var ips = new List<IPEndPoint>();
do
{
// IP:Port?
Console.Write("[{0}]> ", ips.Count > 0 ? "finish" : "0.0.0.0:2593");
var ipStr = Console.ReadLine();
if (string.IsNullOrWhiteSpace(ipStr))
{
break;
}
if (!ipStr.Contains(':', StringComparison.Ordinal))
{
ipStr += ":2593";
}
if (IPEndPoint.TryParse(ipStr, out var ip))
{
ips.Add(ip);
Console.WriteLine("Core: {0} added.", ipStr);
}
else
{
Console.WriteLine("Core: {0} is not a valid IP or port");
}
} while (true);
if (ips.Count == 0)
{
ips.Add(new IPEndPoint(IPAddress.Any, 2593));
}
return ips;
}
public static void Save()
{
if (m_Mocked)
{
return;
}
JsonConfig.Serialize(m_FilePath, m_Settings);
}
internal class ServerSettings
{
[JsonPropertyName("dataDirectories")]
public List<string> dataDirectories { get; set; } = new List<string>();
[JsonPropertyName("listeners")]
public List<IPEndPoint> listeners { get; set; } = new List<IPEndPoint>();
[JsonPropertyName("settings")]
public SortedDictionary<string, string> settings { get; set; } = new SortedDictionary<string, string>();
}
}
}