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 #endif
if (args.Contains("offline")) offline = true; if (args.Contains("offline")) offline = true;
if (args.Contains("updated")) updated = true; if (args.Contains("updated")) updated = true;
Config = !File.Exists(LauncherConfig.ConfigFilePath)
? LauncherConfig.CreateDefault() Config = LauncherConfig.LoadOrCreateDefault();
: LauncherConfig.LoadFromFile();
Logger.DebugLogEnabled = debug; Logger.DebugLogEnabled = debug;
Logger.LogInfo("Main", "launcher is starting"); Logger.LogInfo("Main", "launcher is starting");

View File

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

View File

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

View File

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