查看: 3987|回复: 0

[原创发布] 【不是创意的创意】解决调用环境变量中文乱码现象

[复制链接]

精灵族の天使<

梦石
0
星屑
1712
在线时间
3039 小时
注册时间
2007-3-16
回帖
33092

开拓者贵宾

发表于 2014-10-11 15:18:21 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 精灵使者 于 2015-2-1 17:44 编辑

精灵在汉化游戏的时候,发现一些使用中文用户名的游戏无法启动,症状如下:
QQ图片20141011144317.png
经检查出问题的脚本如下:
  1. # Create Path Where Folder Should Be Created
  2.   path = " " * 256
  3.   path = ENV['AV_APPDATA'].rstrip
  4.   $appPath = path.to_s + "\\To the Moon - Freebird Games"   
  5.   
  6.   # Create Your Folder
  7.   if !File.exists?($appPath)  
  8.     Dir.mkdir($appPath) #出问题的那行
  9.   end
复制代码

于是就麻烦了。
中文用户名导致EV['AV_APPDATA']产生乱码,以至于后面的建立文件夹失败。
所以就需要进行S2U转换一下。
解决脚本如下:

  1. #Using for Non-english speaking words encoding conversion
  2.   #quintess non-english paths
  3.   APPNAME = "\\To the Moon - Freebird Games"
  4. module Seiran20
  5.   module_function
  6.   class ::Integer; def to_ptr; self; end; def to_param; 'i'; end; def ord; self; end; end
  7.   class ::String;def to_ptr; [self].pack('p').unpack('L').first; end; def to_param; 'p'; end; end

  8.   def procname(a, b)
  9.     class << a; self; end.send :define_method, :inspect do b end
  10.     a
  11.   end

  12.   def api(dll,func)
  13.     procname lambda{|*args|
  14.        Win32API.new(dll,func,args.map{|x|x.to_param}, 'i').call *args
  15.     }, "Seiran20.api<#{dll}!#{func}>"
  16.   end

  17.   LL = api("kernel32", "LoadLibrary")
  18.   GPA = api("kernel32", "GetProcAddress")
  19.   def funcaddr(dll, func)
  20.      x = GPA.call(LL.call(dll), func)
  21.      x == 0 ? nil : x
  22.   end
  23.   def capi(dll, func)
  24.      callproc(GPA.call(LL.call(dll), func))
  25.       procname lambda{|*args|apicall.call((code=[0x55,0xe589].pack('CS')+args.map{|x| [0x68, x.to_ptr].pack('CL')}.reverse.join+[0xb8, addr.to_ptr, 0xD0FF, 0xc481, (stdcall ? 0 : args.length*4) , 0x10c2c9].pack('CLSSLL')),0,0,0,0)}, "Seiran20.capi<#{dll}!#{func}>"
  26.   end

  27.     def to_wc(str, cp = 65001)
  28.     (buf = "\0\0"*str.length)[0, 2*api('Kernel32', 'MultiByteToWideChar').call(cp, 0, str.to_ptr, -1, buf, buf.length)]
  29.   end

  30.   def to_mb(str, cp = 65001)
  31.     (buf = "\0\0"* str.length)[0, api('Kernel32', 'WideCharToMultiByte').call(cp, 0, str.to_ptr, -1, buf, buf.length, 0, 0)]
  32.   end

  33. end
  34. # Create Path Where Folder Should Be Created (edited)
  35. #  path = " " * 256
  36. #  path = ENV['AV_APPDATA'].rstrip
  37. path = Seiran20.to_mb(Seiran20.to_wc(ENV['AV_APPDATA']+"\0", 0)+"\0\0").sub(/\0+$/, "")
  38. $appPath = path.to_s + APPNAME   

  39. # redef File.exist for #72 Error
  40. def File.exists?(f)
  41.   ((open("#{f}\\nul", "rb").close || true) rescue false) ||
  42.   ((open(f, "rb").close || true) rescue false)
  43. end

  44.   # Create Your Folder
  45.   if !File.exists?($appPath)  
  46.     Dir.mkdir($appPath)
  47.   end
复制代码

由于是商业脚本所以考虑到没有权限修改,所以在此备份。
这是个严重的问题。
上面那个脚本依然出错的原因是,中文路径不支持File.exists
于是精灵就采用更改路径到公共路径下的方式来做,而各自的用户名将会被转换成数字分开。
  1.   # Create Path Where Folder Should Be Created(edited)
  2.   $commonpath = ENV['CommonProgramfiles'] + "\\To the Moon - Freebird Games"
  3.   $filename = " " * 36
  4.   $filename = (ENV['USERNAME'].rstrip).unpack("H*")[0].to_i(16).to_s(36)
  5.   $appPath = $commonpath + "\" + $filename
  6.   
  7.   # Create Your Folder
  8.   if !File.exists?($commonpath)  
  9.     Dir.mkdir($commonpath)
  10.   end
  11.   
  12.   if !File.exists?($appPath)  
  13.     Dir.mkdir($appPath)
  14.   end
  15.   
  16.   save_file_a = $appPath
  17.   music_file = $appPath + "\" + "music.txt"
  18.   window_file = $appPath + "\" + "window.txt"
  19.   user_name_file = $appPath + "\" "user_name.txt" #make a mark of username
复制代码

这是另外一种方案,但是会和以前的英文版路径不兼容。这个问题解决有些麻烦。
所以使用注册表修改av_data这个也是个好办法,就是直接修改变量。这样不会影响到用户名的绝大多数玩家。
2015年2月1日更新:
精灵已经完成了插件脚本方式,不需要对原始的脚本做任何修改就可以兼容中文!
使用方法新建文档插入即可。
脚本如下:
  1. #==============================================================================
  2. # ** Rewrite function on Dir and File (edited by fay_envoy)
  3. #------------------------------------------------------------------------------
  4. #  rewrite this code for solve the problem of non-English character problem.
  5. #  some function only using for the game commercial scripts.
  6. #==============================================================================
  7.   #--------------------------------------------------------------------------
  8.   # * Function needed
  9.   #--------------------------------------------------------------------------
  10. module Seiran20
  11.   module_function
  12.   class ::Integer; def to_ptr; self; end; def to_param; 'i'; end; def ord; self; end; end
  13.   class ::String;def to_ptr; [self].pack('p').unpack('L').first; end; def to_param; 'p'; end; end

  14.   def procname(a, b)
  15.     class << a; self; end.send :define_method, :inspect do b end
  16.     a
  17.   end

  18.   def api(dll,func)
  19.     procname lambda{|*args|
  20.        Win32API.new(dll,func,args.map{|x|x.to_param}, 'i').call *args
  21.     }, "Seiran20.api<#{dll}!#{func}>"
  22.   end

  23.   LL = api("kernel32", "LoadLibrary")
  24.   GPA = api("kernel32", "GetProcAddress")
  25.   def funcaddr(dll, func)
  26.      x = GPA.call(LL.call(dll), func)
  27.      x == 0 ? nil : x
  28.   end
  29.   def capi(dll, func)
  30.      callproc(GPA.call(LL.call(dll), func))
  31.       procname lambda{|*args|apicall.call((code=[0x55,0xe589].pack('CS')+args.map{|x| [0x68, x.to_ptr].pack('CL')}.reverse.join+[0xb8, addr.to_ptr, 0xD0FF, 0xc481, (stdcall ? 0 : args.length*4) , 0x10c2c9].pack('CLSSLL')),0,0,0,0)}, "Seiran20.capi<#{dll}!#{func}>"
  32.   end

  33.     def to_wc(str, cp = 65001)
  34.     (buf = "\0\0"*str.length)[0, 2*api('Kernel32', 'MultiByteToWideChar').call(cp, 0, str.to_ptr, -1, buf, buf.length)]
  35.   end

  36.   def to_mb(str, cp = 65001)
  37.     (buf = "\0\0"* str.length)[0, api('Kernel32', 'WideCharToMultiByte').call(cp, 0, str.to_ptr, -1, buf, buf.length, 0, 0)]
  38.   end
  39.   def transcode(str)
  40.   return to_mb(to_wc(str +"\0", 0)+"\0\0").sub(/\0+$/, "")
  41. end
  42. end
  43. PathFileExists = Win32API.new("shlwapi", "PathFileExistsW", "P", "I")
  44. MultiByteToWideChar = Win32API.new("kernel32", "MultiByteToWideChar", "ILPIPI", "I")
  45.   #--------------------------------------------------------------------------
  46.   # * File.exists rewrite
  47.   #--------------------------------------------------------------------------
  48. def File.exists?(filename)
  49.   nfilename = Seiran20.transcode(filename) + "\0"
  50.   len = MultiByteToWideChar.call(65001, 0, nfilename, -1, 0, 0) << 1
  51.   buf = " " * len
  52.   MultiByteToWideChar.call(65001, 0, nfilename, -1, buf, len)
  53.   return PathFileExists.call(buf) == 0 ? false : true
  54.   end
  55.   #--------------------------------------------------------------------------
  56.   # * FileTest.exist rewrite
  57.   #--------------------------------------------------------------------------
  58. def FileTest.exist?(filename)
  59.   return File.exists?(filename)
  60. end
  61.   #--------------------------------------------------------------------------
  62.   # * Dir function rewrite (only needed)
  63.   #--------------------------------------------------------------------------
  64. class << Dir
  65.    alias_method :old_mkdir, :mkdir
  66.    def mkdir(*args,&block)
  67.     args[0] = Seiran20.transcode(args[0])
  68.     old_mkdir(*args,&block)
  69.    end
  70. end
  71.   #--------------------------------------------------------------------------
  72.   # * File.new and open rewrite
  73.   #--------------------------------------------------------------------------
  74. class << File
  75.    alias_method :old_new, :new
  76.    alias_method :old_open, :open
  77.    def new(*args,&block)
  78.     args[0] = Seiran20.transcode(args[0])
  79.     old_new(*args,&block)
  80.    end
  81.    def open(*args,&block)
  82.     args[0] = Seiran20.transcode(args[0])
  83.     old_open(*args,&block)
  84.    end
  85. end
  86.   #--------------------------------------------------------------------------
  87.   # * open rewrite( for commercial scripts)
  88.   #--------------------------------------------------------------------------
  89.   alias old_open open
  90.   def open(*args,&block)
  91.     args[0] = Seiran20.transcode(args[0])
  92.     old_open(*args,&block)
  93.   end
复制代码
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

在本版发帖返回顶部