Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_popen() Pipe crashes / response is nullptr when using the sim. #10118

Open
RyDawgE opened this issue Jan 16, 2024 · 3 comments
Open

_popen() Pipe crashes / response is nullptr when using the sim. #10118

RyDawgE opened this issue Jan 16, 2024 · 3 comments

Comments

@RyDawgE
Copy link

RyDawgE commented Jan 16, 2024

This might be a C++ problem more than it is a sim problem, but I cannot for the life of me figure out what's happening here.

I have the following c++ code:
Don't mind the strange syntax. I'm using Unreal Engine, which has some differences over C++ (Maybe that's the issue too)

#include "CmdStream.h"

#include <sstream>
#include <stdexcept>
#include <array>

#include <iostream>
#include <cstdio>
#include <cstring>

UCmdStream::UCmdStream() {
    return;
}

void UCmdStream::Start()
{
    pipe = _popen("node C:\\Users\\RyDaw\\Documents\\pokemon-showdown-master\\pokemon-showdown simulate-battle", "w");
}

void UCmdStream::SendCommand(FString input)
{
    FString command = input;
    std::string message = std::string(TCHAR_TO_UTF8(*command));
    fwrite(message.c_str(), sizeof(char), message.size(), pipe);

    ReceiveResponse();

    return;
}

FString UCmdStream::ReceiveResponse()
{
    FString Fs = "Error";
    char buffer[128];
    while (fgets(buffer, 128, pipe) != nullptr) {
        Fs = FString(UTF8_TO_TCHAR(buffer));
        GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, Fs);
    }
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Response equals nullptr"));

    return Fs;
}

When opening the pipe, a blank cmd window opens. I'd imagine this is normal behavior, but when running

SendCommand(">start{\"formatid\":\"gen7randombattle\"}")

nothing happens. The response() function resolves to nullptr.

When trying to open the pipe with the command listed in .\sim\SIMULATOR.md :

The stream can also be accessed from other programming languages using standard IO.
In this case, you would clone the repository, and then run:

echo '>start {"formatid":"gen7randombattle"}
>player p1 {"name":"Alice"}
>player p2 {"name":"Bob"}
' | ./pokemon-showdown simulate-battle

The command window opens for a few seconds but then automatically closes. That command also has the same result when pasted straight into a CMD window.

My above code was written assuming that the "standard IO" thing is referring to things like iostream.
I'm absolutely sure this is an issue on my part, but I think the SIMULATOR.md example should be more descriptive.

Can anyone help me figure out what's going on here?

@Zarel
Copy link
Member

Zarel commented Jan 18, 2024

I can't tell exactly what the problem is, but my guess is that you did not write a trailing \n.

❯ echo '>start {"formatid":"gen7randombattle"}
❯ ' | ./pokemon-showdown simulate-battle
update
|t:|1705554263
|gametype|singles

❯ echo '>start {"formatid":"gen7randombattle"}' | ./pokemon-showdown simulate-battle

@Zarel
Copy link
Member

Zarel commented Jan 18, 2024

In what way would you prefer it to be more descriptive? I wrote some more words in 172c495 but maybe you'd have a better idea.

@RyDawgE
Copy link
Author

RyDawgE commented Jan 31, 2024

I apologize for taking so long to respond.. I appreciate those changes you made. Unfortunately it doesn't appear to be an issue with trailing \n. I've discovered an alternative to getting the sim to run with Pipes in Unreal directly using a Node JS runtime built into the engine with a plugin... so I'm no longer concerned. Regardless, thank you for the response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants