/// <summary> /// Handles the connection state when receiving a packet /// </summary> public static void CheckState(ref HookContext ctx, ref HookArgs.CheckBufferState args) { if (Terraria.Netplay.Clients[args.BufferId].State == (int)ConnectionState.AwaitingUserPassword) { //Since this is a custom state, we accept it [true to kick the connection, false to accept] ctx.SetResult(HookResult.RECTIFY, true, false /* TODO validate packets */); } }
void OnChat(ref HookContext ctx, ref TDSMHookArgs.PlayerChat args) { if (args.Message.Length > 0 && args.Message.Substring(0, 1).Equals("/")) { ProgramLog.Log(ctx.Player.name + " sent command: " + args.Message); ctx.SetResult(HookResult.IGNORE); CommandParser.ParsePlayerCommand(ctx.Player, args.Message); } }
/// <summary> /// Handles packets received from OTA /// </summary> public static void HandlePacket(ref HookContext ctx, ref HookArgs.ReceiveNetMessage args) { if (_packetHandlers != null) { if (_packetHandlers[args.PacketId] != null) { if (_packetHandlers[args.PacketId].Read(args.BufferId, args.Start, args.Length)) { //Packet informed us that it was read, let OTA know we consumed the packet ctx.SetResult(HookResult.IGNORE, true); } } } }
void OnGreetPlayer(ref HookContext ctx, ref HookArgs.PlayerPreGreeting args) { ctx.SetResult(HookResult.IGNORE); var lines = args.Motd.Split(new string[] { "\\0" }, StringSplitOptions.None); foreach (var line in lines) ctx.Player.SendMessage(line, 255, 0, 0, 255); string list = ""; for (int i = 0; i < 255; i++) { if (Main.player[i].active) { if (list == "") list += Main.player[i].name; else list = list + ", " + Main.player[i].name; } } ctx.Player.SendMessage("Current players: " + list + ".", 255, 255, 240, 20); }
void OnDefaultServerStart(ref HookContext ctx, ref HookArgs.StartDefaultServer args) { if (RunServerCore) { ProgramLog.Log("Starting TDSM's slot server..."); ctx.SetResult(HookResult.IGNORE); ServerCore.Server.StartServer(); } else ProgramLog.Log("Vanilla only specified, continuing on with Re-Logic code..."); }
private void OnSetTitle(ref HookContext ctx, ref HookArgs.SetWindowTitle args) { ctx.SetResult(HookResult.RECTIFY, resultParam: $"{args.Title} | TDSM ({Entry.CoreVersion}) , OTAPI ({Globals.BuildInfo})"); }
void OnPlayerKilled(ref HookContext ctx, ref HookArgs.PlayerKilled args) { if (_likeABoss) { if (Terraria.Main.rand == null) Terraria.Main.rand = new Random(); if (ctx.Player.talkNPC > -1 && Terraria.Main.rand.Next(_labDeathMessages.Count - 1) == 1) { args.DeathText = " was too busy talking"; } // else if (Terraria.Main.rand.Next(_labDeathMessages.Count - 1) == 1) // { // // } // forgot [NPC]'s birthday // tried to hit on [NPC] else { args.DeathText = _labDeathMessages.Next(); } args.DeathText = ctx.Player.name + args.DeathText; ctx.SetResult(HookResult.CONTINUE); Loggers.Death.Log(args.DeathText); } else { //Standard death log Loggers.Death.Log(ctx.Player.name + args.DeathText); } }
void OnNPCSpawned(ref HookContext ctx, ref HookArgs.NpcSpawn args) { if (StopNPCSpawning) ctx.SetResult(HookResult.IGNORE); }
void OnInvasionWarning(ref HookContext ctx, ref HookArgs.InvasionWarning args) { if (Main.invasionType == Core._assignedInvasionType && Core._invasion != null) { if (Main.invasionSize > 0) { string message = null; if (Main.invasionX < (double)Main.spawnTileX) { //West if (!Core._notfInbound) { Core._notfInbound = true; message = "An invasion is approaching from the west!"; } } else if (Main.invasionX > (double)Main.spawnTileX) { //East if (!Core._notfInbound) { Core._notfInbound = true; message = "An invasion is approaching from the east!"; } } else { //Arrived message = "The invasion has arrived!"; } if (null != message) NetMessage.SendData(25, -1, -1, message, 255, 175f, 75f, 255f); } ctx.SetResult(HookResult.IGNORE); } }
void OnStatusTextChanged(ref HookContext ctx, ref HookArgs.StatusTextChange args) { ctx.SetResult(HookResult.IGNORE); //There's no locking and two seperate threads, so we must use local variables incase of changes var statusText = Terraria.Main.statusText; var oldStatusText = Terraria.Main.oldStatusText; if (oldStatusText != statusText) { if (!String.IsNullOrEmpty(statusText)) { string previousProgress, currentProgress; string keyA = GetProgressKey(oldStatusText, out previousProgress); string keyB = GetProgressKey(statusText, out currentProgress); if (keyA != null && keyB != null) { keyA = keyA.Trim(); keyB = keyB.Trim(); if (keyA.Length > 0 && keyB.Length > 0) { if (keyA == keyB) { if (lastWritten > 0) { for (var x = 0; x < lastWritten; x++) Console.Write("\b"); } Console.Write(currentProgress); lastWritten += currentProgress.Length - lastWritten; } else { Console.WriteLine(); lastWritten = 0; Console.Write(statusText); lastWritten += currentProgress.Length; if (currentProgress.Length == 0) Console.WriteLine(); } } else { if (lastWritten > 0)//!String.IsNullOrEmpty(oldStatusText)) //There was existing text { Console.WriteLine(); lastWritten = 0; } Console.Write(statusText); lastWritten += currentProgress.Length; } } else if (keyA == null && keyB != null) { Console.Write(statusText); lastWritten += currentProgress.Length; } } else { if (lastWritten > 0)//!String.IsNullOrEmpty(oldStatusText)) //There was existing text { Console.WriteLine(); lastWritten = 0; } } } else if (statusText == String.Empty) { if (lastWritten > 0)//!String.IsNullOrEmpty(Terraria.Main.oldStatusText)) //There was existing text { Console.WriteLine(); lastWritten = 0; } } Terraria.Main.oldStatusText = statusText; }
static void OnNetMessageReceived(ref HookContext ctx, ref HookArgs.ReceiveNetMessage args) { if (Terraria.Main.ServerSideCharacter) { switch ((Packet)args.PacketId) { case Packet.INVENTORY_DATA: if (!AllowGuestInfo && !ctx.Player.IsAuthenticated()) ctx.SetResult(HookResult.IGNORE); break; case Packet.PLAYER_MANA_UPDATE: if (!AllowGuestInfo && !ctx.Player.IsAuthenticated()) ctx.SetResult(HookResult.IGNORE); break; case Packet.PLAYER_HEALTH_UPDATE: if (!AllowGuestInfo && !ctx.Player.IsAuthenticated()) ctx.SetResult(HookResult.IGNORE); break; } } }
void onPlayerOpenChest(ref HookContext ctx, ref HookArgs.ChestOpenReceived args) { ISender sender = ctx.Sender; int ChestId = args.ChestIndex; if(!(sender is Player)) { return; } // we only want players ? Player player = sender as Player; // get the Chest object Chest chest = Main.chest[ChestId]; // the location of the chest LocationKey key = new LocationKey(chest.x, chest.y); // see if we have a protection attached to the chest Protection protection = Cache.Protections.Get(key); bool CanAccess = true; // if it's a valid protection, ensure they can access it if(protection != null) { CanAccess = protection.CanAccess(player); // PASSWORD PROTECTION CHECK if(protection.Type == Protection.PASSWORD_PROTECTION) { if(!CanAccess) { Pair<Action, Protection> PassTemp = new Pair<Action, Protection>(Action.UNLOCK, protection); ResetActions(player); Cache.Actions.Add(player.Name, PassTemp); player.SendMessage("This chest is locked with a password!", 255, 255, 0, 0); player.SendMessage("Type /cunlock <password> to unlock it.", 150, 255, 0, 0); } } // Update the chest id if it changed somehow if(protection.ChestId != ChestId) { protection.ChestId = ChestId; } } // if they can't access it, cancel the event !! if(!CanAccess) { ctx.SetResult (HookResult.IGNORE); } // is there an action for this player? Pair<Action, Protection> pair = Cache.Actions.Get(sender.SenderName); if(pair == null) { // check again if they dont have access and pester them if(!CanAccess) { sender.SendMessage("That Chest is locked with a magical spell.", 255, 255, 0, 0); } return; } // action data Action action = pair.First; Protection Temp = pair.Second; switch(action) { case Action.INFO: if(protection == null) { player.SendMessage("That chest is not protected!", 255, 255, 0, 0); } else { player.SendMessage("Owner: " + protection.Owner, 255, 255, 0, 0); player.SendMessage("Type: " + protection.TypeToString(), 255, 255, 0, 0); if(CanAccess) { player.SendMessage("Can access: Yes", 255, 0, 255, 0); } else { player.SendMessage("Can access: No", 255, 255, 0, 0); } } ResetActions(player); break; case Action.CREATE: if(protection != null) { player.SendMessage("That chest has already been registered!", 255, 255, 0, 0); break; } // good good, now set the rest of the meta data Temp.ChestId = ChestId; Temp.X = chest.x; Temp.Y = chest.y; Temp.Valid = true; // register it !! Cache.Protections.Add(key, Temp); // remove the action Cache.Actions.Remove(player.Name); player.SendMessage("Registered a " + Temp.TypeToString() + " Chest successfully!", 255, 0, 255, 0); player.SendMessage("Note: Currently, empty chests can be destroyed !!", 255, 255, 0, 255); player.SendMessage("Ensure 1 item stays in it so the chest itself cannot be stolen.", 255, 255, 0, 255); break; case Action.REMOVE: if(protection == null) { player.SendMessage("Please open a protected chest!", 255, 255, 0, 0); return; } if(!protection.IsOwner(player)) { player.SendMessage("You are not the owner of that chest!", 255, 255, 0, 0); return; } // we're the owner, remove the protection !! protection.Remove(); Cache.Actions.Remove(player.Name); player.SendMessage("Protection removed!", 255, 255, 0, 255); break; } }
void OnStartCommandProcessing(ref HookContext ctx, ref HookArgs.StartCommandProcessing args) { ctx.SetResult(HookResult.IGNORE); if (Console.IsInputRedirected) { ProgramLog.Admin.Log("Console input redirection has been detected."); return; } (new OTA.Misc.ProgramThread("Command", ListenForCommands)).Start(); }
void OnServerStateChange(ref HookContext ctx, ref HookArgs.ServerStateChange args) { ProgramLog.Log("Server state changed to: " + args.ServerChangeState.ToString()); if (args.ServerChangeState == ServerState.Initialising) { if (!String.IsNullOrEmpty(RConBindAddress)) { ProgramLog.Log("Starting RCON Server"); RemoteConsole.RConServer.Start(Path.Combine(Globals.DataPath, "rcon_logins.properties")); } if (Terraria.Main.ServerSideCharacter) { CharacterManager.Init(); } if (BackupManager.BackupsEnabled && BackupManager.BackupIntervalMinutes < SaveManager.SaveIntervalMinutes) { ProgramLog.Admin.Log("[Warning] Backup interval is smaller than the save interval."); } } if (args.ServerChangeState == ServerState.Stopping) { RunComponent(ComponentEvent.ServerStopping); if (!String.IsNullOrEmpty(RConBindAddress)) { ProgramLog.Log("Stopping RCON Server"); RemoteConsole.RConServer.Stop(); } } if (args.ServerChangeState == ServerState.Starting) { RunComponent(ComponentEvent.ServerStarting); } //if (args.ServerChangeState == ServerState.Initialising) #if TDSMServer if (!Server.IsInitialised) { Server.Init(); if (!String.IsNullOrEmpty(RConBindAddress)) { ProgramLog.Log("Starting RCON Server"); RemoteConsole.RConServer.Start(Path.Combine(Globals.DataPath, "rcon_logins.properties")); } if (!String.IsNullOrEmpty(_webServerAddress)) { ProgramLog.Log("Starting Web Server"); WebInterface.WebServer.Begin(_webServerAddress, _webServerProvider); this.AddCommand("webauth") .WithAccessLevel(AccessLevel.OP) .Calls(WebInterface.WebServer.WebAuthCommand); } } if (args.ServerChangeState == ServerState.Stopping) { RemoteConsole.RConServer.Stop(); WebInterface.WebServer.End(); //if (properties != null && File.Exists(properties.PIDFile.Trim())) //File.Delete(properties.PIDFile.Trim()); } ctx.SetResult(HookResult.IGNORE); //Don't continue on with vanilla code #endif }
void OnLoadConfiguration(ref HookContext ctx, ref HookArgs.LoadConfigurationFile args) { ctx.SetResult(HookResult.IGNORE); }
void OnNetMessageSend(ref HookContext ctx, ref HookArgs.SendNetMessage args) { if (Terraria.Main.ServerSideCharacter) { switch ((Packet)args.MsgType) { case Packet.WORLD_DATA: ctx.SetResult(HookResult.IGNORE); var writer = NetMessage.buffer[args.BufferId].writer; writer.Write((int)Main.time); byte value; value = 0; if (Main.dayTime) value += 1; if (Main.bloodMoon) value += 2; if (Main.bloodMoon) value += 4; writer.Write(value); writer.Write((byte)Main.moonPhase); writer.Write((short)Main.maxTilesX); writer.Write((short)Main.maxTilesY); writer.Write((short)Main.spawnTileX); writer.Write((short)Main.spawnTileY); writer.Write((short)Main.worldSurface); writer.Write((short)Main.rockLayer); writer.Write(Main.worldID); writer.Write(Main.worldName); writer.Write((byte)Main.moonType); writer.Write((byte)WorldGen.treeBG); writer.Write((byte)WorldGen.corruptBG); writer.Write((byte)WorldGen.jungleBG); writer.Write((byte)WorldGen.snowBG); writer.Write((byte)WorldGen.hallowBG); writer.Write((byte)WorldGen.crimsonBG); writer.Write((byte)WorldGen.desertBG); writer.Write((byte)WorldGen.oceanBG); writer.Write((byte)Main.iceBackStyle); writer.Write((byte)Main.jungleBackStyle); writer.Write((byte)Main.hellBackStyle); writer.Write(Main.windSpeedSet); writer.Write((byte)Main.numClouds); for (int k = 0; k < 3; k++) writer.Write(Main.treeX[k]); for (int l = 0; l < 4; l++) writer.Write((byte)Main.treeStyle[l]); for (int m = 0; m < 3; m++) writer.Write(Main.caveBackX[m]); for (int n = 0; n < 4; n++) writer.Write((byte)Main.caveBackStyle[n]); if (!Main.raining) Main.maxRaining = 0; writer.Write(Main.maxRaining); value = 0; if (WorldGen.shadowOrbSmashed) value += 1; if (NPC.downedBoss1) value += 2; if (NPC.downedBoss2) value += 4; if (NPC.downedBoss3) value += 8; if (Main.hardMode) value += 16; if (NPC.downedClown) value += 32; if (Main.ServerSideCharacter) value += 64; if (NPC.downedPlantBoss) value += 128; writer.Write(value); value = 0; if (NPC.downedMechBoss1) value += 1; if (NPC.downedMechBoss2) value += 2; if (NPC.downedMechBoss3) value += 4; if (NPC.downedMechBossAny) value += 8; if (Main.cloudBGActive >= 1) value += 16; if (WorldGen.crimson) value += 32; if (Main.pumpkinMoon) value += 64; if (Main.snowMoon) value += 128; writer.Write(value); value = 0; if (Main.expertMode) value += 1; if (Main.fastForwardTime) value += 2; if (Main.slimeRain) value += 4; if (NPC.downedSlimeKing) value += 8; if (NPC.downedQueenBee) value += 16; if (NPC.downedFishron) value += 32; if (NPC.downedMartians) value += 64; if (NPC.downedAncientCultist) value += 128; writer.Write(value); value = 0; if (NPC.downedMoonlord) value += 1; if (NPC.downedHalloweenKing) value += 2; if (NPC.downedHalloweenTree) value += 4; if (NPC.downedChristmasIceQueen) value += 8; if (NPC.downedChristmasSantank) value += 16; if (NPC.downedChristmasTree) value += 32; if (NPC.downedGolemBoss) value += 64; writer.Write(value); writer.Write((sbyte)Main.invasionType); /*if (SocialAPI.Network != null) writer.Write(SocialAPI.Network.GetLobbyId()); else*/ writer.Write(0); break; } } }
void OnWorldSave(ref HookContext ctx, ref HookArgs.WorldAutoSave args) { //let our backup manager do it's thing ctx.SetResult(HookResult.IGNORE, true); }