Stardew Valley (413150)

Đang mở
#10,357 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c
Lĩnh vực
cli, operating-systems

Hướng nghiên cứu

The bug is in programs/conhost/conhost.c: read_console records the edit line's home position, and write_console never updates it. Start by reading how update_read_output redraws the pending line, then build the attached console-test.c and check that its verify() report ends in PASS. The suggested patch is one approach, but a fix must keep the input line below output in both the Wine and Proton builds.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Game compatibility - Unofficial

Proton version: Proton Experimental, experimental-11.0-20261027b. The same code is in current upstream Wine (programs/conhost/conhost.c).
Game: Stardew Valley (413150) with SMAPI, whose console logs from one thread while another thread waits in ReadConsole for commands.

Symptoms

While a program waits in a cooked ReadConsole with echo and other output is written to the console meanwhile, the first key pressed redraws the input line at the row where the read started, far above the newest output, and moves the cursor there. All output written afterwards lands from that row downwards and overwrites the earlier history, while older lines remain below it. The console's text ends up out of order: the SMAPI console shows new log lines in the middle of old ones, and the cursor and the visible window jump back to an old position.

Reproduction

The program below writes numbered lines from a second thread while the main thread waits in ReadConsoleW, types into the pending read with WriteConsoleInputW between bursts of output, and then checks the whole screen buffer: every line once and in order, the typed text after the output, the cursor on it. Build with x86_64-w64-mingw32-gcc -O1 -o console-test.exe console-test.c, run it in a console window (wine start /wait console-test.exe), and read C:\contest-report.txt.

console-test.c
#include <windows.h>
#include <stdio.h>
static HANDLE out, in;
static FILE *report;
static int counter;
static void row_text(int y, int w, wchar_t *row) {
    DWORD n = 0; COORD at = {0, (SHORT)y};
    for (int i = 0; i < w; ++i) row[i] = L' ';
    ReadConsoleOutputCharacterW(out, row, w, at, &n); row[w] = 0;
    int len = w; while (len && row[len-1] == L' ') --len; row[len] = 0;
}
static void dump(const char *when) {
    CONSOLE_SCREEN_BUFFER_INFO i; GetConsoleScreenBufferInfo(out, &i);
    int w = i.dwSize.X, last = -1;
    wchar_t row[512];
    for (int y = 0; y < i.dwSize.Y && y < 3000; ++y) { row_text(y, w, row); if (row[0]) last = y; }
    fprintf(report, "%s: buffer %dx%d window %d-%d cursor %d,%d lastrow %d\n", when, w, i.dwSize.Y,
        i.srWindow.Top, i.srWindow.Bottom, i.dwCursorPosition.X, i.dwCursorPosition.Y, last);
    int probe[] = {i.dwCursorPosition.Y - 1, i.dwCursorPosition.Y, i.dwCursorPosition.Y + 1, last};
    for (int k = 0; k < 4; ++k) if (probe[k] >= 0) { row_text(probe[k], w, row); fprintf(report, "   row %d: %ls\n", probe[k], row); }
    fflush(report);
}
static void lines(int n) { for (int k = 0; k < n; ++k) { printf("line %04d of the test output\n", counter++); fflush(stdout); Sleep(3); } Sleep(300); }
static void type(const wchar_t *s) {
    for (; *s; ++s) {
        INPUT_RECORD r[2] = {0}; DWORD n;
        for (int d = 0; d < 2; ++d) { r[d].EventType = KEY_EVENT; r[d].Event.KeyEvent.bKeyDown = !d; r[d].Event.KeyEvent.wRepeatCount = 1;
            r[d].Event.KeyEvent.uChar.UnicodeChar = *s; r[d].Event.KeyEvent.wVirtualKeyCode = VkKeyScanW(*s) & 0xff; }
        WriteConsoleInputW(in, r, 2, &n); Sleep(50);
    }
    Sleep(300);
}
// Every output line is in the buffer once and in order, and the typed text sits after the output
// with the cursor at its end.
static void verify(void) {
    CONSOLE_SCREEN_BUFFER_INFO i; GetConsoleScreenBufferInfo(out, &i);
    int w = i.dwSize.X, expect = 0, bad = 0, last = -1; wchar_t row[512];
    for (int y = 0; y < 3000 && y < i.dwSize.Y; ++y) {
        row_text(y, w, row); if (!row[0]) continue; last = y;
        wchar_t *at = wcsstr(row, L"line "); if (!at) continue;
        int n = _wtoi(at + 5);
        if (n != expect) { fprintf(report, "   out of order at row %d: line %d, expected %d\n", y, n, expect); bad = 1; expect = n; }
        ++expect;
    }
    row_text(i.dwCursorPosition.Y, w, row);
    int typed_ok = !wcscmp(row, L"hel") && i.dwCursorPosition.X == 3 && i.dwCursorPosition.Y == last;
    int visible = i.dwCursorPosition.Y >= i.srWindow.Top && i.dwCursorPosition.Y <= i.srWindow.Bottom;
    fprintf(report, "%s: lines %s, typed line %s, cursor %s\n", !bad && expect == counter && typed_ok && visible ? "PASS" : "FAIL",
        !bad && expect == counter ? "in order" : "broken", typed_ok ? "after the output" : "misplaced", visible ? "visible" : "off screen");
    fflush(report);
}
static DWORD WINAPI writer(void *p) {
    Sleep(500);
    lines(200); dump("A 200 lines, read pending");
    type(L"he"); dump("B typed 'he'");
    lines(50); dump("C 50 more lines after typing");
    type(L"l"); dump("D typed 'l'");
    lines(50); dump("E 50 more lines");
    verify();
    ExitProcess(0);
}
int main(void) {
    out = GetStdHandle(STD_OUTPUT_HANDLE); in = GetStdHandle(STD_INPUT_HANDLE);
    report = fopen("C:\\contest-report.txt", "w");
    COORD size = {80, 9001}; SetConsoleScreenBufferSize(out, size);
    lines(20); printf("> "); fflush(stdout);
    dump("start");
    CreateThread(NULL, 0, writer, NULL, 0, NULL);
    wchar_t buf[256]; DWORD n;
    ReadConsoleW(in, buf, 255, &n, NULL);
    return 0;
}

Result with Proton Experimental as shipped: FAIL: lines broken, typed line misplaced. Lines 220 to 269 overwrite rows 20 to 69, and rows 70 to 219 still hold lines 70 to 219.

Cause

read_console stores the start of the line being edited (edit_line.home_x, home_y) when the read begins. write_console moves the cursor for the output but never moves that start, so the next update_read_output (any key) draws the line at the old row and leaves the cursor there, and the following output continues from it.

Suggested change

Before output is written during a pending read with echo, erase the echoed line and put the cursor at its start; after the output, move the line's start to the cursor and draw it again. The input line then always stays below the output, as on Windows. With this change built as a native conhost.exe for this Proton build and used through a DLL override, the test above passes and the SMAPI console keeps its history in order.

Patch against programs/conhost/conhost.c
diff --git a/programs/conhost/conhost.c b/programs/conhost/conhost.c
index 8298743..1bad4ef 100644
--- a/programs/conhost/conhost.c
+++ b/programs/conhost/conhost.c
@@ -2103,13 +2103,59 @@ static NTSTATUS set_output_info( struct screen_buffer *screen_buffer,
     return STATUS_SUCCESS;
 }
 
+/* A line being read with echo while the program writes (a background thread logging while the
+ * main thread waits in ReadConsole): the line being edited stays below the output. Its home
+ * position was taken when the read started, so without this the next keystroke redraws it there,
+ * moving the cursor back up, and later output overwrites the text written meanwhile. */
+static BOOL edit_line_follows_output( struct screen_buffer *screen_buffer )
+{
+    struct console *console = screen_buffer->console;
+    return console->edit_line.status == STATUS_PENDING && console->active == screen_buffer &&
+           (console->mode & (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)) == (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
+}
+
+/* Erase the echoed line and put the cursor where it starts, so output takes its place. */
+static void edit_line_hide( struct console *console )
+{
+    struct screen_buffer *screen_buffer = console->active;
+    struct edit_line *ctx = &console->edit_line;
+    RECT update_rect;
+    unsigned int i;
+
+    empty_update_rect( screen_buffer, &update_rect );
+    screen_buffer->cursor_x = ctx->home_x;
+    screen_buffer->cursor_y = ctx->home_y;
+    for (i = 0; i < ctx->end_offset && screen_buffer->cursor_y < screen_buffer->height; i++)
+        write_char( screen_buffer, ' ', &update_rect, &ctx->home_y );
+    screen_buffer->cursor_x = ctx->home_x;
+    screen_buffer->cursor_y = ctx->home_y;
+    update_output( screen_buffer, &update_rect );
+}
+
+/* The line starts where the output ended and is drawn there again. */
+static void edit_line_show( struct console *console )
+{
+    struct screen_buffer *screen_buffer = console->active;
+    struct edit_line *ctx = &console->edit_line;
+
+    ctx->home_x = min( screen_buffer->cursor_x, screen_buffer->width - 1 );
+    ctx->home_y = screen_buffer->cursor_y;
+    ctx->end_offset = 0;
+    if (!ctx->len) return;
+    ctx->update_begin = 0;
+    ctx->update_end = ctx->len - 1;
+    update_read_output( console, FALSE );
+}
+
 static NTSTATUS write_console( struct screen_buffer *screen_buffer, const WCHAR *buffer, size_t len )
 {
     RECT update_rect;
     size_t i, j;
+    BOOL editing = edit_line_follows_output( screen_buffer );
 
     TRACE( "%s\n", debugstr_wn(buffer, len) );
 
+    if (editing && screen_buffer->console->edit_line.end_offset) edit_line_hide( screen_buffer->console );
     empty_update_rect( screen_buffer, &update_rect );
 
     for (i = 0; i < len; i++)
@@ -2166,6 +2212,7 @@ static NTSTATUS write_console( struct screen_buffer *screen_buffer, const WCHAR
 
     scroll_to_cursor( screen_buffer );
     update_output( screen_buffer, &update_rect );
+    if (editing) edit_line_show( screen_buffer->console );
     tty_sync( screen_buffer->console );
     update_window_config( screen_buffer->console, TRUE );
     return STATUS_SUCCESS;
Ngôn ngữ chính
C++
Star
33.4k
Fork
1.8k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ValveSoftware/Proton

Tất cả issue của ValveSoftware/Proton

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.