private static byte GetHeaderByte(EventClass ec, EventType et, HookArgs ha) { byte head = 0; if (ec == EventClass.MouseEvent) { head = 0x80; } head |= (byte)((uint)et << 4); if (ha.Alt) { head |= 0x08; } if (ha.Control) { head |= 0x04; } if (ha.Shift) { head |= 0x02; } if (ha.CapsLock) { head |= 0x01; } return(head); }
public static void OnUnkownPacketSend(ref HookContext ctx, ref HookArgs.UnkownSendPacket args) { switch (args.PacketId) { case (int)Packets.CLIENT_MOD_GOD: { NetMessageExtension msg = new NetMessageExtension(); if (args.RemoteClient != -1) { var player = Main.players[args.RemoteClient]; if (player.HasClientMod) { if (Server.AllowTDCMRPG) { Server.notifyOps( String.Format("Failed attempt to {0} God Mode on an RPG Server.", true, (args.Number == 1) ? "give" : "remove")); return; } Server.notifyOps( String.Format("{0} {1} God Mode.", true, player.Name, (args.Number == 1) ? "has" : "doesn't have")); msg.GodTDCMClient(args.Number == 1); args.Message = msg; ctx.SetResult(HookResult.IGNORE); //Let TDSM know it's to ignore returning. } } break; } } }
void LeftHook(ref HookContext ctx, ref HookArgs.PlayerLeftGame args) { // If player has a last command stored, remove it now. if ( history.ContainsKey(ctx.Sender.SenderName) ) { history.Remove(ctx.Sender.SenderName); } }
void OnReadConfig(ref HookContext ctx, ref HookArgs.ConfigurationLine args) { switch (args.Key) { case "mysql": if (!Storage.IsAvailable) { MySQLConnector cn = null; try { cn = new MySQLConnector(args.Value); cn.Open(); } catch (Exception e) { ProgramLog.Error.Log("Exception connecting to MySQL database: {0}", e); return; } Storage.SetConnector(cn); _connector = cn; } break; } }
void OnStateChange(ref HookContext ctx, ref HookArgs.ServerStateChange args) { if (args.ServerChangeState == TDSM.API.ServerState.Initialising) { ProgramLog.Plugin.Log("SQLite connector is: " + (_connector == null ? "disabled" : "enabled")); } }
/// <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 OnStateChange(ref HookContext ctx, ref HookArgs.ServerStateChange args) { if (args.ServerChangeState == TDSM.API.ServerState.Initialising) { //Data connectors must have loaded by now //Get TDSM to swap the current permission handler to our own TDSM.API.Permissions.PermissionsManager.SetHandler(_instance); } }
public static byte[] GetEncodedData(EventClass eventClass, EventType type, HookArgs args, int[] pressedKeys = null, char pressedChar = '\0') { byte[] data = null; using (MemoryStream ms = new MemoryStream()) { ms.WriteByte(GetHeaderByte(eventClass, type, args)); if (eventClass == EventClass.MouseEvent) { MouseHookEventArgs me = args as MouseHookEventArgs; ms.Write(BitConverter.GetBytes((ushort)me.Location.X), 0, 2); ms.Write(BitConverter.GetBytes((ushort)me.Location.Y), 0, 2); //ms.WriteByte((byte)me.Location.X); //ms.WriteByte((byte)me.Location.Y); //System.Windows.Forms.MouseButtons.Right; ms.WriteByte((byte)((int)me.Button >> 20)); byte extra = 0; switch (type) { case EventType.MouseClick: case EventType.MouseDblClick: extra = (byte)me.ClickCount; break; case EventType.MouseWheel: extra = (byte)Math.Abs(me.ScrollAmount); if (me.ScrollAmount < 0) { extra |= 0x80; } break; } ms.WriteByte(extra); } else { KeyHookEventArgs ke = args as KeyHookEventArgs; byte[] pKeys = { 0, 0, 0 }; int len = Math.Min(3, pressedKeys.Length); for (int i = 0; i < len; i++) { pKeys[i] = (byte)pressedKeys[i]; } ms.Write(pKeys, 0, 3); ms.WriteByte((byte)pressedChar); } data = ms.ToArray(); } return(data); }
void CommmandHook(ref HookContext ctx, ref HookArgs.Command args) { // If the command issued is not !... if ( args.Prefix != "!" ) { string WhoCalled = ctx.Sender.SenderName; // get the name of the sender // If there is a command saved for the sender already... if ( history.ContainsKey( WhoCalled ) ) { history[WhoCalled] = args.Prefix + " " + args.ArgumentString; // Replace the sender's last command. } else { history.Add( WhoCalled, args.Prefix + " " + args.ArgumentString ); // Record the sender's command for the first time. } } }
/// <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 OnServerStateChange(ref HookContext ctx, ref HookArgs.ServerStateChange args) { if (args.ServerChangeState == ServerState.LOADED) { ProgramLog.Plugin.Log("Starting fishy mod..."); fishy = new Fishy(); ProgramLog.Plugin.Log("Fishy mod Started."); } else if (args.ServerChangeState == ServerState.STOPPING || args.ServerChangeState == ServerState.RESTARTING) { ProgramLog.Plugin.Log("Stopping fishy mod..."); fishy.Stop(); ProgramLog.Plugin.Log("Fishy mod Stopped."); } }
private static void FromHeaderByte(byte head, out EventClass ec, out EventType et, out HookArgs ha) { if ((head & 0x80) == 0x80) { ec = EventClass.MouseEvent; } else { ec = EventClass.KeyEvent; } et = (EventType)((head & 0x70) >> 4); ha = new HookArgs((head & 0x08) == 0x08, (head & 0x04) == 0x04, (head & 0x02) == 0x02, (head & 0x01) == 0x01, false, false); }
void OnReadConfig(ref HookContext ctx, ref HookArgs.ConfigurationLine args) { switch (args.Key) { case "sqlite": if (_connector == null) { var cn = new SQLiteConnector(args.Value); cn.Open(); Storage.SetConnector(cn); _connector = cn; } break; } }
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..."); }
void OnPluginsLoaded(ref HookContext ctx, ref HookArgs.PluginsLoaded args) { LoadPerms(); }
void onPlayerJoin(ref HookContext ctx, ref HookArgs.PlayerEnteringGame args) { //ctx.Player.A = AccessLevel.OP; }
void OnSignEdit(ref HookContext ctx, ref HookArgs.SignTextSet args) { foreach (Region rgn in regionManager.Regions) { if (rgn.HasPoint(new Vector2(args.X, args.Y))) { if (ctx.Sender is Player) { if (IsRestrictedForUser(ctx.Player, rgn, DoorChange)) { ctx.SetResult(HookResult.IGNORE); ctx.Player.sendMessage("You cannot edit this area!", ChatColor.Red); return; } } } } }
void OnServerStateChange(ref HookContext ctx, ref HookArgs.ServerStateChange args) { if (args.ServerChangeState == ServerState.LOADED) regionManager.LoadRegions(); }
void OnProjectileReceived(ref HookContext ctx, ref HookArgs.ProjectileReceived args) { Vector2 Position = new Vector2(args.X, args.Y); foreach (Region rgn in regionManager.Regions) { if (rgn.HasPoint(Position / 16)) { if (rgn.ProjectileList.Contains("*") || rgn.ProjectileList.Contains(args.Type.ToString()))// || //rgn.ProjectileList.Contains(args.Projectile.Name.ToLower().Replace(" ", ""))) { if (IsRestrictedForUser(ctx.Player, rgn, ProjectileUse)) { ctx.SetResult(HookResult.ERASE); ctx.Player.sendMessage("You cannot edit this area!", ChatColor.Red); return; } } } } }
void OnPluginsLoaded(ref HookContext ctx, ref HookArgs.PluginsLoaded args) { UsingPermissions = IsRunningPermissions(); if (UsingPermissions) Log("Using Permissions."); else Log("No Permissions Found\nUsing Internal User System"); }
public static bool GetDecodedData(int offset, byte[] data, out EventClass eventClass, out EventType type, out HookArgs args, out byte[] pressedKeys, out char pressedChar) { using (MemoryStream ms = new MemoryStream(data)) { // Set Offset ms.Position = offset; // Assign pressedKeys = null; pressedChar = '\0'; eventClass = EventClass.None; type = EventType.None; args = null; // Check if (ms.Position < ms.Length) { FromHeaderByte((byte)ms.ReadByte(), out eventClass, out type, out args); pressedKeys = new byte[3] { 0, 0, 0 }; if (eventClass == EventClass.MouseEvent) { //int x = ms.ReadByte(); //int y = ms.ReadByte(); byte[] loc = new byte[2]; ms.Read(loc, 0, 2); int x = BitConverter.ToInt16(loc, 0); ms.Read(loc, 0, 2); int y = BitConverter.ToInt16(loc, 0); System.Windows.Forms.MouseButtons button = System.Windows.Forms.MouseButtons.None; switch (ms.ReadByte()) { case 1: button = System.Windows.Forms.MouseButtons.Left; break; case 2: button = System.Windows.Forms.MouseButtons.Right; break; case 4: button = System.Windows.Forms.MouseButtons.Middle; break; case 8: button = System.Windows.Forms.MouseButtons.XButton1; break; case 16: button = System.Windows.Forms.MouseButtons.XButton2; break; } int extra = ms.ReadByte(); int click = 0; int scroll = 0; switch (type) { case EventType.MouseClick: case EventType.MouseDblClick: click = extra; break; case EventType.MouseWheel: if ((extra & 0x80) == 0x80) { scroll = -1; } else { scroll = 1; } extra &= 0x7F; scroll = scroll * extra * 120; //Delta break; } args = new MouseHookEventArgs(false, button, click, x, y, scroll, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock); } else { ms.Read(pressedKeys, 0, 3); pressedChar = (char)ms.ReadByte(); args = new KeyHookEventArgs(pressedKeys[0], pressedChar, false, args.Alt, args.Control, args.Shift, args.CapsLock, args.NumLock, args.ScrollLock); } return(true); } } return(false); }
void OnChat(ref HookContext ctx, ref HookArgs.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); UserInput.CommandParser.ParsePlayerCommand(ctx.Player, args.Message); } }
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 (args.ServerChangeState == ServerState.Stopping) { RemoteConsole.RConServer.Stop(); } //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); 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 OnPlayerDisconnected(ref HookContext ctx, ref HookArgs.PlayerLeftGame args) { #if TDSMServer if (RestartWhenNoPlayers && ClientConnection.All.Count - 1 == 0) { PerformRestart(); } #endif }
void OnInventoryItemReceived(ref HookContext ctx, ref HookArgs.InventoryItemReceived args) { #if TDSMSever if (Server.ItemRejections.Count > 0) { if (args.Item != null) { if (Server.ItemRejections.Contains(args.Item.name) || Server.ItemRejections.Contains(args.Item.type.ToString())) { if (!String.IsNullOrEmpty(args.Item.name)) { ctx.SetKick(args.Item.name + " is not allowed on this server."); } else { ctx.SetKick("Item type " + args.Item.type.ToString() + " is not allowed on this server."); } } } } #endif }
void OnConfigLineRead(ref HookContext ctx, ref HookArgs.ConfigurationLine args) { switch (args.Key) { #if TDSMServer case "usewhitelist": bool usewhitelist; if (Boolean.TryParse(args.Value, out usewhitelist)) { Server.WhitelistEnabled = usewhitelist; } break; #endif case "vanilla-linux": bool vanillaOnly; if (Boolean.TryParse(args.Value, out vanillaOnly)) { VanillaOnly = vanillaOnly; } break; case "heartbeat": bool hb; if (Boolean.TryParse(args.Value, out hb) && hb) { Heartbeat.Begin(CoreBuild); } break; case "server-list": bool serverList; if (Boolean.TryParse(args.Value, out serverList)) { Heartbeat.PublishToList = serverList; } break; case "server-list-name": Heartbeat.ServerName = args.Value; break; case "server-list-desc": Heartbeat.ServerDescription = args.Value; break; case "server-list-domain": Heartbeat.ServerDomain = args.Value; break; case "rcon-hash-nonce": RConHashNonce = args.Value; break; case "rcon-bind-address": RConBindAddress = args.Value; break; case "web-server-bind-address": _webServerAddress = args.Value; break; case "web-server-provider": _webServerProvider = args.Value; break; case "web-server-serve-files": bool serveFiles; if (Boolean.TryParse(args.Value, out serveFiles)) { //WebInterface.WebServer.ServeWebFiles = serveFiles; } break; #if TDSMServer case "send-queue-quota": int sendQueueQuota; if (Int32.TryParse(args.Value, out sendQueueQuota)) { Connection.SendQueueQuota = sendQueueQuota; } break; case "overlimit-slots": int overlimitSlots; if (Int32.TryParse(args.Value, out overlimitSlots)) { Server.OverlimitSlots = overlimitSlots; } break; #endif case "pid-file": ProcessPIDFile(args.Value); break; case "cheat-detection": bool cheatDetection; if (Boolean.TryParse(args.Value, out cheatDetection)) { EnableCheatProtection = cheatDetection; } break; case "log-rotation": bool logRotation; if (Boolean.TryParse(args.Value, out logRotation)) { ProgramLog.LogRotation = logRotation; } break; case "server-side-characters": bool serverSideCharacters; if (Boolean.TryParse(args.Value, out serverSideCharacters)) { Terraria.Main.ServerSideCharacter = serverSideCharacters; } break; case "tdsm-server-core": bool runServerCore; if (Boolean.TryParse(args.Value, out runServerCore)) { RunServerCore = runServerCore; } break; } }
void OnPlayerWorldAlteration(ref HookContext ctx, ref HookArgs.PlayerWorldAlteration args) { Vector2 Position = new Vector2(args.X, args.Y); if (args.TileWasPlaced && args.Type == SelectorItem && selection.isInSelectionlist(ctx.Player) && ctx.Player.Op) { ctx.SetResult(HookResult.RECTIFY); SelectorPos = !SelectorPos; Vector2[] mousePoints = selection.GetSelection(ctx.Player); if (!SelectorPos) mousePoints[0] = Position; else mousePoints[1] = Position; selection.SetSelection(ctx.Player, mousePoints); ctx.Player.sendMessage(String.Format("You have selected block at {0},{1}, {2} position", Position.X, Position.Y, (!SelectorPos) ? "First" : "Second"), ChatColor.Green); return; } foreach (Region rgn in regionManager.Regions) { if (rgn.HasPoint(Position)) { if (IsRestrictedForUser(ctx.Player, rgn, ((args.TileWasRemoved || args.WallWasRemoved) ? TileBreak : TilePlace))) { ctx.SetResult(WorldAlter); ctx.Player.sendMessage("You cannot edit this area!", ChatColor.Red); return; } } } }
void OnNPCSpawned(ref HookContext ctx, ref HookArgs.NPCSpawn args) { if (StopNPCSpawning) ctx.SetResult(HookResult.IGNORE); }
void OnDoorStateChange(ref HookContext ctx, ref HookArgs.DoorStateChanged args) { foreach (Region rgn in regionManager.Regions) { if (rgn.HasPoint(new Vector2(args.X, args.Y))) { if (ctx.Sender is Player) { Player player = ctx.Sender as Player; if (IsRestrictedForUser(player, rgn, DoorChange)) { ctx.SetResult(HookResult.RECTIFY); player.sendMessage("You cannot edit this area!", ChatColor.Red); return; } } else if (ctx.Sender is NPC) { if (rgn.RestrictedNPCs) { ctx.SetResult(HookResult.RECTIFY); //[TODO] look into RECIFYing for NPC's, They don't need to be resent, only cancelled, IGRNORE? return; } } } } }
void OnPlayerJoin(ref HookContext ctx, ref HookArgs.PlayerEnteredGame args) { //The player may randomly disconnect at any time, and if it's before the point of saving then the data may be lost. //So we must ensure the data is saved. //ServerCharacters.CharacterManager.EnsureSave = true; }
void OnLiquidFlowReceived(ref HookContext ctx, ref HookArgs.LiquidFlowReceived args) { Vector2 Position = new Vector2(args.X, args.Y); foreach (Region rgn in regionManager.Regions) { if (rgn.HasPoint(Position)) { if (IsRestrictedForUser(ctx.Player, rgn, LiquidFlow)) { ctx.SetResult(HookResult.ERASE); ctx.Player.sendMessage("You cannot edit this area!", ChatColor.Red); return; } } } }
void OnStartCommandProcessing(ref HookContext ctx, ref HookArgs.StartCommandProcessing args) { ctx.SetResult(HookResult.IGNORE); (new tdsm.api.Misc.ProgramThread("Command", ListenForCommands)).Start(); }
void OnPlayerEnteredGame(ref HookContext ctx, ref HookArgs.PlayerEnteredGame args) { /* If a player left without finishing the region, Clear it or the next player can use it. */ if (selection.isInSelectionlist(ctx.Player)) selection.RemovePlayer(ctx.Player); }