You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OMS.NET/Common/ServerConfig.cs

316 lines
9.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Reflection;
using System.Text;
using System.Text.Json;
namespace OMS.NET.Common
{
public sealed class ServerConfig
{
private Dictionary<string, string> _config = new();
/// <summary>
/// 连接数据库的连接字符串 default Empty
/// 默认不带数据库名称
/// </summary>
public string ConnectionString => _config.TryGetValue("ConnectionString", out string? value)
? value
: "";
/// <summary>
/// 数据库名称 default map_db
/// </summary>
public string DataBaseName => _config.TryGetValue("DataBaseName", out string? value)
? value
: "map_db";
/// <summary>
/// 监听端口 default 8080
/// </summary>
public string ListenPort => _config.TryGetValue("ListenPort", out string? value)
? value
: "8080";
/// <summary>
/// 是否开启匿名登录 default false
/// </summary>
public string AnonymousLogin => _config.TryGetValue("AnonymousLogin", out string? value)
? value
: "false";
/// <summary>
/// 地图的背景图片位置图像尺寸为220px * 165px 最大为60kb 支持PNG和jpg类型的图片格式
/// default ./map_img.png
/// </summary>
public string Img => _config.TryGetValue("ServerConfigImg", out string? value)
? value
: "./map_img.png";
/// <summary>
/// key是唯一的,长度不限格式k[a-Z0-9]是客户端采用https://name.com/ + m/key 的方式快捷访问地图服务器的
/// default k0
/// </summary>
public string Key => _config.TryGetValue("ServerConfigKey", out string? value)
? value
: "k0";
/// <summary>
/// 服务器websocket链接的地址
/// default 'ws://0.0.0.0:8080'
/// </summary>
public string Url => _config.TryGetValue("ServerConfigUrl", out string? value)
? value
: "ws://0.0.0.0:8080";
/// <summary>
/// 服务器名称
/// default 地图名称
/// </summary>
public string Name => _config.TryGetValue("ServerConfigName", out string? value)
? value
: "地图名称";
/// <summary>
/// 服务器最大在线人数
/// default 20
/// </summary>
public string MaxUser => _config.TryGetValue("ServerConfigMaxUser", out string? value)
? value
: "20";
/// <summary>
/// 最大高度
/// default 10000
/// </summary>
public string MaxHeight => _config.TryGetValue("ServerConfigMaxHeight", out string? value)
? value
: "10000";
/// <summary>
/// 最小高度
/// default -10000
/// </summary>
public string MinHeight => _config.TryGetValue("ServerConfigMinHeight", out string? value)
? value
: "-10000";
/// <summary>
/// 最大宽度
/// default 10000
/// </summary>
public string MaxWidth => _config.TryGetValue("ServerConfigMaxWidth", out string? value)
? value
: "10000";
/// <summary>
/// 最小宽度
/// default -10000
/// </summary>
public string MinWidth => _config.TryGetValue("ServerConfigMinWidth", out string? value)
? value
: "-10000";
/// <summary>
/// 区域内横轴和纵轴的最小层级下layer0每像素移动量单位 纵轴单位
/// default 1
/// </summary>
public string Unit1Y => _config.TryGetValue("ServerConfigUnit1Y", out string? value)
? value
: "1";
/// <summary>
/// 区域内横轴和纵轴的最小层级下layer0每像素移动量单位 横轴单位
/// default 1
/// </summary>
public string Unit1X => _config.TryGetValue("ServerConfigUnit1X", out string? value)
? value
: "1";
/// <summary>
/// 打开地图时的默认中心点 x
/// default 0
/// </summary>
public string P0X => _config.TryGetValue("ServerConfigP0X", out string? value)
? value
: "0";
/// <summary>
/// 打开地图时的默认中心点 y
/// default 0
/// </summary>
public string P0Y => _config.TryGetValue("ServerConfigP0Y", out string? value)
? value
: "0";
/// <summary>
/// 最大层级
/// default 5
/// </summary>
public string MaxLayer => _config.TryGetValue("ServerConfigMaxLayer", out string? value)
? value
: "default";
/// <summary>
/// 最小层级
/// default 0
/// </summary>
public string MinLayer => _config.TryGetValue("ServerConfigMinLayer", out string? value)
? value
: "default";
/// <summary>
/// 默认层级
/// default 0
/// </summary>
public string DefaultLayer => _config.TryGetValue("ServerConfigDefaultLayer", out string? value)
? value
: "0";
/// <summary>
/// 默认缩放比例
/// default 1
/// </summary>
public string ZoomAdd => _config.TryGetValue("ServerConfigZoomAdd", out string? value)
? value
: "1";
/// <summary>
/// 表示是否启用额外的底图服务
/// default false
/// </summary>
public string EnableBase => _config.TryGetValue("ServerConfigEnableBase", out string? value)
? value
: "false";
/// <summary>
/// 默认x
/// default 0
/// </summary>
public string DefaultX => _config.TryGetValue("ServerConfigDefaultX", out string? value)
? value
: "0";
/// <summary>
/// 默认y
/// default 0
/// </summary>
public string DefaultY => _config.TryGetValue("ServerConfigDefaultY", out string? value)
? value
: "0";
/// <summary>
/// 屏幕默认分辨率x
/// default 1920
/// </summary>
public string ResolutionX => _config.TryGetValue("ServerConfigResolutionX", out string? value)
? value
: "1920";
/// <summary>
/// 屏幕默认分辨率y
/// default 980
/// </summary>
public string ResolutionY => _config.TryGetValue("ServerConfigResolutionY", out string? value)
? value
: "980";
/// <summary>
/// 最大底图缩放等级
/// default 0
/// </summary>
public string MaxZoom => _config.TryGetValue("ServerConfigMaxZoom", out string? value)
? value
: "0";
/// <summary>
/// 最小底图缩放等级
/// default 0
/// </summary>
public string MinZoom => _config.TryGetValue("ServerConfigMinZoom", out string? value)
? value
: "0";
/// <summary>
/// 默认底图缩放等级
/// default 0
/// </summary>
public string DefaultZoom => _config.TryGetValue("ServerConfigDefaultZoom", out string? value)
? value
: "0";
/// <summary>
/// 底图服务的服务器URL
/// default empty
/// </summary>
public string BaseMapUrl => _config.TryGetValue("ServerConfigBaseMapUrl", out string? value)
? value
: "";
/// <summary>
/// 底图的默认类型
/// default empty
/// </summary>
public string ServerConfigBaseMapType => _config.TryGetValue("ServerConfigBaseMapType", out string? value)
? value
: "realistic";
public ServerConfig()
{
//do nothing,everything default
}
public ServerConfig(string configPath)
{
if (!File.Exists(configPath))
{
throw new FileNotFoundException("服务器配置文件未找到, path: " + configPath);
}
foreach (var line in File.ReadLines(configPath))
{
// 忽略空行和注释行
var trimmedLine = line.Trim();
if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith("#"))
{
continue;
}
// 去掉行尾的注释
var commentIndex = trimmedLine.IndexOf('#');
if (commentIndex > -1)
{
trimmedLine = trimmedLine[..commentIndex].Trim();
}
// 查找冒号的位置
var separatorIndex = trimmedLine.IndexOf(':');
if (separatorIndex > -1)
{
// 获取键和值,并去除冒号前后的空格
var key = trimmedLine[..separatorIndex].Trim();
var value = trimmedLine[(separatorIndex + 1)..].Trim();
// 将键值对添加到字典中
_config[key] = value;
}
}
}
public override string ToString()
{
StringBuilder stringBuilder = new();
foreach (var key in _config.Keys)
{
stringBuilder.AppendLine($"{key}:{_config[key]}");
}
//通过属性访问
// stringBuilder.AppendLine("====加载默认值后的配置项====");
// Type type = this.GetType();
// PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// foreach (PropertyInfo property in properties)
// {
// if (property.CanRead && !property.CanWrite)
// {
// object? value = property.GetValue(this);
// stringBuilder.AppendLine($"{property.Name} = {value}");
// }
// }
return stringBuilder.ToString();
}
}
}