Advertisement
Guest User

TouchConsole Pro Basic Command Security System

a guest
Jan 2nd, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Opencoding.CommandHandlerSystem;
  2. using Opencoding.Console;
  3. using UnityEngine;
  4.  
  5. namespace Opencoding.Demo.CommandSecurity
  6. {
  7.     /// <summary>
  8.     /// This class implements a very basic security system. It's not intended to stand up to any real
  9.     /// attempt to hack the game, it's more to just an example of how you could protect yourself from
  10.     /// your testers executing commands that might mess up internal tests, public betas etc.
  11.     /// You could modify this to implement a more advanced system if you wish!
  12.     /// </summary>
  13.     public class ConsoleCommandSecurity : MonoBehaviour
  14.     {
  15.         private bool _authenticated = false;
  16.         private CommandSecurityPopup _commandSecurityPopup;
  17.         public const string PASSWORD = "password"; // You might want something slightly more secure!
  18.         public bool _SavePassword = false; // If this is set, the password is saved in PlayerPrefs.
  19.  
  20.         private void Awake()
  21.         {
  22.             // This hook is called just before each command is executed. If the attached method returns
  23.             // true, then the command is executed, otherwise it isn't.
  24.             CommandHandlers.BeforeCommandExecutedHook += BeforeCommandExecuted;
  25.             DontDestroyOnLoad(gameObject);
  26.  
  27.             if (_SavePassword)
  28.             {
  29.                 if (PlayerPrefs.GetString("Opencoding.Console.ConsoleCommandSecurity.Password") == PASSWORD)
  30.                     _authenticated = true;
  31.             }
  32.         }
  33.  
  34.         private void OnGUI()
  35.         {
  36.             if(_commandSecurityPopup != null)
  37.                 _commandSecurityPopup.OnGUI();
  38.         }
  39.  
  40.         private bool BeforeCommandExecuted(CommandHandler commandHandler, string[] strings)
  41.         {
  42.             if (_authenticated)
  43.                 return true; // already authenticated - execute the command as normal
  44.  
  45.             // Check to see if the CommandHandler method or property has the SecureCommandHandler attribute on it
  46.             bool isSecureCommandHandler =
  47.                 commandHandler.MemberInfo.GetCustomAttributes(typeof (SecureCommandHandlerAttribute), false).Length != 0;
  48.            
  49.             if (!isSecureCommandHandler)
  50.                 return true; // not a 'secured' command - execute the command as normal
  51.  
  52.             if (_commandSecurityPopup == null)
  53.             {
  54.                 _commandSecurityPopup = new CommandSecurityPopup(this, commandHandler, strings);
  55.             }
  56.             return false; // prevent the command executing
  57.         }
  58.  
  59.         public void PopupClosed(bool authenticated)
  60.         {
  61.             _commandSecurityPopup = null;
  62.             _authenticated = authenticated;
  63.         }
  64.     }
  65.  
  66.     /// <summary>
  67.     /// This attribute prevents users from being able to execute a command without entering a password.
  68.     /// The ConsoleCommandSecurity behaviour must exist in the scene for this to work.
  69.     /// </summary>
  70.     [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false)]
  71.     public class SecureCommandHandlerAttribute : Attribute
  72.     {
  73.        
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement