-
Notifications
You must be signed in to change notification settings - Fork 9
/
RPC.cs
138 lines (133 loc) · 4.67 KB
/
RPC.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Collections.Generic;
using ValheimPermissions;
using System;
using System.Text.RegularExpressions;
namespace WorldofValheimServerSideCharacters
{
public static class RPC
{
public static void CharacterData(ZRpc rpc, ZPackage data)
{
Debug.Log("Server->Client CharacterData");
Debug.Assert(!ZNet.instance.IsServer());
Debug.Assert(ServerState.ClientLoadingData == null);
ServerState.ClientLoadingData = data;
if (!ZNet.instance.IsServer())
{
Debug.Log("Connection to server established! Changing ConnectionCount!");
ServerState.ConnectionCount = 1;
Debug.Log("Clearing all RPC connections");
ServerState.Connections = new List<ServerState.ConnectionData>();
}
Debug.Log("Establishing RPC Connection");
ServerState.Connections.Add(new ServerState.ConnectionData
{
rpc = rpc
});
}
public static void SaveAll(long sender, ZPackage pkg)
{
ZNetPeer peer = ZNet.instance.GetPeer(sender);
if (peer != null)
{
string permissionnode = "HackShardGaming.WoV-SSC.SaveAll";
string peerSteamID = ((ZSteamSocket)peer.m_socket).GetPeerID().m_SteamID.ToString(); // Get the SteamID from peer.
bool PlayerPermission = ValheimPermissions.ValheimDB.CheckUserPermission(peerSteamID, permissionnode);
if (PlayerPermission)
{
Util.SaveAll();
}
else
{
Util.RoutedBroadcast(sender, $"Sorry! You do not have the permission to use !SaveAll (Required Permission: {permissionnode})");
}
}
}
public static void ShutdownServer(long sender, ZPackage pkg)
{
ZNetPeer peer = ZNet.instance.GetPeer(sender);
if (peer != null)
{
string permissionnode = "HackShardGaming.WoV-SSC.ShutdownServer";
string peerSteamID = ((ZSteamSocket)peer.m_socket).GetPeerID().m_SteamID.ToString(); // Get the SteamID from peer.
bool PlayerPermission = ValheimPermissions.ValheimDB.CheckUserPermission(peerSteamID, permissionnode);
if (PlayerPermission)
{
Game.instance.StartCoroutine(Util.ShutdownServer());
}
else
{
Util.RoutedBroadcast(sender, $"Sorry! You do not have the permission to use !ShutdownServer (Required Permission: {permissionnode})");
}
}
}
public static void ReloadDefault(long sender, ZPackage pkg)
{
ZNetPeer peer = ZNet.instance.GetPeer(sender);
if (peer != null)
{
string permissionnode = "HackShardGaming.WoV-SSC.ReloadDefault";
string peerSteamID = ((ZSteamSocket)peer.m_socket).GetPeerID().m_SteamID.ToString(); // Get the SteamID from peer.
bool PlayerPermission = ValheimPermissions.ValheimDB.CheckUserPermission(peerSteamID, permissionnode);
if (PlayerPermission)
{
Util.LoadOrMakeDefaultCharacter();
}
else
{
Util.RoutedBroadcast(sender, $"Sorry! You do not have the permission to use !ReloadDefault (Required Permission: {permissionnode})");
}
}
}
public static void CharacterUpdate(ZRpc rpc, ZPackage data)
{
// Are we the server? Update the character.
if (ZNet.instance.IsServer())
{
Debug.Log("Client->Server CharacterUpdate");
string hostName = rpc.GetSocket().GetHostName();
ZNetPeer peer = ZNet.instance.GetPeerByHostName(hostName);
string PlayerNameRaw = peer.m_playerName;
string PlayerName = "";
if (WorldofValheimServerSideCharacters.AllowMultipleCharacters.Value)
PlayerName = Regex.Replace(PlayerNameRaw, @"<[^>]*>", String.Empty);
else
PlayerName = "Single_Character_Mode";
string CharacterLocation = Util.GetCharacterPath(hostName, PlayerName);
Debug.Log($"Saving character from SteamID {hostName}.");
Util.WriteCharacter(CharacterLocation, Util.Decompress(data).GetArray());
return;
}
// Are we the client? Send our character.
Debug.Log("Server->Client CharacterUpdate");
if (Player.m_localPlayer != null)
{
rpc.Invoke("CharacterUpdate", new object[]
{
Util.Compress(Game.instance.GetPlayerProfile().Serialize(Player.m_localPlayer, true))
});
return;
}
}
public static void ExitServer(ZRpc rpc, ZPackage data)
{
if (ZNet.instance.IsServer())
{
Debug.Log("Client->Server ExitServer");
RPC.CharacterUpdate(rpc, data);
rpc.Invoke("ExitServer", new object[]
{
new ZPackage()
});
Debug.Log($"Removing Client {rpc.GetSocket().GetHostName()} from our list");
ServerState.Connections.RemoveAll((ServerState.ConnectionData conn) => conn.rpc.GetSocket() == rpc.GetSocket());
Debug.Log("Connections " + ServerState.Connections.Count.ToString());
return;
}
Debug.Log("Server->Client ExitServer");
ServerState.Connections.RemoveAll((ServerState.ConnectionData conn) => conn.rpc.GetSocket() == rpc.GetSocket());
ServerState.ClientCanDC = true;
}
}
}