| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# kawari8.rb - a (Real) 華和梨 loader for ninix |
| 4 |
# Copyright (C) 2002, 2003 by ABE Hideaki <[email protected]> |
| 5 |
# Copyright (C) 2002-2019 by Shyouzou Sugitani <[email protected]> |
| 6 |
# Copyright (C) 2002, 2003 by MATSUMURA Namihiko <[email protected]> |
| 7 |
# |
| 8 |
# This program is free software; you can redistribute it and/or modify it |
| 9 |
# under the terms of the GNU General Public License (version 2) as |
| 10 |
# published by the Free Software Foundation. It is distributed in the |
| 11 |
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
| 12 |
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 13 |
# PURPOSE. See the GNU General Public License for more details. |
| 14 |
# |
| 15 |
|
| 16 |
require "fiddle/import" |
| 17 |
|
| 18 |
require_relative "../logging" |
| 19 |
|
| 20 |
$_kawari8 = nil |
| 21 |
|
| 22 |
module Kawari8 |
| 23 |
extend Fiddle::Importer |
| 24 |
begin |
| 25 |
dlload "libshiori.#{RbConfig::CONFIG['SOEXT']}" # "_kawari8.so" |
| 26 |
extern "int so_library_init()" |
| 27 |
extern "int so_library_cleanup()" |
| 28 |
extern "unsigned int so_create(const char *, long)" |
| 29 |
extern "int so_dispose(unsigned int)" |
| 30 |
extern "const char *so_request(unsigned int, const char *, long *)" |
| 31 |
extern "void so_free(unsigned int, const char *)" |
| 32 |
result = so_library_init() |
| 33 |
unless result.zero? |
| 34 |
$_kawari8 = self |
| 35 |
else |
| 36 |
$_kawari8 = nil |
| 37 |
end |
| 38 |
rescue |
| 39 |
$_kawari8 = nil |
| 40 |
end |
| 41 |
|
| 42 |
class Shiori |
| 43 |
|
| 44 |
##saori_list = {} |
| 45 |
|
| 46 |
def initialize(dll_name) |
| 47 |
@dll_name = dll_name |
| 48 |
@handle = 0 |
| 49 |
@req = [] |
| 50 |
end |
| 51 |
|
| 52 |
##def use_saori(saori) |
| 53 |
## @saori = saori |
| 54 |
##end |
| 55 |
|
| 56 |
def find(topdir, dll_name) |
| 57 |
result = 0 |
| 58 |
result = 205 if not $_kawari8.nil? and File.file?(File.join(topdir, 'kawarirc.kis')) |
| 59 |
return result |
| 60 |
end |
| 61 |
|
| 62 |
def show_description |
| 63 |
Logging::Logging.info( |
| 64 |
"Shiori: Real Kawari8 loader for ninix\n" \ |
| 65 |
" Copyright (C) 2002, 2003 by ABE Hideaki\n" \ |
| 66 |
" Copyright (C) 2002-2019 by Shyouzou Sugitani\n" \ |
| 67 |
" Copyright (C) 2002, 2003 by MATSUMURA Namihiko") |
| 68 |
end |
| 69 |
|
| 70 |
def load(dir: nil) |
| 71 |
@dir = dir |
| 72 |
return 0 if $_kawari8.nil? |
| 73 |
unless @dir.end_with?(File::SEPARATOR) |
| 74 |
@dir = [@dir, File::SEPARATOR].join() |
| 75 |
end |
| 76 |
##_kawari8.setcallback(self.saori_exist, |
| 77 |
## Shiori.saori_load, |
| 78 |
## Shiori.saori_unload, |
| 79 |
## Shiori.saori_request) |
| 80 |
@handle = $_kawari8.so_create(@dir, @dir.bytesize) |
| 81 |
return @handle.zero? ? 0 : 1 |
| 82 |
end |
| 83 |
|
| 84 |
def unload |
| 85 |
return if $_kawari8.nil? |
| 86 |
$_kawari8.so_dispose(@handle) |
| 87 |
##for name in list(Shiori.saori_list.keys()) |
| 88 |
## if not name.startswith(os.fsdecode(self.dir)) |
| 89 |
## continue |
| 90 |
## end |
| 91 |
## if Shiori.saori_list[name][1]: |
| 92 |
## Shiori.saori_list[name][0].unload() |
| 93 |
## end |
| 94 |
## del Shiori.saori_list[name] |
| 95 |
## # XXX |
| 96 |
## _kawari8.setcallback(lambda *a: 0, # dummy |
| 97 |
## Shiori.saori_load, |
| 98 |
## Shiori.saori_unload, |
| 99 |
## Shiori.saori_request) |
| 100 |
##end |
| 101 |
end |
| 102 |
|
| 103 |
def request(req_string) |
| 104 |
return '' if $_kawari8.nil? # FIXME |
| 105 |
req_len = [req_string.bytesize].pack("l!") |
| 106 |
result = $_kawari8.so_request(@handle, req_string, req_len) |
| 107 |
req_len, = req_len.unpack("l!") |
| 108 |
return_val = result[0, req_len].to_s |
| 109 |
$_kawari8.so_free(@handle, result) |
| 110 |
return return_val |
| 111 |
end |
| 112 |
|
| 113 |
##def saori_exist(self, saori): |
| 114 |
## module = self.saori.request(saori) |
| 115 |
## if module: |
| 116 |
## Shiori.saori_list[saori] = [module, 0] |
| 117 |
## return len(Shiori.saori_list) |
| 118 |
## else: |
| 119 |
## return 0 |
| 120 |
|
| 121 |
##@classmethod |
| 122 |
##def saori_load(cls, saori, path): |
| 123 |
## result = 0 |
| 124 |
## if saori in cls.saori_list and cls.saori_list[saori][1] == 0: |
| 125 |
## result = cls.saori_list[saori][0].load(path) |
| 126 |
## cls.saori_list[saori][1] = result |
| 127 |
## return result |
| 128 |
|
| 129 |
##@classmethod |
| 130 |
##def saori_unload(cls, saori): |
| 131 |
## result = 0 |
| 132 |
## if saori in cls.saori_list and cls.saori_list[saori][1] != 0: |
| 133 |
## result = cls.saori_list[saori][0].unload() |
| 134 |
## cls.saori_list[saori][1] = 0 |
| 135 |
## return result |
| 136 |
|
| 137 |
##@classmethod |
| 138 |
##def saori_request(cls, saori, req): |
| 139 |
## result = b'SAORI/1.0 500 Internal Server Error' |
| 140 |
## if saori in cls.saori_list: |
| 141 |
## if cls.saori_list[saori][1] == 0: |
| 142 |
## head, tail = os.path.split(os.fsencode(saori)) |
| 143 |
## cls.saori_list[saori][1] = cls.saori_list[saori][0].load(head) |
| 144 |
## if cls.saori_list[saori][1]: |
| 145 |
## result = cls.saori_list[saori][0].request(req) |
| 146 |
## return result |
| 147 |
|
| 148 |
end |
| 149 |
end |