@dan-uni/dan-any 0.0.2 → 0.0.5

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.
@@ -291,6 +291,23 @@ export declare class UniDM {
291
291
  * - Artplayer: style不为空时,将其JSON.stringify()存入
292
292
  */
293
293
  extraStr?: string | undefined, DMID?: string | undefined);
294
+ static create(args: Partial<{
295
+ FCID: string;
296
+ progress: number;
297
+ mode: Modes;
298
+ fontsize: number;
299
+ color: number;
300
+ senderID: string;
301
+ content: string;
302
+ ctime: Date;
303
+ weight: number;
304
+ pool: Pools;
305
+ attr: DMAttr[];
306
+ platform: platfrom | string;
307
+ SPMO: string;
308
+ extra: string;
309
+ DMID: string;
310
+ }>): UniDM;
294
311
  get extra(): Extra;
295
312
  get isFrom3rdPlatform(): boolean;
296
313
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dan-uni/dan-any",
3
- "version": "0.0.2",
4
- "description": "The toolbox of dan-uni.",
3
+ "version": "0.0.5",
4
+ "description": "A danmaku transformer lib, supporting danmaku from different platforms.",
5
5
  "keywords": [
6
6
  "bangumi",
7
7
  "danmaku"
package/src/index.ts CHANGED
@@ -60,8 +60,54 @@ export interface DM_JSON_DDPlay {
60
60
  m: string
61
61
  }[]
62
62
  }
63
+
64
+ export type DM_format =
65
+ | 'danuni.json'
66
+ | 'danuni.bin'
67
+ | 'danuni.pb.zst'
68
+ | 'bili.xml'
69
+ | 'bili.bin'
70
+ | 'bili.cmd.bin'
71
+ | 'dplayer.json'
72
+ | 'artplayer.json'
73
+ | 'ddplay.json'
74
+
63
75
  export class UniPool {
64
76
  constructor(public dans: UniDM[]) {}
77
+ static create() {
78
+ return new UniPool([])
79
+ }
80
+ assign(dans: UniPool | UniDM | UniDM[]) {
81
+ if (dans instanceof UniPool) {
82
+ return new UniPool([...this.dans, ...dans.dans])
83
+ } else if (dans instanceof UniDM) {
84
+ return new UniPool([...this.dans, dans])
85
+ } else if (Array.isArray(dans) && dans.every((d) => d instanceof UniDM)) {
86
+ return new UniPool([...this.dans, ...dans])
87
+ } else return this
88
+ }
89
+ convert2(format: DM_format) {
90
+ switch (format) {
91
+ case 'danuni.json':
92
+ return JSON.stringify(this.dans)
93
+ case 'danuni.bin':
94
+ return this.toPb()
95
+ // case 'bili.xml':
96
+ // return this.toBiliXML()
97
+ // case 'bili.bin':
98
+ // return this.toBiliBin()
99
+ // case 'bili.cmd.bin':
100
+ // return this.toBiliCmdBin()
101
+ case 'dplayer.json':
102
+ return this.toDplayer()
103
+ case 'artplayer.json':
104
+ return this.toArtplayer()
105
+ case 'ddplay.json':
106
+ return this.toDDplay()
107
+ default:
108
+ throw new Error('unknown format or unsupported now!')
109
+ }
110
+ }
65
111
  static fromPb(bin: Uint8Array | ArrayBuffer) {
66
112
  const data = fromBinary(DanmakuReplySchema, new Uint8Array(bin))
67
113
  return new UniPool(
@@ -342,6 +342,43 @@ export class UniDM {
342
342
  // if (attr < 0 || attr > 0b111) this.attr = 0
343
343
  if (!DMID) DMID = this.toDMID()
344
344
  }
345
+ static create(
346
+ args: Partial<{
347
+ FCID: string
348
+ progress: number
349
+ mode: Modes
350
+ fontsize: number
351
+ color: number
352
+ senderID: string
353
+ content: string
354
+ ctime: Date
355
+ weight: number
356
+ pool: Pools
357
+ attr: DMAttr[]
358
+ platform: platfrom | string
359
+ SPMO: string
360
+ extra: string
361
+ DMID: string
362
+ }>,
363
+ ) {
364
+ return new UniDM(
365
+ args.FCID || ID.fromNull().toString(),
366
+ args.progress,
367
+ args.mode,
368
+ args.fontsize,
369
+ args.color,
370
+ args.senderID,
371
+ args.content,
372
+ args.ctime,
373
+ args.weight,
374
+ args.pool,
375
+ args.attr,
376
+ args.platform,
377
+ args.SPMO,
378
+ args.extra,
379
+ args.DMID,
380
+ )
381
+ }
345
382
  get extra() {
346
383
  const extra = JSON.parse(this.extraStr || '{}')
347
384
  return (typeof extra === 'object' ? extra : {}) as Extra
@@ -476,46 +513,48 @@ export class UniDM {
476
513
  // if (args.mode === 7) extra.bili.adv = args.content
477
514
  // else if (args.mode === 8) extra.bili.code = args.content
478
515
  // else if (args.mode === 9) extra.bili.bas = args.content
479
- return new UniDM(
480
- FCID.toString(),
481
- args.progress,
516
+ return this.create({
517
+ ...args,
518
+ FCID: FCID.toString(),
519
+ // progress: args.progress,
482
520
  mode,
483
- args.fontsize,
484
- args.color,
485
- senderID.toString(),
486
- args.content,
487
- this.transCtime(args.ctime),
488
- args.weight ? args.weight : pool === Pools.Ix ? 1 : 0,
521
+ // fontsize: args.fontsize,
522
+ // color: args.color,
523
+ senderID: senderID.toString(),
524
+ // content: args.content,
525
+ ctime: this.transCtime(args.ctime),
526
+ weight: args.weight ? args.weight : pool === Pools.Ix ? 1 : 0,
489
527
  pool,
490
- DMAttrUtils.fromBin(args.attr, 'bili'),
491
- domainPreset.bili,
528
+ attr: DMAttrUtils.fromBin(args.attr, 'bili'),
529
+ platform: domainPreset.bili,
492
530
  SPMO,
493
531
  // 需改进,7=>advanced 8=>code 9=>bas 互动=>command
494
532
  // 同时塞进无法/无需直接解析的数据
495
533
  // 另开一个解析器,为大部分播放器(无法解析该类dm)做文本类型降级处理
496
- args.mode >= 7 ? JSON.stringify(extra, BigIntSerializer) : undefined,
497
- )
534
+ extra:
535
+ args.mode >= 7 ? JSON.stringify(extra, BigIntSerializer) : undefined,
536
+ })
498
537
  }
499
538
  static fromBiliCommand(args: DMBiliCommand, SPMO?: string, cid?: bigint) {
500
539
  if (args.oid && !cid) cid = args.oid
501
540
  const FCID = ID.fromBili({ cid }),
502
541
  senderID = ID.fromBili({ mid: args.mid })
503
- return new UniDM(
504
- FCID.toString(),
505
- args.progress,
506
- Modes.Ext,
507
- 0,
508
- 0,
509
- senderID.toString(),
510
- args.content,
511
- // BigInt(Date.parse(args.ctime + ' GMT+0800')), // 无视本地时区,按照B站的东8区计算时间
512
- new Date(`${args.ctime} GMT+0800`), // 无视本地时区,按照B站的东8区计算时间
513
- 10,
514
- Pools.Adv,
515
- ['Protect'],
516
- domainPreset.bili,
542
+ return this.create({
543
+ ...args,
544
+ FCID: FCID.toString(),
545
+ // progress: args.progress,
546
+ mode: Modes.Ext,
547
+ // fontsize: args.fontsize,
548
+ // color: args.color,
549
+ senderID: senderID.toString(),
550
+ // content: args.content,
551
+ ctime: new Date(`${args.ctime} GMT+0800`), // 无视本地时区,按照B站的东8区计算时间
552
+ weight: 10,
553
+ pool: Pools.Adv,
554
+ attr: ['Protect'],
555
+ platform: domainPreset.bili,
517
556
  SPMO,
518
- JSON.stringify(
557
+ extra: JSON.stringify(
519
558
  {
520
559
  bili: {
521
560
  command: args,
@@ -523,25 +562,22 @@ export class UniDM {
523
562
  },
524
563
  BigIntSerializer,
525
564
  ),
526
- )
565
+ })
527
566
  }
528
567
  static fromDplayer(args: DMDplayer, playerID: string, domain: string) {
529
568
  const FCID = ID.fromUnknown(playerID, domain),
530
569
  senderID = ID.fromUnknown(args.midHash, domain)
531
- return new UniDM(
532
- FCID.toString(),
533
- args.progress,
534
- this.transMode(args.mode, 'dplayer'),
535
- 0,
536
- args.color,
537
- senderID.toString(),
538
- args.content,
539
- new Date(),
540
- 0,
541
- 0,
542
- [],
543
- domain,
544
- )
570
+ return this.create({
571
+ ...args,
572
+ FCID: FCID.toString(),
573
+ // progress: args.progress,
574
+ mode: this.transMode(args.mode, 'dplayer'),
575
+ // fontsize: 25,
576
+ // color: args.color,
577
+ senderID: senderID.toString(),
578
+ // content: args.content,
579
+ platform: domain,
580
+ })
545
581
  }
546
582
  toDplayer(): DMDplayer {
547
583
  let mode = 0
@@ -569,22 +605,18 @@ export class UniDM {
569
605
  }
570
606
  else extra = { artplayer: { style: args.style } }
571
607
  }
572
- return new UniDM(
573
- FCID.toString(),
574
- args.progress,
575
- this.transMode(args.mode, 'artplayer'),
576
- 0,
577
- args.color,
578
- senderID.toString(),
579
- args.content,
580
- new Date(),
581
- 0,
582
- 0,
583
- [],
584
- domain,
585
- undefined,
586
- JSON.stringify(extra),
587
- )
608
+ return this.create({
609
+ ...args,
610
+ FCID: FCID.toString(),
611
+ // progress: args.progress,
612
+ mode: this.transMode(args.mode, 'artplayer'),
613
+ // fontsize: 25,
614
+ // color: args.color,
615
+ senderID: senderID.toString(),
616
+ // content: args.content,
617
+ platform: domain,
618
+ extra: JSON.stringify(extra, BigIntSerializer), //optional BigINt parser
619
+ })
588
620
  }
589
621
  toArtplayer(): DMArtplayer {
590
622
  let mode = 0
@@ -604,23 +636,18 @@ export class UniDM {
604
636
  domain = domainPreset.ddplay,
605
637
  ) {
606
638
  const FCID = ID.fromUnknown(episodeId, domain)
607
- return new UniDM(
608
- FCID.toString(),
609
- args.progress,
610
- this.transMode(args.mode, 'ddplay'),
611
- 0,
612
- args.color,
613
- args.uid,
614
- args.m,
615
- new Date(),
616
- 0,
617
- 0,
618
- [],
619
- domain,
620
- undefined,
621
- undefined,
622
- args.cid.toString(), //无需 new ID() 获取带suffix的ID
623
- )
639
+ return this.create({
640
+ ...args,
641
+ FCID: FCID.toString(),
642
+ // progress: args.progress,
643
+ mode: this.transMode(args.mode, 'ddplay'),
644
+ // fontsize: 25,
645
+ // color: args.color,
646
+ senderID: args.uid,
647
+ content: args.m,
648
+ platform: domain,
649
+ DMID: args.cid.toString(), //无需 new ID() 获取带suffix的ID
650
+ })
624
651
  }
625
652
  toDDplay(): DMDDplay {
626
653
  let mode = 1