chaser-util 0.1.2

CHaser Online MeetingPlace / game-server scraper with C FFI, proxy support, and real-time map polling
Documentation
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
 * chaser_util.h  -  CHaser Online MeetingPlace scraper  C / C++ API
 *
 * -----------------------------------------------------------------
 * Build
 * -----------------------------------------------------------------
 *
 *   cargo build --release
 *
 *   Output:
 *     Windows : target\release\chaser_util.dll
 *               target\release\chaser_util.dll.lib   (import library)
 *     Linux   : target/release/libchaser_util.so
 *     Android : target/<abi>/release/libchaser_util.so
 *
 * -----------------------------------------------------------------
 * Linking
 * -----------------------------------------------------------------
 *
 *   Windows (MSVC):
 *     cl /std:c++17 your_app.cpp /I<include_dir> chaser_util.dll.lib
 *     Place chaser_util.dll in the same directory as the .exe at runtime.
 *
 *   Linux / Android (GCC / Clang):
 *     g++ -std=c++17 your_app.cpp -I<include_dir> \
 *         -L<lib_dir> -lchaser_util -Wl,-rpath,<lib_dir> -o your_app
 *
 * -----------------------------------------------------------------
 * Memory management
 * -----------------------------------------------------------------
 *
 *   Pointers returned by scraper_scrape*() are heap-allocated by Rust.
 *   Always free them with scraper_free_result().
 *   Do NOT free individual fields with free().
 *
 * -----------------------------------------------------------------
 * String encoding
 * -----------------------------------------------------------------
 *
 *   All strings are UTF-8 / null-terminated.
 *
 * -----------------------------------------------------------------
 * Error codes (CScrapeResult::error_code)
 * -----------------------------------------------------------------
 *
 *   0  success
 *   1  scrape / network error  (see scraper_last_error())
 *   2  invalid argument        (NULL user, pass, or proxy_uri)
 *   3  internal panic          (should never happen; please report)
 */

#pragma once
#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>

/* ================================================================
 * C structs
 * ================================================================ */

typedef struct {
    unsigned int  room;
    unsigned int  max_connections;
    char*         map_display;
    char*         public_date;
    char*         patrol;
    char*         remarks;
} CRoomInfo;

typedef struct {
    unsigned int  order;
    char*         username;
    unsigned int  room;
    unsigned int  state;
} CLoggedInUser;

/**
 * Heap-allocated scrape result returned by scraper_scrape*().
 *
 * IMPORTANT: the two *_cap fields are INTERNAL bookkeeping used by
 * scraper_free_result().  Do NOT read, write, or depend on their values.
 * Their layout may change between releases.
 */
typedef struct {
    CRoomInfo*      rooms;
    size_t          rooms_len;
    size_t          rooms_cap;     /* INTERNAL — do not use */
    CLoggedInUser*  users;         /* NULL = no logged-in users */
    size_t          users_len;     /* 0    = no logged-in users */
    size_t          users_cap;     /* INTERNAL — do not use */
    unsigned int    error_code;    /* 0=success 1=error 2=bad arg 3=panic */
} CScrapeResult;

/* ================================================================
 * Filter structs
 *
 *   Numeric fields: *_enabled = 0 disables the filter, 1 enables it.
 *   String fields:  NULL disables the filter.
 * ================================================================ */

typedef struct {
    unsigned int  room_enabled;
    unsigned int  room;
    unsigned int  room_min_enabled;
    unsigned int  room_min;
    unsigned int  room_max_enabled;
    unsigned int  room_max;
    unsigned int  min_max_conn_enabled;
    unsigned int  min_max_conn;
    unsigned int  max_max_conn_enabled;
    unsigned int  max_max_conn;
    const char*   map_display;
    const char*   public_date;
    const char*   public_date_contains;
    const char*   patrol;
    const char*   remarks;
    const char*   remarks_contains;
} CRoomFilter;

typedef struct {
    unsigned int  order_enabled;
    unsigned int  order;
    unsigned int  order_min_enabled;
    unsigned int  order_min;
    unsigned int  order_max_enabled;
    unsigned int  order_max;
    const char*   username;
    const char*   username_contains;
    unsigned int  room_enabled;
    unsigned int  room;
    unsigned int  room_min_enabled;
    unsigned int  room_min;
    unsigned int  room_max_enabled;
    unsigned int  room_max;
    unsigned int  state_enabled;
    unsigned int  state;
} CUserFilter;

/* ================================================================
 * C API functions
 * ================================================================ */

/**
 * Scrapes with automatic proxy detection.
 *
 * Detection order:
 *   1. HTTP_PROXY / HTTPS_PROXY environment variables
 *   2. Windows registry (Windows only)
 *   3. macOS System Configuration (macOS only)
 *   4. Direct connection
 *
 * @param user        Login username (UTF-8, null-terminated).  MUST NOT be NULL.
 * @param pass        Login password (UTF-8, null-terminated).  MUST NOT be NULL.
 * @param room_filter Room filter, or NULL for no filter.
 * @param user_filter User filter, or NULL for no filter.
 * @return            Heap-allocated result; free with scraper_free_result().
 *                    Never returns NULL.  Check error_code on the result.
 */
CScrapeResult* scraper_scrape(
    const char*        user,
    const char*        pass,
    const CRoomFilter* room_filter,
    const CUserFilter* user_filter
);

/**
 * Scrapes with a manually specified proxy.
 *
 * @param user        Login username (UTF-8, null-terminated).  MUST NOT be NULL.
 * @param pass        Login password (UTF-8, null-terminated).  MUST NOT be NULL.
 * @param proxy_uri   e.g. "http://192.168.1.1:8080"; pass "" for direct connection.
 *                    MUST NOT be NULL.
 * @param room_filter Room filter, or NULL for no filter.
 * @param user_filter User filter, or NULL for no filter.
 * @return            Heap-allocated result; free with scraper_free_result().
 *                    Never returns NULL.  Check error_code on the result.
 */
CScrapeResult* scraper_scrape_with_proxy(
    const char*        user,
    const char*        pass,
    const char*        proxy_uri,
    const CRoomFilter* room_filter,
    const CUserFilter* user_filter
);

/**
 * Frees a CScrapeResult.  Must always be called.  Passing NULL is a no-op.
 */
void scraper_free_result(CScrapeResult* result);

/**
 * Returns the last error message (UTF-8).
 * Valid until the next FFI call on this thread.
 */
const char* scraper_last_error(void);

#ifdef __cplusplus
} /* extern "C" */
#endif


/* ================================================================
 * C++ wrapper (header-only)
 * ================================================================ */
#ifdef __cplusplus
#include <string>
#include <vector>
#include <optional>
#include <stdexcept>

namespace chaser_util {

/* ---- Constants ---- */

namespace MapDisplay {
    static constexpr const char* ENABLED  = "\xe5\x8f\xaf";  ///< 可 (UTF-8)
    static constexpr const char* DISABLED = "\xe5\x90\xa6";  ///< 否 (UTF-8)
}

namespace Patrol {
    static constexpr const char* YES = "\xe6\x9c\x89";  ///< 有 (UTF-8)
    static constexpr const char* NO  = "\xc3\x97";      ///< × (UTF-8)
}

namespace Remarks {
    static constexpr const char* RA  = "\xe3\x83\xa9";  ///< ラ (UTF-8)
    static constexpr const char* SAI = "\xe5\x9f\xbc";  ///< 埼 (UTF-8)
    static constexpr const char* ZEN = "\xe5\x85\xa8";  ///< 全 (UTF-8)
}

/* ---- Data types ---- */

struct RoomInfo {
    unsigned int room;
    unsigned int max_connections;
    std::string  map_display;
    std::string  public_date;
    std::string  patrol;
    std::string  remarks;
};

struct LoggedInUser {
    unsigned int order;
    std::string  username;
    unsigned int room;
    unsigned int state;
};

struct ScrapeResult {
    std::optional<std::vector<LoggedInUser>> logged_in_users;
    std::vector<RoomInfo>                    rooms;
};

/* ---- Filter builders (method chaining) ---- */

struct RoomFilter {
    CRoomFilter c{};

    RoomFilter& room(unsigned int v)
        { c.room_enabled=1; c.room=v; return *this; }
    RoomFilter& room_range(unsigned int lo, unsigned int hi)
        { c.room_min_enabled=1; c.room_min=lo;
          c.room_max_enabled=1; c.room_max=hi; return *this; }
    RoomFilter& min_max_conn(unsigned int v)
        { c.min_max_conn_enabled=1; c.min_max_conn=v; return *this; }
    RoomFilter& max_max_conn(unsigned int v)
        { c.max_max_conn_enabled=1; c.max_max_conn=v; return *this; }
    RoomFilter& map_display(const char* v)
        { c.map_display=v; return *this; }
    RoomFilter& map_display(const char8_t* v)
        { return map_display(reinterpret_cast<const char*>(v)); }
    RoomFilter& public_date(const char* v)
        { c.public_date=v; return *this; }
    RoomFilter& public_date(const char8_t* v)
        { return public_date(reinterpret_cast<const char*>(v)); }
    RoomFilter& public_date_contains(const char* v)
        { c.public_date_contains=v; return *this; }
    RoomFilter& public_date_contains(const char8_t* v)
        { return public_date_contains(reinterpret_cast<const char*>(v)); }
    RoomFilter& patrol(const char* v)
        { c.patrol=v; return *this; }
    RoomFilter& patrol(const char8_t* v)
        { return patrol(reinterpret_cast<const char*>(v)); }
    RoomFilter& remarks(const char* v)
        { c.remarks=v; return *this; }
    RoomFilter& remarks(const char8_t* v)
        { return remarks(reinterpret_cast<const char*>(v)); }
    RoomFilter& remarks_contains(const char* v)
        { c.remarks_contains=v; return *this; }
    RoomFilter& remarks_contains(const char8_t* v)
        { return remarks_contains(reinterpret_cast<const char*>(v)); }
};

struct UserFilter {
    CUserFilter c{};

    UserFilter& order(unsigned int v)
        { c.order_enabled=1; c.order=v; return *this; }
    UserFilter& order_range(unsigned int lo, unsigned int hi)
        { c.order_min_enabled=1; c.order_min=lo;
          c.order_max_enabled=1; c.order_max=hi; return *this; }
    UserFilter& username(const char* v)
        { c.username=v; return *this; }
    UserFilter& username(const char8_t* v)
        { return username(reinterpret_cast<const char*>(v)); }
    UserFilter& username_contains(const char* v)
        { c.username_contains=v; return *this; }
    UserFilter& username_contains(const char8_t* v)
        { return username_contains(reinterpret_cast<const char*>(v)); }
    UserFilter& room(unsigned int v)
        { c.room_enabled=1; c.room=v; return *this; }
    UserFilter& room_range(unsigned int lo, unsigned int hi)
        { c.room_min_enabled=1; c.room_min=lo;
          c.room_max_enabled=1; c.room_max=hi; return *this; }
    UserFilter& state(unsigned int v)
        { c.state_enabled=1; c.state=v; return *this; }
};

/* ---- Internal conversion helper ---- */

inline ScrapeResult convert(CScrapeResult* raw) {
    if (!raw) throw std::runtime_error("scraper returned NULL (should never happen)");
    if (raw->error_code != 0) {
        std::string msg = scraper_last_error();
        scraper_free_result(raw);
        throw std::runtime_error(msg);
    }

    ScrapeResult out;

    for (size_t i = 0; i < raw->rooms_len; ++i) {
        auto& r = raw->rooms[i];
        out.rooms.push_back({
            r.room,
            r.max_connections,
            r.map_display  ? r.map_display  : "",
            r.public_date  ? r.public_date  : "",
            r.patrol       ? r.patrol       : "",
            r.remarks      ? r.remarks      : "",
        });
    }

    if (raw->users && raw->users_len > 0) {
        std::vector<LoggedInUser> users;
        for (size_t i = 0; i < raw->users_len; ++i) {
            auto& u = raw->users[i];
            users.push_back({
                u.order,
                u.username ? u.username : "",
                u.room,
                u.state,
            });
        }
        out.logged_in_users = std::move(users);
    }

    scraper_free_result(raw);
    return out;
}

/* ---- Public API ---- */

/**
 * Scrapes with automatic proxy detection.
 * Throws std::runtime_error on failure.
 */
inline ScrapeResult scrape(
    const std::string& user,
    const std::string& pass,
    const RoomFilter*  rf = nullptr,
    const UserFilter*  uf = nullptr)
{
    return convert(scraper_scrape(
        user.c_str(), pass.c_str(),
        rf ? &rf->c : nullptr,
        uf ? &uf->c : nullptr
    ));
}

/**
 * Scrapes with a manually specified proxy.
 * Pass proxy_uri="" for direct connection.
 * Throws std::runtime_error on failure.
 */
inline ScrapeResult scrape_with_proxy(
    const std::string& user,
    const std::string& pass,
    const std::string& proxy_uri,
    const RoomFilter*  rf = nullptr,
    const UserFilter*  uf = nullptr)
{
    return convert(scraper_scrape_with_proxy(
        user.c_str(), pass.c_str(), proxy_uri.c_str(),
        rf ? &rf->c : nullptr,
        uf ? &uf->c : nullptr
    ));
}

} // namespace chaser_util

#endif /* __cplusplus */