0

XNA を使用する SteamRE ボットを作成しようとしています。今、新しい XNA プロジェクトを作成し、SteamRE のサンプル コードを平手打ちしました。実行すると正常に動作しますが、実際には動作しません。実際に XNA ウィンドウを起動するため、キーボード操作などを探すことができません。

while(true) ループを削除すると機能しますが、接続しません。

これが私のコードです。皆さんがこれを見て、おそらく私を助けてくれれば、それは素晴らしいことです.

namespace Steam
{
  public class Game1 : Microsoft.Xna.Framework.Game
  {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    string userName = "username";
    string passWord = "password";

    SteamClient steamClient = new SteamClient(); // initialize our client
    SteamUser steamUser;
    SteamFriends steamFriends;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        steamUser = steamClient.GetHandler<SteamUser>(); // we'll use this later to logon
        steamFriends = steamClient.GetHandler<SteamFriends>();

    }

    protected override void UnloadContent()
    {
    }

    KeyboardState oldState = Keyboard.GetState();

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        ConnectSteam();

        base.Update(gameTime);
    }

    public void ConnectSteam() 
    {
        steamClient.Connect(); // connect to the steam network

        while (true)
        {
            CallbackMsg msg = steamClient.WaitForCallback(true); // block and wait until a callback is posted

            msg.Handle<SteamClient.ConnectCallback>(callback =>
            {
                // the Handle function will call this lambda method for this callback

                if (callback.Result != EResult.OK)
                {
                    Console.WriteLine("Failed. 1");
                }
                //break; // the connect result wasn't OK, so something failed

                // we've successfully connected to steam3, so lets logon with our details
                Console.WriteLine("Connected to Steam3.");
                steamUser.LogOn(new SteamUser.LogOnDetails
                {
                    Username = userName,
                    Password = passWord,
                });
                Console.WriteLine("Logged on.");
            });
            msg.Handle<SteamUser.LogOnCallback>(callback =>
            {
                if (callback.Result != EResult.OK)
                {
                    Console.WriteLine("Failed. 2");
                }

                // we've now logged onto Steam3
            });
        }
    }

    public void ConnectToFPP() 
    {
        KeyboardState newState = Keyboard.GetState();  // get the newest state

        // handle the input
        if (oldState.IsKeyUp(Keys.Space) && newState.IsKeyDown(Keys.Space))
        {
            steamFriends.JoinChat(110338190871147670);
        }

        oldState = newState;  // set the new state as the old state for next time
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        base.Draw(gameTime);
    }
  }
  }

ありがとう!

4

3 に答える 3

0

私は XNA の専門家ではありませんが、非シーンを処理する必要があるウィンドウのメソッドwhile(true)内でループを使用していることがわかりますがUpdate()、ウィンドウ コントロールの更新 (実際には、シーンが更新されても、この観点からは何も変更されません)。

サービスに接続しようとして無限ループに陥ります。ウィンドウが作成された同じスレッドで実行するため、ウィンドウがブロックされます。マルチスレッドを使用することをお勧めしますので、別のスレッドで接続しようとしているコードを実行してください。ThreadPool.QueueUserWorkItemスレッドを実行する他の方法を使用し、プログラムが接続しているユーザー情報を提供します。

于 2012-04-07T21:46:47.700 に答える
0

他の人が指摘したように、問題はwhile(true)フラグメントにあります。接続に成功しても、ループから抜け出すことはできません。接続後に問題が修正されてもbreak;、接続が確立されるまでプロセス全体の実行がブロックされます。

最善の解決策は、while を削除し、フレームごとに 1 回 (または 1 秒、または非同期呼び出しで使用する待機時間の 2 倍)、接続が確立されていない場合は、1 回だけ再接続を試みることです。接続コードは非同期であるため、ゲームは循環し続け、画面に描画し続け、状況をユーザーに通知できます。

protected override void Update (GameTime gameTime)
{
    // ...
    if (!connected && !tryingToConnect)
    {
        ConnectSteam();

        // Remember to set to false once connection is established
        showConnectingDialog = true;
    } else
    {

    }
    // ...
}

protected override void Draw (GameTime gameTime)
{
    // ...
    if (showConnectingDialog)
    {
        SpriteBatch.DrawString(SpriteFont, "Connecting to Steam servers...", Vector2.Zero, Color.White);
    } else
    {
        // ...
    }
}
于 2012-04-09T14:30:54.350 に答える
0

私が見る限り、問題は、常に接続する必要がない限り、実際に接続するたびに ConnectSteam コードから抜け出す必要があることです。

Console.WriteLine("Logged on.");
break;

または、 isConnected 変数を使用することもできます (非常に疑似コードっぽい):

bool isConnected = false
while(!isConnected)
{
    isConnected = LogonCode;

}
于 2012-04-07T22:01:20.453 に答える