Example #1
0
        public void SendEntityCommand(EntityCommand cmd)
        {
            Guid cmdID = Guid.NewGuid();

            CommandsAwaitingServerAproval.Add(cmdID, cmd);

            NetOutgoingMessage sendMsg = NetPeerObject.CreateMessage();

            var mStream = new MemoryStream();

            SerializationManager.Export(Game, mStream, cmd);
            byte[] byteArray = mStream.ToArray();
            int    len       = byteArray.Length;

            sendMsg.Write((byte)ToServerMsgType.SendPlayerEntityCommand);
            sendMsg.Write(cmdID.ToByteArray());
            sendMsg.Write(cmd.GetType().Name);
            sendMsg.Write(len);
            sendMsg.Write(byteArray);

            NetClientObject.SendMessage(sendMsg, NetClientObject.ServerConnection, NetDeliveryMethod.ReliableOrdered);
            Messages.Add("Sent Command " + cmd.CmdID);
        }
        /// <summary>Creates a new command combining deferred queries.</summary>
        /// <returns>The combined command created from deferred queries.</returns>
        protected DbCommand CreateCommandCombined()
        {
            var command = Context.CreateStoreCommand();

            var sb         = new StringBuilder();
            var queryCount = 1;

            foreach (var query in Queries)
            {
                // GENERATE SQL
#if EF5
                var sql        = query.Query.ToTraceString();
                var parameters = query.Query.Parameters;

                // UPDATE parameter name
                foreach (var parameter in parameters)
                {
                    var oldValue = parameter.Name;
                    var newValue = string.Concat("Z_", queryCount, "_", oldValue);

                    // CREATE parameter
                    var dbParameter = command.CreateParameter();
                    dbParameter.ParameterName = newValue;
                    dbParameter.Value         = parameter.Value;
                    command.Parameters.Add(dbParameter);

                    // REPLACE parameter with new value
                    sql = sql.Replace("@" + oldValue, "@" + newValue);
                }
#elif EF6
                var objectQuery                = query.Query;
                var stateField                 = objectQuery.GetType().BaseType.GetField("_state", BindingFlags.NonPublic | BindingFlags.Instance);
                var state                      = stateField.GetValue(objectQuery);
                var getExecutionPlanMethod     = state.GetType().GetMethod("GetExecutionPlan", BindingFlags.NonPublic | BindingFlags.Instance);
                var getExecutionPlan           = getExecutionPlanMethod.Invoke(state, new object[] { null });
                var prepareEntityCommandMethod = getExecutionPlan.GetType().GetMethod("PrepareEntityCommand", BindingFlags.NonPublic | BindingFlags.Instance);

                string sql = "";
                using (EntityCommand entityCommand = (EntityCommand)prepareEntityCommandMethod.Invoke(getExecutionPlan, new object[] { objectQuery.Context, objectQuery.Parameters }))
                {
                    var getCommandDefinitionMethod = entityCommand.GetType().GetMethod("GetCommandDefinition", BindingFlags.NonPublic | BindingFlags.Instance);
                    var getCommandDefinition       = getCommandDefinitionMethod.Invoke(entityCommand, new object[0]);

                    var prepareEntityCommandBeforeExecutionMethod = getCommandDefinition.GetType().GetMethod("PrepareEntityCommandBeforeExecution", BindingFlags.NonPublic | BindingFlags.Instance);
                    var prepareEntityCommandBeforeExecution       = (DbCommand)prepareEntityCommandBeforeExecutionMethod.Invoke(getCommandDefinition, new object[] { entityCommand });

                    sql = prepareEntityCommandBeforeExecution.CommandText;
                    var parameters = prepareEntityCommandBeforeExecution.Parameters;

                    // UPDATE parameter name
                    foreach (DbParameter parameter in parameters)
                    {
                        var oldValue = parameter.ParameterName;
                        if (oldValue.StartsWith("@"))
                        {
                            oldValue = oldValue.Substring(1);
                        }
                        var newValue = string.Concat("Z_", queryCount, "_", oldValue);

                        // CREATE parameter
                        var dbParameter = command.CreateParameter();
                        dbParameter.ParameterName = newValue;
                        dbParameter.Value         = parameter.Value;
                        command.Parameters.Add(dbParameter);

                        // REPLACE parameter with new value
                        sql = sql.Replace("@" + oldValue, "@" + newValue);
                    }
                }
#elif EFCORE
                RelationalQueryContext queryContext;
                var queryCommand = query.CreateExecutorAndGetCommand(out queryContext);
                var sql          = queryCommand.CommandText;
                var parameters   = queryCommand.Parameters;

                // UPDATE parameter name
                foreach (var parameter in queryContext.ParameterValues)
                {
                    var oldValue = parameter.Key;
                    var newValue = string.Concat("Z_", queryCount, "_", oldValue);

                    // CREATE parameter
                    var dbParameter = command.CreateParameter();
                    dbParameter.ParameterName = newValue;
                    dbParameter.Value         = parameter.Value;
                    command.Parameters.Add(dbParameter);

                    // REPLACE parameter with new value
                    sql = sql.Replace("@" + oldValue, "@" + newValue);
                }
#endif



                sb.AppendLine(string.Concat("-- EF+ Query Future: ", queryCount, " of ", Queries.Count));
                sb.AppendLine(sql);
                sb.Append(";"); // SQL Server, SQL Azure, MySQL
                sb.AppendLine();
                sb.AppendLine();

                queryCount++;
            }

            command.CommandText = sb.ToString();

            return(command);
        }