initializePoWLinks function

void initializePoWLinks()

Implementation

void initializePoWLinks() {
  var insideSdk = path.join('znn_sdk_dart', 'lib', 'src', 'pow', 'blobs');
  var currentPathListParts = path.split(Directory.current.path);
  currentPathListParts.removeLast();
  var executablePathListParts = path.split(Platform.resolvedExecutable);
  executablePathListParts.removeLast();
  var possiblePaths = List<String>.empty(growable: true);
  possiblePaths.add(Directory.current.path);
  possiblePaths.add(path.joinAll(executablePathListParts));
  executablePathListParts.removeLast();
  possiblePaths
      .add(path.join(path.joinAll(executablePathListParts), 'Resources'));
  possiblePaths.add(path.join(path.joinAll(currentPathListParts), insideSdk));
  possiblePaths.add(
      path.join(path.joinAll(currentPathListParts), 'packages', insideSdk));

  var libraryPath = '';
  var found = false;

  for (var currentPath in possiblePaths) {
    libraryPath = path.join(currentPath, 'libpow_links.so');

    if (Platform.isMacOS) {
      libraryPath = path.join(currentPath, 'libpow_links.dylib');
    }
    if (Platform.isWindows) {
      libraryPath = path.join(currentPath, 'libpow_links.dll');
    }

    var libFile = File(libraryPath);

    if (libFile.existsSync()) {
      found = true;
      break;
    }
  }

  logger.info('Loading libpow_links from path ' + libraryPath);

  if (!found) {
    throw invalidPowLinksLibPathException;
  }

  // Open the dynamic library
  final dylib = DynamicLibrary.open(libraryPath);

  // Look up the CPP function 'generatePoW'
  final generatePoWPointer =
      dylib.lookup<NativeFunction<_GeneratePowFunc>>('generatePoW');
  _generatePoWFunction = generatePoWPointer.asFunction<_GeneratePoW>();

  // Look up the C function 'benchmark'
  final functionPointer =
      dylib.lookup<NativeFunction<_BenchmarkPowFunc>>('benchmark');
  _benchmarkFunction = functionPointer.asFunction<_BenchmarkPoW>();
}