nissy-fmc

A Rubik's cube FMC assistant
git clone https://git.tronto.net/nissy-fmc
Download | Log | Files | Refs | README | LICENSE

nissy_flutter_ffi.dart (2351B)


      1 import 'dart:async';
      2 import 'dart:ffi';
      3 import 'dart:io';
      4 import 'dart:isolate';
      5 import 'dart:typed_data';
      6 import 'package:flutter/services.dart';
      7 
      8 import 'package:ffi/ffi.dart';
      9 import 'nissy_flutter_ffi_bindings_generated.dart';
     10 
     11 void nissy_init(ByteData tables) {
     12   // Copy tables to C ffi heap, pass to actual nissy_init()
     13   final tablesList = tables.buffer.asUint8List();
     14   final n = tablesList.lengthInBytes;
     15   final tablesHeap = calloc<Char>(n);
     16   for (var i = 0; i < n; i++) {
     17     tablesHeap[i] = tablesList[i];
     18   }
     19   _bindings.nissy_init(tablesHeap);
     20 }
     21 
     22 String ptrCharToString(Pointer<Char> ptr) => ptr.cast<Utf8>().toDartString();
     23 Pointer<Char> stringToPtrChar(String str) {
     24   final n = str.length;
     25   final strNative = str.toNativeUtf8().cast<Char>();
     26   final ptr = calloc<Char>(n+10);
     27   for (var i = 0; i < n; i++) {
     28     ptr[i] = strNative[i];
     29   }
     30   ptr[n] = 0;
     31   return ptr;
     32 }
     33 
     34 String nissy_test() {
     35   final ptr = calloc<Char>(50);
     36   _bindings.nissy_test(ptr);
     37   final str = ptrCharToString(ptr);
     38   calloc.free(ptr);
     39   return str;
     40 }
     41 
     42 List<String> nissy_eos_in(String scr, int n) {
     43   var ret = List<String>.empty(growable: true);
     44 
     45   final scramble = stringToPtrChar(scr);
     46   final eofb = stringToPtrChar('eofb');
     47   final uf = stringToPtrChar('uf');
     48   final normal = stringToPtrChar('normal');
     49 
     50   print('Doing thing...');
     51 
     52   var bufferPtr = calloc<Char>(5000); // hope it is enough
     53   final nsols = _bindings.nissy_solve(eofb, uf, n, normal, scramble, bufferPtr);
     54   ret.add(ptrCharToString(bufferPtr));
     55 
     56   print('Ret 0: ' + ret[0]);
     57 
     58   calloc.free(bufferPtr);
     59   calloc.free(normal);
     60   calloc.free(uf);
     61   calloc.free(eofb);
     62   calloc.free(scramble);
     63 
     64   return ret;
     65 }
     66 
     67 const String _libName = 'nissy_flutter_ffi';
     68 
     69 /// The dynamic library in which the symbols for [NissyFlutterFfiBindings] can be found.
     70 final DynamicLibrary _dylib = () {
     71   if (Platform.isMacOS || Platform.isIOS) {
     72     return DynamicLibrary.open('$_libName.framework/$_libName');
     73   }
     74   if (Platform.isAndroid || Platform.isLinux) {
     75     return DynamicLibrary.open('lib$_libName.so');
     76   }
     77   if (Platform.isWindows) {
     78     return DynamicLibrary.open('$_libName.dll');
     79   }
     80   throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
     81 }();
     82 
     83 /// The bindings to the native functions in [_dylib].
     84 final NissyFlutterFfiBindings _bindings = NissyFlutterFfiBindings(_dylib);