2

To keep it short: I am making a server program for a game that I am developping. To control the server I want to make use of commands. Something like "/Server Start 5555". These commands are send to the server in string format. This is my code:

ServerCommandsClass ServerCommands = new ServerCommandsClass();
Type Service = typeof(ServerCommandsClass);
MethodInfo method = Service.GetMethod(Signals[Count].MessageArray[0]);

List<object> ObjectList = new List<object>();
for (int i = 1; i < Signals[Count].MessageArray.Length; i++)
{
    ObjectList.Add(Signals[Count].MessageArray[i]);
}

string result = Convert.ToString(method.Invoke(ServerCommands, ObjectList.ToArray()));

It looks messy, because it is...

Basically what I got is a class (SignalClass) which stores new messages/commands. First I determine if a message is a command. If so, this code is ran. My MessageArray contains the message word by word using the string.split function.

This works, but I feel this is a bit of a work-around.

What I want is a command infrastructure (somewhat like in minecraft) which is easy to adjust to my needs. Like in minecraft you can type: "/tp 1000 64 1000" and "/tp someplayer 1000 64 1000". In mine you can't due to the limitations of my code. I would have to make a new method "tp2" or something to be able to take that extra parameter.

I hope someone can help me with this problem, because it's very annoying.

b.t.w. I used .Net 4.5.1 to programm this. And if I'm a little vague somewhere, just ask me and I will clarify.

Dion Dokter

4

4 に答える 4

0

もちろん、問題は投稿したコードではなく、呼び出しているメソッドにあります。あなたがやろうとしていることは、基本的にオーバーロードです。

/tp x y z

/tp someplaya x y z

String.Substring(0) および String.Substring(0, 5) と同じです。私のドリフトをキャッチする場合。

したがって、あなたのメソッドは次のようになると思います。

public static void Teleport(params object[])
{
   //here you validate your parameters and take decisions
   //e.g.
   //if(params[0] is String && params[1] is Int32)...
}
于 2013-10-25T12:56:08.287 に答える
0

私の 2 セントは、分割文字がコマンドになると String.Split が少し面倒であることに同意したので、XML 構造を使用すると言います。

<Command>
<CommandName value="">
<Arg1 value="">
<Arg2 value="">etc...
</Command>

また、ゲームによっては (同じサーバーに接続するマルチプレイヤー?) 複数のポートを使用し、同じクライアントであっても、ゲーム内のさまざまな機能に複数のポートを使用できます (ゲームがそれを保証するほど複雑な場合)。これは、コマンド ロジックを分離するのに役立ちます。

しかし、最終的には、受信したコマンドを解析することに行き詰まります。意味のある文字列名を持つゲーム スティックのデバッグ段階では、展開前にこれらを同じ数バイトに削減できます。

また、ユーザーの text-entry-as-command の決定に関しては、あなたのようなコマンド/tp、次に String.Split で、最初のパラメーターを強制的に必須パラメーターにし、次にオプションのパラメーターにすることをお勧めします。個人的な注意として、入力されたコマンドはパラメータのリストを表示する必要があり、ユーザーはそれを「タイプオーバー」してそれらを支援します

于 2013-10-25T12:57:13.610 に答える
0

私の提案は次のとおりです。文字列を使用しないでください。

必要に応じて、シリアル化を使用できます。したがって、任意のオブジェクトを送信できます。また、問題が異なる数のパラメーターを送信できるようにしたい場合 (ここで何をしているのかを推測するのは少し難しいです)、params-キーワードを使用します

のように

public void DoSomething (string command, params string[] paramters) {
// Whatever
}
于 2013-10-25T12:58:09.890 に答える