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
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
34class Core(object):
35 def __init__(self, native):
36 self._core = native
37
38 @cached_property
39 def tiles(self):
40 return tile.TileView(self)
41
42 @classmethod
43 def _init(cls, native):
44 core = ffi.gc(native, native.deinit)
45 success = bool(core.init(core))
46 if not success:
47 raise RuntimeError("Failed to initialize core")
48 if hasattr(cls, 'PLATFORM_GBA') and core.platform(core) == cls.PLATFORM_GBA:
49 return GBA(core)
50 if hasattr(cls, 'PLATFORM_GB') and core.platform(core) == cls.PLATFORM_GB:
51 return GB(core)
52 return Core(core)
53
54 def _deinit(self):
55 self._core.deinit(self._core)
56
57 def loadFile(self, path):
58 return bool(lib.mCoreLoadFile(self._core, path.encode('UTF-8')))
59
60 def isROM(self, vf):
61 return bool(self._core.isROM(vf.handle))
62
63 def loadROM(self, vf):
64 return bool(self._core.loadROM(self._core, vf.handle))
65
66 def loadSave(self, vf):
67 return bool(self._core.loadSave(self._core, vf.handle))
68
69 def loadTemporarySave(self, vf):
70 return bool(self._core.loadTemporarySave(self._core, vf.handle))
71
72 def loadPatch(self, vf):
73 return bool(self._core.loadPatch(self._core, vf.handle))
74
75 def autoloadSave(self):
76 return bool(lib.mCoreAutoloadSave(self._core))
77
78 def autoloadPatch(self):
79 return bool(lib.mCoreAutoloadPatch(self._core))
80
81 def platform(self):
82 return self._core.platform(self._core)
83
84 def desiredVideoDimensions(self):
85 width = ffi.new("unsigned*")
86 height = ffi.new("unsigned*")
87 self._core.desiredVideoDimensions(self._core, width, height)
88 return width[0], height[0]
89
90 def setVideoBuffer(self, image):
91 self._core.setVideoBuffer(self._core, image.buffer, image.stride)
92
93 def reset(self):
94 self._core.reset(self._core)
95
96 def runFrame(self):
97 self._core.runFrame(self._core)
98
99 def runLoop(self):
100 self._core.runLoop(self._core)
101
102 def step(self):
103 self._core.step(self._core)
104
105 @staticmethod
106 def _keysToInt(*args, **kwargs):
107 keys = 0
108 if 'raw' in kwargs:
109 keys = kwargs['raw']
110 for key in args:
111 keys |= 1 << key
112 return keys
113
114 def setKeys(self, *args, **kwargs):
115 self._core.setKeys(self._core, self._keysToInt(*args, **kwargs))
116
117 def addKeys(self, *args, **kwargs):
118 self._core.addKeys(self._core, self._keysToInt(*args, **kwargs))
119
120 def clearKeys(self, *args, **kwargs):
121 self._core.clearKeys(self._core, self._keysToInt(*args, **kwargs))
122
123 def frameCounter(self):
124 return self._core.frameCounter(self._core)
125
126 def frameCycles(self):
127 return self._core.frameCycles(self._core)
128
129 def frequency(self):
130 return self._core.frequency(self._core)
131
132 def getGameTitle(self):
133 title = ffi.new("char[16]")
134 self._core.getGameTitle(self._core, title)
135 return ffi.string(title, 16).decode("ascii")
136
137 def getGameCode(self):
138 code = ffi.new("char[12]")
139 self._core.getGameCode(self._core, code)
140 return ffi.string(code, 12).decode("ascii")
141
142if hasattr(lib, 'PLATFORM_GBA'):
143 from .gba import GBA
144 Core.PLATFORM_GBA = lib.PLATFORM_GBA
145
146if hasattr(lib, 'PLATFORM_GB'):
147 from .gb import GB
148 from .lr35902 import LR35902Core
149 Core.PLATFORM_GB = lib.PLATFORM_GB