Debian Codegraph @

lib/ninix/dll/satori_native.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # satori_native.rb - a (Real) Satori SHIORI loader for ninix
4 # Copyright (C) 2025 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 Satori_native
19
20 extend Fiddle::Importer
21 begin
22 dlload "libsatori.#{RbConfig::CONFIG['SOEXT']}"
23 extern 'int satori_load(char *, long)'
24 extern 'int satori_unload(int)'
25 extern 'char *satori_request(int, char *, long *)'
26
27 $_satori = self
28 rescue => e
29 $_satori = nil
30 end
31
32 def self.list_dict(top_dir)
33 buf = []
34 begin
35 dir_list = Pathname(top_dir).children().map {|x| x.relative_path_from(Pathname(top_dir)).to_path }
36 rescue #except OSError:
37 dir_list = []
38 end
39 for filename in dir_list
40 basename = File.basename(filename, '.*')
41 ext = File.extname(filename)
42 ext = ext.downcase
43 if (filename.downcase.start_with?('dic') and \
44 ['.txt', '.sat'].include?(ext)) or \
45 ['replace.txt', 'replace_after.txt',
46 'satori_conf.txt', 'satori_conf.sat'].include?(filename.downcase) # XXX
47 buf << File.join(top_dir, filename)
48 end
49 end
50 return buf
51 end
52
53 class Shiori
54
55 def initialize(dll_name)
56 @dll_name = dll_name
57 @handle = nil
58 @func = {}
59 end
60
61 def find(topdir, dll_name)
62 result = 0
63 unless $_satori.nil?
64 unless Satori_native.list_dict(topdir).empty?
65 result = 300
66 end
67 end
68 return result
69 end
70
71 def show_description
72 Logging::Logging.info(
73 "Shiori: a (Real) Satori loader for ninix\n" \
74 " Copyright (C) 2025 by Tatakinov")
75 end
76
77 def load(dir: nil)
78 unless dir.end_with?(File::SEPARATOR)
79 dir = [dir, File::SEPARATOR].join
80 end
81 buf = Fiddle::Pointer.malloc(dir.bytesize + 1, free = nil)
82 buf[0, dir.bytesize] = dir
83 @id = $_satori.satori_load(buf, dir.bytesize)
84 if @id == 0
85 return 0
86 end
87 return 1
88 end
89
90 def unload
91 $_satori.satori_unload(@id)
92 end
93
94 def request(req)
95 buf = Fiddle::Pointer.malloc(req.bytesize + 1, free = nil)
96 buf[0, req.bytesize] = req
97 len = [req.bytesize].pack('l!')
98 result = $_satori.satori_request(@id, buf, len)
99 len, = len.unpack('l!')
100 unless result.null?
101 ret = result[0, len].to_s
102 Fiddle.free(result)
103 ret
104 else
105 ''
106 end
107 end
108 end
109 end