class_name AkinorevSDK extends RefCounted ## Thin GDScript wrapper over the browser SDK injected by the platform. ## Works in the Godot 4 web export; other platforms get warnings instead of calls. const TAG := "[AKINOREV SDK]" const DEVICE_MOBILE := "mobile" const DEVICE_DESKTOP := "desktop" static func is_available() -> bool: if not OS.has_feature("web"): return false return bool(_eval("typeof window.__MT === 'object'")) static func save_set(key: String, value: String) -> bool: if key.is_empty(): push_error("%s save_set: key is required" % TAG) return false var storage = _storage() if storage == null: return false storage.setItem(key, value) return true static func save_get(key: String, fallback := "") -> String: if key.is_empty(): push_error("%s save_get: key is required" % TAG) return fallback var storage = _storage() if storage == null: return fallback var value = storage.getItem(key) if value == null: return fallback return str(value) static func save_remove(key: String) -> void: if key.is_empty(): push_error("%s save_remove: key is required" % TAG) return var storage = _storage() if storage == null: return storage.removeItem(key) static func save_clear() -> void: var storage = _storage() if storage == null: return storage.clear() static func device() -> String: var value = _eval("window.__MT && window.__MT.device ? window.__MT.device.current() : ''") if value == null: return "" return str(value) static func is_mobile() -> bool: return device() == DEVICE_MOBILE static func locale() -> String: var value = _eval("window.__MT_LOCALE__ || ''") if value == null: return "" return str(value) static func set_room(room: String) -> void: if room.strip_edges().is_empty(): push_error("%s set_room: room code is required" % TAG) return var mt = _mt() if mt == null: return mt.setRoom(room) static func clear_room() -> void: var mt = _mt() if mt == null: return mt.clearRoom() static func get_account(on_result: Callable) -> void: if not OS.has_feature("web"): push_warning("%s get_account works only in the web export" % TAG) on_result.call(null) return var mt = _mt() if mt == null: on_result.call(null) return var callback := JavaScriptBridge.create_callback(func(_value): on_result.call(_read_json("window.__MT_ACCOUNT__ || null"))) mt.getAccount().then(callback) static func get_session(on_result: Callable) -> void: if not OS.has_feature("web"): push_warning("%s get_session works only in the web export" % TAG) on_result.call(null) return var mt = _mt() if mt == null: on_result.call(null) return var callback := JavaScriptBridge.create_callback(func(_value): on_result.call(_read_json("window.__MT_SESSION__ || null"))) mt.getSession().then(callback) static func _storage(): if not OS.has_feature("web"): push_warning("%s saves work only in the web export" % TAG) return null var storage = JavaScriptBridge.get_interface("localStorage") if storage == null: push_error("%s localStorage is not available" % TAG) return storage static func _mt(): if not OS.has_feature("web"): push_warning("%s SDK calls work only in the web export" % TAG) return null var mt = JavaScriptBridge.get_interface("__MT") if mt == null: push_error("%s window.__MT is not available" % TAG) return mt static func _eval(code: String) -> Variant: if not OS.has_feature("web"): return null return JavaScriptBridge.eval(code, true) static func _read_json(expression: String) -> Variant: var raw = _eval("JSON.stringify(%s)" % expression) if raw == null or str(raw) == "null": return null var parsed = JSON.parse_string(str(raw)) if parsed == null: push_error("%s failed to parse JSON from %s" % [TAG, expression]) return parsed