示例#1
0
            public LispDelegate(LispEventProducer prod, object target, EventInfo method)
                //: base(target, method.Name)
            {
                Target = target;
                Producer = prod;
                Method = method;
                Handler = Invoke;

                Type t = target.GetType();
                // In order to create a method to handle the Elapsed event,
                // it is necessary to know the signature of the delegate 
                // used to raise the event. Reflection.Emit can then be
                // used to construct a dynamic class with a static method
                // that has the correct signature.

                // Get the event handler type of the Elapsed event. This is
                // a delegate type, so it has an Invoke method that has
                // the same signature as the delegate. The following code
                // creates an array of Type objects that represent the 
                // parameter types of the Invoke method.
                //    
                Type handlerType = method.EventHandlerType;
                MethodInfo invokeMethod = handlerType.GetMethod("Invoke");
                ParameterInfo[] parms = invokeMethod.GetParameters();
                Type[] parmTypes = new Type[parms.Length];
                for (int i = 0; i < parms.Length; i++)
                {
                    parmTypes[i] = parms[i].ParameterType;
                }
                if (false)
                {
                    // Use Reflection.Emit to create a dynamic assembly that
                    // will be run but not saved. An assembly must have at 
                    // least one module, which in this case contains a single
                    // type. The only purpose of this type is to contain the 
                    // event handler method. (In the .NET Framework version 
                    // 2.0 you can use dynamic methods, which are simpler 
                    // because there is no need to create an assembly, module,
                    // or type.)
                    AssemblyName aName = new AssemblyName();
                    aName.Name = "DynamicTypes";
                    AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
                    ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
                    TypeBuilder tb = mb.DefineType("Handler", TypeAttributes.Class | TypeAttributes.Public);
                    var localHandler = this.GetType().GetMethod("Invoke");

                    ConstructorBuilder Ctor = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard,
                                                                   null);
                    {
                        ILGenerator ilc = Ctor.GetILGenerator();
                        ilc.Emit(OpCodes.Ldarg_0);
                        ilc.Emit(OpCodes.Call, typeof (object).GetConstructor(new Type[0]));
                        ilc.Emit(OpCodes.Ret);
                    }
                    // Create the method that will handle the event. The name
                    // is not important. The method is static, because there is
                    // no reason to create an instance of the dynamic type.
                    //
                    // The parameter types and return type of the method are
                    // the same as those of the delegate's Invoke method, 
                    // captured earlier.
                    MethodBuilder handler = tb.DefineMethod("DynamicHandler",
                                                            MethodAttributes.Public | MethodAttributes.Static,
                                                            invokeMethod.ReturnType, parmTypes);

                    // Generate code to handle the event.
                    //
                    ILGenerator il = handler.GetILGenerator();
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldarg_1);
                    il.EmitCall(OpCodes.Call, localHandler, new Type[] {typeof (object), typeof (EventArgs)});
                    il.Emit(OpCodes.Ret);

                    // CreateType must be called before the Handler type can
                    // be used. In order to create the delegate that will
                    // handle the event, a MethodInfo from the finished type
                    // is required.
                    Type finished = tb.CreateType();
                    eventHandler = finished.GetMethod("DynamicHandler");
                }
                if (true)
                {
                    handlerType = Handler.GetType();
                    eventHandler = this.GetType().GetMethod("Invoke");
                }
                // Use the MethodInfo to create a delegate of the correct 
                // type, and call the AddEventHandler method to hook up 
                // the event.
                Delegate d = Delegate.CreateDelegate(handlerType, eventHandler);
                //typeof(object)
                //method.EventHandlerType.
                
                
                //method.AddEventHandler(target, d);

                // Late-bound calls to the Interval and Enabled property 
                // are required to enable the timer with a one-second
                // interval.
                //t.InvokeMember("Interval", BindingFlags.SetProperty, null, timer, new Object[] { 1000 });
                //t.InvokeMember("Enabled", BindingFlags.SetProperty, null, timer, new Object[] { true });

               // DLRConsole.WriteLine("Press the Enter key to end the program.");
                //Console.ReadLine();


                TheDelegate = d;
//                Delegate.CreateDelegate(method.EventHandlerType, this,
  //                                                    this.GetType().GetMethod("DynamicInvokeImpl"));
            }
示例#2
0
        /// <summary>
        /// 
        /// </summary>
        public BotClient(ClientManager manager, GridClient g)
        {
            OpenMetaverse.Utils.InternStrings = true;
            LoginRetries = LoginRetriesFresh;
            ClientManager = manager;
            ClientManager.LastRefBotClient = this;
            _gridClient = g;
            manager.AddBotClient(this);
            NeedRunOnLogin = true;
            //manager.LastRefBotClient = this;

            Settings.USE_INTERPOLATION_TIMER = false;
            Settings.LOG_LEVEL = Helpers.LogLevel.None;

            //   Settings.LOG_RESENDS = false;
            //   Settings.ALWAYS_DECODE_OBJECTS = true;
            //   Settings.ALWAYS_REQUEST_OBJECTS = true;
            //   Settings.SEND_AGENT_UPDATES = true;
            ////   Settings.SYNC_PACKETCALLBACKS = true;
            //   Settings.OBJECT_TRACKING = true;
            //   //Settings.STORE_LAND_PATCHES = true;
            //   //Settings.USE_TEXTURE_CACHE = true;
            //   //Settings.PARCEL_TRACKING = true;
            //   //Settings.FETCH_MISSING_INVENTORY = true;
            //   // Optimize the throttle
            //   Throttle.Wind = 0;
            //   Throttle.Cloud = 0;
            //   Throttle.Land = 1000000;
            //   Throttle.Task = 1000000;
            ////Throttle.Total = 250000;
            // Settings.CAPS_TIMEOUT = 6 * 1000;
            Settings.RESEND_TIMEOUT = 20 * 1000;
            Settings.MAX_RESEND_COUNT = 10;
            Settings.LOGIN_TIMEOUT = 120 * 1000;
            //Settings.LOGOUT_TIMEOUT = 120 * 1000;
            //Settings.SIMULATOR_TIMEOUT = int.MaxValue;
            //Settings.SEND_PINGS = true;
            Settings.SEND_AGENT_APPEARANCE = true;
            Settings.LOG_DISKCACHE = false;
            //Settings.USE_LLSD_LOGIN = true;
            ////Settings.MULTIPLE_SIMS = false;

            VoiceManager = new VoiceManager(gridClient);
            //manager.AddBotClientToTextForm(this);

            botPipeline = new SimEventMulticastPipeline(this);
            OneAtATimeQueue = new TaskQueueHandler(this, new NamedPrefixThing("OneAtATimeQueue", GetName), new TimeSpan(0, 0, 0, 0, 10),
                                                   true, true);
            AddTaskQueue("OneAtATimeQueue", OneAtATimeQueue);

            SetSecurityLevel(OWNERLEVEL, null, BotPermissions.Owner);
            ClientManager.PostAutoExecEnqueue(OneAtATimeQueue.Start);

            botPipeline.AddSubscriber(new SimEventTextSubscriber(SimEventWriteLine, this));
            // SingleInstance = this;
            ///this = this;// new GridClient();


            Settings.ALWAYS_DECODE_OBJECTS = true;
            Settings.ALWAYS_REQUEST_OBJECTS = true;
            Settings.OBJECT_TRACKING = true;
            Settings.AVATAR_TRACKING = true;
            Settings.STORE_LAND_PATCHES = true;
            Settings.CACHE_PRIMITIVES = true;
            Settings.POOL_PARCEL_DATA = true;


            //  Manager = Inventory;
            //Inventory = Manager.Store;

            // config = new Configuration();
            // config.loadConfig();
            /// Settings.LOGIN_SERVER = config.simURL;
            // Opensim recommends 250k total

            //Settings.ENABLE_CAPS = true;
            Self.Movement.Camera.Far = 512f;
            //Settings.LOG_ALL_CAPS_ERRORS = true;
            //Settings.FETCH_MISSING_INVENTORY = true;
            //Settings.SEND_AGENT_THROTTLE = false;

            //muteList = new List<string>();

            Plugins = new Dictionary<string, Cogbot.Listener>();
            //registrationTypes["avatars"] = new Cogbot.Avatars(this);
            //registrationTypes["chat"] = new Cogbot.Chat(this);
            WorldSystem = new Cogbot.WorldObjects(this);
            //registrationTypes["teleport"] = new Cogbot.Teleport(this);
            //registrationTypes["whisper"] = new Cogbot.Whisper(this);
            //ObjectSystem = new Cogbot.Objects(this);
            //registrationTypes["bump"] = new Cogbot.Bump(this);
            //registrationTypes["sound"] = new Cogbot.Sound(this);
            //registrationTypes["sound"] = new Cogbot.Objects(this);
            var gc = gridClient;
            //_gridClient = null;
            Commands = new SortedDictionary<string, CommandInstance>();
			RegisterCommand("login", new Login(this));
			RegisterCommand("logout", new Logout(this));
			RegisterCommand("stop", new StopCommand(this));
			RegisterCommand("teleport", new Teleport(this));
			var desc = newCommandInfo(new Describe(this));
			Commands["describe"] = desc;
			Commands["look"] = desc;
            RegisterCommand("say", new Cogbot.Actions.Communication.SayCommand(this));
			RegisterCommand("help", new Cogbot.Actions.System.Help(this));
            RegisterCommand("setmaster", new Cogbot.Actions.System.SetMasterKeyCommand(this));
            RegisterCommand("setmasterkey", new Cogbot.Actions.System.SetMasterKeyCommand(this));
			RegisterCommand("sit", new Sit(this));
			RegisterCommand("stand", new StandCommand(this));
			RegisterCommand("jump", new JumpCommand(this));
			RegisterCommand("crouch", new CrouchCommand(this));
			RegisterCommand("mute", new MuteCommand(this));
			RegisterCommand("unmute", new UnmuteCommand(this));
            RegisterCommand("move", new MoveToCommand(this)); 
            RegisterCommand("forward", new MoveToCommand(this));
			RegisterCommand("use", new Use(this));
			RegisterCommand("eval", new Eval(this));
			RegisterCommand("wear", new ReplaceOutfitCommand(this));
            RegisterCommand("task", new ThreadCommand(this));
            RegisterCommand("thread", new ThreadCommand(this));
            

            Commands["locate"] = Commands["location"] = Commands["where"] = newCommandInfo(new Actions.Movement.LocationCommand(this));
            var follow = newCommandInfo(new Follow(this));
            Commands["follow"] = follow;
            //Commands["simexport"] = new Cogbot.Actions.SimExport.ExportCommand(this);
            Commands["stop following"] = follow;
            Commands["stop-following"] = follow;

            // ensure all commands are registered when this constructor completes
            foreach (Type type in GetType().Assembly.GetTypes())
            {
                RegisterType(type);
            }
            
            _gridClient = gc;

            XmlInterp = new XmlScriptInterpreter(this);
            XmlInterp.BotClient = this;
            if (false)
                ClientManager.PostAutoExecEnqueue(LoadTaskInterpreter);

            // Start the server
            lock (ClientManager.config)
            {
                int poff = ClientManager.config.GetValue("tcpPortOffset", 10);
                thisTcpPort = ClientManager.nextTcpPort;
                ClientManager.nextTcpPort += poff;
                ClientManager.PostAutoExecEnqueue(() =>
                {
                    Utilities.BotTcpServer UtilitiesTcpServer = new Utilities.BotTcpServer(thisTcpPort, this);
                    UtilitiesTcpServer.ServerPortIncr = poff;
                    UtilitiesTcpServer.startSocketListener();
                    ClientManager.nextTcpPort = UtilitiesTcpServer.ServerPort + UtilitiesTcpServer.ServerPortIncr;
                    this.OnDispose += ((SimEventSubscriber)UtilitiesTcpServer).Dispose;
                });
            }

            Network.RegisterCallback(PacketType.AgentDataUpdate, AgentDataUpdateHandler);
            Network.RegisterCallback(PacketType.AlertMessage, AlertMessageHandler);
            Network.RegisterCallback(PacketType.AvatarAppearance, AvatarAppearanceHandler);

            //Move to effects Appearance.OnAppearanceUpdated += new AppearanceManager.AppearanceUpdatedCallback(Appearance_OnAppearanceUpdated);

            Inventory.InventoryObjectOffered += Inventory_OnInventoryObjectReceived;
            Groups.GroupMembersReply += new EventHandler<GroupMembersReplyEventArgs>(GroupMembersHandler);
            Logger.OnLogMessage += new Logger.LogCallback(client_OnLogMessage);
            Network.EventQueueRunning += Network_OnEventQueueRunning;
            Network.LoginProgress += Network_OnLogin;
            Network.LoggedOut += Network_OnLogoutReply;
            Network.SimConnected += Network_OnSimConnected;
            Network.SimDisconnected += Network_OnSimDisconnected;
            //Network.OnConnected += Network_OnConnected;
            Network.Disconnected += Network_OnDisconnected;
            Self.IM += Self_OnInstantMessage;
            //Self.OnScriptDialog += new AgentManager.ScriptDialogCallback(Self_OnScriptDialog);
            //Self.OnScriptQuestion += new AgentManager.ScriptQuestionCallback(Self_OnScriptQuestion);
            Self.TeleportProgress += Self_OnTeleport;
            Self.ChatFromSimulator += Self_OnChat;
            var callback = new EventHandler<CurrentGroupsEventArgs>(Groups_OnCurrentGroups);
            Groups.CurrentGroups += callback;

            BotInventory = new BotInventoryEval(this);
            ClientManager.PostAutoExecEnqueue(() =>
            {
                if (UseLispEventProducer)
                {
                    lispEventProducer = new LispEventProducer(this, LispTaskInterperter);
                }
            });
            ClientManager.PostAutoExecEnqueue(ClientManager.EnsureREPLNotPaused);

        }
示例#3
0
            public LispDelegate(LispEventProducer prod, object target, EventInfo method)
            //: base(target, method.Name)
            {
                Target   = target;
                Producer = prod;
                Method   = method;
                Handler  = Invoke;

                Type t = target.GetType();
                // In order to create a method to handle the Elapsed event,
                // it is necessary to know the signature of the delegate
                // used to raise the event. Reflection.Emit can then be
                // used to construct a dynamic class with a static method
                // that has the correct signature.

                // Get the event handler type of the Elapsed event. This is
                // a delegate type, so it has an Invoke method that has
                // the same signature as the delegate. The following code
                // creates an array of Type objects that represent the
                // parameter types of the Invoke method.
                //
                Type       handlerType  = method.EventHandlerType;
                MethodInfo invokeMethod = handlerType.GetMethod("Invoke");

                ParameterInfo[] parms     = invokeMethod.GetParameters();
                Type[]          parmTypes = new Type[parms.Length];
                for (int i = 0; i < parms.Length; i++)
                {
                    parmTypes[i] = parms[i].ParameterType;
                }
                if (false)
                {
                    // Use Reflection.Emit to create a dynamic assembly that
                    // will be run but not saved. An assembly must have at
                    // least one module, which in this case contains a single
                    // type. The only purpose of this type is to contain the
                    // event handler method. (In the .NET Framework version
                    // 2.0 you can use dynamic methods, which are simpler
                    // because there is no need to create an assembly, module,
                    // or type.)
                    AssemblyName aName = new AssemblyName();
                    aName.Name = "DynamicTypes";
                    AssemblyBuilder ab           = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
                    ModuleBuilder   mb           = ab.DefineDynamicModule(aName.Name);
                    TypeBuilder     tb           = mb.DefineType("Handler", TypeAttributes.Class | TypeAttributes.Public);
                    var             localHandler = this.GetType().GetMethod("Invoke");

                    ConstructorBuilder Ctor = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard,
                                                                   null);
                    {
                        ILGenerator ilc = Ctor.GetILGenerator();
                        ilc.Emit(OpCodes.Ldarg_0);
                        ilc.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0]));
                        ilc.Emit(OpCodes.Ret);
                    }
                    // Create the method that will handle the event. The name
                    // is not important. The method is static, because there is
                    // no reason to create an instance of the dynamic type.
                    //
                    // The parameter types and return type of the method are
                    // the same as those of the delegate's Invoke method,
                    // captured earlier.
                    MethodBuilder handler = tb.DefineMethod("DynamicHandler",
                                                            MethodAttributes.Public | MethodAttributes.Static,
                                                            invokeMethod.ReturnType, parmTypes);

                    // Generate code to handle the event.
                    //
                    ILGenerator il = handler.GetILGenerator();
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldarg_1);
                    il.EmitCall(OpCodes.Call, localHandler, new Type[] { typeof(object), typeof(EventArgs) });
                    il.Emit(OpCodes.Ret);

                    // CreateType must be called before the Handler type can
                    // be used. In order to create the delegate that will
                    // handle the event, a MethodInfo from the finished type
                    // is required.
                    Type finished = tb.CreateType();
                    eventHandler = finished.GetMethod("DynamicHandler");
                }
                if (true)
                {
                    handlerType  = Handler.GetType();
                    eventHandler = this.GetType().GetMethod("Invoke");
                }
                // Use the MethodInfo to create a delegate of the correct
                // type, and call the AddEventHandler method to hook up
                // the event.
                Delegate d = Delegate.CreateDelegate(handlerType, eventHandler);

                //typeof(object)
                //method.EventHandlerType.


                //method.AddEventHandler(target, d);

                // Late-bound calls to the Interval and Enabled property
                // are required to enable the timer with a one-second
                // interval.
                //t.InvokeMember("Interval", BindingFlags.SetProperty, null, timer, new Object[] { 1000 });
                //t.InvokeMember("Enabled", BindingFlags.SetProperty, null, timer, new Object[] { true });

                // DLRConsole.WriteLine("Press the Enter key to end the program.");
                //Console.ReadLine();


                TheDelegate = d;
//                Delegate.CreateDelegate(method.EventHandlerType, this,
//                                                    this.GetType().GetMethod("DynamicInvokeImpl"));
            }