src/platform/python/mgba/core.py (view raw)
1# Copyright (c) 2013-2016 Jeffrey Pfau
2#
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6from ._pylib import ffi, lib
7from . import tile, createCallback
8from cached_property import cached_property
9
10def find(path):
11 core = lib.mCoreFind(path.encode('UTF-8'))
12 if core == ffi.NULL:
13 return None
14 return Core._init(core)
15
16def findVF(vf):
17 core = lib.mCoreFindVF(vf.handle)
18 if core == ffi.NULL:
19 return None
20 return Core._init(core)
21
22def loadPath(path):
23 core = find(path)
24 if not core or not core.loadFile(path):
25 return None
26 return core
27
28def loadVF(vf):
29 core = findVF(vf)
30 if not core or not core.loadROM(vf):
31 return None
32 return core
33
34def needsReset(f):
35 def wrapper(self, *args, **kwargs):
36 if not self._wasReset:
37 raise RuntimeError("Core must be reset first")
38 return f(self, *args, **kwargs)
39 return wrapper
40
41def protected(f):
42 def wrapper(self, *args, **kwargs):
43 if self._protected:
44 raise RuntimeError("Core is protected")
45 return f(self, *args, **kwargs)
46 return wrapper
47
48@ffi.def_extern()
49def _mCorePythonCallbacksVideoFrameStarted(user):
50 context = ffi.from_handle(user)
51 context._videoFrameStarted()
52
53@ffi.def_extern()
54def _mCorePythonCallbacksVideoFrameEnded(user):
55 context = ffi.from_handle(user)
56 context._videoFrameEnded()
57
58@ffi.def_extern()
59def _mCorePythonCallbacksCoreCrashed(user):
60 context = ffi.from_handle(user)
61 context._coreCrashed()
62
63@ffi.def_extern()
64def _mCorePythonCallbacksSleep(user):
65 context = ffi.from_handle(user)
66 context._sleep()
67
68class CoreCallbacks(object):
69 def __init__(self):
70 self._handle = ffi.new_handle(self)
71 self.videoFrameStarted = []
72 self.videoFrameEnded = []
73 self.coreCrashed = []
74 self.sleep = []
75 self.context = lib.mCorePythonCallbackCreate(self._handle)
76
77 def _videoFrameStarted(self):
78 for cb in self.videoFrameStarted:
79 cb()
80
81 def _videoFrameEnded(self):
82 for cb in self.videoFrameEnded:
83 cb()
84
85 def _coreCrashed(self):
86 for cb in self.coreCrashed:
87 cb()
88
89 def _sleep(self):
90 for cb in self.sleep:
91 cb()
92
93class Core(object):
94 if hasattr(lib, 'PLATFORM_GBA'):
95 PLATFORM_GBA = lib.PLATFORM_GBA
96
97 if hasattr(lib, 'PLATFORM_GB'):
98 PLATFORM_GB = lib.PLATFORM_GB
99
100 if hasattr(lib, 'PLATFORM_DS'):
101 PLATFORM_GB = lib.PLATFORM_DS
102
103 def __init__(self, native):
104 self._core = native
105 self._wasReset = False
106 self._protected = False
107 self._callbacks = CoreCallbacks()
108 self._core.addCoreCallbacks(self._core, self._callbacks.context)
109
110 @cached_property
111 def tiles(self):
112 return tile.TileView(self)
113
114 @classmethod
115 def _init(cls, native):
116 core = ffi.gc(native, native.deinit)
117 success = bool(core.init(core))
118 lib.mCoreInitConfig(core, ffi.NULL)
119 if not success:
120 raise RuntimeError("Failed to initialize core")
121 return cls._detect(core)
122
123 def _deinit(self):
124 self._core.deinit(self._core)
125
126 @classmethod
127 def _detect(cls, core):
128 if hasattr(cls, 'PLATFORM_GBA') and core.platform(core) == cls.PLATFORM_GBA:
129 from .gba import GBA
130 return GBA(core)
131 if hasattr(cls, 'PLATFORM_GB') and core.platform(core) == cls.PLATFORM_GB:
132 from .gb import GB
133 return GB(core)
134 if hasattr(cls, 'PLATFORM_DS') and core.platform(core) == cls.PLATFORM_DS:
135 from .ds import DS
136 return DS(core)
137 return Core(core)
138
139 def loadFile(self, path):
140 return bool(lib.mCoreLoadFile(self._core, path.encode('UTF-8')))
141
142 def isROM(self, vf):
143 return bool(self._core.isROM(vf.handle))
144
145 def loadROM(self, vf):
146 return bool(self._core.loadROM(self._core, vf.handle))
147
148 def loadSave(self, vf):
149 return bool(self._core.loadSave(self._core, vf.handle))
150
151 def loadTemporarySave(self, vf):
152 return bool(self._core.loadTemporarySave(self._core, vf.handle))
153
154 def loadPatch(self, vf):
155 return bool(self._core.loadPatch(self._core, vf.handle))
156
157 def autoloadSave(self):
158 return bool(lib.mCoreAutoloadSave(self._core))
159
160 def autoloadPatch(self):
161 return bool(lib.mCoreAutoloadPatch(self._core))
162
163 def platform(self):
164 return self._core.platform(self._core)
165
166 def desiredVideoDimensions(self):
167 width = ffi.new("unsigned*")
168 height = ffi.new("unsigned*")
169 self._core.desiredVideoDimensions(self._core, width, height)
170 return width[0], height[0]
171
172 def setVideoBuffer(self, image):
173 self._core.setVideoBuffer(self._core, image.buffer, image.stride)
174
175 def reset(self):
176 self._core.reset(self._core)
177 self._wasReset = True
178
179 @needsReset
180 @protected
181 def runFrame(self):
182 self._core.runFrame(self._core)
183
184 @needsReset
185 @protected
186 def runLoop(self):
187 self._core.runLoop(self._core)
188
189 @needsReset
190 def step(self):
191 self._core.step(self._core)
192
193 @staticmethod
194 def _keysToInt(*args, **kwargs):
195 keys = 0
196 if 'raw' in kwargs:
197 keys = kwargs['raw']
198 for key in args:
199 keys |= 1 << key
200 return keys
201
202 def setKeys(self, *args, **kwargs):
203 self._core.setKeys(self._core, self._keysToInt(*args, **kwargs))
204
205 def addKeys(self, *args, **kwargs):
206 self._core.addKeys(self._core, self._keysToInt(*args, **kwargs))
207
208 def clearKeys(self, *args, **kwargs):
209 self._core.clearKeys(self._core, self._keysToInt(*args, **kwargs))
210
211 @property
212 @needsReset
213 def frameCounter(self):
214 return self._core.frameCounter(self._core)
215
216 @property
217 def frameCycles(self):
218 return self._core.frameCycles(self._core)
219
220 @property
221 def frequency(self):
222 return self._core.frequency(self._core)
223
224 @property
225 def gameTitle(self):
226 title = ffi.new("char[16]")
227 self._core.getGameTitle(self._core, title)
228 return ffi.string(title, 16).decode("ascii")
229
230 @property
231 def gameCode(self):
232 code = ffi.new("char[12]")
233 self._core.getGameCode(self._core, code)
234 return ffi.string(code, 12).decode("ascii")
235
236 def addFrameCallback(self, cb):
237 self._callbacks.videoFrameEnded.append(cb)
238
239class ICoreOwner(object):
240 def claim(self):
241 raise NotImplementedError
242
243 def release(self):
244 raise NotImplementedError
245
246 def __enter__(self):
247 self.core = self.claim()
248 self.core._protected = True
249 return self.core
250
251 def __exit__(self, type, value, traceback):
252 self.core._protected = False
253 self.release()
254
255class IRunner(object):
256 def pause(self):
257 raise NotImplementedError
258
259 def unpause(self):
260 raise NotImplementedError
261
262 def useCore(self):
263 raise NotImplementedError
264
265 def isRunning(self):
266 raise NotImplementedError
267
268 def isPaused(self):
269 raise NotImplementedError