dsh-zoo 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/desktop/farm.js CHANGED
@@ -390,6 +390,7 @@ function renderVisitorPlots () {
390
390
  crop: plot.crop,
391
391
  cropName: meta === null ? '空地' : meta.name,
392
392
  emoji: meta === null ? '+' : meta.emoji,
393
+ art: meta === null ? '' : meta.art,
393
394
  badge: stage === 'mature' ? '可收割' : stage === 'growing' ? '生长中' : '空地',
394
395
  mature: stage === 'mature',
395
396
  progress: view.progressOf(plot),
@@ -438,6 +439,7 @@ function renderLookPlots () {
438
439
  crop: plot.crop,
439
440
  cropName: meta === null ? '空地' : meta.name,
440
441
  emoji: meta === null ? '+' : meta.emoji,
442
+ art: meta === null ? '' : meta.art,
441
443
  badge: stage === 'mature' ? '可收割' : stage === 'growing' ? '生长中' : '空地',
442
444
  mature: stage === 'mature',
443
445
  progress: view.progressOf(plot),
@@ -626,9 +628,7 @@ function stackChip (meta, count, kind, cropId) {
626
628
  chip.className = 'stack-chip' + (count <= 0 ? ' muted' : '')
627
629
  chip.dataset.stackKind = kind
628
630
  chip.dataset.crop = cropId
629
- const emoji = document.createElement('span')
630
- emoji.setAttribute('aria-hidden', 'true')
631
- emoji.textContent = meta.emoji
631
+ const emoji = view.cropEmblem(document, meta, 'stack-emoji')
632
632
  const name = document.createElement('span')
633
633
  name.textContent = meta.name
634
634
  const value = document.createElement('b')
@@ -732,8 +732,9 @@ function renderCarried () {
732
732
  } else if (entry !== null && typeof entry === 'object' && entry.kind === 'crop') {
733
733
  const crop = cropMeta(entry.crop)
734
734
  const qty = Number(entry.qty)
735
- item.textContent = (crop === null ? '作物' : crop.emoji + ' ' + crop.name) +
736
- ' ×' + (Number.isFinite(qty) && qty > 0 ? Math.floor(qty) : 1) + ' · 在驮包里'
735
+ item.appendChild(view.cropEmblem(document, crop, 'carried-art'))
736
+ item.appendChild(document.createTextNode(' ' + (crop === null ? '作物' : crop.name) +
737
+ ' ×' + (Number.isFinite(qty) && qty > 0 ? Math.floor(qty) : 1) + ' · 在驮包里'))
737
738
  } else {
738
739
  continue
739
740
  }
@@ -886,10 +887,7 @@ function renderMarket () {
886
887
 
887
888
  const head = document.createElement('div')
888
889
  head.className = 'market-row-head'
889
- const emoji = document.createElement('span')
890
- emoji.className = 'emoji'
891
- emoji.setAttribute('aria-hidden', 'true')
892
- emoji.textContent = meta.emoji
890
+ const emoji = view.cropEmblem(document, meta, 'emoji')
893
891
  const name = document.createElement('span')
894
892
  name.className = 'name'
895
893
  name.textContent = meta.name
@@ -1493,10 +1491,7 @@ function renderPlantModal () {
1493
1491
  row.disabled = busy || count <= 0
1494
1492
  if (count <= 0) row.title = '没有这种种子了'
1495
1493
  row.setAttribute('aria-label', meta.name + ',库存 ' + count)
1496
- const emoji = document.createElement('span')
1497
- emoji.className = 'seed-emoji'
1498
- emoji.setAttribute('aria-hidden', 'true')
1499
- emoji.textContent = meta.emoji
1494
+ const emoji = view.cropEmblem(document, meta, 'seed-emoji')
1500
1495
  const info = document.createElement('span')
1501
1496
  info.className = 'seed-info'
1502
1497
  const name = document.createElement('strong')
@@ -1998,6 +1993,9 @@ document.addEventListener('keydown', (event) => {
1998
1993
 
1999
1994
  api?.onState((payload) => {
2000
1995
  if (payload === null || typeof payload !== 'object') return
1996
+ // 票 59:作物目录随每一包下发(内置六种 + `<dataDir>/crops.json` 里手写的)。
1997
+ // 先换目录再画,不然新作物会被当成未知 id 画成空地。
1998
+ view.setCrops(payload.crops)
2001
1999
  farm = payload.farm !== null && typeof payload.farm === 'object' ? payload.farm : null
2002
2000
  market = payload.market !== null && typeof payload.market === 'object' ? payload.market : null
2003
2001
  contracts = payload.contracts !== null && typeof payload.contracts === 'object' ? payload.contracts : null
@@ -685,14 +685,36 @@
685
685
  font-size: 12px;
686
686
  font-weight: 700;
687
687
  line-height: 1.2;
688
- cursor: pointer;
688
+ /* 能拖走(ticket 57):抓取手势让“可以挪开”这件事自己说出来。 */
689
+ cursor: grab;
689
690
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.32);
690
691
  pointer-events: auto;
692
+ user-select: none;
691
693
  }
692
- .lp-recall-chip .lp-chip-sub {
693
- font-size: 10.5px;
694
+ .lp-recall-chip[data-dragging="true"] {
695
+ cursor: grabbing;
696
+ }
697
+ .lp-recall-chip .lp-chip-main {
698
+ max-width: 168px;
699
+ overflow: hidden;
700
+ text-overflow: ellipsis;
701
+ white-space: nowrap;
702
+ }
703
+ .lp-recall-chip .lp-chip-caret {
704
+ font-size: 10px;
705
+ line-height: 1;
706
+ color: rgba(244, 248, 255, 0.6);
707
+ }
708
+ /* ticket 57:「不在家」面板里那句“下一步会怎样”。比按钮淡,但必须能读。 */
709
+ .lp-away-panel .lp-away-status {
710
+ padding: 0 2px 2px;
711
+ font-size: 11px;
694
712
  font-weight: 500;
695
- color: rgba(244, 248, 255, 0.66);
713
+ line-height: 1.45;
714
+ color: rgba(244, 248, 255, 0.7);
715
+ }
716
+ .lp-away-panel .lp-popover-title {
717
+ white-space: normal;
696
718
  }
697
719
  .lp-notice {
698
720
  max-width: 246px;
package/desktop/main.js CHANGED
@@ -52,6 +52,8 @@ const { app, BrowserWindow, clipboard, dialog, ipcMain, protocol, screen, shell
52
52
  const shared = require('./shared.js')
53
53
  // `dshzoo://gifts/...` 的路径白名单(ticket 25):纯函数,单独一份、可单测。
54
54
  const giftPath = require('./gift-path.js')
55
+ // `dshzoo://crops/<hash>.<ext>` 的白名单(票 59):同样是一份纯函数、单独可单测。
56
+ const cropPath = require('./crop-path.js')
55
57
 
56
58
  const APP_NAME = 'dsh-zoo-desktop'
57
59
  const HOST_STALE_MS = 30000
@@ -63,8 +65,12 @@ const ASSET_HOST = 'asset'
63
65
  const CACHE_HOST = 'cache'
64
66
  /** 礼物缩略图主机:与 asset/cache 同一套注册好的 dshzoo:// scheme,只多一个 host。 */
65
67
  const GIFT_HOST = 'gifts'
68
+ /** 作物图主机(票 59):`dshzoo://crops/<内容哈希>.<svg|png>`。 */
69
+ const CROP_HOST = 'crops'
66
70
  const MAX_ASSET_BYTES = 8 * 1024 * 1024
67
71
  const MAX_ASSET_NAME = 200
72
+ /** 作物图上限:宿主侧按 256 KB 收,这里放宽一档只做兜底,防止把整份大文件读进内存。 */
73
+ const MAX_CROP_ART_BYTES = 1024 * 1024
68
74
 
69
75
  // 必须先定名:直接用 `electron main.js` 启动时 Electron 取不到 app 名会回落成 "Electron",
70
76
  // userData 就落到所有脚本共用的目录里。
@@ -98,6 +104,13 @@ const cacheRoot = typeof options.cache === 'string' && options.cache !== '' && p
98
104
  const giftsRoot = typeof options.gifts === 'string' && options.gifts !== '' && path.isAbsolute(options.gifts)
99
105
  ? options.gifts
100
106
  : null
107
+ /**
108
+ * 作物图根目录(票 59):宿主把 `<dataDir>/crop-art` 传进来;helper 只读它,
109
+ * 只为 `dshzoo://crops/<hash>.<ext>` 服务。没传 = 不注册这条读路径(所有 crops URL 404)。
110
+ */
111
+ const cropsRoot = typeof options.crops === 'string' && options.crops !== '' && path.isAbsolute(options.crops)
112
+ ? options.crops
113
+ : null
101
114
  if (isShotMode && !path.isAbsolute(options.shot)) {
102
115
  process.stderr.write('[dsh-zoo helper] --dsh-zoo-shot 需要绝对路径:' + options.shot + '\n')
103
116
  process.exit(3)
@@ -150,6 +163,8 @@ let giftSeq = 0
150
163
  let farmWin = null
151
164
  /** 宿主交下来的完整 farm view(地块/种子/作物/硬币/露水/日志),窗口打开或刷新时原样转发。 */
152
165
  let farmState = null
166
+ /** 票 59:作物目录随每一包下发;窗口按它画种子列表/行情/地块。 */
167
+ let farmCrops = null
153
168
  /** 交易所页 / `farm-window` 里随农场一起下发的 market view(价目/历史/index)。 */
154
169
  let farmMarket = null
155
170
  /** 雇工页(ticket 44):合同/邻居挂牌/在飞状态,和 farm 一起下发、一起缓存。 */
@@ -177,6 +192,13 @@ let farmSeq = 0
177
192
  let workArea = null
178
193
  let scene = shared.emptyScene()
179
194
  let sceneAt = 0
195
+ /**
196
+ * 这只宠物最后一次被看见时叫什么(ticket 57)。
197
+ *
198
+ * 改名小窗预填的是「当前名字」。宠物出门/打工时桌面上没有 `self`,只看 scene 会得到一个
199
+ * 空输入框——而「不在家」面板里就有改名入口(ticket 57),空着会被当成坏了。
200
+ */
201
+ let lastSelfLabel = ''
180
202
  /** 渲染端上报的当前素材与失败集合;只用于诊断(宿主 /dsh-zoo/desktop 能读到)。 */
181
203
  let lastMedia = null
182
204
  const mediaHistory = []
@@ -466,6 +488,7 @@ async function handleAssetRequest (request) {
466
488
  if (url.protocol !== ASSET_SCHEME + ':') return textResponse(404, 'unknown asset')
467
489
  // 礼物缩略图(ticket 25):host=gifts,路径恰好两段;守卫与夹取都在 gift-path.js 里。
468
490
  if (url.hostname === GIFT_HOST) return handleGiftRequest(url)
491
+ if (url.hostname === CROP_HOST) return handleCropRequest(url)
469
492
  if (url.hostname !== ASSET_HOST && url.hostname !== CACHE_HOST) {
470
493
  return textResponse(404, 'unknown asset')
471
494
  }
@@ -519,6 +542,48 @@ function textResponse (status, body) {
519
542
  return new Response(body, { status, headers: { 'content-type': 'text/plain; charset=utf-8' } })
520
543
  }
521
544
 
545
+ /**
546
+ * `dshzoo://crops/<内容哈希>.<svg|png>`(票 59)。
547
+ * 名字只认 16 位十六进制哈希 + 两种扩展名,文件必须落在 crop-art 根目录里;
548
+ * 读不到一律 404(不区分"没这张图"和"名字不合法")。
549
+ * 内容寻址,所以可以缓存:同一张图换个作物 id 不会换 URL。
550
+ */
551
+ async function handleCropRequest (url) {
552
+ try {
553
+ if (cropsRoot === null) return textResponse(404, 'crop art not found')
554
+ let name
555
+ try {
556
+ name = decodeURIComponent(url.pathname.replace(/^\/+/, ''))
557
+ } catch {
558
+ return textResponse(400, 'bad crop art name')
559
+ }
560
+ const contentType = cropPath.cropArtContentType(name)
561
+ if (contentType === null) return textResponse(404, 'crop art not found')
562
+ const file = resolveCropArtFile(name)
563
+ if (file === null) return textResponse(404, 'crop art not found')
564
+ let info
565
+ try {
566
+ info = await fsp.stat(file)
567
+ } catch {
568
+ return textResponse(404, 'crop art not found')
569
+ }
570
+ if (!info.isFile() || info.size <= 0 || info.size > MAX_CROP_ART_BYTES) {
571
+ return textResponse(404, 'crop art not found')
572
+ }
573
+ const data = await fsp.readFile(file)
574
+ return new Response(new Uint8Array(data), {
575
+ headers: {
576
+ 'content-type': contentType,
577
+ 'content-length': String(data.length),
578
+ // 内容寻址:同名即同图,可以放心让 Chromium 缓存。
579
+ 'cache-control': 'public, max-age=300'
580
+ }
581
+ })
582
+ } catch {
583
+ return textResponse(404, 'crop art not found')
584
+ }
585
+ }
586
+
522
587
  /**
523
588
  * `dshzoo://gifts/<礼物目录>/<图片>`(ticket 25)。
524
589
  * 只认两层、只认四种图片扩展名、只认 gifts 根目录下的文件;读不到一律 404(不区分原因)。
@@ -594,6 +659,25 @@ function isSafeCacheName (name) {
594
659
  return typeof name === 'string' && /^[0-9a-f]{64}\.webm$/.test(name)
595
660
  }
596
661
 
662
+ function resolveCropArtFile (name) {
663
+ if (cropsRoot === null || !cropPath.isCropArtName(name)) return null
664
+ let base
665
+ try {
666
+ base = path.resolve(cropsRoot)
667
+ } catch {
668
+ return null
669
+ }
670
+ const candidate = path.resolve(base, name)
671
+ if (candidate === base || !candidate.startsWith(base + path.sep)) return null
672
+ try {
673
+ const stat = fs.statSync(candidate)
674
+ if (stat.isFile() && stat.size > 0 && stat.size <= MAX_CROP_ART_BYTES) return candidate
675
+ } catch {
676
+ return null
677
+ }
678
+ return null
679
+ }
680
+
597
681
  function resolveCacheFile (name) {
598
682
  if (cacheRoot === null || !isSafeCacheName(name)) return null
599
683
  let base
@@ -898,9 +982,9 @@ ipcMain.on('dsh-zoo:action', (event, payload) => {
898
982
  const id = shared.normalizeId(payload?.id)
899
983
  const action = typeof payload?.action === 'string' ? payload.action.trim().toLowerCase() : ''
900
984
  if (id === null || !shared.DESKTOP_ACTIONS.includes(action)) return
901
- const needsDevice = action === 'end-visit' || action === 'admit' || action === 'reject' || action === 'dismiss' ||
902
- // 票 54:看田与派它去偷都**必须有对象**——右键的是哪一户,就看/偷哪一户。
903
- action === 'farm-look' || action === 'farm-raid'
985
+ // 谁必须带对象只写一份(`shared.DEVICE_ACTIONS`):票 54 的两个农场动作曾经只加在这一行,
986
+ // 宿主那一侧的白名单没跟上,deviceId 到宿主就变成 null 了。
987
+ const needsDevice = shared.DEVICE_ACTIONS.includes(action)
904
988
  // `visit` 是「可能带对象」:从邻居面板点某一户就带 deviceId;没有就交给宿主随机挑一位
905
989
  // (HTTP 口的老行为)。needsDevice 的那几个动作则必须有对象,缺了直接拒绝。
906
990
  const deviceId = needsDevice || action === 'visit' ? shared.normalizeId(payload?.deviceId) : null
@@ -1226,7 +1310,9 @@ function renameBounds () {
1226
1310
 
1227
1311
  function currentName () {
1228
1312
  const self = scene.items.find((item) => item.id === 'self')
1229
- return typeof self?.label === 'string' ? self.label : ''
1313
+ if (typeof self?.label === 'string' && self.label !== '') return self.label
1314
+ // 不在家:用最后一次见到的名字预填,别给一个空输入框。
1315
+ return lastSelfLabel
1230
1316
  }
1231
1317
 
1232
1318
  function openRenameWindow () {
@@ -1651,6 +1737,7 @@ function sendFarmState () {
1651
1737
  farmWin.webContents.send('dsh-zoo:farm-state', {
1652
1738
  farm: farmState,
1653
1739
  market: farmMarket,
1740
+ crops: farmCrops,
1654
1741
  contracts: farmContracts,
1655
1742
  mode: farmMode,
1656
1743
  visit: farmVisit,
@@ -1784,6 +1871,11 @@ function applyCommand (command) {
1784
1871
  case 'update': {
1785
1872
  scene = command.scene
1786
1873
  sceneAt = Date.now()
1874
+ // 记下最后一次见到的名字:宠物出门后桌面上没有 self,改名小窗也要能预填(ticket 57)。
1875
+ const selfItem = scene.items.find((item) => item.id === 'self')
1876
+ if (selfItem !== undefined && typeof selfItem.label === 'string' && selfItem.label !== '') {
1877
+ lastSelfLabel = selfItem.label
1878
+ }
1787
1879
  // ticket 33:换皮之后素材目录跟着场景走。空数组 = 这次没说,沿用启动参数
1788
1880
  // (旧宿主不会带这个字段,helper 不能因此把根目录清空)。
1789
1881
  if (Array.isArray(scene.assetRoots) && scene.assetRoots.length > 0) {
@@ -1891,6 +1983,7 @@ function applyCommand (command) {
1891
1983
  }
1892
1984
  farmState = command.farm
1893
1985
  farmMarket = command.market
1986
+ farmCrops = Array.isArray(command.crops) ? command.crops : null
1894
1987
  farmContracts = command.contracts ?? null
1895
1988
  farmMode = command.mode === 'look' ? 'look' : command.mode === 'visitor' ? 'visitor' : 'owner'
1896
1989
  farmVisit = command.visit ?? null
@@ -2705,7 +2798,8 @@ function parseArgs (argv) {
2705
2798
  debug: false,
2706
2799
  assets: [],
2707
2800
  cache: null,
2708
- gifts: null
2801
+ gifts: null,
2802
+ crops: null
2709
2803
  }
2710
2804
  for (let index = 0; index < argv.length; index += 1) {
2711
2805
  const arg = argv[index]
@@ -2734,6 +2828,11 @@ function parseArgs (argv) {
2734
2828
  index += 1
2735
2829
  } else if (arg.startsWith('--dsh-zoo-cache=')) {
2736
2830
  parsed.cache = arg.slice('--dsh-zoo-cache='.length)
2831
+ } else if (arg === '--dsh-zoo-crops') {
2832
+ parsed.crops = argv[index + 1] ?? null
2833
+ index += 1
2834
+ } else if (arg.startsWith('--dsh-zoo-crops=')) {
2835
+ parsed.crops = arg.slice('--dsh-zoo-crops='.length)
2737
2836
  } else if (arg === '--dsh-zoo-gifts') {
2738
2837
  if (typeof argv[index + 1] === 'string') parsed.gifts = argv[index + 1]
2739
2838
  index += 1
@@ -67,7 +67,9 @@ contextBridge.exposeInMainWorld('dshZooDesktop', {
67
67
  giftId: payload.giftId,
68
68
  // 农场(ticket 40):开窗可带 plotId,四个地块动作必须带;形状由 shared 归一化。
69
69
  plotId: payload.plotId,
70
- cropId: payload.cropId
70
+ cropId: payload.cropId,
71
+ // 合同的「叫它回家」(ticket 57):只带合同号;钱与结算全在宿主。
72
+ contractId: payload.contractId
71
73
  })
72
74
  },
73
75
  /**