Multiplayer Code lol

This commit is contained in:
ApfelTeeSaft
2024-09-23 01:15:24 +02:00
parent 5ea1362f5a
commit 484b0a3820
7 changed files with 354 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
# Ignore Visual Studio temporary files, build results, and
# build logs.
[vb]*.user
[vb]*.suo
[vb]*.userosscache
[vb]*.sln.docstates
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# Mono Auto Generated Files
mono_crash.*
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows shortcuts
*.lnk
# Visual Studio Code settings
.vscode/
# Rider settings
.idea/
*.sln.iml
# DotNet core settings
*.pidb
*.svd
*.suo
*.user
*.userprefs
*.csproj.user
*.csproj.vspscc
*.csproj.vssscc
*.vs/
.vscode/
.idea/
build/
out/
temp/
backup/
# Build directories
bin/
obj/
# Log files
*.log
# NuGet Packages
*.nupkg
*.snupkg
*.nuspec
packages/
# Windows
*.DS_Store
*.env
# JetBrains Rider project-specific files
.idea/
+25
View File
@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35027.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multikill", "Multikill\Multikill.csproj", "{3AA568A4-EF62-4515-BEE0-A313E7B9E1A0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3AA568A4-EF62-4515-BEE0-A313E7B9E1A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AA568A4-EF62-4515-BEE0-A313E7B9E1A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AA568A4-EF62-4515-BEE0-A313E7B9E1A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AA568A4-EF62-4515-BEE0-A313E7B9E1A0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B514BB2B-8C7B-415C-9801-88BC684B8220}
EndGlobalSection
EndGlobal
+51
View File
@@ -0,0 +1,51 @@
using BepInEx;
using UnityEngine;
namespace Multikill
{
[BepInPlugin("com.apfelteesaft.multikillmod", "MultikillMod", "1.0.0")]
public class MultiplayerMod : BaseUnityPlugin
{
private Multikill.ConsoleManager consoleManager;
private Multikill.UIManager uiManager;
private Multikill.NetworkManager networkManager;
void Awake()
{
consoleManager = new Multikill.ConsoleManager();
consoleManager.InitializeConsole();
uiManager = new Multikill.UIManager();
uiManager.SetupUI(ConnectToServer);
networkManager = new Multikill.NetworkManager();
networkManager.InitializeNetwork();
}
void Update()
{
networkManager.PollNetworkEvents();
if (Input.GetKeyDown(KeyCode.F4))
{
consoleManager.ToggleConsole();
}
if (Input.GetKeyDown(KeyCode.F1))
{
uiManager.ToggleUIVisibility();
}
}
private void ConnectToServer(string ip, int port)
{
networkManager.ConnectToServer(ip, port);
}
void OnDestroy()
{
consoleManager.Cleanup();
}
}
}
+32
View File
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LiteNetLib" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>D:\SteamLibrary\steamapps\common\ULTRAKILL\ULTRAKILL_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="BepInEx">
<HintPath>D:\SteamLibrary\steamapps\common\ULTRAKILL\BepInEx\core\BepInEx.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>D:\SteamLibrary\steamapps\common\ULTRAKILL\ULTRAKILL_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>D:\SteamLibrary\steamapps\common\ULTRAKILL\ULTRAKILL_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI">
<HintPath>D:\SteamLibrary\steamapps\common\ULTRAKILL\ULTRAKILL_Data\Managed\UnityEngine.UI.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UIModule">
<HintPath>D:\SteamLibrary\steamapps\common\ULTRAKILL\ULTRAKILL_Data\Managed\UnityEngine.UIModule.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
@@ -0,0 +1,59 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
namespace Multikill.Multikill
{
public class ConsoleManager
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeConsole();
private bool consoleVisible = false;
public void InitializeConsole()
{
if (AllocConsole())
{
var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true };
Console.SetOut(writer);
Console.SetError(writer);
Debug.Log("Console allocated.");
Application.logMessageReceived += HandleLog;
}
}
private void HandleLog(string logString, string stackTrace, LogType type)
{
Console.WriteLine($"{type}: {logString}");
if (type == LogType.Exception)
{
Console.WriteLine(stackTrace);
}
}
public void ToggleConsole()
{
consoleVisible = !consoleVisible;
if (consoleVisible)
{
AllocConsole();
}
else
{
FreeConsole();
}
}
public void Cleanup()
{
Application.logMessageReceived -= HandleLog;
FreeConsole();
}
}
}
@@ -0,0 +1,65 @@
using LiteNetLib;
using LiteNetLib.Utils;
using System.Net;
using System.Net.Sockets;
using UnityEngine;
namespace Multikill.Multikill
{
public class NetworkManager : INetEventListener
{
private NetManager client;
private NetPeer serverPeer;
public void InitializeNetwork()
{
client = new NetManager(this);
client.Start();
}
public void ConnectToServer(string ip, int port)
{
serverPeer = client.Connect(ip, port, "ConnectionKey");
Debug.Log($"Attempting to connect to {ip}:{port}");
}
public void PollNetworkEvents()
{
client.PollEvents();
}
public void OnPeerConnected(NetPeer peer)
{
Debug.Log("Connected to server!");
}
public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
{
Debug.Log($"Disconnected from server: {disconnectInfo.Reason}");
}
public void OnNetworkError(IPEndPoint endPoint, SocketError socketErrorCode)
{
Debug.LogError($"Network error: {socketErrorCode}");
}
public void OnConnectionRequest(ConnectionRequest request)
{
request.Accept();
}
public void OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod)
{
Debug.Log("Received data from server.");
}
public void OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, UnconnectedMessageType messageType)
{
}
public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
{
Debug.Log($"Latency update: {latency} ms");
}
}
}
@@ -0,0 +1,50 @@
using UnityEngine;
using UnityEngine.UI;
namespace Multikill.Multikill
{
public class UIManager
{
private GameObject panel;
private InputField ipInput;
private InputField portInput;
private Button connectButton;
public void SetupUI(System.Action<string, int> onConnectButtonClick)
{
panel = new GameObject("MultiplayerPanel");
panel.AddComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
GameObject ipFieldObj = new GameObject("IPInputField");
ipInput = ipFieldObj.AddComponent<InputField>();
ipInput.placeholder.GetComponent<Text>().text = "Enter Server IP";
ipInput.text = "127.0.0.1";
ipFieldObj.transform.SetParent(panel.transform);
GameObject portFieldObj = new GameObject("PortInputField");
portInput = portFieldObj.AddComponent<InputField>();
portInput.placeholder.GetComponent<Text>().text = "Enter Port";
portInput.text = "9050";
portFieldObj.transform.SetParent(panel.transform);
GameObject buttonObj = new GameObject("ConnectButton");
connectButton = buttonObj.AddComponent<Button>();
connectButton.GetComponentInChildren<Text>().text = "Connect";
buttonObj.transform.SetParent(panel.transform);
connectButton.onClick.AddListener(() =>
{
string ip = ipInput.text;
int port = int.Parse(portInput.text);
onConnectButtonClick(ip, port);
});
panel.SetActive(false);
}
public void ToggleUIVisibility()
{
panel.SetActive(!panel.activeSelf);
}
}
}