Native macOS App (first version): dm/tools/mac_app.sh, docs/MAC.md
The Mac host can now be packaged as a double-click macOS App with keyboard, mouse and gamepad input, saves under Application Support, a first-launch resource-folder picker and save-before-quit. dm/tools/mac_app.sh builds it after ./setup.sh; by default the package carries no derived content and points at this machine's build outputs, so it runs only on the Mac that built it and needs only a local ad-hoc signature. Nobody has played this version by hand yet; what was verified was run unattended. See docs/MAC.md.
This commit is contained in:
+3
-3
@@ -2,7 +2,7 @@ DM_ROOT ?= $(HOME)/dm-work
|
||||
CFLAGS=-O1 -g -Wall -Wno-unused-function
|
||||
# 服务层接管 hle_*.c 按通配纳入:新增文件不改这一行(免合并冲突)
|
||||
# Service-layer takeover hle_*.c files are included by wildcard: adding a file does not touch this line (avoids merge conflicts)
|
||||
SRC=main.c loader.c hle.c crash.c diag.c thread.c memstat.m dm_cursor.c dm_cursor_item.c dm_world.c dm_textinput.c dm_update.c dm_actions.c dm_namegen.c dm_cloudsync.c dm_custom.c dm_elysium.c dm_bossrush.c dm_pages.c dm_vault.c dm_vault_ui.c dm_kills.c dm_textbox.c dm_textbox_core.c dm_potpick.c dm_lootfilter.c dm_autobuff.c dm_beltauto.c dm_padmode.c dm_pouch.c dm_drophaptic.c dm_freetp.c dm_townwp.c dm_shoprefresh.c dm_runeword.c dm_uiscreen.c dm_dropfilter.c dm_hud.c dm_rift.c dm_rift_ui.c dm_bright.c touch_mac.c romfs_validate.c ../ios/Sources/dm_touch_controls.c $(sort $(wildcard hle_*.c)) front_mac.m entry.S dm_i18n.c
|
||||
SRC=main.c loader.c hle.c crash.c diag.c thread.c memstat.m dm_cursor.c dm_cursor_item.c dm_world.c dm_textinput.c dm_update.c dm_actions.c dm_namegen.c dm_cloudsync.c dm_custom.c dm_elysium.c dm_bossrush.c dm_pages.c dm_vault.c dm_vault_ui.c dm_kills.c dm_textbox.c dm_textbox_core.c dm_potpick.c dm_lootfilter.c dm_autobuff.c dm_beltauto.c dm_padmode.c dm_pouch.c dm_drophaptic.c dm_freetp.c dm_townwp.c dm_shoprefresh.c dm_runeword.c dm_uiscreen.c dm_dropfilter.c dm_hud.c dm_rift.c dm_rift_ui.c dm_bright.c touch_mac.c romfs_validate.c ../ios/Sources/dm_touch_controls.c $(sort $(wildcard hle_*.c)) front_mac.m app_mac.m ../ios/Sources/dm_gamepad.m entry.S dm_i18n.c
|
||||
# NVN 空设备 + nn::vi(dm/gfx/)
|
||||
# 按通配纳入 dm/gfx 与 dm/gfx/nvn 下的源(tests/ metal/ 不进宿主):新增文件不改这一行
|
||||
# NVN null device + nn::vi (dm/gfx/)
|
||||
@@ -27,5 +27,5 @@ dm_hud_exp.h: ../ios/tools/gen_hud_exp.py ../ios/tools/romfs_art.py
|
||||
dm_bright_levels.inc: ../tools/gen_bright_levels.py ../ios/tools/touch_layout.py
|
||||
$(NEED_ROMFS)
|
||||
python3 ../tools/gen_bright_levels.py "$(ROMFS)" > $@.tmp && mv $@.tmp $@
|
||||
dmhost: $(SRC) $(GFX) $(LIBDEFLATE) dm.h hle.h dm_input.h front.h touch_mac.h dm_cursor.h ../ios/Sources/dm_touch_controls.[ch] $(GFX_H) $(GENH)
|
||||
clang $(CFLAGS) -I../gfx/nvn/include -fobjc-arc -o $@ $(SRC) $(GFX) $(LIBDEFLATE) -framework Cocoa -framework QuartzCore -framework Metal -framework AudioToolbox -framework ImageIO -framework UniformTypeIdentifiers
|
||||
dmhost: $(SRC) $(GFX) $(LIBDEFLATE) dm.h hle.h dm_input.h front.h touch_mac.h dm_cursor.h dm_i18n_tab.h ../ios/Sources/dm_touch_controls.[ch] $(GFX_H) $(GENH)
|
||||
clang $(CFLAGS) -I. -I../gfx/nvn/include -fobjc-arc -o $@ $(SRC) $(GFX) $(LIBDEFLATE) -framework Cocoa -framework QuartzCore -framework Metal -framework AudioToolbox -framework ImageIO -framework UniformTypeIdentifiers -framework GameController
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
// macOS App 形态(双击起):宿主被包成 <名>.app 时,启动最前面替玩家把命令行那套准备好——
|
||||
// 窗口开着;存档放 ~/Library/Application Support/D2R Mobile/save(与开发用的工作区、测试存档都不相干);
|
||||
// 游戏资源文件夹第一次启动时请玩家选(记在偏好里,下次不再问;选的不是资源文件夹给明确报错、重选);
|
||||
// 程序文件(改写后的游戏程序,产物目录)随包放在 Contents/Resources/product;日志写到 ~/Library/Logs/D2R Mobile/host.log。
|
||||
// 命令行起(闸门、截图、脚本)不走这里:不在 .app 包里,或带了参数,一律原样。打包脚本 dm/tools/mac_app.sh。
|
||||
// macOS App form (started by double-click): when the host is packaged as <name>.app, the very first thing at startup is to prepare the command-line setup on the player's behalf --
|
||||
// the window is on; saves go to ~/Library/Application Support/D2R Mobile/save (unrelated to the development workspace and to test saves);
|
||||
// the game resource folder is chosen by the player on first launch (remembered in preferences and not asked again; choosing something that is not a resource folder gives a clear error and another try);
|
||||
// the program files (the rewritten game program, the build output directory) are in Contents/Resources/product inside the package; the log is written to ~/Library/Logs/D2R Mobile/host.log.
|
||||
// Command-line starts (gates, screenshots, scripts) do not come here: not inside an .app package, or with arguments, everything stays as it was. Packaging script: dm/tools/mac_app.sh.
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#include <sys/stat.h>
|
||||
#include "hle.h"
|
||||
#include "dm_i18n.h"
|
||||
#define T(k) (@(dm_ui(k)))
|
||||
|
||||
#define APP_NAME "D2R Mobile"
|
||||
static NSString *const kRomfsKey = @"RomfsDir";
|
||||
|
||||
// 这个文件夹是不是游戏资源:认 data/build_manifest.psv。选到上一两层(解包目录的根、Update)也认,往里找一层
|
||||
// Whether this folder is the game resources: recognized by data/build_manifest.psv. Choosing one or two levels up (the root of the unpacked directory, Update) is accepted too, looking one level in
|
||||
static NSString *romfs_root(NSString *dir, NSString **why) {
|
||||
NSFileManager *fm = NSFileManager.defaultManager;
|
||||
for (NSString *sub in @[@"", @"RomFS", @"romfs", @"Update/RomFS"]) {
|
||||
NSString *d = sub.length ? [dir stringByAppendingPathComponent:sub] : dir;
|
||||
if ([fm fileExistsAtPath:[d stringByAppendingPathComponent:@"data/build_manifest.psv"]]) {
|
||||
if (![fm isReadableFileAtPath:[d stringByAppendingPathComponent:@"data/build_manifest.psv"]]) { if (why) *why = T("dmAppNoRead"); return nil; }
|
||||
return d;
|
||||
}
|
||||
}
|
||||
if (why) *why = [NSString stringWithFormat:T("dmAppNotRomfs"), dir.lastPathComponent.UTF8String];
|
||||
return nil;
|
||||
}
|
||||
static void alert(NSString *title, NSString *text) {
|
||||
NSAlert *a = [NSAlert new]; a.messageText = title; a.informativeText = text; [a addButtonWithTitle:T("dmAppOK")]; [a runModal];
|
||||
}
|
||||
// 请玩家选资源文件夹,直到选对或取消。返回资源根目录,取消返回 nil
|
||||
// Ask the player to choose the resource folder until it is right or cancelled. Returns the resource root, or nil on cancel
|
||||
static NSString *pick_romfs(NSString *first_why) {
|
||||
NSString *why = first_why;
|
||||
for (;;) {
|
||||
NSOpenPanel *p = [NSOpenPanel openPanel];
|
||||
p.canChooseDirectories = YES; p.canChooseFiles = NO; p.allowsMultipleSelection = NO; p.prompt = T("dmAppPickUse");
|
||||
p.message = why ? [NSString stringWithFormat:T("dmAppPickAgain"), why.UTF8String] : T("dmAppPickMsg");
|
||||
if ([p runModal] != NSModalResponseOK) return nil;
|
||||
NSString *w = nil, *r = romfs_root(p.URL.path, &w);
|
||||
if (r) return r;
|
||||
alert(T("dmAppNotRomfsTitle"), w); why = w;
|
||||
}
|
||||
}
|
||||
|
||||
// 包里的一项(product / msl / romfs_overlay):实物在 Contents/Resources/<名> 就用它;没有就看 Resources/paths.txt 里「名=本机路径」那一行
|
||||
// (mac_app.sh 的「不带派生物」出法:改写后的游戏程序、着色器库、界面覆盖层都从游戏内容派生,公开发布的包不带,指向本机自己构建出来的)
|
||||
// One item of the package (product / msl / romfs_overlay): if the real thing is in Contents/Resources/<name> it is used; otherwise the line "name=local path" in Resources/paths.txt is consulted
|
||||
// (the "without derived content" build of mac_app.sh: the rewritten game program, the shader library and the UI overlay are all derived from game content, a publicly released package does not carry them and points at what this machine built itself)
|
||||
static NSString *res_item(NSString *name) {
|
||||
NSFileManager *fm = NSFileManager.defaultManager; NSString *rp = NSBundle.mainBundle.resourcePath, *d = [rp stringByAppendingPathComponent:name];
|
||||
if ([fm fileExistsAtPath:d]) return d;
|
||||
NSString *txt = [NSString stringWithContentsOfFile:[rp stringByAppendingPathComponent:@"paths.txt"] encoding:NSUTF8StringEncoding error:nil];
|
||||
for (NSString *ln in [txt componentsSeparatedByString:@"\n"]) { NSString *pre = [name stringByAppendingString:@"="]; if ([ln hasPrefix:pre]) { d = [ln substringFromIndex:pre.length]; return [fm fileExistsAtPath:d] ? d : nil; } }
|
||||
return nil;
|
||||
}
|
||||
// main() 最前面调。是 App 形态就把环境与参数备好并返回 1;不是返回 0、什么都不动
|
||||
// Called at the very start of main(). In App form it prepares the environment and arguments and returns 1; otherwise it returns 0 and touches nothing
|
||||
int dm_app_bootstrap(int *argc, char ***argv) {
|
||||
@autoreleasepool {
|
||||
NSString *bp = NSBundle.mainBundle.bundlePath;
|
||||
if (*argc > 1 || ![bp.pathExtension isEqualToString:@"app"]) return 0;
|
||||
NSFileManager *fm = NSFileManager.defaultManager;
|
||||
if (!getenv("DM_LANG")) setenv("DM_LANG", [NSLocale.preferredLanguages.firstObject hasPrefix:@"zh"] ? "zh-Hans" : "en", 1); // 命令行宿主一律简中;App 形态跟系统首选语言(提示框与游戏内同一个开关) | The command-line host is always Simplified Chinese; the App form follows the system's preferred language (dialogs and in-game text use the same switch)
|
||||
NSString *sup = [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@APP_NAME] copy];
|
||||
if (getenv("DM_APP_SUPPORT")) sup = @(getenv("DM_APP_SUPPORT")); // 测试:存档等放到指定目录,不碰玩家那份 | Test: put saves and the rest in a given directory, leaving the player's own untouched
|
||||
NSString *logd = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"Logs/" APP_NAME] copy];
|
||||
NSString *save = [sup stringByAppendingPathComponent:@"save"], *temp = [sup stringByAppendingPathComponent:@"temp"];
|
||||
for (NSString *d in @[sup, logd, save, temp]) [fm createDirectoryAtPath:d withIntermediateDirectories:YES attributes:nil error:nil];
|
||||
// 双击起没有终端:日志落文件,上一次的留一份
|
||||
// A double-click start has no terminal: the log goes to a file, and the previous one is kept
|
||||
NSString *lf = [logd stringByAppendingPathComponent:@"host.log"];
|
||||
[fm removeItemAtPath:[logd stringByAppendingPathComponent:@"host.prev.log"] error:nil];
|
||||
[fm moveItemAtPath:lf toPath:[logd stringByAppendingPathComponent:@"host.prev.log"] error:nil];
|
||||
if (!isatty(2)) { freopen(lf.fileSystemRepresentation, "w", stderr); setvbuf(stderr, NULL, _IOLBF, 0); }
|
||||
[NSApplication sharedApplication]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp activateIgnoringOtherApps:YES];
|
||||
NSString *prod = res_item(@"product");
|
||||
if (!prod || ![fm fileExistsAtPath:[prod stringByAppendingPathComponent:@"main.patched.bin"]]) {
|
||||
alert(T("dmAppBrokenTitle"), T("dmAppBroken")); exit(1);
|
||||
}
|
||||
NSUserDefaults *ud = NSUserDefaults.standardUserDefaults;
|
||||
NSString *saved = [ud stringForKey:kRomfsKey], *why = nil, *rom = saved ? romfs_root(saved, &why) : nil;
|
||||
if (getenv("DM_APP_ROMFS")) { // 测试(没人点对话框时用):直接给资源文件夹;不对就把同一句报错写进日志并退出,不弹框、不记进偏好 | Test (for when nobody is there to click the dialog): give the resource folder directly; if it is wrong, write the same error sentence to the log and exit, with no dialog and nothing saved to preferences
|
||||
rom = romfs_root(@(getenv("DM_APP_ROMFS")), &why);
|
||||
if (!rom) { LOG("【红】App:DM_APP_ROMFS 给的不是游戏资源文件夹:%s", why.UTF8String); exit(2); }
|
||||
} else if (!rom) {
|
||||
rom = pick_romfs(saved ? [NSString stringWithFormat:T("dmAppRomfsGone"), why.UTF8String] : nil);
|
||||
if (!rom) { LOG("App:没有选资源文件夹,退出"); exit(0); }
|
||||
[ud setObject:rom forKey:kRomfsKey];
|
||||
}
|
||||
setenv("DM_WINDOW", "1", 1); setenv("DM_APP", "1", 1);
|
||||
setenv("DM_SAVE", save.fileSystemRepresentation, 1); setenv("DM_TEMP", temp.fileSystemRepresentation, 1);
|
||||
setenv("DM_ROMFS", rom.fileSystemRepresentation, 1); setenv("DM_ROOT", sup.fileSystemRepresentation, 1);
|
||||
NSString *shmap = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"shader_map.tsv"]; // 图形后端的着色器映射表:命令行起时按可执行文件旁的源码树找,包里放在 Resources | The graphics backend's shader mapping table: when started from the command line it is found via the source tree next to the executable; in the package it is in Resources
|
||||
if ([fm fileExistsAtPath:shmap]) setenv("DM_NVN_SHMAP", shmap.fileSystemRepresentation, 1);
|
||||
NSString *msl = res_item(@"msl"); // 编好的着色器库(msl/b-<翻译链版本>/) | The compiled shader library (msl/b-<translation chain version>/)
|
||||
if (msl) setenv("DM_MSL_DIR", msl.fileSystemRepresentation, 1);
|
||||
NSString *ov = res_item(@"romfs_overlay");
|
||||
if (ov) setenv("DM_ROMFS_OVERLAY", ov.fileSystemRepresentation, 1);
|
||||
static char *av[5]; av[0] = (*argv)[0]; av[1] = strdup(prod.fileSystemRepresentation); av[2] = "main"; av[3] = "sdk"; av[4] = NULL;
|
||||
*argv = av; *argc = 4;
|
||||
LOG("App:存档 %s;资源 %s;程序文件 %s;界面覆盖层 %s;日志 %s", save.UTF8String, rom.UTF8String, prod.UTF8String, getenv("DM_ROMFS_OVERLAY") ?: "(没有,原版界面)", lf.UTF8String);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 手柄代码(ios/Sources/dm_gamepad.m,两边共用)问「现在是不是面板 / 读取画面开着」:只用来决定空着的 ZR 格要不要当免费回城。
|
||||
// 宿主没有 iOS 壳那套面板栈;一律答「是」= ZR 整颗交给原版(面板里它是翻页签键),手柄上的免费回城这一版没有(记在已知不足里)
|
||||
// The gamepad code (ios/Sources/dm_gamepad.m, shared by both sides) asks "is a panel / loading screen open right now": used only to decide whether the empty ZR slot should act as the free town portal.
|
||||
// The host has no panel stack like the iOS shell; it always answers "yes" = ZR is handed entirely to the original game (in panels it is the tab-switch key), so the free town portal on the gamepad is absent in this version (listed under known gaps)
|
||||
int dm_front_navigation(void) { return 1; }
|
||||
@@ -1183,6 +1183,15 @@ int dm_request_save(void) {
|
||||
*(volatile uint64_t *)(uintptr_t)(g_lastG + 0x5800) = 1;
|
||||
return 1;
|
||||
}
|
||||
// 退出前存盘用(Mac App):单机暂停着(暂停菜单 / 大秘境面板开着,原版界面状态字节 [main+0x2d5ff51] | [main+0x2d5ff67] 非 0,见 dm_rift_ui.c)时游戏不跑拍,
|
||||
// 上面拨的那次存盘等不到。把这两个字节清掉让拍接着走;只在进程马上要退出时调(不写回)。返回清之前是否暂停着
|
||||
// For saving before exit (Mac App): while single-player is paused (pause menu / rift panel open, original UI state bytes [main+0x2d5ff51] | [main+0x2d5ff67] non-zero, see dm_rift_ui.c) the game does not tick,
|
||||
// so the save requested above would never happen. Clear these two bytes so the ticks continue; called only when the process is about to exit (not written back). Returns whether it was paused before clearing
|
||||
int dm_unpause_for_exit(void) {
|
||||
uint8_t *b = NULL; for (int i = 0; i < g_nmod; i++) if (!strcmp(g_mod[i].name, "main")) b = (uint8_t *)g_mod[i].base;
|
||||
if (!b || !in_game()) return 0;
|
||||
volatile uint8_t *f = b + 0x2d5ff51, *g = b + 0x2d5ff67; int was = *f || *g; *f = 0; *g = 0; return was;
|
||||
}
|
||||
// 测试用:按 id 取服务端物品单位(物品表 G+0x2e30,128 桶、next=+0x158);不在局内 / 没找到返回 0
|
||||
// For tests: get a server item unit by id (item table G+0x2e30, 128 buckets, next=+0x158); returns 0 when not in a run / not found
|
||||
uint64_t dm_diag_srv_item(uint32_t id) { if (!in_game()) return 0; uint64_t u = 0; tk_rd(g_lastG + 0x2e30 + 8 * (id & 0x7f), &u, 8); for (int k = 0; u && k < 4096; k++) { uint32_t ty = 0, uid = 0; if (!tk_rd(u, &ty, 4) || !tk_rd(u + 8, &uid, 4)) return 0; if (ty == 4 && uid == id) return u; if (!tk_rd(u + 0x158, &u, 8)) return 0; } return 0; }
|
||||
|
||||
@@ -302,4 +302,20 @@ static const struct { const char *k, *zh, *en; } k_dm_i18n[] = {
|
||||
{ "dmPadDiffLine", "游戏难度:%d", "Game Difficulty: %d" },
|
||||
{ "dmRiftGamAfterEn", "通关本层后", "After This Level" },
|
||||
{ "dmRiftMsNEn", "每档另得 符文 · 赦免之证 · 打孔", "+ Rune, Token of Absolution, Sockets" },
|
||||
{ "dmAppQuit", "退出 D2R Mobile", "Quit D2R Mobile" },
|
||||
{ "dmAppOK", "好", "OK" },
|
||||
{ "dmAppPickUse", "用这个文件夹", "Use This Folder" },
|
||||
{ "dmAppPickMsg", "请选择游戏资源文件夹(里面有 data 文件夹)。只需要选这一次。", "Choose the game data folder (the one that contains a “data” folder). You only need to do this once." },
|
||||
{ "dmAppPickAgain", "%s\n\n请重新选择游戏资源文件夹。", "%s\n\nPlease choose the game data folder again." },
|
||||
{ "dmAppNotRomfsTitle", "这不是游戏资源文件夹", "This is not the game data folder" },
|
||||
{ "dmAppNotRomfs", "「%s」里没有 data/build_manifest.psv。\n要选的是游戏资源文件夹:里面有一个 data 文件夹,data 里有 build_manifest.psv 和若干很大的数据文件。", "“%s” has no data/build_manifest.psv.\nChoose the game data folder: it contains a “data” folder with build_manifest.psv and several very large data files." },
|
||||
{ "dmAppNoRead", "找到了资源清单,但没有读取权限。", "The data manifest was found but cannot be read." },
|
||||
{ "dmAppRomfsGone", "上次选的资源文件夹用不了了:\n%s", "The data folder chosen last time can no longer be used:\n%s" },
|
||||
{ "dmAppBrokenTitle", "这个 App 不完整", "This app is incomplete" },
|
||||
{ "dmAppBroken", "包里没有程序文件(Contents/Resources/product)。请重新构建或重新下载。", "The app bundle has no program files (Contents/Resources/product). Rebuild it or download it again." },
|
||||
{ "dmAppMenuView", "显示", "View" },
|
||||
{ "dmAppFullscreen", "全屏", "Toggle Full Screen" },
|
||||
{ "dmAppMenuHelp", "帮助", "Help" },
|
||||
{ "dmAppKeysTitle", "键盘与手柄按键对照", "Keyboard and Controller Buttons" },
|
||||
{ "dmAppKeysText", "界面上的按键提示是手柄字母,键盘这样对应:\n\n移动:W A S D(左摇杆)\n十字键:方向键\nA 确认:J B 返回:K X:U Y:I\nL:Q R:E ZL:Z ZR:C\n按左摇杆:F 按右摇杆:G\n+(菜单):回车 −(地图):Tab\n\n鼠标:左键点目标或界面,右键 = B,滚轮滚列表。\n接上手柄可以直接用。这一页以后在「帮助」菜单里。", "On-screen button hints use controller letters. Keyboard mapping:\n\nMove: W A S D (left stick)\nD-pad: arrow keys\nA confirm: J B back: K X: U Y: I\nL: Q R: E ZL: Z ZR: C\nPress left stick: F Press right stick: G\n+ (menu): Return − (map): Tab\n\nMouse: left click a target or the interface, right click = B, wheel scrolls lists.\nA connected controller works right away. This page stays in the Help menu." },
|
||||
};
|
||||
|
||||
@@ -125,7 +125,8 @@ void dm_padmode_boot(void) { // 开机:游戏建面板之前(main.c,nnMa
|
||||
module_t *m = NULL; for (int i = 0; i < g_nmod; ++i) if (!strcmp(g_mod[i].name, "main")) m = &g_mod[i]; char mk[1100];
|
||||
if (!m || !getenv("DM_ROMFS_OVERLAY")) return;
|
||||
if (!dm_overlay_file("ctrl_touch", mk, sizeof mk)) { LOG("手柄 / 触屏布局:覆盖层没有 ctrl_touch 标记,照原版布局"); return; }
|
||||
if (pfx_write((uint8_t *)m->base, 0)) { g_have = 1; g_cur = 0; g_b = (uint8_t *)m->base; str_load(); LOG("手柄 / 触屏布局:开机用触屏版(layouts/xtouch_lay 等三个目录)"); }
|
||||
int pad = getenv("DM_APP") != NULL; // Mac App:没有触屏圆钮与底栏的绘制层,开机就用原版手柄版(我们的功能页在手柄版布局里同样有),之后也不切 | Mac App: there is no drawing layer for the touch buttons and bottom bar, so it starts in the original gamepad UI (our feature pages exist in the gamepad layout as well) and never switches afterwards
|
||||
if (pfx_write((uint8_t *)m->base, pad)) { g_have = 1; g_cur = pad; atomic_store(&g_want, pad); g_b = (uint8_t *)m->base; str_load(); LOG("手柄 / 触屏布局:开机用%s", pad ? "原版手柄版(Mac App)" : "触屏版(layouts/xtouch_lay 等三个目录)"); }
|
||||
}
|
||||
void dm_padmode_request(int pad) { pad = pad ? 1 : 0; atomic_store(&g_want, pad); if (g_have && pad != g_cur) atomic_store(&g_freeze, 1); } // 前端(任意线程):从这一刻起输入就不再交给游戏 | Front end (any thread): from this moment on input is no longer handed to the game
|
||||
int dm_padmode_current(void) { return g_cur; }
|
||||
|
||||
+90
-12
@@ -5,7 +5,7 @@
|
||||
// Without DM_WINDOW: the guest runs directly on the current thread, behaving the same as with no front end (windowless tests use this).
|
||||
// Key map (Nintendo layout): WASD left stick, arrow keys D-pad, J=A K=B U=X I=Y, Q=L E=R, Z=ZL C=ZR, F=press left stick G=press right stick, Return=+ Tab=−.
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <QuartzCore/CAMetalLayer.h>
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
#import <Metal/Metal.h>
|
||||
#include "dm.h"
|
||||
#include "dm_input.h"
|
||||
@@ -17,7 +17,51 @@ __attribute__((constructor)) static void ui_shift_reg(void) { mtl_set_ui_rect_fn
|
||||
#include "hle.h"
|
||||
#include <signal.h>
|
||||
|
||||
void dm_touch_host_live_enable(void); void dm_touch_host_live(int op, float nx, float ny); // touch_mac.c
|
||||
static int g_live; // App 形态(或 DM_TOUCH_LIVE=1):鼠标进我们的触屏状态机;否则照旧当原版触摸屏的一个触点 | App form (or DM_TOUCH_LIVE=1): the mouse goes into our touch state machine; otherwise it stays a single touch point of the original touch screen as before
|
||||
static CAMetalLayer *g_layer;
|
||||
static NSView *g_view;
|
||||
// 窗口不是 16:9(拉伸窗口、全屏到 16:10 的屏):游戏画面保持 16:9 居中,四周留黑边;鼠标位置按游戏画面这一块算
|
||||
// The window is not 16:9 (stretched window, full screen on a 16:10 display): the game picture stays 16:9 and centred with black bars around it; the mouse position is computed against the game picture
|
||||
static NSRect game_rect(void) {
|
||||
NSSize b = g_view.bounds.size; CGFloat w = b.width, h = b.height;
|
||||
if (w * 9 > h * 16) w = floor(h * 16 / 9); else h = floor(w * 9 / 16);
|
||||
return NSMakeRect(floor((b.width - w) / 2), floor((b.height - h) / 2), w, h);
|
||||
}
|
||||
static void layout_game(void) {
|
||||
NSRect r = game_rect(); if (NSEqualRects(r, g_layer.frame)) return;
|
||||
[CATransaction begin]; [CATransaction setDisableActions:YES]; g_layer.frame = r; g_layer.contentsScale = g_view.window.backingScaleFactor; [CATransaction commit];
|
||||
LOG("窗口:内容区 %.0f×%.0f,游戏画面 %.0f×%.0f 放在 (%.0f,%.0f)", g_view.bounds.size.width, g_view.bounds.size.height, r.size.width, r.size.height, r.origin.x, r.origin.y);
|
||||
}
|
||||
// 退出(关窗口 / ⌘Q / 菜单):有局在就先请游戏走一次它自己的存盘并等存档提交(做法同 iOS 壳进后台),最多 3 秒,再有序收尾。
|
||||
// 暂停菜单 / 大秘境面板开着时游戏停拍:先放开暂停让拍走起来;读取画面里等它进到局内再请。3 秒到了还没有提交就照实记「未存盘退出」
|
||||
// Quit (closing the window / ⌘Q / menu): if a game is in progress, first ask the game to do its own save and wait for the save to be committed (the same way the iOS shell does when going to the background), for at most 3 seconds, then shut down in order.
|
||||
// With the pause menu / rift panel open the game is not ticking: release the pause first so the ticks run; in a loading screen wait until it is in the game, then ask. If nothing is committed when the 3 seconds are up, truthfully log "quit without saving"
|
||||
static void app_quit(const char *why) {
|
||||
static int once; if (once++) return;
|
||||
int dm_ingame_now(void); int dm_request_save(void); int dm_unpause_for_exit(void); uint64_t dm_fs_commit_count(void); uint64_t now_ns(void);
|
||||
if (!dm_ingame_now() && !dm_uiscreen_ingame()) { LOG("窗口:退出(不在局内,没有待存的进度)"); dm_host_shutdown(why); return; }
|
||||
uint64_t c0 = dm_fs_commit_count(), t0 = now_ns(), last = c0, last_t = 0; int asked = 0, paused = 0;
|
||||
for (;;) {
|
||||
paused |= dm_unpause_for_exit(); if (!asked) asked = dm_request_save();
|
||||
usleep(20000); uint64_t c = dm_fs_commit_count(), t = now_ns();
|
||||
if (c != last) { last = c; last_t = t; }
|
||||
if ((asked && last_t && t - last_t >= 200000000ull) || t - t0 >= 3000000000ull) break;
|
||||
}
|
||||
if (last != c0 && asked) LOG("窗口:退出前等游戏存盘 %.0f ms,期间存档提交 %llu 次%s", (now_ns() - t0) / 1e6, (unsigned long long)(last - c0), paused ? "(原先停在暂停菜单 / 面板,已放开)" : "");
|
||||
else LOG("【红】窗口:未存盘退出——3 秒内没等到存档提交(%s%s)", asked ? "已请游戏存盘" : "一直没到能请存盘的状态(读取画面?)", paused ? ",原先暂停着、已放开" : "");
|
||||
dm_host_shutdown(why);
|
||||
}
|
||||
static const char *ui(const char *k) { const char *dm_ui(const char *key); return dm_ui(k); }
|
||||
static void show_keys(void) { NSAlert *a = [NSAlert new]; a.messageText = @(ui("dmAppKeysTitle")); a.informativeText = @(ui("dmAppKeysText")); [a addButtonWithTitle:@(ui("dmAppOK"))]; [a runModal]; }
|
||||
@interface DmWinCtl : NSObject <NSWindowDelegate> @end
|
||||
@implementation DmWinCtl
|
||||
- (BOOL)windowShouldClose:(NSWindow *)w { (void)w; app_quit("关窗口"); return NO; }
|
||||
- (void)windowDidResize:(NSNotification *)n { (void)n; layout_game(); }
|
||||
- (void)windowDidChangeBackingProperties:(NSNotification *)n { (void)n; layout_game(); }
|
||||
- (void)quit:(id)s { (void)s; app_quit("菜单退出"); }
|
||||
- (void)keys:(id)s { (void)s; show_keys(); }
|
||||
@end
|
||||
static uint64_t g_btn;
|
||||
static int g_w, g_a, g_s, g_d;
|
||||
|
||||
@@ -30,9 +74,10 @@ uint64_t front_game_res(int docked) {
|
||||
return docked ? 900ull << 32 | 1600 : 720ull << 32 | 1280;
|
||||
}
|
||||
|
||||
static int g_wheel; // 滚轮:+1 / −1 = 右摇杆向上 / 向下推一小会儿(原版手柄版用右摇杆滚列表) | Wheel: +1 / -1 = push the right stick up / down for a short while (the original gamepad UI scrolls lists with the right stick)
|
||||
static void push(void) {
|
||||
int32_t lx = (g_d - g_a) * 32767, ly = (g_w - g_s) * 32767;
|
||||
dm_input_set(g_btn, lx, ly, 0, 0);
|
||||
dm_input_set(g_btn, lx, ly, 0, g_wheel * 32767);
|
||||
}
|
||||
static int key_bit(unsigned short code) {
|
||||
switch (code) {
|
||||
@@ -69,18 +114,30 @@ void front_run(void (*guest)(void)) {
|
||||
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable
|
||||
backing:NSBackingStoreBuffered defer:NO];
|
||||
win.title = @"D2R Mobile";
|
||||
NSView *v = win.contentView;
|
||||
v.wantsLayer = YES;
|
||||
NSView *v = win.contentView; g_view = v;
|
||||
v.wantsLayer = YES; v.layer.backgroundColor = CGColorGetConstantColor(kCGColorBlack);
|
||||
win.collectionBehavior = NSWindowCollectionBehaviorFullScreenPrimary; win.contentMinSize = NSMakeSize(640, 360);
|
||||
static DmWinCtl *ctl; ctl = [DmWinCtl new]; win.delegate = ctl;
|
||||
g_layer = [CAMetalLayer layer];
|
||||
g_layer.device = MTLCreateSystemDefaultDevice();
|
||||
g_layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
|
||||
g_layer.contentsScale = win.backingScaleFactor;
|
||||
v.layer = g_layer;
|
||||
[v.layer addSublayer:g_layer]; layout_game();
|
||||
[win center];
|
||||
g_live = getenv("DM_APP") || getenv("DM_TOUCH_LIVE"); if (g_live) dm_touch_host_live_enable();
|
||||
{ // 菜单栏:应用(退出 ⌘Q)/ 显示(全屏 ⌃⌘F)/ 帮助(键位对照) | Menu bar: application (Quit ⌘Q) / View (Full Screen ⌃⌘F) / Help (key mapping)
|
||||
NSMenu *bar = [NSMenu new];
|
||||
NSMenu *(^add)(const char *) = ^NSMenu *(const char *t) { NSMenuItem *it = [NSMenuItem new]; [bar addItem:it]; NSMenu *m = [[NSMenu alloc] initWithTitle:@(t)]; it.submenu = m; return m; };
|
||||
[[add("") addItemWithTitle:@(ui("dmAppQuit")) action:@selector(quit:) keyEquivalent:@"q"] setTarget:ctl];
|
||||
NSMenuItem *fs = [add(ui("dmAppMenuView")) addItemWithTitle:@(ui("dmAppFullscreen")) action:@selector(toggleFullScreen:) keyEquivalent:@"f"]; fs.keyEquivalentModifierMask = NSEventModifierFlagControl | NSEventModifierFlagCommand;
|
||||
[[add(ui("dmAppMenuHelp")) addItemWithTitle:@(ui("dmAppKeysTitle")) action:@selector(keys:) keyEquivalent:@""] setTarget:ctl];
|
||||
NSApp.mainMenu = bar; }
|
||||
{ void dm_gamepad_start(void (*on_mode)(int pad)); dm_gamepad_start(NULL); } // 手柄(与 iOS 壳共用 ios/Sources/dm_gamepad.m):连上即用,与键盘并存 | Gamepad (ios/Sources/dm_gamepad.m, shared with the iOS shell): works as soon as it is connected, alongside the keyboard
|
||||
[win makeKeyAndOrderFront:nil];
|
||||
[NSApp activateIgnoringOtherApps:YES];
|
||||
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown | NSEventMaskKeyUp handler:^NSEvent *(NSEvent *e) {
|
||||
if (e.window != win) return e; // 只吃游戏窗口的键;软键盘输入框等其他窗口照常收字 | Only swallow keys for the game window; other windows such as the software keyboard input box receive typing as usual
|
||||
if (e.modifierFlags & NSEventModifierFlagCommand) return e; // 带 ⌘ 的留给菜单(⌘Q、⌃⌘F) | Keys with ⌘ are left to the menu (⌘Q, ⌃⌘F)
|
||||
if (!e.isARepeat) on_key(e, e.type == NSEventTypeKeyDown);
|
||||
return nil; // 吃掉,免得系统提示音 | Swallow it to avoid the system beep
|
||||
}];
|
||||
@@ -88,18 +145,39 @@ void front_run(void (*guest)(void)) {
|
||||
// Mouse as a single-point touchscreen (for testing touch on the local machine, front.h dm_touch_set): left button down / drag = finger 0 at the normalized coordinates, release = lifted
|
||||
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskLeftMouseDown | NSEventMaskLeftMouseDragged | NSEventMaskLeftMouseUp handler:^NSEvent *(NSEvent *e) {
|
||||
if (e.window != win) return e;
|
||||
if (e.type == NSEventTypeLeftMouseUp) { dm_touch_set(NULL, 0); return nil; }
|
||||
NSPoint p = [v convertPoint:e.locationInWindow fromView:nil]; NSSize sz = v.bounds.size;
|
||||
if (sz.width <= 0 || sz.height <= 0) return e;
|
||||
dm_touch t = {0, (float)(p.x / sz.width), (float)(1.0 - p.y / sz.height)}; // AppKit 视图原点在左下,触屏原点在左上 | The AppKit view origin is at bottom left, the touchscreen origin is at top left
|
||||
if (e.type == NSEventTypeLeftMouseUp) { if (g_live) dm_touch_host_live(2, 0, 0); else dm_touch_set(NULL, 0); return nil; }
|
||||
NSPoint p = [v convertPoint:e.locationInWindow fromView:nil]; NSRect gr = game_rect();
|
||||
if (gr.size.width <= 0 || gr.size.height <= 0) return e;
|
||||
float nx = (float)((p.x - gr.origin.x) / gr.size.width), ny = (float)(1.0 - (p.y - gr.origin.y) / gr.size.height); // AppKit 视图原点在左下,触屏原点在左上 | The AppKit view origin is at bottom left, the touchscreen origin is at top left
|
||||
if (e.type == NSEventTypeLeftMouseDown && (nx < 0 || nx > 1 || ny < 0 || ny > 1)) return nil; // 按在黑边上:不算点 | Pressed on the black bars: not a click
|
||||
dm_touch t = {0, nx < 0 ? 0 : nx > 1 ? 1 : nx, ny < 0 ? 0 : ny > 1 ? 1 : ny};
|
||||
if (g_live) { dm_touch_host_live(e.type == NSEventTypeLeftMouseDown ? 0 : 1, t.x, t.y); return nil; }
|
||||
dm_touch_set(&t, 1); return nil;
|
||||
}];
|
||||
[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowWillCloseNotification object:win queue:nil
|
||||
usingBlock:^(NSNotification *n) { (void)n; exit(0); }];
|
||||
// 右键 = 手柄 B(返回 / 关面板);滚轮 = 右摇杆上下推 0.12 秒(滚列表)。只在 App 形态接(命令行宿主的测试不受影响)
|
||||
// Right click = gamepad B (back / close panel); wheel = push the right stick up or down for 0.12 s (scroll lists). Only wired in App form (tests of the command-line host are unaffected)
|
||||
if (g_live) {
|
||||
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskRightMouseDown | NSEventMaskRightMouseUp handler:^NSEvent *(NSEvent *e) {
|
||||
if (e.window != win) return e;
|
||||
if (e.type == NSEventTypeRightMouseDown) g_btn |= DM_BTN_B; else g_btn &= ~(uint64_t)DM_BTN_B;
|
||||
push(); return nil;
|
||||
}];
|
||||
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskScrollWheel handler:^NSEvent *(NSEvent *e) {
|
||||
if (e.window != win || e.scrollingDeltaY == 0) return e;
|
||||
static int gen; int my = ++gen; g_wheel = e.scrollingDeltaY > 0 ? 1 : -1; push();
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.12 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (my == gen) { g_wheel = 0; push(); } });
|
||||
return nil;
|
||||
}];
|
||||
}
|
||||
if (getenv("DM_APP") && !getenv("DM_APP_ROMFS") && ![NSUserDefaults.standardUserDefaults boolForKey:@"KeysShown"]) { show_keys(); [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"KeysShown"]; } // 第一次启动给一次键位对照(界面上的按键提示是手柄字母);以后在「帮助」菜单里 | Show the key mapping once on first launch (button prompts on screen are gamepad letters); afterwards it is in the Help menu
|
||||
{ const char *e; // 测试(没人点窗口时用):DM_TEST_WINSIZE=宽x高 设内容区大小;DM_TEST_FULLSCREEN_AT=<秒> 进全屏;DM_TEST_QUIT_AT=<秒> 走一次退出 | Tests (for when nobody is there to click the window): DM_TEST_WINSIZE=WxH sets the content size; DM_TEST_FULLSCREEN_AT=<seconds> enters full screen; DM_TEST_QUIT_AT=<seconds> goes through one quit
|
||||
int w, h; if ((e = getenv("DM_TEST_WINSIZE")) && sscanf(e, "%dx%d", &w, &h) == 2) { [win setContentSize:NSMakeSize(w, h)]; layout_game(); }
|
||||
if ((e = getenv("DM_TEST_FULLSCREEN_AT"))) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(atof(e) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [win toggleFullScreen:nil]; });
|
||||
if ((e = getenv("DM_TEST_QUIT_AT"))) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(atof(e) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ app_quit("DM_TEST_QUIT_AT"); }); }
|
||||
// 生命周期(hle.h / dm.h):失焦 → 2、拿回焦点 → 1;最小化 → 3 + 音频暂停 + GPU 挂起,还原 → 恢复(随后拿回焦点 → 1)
|
||||
// Lifecycle (hle.h / dm.h): focus lost → 2, focus regained → 1; minimized → 3 + audio paused + GPU suspended, restored → resume (followed by focus regained → 1)
|
||||
NSNotificationCenter *nc = NSNotificationCenter.defaultCenter;
|
||||
[nc addObserverForName:NSWindowDidResignKeyNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; if (!win.miniaturized) dm_focus_set(2); }];
|
||||
[nc addObserverForName:NSWindowDidResignKeyNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; if (!win.miniaturized) dm_focus_set(2); g_btn = 0; g_w = g_a = g_s = g_d = 0; push(); { void dm_gamepad_reset(void); dm_gamepad_reset(); } }]; // 失焦:键盘和手柄按着的键清掉(松开事件收不到) | Focus lost: clear the keys held on keyboard and gamepad (their release events will not arrive)
|
||||
[nc addObserverForName:NSWindowDidBecomeKeyNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; dm_focus_set(1); }];
|
||||
[nc addObserverForName:NSWindowDidMiniaturizeNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; dm_focus_set(3); dm_audio_suspend(1); dm_gfx_suspend(1); }];
|
||||
[nc addObserverForName:NSWindowDidDeminiaturizeNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; dm_gfx_suspend(0); dm_audio_suspend(0); dm_focus_set(2); }];
|
||||
|
||||
@@ -167,7 +167,12 @@ void dm_host_preload_start(int argc, char **argv) {
|
||||
pthread_attr_t a; pthread_attr_init(&a); pthread_attr_setstacksize(&a, 8 << 20);
|
||||
if (pthread_create(&g_pre.th, &a, preload_thread, NULL)) die("dm_host_preload_start:起线程失败");
|
||||
}
|
||||
#include <TargetConditionals.h>
|
||||
int dm_app_bootstrap(int *argc, char ***argv); // app_mac.m:包成 .app 双击起时备好环境与参数;命令行起原样 | app_mac.m: when packaged as an .app and started by double-click, prepare the environment and arguments; command-line starts are unchanged
|
||||
int main(int argc, char **argv) {
|
||||
#if TARGET_OS_OSX // iOS 包也编这个文件(入口由外壳调),那边没有 app_mac.m | The iOS package compiles this file too (the entry is called by the shell); there is no app_mac.m on that side
|
||||
dm_app_bootstrap(&argc, &argv);
|
||||
#endif
|
||||
if (getenv("DM_PRELOAD")) dm_host_preload_start(argc, argv); // Mac 上验证提前起跑的路径(iOS 由 App 的 main() 调) | Exercise the early-start path on Mac (on iOS it is called from the App's main())
|
||||
if (atomic_load(&g_pre.started)) {
|
||||
pthread_join(g_pre.th, NULL);
|
||||
|
||||
+51
-1
@@ -28,6 +28,19 @@ static struct {
|
||||
dm_tc_state state;
|
||||
} g_touch;
|
||||
|
||||
// ---- 真鼠标(Mac App,front_mac.m):事件来自前端而不是脚本表,进同一套状态机。界面用原版手柄 HUD,屏上没有触屏圆钮与摇杆:
|
||||
// 非面板态一个按钮目标都不开、左下角不起摇杆(点哪里都只是「点世界」:点到目标走过去交互,点空地不动);面板态照旧(点控件、页签、物品格)。
|
||||
// 前端页(主菜单 / 选角 / 建角,不在局内)不经状态机,触点直接当界面点击(同 iOS 前端的 frontendMode)。
|
||||
// ---- Real mouse (Mac App, front_mac.m): events come from the front end rather than the script table and enter the same state machine. The UI is the original gamepad HUD, with no touch buttons or stick on screen:
|
||||
// outside panels no button target is opened and no stick is raised at the bottom left (a click anywhere is just "click the world": clicking a target walks over and interacts, clicking empty ground does nothing); in panels it works as before (clicking controls, tabs, item cells).
|
||||
// Front-end pages (main menu / character select / character creation, not in a game) bypass the state machine and the touch point is taken directly as a UI click (same as frontendMode in the iOS front end).
|
||||
#include <stdatomic.h>
|
||||
static int g_live_on; static uint32_t g_live_tick0; static int g_live_based;
|
||||
static struct { _Atomic unsigned head, tail; struct { int op; float nx, ny; } q[64]; } g_live;
|
||||
void dm_touch_host_live(int op, float nx, float ny) { // 前端线程:op 0 按下 1 移动 2 抬起;坐标 = 游戏画面归一化 [0,1] | Front-end thread: op 0 down, 1 move, 2 up; coordinates = normalized to the game picture [0,1]
|
||||
if (!g_live_on) return; unsigned t = atomic_load(&g_live.tail); if (t - atomic_load(&g_live.head) >= 64) return;
|
||||
g_live.q[t % 64].op = op; g_live.q[t % 64].nx = nx; g_live.q[t % 64].ny = ny; atomic_store(&g_live.tail, t + 1);
|
||||
}
|
||||
static void touch_uimsg(const char *cat, const char *act, const char *arg) {
|
||||
static dm_tc_scene scene;
|
||||
if (!strcmp(cat, "DM") && !strcmp(act, "ReplayDone") && g_touch.prefix)
|
||||
@@ -37,7 +50,7 @@ static void touch_uimsg(const char *cat, const char *act, const char *arg) {
|
||||
if (g_touch.enabled) {
|
||||
dm_tc_set_navigation_mode(&g_touch.state, navigation);
|
||||
static int pad = -1; if (pad < 0) { const char *e = getenv("DM_TOUCH_NAV_PAD"); pad = e && e[0] == '1'; } // 缺省不给圆钮(同 front_ios.m) | no round button by default (same as front_ios.m)
|
||||
uint32_t mask = navigation ? (dm_tc_nav_mask(pad && !dm_tc_scene_dead()) | (dm_tc_scene_dialog_only(&scene) ? 1u << DM_TC_MENU : 0)) : ((1u << DM_TC_COUNT) - 1);
|
||||
uint32_t mask = navigation ? (dm_tc_nav_mask(pad && !dm_tc_scene_dead()) | (dm_tc_scene_dialog_only(&scene) ? 1u << DM_TC_MENU : 0)) : g_live_on ? 0 : ((1u << DM_TC_COUNT) - 1);
|
||||
dm_tc_enable_targets(&g_touch.state, mask);
|
||||
}
|
||||
if (!strcmp(cat, "DM") && !strcmp(act, "ReplayDone") && g_touch.prefix && !g_touch.started) {
|
||||
@@ -108,6 +121,30 @@ static void touch_init(uint64_t now) {
|
||||
fprintf(stderr, "[dm] DM_TOUCH_SCRIPT ← %s:%d 条,按服务端游戏拍推进(脚本 25 拍/秒)%s\n", path, g_touch.count, g_touch.drop ? "(测试丢弃输出)" : "");
|
||||
}
|
||||
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
static void *live_test_thread(void *arg) {
|
||||
FILE *f = fopen((const char *)arg, "r"); if (!f) { fprintf(stderr, "[dm] 【红】DM_TEST_LIVE 打不开 %s\n", (const char *)arg); return NULL; }
|
||||
char line[160]; double t0 = (double)now_ns() / 1e9; int n = 0;
|
||||
while (fgets(line, sizeof line, f)) { double sec; char op[8]; float x = 0, y = 0; if (line[0] == '#' || sscanf(line, "%lf %7s %f,%f", &sec, op, &x, &y) < 2) continue;
|
||||
double wait = sec - ((double)now_ns() / 1e9 - t0); if (wait > 0) usleep((useconds_t)(wait * 1e6));
|
||||
dm_touch_host_live(!strcmp(op, "down") ? 0 : !strcmp(op, "move") ? 1 : 2, x / 2868, y / 1320); n++; }
|
||||
fclose(f); fprintf(stderr, "[dm] DM_TEST_LIVE:%d 条发完\n", n); return NULL;
|
||||
}
|
||||
void dm_touch_host_step(uint64_t now, uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry);
|
||||
void dm_touch_host_live_enable(void) { // 前端开窗后、客体线程起来之前调一次 | Called once after the front end opens the window and before the guest threads start
|
||||
if (g_live_on) return;
|
||||
if (!g_touch.ready) touch_init(0); // 同时带了触点脚本(测试)就照常装载 | If a touch script (test) is given as well, it is loaded as usual
|
||||
if (!g_touch.enabled) { dm_tc_init(&g_touch.state, 2868, 1320); g_touch.state.mouse_click_mode = 1; g_touch.enabled = 1;
|
||||
void uimsg_subscribe(void (*fn)(const char *, const char *, const char *)); uimsg_subscribe(touch_uimsg); }
|
||||
dm_tc_set_joy_block(&g_touch.state, 1); dm_tc_enable_targets(&g_touch.state, 0);
|
||||
g_live_on = 1; dm_pad_hook = dm_touch_host_step; fprintf(stderr, "[dm] 鼠标:接到触屏状态机(点目标 / 点界面;没有触屏圆钮与摇杆)\n");
|
||||
// 测试开关 DM_TEST_LIVE=<文件>(没人操作窗口时验这条路):每行「秒 down|move|up x,y」,坐标按 2868×1320,秒 = 从这里起的墙钟;到点调上面的 dm_touch_host_live,
|
||||
// 与真鼠标只差「系统鼠标事件 → 归一化坐标」那三行
|
||||
// Test switch DM_TEST_LIVE=<file> (to verify this path when nobody operates the window): each line is "seconds down|move|up x,y", coordinates on a 2868×1320 basis, seconds = wall clock from here; at that time the dm_touch_host_live above is called,
|
||||
// differing from a real mouse only by the three lines "system mouse event -> normalized coordinates"
|
||||
if (getenv("DM_TEST_LIVE")) { pthread_t th; pthread_create(&th, NULL, live_test_thread, strdup(getenv("DM_TEST_LIVE"))); pthread_detach(th); }
|
||||
}
|
||||
int dm_touch_host_enabled(void) {
|
||||
return getenv("DM_TOUCH_SCRIPT") && *getenv("DM_TOUCH_SCRIPT");
|
||||
}
|
||||
@@ -119,6 +156,19 @@ void dm_touch_host_game_start(uint64_t now) {
|
||||
|
||||
void dm_touch_host_step(uint64_t now, uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) {
|
||||
if (!g_touch.ready) touch_init(now);
|
||||
if (g_live_on) { // 真鼠标:前端页直通界面点击;局内并进事件表(拍 = 此刻),与脚本事件同一段处理 | Real mouse: on front-end pages it goes straight to a UI click; in game it joins the event table (tick = now) and is handled in the same pass as script events
|
||||
int dm_uiscreen_ingame(void); int in = dm_uiscreen_ingame(); static float px, py; unsigned h;
|
||||
if (in && !g_live_based) { g_live_based = 1; g_live_tick0 = touch_cur_tick(); if (!g_touch.based) { g_touch.started = 1; g_touch.based = 1; g_touch.loaded = 1; g_touch.tick_set = 1; g_touch.start_tick = g_live_tick0; g_touch.to_game_tick = g_live_tick0; } }
|
||||
if (!in) g_live_based = 0;
|
||||
while ((h = atomic_load(&g_live.head)) != atomic_load(&g_live.tail)) {
|
||||
int op = g_live.q[h % 64].op; float nx = g_live.q[h % 64].nx, ny = g_live.q[h % 64].ny; if (op != 2) { px = nx; py = ny; }
|
||||
if (!in) { dm_ui_touch(op, px, py); atomic_store(&g_live.head, h + 1); continue; }
|
||||
if (g_touch.next == g_touch.count) g_touch.next = g_touch.count = 0; // 表里的都处理完了:从头复用 | Everything in the table has been handled: reuse from the start
|
||||
if (g_touch.count >= 256) break;
|
||||
touch_event *e = &g_touch.event[g_touch.count++]; e->beat = 0; e->slot = 0; e->op = op == 0 ? TOUCH_DOWN : op == 1 ? TOUCH_MOVE : TOUCH_UP; e->x = px * 2868; e->y = py * 1320;
|
||||
atomic_store(&g_live.head, h + 1);
|
||||
}
|
||||
}
|
||||
if (!g_touch.enabled || !g_touch.started) return;
|
||||
uint32_t tick = touch_cur_tick();
|
||||
if (!g_touch.prefix && !g_touch.tick_set) { g_touch.to_game_tick = tick; g_touch.tick_set = 1; }
|
||||
|
||||
Reference in New Issue
Block a user