Initiate server (+Basic connection)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<appSettings>
|
||||
<add key="SERVER_IP" value="localhost"/>
|
||||
<add key="PORT" value="25565"/>
|
||||
<add key="PSWD" value="password"/>
|
||||
</appSettings>
|
||||
</configuration>
|
||||
+32
-1
@@ -1,10 +1,19 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using LiteNetLib;
|
||||
using LiteNetLib.Utils;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace client
|
||||
{
|
||||
public class Game1 : Game
|
||||
{
|
||||
// Network
|
||||
EventBasedNetListener listener = new();
|
||||
NetManager client;
|
||||
NetPeer server;
|
||||
|
||||
// Game instances
|
||||
private Player pl1;
|
||||
private Player pl2;
|
||||
@@ -23,10 +32,32 @@ namespace client
|
||||
_graphics = new GraphicsDeviceManager(this);
|
||||
Content.RootDirectory = "Content";
|
||||
IsMouseVisible = true;
|
||||
|
||||
Dictionary<string, string> settings = Lib.ReadAllSettings();
|
||||
if (!settings.ContainsKey("SERVER_IP") || (settings.ContainsKey("SERVER_IP") && Lib.ReadSetting("SERVER_IP") == null))
|
||||
{
|
||||
Console.Error.WriteLine("Server IP missing in config file!");
|
||||
} else if (!settings.ContainsKey("PORT") || (settings.ContainsKey("PORT") && Lib.ReadSetting("PORT") == null))
|
||||
{
|
||||
Console.Error.WriteLine("Port missing in config file!");
|
||||
} else if (!settings.ContainsKey("PSWD") || (settings.ContainsKey("PSWD") && Lib.ReadSetting("PSWD") == null))
|
||||
{
|
||||
Console.Error.WriteLine("Server password missing in config file!");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
// Network
|
||||
client = new NetManager(listener);
|
||||
client.Start();
|
||||
server = client.Connect(Lib.ReadSetting("SERVER_IP"), int.Parse(Lib.ReadSetting("PORT")), Lib.ReadSetting("PSWD"));
|
||||
if (server == null)
|
||||
{
|
||||
// TODO: Replace by a error message :)
|
||||
Exit();
|
||||
}
|
||||
|
||||
// Window
|
||||
Window.Title = "Pong";
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace client
|
||||
{
|
||||
public class Lib
|
||||
{
|
||||
public static Dictionary<string, string> ReadAllSettings()
|
||||
{
|
||||
Dictionary<string, string> settings = new();
|
||||
try
|
||||
{
|
||||
var appSettings = ConfigurationManager.AppSettings;
|
||||
|
||||
if (appSettings.Count == 0)
|
||||
{
|
||||
Console.Error.WriteLine("AppSettings is empty.");
|
||||
return settings;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var key in appSettings.AllKeys)
|
||||
{
|
||||
settings.Add(key, appSettings[key]);
|
||||
}
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
catch (ConfigurationErrorsException)
|
||||
{
|
||||
Console.Error.WriteLine("Error reading app settings");
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
|
||||
public static string? ReadSetting(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
var appSettings = ConfigurationManager.AppSettings;
|
||||
string? result = appSettings[key] ?? null;
|
||||
return result;
|
||||
}
|
||||
catch (ConfigurationErrorsException)
|
||||
{
|
||||
Console.Error.WriteLine("Error reading app settings");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddUpdateAppSettings(string key, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||||
var settings = configFile.AppSettings.Settings;
|
||||
if (settings[key] == null)
|
||||
{
|
||||
settings.Add(key, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
settings[key].Value = value;
|
||||
}
|
||||
configFile.Save(ConfigurationSaveMode.Modified);
|
||||
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
|
||||
}
|
||||
catch (ConfigurationErrorsException)
|
||||
{
|
||||
Console.Error.WriteLine("Error writing app settings");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
<EmbeddedResource Include="Icon.bmp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LiteNetLib" Version="0.9.5.2" />
|
||||
<PackageReference Include="MonoGame.Extended" Version="3.8.0" />
|
||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
|
||||
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
|
||||
|
||||
Reference in New Issue
Block a user