Skip to content

Manic EMU is an all-in-one retro game emulator for iOS. It packs powerful features while keeping a clean, sleek UI and delivering buttery-smooth gameplay.

Swift

add J2ME files

Daiuno6271f782c7cc
diff --git a/.gitignore b/.gitignore
index cc229140..d6fb9097 100644
--- a/.gitignore
+++ b/.gitignore
@@ -95,4 +95,6 @@ Cores/libretro-desmume
Cores/libretro-melondsds
Cores/libretro-ppsspp
Cores/libretro-azahar
+Cores/J2meJS
+Cores/freej2me-web
i18n/
diff --git a/ManicEmu/ManicEmu.xcodeproj/project.pbxproj b/ManicEmu/ManicEmu.xcodeproj/project.pbxproj
index 08b4c769..a216ad74 100644
--- a/ManicEmu/ManicEmu.xcodeproj/project.pbxproj
+++ b/ManicEmu/ManicEmu.xcodeproj/project.pbxproj
@@ -752,6 +752,8 @@
57D7CFC22EAA1B5E00F41540 /* ShadowLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 57D7CFC12EAA1B5E00F41540 /* ShadowLabel.m */; };
57DA89132D82B0C20074052A /* SmartCodable in Frameworks */ = {isa = PBXBuildFile; productRef = 57DA89122D82B0C20074052A /* SmartCodable */; };
57DA89322D8328510074052A /* CKAccountStatusExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57DA89312D8328420074052A /* CKAccountStatusExtensions.swift */; };
+ 57DC7FF52F52E931004B6FFD /* J2MEView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57DC7FF32F52E92B004B6FFD /* J2MEView.swift */; };
+ 57DC7FFC2F52EC39004B6FFD /* WeakScriptMessageHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57DC7FFB2F52EC38004B6FFD /* WeakScriptMessageHandler.swift */; };
57DCE7A12E0C434C0079E3AA /* mednafen.saturn.libretro.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 57DCE79D2E0C43310079E3AA /* mednafen.saturn.libretro.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
57DCE7D72E0D2EFC0079E3AA /* CitraAdvancedSettingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57DCE7D62E0D2EEB0079E3AA /* CitraAdvancedSettingViewController.swift */; };
57DCE7DB2E0D38650079E3AA /* INIFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57DCE7DA2E0D385E0079E3AA /* INIFile.swift */; };
@@ -1864,6 +1866,8 @@
57D7CFC02EAA1B5E00F41540 /* ShadowLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShadowLabel.h; sourceTree = "<group>"; };
57D7CFC12EAA1B5E00F41540 /* ShadowLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ShadowLabel.m; sourceTree = "<group>"; };
57DA89312D8328420074052A /* CKAccountStatusExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CKAccountStatusExtensions.swift; sourceTree = "<group>"; };
+ 57DC7FF32F52E92B004B6FFD /* J2MEView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = J2MEView.swift; sourceTree = "<group>"; };
+ 57DC7FFB2F52EC38004B6FFD /* WeakScriptMessageHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakScriptMessageHandler.swift; sourceTree = "<group>"; };
57DCE79D2E0C43310079E3AA /* mednafen.saturn.libretro.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = mednafen.saturn.libretro.framework; path = ../Cores/mednafen.saturn.libretro.framework; sourceTree = SOURCE_ROOT; };
57DCE7D62E0D2EEB0079E3AA /* CitraAdvancedSettingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CitraAdvancedSettingViewController.swift; sourceTree = "<group>"; };
57DCE7DA2E0D385E0079E3AA /* INIFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = INIFile.swift; sourceTree = "<group>"; };
@@ -4374,6 +4378,7 @@
children = (
57AEB9DA2F2614BF005CB496 /* NavigationBlurView.swift */,
573CEE0C2F20D05100EA24F1 /* RomPatcherView.swift */,
+ 57DC7FF32F52E92B004B6FFD /* J2MEView.swift */,
5781F5AB2EF45C6900916D8B /* JGenesisView.swift */,
57EED0752ECB8172003971BD /* IconView.swift */,
A392D5B22E9796B800F920E7 /* ServiceIconView.swift */,
@@ -4512,6 +4517,7 @@
A5A69BA9256E56FF00D51C09 /* Others */ = {
isa = PBXGroup;
children = (
+ 57DC7FFB2F52EC38004B6FFD /* WeakScriptMessageHandler.swift */,
57A912022F3B765B00628B04 /* NDSSaveConverter.swift */,
578F28C02F39A9CA00D2D998 /* BonjourKit.swift */,
573CEE112F20D46B00EA24F1 /* LocalWebServer.swift */,
@@ -5508,6 +5514,7 @@
57DCE8092E0D9F300079E3AA /* SwitchCell.swift in Sources */,
574BCC742F324C92009BD74E /* Cipher.swift in Sources */,
57DCE80A2E0D9F300079E3AA /* Section.swift in Sources */,
+ 57DC7FFC2F52EC39004B6FFD /* WeakScriptMessageHandler.swift in Sources */,
57DCE80B2E0D9F300079E3AA /* Subtitle.swift in Sources */,
57DCE80C2E0D9F300079E3AA /* Row.swift in Sources */,
5754D8962E2752C6001EA5ED /* ExtraKey.swift in Sources */,
@@ -5540,6 +5547,7 @@
57478A712D52037F006F0D99 /* RandomGameCollectionReusableView.swift in Sources */,
573CEE6D2F224FB300EA24F1 /* ShaderConfig.swift in Sources */,
5720E8112E559EE600EC0DA5 /* RetroAchievementCountView.swift in Sources */,
+ 57DC7FF52F52E931004B6FFD /* J2MEView.swift in Sources */,
578F28D52F39E0A200D2D998 /* PSPNetworkingConfigLocalView.swift in Sources */,
577005B92D3E71BC00762E8F /* ImportService.swift in Sources */,
570622EC2D7BD2FE004F09E7 /* ShaderManager.swift in Sources */,
diff --git a/ManicEmu/ManicEmu/Resources/Config-Base.xcconfig b/ManicEmu/ManicEmu/Resources/Config-Base.xcconfig
index 95c3e0a4..394bc456 100644
--- a/ManicEmu/ManicEmu/Resources/Config-Base.xcconfig
+++ b/ManicEmu/ManicEmu/Resources/Config-Base.xcconfig
@@ -11,5 +11,5 @@
// https://help.apple.com/xcode/#/dev745c5c974
APP_BUNDLE_IDENTIFIER = com.aoshuang.manicemu
-APP_BUILD_VERSION = 2
-APP_VERSION = 1.8.3
+APP_BUILD_VERSION = 0
+APP_VERSION = 1.9.0
diff --git a/ManicEmu/ManicEmu/Sources/Business/Common/Views/J2MEView.swift b/ManicEmu/ManicEmu/Sources/Business/Common/Views/J2MEView.swift
new file mode 100644
index 00000000..8b4f2231
--- /dev/null
+++ b/ManicEmu/ManicEmu/Sources/Business/Common/Views/J2MEView.swift
@@ -0,0 +1,212 @@
+//
+// J2MEView.swift
+// ManicEmu
+//
+// Created by Daiuno on 2026/2/28.
+// Copyright © 2026 Manic EMU. All rights reserved.
+//
+import WebKit
+
+/// Genesis/Sega CD/32X 6键手柄按钮映射
+/// 这些值对应 KeyboardEvent.code 字符串,用于与 JGenesis 核心通信
+enum J2MEButton: String, CaseIterable {
+ // 方向键
+ case up
+ case down
+ case left
+ case right
+ case fire
+
+ // 数字键
+ case num0
+ case num1
+ case num2
+ case num3
+ case num4
+ case num5
+ case num6
+ case num7
+ case num8
+ case num9
+ case star
+ case pound
+
+ // 功能键
+ case softkeyLeft
+ case softkeyRight
+}
+
+class J2MEView: BaseView {
+
+ /// ROM路径
+ var romPath: String? = nil
+ private let serverType: LocalWebServer.ServerType
+
+ private lazy var webView: WKWebView = {
+ let configuration = WKWebViewConfiguration()
+ configuration.allowsInlineMediaPlayback = true
+ configuration.mediaTypesRequiringUserActionForPlayback = []
+
+ // 添加消息处理器
+ let contentController = WKUserContentController()
+ let proxy = WeakScriptMessageHandler(target: self)
+ contentController.add(proxy, name: "console")
+ configuration.userContentController = contentController
+
+ let view = WKWebView(frame: .zero, configuration: configuration)
+ view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
+ view.navigationDelegate = self
+ view.uiDelegate = self
+ view.allowsBackForwardNavigationGestures = false
+ view.scrollView.isScrollEnabled = false
+
+ let consoleHookJS = """
+ (function () {
+ function wrap(type) {
+ const original = console[type];
+ console[type] = function () {
+ window.webkit.messageHandlers.console.postMessage({
+ type: type,
+ message: Array.from(arguments).join(' ')
+ });
+ original.apply(console, arguments);
+ };
+ }
+
+ ['log', 'warn', 'error', 'info', 'debug'].forEach(wrap);
+ })();
+ """
+ let script = WKUserScript(
+ source: consoleHookJS,
+ injectionTime: .atDocumentStart,
+ forMainFrameOnly: false
+ )
+ view.configuration.userContentController.addUserScript(script)
+
+ return view
+ }()
+
+ private lazy var localServer: LocalWebServer = {
+ let server = LocalWebServer()
+ try? server.start(serverType: serverType)
+ return server
+ }()
+
+ deinit {
+ Log.debug("\(String(describing: Self.self)) deinit")
+ localServer.stop()
+ }
+
+ init(serverType: LocalWebServer.ServerType) {
+ self.serverType = serverType
+ super.init(frame: .zero)
+
+ addSubview(webView)
+ webView.snp.makeConstraints { make in
+ make.edges.equalToSuperview()
+ }
+ if let url = localServer.getURL() {
+ webView.load(URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData))
+ }
+ }
+
+ required init?(coder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+}
+
+// MARK: - 公开接口
+extension J2MEView {
+
+ /// 运行Jar
+ func openJar(filePath: String) {
+
+ }
+
+ /// 重置模拟器
+ func reset() {
+
+ }
+
+ /// 保存 SRAM 存档到文件
+ /// - Parameters:
+ /// - path: 保存路径
+ /// - completion: 完成回调
+ func save(to path: String, completion: ((_ isSucess: Bool)->Void)? = nil) {
+
+ }
+
+ /// 从文件加载 SRAM 存档
+ /// - Parameter path: 存档文件路径
+ func loadSave(path: String) {
+
+ }
+
+ /// 设置纵横比
+ func setAspect(width: Int, height: Int) {
+
+ }
+
+ // MARK: 输入控制
+ /// - Parameters:
+ /// - button: 按钮
+ /// - pressed: true 表示按下,false 表示释放
+ func pressButton(_ button: J2MEButton, pressed: Bool) {
+
+ }
+
+ /// 设置是否静音
+ func setMute(_ mute: Bool) {
+
+ }
+
+ /// 获取截图
+ /// - Note: 通过 WebView 截图获取当前画面
+ func snapShot() -> UIImage? {
+ // 使用 WebView 的截图能力
+ let renderer = UIGraphicsImageRenderer(bounds: webView.bounds)
+ let image = renderer.image { _ in
+ webView.drawHierarchy(in: webView.bounds, afterScreenUpdates: true)
+ }
+ return image
+ }
+
+ /// 快进 - 设置模拟器运行速度
+ /// - Parameter speed: 速度倍率 (1.0 = 正常速度, 2.0 = 2倍速, 等等)
+ func fastForward(speed: Float) {
+
+ }
+
+ /// 暂停模拟器
+ func pause() {
+
+ }
+
+ /// 恢复模拟器
+ func resume() {
+
+ }
+
+}
+
+// MARK: - WKScriptMessageHandler
+extension J2MEView: WKScriptMessageHandler {
+
+ func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
+ if message.name == "console" {
+ Log.debug("JS: \(message.body)")
+ }
+ }
+}
+
+// MARK: - WKNavigationDelegate
+extension J2MEView: WKNavigationDelegate {
+ func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
+ Log.debug("✅ WebView 加载完成")
+ }
+}
+
+// MARK: - WKUIDelegate
+extension J2MEView: WKUIDelegate {
+
+}
diff --git a/ManicEmu/ManicEmu/Sources/Business/Common/Views/JGenesisView.swift b/ManicEmu/ManicEmu/Sources/Business/Common/Views/JGenesisView.swift
index 121ba63c..7b26392a 100644
--- a/ManicEmu/ManicEmu/Sources/Business/Common/Views/JGenesisView.swift
+++ b/ManicEmu/ManicEmu/Sources/Business/Common/Views/JGenesisView.swift
@@ -85,20 +85,6 @@ class JGenesisView: BaseView {
/// ROM路径
var romPath: String? = nil
- /// 转义 JavaScript 字符串中的特殊字符
- /// - Parameter string: 原始字符串
- /// - Returns: 转义后的字符串,可安全用于 JavaScript 单引号字符串
- private func escapeJSString(_ string: String) -> String {
- var result = string
- result = result.replacingOccurrences(of: "\\", with: "\\\\") // 反斜杠
- result = result.replacingOccurrences(of: "'", with: "\\'") // 单引号
- result = result.replacingOccurrences(of: "\"", with: "\\\"") // 双引号
- result = result.replacingOccurrences(of: "\n", with: "\\n") // 换行
- result = result.replacingOccurrences(of: "\r", with: "\\r") // 回车
- result = result.replacingOccurrences(of: "\t", with: "\\t") // 制表符
- return result
- }
-
private lazy var webView: WKWebView = {
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
@@ -188,7 +174,7 @@ extension JGenesisView {
}
romPath = filePath
- let fileName = escapeJSString(filePath.lastPathComponent)
+ let fileName = filePath.lastPathComponent.escapeJSString()
// 注册文件到本地服务器
let fileId = localServer.registerFile(filePath: filePath)
@@ -239,7 +225,7 @@ extension JGenesisView {
}
romPath = filePath
- let fileName = escapeJSString(filePath.lastPathComponent)
+ let fileName = filePath.lastPathComponent.escapeJSString()
// 注册 ROM 文件到本地服务器
let romFileId = localServer.registerFile(filePath: filePath)
@@ -711,23 +697,6 @@ extension JGenesisView {
}
-final class WeakScriptMessageHandler: NSObject, WKScriptMessageHandler {
-
- weak var target: WKScriptMessageHandler?
-
- init(target: WKScriptMessageHandler) {
- self.target = target
- super.init()
- }
-
- func userContentController(
- _ userContentController: WKUserContentController,
- didReceive message: WKScriptMessage
- ) {
- target?.userContentController(userContentController, didReceive: message)
- }
-}
-
// MARK: - WKScriptMessageHandler
extension JGenesisView: WKScriptMessageHandler {
func decodeUnicodeString(_ str: String) -> String {
diff --git a/ManicEmu/ManicEmu/Sources/Tools/Extensions/StringExtensions.swift b/ManicEmu/ManicEmu/Sources/Tools/Extensions/StringExtensions.swift
index da477818..7c770747 100644
--- a/ManicEmu/ManicEmu/Sources/Tools/Extensions/StringExtensions.swift
+++ b/ManicEmu/ManicEmu/Sources/Tools/Extensions/StringExtensions.swift
@@ -243,6 +243,19 @@ extension String {
return (ipDigits, port)
}
+
+ /// 转义 JavaScript 字符串中的特殊字符
+ /// - Returns: 转义后的字符串,可安全用于 JavaScript 单引号字符串
+ func escapeJSString() -> String {
+ var result = self
+ result = result.replacingOccurrences(of: "\\", with: "\\\\") // 反斜杠
+ result = result.replacingOccurrences(of: "'", with: "\\'") // 单引号
+ result = result.replacingOccurrences(of: "\"", with: "\\\"") // 双引号
+ result = result.replacingOccurrences(of: "\n", with: "\\n") // 换行
+ result = result.replacingOccurrences(of: "\r", with: "\\r") // 回车
+ result = result.replacingOccurrences(of: "\t", with: "\\t") // 制表符
+ return result
+ }
}
// 扩展用于识别 emoji 的字符集
diff --git a/ManicEmu/ManicEmu/Sources/Tools/Others/LocalWebServer.swift b/ManicEmu/ManicEmu/Sources/Tools/Others/LocalWebServer.swift
index 72c5e72b..2652964a 100644
--- a/ManicEmu/ManicEmu/Sources/Tools/Others/LocalWebServer.swift
+++ b/ManicEmu/ManicEmu/Sources/Tools/Others/LocalWebServer.swift
@@ -9,7 +9,7 @@ import GCDWebServer
class LocalWebServer {
enum ServerType {
- case JGenesis, RomPatcher
+ case JGenesis, RomPatcher, J2meJS, freej2me
}
private var server: GCDWebServer?
@@ -86,6 +86,10 @@ class LocalWebServer {
indexFilename: nil,
cacheAge: 0,
allowRangeRequests: true)
+ case .J2meJS:
+ break
+ case .freej2me:
+ break
}
try server?.start(options: [
diff --git a/ManicEmu/ManicEmu/Sources/Tools/Others/WeakScriptMessageHandler.swift b/ManicEmu/ManicEmu/Sources/Tools/Others/WeakScriptMessageHandler.swift
new file mode 100644
index 00000000..aa0044c2
--- /dev/null
+++ b/ManicEmu/ManicEmu/Sources/Tools/Others/WeakScriptMessageHandler.swift
@@ -0,0 +1,24 @@
+//
+// WeakScriptMessageHandler.swift
+// ManicEmu
+//
+// Created by Daiuno on 2026/2/28.
+// Copyright © 2026 Manic EMU. All rights reserved.
+//
+
+final class WeakScriptMessageHandler: NSObject, WKScriptMessageHandler {
+
+ weak var target: WKScriptMessageHandler?
+
+ init(target: WKScriptMessageHandler) {
+ self.target = target
+ super.init()
+ }
+
+ func userContentController(
+ _ userContentController: WKUserContentController,
+ didReceive message: WKScriptMessage
+ ) {
+ target?.userContentController(userContentController, didReceive: message)
+ }
+}