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

@@ -2,5 +2,5 @@
windows:
@echo "Building for Windows..."
go build -ldflags="-X polaris/db.Version=$(git describe --tags --long)" -buildmode=c-shared -o ui/windows/libpolaris.dll ./cmd/binding
go build -tags c -ldflags="-X polaris/db.Version=$(git describe --tags --long)" -buildmode=c-shared -o ui/windows/libpolaris.dll ./cmd/binding
cd ui && flutter build windows

View File

@@ -1,11 +1,21 @@
package main
import "C"
import "polaris/cmd"
import (
"os"
"polaris/cmd"
"polaris/log"
)
func main() {}
//export Start
func Start() {
cmd.Start()
cmd.Start(true)
}
//export Stop
func Stop() {
log.Infof("stop polaris")
os.Exit(0)
}

View File

@@ -1,15 +1,20 @@
package cmd
import (
"os"
"polaris/db"
"polaris/log"
"polaris/server"
)
func Start(sharedLib bool) {
if sharedLib || os.Getenv("GIN_MODE") == "release" {
log.InitLogger(true)
} else {
log.InitLogger(false)
}
func Start() {
log.Infof("------------------- Starting Polaris ---------------------")
dbClient, err := db.Open()
if err != nil {
log.Panicf("init db error: %v", err)

View File

@@ -3,5 +3,5 @@ package main
import "polaris/cmd"
func main() {
cmd.Start()
cmd.Start(false)
}

View File

@@ -15,12 +15,12 @@ var atom zap.AtomicLevel
const dataPath = "./data"
func init() {
func InitLogger(toFile bool) {
atom = zap.NewAtomicLevel()
atom.SetLevel(zap.DebugLevel)
w := zapcore.Lock(os.Stdout)
if os.Getenv("GIN_MODE") == "release" {
if toFile {
w = zapcore.AddSync(&lumberjack.Logger{
Filename: filepath.Join(dataPath, "logs", "polaris.log"),
MaxSize: 50, // megabytes

View File

@@ -20,7 +20,7 @@ type LogFile struct {
func (s *Server) GetAllLogs(c *gin.Context) (interface{}, error) {
fs, err := os.ReadDir(db.LogPath)
if err != nil {
return nil, errors.Wrap(err, "read log dir")
return []LogFile{}, nil
}
var logs []LogFile
for _, f := range fs {

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}"