예제 #1
0
        public ClientModule(System.Net.IPAddress host, int port, ClientGame parentGame)
        {
            netSession = new Network.ClientSession(0, new System.Net.IPEndPoint(host, port),
                    parentGame.Main.ScriptConsole.Write);

                netSession.onDatagramReceived += (data) =>
                {
                    var gram = new Network.ReadOnlyDatagram(data);
                    while (gram.More)
                    {
                        uint messageCode = 0;
                        gram.ReadUInt(out messageCode, 8);

                        switch (messageCode)
                        {
                            case 0:
                                if (simulation != null) throw new InvalidProgramException();
                                {
                                    String episodeName;
                                    uint version;
                                    gram.ReadString(out episodeName);
                                    gram.ReadUInt(out version, 32);
                                    parentGame.StartSimulation(episodeName, version);
                                }
                                break;
                            case 1:
                                {
                                    UInt32 entityID;
                                    UInt32 syncID;
                                    UInt32 dataLength;
                                    gram.ReadUInt(out entityID, 32);
                                    gram.ReadUInt(out syncID, 8);
                                    gram.ReadUInt(out dataLength, 32);
                                    var bytes = new byte[dataLength];
                                    gram.ReadBytes(bytes, dataLength);

                                    if (syncables.ContainsKey(entityID))
                                        foreach (var syncable in syncables[entityID])
                                            //if (syncable.SyncID == syncID)
                                                syncable.ReadFullSync(new ReadOnlyDatagram(bytes));
                                }
                                break;
                            case 2:
                                {
                                    UInt32 length;
                                    gram.ReadUInt(out length, 32);
                                    byte[] message = new byte[length];
                                    gram.ReadBytes(message, length);
                                    string messageID = null;
                                    MISP.ScriptList messageData = null;
                                    ScriptMessage.DecodeMessage(message, out messageID, out messageData);
                                    simulation.EnqueueEvent(messageID, messageData);
                                }
                                break;
                        }
                    }
                };

                parentGame.Main.Write("Connected to server " + host + " on port " + port + "\n");
        }
예제 #2
0
파일: Main.cs 프로젝트: hvp/Gemgine
        protected override void LoadContent()
        {
            ScriptEngine = new MISP.Console(s => this.ScriptConsole.Write(s), null);
            ScriptConsole = new Console.VirtualConsole(command =>
            {
                InjectAction(() =>
                {
                    try
                    {
                        ScriptEngine.ExecuteCommand(command);
                    }
                    catch (Exception e)
                    {
                        ScriptConsole.Write(e.Message);
                    }
                });
            },
            GraphicsDevice, new ContentManager(Services, "Content"));

            Write("Created MISP environment.\n");

            ScriptEngine.environment.engine.AddGlobalVariable("game", (context) => { return this.activeGame; });

            ScriptEngine.environment.engine.AddFunction("start-server-session", "Start a server session on the specified port with the specified episode.",
                (context, arguments) =>
                {
                    var port = arguments[0] as int?;
                    if (port == null || !port.HasValue) throw new InvalidOperationException("Port must be an integer.");
                    Game = new ServerGame(arguments[1].ToString(), port.Value);
                    return Game;
                }, MISP.Arguments.Arg("port"), MISP.Arguments.Arg("episode"));

            ScriptEngine.environment.engine.AddFunction("start-client-session", "Connect to a server at a specified address and port.",
                (context, arguments) =>
                {
                    var address = arguments[0].ToString();
                    var port = arguments[1] as int?;
                    if (port == null || !port.HasValue) throw new InvalidOperationException("Port must be an integer.");
                    Game = new ClientGame(System.Net.IPAddress.Parse(address), port.Value);
                    return Game;
                }, MISP.Arguments.Arg("address"), MISP.Arguments.Arg("port"));

            if (!String.IsNullOrEmpty(startupCommand))
            {
                System.Console.WriteLine(startupCommand);
                InjectAction(() => { ScriptEngine.ExecuteCommand(startupCommand); });
            }

            Input.textHook.KeyDown += (hook, args) =>
                {
                    if (consoleOpen)
                        ScriptConsole.KeyDown(args.KeyCode, args.KeyValue);
                };

            Input.textHook.KeyUp += (hook, args) =>
                {
                    //if (consoleOpen)
                    ScriptConsole.KeyUp(args.KeyCode, args.KeyValue);
                };

            Input.textHook.KeyPress += (hook, args) =>
                {
                    if (args.KeyChar == '~')
                        consoleOpen = !consoleOpen;
                    else if (consoleOpen)
                        ScriptConsole.KeyPress(args.KeyChar);
                };

            ScriptEngine.environment.engine.AddFunction("clear", "Clear the console",
                (context, arguments) =>
                {
                    ScriptConsole.Clear();
                    return null;
                });

            this.Game = new LocalGame();
        }