Debian Codegraph @

lib/ninix/dll/kagari.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # kagari.rb - a (Real) Kagari SHIORI loader for ninix
4 # Copyright (C) 2024 by 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/import"
15
16 require_relative "../logging"
17
18 module Kagari
19
20 extend Fiddle::Importer
21 begin
22 dlload "libkagari.#{RbConfig::CONFIG['SOEXT']}"
23 extern 'int kagari_load(char *, long)'
24 extern 'int kagari_unload(int)'
25 extern 'char *kagari_request(int, char *, long *)'
26
27 $_kagari = self
28 rescue
29 $_kagari = nil
30 end
31
32 class Shiori
33
34 def initialize(dll_name)
35 @dll_name = dll_name
36 @handle = nil
37 @func = {}
38 end
39
40 def find(topdir, dll_name)
41 result = 0
42 unless $_kagari.nil?
43 if File.exist?(File.join(topdir, 'kagari.conf.lua'))
44 result = 300
45 end
46 end
47 return result
48 end
49
50 def show_description
51 Logging::Logging.info(
52 "Shiori: a (Real) KAGARI loader for ninix\n" \
53 " Copyright (C) 2024 by Tatakinov")
54 end
55
56 def load(dir: nil)
57 unless dir.end_with?(File::SEPARATOR)
58 dir = [dir, File::SEPARATOR].join
59 end
60 buf = Fiddle::Pointer.malloc(dir.bytesize + 1, free = nil)
61 buf[0, dir.bytesize] = dir
62 @id = $_kagari.kagari_load(buf, dir.bytesize)
63 if @id ==-1
64 return 0
65 end
66 return 1
67 end
68
69 def unload
70 $_kagari.kagari_unload(@id)
71 end
72
73 def request(req)
74 buf = Fiddle::Pointer.malloc(req.bytesize + 1, free = nil)
75 buf[0, req.bytesize] = req
76 len = [req.bytesize].pack('l!')
77 result = $_kagari.kagari_request(@id, buf, len)
78 len, = len.unpack('l!')
79 unless result.null?
80 ret = result[0, len].to_s
81 Fiddle.free(result)
82 ret
83 else
84 ''
85 end
86 end
87 end
88 end