feat: build windows dll and call dll in flutter

This commit is contained in:
Simon Ding
2025-04-10 14:24:46 +08:00
parent 5ab347845a
commit 6371139607
14 changed files with 95 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
//go:build !c
package ui
import "embed"

6
ui/embed_c.go Normal file
View File

@@ -0,0 +1,6 @@
//go:build c
package ui
import "embed"
var Web embed.FS

41
ui/lib/ffi/backend.dart Normal file
View File

@@ -0,0 +1,41 @@
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'package:ui/widgets/utils.dart' as Utils;
class FFIBackend {
final lib = DynamicLibrary.open(libname());
static String libname() {
if (Utils.isDesktop()) {
if (Platform.isWindows) {
return 'libpolaris.dll';
} else if (Platform.isLinux) {
return 'libpolaris.so';
} else if (Platform.isMacOS) {
return 'libpolaris.dylib';
} else {
throw UnsupportedError(
'Unsupported platform: ${Platform.operatingSystem}');
}
} else {
return "";
}
}
Future<void> start() async {
var s = lib
.lookup<NativeFunction<Void Function()>>('Start')
.asFunction<void Function()>();
return Isolate.run(s);
}
Future<void> stop() async {
var s = lib
.lookup<NativeFunction<Void Function()>>('Stop')
.asFunction<void Function()>();
return s();
}
}

View File

@@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:ui/activity.dart';
import 'package:ui/ffi/backend.dart';
import 'package:ui/init_wizard.dart';
import 'package:ui/login_page.dart';
import 'package:ui/movie_watchlist.dart';
@@ -15,7 +16,11 @@ import 'package:ui/tv_details.dart';
import 'package:ui/welcome_page.dart';
import 'package:ui/widgets/utils.dart';
void main() {
void main() async {
if (isDesktop()) {
FFIBackend().start();
}
initializeDateFormatting()
.then((_) => runApp(const ProviderScope(child: MyApp())));
}

View File

@@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:ui/providers/server_response.dart';
import 'package:ui/widgets/utils.dart';
class APIs {
static final _baseUrl = baseUrl();
@@ -68,6 +69,9 @@ class APIs {
static String baseUrl() {
if (kReleaseMode) {
if (isDesktop()) {
return "http://127.0.0.1:8080";
}
return "";
}
return "http://127.0.0.1:8080";

View File

@@ -97,6 +97,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.2"
ffi:
dependency: "direct main"
description:
name: ffi
sha256: "289279317b4b16eb2bb7e271abccd4bf84ec9bdcbe999e278a94b804f5630418"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.4"
flutter:
dependency: "direct main"
description: flutter
@@ -547,5 +555,5 @@ packages:
source: hosted
version: "1.1.1"
sdks:
dart: ">=3.7.0-0 <4.0.0"
dart: ">=3.7.0 <4.0.0"
flutter: ">=3.27.0"

View File

@@ -31,6 +31,7 @@ dependencies:
flutter:
sdk: flutter
dio: ^5.7.0
ffi: ^2.0.0
# The following adds the Cupertino Icons font to your application.

View File

@@ -81,6 +81,9 @@ install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}
install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/libpolaris.dll" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)
if(PLUGIN_BUNDLED_LIBRARIES)
install(FILES "${PLUGIN_BUNDLED_LIBRARIES}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"