-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstd_io.cpp
More file actions
91 lines (69 loc) · 2.24 KB
/
Copy pathstd_io.cpp
File metadata and controls
91 lines (69 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// std_io.cpp
// std_io
//
// Created by Oded Streigold on 4/18/20.
//
#include "std_io.h"
#include <iostream>
#include <queue>
int STD_IO::start_process(String path_godot_str)
{
std::wstring path_wstring = path_godot_str.c_str();
std::string path_string( path_wstring.begin(), path_wstring.end() );
if(this->process != nullptr)
{
stop_process();
}
this->process = new Process(path_string, "",
[this](const char *bytes, size_t n)
{
//cout << "Output from stdout: " << string(bytes, n);
string output_str(bytes, n);
String output_godot_str( output_str.c_str() );
Variant v(output_godot_str);
Variant** args = new Variant*[1];
args[0] = {&v};
Variant::CallError err;
this->godot_output_callback->call_func( (const Variant**)args, 1, err);
delete[] args;
},
[this](const char *bytes, size_t n)
{
//cout << "Output from stderr: " << string(bytes, n);
}, true);
return 0;
}
void STD_IO::set_stdout_callback(Ref<FuncRef> callback )
{
this->godot_output_callback = callback ;
}
std::string STD_IO::godot_string_std_string(String godot_string)
{
std::wstring std_wstring = godot_string.c_str();
std::string std_string( std_wstring.begin(), std_wstring.end() );
return std_string;
}
int STD_IO::send_command(String command){
if(this->process == nullptr)
return -1;
std::string std_str_command = godot_string_std_string(command);
this->process->write(std_str_command + "\n");
return 0;
}
int STD_IO::stop_process(){
if(this->process == nullptr)
return -1;
this->process->kill();
delete(this->process);
this->process = nullptr;
return 0;
}
void STD_IO::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_stdout_callback", "callback"), &STD_IO::set_stdout_callback);
ClassDB::bind_method(D_METHOD("start_process", "path_godot_str"), &STD_IO::start_process);
ClassDB::bind_method(D_METHOD("send_command", "command"), &STD_IO::send_command);
ClassDB::bind_method(D_METHOD("stop_process"), &STD_IO::stop_process);
}
STD_IO::STD_IO() {
}