113 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-12-01 22:26:41 +08:00
import fs from 'fs-extra'
import path from 'path'
2022-12-04 22:10:30 +08:00
import { ApiMsgEnum, InputTypeEnum, strdecode } from '../Common'
2022-12-01 22:26:41 +08:00
export const getTime = () => new Date().toLocaleString().split("├")[0]
2022-12-03 20:06:57 +08:00
//symlink同步
export const symlinkCommon = async () => {
const src = path.resolve(__dirname, '../Common')
const dst = path.resolve(__dirname, '../../../client/assets/Scripts/Common')
2022-12-01 22:26:41 +08:00
2022-12-03 20:06:57 +08:00
if (await fs.lstat(dst).then(v => v.isSymbolicLink()).catch(() => false) && await fs.readlink(dst) === src) {
console.log('同步成功!')
} else {
fs.symlink(src, dst).then(() => {
console.log('同步成功!')
}).catch((e) => {
console.log('同步失败!', e)
})
}
}
//copy同步
export const copyCommon = async () => {
const src = path.resolve(__dirname, '../Common')
const dst = path.resolve(__dirname, '../../../client/assets/Scripts/Common')
2022-12-01 22:26:41 +08:00
console.log(src, dst);
// clean
await fs.remove(dst)
//create
await fs.ensureDir(dst)
// copy
await fs.copy(src, dst)
2022-12-03 20:06:57 +08:00
console.log('同步成功!')
2022-12-04 22:10:30 +08:00
}
export const binaryDecode = (buffer: Buffer) => {
let index = 0
const type = buffer.readUint8(index++)
if (type === ApiMsgEnum.MsgClientSync) {
const inputType = buffer.readUint8(index++)
if (inputType === InputTypeEnum.ActorMove) {
const id = buffer.readUint8(index++)
const directionX = buffer.readFloatBE(index)
index += 4
const directionY = buffer.readFloatBE(index)
index += 4
const dt = buffer.readFloatBE(index)
index += 4
const msg = {
name: ApiMsgEnum.MsgClientSync,
data: {
type: InputTypeEnum.ActorMove,
id,
direction: {
x: directionX,
y: directionY,
},
dt
}
}
return msg
} else if (inputType === InputTypeEnum.WeaponShoot) {
const id = buffer.readUint8(index++)
const positionX = buffer.readFloatBE(index)
index += 4
const positionY = buffer.readFloatBE(index)
index += 4
const directionX = buffer.readFloatBE(index)
index += 4
const directionY = buffer.readFloatBE(index)
index += 4
const msg = {
name: ApiMsgEnum.MsgClientSync,
data: {
type: InputTypeEnum.WeaponShoot,
id,
position: {
x: positionX,
y: positionY,
},
direction: {
x: directionX,
y: directionY,
},
}
}
return msg
} else {
const dt = buffer.readFloatBE(index)
index += 4
const msg = {
name: ApiMsgEnum.MsgClientSync,
data: {
type: InputTypeEnum.TimePast,
dt,
}
}
return msg
}
} else {
return {
name: type,
data: JSON.parse(strdecode(new Uint8Array(buffer.slice(1))))
}
}
2022-12-03 20:06:57 +08:00
}