| 赞 | 76 |
| VIP | |
| 好人卡 | |
| 积分 | 99 |
| 经验 | |
| 最后登录 | 2026-3-14 |
| 在线时间 | 580 小时 |
- 梦石
- 0
- 星屑
- 9862
- 在线时间
- 580 小时
- 注册时间
- 2017-9-28
- 回帖
- 203
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有账号?注册会员
×
本帖最后由 hyrious 于 2019-4-25 14:04 编辑
一个比较古老的问题了,以前就被提到过多次比如这条
不过一般发布的时候都是把脚本塞进 Scripts.rvdata2 里所以大家基本会选择跳过这个问题
考虑到 File.read 等方法是支持中文路径的,我们可以利用这个对 require 等进行重写:
[fold=查看代码][pre lang="ruby" file="unicode_require.rb"]# coding: utf-8
class ::Object
def load path, wrap=false
if wrap
TOPLEVEL_BINDING.dup.eval (File.read path), path
else
TOPLEVEL_BINDING.eval (File.read path), path
end
true
end
def require path
such_file = path
path += '.rb' unless path.end_with? '.rb'
possibles = $:.map { |dirname| File.expand_path path, dirname }
return false if possibles.any? { |file| $".include? file }
possibles.reverse_each do |file|
if File.exist? file
$" << file and load file
return true
end
end
raise LoadError, "cannot load such file -- #{such_file}"
end
def require_relative path
file = caller[0][0, caller[0].index(':', 2)]
$: << (File.dirname file).force_encoding('utf-8')
require path
$:.pop
end
end[/pre][/fold]
以上脚本仅在 RGSS3 (RMVA/RGD) 下测试通过
注意:上述仍然不支持将游戏本身放在中文路径下,原因是 Dir.pwd 等返回的上级路径是用 GBK 编码存的
为了临时兼容一下中文路径,我们可以仅修改两行,不让 pwd 出现在 loaded_features 里
[fold=查看 patch]- diff --git a/old.rb b/require-patch.rb
- index 77d8862..f4fb13b 100644
- --- a/old.rb
- +++ b/require-patch.rb
- @@ -13,7 +13,7 @@ class ::Object
- def require path
- such_file = path
- path += '.rb' unless path.end_with? '.rb'
- - possibles = $:.map { |dirname| File.expand_path path, dirname }
- + possibles = $:.map { |dirname| File.join dirname, path }
- return false if possibles.any? { |file| $".include? file }
- possibles.reverse_each do |file|
- if File.exist? file
- @@ -23,9 +23,9 @@ class ::Object
- end
- raise LoadError, "cannot load such file -- #{such_file}"
- end
- def require_relative path
- - file = caller[0][0, caller[0].index(':', 2)]
- + file = caller[0][0, caller[0].index(':', 2)].sub(Dir.pwd, '.')
- $: << (File.dirname file).force_encoding('utf-8')
- require path
- $:.pop
复制代码 [/fold] |
评分
-
查看全部评分
|