Python: Add basic support for Configs

This commit is contained in:
Vicki Pfau 2017-08-05 07:57:47 -07:00
parent a496ddc072
commit f0686a3b78
1 changed files with 29 additions and 0 deletions

View File

@ -103,6 +103,7 @@ class Core(object):
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):
@ -148,6 +149,9 @@ class Core(object):
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 @@ class IRunner(object):
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")))