Debian Codegraph @

lib/ninix/dll/saori_native.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # saori_cpuid.rb - a saori_cpuid compatible Saori module for ninix
4 # Copyright (C) 2024 Tatakinov
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License (version 2) as
8 # published by the Free Software Foundation. It is distributed in the
9 # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 # PURPOSE. See the GNU General Public License for more details.
12 #
13
14 require 'fiddle'
15
16 require_relative '../dll'
17 require_relative '../error'
18
19 module SaoriNative
20
21 class Saori < DLL::SAORI
22 def self.create(name)
23 begin
24 instance = self.new(name)
25 rescue
26 return nil
27 end
28 return instance
29 end
30
31 def initialize(name)
32 @func = {}
33 filename = File.basename(name, ".*")
34 raise NotFoundError unless ENV.include?('SAORI_FALLBACK_PATH')
35 path_list = ENV['SAORI_FALLBACK_PATH'].split(':')
36 for path in path_list
37 begin
38 @handle = Fiddle::Handle.new(File.join(path, name))
39 @func[:load] = Fiddle::Function.new(@handle[filename + '_saori_load'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG], Fiddle::TYPE_INT)
40 @func[:request] = Fiddle::Function.new(@handle[filename + '_saori_request'], [Fiddle::TYPE_LONG, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP)
41 @func[:unload] = Fiddle::Function.new(@handle[filename + '_saori_unload'], [Fiddle::TYPE_LONG], Fiddle::TYPE_INT)
42 break
43 rescue
44 # nop
45 end
46 end
47 raise NotFoundError if @handle.nil?
48 end
49
50 def load(dir: nil)
51 unless dir.end_with?(File::SEPARATOR)
52 dir = [dir, File::SEPARATOR].join()
53 end
54 path = Fiddle::Pointer.malloc(
55 dir.bytesize + 1,
56 freefunc=nil
57 )
58 path[0, dir.bytesize] = dir
59 @id = @func[:load].call(path, dir.bytesize)
60 1
61 end
62
63 def unload
64 @func[:unload].call(@id)
65 @handle.close
66 end
67
68 def request(req)
69 request = Fiddle::Pointer.malloc(
70 req.bytesize + 1,
71 freefunc=nil
72 )
73 request[0, req.bytesize] = req
74 rlen =[req.bytesize].pack("l!")
75 result = @func[:request].call(@id, request, rlen)
76 rlen, = rlen.unpack("l!")
77 unless result.null?
78 ret = result[0, rlen].to_s
79 Fiddle.free(result)
80 ret
81 else
82 ''
83 end
84 end
85 end
86 end