refactor: 将 src/common src/core/common 移入 @motajs/legacy-common

This commit is contained in:
AncTe committed 2025-03-04 23:22:14 +08:00
1 parent a5e6f5b5f6
commit dc502ddd64
25 files changed
+256 -201

No files matched your search

-128
View File
@@ -1,128 +0,0 @@
import { logger } from '@motajs/common';
export const enum PatchClass {
Actions,
Control,
Core,
Data,
Enemys,
Events,
Icons,
Items,
Loader,
Maps,
UI,
Utils
}
interface PatchList {
[PatchClass.Actions]: Actions;
[PatchClass.Control]: Control;
[PatchClass.Core]: Core;
[PatchClass.Data]: Omit<DataCore, 'main'>;
[PatchClass.Enemys]: Enemys;
[PatchClass.Events]: Events;
[PatchClass.Icons]: Icons;
[PatchClass.Items]: Items;
[PatchClass.Loader]: Loader;
[PatchClass.Maps]: Maps;
[PatchClass.UI]: Ui;
[PatchClass.Utils]: Utils;
}
const patchName = {
[PatchClass.Actions]: 'actions',
[PatchClass.Control]: 'control',
[PatchClass.Core]: 'core',
[PatchClass.Data]: 'data',
[PatchClass.Enemys]: 'enemys',
[PatchClass.Events]: 'events',
[PatchClass.Icons]: 'icons',
[PatchClass.Items]: 'items',
[PatchClass.Loader]: 'loader',
[PatchClass.Maps]: 'maps',
[PatchClass.UI]: 'ui',
[PatchClass.Utils]: 'utils'
};
export class Patch<T extends PatchClass> {
private static patchList: Set<Patch<PatchClass>> = new Set();
private static patched: Partial<Record<PatchClass, Set<string>>> = {};
private patches: Map<string, (...params: any[]) => any> = new Map();
constructor(public readonly patchClass: T) {
Patch.patchList.add(this);
}
/**
* 添加函数修改
* @param key 要修改的函数名
* @param patch 修改为的函数内容
*/
add<
K extends Exclude<
SelectKey<PatchList[T], (...params: any[]) => any>,
symbol | number
>
>(key: K, patch: PatchList[T][K]) {
if (this.patches.has(key)) {
logger.warn(49, patchName[this.patchClass], key);
}
this.patches.set(key, patch);
}
private static getPatchClass(patch: PatchClass): any {
switch (patch) {
case PatchClass.Actions:
return actions.prototype;
case PatchClass.Control:
return control.prototype;
case PatchClass.Core:
return core;
case PatchClass.Data:
return data.prototype;
case PatchClass.Enemys:
return enemys.prototype;
case PatchClass.Events:
return events.prototype;
case PatchClass.Icons:
return icons.prototype;
case PatchClass.Items:
return items.prototype;
case PatchClass.Loader:
return loader.prototype;
case PatchClass.Maps:
return maps.prototype;
case PatchClass.UI:
return ui.prototype;
case PatchClass.Utils:
return utils.prototype;
}
}
/**
* 修改添加的所有函数
*/
static patchAll() {
this.patchList.forEach(v => this.patch(v));
}
/**
* 修改某个实例添加的所有函数
* @param patch 要修改的函数实例
*/
static patch(patch: Patch<PatchClass>) {
const patchClass = patch.patchClass;
this.patched[patchClass] ??= new Set();
const set = this.patched[patchClass];
const obj = this.getPatchClass(patchClass);
for (const [key, func] of patch.patches) {
if (set.has(key)) {
logger.warn(49, patchName[patchClass], key);
}
obj[key] = func;
}
this.patchList.delete(patch);
}
}
-75
View File
@@ -1,75 +0,0 @@
import { EventEmitter } from 'eventemitter3';
interface MonoStoreEvent<T> {
change: [before: T | undefined, after: T | undefined];
}
/**
* 一个可序列化存储结构,存储多个数据,但是同时只能有一个数据正在使用,适合那些需要切换的场景,
* 例如多勇士,多摄像机等,就可以通过此结构进行存储,然后同时只能操作一个勇士、摄像机等。
*/
export class MonoStore<T> extends EventEmitter<MonoStoreEvent<T>> {
list: Map<string, T> = new Map();
using?: T;
usingId?: string;
/**
* 使用指定id的数据
* @param id 要使用的数据id
*/
use(id: string) {
const before = this.using;
this.using = this.list.get(id);
this.usingId = id;
this.emit('change', before, this.using);
}
static toJSON(data: MonoStore<any>) {
return JSON.stringify({
now: data.usingId,
data: [...data.list]
});
}
static fromJSON<T>(data: string): MonoStore<T> {
const d = JSON.parse(data);
const arr: [string, T][] = d.data;
const store = new MonoStore<T>();
arr.forEach(([key, value]) => {
store.list.set(key, value);
});
if (d.now) store.use(d.now);
return store;
}
}
export namespace SerializeUtils {
interface StructSerializer<T> {
toJSON(data: T): string;
fromJSON(data: string): T;
}
/**
* Map键值对序列化函数,如果使用Map作为state,那么需要使用这里面的函数作为序列化与反序列化函数
*/
export const mapSerializer: StructSerializer<Map<any, any>> = {
toJSON(data) {
return JSON.stringify([...data]);
},
fromJSON(data) {
return new Map(JSON.parse(data));
}
};
/**
* Set集合序列化函数,如果使用Set作为state,那么需要使用这里面的函数作为序列化与反序列化函数
*/
export const setSerializer: StructSerializer<Set<any>> = {
toJSON(data) {
return JSON.stringify([...data]);
},
fromJSON(data) {
return new Set(JSON.parse(data));
}
};
}
-57
View File
@@ -1,57 +0,0 @@
import { EventEmitter } from './eventEmitter';
interface DisposableEvent<T> {
active: (value: T) => void;
dispose: (value: T) => void;
destroy: () => void;
}
export class Disposable<T> extends EventEmitter<DisposableEvent<T>> {
protected _data?: T;
set data(value: T | null) {
if (this.destroyed) {
throw new Error(
`Cannot set value of destroyed disposable variable.`
);
}
if (value !== null) this._data = value;
}
get data(): T | null {
if (this.destroyed) {
throw new Error(
`Cannot get value of destroyed disposable variable.`
);
}
if (!this.activated) {
return null;
}
return this._data!;
}
protected activated: boolean = false;
protected destroyed: boolean = false;
constructor(data: T) {
super();
this._data = data;
}
active() {
if (this.activated) return;
this.activated = true;
this.emit('active', this._data!);
}
dispose() {
if (!this.activated) return;
this.activated = false;
this.emit('dispose', this._data!);
}
destroy() {
if (this.destroyed) return;
this.destroyed = true;
this.emit('destroy');
delete this._data;
}
}
+2 -2
View File
@@ -101,7 +101,7 @@ export class EventEmitter<T extends Record<keyof T, Callable> = {}> {
emit(event: string, ...params: any[]): any[] {
this.emitted.add(event);
const events = (this.events[event] ??= []);
if (!!this.emitter[event]) {
if (this.emitter[event]) {
const returns = this.emitter[event]!(events, ...params);
this.events[event] = events.filter(v => !v.once);
return returns;
@@ -121,7 +121,7 @@ export class EventEmitter<T extends Record<keyof T, Callable> = {}> {
* @param event 要设置的事件
* @param fn 事件执行器,留空以清除触发器
*/
// @ts-ignore
// @ts-expect-error 无法推导
setEmitter<K extends keyof T>(event: K, fn?: EmitFn<T[K]>): void;
setEmitter(event: string, fn?: EmitFn<Callable>): void;
setEmitter(event: string, fn?: EmitFn<Callable>): void {
+1 -2
View File
@@ -1,8 +1,7 @@
import axios, { AxiosRequestConfig, ResponseType } from 'axios';
import { Disposable } from './disposable';
import { Disposable, EventEmitter } from '@motajs/legacy-common';
import { logger } from '@motajs/common';
import JSZip from 'jszip';
import { EventEmitter } from './eventEmitter';
type ProgressFn = (now: number, total: number) => void;
-2
View File
@@ -55,7 +55,6 @@ import EnemySpecial from '@/panel/enemySpecial.vue';
import EnemyTarget from '@/panel/enemyTarget.vue';
import KeyboardPanel from '@/panel/keyboard.vue';
import { MCGenerator } from './main/layout';
import { ResourceController } from './loader/controller';
import { logger } from '@motajs/common';
import { Danmaku } from './main/custom/danmaku';
import * as Shadow from './fx/shadow';
@@ -75,7 +74,6 @@ Mota.register('class', 'MotaSetting', MotaSetting);
Mota.register('class', 'SettingDisplayer', SettingDisplayer);
Mota.register('class', 'UiController', UiController);
Mota.register('class', 'MComponent', MComponent);
Mota.register('class', 'ResourceController', ResourceController);
Mota.register('class', 'Danmaku', Danmaku);
// ----- 函数注册
Mota.register('fn', 'm', m);
-30
View File
@@ -1,30 +0,0 @@
import { EventEmitter } from '../common/eventEmitter';
interface ResourceControllerEvent<D = any, T = D> {
add: (uri: string, data: D) => void;
delete: (uri: string, content: T) => void;
}
export abstract class ResourceController<
D,
T = D
> extends EventEmitter<ResourceControllerEvent> {
list: Record<string, T> = {};
/**
* 添加一个资源
* @param uri 资源uri
* @param data 资源数据
*/
abstract add(uri: string, data: D): void;
/**
* 删除一个资源
* @param uri 要删除的资源的uri
*/
remove(uri: string) {
const content = this.list[uri];
delete this.list[uri];
// this.emit(uri, content);
}
}
+4 -4
View File
@@ -1,5 +1,5 @@
import BoxAnimate from '@/components/boxAnimate.vue';
import { EventEmitter } from '@/core/common/eventEmitter';
import { EventEmitter } from 'eventemitter3';
import { logger } from '@motajs/common';
import { ResponseBase } from '@/core/interface';
import {
@@ -53,9 +53,9 @@ interface PostLikeResponse extends ResponseBase {
}
interface DanmakuEvent {
showStart: (danmaku: Danmaku) => void;
showEnd: (danmaku: Danmaku) => void;
like: (liked: boolean, danmaku: Danmaku) => void;
showStart: [danmaku: Danmaku];
showEnd: [danmaku: Danmaku];
like: [liked: boolean, danmaku: Danmaku];
}
type SpecContentFn = (content: string, type: string) => VNode;
+1 -1
View File
@@ -1,4 +1,4 @@
import { EventEmitter, Listener } from '@/core/common/eventEmitter';
import { EventEmitter, Listener } from '@motajs/legacy-common';
import { KeyCode } from '@/plugin/keyCodes';
import { gameKey } from './hotkey';
import { unwarpBinary } from './hotkey';
+1 -2
View File
@@ -1,4 +1,4 @@
import { EventEmitter } from '@/core/common/eventEmitter';
import { EventEmitter } from '@motajs/legacy-common';
import { deleteWith, has } from '@/plugin/utils';
import { Component, nextTick, reactive, shallowReactive } from 'vue';
import { fixedUi } from '../init/ui';
@@ -11,7 +11,6 @@ import type {
ToolbarItemMap,
ToolbarItemType
} from '../init/toolbar';
import { isMobile } from '@/plugin/use';
interface CustomToolbarEvent {
add: (item: ValueOf<ToolbarItemMap>) => void;
+1 -3
View File
@@ -1,8 +1,6 @@
import { Component, shallowReactive } from 'vue';
import { Callable, EventEmitter } from '../../common/eventEmitter';
import { KeyCode } from '@/plugin/keyCodes';
import { EventEmitter } from '@motajs/legacy-common';
import { Hotkey } from './hotkey';
import { generateBinary } from '@/plugin/utils';
interface FocusEvent<T> {
focus: (before: T | null, after: T) => void;
+1 -1
View File
@@ -1,5 +1,5 @@
import { FunctionalComponent, reactive } from 'vue';
import { EventEmitter } from '../common/eventEmitter';
import { EventEmitter } from '@motajs/legacy-common';
import { GameStorage } from './storage';
import { has, triggerFullscreen } from '@/plugin/utils';
import { createSettingComponents } from './init/settings';
+1 -4
View File
@@ -1,13 +1,12 @@
import './system';
import '../plugin/game/index';
import * as damage from './enemy/damage';
import { EventEmitter, IndexedEventEmitter } from '@/core/common/eventEmitter';
import { EventEmitter, IndexedEventEmitter } from '@motajs/legacy-common';
import { specials } from './enemy/special';
import { gameListener, hook, loading } from './game';
import * as battle from './enemy/battle';
import * as hero from './state/hero';
import * as miscMechanism from './mechanism/misc';
import { registerPresetState } from './state/preset';
import { ItemState } from './state/item';
import {
BlockMover,
@@ -52,8 +51,6 @@ loading.once('coreInit', () => {
Mota.Plugin.init();
});
registerPresetState();
window.addEventListener('beforeunload', () => {
core.checkAutosave();
});
-64
View File
@@ -1,64 +0,0 @@
import { MonoStore } from '@/common/struct';
import { GameState } from './state';
import { Hero, HeroState } from './hero';
export function registerPresetState() {
GameState.register<MonoStore<Hero<any>>>('hero', heroToJSON, heroFromJSON);
}
interface HeroSave {
x: number;
y: number;
floorId: FloorIds;
id: string;
items: [AllIdsOf<'items'>, number][];
state: {
status: any;
buffable: (string | number | symbol)[];
buffMap: [string | number | symbol, number][];
};
}
interface HeroSerializable {
now: string | null;
saves: HeroSave[];
}
function heroToJSON(data: MonoStore<Hero<any>>): string {
const now = data.usingId ?? null;
const saves: HeroSave[] = [...data.list.values()].map(v => {
return {
x: v.x,
y: v.y,
floorId: v.floorId,
id: v.id,
items: [...v.items],
state: {
status: v.state.status,
buffable: [...v.state.buffable],
buffMap: [...v.state.buffMap]
}
};
});
const obj: HeroSerializable = {
now,
saves
};
return JSON.stringify(obj);
}
function heroFromJSON(data: string): MonoStore<Hero<any>> {
const obj: HeroSerializable = JSON.parse(data);
const store = new MonoStore<Hero<any>>();
const saves: [string, Hero<any>][] = obj.saves.map(v => {
const state = new HeroState(v.state.status);
v.state.buffable.forEach(v => state.buffable.add(v));
v.state.buffMap.forEach(v => state.buffMap.set(v[0], v[1]));
const hero = new Hero(v.id, v.x, v.y, v.floorId, state);
v.items.forEach(v => hero.items.set(v[0], v[1]));
return [hero.id, hero];
});
store.list = new Map(saves);
if (obj.now) store.use(obj.now);
return store;
}
+2 -2
View File
@@ -1,8 +1,8 @@
import type { Disposable } from '@/core/common/disposable';
import type {
Disposable,
EventEmitter,
IndexedEventEmitter
} from '@/core/common/eventEmitter';
} from '@motajs/legacy-common';
import type { loading } from './game';
import type { Hotkey } from '@/core/main/custom/hotkey';
import type { Keyboard } from '@/core/main/custom/keyboard';
+1 -1
View File
@@ -1,4 +1,4 @@
import { Patch, PatchClass } from '@/common/patch';
import { Patch, PatchClass } from '@motajs/legacy-common';
import { audioPlayer, bgmController, soundPlayer } from '../audio';
import { mainSetting } from '@/core/main/setting';
import { sleep } from 'mutate-animate';
+1 -1
View File
@@ -1,4 +1,4 @@
import { Patch } from '@/common/patch';
import { Patch } from '@motajs/legacy-common';
import { patchAudio } from './audio';
import { patchWeather } from './weather';
import { patchUI } from './ui';
+1 -1
View File
@@ -1,4 +1,4 @@
import { Patch, PatchClass } from '@/common/patch';
import { Patch, PatchClass } from '@motajs/legacy-common';
import { TipStore } from '../render/components/tip';
export function patchUI() {
+1 -1
View File
@@ -1,4 +1,4 @@
import { Patch, PatchClass } from '@/common/patch';
import { Patch, PatchClass } from '@motajs/legacy-common';
import { WeatherController } from '../weather';
import { isNil } from 'lodash-es';
+4 -1
View File
@@ -5,6 +5,9 @@
"@motajs/common": "workspace:*",
"@motajs/render": "workspace:*",
"@motajs/system": "workspace:*",
"@motajs/system-ui": "workspace:*"
"@motajs/system-ui": "workspace:*",
"@motajs/legacy-common": "workspace:*",
"@motajs/legacy-client": "workspace:*",
"@motajs/legacy-data": "workspace:*"
}
}