aboutsummarylogtreecommitdiffstats
path: root/native-stockfish.patch
blob: 8457babc96695b71f03f6d0e51e57f027e11fb9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
diff --git a/src/stockfish/index.ts b/src/stockfish/index.ts
index 75cf3ec01..0adb23643 100644
--- a/src/stockfish/index.ts
+++ b/src/stockfish/index.ts
@@ -1,58 +1,69 @@
-import { Capacitor, registerPlugin } from '@capacitor/core'
-import { Stockfish, StockfishPlugin as IStockfishPlugin } from 'capacitor-stockfish'
 import { VariantKey } from '../lichess/interfaces/variant'
-import settings from '../settings'
 
-export const StockfishVariants = registerPlugin<IStockfishPlugin>('StockfishVariants', {
-  web: () => import('./StockfishVariantsWeb').then(m => new m.StockfishVariantsWeb()),
-})
+// stub for externally used functionality
+export const StockfishVariants = {
+  getMaxMemory: () => Promise.reject("unimplemented"),
+  getCPUArch: () => Promise.reject("unimplemented"),
+};
+
+interface INativeEngine {
+  send(line: string): void;
+  addListener(listener: (line: string) => void): void;
+  removeListener(listener: (line: string) => void): void;
+  kill(): boolean;
+}
+
+declare const startStockfish: () => INativeEngine;
 
 export class StockfishPlugin {
-  private plugin: IStockfishPlugin
+  private engine: INativeEngine | undefined
+  private startPromise: Promise<{ engineName: string }> | undefined
 
   constructor(readonly variant: VariantKey) {
-    this.plugin = !this.isVariant() &&
-      canUseNNUE() &&
-      settings.analyse.cevalUseNNUE() ? Stockfish : StockfishVariants
   }
 
   public async start(): Promise<{ engineName: string }> {
-    return new Promise((resolve) => {
-      let engineName = 'Stockfish'
-      const listener = (e: Event) => {
-        const line = (e as any).output
-        console.debug('[stockfish >>] ' + line)
-        if (line.startsWith('id name ')) {
-          engineName = line.substring('id name '.length)
-        }
-        if (line.startsWith('uciok')) {
-          window.removeEventListener('stockfish', listener, false)
-          resolve({ engineName })
-        }
-      }
-      window.addEventListener('stockfish', listener, { passive: true })
-      this.plugin.start()
-      .then(() => this.send('uci'))
-    })
+    console.log('trying to start stockfish')
+    if(typeof this.startPromise === 'undefined') {
+      this.startPromise = new Promise<{engineName: string}>((resolve) => {
+        let engineName = 'Stockfish'
+        this.engine = startStockfish();
+        
+        const listener = (line: string) => {
+          console.debug('[stockfish >>] ' + line)
+          if (line.startsWith('id name ')) {
+            engineName = line.substring('id name '.length)
+          }
+          if (line.startsWith('uciok')) {
+            this.engine!.removeListener(listener)
+            resolve({ engineName })
+          }
+         }
+        this.engine.addListener(listener)
+        this.send('uci')
+      })
+    }
+
+    return this.startPromise
   }
 
   public isReady(): Promise<void> {
     return new Promise((resolve) => {
-      const listener = (e: Event) => {
-        const line = (e as any).output
+      const listener = (line: string) => {
         if (line.startsWith('readyok')) {
-          window.removeEventListener('stockfish', listener, false)
+          this.engine!.removeListener(listener)
           resolve()
         }
       }
-      window.addEventListener('stockfish', listener, { passive: true })
+      this.engine!.addListener(listener)
       this.send('isready')
     })
   }
 
   public send(text: string): Promise<void> {
     console.debug('[stockfish <<] ' + text)
-    return this.plugin.cmd({ cmd: text })
+    this.engine!.send(text)
+    return Promise.resolve()
   }
 
   public setOption(name: string, value: string | number | boolean): Promise<void> {
@@ -61,10 +72,8 @@ export class StockfishPlugin {
 
   public setVariant(): Promise<void> {
     if (this.isVariant()) {
-      if (Capacitor.getPlatform() !== 'web' && this.variant === 'threeCheck')
+      if (this.variant === 'threeCheck')
         return this.setOption('UCI_Variant', '3check')
-      if (Capacitor.getPlatform() === 'web' && this.variant === 'antichess')
-        return this.setOption('UCI_Variant', 'giveaway')
       else
         return this.setOption('UCI_Variant', this.variant.toLowerCase())
     } else {
@@ -73,7 +82,11 @@ export class StockfishPlugin {
   }
 
   public exit(): Promise<void> {
-    return this.plugin.exit()
+    if(this.engine!.kill()) {
+      return Promise.resolve()
+    } else {
+      return Promise.reject('Failed to kill process')
+    }
   }
 
   private isVariant() {
@@ -95,11 +108,13 @@ export function getNbCores(): number {
 }
 
 export function canUseNNUE(): boolean {
-  if (Capacitor.getPlatform() === 'android') {
-    return window.lichess.cpuArch === 'arm64-v8a'
-  } else if (Capacitor.getPlatform() === 'ios') {
-    return true
-  } else {
-    return false
-  }
+  // TODO maybe later
+  return false
 }
+
+window.addEventListener("stockfish-outer" as any, (evt: CustomEvent) => {
+  const newEvent = new CustomEvent("stockfish");
+  (newEvent as any).output = evt.detail;
+
+  window.dispatchEvent(newEvent);
+});