ServerConfig class

This commit is contained in:
Timerix22 2024-01-06 21:37:21 +06:00
parent f52b49effd
commit bc868356e7
6 changed files with 81 additions and 31 deletions

View File

@ -40,9 +40,8 @@ internal static partial class Launcher
#endif
if (args.Contains("offline")) offline = true;
if (args.Contains("updated")) updated = true;
Config = !File.Exists(LauncherConfig.ConfigFilePath)
? LauncherConfig.CreateDefault()
: LauncherConfig.LoadFromFile();
Config = LauncherConfig.LoadOrCreateDefault();
Logger.DebugLogEnabled = debug;
Logger.LogInfo("Main", "launcher is starting");

View File

@ -28,7 +28,8 @@ public class LauncherConfig
Username = dtsod["username"];
}
public static LauncherConfig LoadFromFile() => new(new DtsodV23(File.ReadAllText(ConfigFilePath)));
private static LauncherConfig LoadFromFile() =>
new(new DtsodV23(File.ReadAllText(ConfigFilePath)));
public DtsodV23 ToDtsod() =>
new()
@ -42,15 +43,18 @@ public class LauncherConfig
{ "username", Username },
};
public void Save()
{
public void Save() =>
File.WriteAllText(ConfigFilePath, ToDtsod().ToString());
}
public static LauncherConfig CreateDefault()
private static LauncherConfig CreateDefault()
{
var c = new LauncherConfig();
c.Save();
return c;
}
public static LauncherConfig LoadOrCreateDefault() =>
File.Exists(ConfigFilePath)
? LoadFromFile()
: CreateDefault();
}

View File

@ -9,7 +9,7 @@ namespace launcher_server;
public static class Manifests
{
static object manifestLocker = new();
public static object manifestLocker = new();
public static void CreateManifest(IOPath dir)
{

View File

@ -1,13 +1,15 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using DTLib.Dtsod;
using DTLib.Extensions;
using DTLib.Filesystem;
using DTLib.Logging;
using DTLib.Network;
global using System;
global using System.Collections.Generic;
global using System.Net;
global using System.Net.Sockets;
global using System.Text;
global using System.Threading;
global using System.Threading.Tasks;
global using DTLib.Dtsod;
global using DTLib.Extensions;
global using DTLib.Filesystem;
global using DTLib.Logging;
global using DTLib.Network;
namespace launcher_server;
@ -17,7 +19,7 @@ static class Server
new FileLogger("logs","launcher-server"),
new ConsoleLogger());
static readonly Socket mainSocket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static DtsodV23 config = null!;
static ServerConfig Config = null!;
public static readonly IOPath shared_dir = "public";
@ -28,13 +30,13 @@ static class Server
Console.Title = "minecraft_launcher_server";
Console.InputEncoding = Encoding.Unicode;
Console.OutputEncoding = Encoding.Unicode;
Config = ServerConfig.LoadOrCreateDefault();
config = new DtsodV23(File.ReadAllText("minecraft-launcher-server.dtsod"));
logger.LogInfo("Main", $"local address: {config["local_ip"]}");
logger.LogInfo("Main", $"local address: {Config.LocalIp}");
logger.LogInfo("Main", $"public address: {Functions.GetPublicIP()}");
logger.LogInfo("Main", $"port: {config["local_port"]}");
mainSocket.Bind(new IPEndPoint(IPAddress.Parse(config["local_ip"]), config["local_port"]));
logger.LogInfo("Main", $"port: {Config.LocalPort}");
mainSocket.Bind(new IPEndPoint(IPAddress.Parse(Config.LocalIp), Config.LocalPort));
mainSocket.Listen(1000);
Manifests.CreateAllManifests();
logger.LogInfo("Main", "server started succesfully");
@ -85,10 +87,15 @@ static class Server
fsp.UploadFile(Path.Concat(shared_dir, "minecraft-launcher.exe"));
break;
case "requesting file download":
var file = handlerSocket.GetPackage().BytesToString();
logger.LogInfo(nameof(HandleUser), $"updater requested file {file}");
var filePath = handlerSocket.GetPackage().BytesToString();
logger.LogInfo(nameof(HandleUser), $"updater requested file {filePath}");
if(filePath.EndsWith("manifest.dtsod"))
lock (Manifests.manifestLocker)
{
fsp.UploadFile(Path.Concat(shared_dir, filePath));
}
// ReSharper disable once InconsistentlySynchronizedField
fsp.UploadFile(Path.Concat(shared_dir, file));
else fsp.UploadFile(Path.Concat(shared_dir, filePath));
break;
default:
throw new Exception("unknown request: " + request);

View File

@ -0,0 +1,42 @@
namespace launcher_server;
public class ServerConfig
{
public static IOPath ConfigFilePath = "minecraft-launcher.dtsod";
public int LocalPort = 25000;
public string LocalIp = "127.0.0.1";
private ServerConfig(){}
private ServerConfig(DtsodV23 dtsod)
{
LocalIp = dtsod["local_ip"];
LocalPort = dtsod["local_port"];
}
private static ServerConfig LoadFromFile() =>
new(new DtsodV23(File.ReadAllText(ConfigFilePath)));
public DtsodV23 ToDtsod() =>
new()
{
{ "local_ip", LocalIp },
{ "local_port", LocalPort }
};
public void Save() =>
File.WriteAllText(ConfigFilePath, ToDtsod().ToString());
private static ServerConfig CreateDefault()
{
var c = new ServerConfig();
c.Save();
return c;
}
public static ServerConfig LoadOrCreateDefault() =>
File.Exists(ConfigFilePath)
? LoadFromFile()
: CreateDefault();
}

View File

@ -1,2 +0,0 @@
local_ip: "127.0.0.1";
local_port: 25000;