Python: Add basic support for Configs
Vicki Pfau vi@endrift.com
Sat, 05 Aug 2017 07:57:47 -0700
1 files changed,
29 insertions(+),
0 deletions(-)
jump to
M
src/platform/python/mgba/core.py
→
src/platform/python/mgba/core.py
@@ -103,6 +103,7 @@ self._wasReset = False
self._protected = False self._callbacks = CoreCallbacks() self._core.addCoreCallbacks(self._core, self._callbacks.context) + self.config = Config(ffi.addressof(native.config)) @cached_property def tiles(self):@@ -147,6 +148,9 @@ return bool(self._core.loadTemporarySave(self._core, vf.handle))
def loadPatch(self, vf): return bool(self._core.loadPatch(self._core, vf.handle)) + + def loadConfig(self, config): + lib.mCoreLoadForeignConfig(self._core, config._native) def autoloadSave(self): return bool(lib.mCoreAutoloadSave(self._core))@@ -261,3 +265,28 @@ raise NotImplementedError
def isPaused(self): raise NotImplementedError + +class Config(object): + def __init__(self, native=None, port=None, defaults={}): + if not native: + self._port = ffi.NULL + if port: + self._port = ffi.new("char[]", port.encode("UTF-8")) + native = ffi.gc(ffi.new("struct mCoreConfig*"), lib.mCoreConfigDeinit) + lib.mCoreConfigInit(native, self._port) + self._native = native + for key, value in defaults.items(): + if isinstance(value, bool): + value = int(value) + lib.mCoreConfigSetDefaultValue(self._native, ffi.new("char[]", key.encode("UTF-8")), ffi.new("char[]", str(value).encode("UTF-8"))) + + def __getitem__(self, key): + string = lib.mCoreConfigGetValue(self._native, ffi.new("char[]", key.encode("UTF-8"))) + if not string: + return None + return ffi.string(string) + + def __setitem__(self, key, value): + if isinstance(value, bool): + value = int(value) + lib.mCoreConfigSetValue(self._native, ffi.new("char[]", key.encode("UTF-8")), ffi.new("char[]", str(value).encode("UTF-8")))