-
Notifications
You must be signed in to change notification settings - Fork 25
/
API.cs
100 lines (95 loc) · 3.68 KB
/
API.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GameServer;
namespace SkyCoop
{
public class API
{
public enum SkyCoopClientState
{
NONE = -1,
CLIENT,
HOST,
}
public static SkyCoopClientState m_ClientState = SkyCoopClientState.NONE; // -1 Not playing online, 0 Client, 1 Host.
public static bool m_AllowMods = true; // Using mods allowed.
public static int m_MyClientID = 0; // ID of player on server, (this related to slot what player use).
public static bool m_Debug = false;
public static bool IsP2PServer = false;
public static int Port = 0;
public static void CustomEventCallback(Packet _pak, int from)
{
if (m_Debug)
{
MelonLoader.MelonLogger.Msg(ConsoleColor.Blue, "[SkyCoop API] ");
}
}
//HOST
public static void SendDataToEveryone(Packet _pak, int from, bool ignoreSender)
{
if(m_ClientState == SkyCoopClientState.HOST)
{
_pak.Write(from);
if (ignoreSender)
{
ServerSend.SendUDPDataToAllButNotSender(_pak, from);
}else{
ServerSend.SendUDPDataToAll(_pak);
}
}
else{
if(m_ClientState == SkyCoopClientState.CLIENT)
{
MelonLoader.MelonLogger.Msg(ConsoleColor.Red, "[SkyCoop API] Client trying call SendDataToEveryone, but this host only method.");
}
}
}
public static void SendDataToClient(Packet _pak, int from, int to)
{
if (m_ClientState == SkyCoopClientState.HOST)
{
_pak.Write(from);
ServerSend.SendUDPData(to, _pak);
}else{
if (m_ClientState == SkyCoopClientState.CLIENT)
{
MelonLoader.MelonLogger.Msg(ConsoleColor.Red, "[SkyCoop API] Client trying call SendDataToClient, but this host only method.");
}
}
}
//Client
public static void SendToHost(Packet _pak)
{
if (m_ClientState == SkyCoopClientState.CLIENT)
{
MyMod.SendUDPData(_pak);
}else{
if (m_ClientState == SkyCoopClientState.HOST)
{
MelonLoader.MelonLogger.Msg(ConsoleColor.Red, "[SkyCoop API] Host trying call SendToHost, but this client only method.");
}
}
}
public static void TestCustom()
{
using (Packet _packet = new Packet((int)ServerPackets.CUSTOM))
{
_packet.Write("MODNAME_EVENTNAME"); //Name of event. This to next time you would be able to recognize what event has been received. String type.
_packet.Write("Hello this is test message"); //Data to send. Check out Write method's overloads there almost every basic types of data like bool, int, string, and etc.
if(m_AllowMods)
{
if(m_ClientState == SkyCoopClientState.HOST)
{
SendDataToEveryone(_packet, m_MyClientID, true); //Send data to everyone.
}else if(m_ClientState == SkyCoopClientState.CLIENT)
{
SendToHost(_packet);
}
}
}
}
}
}