Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
feat: fix task running
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para committed Aug 11, 2023
1 parent 3e1d5a8 commit 51fc0bb
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 66 deletions.
67 changes: 38 additions & 29 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ interface Config {
let config: Config
let loader: MaaFrameworkLoader
let controller: MaaController
let resource: MaaResource
let instance: MaaInstance
const instanceListener: ((msg: string, detail: unknown) => void)[] = []

sms.install()

Expand Down Expand Up @@ -79,6 +82,21 @@ async function main() {
}
})

app.post('/api/config/load', (req, res) => {
res.send({
success: true,
config
})
})

app.post('/api/config/save', (req, res) => {
config = req.body
fs.writeFile('config.json', JSON.stringify(config, null, 2))
res.send({
success: true
})
})

app.ws('/api/controller', async (ws, req) => {
ws.on('message', async data => {
const action = JSON.parse(data.toString('utf-8')) as {
Expand Down Expand Up @@ -116,34 +134,19 @@ async function main() {
return
}

const resource = await prepareResource()
if (!resource) {
const loaded = await prepareResource()
if (!loaded) {
ws.close()
return
}

const instance = new MaaInstance(loader, (msg, detail) => {
ws.send(
JSON.stringify({
type: 'callback',
msg,
detail
})
)
})

instance.bindController(controller)
instance.bindResource(resource)

if (!instance.inited()) {
ws.close()
resource.destroy()
return
}

ws.on('close', () => {
// instance.destroy()
resource.destroy()
instance.stop()
})

ws.send(
Expand Down Expand Up @@ -189,9 +192,21 @@ async function main() {
loader.setDebugMode(config.maaframework.debug)

const ctrl = await prepareController()
if (ctrl) {
controller = ctrl
if (!ctrl) {
return
}

controller = ctrl
resource = new MaaResource(loader)
instance = new MaaInstance(loader, (msg, detail) => {
detail = JSON.parse(detail)
for (const cb of instanceListener) {
cb(msg, detail)
}
})

instance.bindController(controller)
instance.bindResource(resource)
}
}

Expand Down Expand Up @@ -220,24 +235,18 @@ async function prepareResource() {
)
console.log('resource prepared at', tempResourceDir)

const res = new MaaResource(loader)
const loaded = await res.load(tempResourceDir)
const loaded = await resource.load(tempResourceDir)
console.log('resource load:', loaded)

if (!loaded) {
res.destroy()
return null
}

return res
return loaded
}

async function prepareController() {
const ctrl = new MaaController(
loader,
config.maaframework.adb,
config.maaframework.address,
MaaAdbControllerTypeEnum.Input_Preset_Minitouch |
MaaAdbControllerTypeEnum.Input_Preset_Adb |
MaaAdbControllerTypeEnum.Screencap_Encode,
await fs.readFile(
path.join(config.maaframework.root, 'controller_config.json'),
Expand Down
19 changes: 17 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
<script setup lang="ts">
import { onMounted } from 'vue'
import { useDebounceFn } from '@vueuse/core'
import { onMounted, watch } from 'vue'
import { RouterView } from 'vue-router'
import { config } from '@/data'
import { fs } from '@/filesystem'
import { loadFS } from '@/loader'
import { loadCfg, loadFS, saveCfg } from '@/loader'
onMounted(async () => {
await loadFS()
await loadCfg()
const doSave = useDebounceFn(() => {
saveCfg()
}, 500)
watch(
config,
v => {
doSave()
},
{
deep: true
}
)
window.onkeydown = ev => {
if (ev.ctrlKey && (ev.key === 'z' || ev.key === 'Z')) {
ev.stopPropagation()
Expand Down
19 changes: 19 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from 'axios'

import type { Config } from './types'

export async function load() {
return (
await axios.post('/api/load', null, {
Expand All @@ -18,6 +20,23 @@ export async function save(blob: Blob) {
})
}

export async function loadConfig() {
return (
(
await axios.post('/api/config/load', null, {
responseType: 'json'
})
).data as {
success: boolean
config: Config
}
).config
}

export async function saveConfig(cfg: Config) {
return await axios.post('/api/config/save', cfg)
}

export async function controller() {
return new Promise<WebSocket>(resolve => {
const ws = new WebSocket('ws://' + window.location.host + '/api/controller')
Expand Down
42 changes: 42 additions & 0 deletions src/components/ConfigEdit.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<script setup lang="ts">
import { useVModel } from '@vueuse/core'
import { NInput } from 'naive-ui'
import { wrapProp } from '@/misc'
import type { Config } from '@/types'
import ClearButton from '@/components/atomic/ClearButton.vue'
import FormLayout from '@/layout/FormLayout.vue'
const props = defineProps<{
value: Config
}>()
const emits = defineEmits<{
'update:value': [Config]
}>()
const config = useVModel(props, 'value', emits, {
passive: true,
deep: true
})
const fw = wrapProp(config, 'maaframework')
const adb = wrapProp(fw, 'adb')
const address = wrapProp(fw, 'address')
console.log(adb)
</script>

<template>
<div class="flex flex-col gap-2">
<span class="font-bold">下列配置需要重启才能生效</span>
<FormLayout>
<ClearButton propkey="adb" v-model:value="adb"> ADB路径 </ClearButton>
<NInput v-model:value="adb" placeholder="adb"></NInput>
<ClearButton propkey="address" v-model:value="address">
连接地址
</ClearButton>
<NInput v-model:value="address" placeholder="127.0.0.1:5555"></NInput>
</FormLayout>
</div>
</template>
5 changes: 5 additions & 0 deletions src/data/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ref } from 'vue'

import type { Config } from '@/types'

export const config = ref<Config | null>(null)
1 change: 1 addition & 0 deletions src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { computed } from 'vue'
import { history } from '@/data'
import type { PathKey } from '@/filesystem'

export * from './config'
export * from './filesystem'
export * from './history'
export * from './image'
Expand Down
11 changes: 11 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as api from '@/api'
import { config } from '@/data'
import { fs } from '@/filesystem'

export async function loadFS() {
Expand All @@ -10,3 +11,13 @@ export async function saveFS() {
const zip = await fs.saveZip()
await api.save(zip)
}

export async function loadCfg() {
config.value = await api.loadConfig()
}

export async function saveCfg() {
if (config.value) {
await api.saveConfig(config.value)
}
}
12 changes: 9 additions & 3 deletions src/misc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { type Ref, computed } from 'vue'
import { type Ref, type WritableComputedRef, computed } from 'vue'

export function wrapProp<T extends {}, K extends keyof T>(obj: Ref<T>, key: K) {
export function wrapProp<T extends {}, K extends keyof T>(
obj: Ref<T> | WritableComputedRef<T | null>,
key: K
) {
return computed<NonNullable<T[K]> | null>({
set(v) {
if (obj.value === null) {
return
}
if (v === null) {
if (key in obj.value) {
delete obj.value[key]
Expand All @@ -12,7 +18,7 @@ export function wrapProp<T extends {}, K extends keyof T>(obj: Ref<T>, key: K) {
}
},
get() {
return obj.value[key] ?? null
return obj.value?.[key] ?? null
}
})
}
Expand Down
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,19 @@ export const enum DispatcherStatus {
Failed,
Stopped
}

export interface Config {
port: number
web: string
date: string
saves: string
active: string
maaframework: {
emulator: boolean
adb: string
address: string
root: string
log: string
debug: boolean
}
}
Loading

0 comments on commit 51fc0bb

Please sign in to comment.