查看: 3653|回复: 15

[胡扯] 让 require 支持中文路径

[复制链接]
梦石
0
星屑
9862
在线时间
580 小时
注册时间
2017-9-28
回帖
203
发表于 2019-4-25 12:43:09 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 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]
  1. diff --git a/old.rb b/require-patch.rb
  2. index 77d8862..f4fb13b 100644
  3. --- a/old.rb
  4. +++ b/require-patch.rb
  5. @@ -13,7 +13,7 @@ class ::Object
  6.    def require path
  7.      such_file = path
  8.      path += '.rb' unless path.end_with? '.rb'
  9. -    possibles = $:.map { |dirname| File.expand_path path, dirname }
  10. +    possibles = $:.map { |dirname| File.join dirname, path }
  11.      return false if possibles.any? { |file| $".include? file }
  12.      possibles.reverse_each do |file|
  13.        if File.exist? file
  14. @@ -23,9 +23,9 @@ class ::Object
  15.      end
  16.      raise LoadError, "cannot load such file -- #{such_file}"
  17.    end

  18.    def require_relative path
  19. -    file = caller[0][0, caller[0].index(':', 2)]
  20. +    file = caller[0][0, caller[0].index(':', 2)].sub(Dir.pwd, '.')
  21.      $: << (File.dirname file).force_encoding('utf-8')
  22.      require path
  23.      $:.pop
复制代码
[/fold]

评分

参与人数 1赞 +1 收起 理由
百里_飞柳 + 1 认可答案

查看全部评分

喵喵喵

老鹰

梦石
40
星屑
37812
在线时间
7091 小时
注册时间
2012-5-26
回帖
3297

极短24评委极短23参与极短22参与极短21评委老司机慢点开短篇十吟唱者组别冠军开拓者剧作品鉴家

发表于 2019-4-25 14:17:02 | 显示全部楼层
居然发到了水世界,但是这样你就会以为有人敢回复了吗(?)
回复

使用道具 举报

老黄鸡

梦石
4
星屑
45700
在线时间
7948 小时
注册时间
2009-7-6
回帖
13338

MZ评测员RM创作大赛01组委会开拓者贵宾

发表于 2019-4-25 16:37:33 | 显示全部楼层
有本事让它支持so库(x

点评

懂了 那我就先去支持so库去  发表于 2019-4-26 14:35
不兼容是 有些写法变了(所以除非你把之前的代码都改成适合之后版本的写法) fiber的问题是 之后不再是默认有了 需要 require 'continuation'  发表于 2019-4-26 13:50
改rb版本只要把原来的功能都接好 单纯要调so库就写个调用dll 我回去试试  发表于 2019-4-26 13:30
不过不改版本的话 我觉得那可以外接一个空壳dll调原rm的函数 这样做起来会更方便 也不会有什么冲突 和RGD也好兼容  发表于 2019-4-26 13:28
理论上不会不兼容吧 因为之后执行全是新ruby在执行了 只有rgss部分是原版本在执行 用的fiber也就全是新的那一套 不需要管新老不同的地方  发表于 2019-4-26 13:26
RGDirect - DirectX驱动的RGSS,点我了解.
(排满,暂停)RM全系列成套系统定制请联系QQ1213237796
不接受对其他插件维护的委托
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部