public static string GetPeerSteamID(string ID) { string Updated = ID.Replace("/", " "); List <ZNet.PlayerInfo> OnlinePlayers = ZNet.instance.GetPlayerList(); bool PlayerExists = false; string CurrentPlayer = string.Empty; for (int i = 0; i < OnlinePlayers.Count; i++) { CurrentPlayer = OnlinePlayers[i].m_name; if (CurrentPlayer == Updated) { PlayerExists = true; } } if (PlayerExists) { ZNetPeer peer = ZNet.instance.GetPeerByPlayerName(Updated); string peerSteamID = ((ZSteamSocket)peer.m_socket).GetPeerID().m_SteamID.ToString(); return(peerSteamID); } else { if ((ID.ToString().Length == 17) && IsAllDigits(ID.ToString())) { ZNetPeer peer = ZNet.instance.GetPeer(long.Parse(ID)); string peerSteamID = ((ZSteamSocket)peer.m_socket).GetPeerID().m_SteamID.ToString(); return(peerSteamID); } } return("00000000000000000"); }
private bool Kick(string[] args) { if (UserSupplied(args)) { string user = rebuildString(args); ZNet zNet = ZNet.instance; ZNetPeer znetPeer = zNet.GetPeerByHostName(user); if (znetPeer == null) { znetPeer = zNet.GetPeerByPlayerName(user); } if (znetPeer != null) { print("Kicking " + znetPeer.m_playerName); znetPeer.m_rpc.Invoke("Disconnect", Array.Empty <object>()); zNet.Disconnect(znetPeer); return(true); } else { return(false); } } return(false); }
// Token: 0x060007C2 RID: 1986 RVA: 0x0003D0E4 File Offset: 0x0003B2E4 public void AddPeer(ZNetPeer netPeer) { ZDOMan.ZDOPeer zdopeer = new ZDOMan.ZDOPeer(); zdopeer.m_peer = netPeer; this.m_peers.Add(zdopeer); zdopeer.m_peer.m_rpc.Register <ZPackage>("ZDOData", new Action <ZRpc, ZPackage>(this.RPC_ZDOData)); }
// ReSharper disable once InconsistentNaming private static void Postfix(ZNetPeer peer, ZNet __instance) { if (__instance.IsServer()) { Utility.Log("Registered Server Events"); if (Store.IsSharingMap()) { peer.m_rpc.Register <int, int>("OnClientExplore", new Action <ZRpc, int, int>(ExplorationMapSync.OnClientExplore)); peer.m_rpc.Register <ZPackage>("OnClientInitialData", new Action <ZRpc, ZPackage>(InitialMapSync.OnClientInitialData)); } peer.m_rpc.Register <ZPackage>("OnClientInitialDataPin", new Action <ZRpc, ZPackage>(InitialPinSync.OnClientInitialDataPin)); peer.m_rpc.Register <ZPackage>("OnClientAddPin", new Action <ZRpc, ZPackage>(PinSync.OnClientAddPin)); peer.m_rpc.Register <ZPackage>("OnClientRemovePin", new Action <ZRpc, ZPackage>(PinSync.OnClientRemovePin)); peer.m_rpc.Register <ZPackage>("OnClientCheckPin", new Action <ZRpc, ZPackage>(PinSync.OnClientCheckPin)); } else { Utility.Log("Registered Client Events"); if (Store.IsSharingMap()) { peer.m_rpc.Register <ZPackage>("OnReceiveMapData", new Action <ZRpc, ZPackage>(ExplorationDatabase.OnReceiveMapData)); peer.m_rpc.Register <ZPackage>("OnReceiveMapDataInitial", new Action <ZRpc, ZPackage>(InitialMapSync.OnReceiveMapDataInitial)); } peer.m_rpc.Register <ZPackage>("OnReceiveInitialDataPin", new Action <ZRpc, ZPackage>(InitialPinSync.OnReceiveInitialDataPin)); peer.m_rpc.Register <ZPackage>("OnServerAddPin", new Action <ZRpc, ZPackage>(PinSync.OnServerAddPin)); peer.m_rpc.Register <ZPackage>("OnServerRemovePin", new Action <ZRpc, ZPackage>(PinSync.OnServerRemovePin)); peer.m_rpc.Register <ZPackage>("OnServerCheckPin", new Action <ZRpc, ZPackage>(PinSync.OnServerCheckPin)); } }
public static void RPC_RequestSync(long sender, ZPackage pkg) { ZNetPeer peer = ZNet.instance.GetPeer(sender); // Get the Peer from the sender if (peer != null) { // Confirm the peer exists ZPackage timePkg = new ZPackage(); String tod_str = EnvMan.instance.m_debugTime.ToString(); timePkg.Write(tod_str); ZPackage weatherPkg = new ZPackage(); String env_str = EnvMan.instance.m_debugEnv; weatherPkg.Write(env_str); UnityEngine.Debug.Log("syncing with clients..."); ZRoutedRpc.instance.InvokeRoutedRPC(sender, "EventTestConnection", new object[] { new ZPackage() }); ZRoutedRpc.instance.InvokeRoutedRPC(sender, "EventSetTime", new object[] { timePkg }); ZRoutedRpc.instance.InvokeRoutedRPC(sender, "EventSetWeather", new object[] { weatherPkg }); foreach (string name in GamanMaker.invisible_players) { pkg.Write(name); pkg.Write(false); ZRoutedRpc.instance.InvokeRoutedRPC(sender, "EventSetVisible", new object[] { pkg }); } } else { ZPackage newPkg = new ZPackage(); // Create a new ZPackage. newPkg.Write("Peer doesn't exist"); // Tell them what's going on. ZRoutedRpc.instance.InvokeRoutedRPC(sender, "BadRequestMsg", new object[] { newPkg }); // Send the error message. } }
static void Postfix(ZNet __instance, ref ZNetPeer peer, ref RectTransform ___m_connectingDialog, ref RectTransform ___m_passwordDialog) { peer.m_rpc.Register <bool>("ClientHandshake", RPC_ClientHandshake); m_connectingDialog = ___m_connectingDialog; m_passwordDialog = ___m_passwordDialog; m_instance = __instance; }
// code borrowed from https://github.com/Valheim-Modding/Wiki/wiki/Server-Validated-RPC-System and modified public static void RPC_RequestSetWeather(long sender, ZPackage pkg) { if (pkg != null && pkg.Size() > 0) { // Check that our Package is not null, and if it isn't check that it isn't empty. ZNetPeer peer = ZNet.instance.GetPeer(sender); // Get the Peer from the sender, to later check the SteamID against our Adminlist. if (peer != null) { // Confirm the peer exists string peerSteamID = ((ZSteamSocket)peer.m_socket).GetPeerID().m_SteamID.ToString(); // Get the SteamID from peer. if ( ZNet.instance.m_adminList != null && ZNet.instance.m_adminList.Contains(peerSteamID) ) { // Check that the SteamID is in our Admin List. string msg = pkg.ReadString(); // Read the message from the user. pkg.SetPos(0); // Reset the position of our cursor so the client's can re-read the package. ZRoutedRpc.instance.InvokeRoutedRPC(0L, "EventSetWeather", new object[] { pkg }); // Send our Event to all Clients. 0L specifies that it will be sent to everybody EnvMan.instance.m_debugEnv = msg; } } else { ZPackage newPkg = new ZPackage(); // Create a new ZPackage. newPkg.Write("You aren't an Admin!"); // Tell them what's going on. ZRoutedRpc.instance.InvokeRoutedRPC(sender, "BadRequestMsg", new object[] { newPkg }); // Send the error message. } } }
private static void AddNewPlayerInfo(ZNetPeer peer) { var steamID = (peer.m_socket as ZSteamSocket).GetPeerID(); string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ServerRewards"); if (!Directory.Exists(path)) { Dbgl("Creating mod folder"); Directory.CreateDirectory(path); } string infoPath = Path.Combine(path, "PlayerInfo"); if (!Directory.Exists(infoPath)) { Directory.CreateDirectory(infoPath); } var info = new PlayerInfo() { id = steamID.m_SteamID, currency = playerStartCurrency.Value }; string json = JsonUtility.ToJson(info); string file = Path.Combine(infoPath, steamID + ".json"); File.WriteAllText(file, json); }
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; } }
static void NotifyOtherPlayersOfDeath(ZRoutedRpc __instance, ZRoutedRpc.RoutedRPCData rpcData) { // Late bind to instance if (_getPeer == null) { _getPeer = (Func <long, ZNetPeer>)Delegate.CreateDelegate(typeof(Func <long, ZNetPeer>), __instance, _getPeerInfo); } if (ZNet.instance == null || !ZNet.instance.IsServer()) { return; } if (rpcData.m_methodHash != _onDeathHash) { return; } ZNetPeer peer = _getPeer(rpcData.m_senderPeerID); if (peer == null) { return; } ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "ShowMessage", 2, "Player " + peer.m_playerName + " has died."); }
private static IEnumerator ReceivedSettings(ZNetPeer peer) { int finalHash = GetHashCode(SettingsReceiveBuffer); if (finalHash == SettingsReceiveHash) { Log($"Settings transfer complete, unpacking them now"); var loadingTask = Task.Run(() => { var settingsPkg = new ZPackage(SettingsReceiveBuffer); var settings = BetterContinentsSettings.Load(settingsPkg); WorldCache.Add(settingsPkg); return(settings); }); try { UI.Add("ReceivedSettings", () => UI.DisplayMessage($"Better Continents: initializing from server config")); yield return(new WaitUntil(() => loadingTask.IsCompleted)); } finally { UI.Remove("ReceivedSettings"); } if (loadingTask.IsFaulted) { LastConnectionError = $"Better Continents: cached world settings failed to load ({loadingTask.Exception.Message}), please reconnect to download them again!"; LogError(LastConnectionError); m_connectionStatus.SetValue(null, ZNet.ConnectionStatus.ErrorConnectFailed); ZNet.instance.Disconnect(peer); yield break; } Settings = loadingTask.Result; Settings.Dump(); // We only care about server/client version match when the server sends a world that actually uses the mod if (Settings.EnabledForThisWorld && ServerVersion != ModInfo.Version) { LastConnectionError = $"Better Continents: world has Better Continents enabled, but server {ServerVersion} and client {ModInfo.Version} mod versions don't match"; LogError(LastConnectionError); m_connectionStatus.SetValue(null, ZNet.ConnectionStatus.ErrorConnectFailed); ZNet.instance.Disconnect(peer); } else if (!Settings.EnabledForThisWorld) { Log($"Server world does not have Better Continents enabled, skipping version check"); } peer.m_rpc.Invoke("BetterContinentsReady", 0); } else { LogError($"{LastConnectionError}: hash mismatch, got {finalHash}, expected {SettingsReceiveHash}"); m_connectionStatus.SetValue(null, ZNet.ConnectionStatus.ErrorConnectFailed); ZNet.instance.Disconnect(peer); } }
private static void ZNet_OnNewConnection(ZNet __instance, ZNetPeer peer) { // clear the previous connection, if existing LastServerVersion = null; // Register our RPC very early peer.m_rpc.Register <ZPackage>(nameof(RPC_Jotunn_ReceiveVersionData), RPC_Jotunn_ReceiveVersionData); }
public static void Postfix(ZNet __instance, ZNetPeer peer) { if (VACPlugin.posMap.ContainsKey(peer)) { return; } VACPlugin.posMap.Add(peer, Vector3.zero); }
public static void ServerSendKnownRecipes(ZNetPeer peer) { var pkg = new ZPackage(); WriteServerKnownRecipes(PlayerKnownRecipes, pkg); ZRoutedRpc.instance.InvokeRoutedRPC(peer.m_uid, Name_RPC_ServerKnownRecipes, pkg); }
public static void OnPeerConnect(ZNetPeer peer) { if (Common.Utils.IsServer()) { ServerSendKnownMats(peer); ServerSendKnownRecipes(peer); } }
// comparing UpdateSaving public static void CheckMotd(float dt, bool ignoreSeen = false) { m_motdTimer += dt; // check every 10 seconds and return if lower than the interval and ignoreSeen is false. if (m_motdTimer < MessageOfTheDayCheckInterval.Value && !ignoreSeen) { return; } LogDebug($"Checking Motd. Note: Refresh {m_refresh}"); // reset the motd timer m_motdTimer = 0f; //SavePlayerProfile(setLogoutPoint: false); // validate this instance is good if (ZNet.instance) { // get all the characters List <ZDO> allCharacterZDOS = ZNet.instance.GetAllCharacterZDOS(); LogVerbose($"allCharacterZDOS count: {allCharacterZDOS.Count}"); // set character count foreach (ZDO characterZDO in allCharacterZDOS) { LogVerbose($"Found uid: {characterZDO.m_uid} owner: {characterZDO.m_owner}"); // get last Message OF The Day displayed time bool SeenMOTD = characterZDO.GetBool("mm_shownmotd", false); // if the message hasn't been seen OR we're ignoring that it's been seen.... if (!SeenMOTD || ignoreSeen) { //GetPeerByPlayerName string playerName = characterZDO.GetString("playerName"); LogVerbose($"playerName {playerName}"); ZNetPeer peer = ZNet.instance.GetPeerByPlayerName(playerName); //ZNetPeer peer = ZNet.instance.GetPeer(peerid); LogVerbose($"Peer not found: {(peer == null)}"); if (null != peer && peer.IsReady()) { LogVerbose($"Sending Message to {peer.m_uid}"); ZRoutedRpc.instance.InvokeRoutedRPC(peer.m_uid, "ShowMessage", (int)MessageOfTheDayType.Value, MessageOfTheDay.Value); characterZDO.Set("mm_shownmotd", true); } else { LogVerbose($"Peer {characterZDO.m_owner} not ready or peer not found"); } } } } }
// Token: 0x060008F5 RID: 2293 RVA: 0x00042D34 File Offset: 0x00040F34 public void AddPeer(ZNetPeer peer) { this.m_peers.Add(peer); peer.m_rpc.Register <ZPackage>("RoutedRPC", new Action <ZRpc, ZPackage>(this.RPC_RoutedRPC)); if (this.m_onNewPeer != null) { this.m_onNewPeer(peer.m_uid); } }
private static void Dispose(ZNetPeer __instance) { var socketQueueIdentifier = __instance.m_socket.GetEndPointString(); lock (SocketQueues) { SocketQueues.Remove(socketQueueIdentifier); } }
private static IEnumerator LoadFromCache(ZNetPeer peer, string id) { var loadTask = Task.Run <BetterContinentsSettings?>(() => { var package = WorldCache.LoadCacheItem(id); // Recalculate the id again to confirm it really matches string localId = WorldCache.PackageID(package); if (id != localId) { return(null); } return(BetterContinentsSettings.Load(package)); }); try { UI.Add("LoadingFromCache", () => UI.DisplayMessage($"Better Continents: initializing from cached config")); yield return(new WaitUntil(() => loadTask.IsCompleted)); } finally { UI.Remove("LoadingFromCache"); } if (loadTask.IsFaulted || loadTask.Result == null) { LastConnectionError = loadTask.Exception != null ? $"Better Continents: cached world settings failed to load ({loadTask.Exception.Message}), please reconnect to download them again!" : $"Better Continents: cached world settings are corrupted, please reconnect to download them again!"; LogError(LastConnectionError); m_connectionStatus.SetValue(null, ZNet.ConnectionStatus.ErrorConnectFailed); ZNet.instance.Disconnect(peer); WorldCache.DeleteCacheItem(id); yield break; } Settings = loadTask.Result.Value; Settings.Dump(); // We only care about server/client version match when the server sends a world that actually uses the mod if (Settings.EnabledForThisWorld && ServerVersion != ModInfo.Version) { LastConnectionError = $"Better Continents: world has the mod enabled, but server {ServerVersion} and client {ModInfo.Version} versions don't match"; LogError(LastConnectionError); m_connectionStatus.SetValue(null, ZNet.ConnectionStatus.ErrorConnectFailed); ZNet.instance.Disconnect(peer); } else if (!Settings.EnabledForThisWorld) { Log($"Server world does not have Better Continents enabled, skipping version check"); } peer.m_rpc.Invoke("BetterContinentsReady", 0); }
// Token: 0x060007CB RID: 1995 RVA: 0x0003D420 File Offset: 0x0003B620 private bool IsInPeerActiveArea(Vector2i sector, long uid) { if (uid == this.m_myid) { return(ZNetScene.instance.InActiveArea(sector, ZNet.instance.GetReferencePosition())); } ZNetPeer peer = ZNet.instance.GetPeer(uid); return(peer != null && ZNetScene.instance.InActiveArea(sector, peer.GetRefPos())); }
public static void RPC_Known(ZRpc rpc, ZPackage pkg) { ZNetPeer peer = ZNet.instance.GetPeer(rpc); long sender = peer.m_uid; int materialCount = LoadKnownFromPkg(sender, pkg, playerKnownMaterial); int recipeCount = LoadKnownFromPkg(sender, pkg, playerKnownRecipes); Plugin.logger.LogInfo($"Received known from peer {sender}: {materialCount} materials / {recipeCount} recipes"); }
private static void Postfix(ZNetPeer peer, ZNet __instance) { if (__instance.IsServer()) { peer.m_rpc.Register <int, int>("OnClientExplore", new Action <ZRpc, int, int>(onClientExplore)); } else { peer.m_rpc.Register <ZPackage>("OnReceiveMapData", new Action <ZRpc, ZPackage>(onReceiveMapData)); } }
private static void Chat__InputText(ref Chat __instance) { Debug.Log($"Found some input {__instance.m_input.text}"); ZNetPeer peer = __instance.GetComponent <ZNetPeer>(); long sender = long.Parse(((ZSteamSocket)peer.m_socket).GetPeerID().m_SteamID.ToString()); var text = __instance.m_input.text; if (text.ToLower().StartsWith("!user") || text.ToLower().StartsWith("!group")) { } }
// Token: 0x060007C4 RID: 1988 RVA: 0x0003D164 File Offset: 0x0003B364 private ZDOMan.ZDOPeer FindPeer(ZNetPeer netPeer) { foreach (ZDOMan.ZDOPeer zdopeer in this.m_peers) { if (zdopeer.m_peer == netPeer) { return(zdopeer); } } return(null); }
public static void OnPeerDisconnect(ZNetPeer peer) { if (playerKnownRecipes.ContainsKey(peer.m_uid)) { playerKnownRecipes.Remove(peer.m_uid); } if (playerKnownMaterial.ContainsKey(peer.m_uid)) { playerKnownMaterial.Remove(peer.m_uid); } }
public static void Postfix(ZNet __instance, ZNetPeer peer) { if (!modEnabled.Value) { return; } Dbgl("New connection"); peer.m_rpc.Register <string>("SendServerRewardsJSON", new Action <ZRpc, string>(RPC_SendJSON)); }
public static void Postfix(ZNet __instance, ZRpc rpc) { ZNetPeer peer = Traverse.Create(__instance).Method("GetPeer", new object[] { rpc }).GetValue <ZNetPeer>(); Dbgl($"Sending loading screen {serverLoadingScreen.Value}"); peer.m_rpc.Invoke("ShareLoadingScreen", new object[] { serverLoadingScreen.Value }); }
public static void OnPeerDisconnect(ZNetPeer peer) { EpicLoot.Log($"Removing known for peer {peer.m_uid}"); if (PlayerKnownRecipes.ContainsKey(peer.m_uid)) { PlayerKnownRecipes.Remove(peer.m_uid); } if (PlayerKnownMaterial.ContainsKey(peer.m_uid)) { PlayerKnownMaterial.Remove(peer.m_uid); } }
// Token: 0x060007C3 RID: 1987 RVA: 0x0003D12C File Offset: 0x0003B32C public void RemovePeer(ZNetPeer netPeer) { ZDOMan.ZDOPeer zdopeer = this.FindPeer(netPeer); if (zdopeer != null) { this.m_peers.Remove(zdopeer); if (ZNet.instance.IsServer()) { this.RemoveOrphanNonPersistentZDOS(); } } }
public static void Postfix(ref ZRoutedRpc __instance, ref ZNetPeer peer) { // Send a greeting message to the server or client. if (ZNet.m_isServer) { GreetingMessage.SendToClient(peer.m_uid); } else { GreetingMessage.SendToServer(); } }