Commit 85a31944 authored by 神楽坂玲奈's avatar 神楽坂玲奈

fix

parent 6a8fd953
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
################# #################
/error.txt /error.txt
/main.exe /main.exe
/Gemfile.lock
/main.exa
/main.exy
/main.upx
/upx.exe
################# #################
## Jetbrains ## Jetbrains
......
source 'https://rubygems.org'
gem 'websocket-eventmachine-server'
gem 'rb-notifu'
gem 'i18n'
gem 'locale'
gem 'mail'
\ No newline at end of file
mkexy --encoding=utf-8 main.rb
exerb -c gui -k utf8 main.exy
upx main.exe
\ No newline at end of file
# = Building
if defined?(Ocra) or defined?(Exerb)
require 'json'
require 'pathname'
require 'fileutils'
require 'uri'
require 'open-uri'
require 'win32api'
require 'win32ole'
require 'win32/registry'
require 'websocket-eventmachine-server'
exit
end
# = Runtime
begin begin
# == initialize
Version = "0.1.0" Version = "0.1.0"
Platform = (RUBY_PLATFORM['mswin'] || RUBY_PLATFORM['mingw']) ? :win32 : :linux Platform = (RUBY_PLATFORM['mswin'] || RUBY_PLATFORM['mingw']) ? :win32 : :linux
System_Encoding = Encoding.find("locale") rescue Encoding.find(Encoding.locale_charmap)
Dir.chdir File.dirname(defined?(ExerbRuntime) ? ExerbRuntime.filepath.dup.force_encoding(System_Encoding).encode!(Encoding::UTF_8) : ENV["OCRA_EXECUTABLE"] || __FILE__)
# == config
Config = { Config = {
'port' => 9998, 'port' => 9998,
'ygopro' => { 'ygopro' => {
'path' => ['ygocore/ygopro_vs.exe', 'ygopro_vs.exe'], 'path' => ['ygocore/ygopro_vs.exe', 'ygopro_vs.exe', 'ygocore/ygopro', 'ygopro', 'ygocore/gframe', 'gframe'],
'textfont' => ['fonts/wqy-microhei.ttc', '/usr/share/fonts/wqy-microhei/wqy-microhei.ttc', '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 'c:/windows/fonts/simsun.ttc'], 'textfont' => ['fonts/wqy-microhei.ttc', '/usr/share/fonts/wqy-microhei/wqy-microhei.ttc', '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', '/Library/Fonts/Hiragino Sans GB W3.otf', 'c:/windows/fonts/simsun.ttc'],
'numfont' => ['/usr/share/fonts/gnu-free/FreeSansBold.ttf', 'c:/windows/fonts/arialbd.ttf'] 'numfont' => ['/usr/share/fonts/gnu-free/FreeSansBold.ttf', 'c:/windows/fonts/arialbd.ttf']
}, },
"url" => 'http://my-card.in/rooms' "url" => 'http://my-card.in/rooms'
} }
Dir.chdir File.dirname(ENV["OCRA_EXECUTABLE"] || __FILE__)
require 'json' require 'json'
if File.file? 'config.json' if File.file? 'config.json'
config = open('config.json') { |f| JSON.load(f) } config = open('config.json') { |f| JSON.load(f) }
...@@ -21,6 +37,10 @@ begin ...@@ -21,6 +37,10 @@ begin
end end
Config['ygopro']['path'] = Config['ygopro']['path'].find { |path| File.file? path } if Config['ygopro']['path'].is_a? Enumerable Config['ygopro']['path'] = Config['ygopro']['path'].find { |path| File.file? path } if Config['ygopro']['path'].is_a? Enumerable
Config['ygopro']['textfont'] = Config['ygopro']['textfont'].find { |path| File.file? path } if Config['ygopro']['textfont'].is_a? Enumerable
Config['ygopro']['numfont'] = Config['ygopro']['numfont'].find { |path| File.file? path } if Config['ygopro']['numfont'].is_a? Enumerable
if !Config['ygopro']['path'] if !Config['ygopro']['path']
require 'win32api' require 'win32api'
GetOpenFileName = Win32API.new("comdlg32.dll", "GetOpenFileNameW", "p", "i") GetOpenFileName = Win32API.new("comdlg32.dll", "GetOpenFileNameW", "p", "i")
...@@ -65,7 +85,7 @@ begin ...@@ -65,7 +85,7 @@ begin
0 # lpTemplateName L 0 # lpTemplateName L
].pack("LLLPLLLPLPLPPLS2L4") ].pack("LLLPLLLPLPLPPLS2L4")
Dir.chdir('.') { Dir.chdir('.') {
p GetOpenFileName.call(ofn) GetOpenFileName.call(ofn)
} }
szFile.delete!("\0".encode("UTF-16LE")) szFile.delete!("\0".encode("UTF-16LE"))
result = szFile.encode("UTF-8") result = szFile.encode("UTF-8")
...@@ -76,8 +96,7 @@ begin ...@@ -76,8 +96,7 @@ begin
exit exit
end end
end end
Config['ygopro']['textfont'] = Config['ygopro']['textfont'].find { |path| File.file? path } if Config['ygopro']['textfont'].is_a? Enumerable
Config['ygopro']['numfont'] = Config['ygopro']['numfont'].find { |path| File.file? path } if Config['ygopro']['numfont'].is_a? Enumerable
def save_config(config=Config) def save_config(config=Config)
require 'json' require 'json'
...@@ -101,7 +120,7 @@ begin ...@@ -101,7 +120,7 @@ begin
end end
def register_paths def register_paths
path = File.expand_path(ENV["OCRA_EXECUTABLE"] || $0) path = defined?(ExerbRuntime) ? ExerbRuntime.filepath : ENV["OCRA_EXECUTABLE"] || File.expand_path($0).gsub('/', '\\')
command = "\"#{path}\" \"%1\"" command = "\"#{path}\" \"%1\""
icon = "\"#{path}\" ,0" icon = "\"#{path}\" ,0"
[path, command, icon] [path, command, icon]
...@@ -111,15 +130,17 @@ begin ...@@ -111,15 +130,17 @@ begin
require 'win32/registry' require 'win32/registry'
path, command, icon = register_paths path, command, icon = register_paths
begin begin
Win32::Registry::HKEY_CLASSES_ROOT.create('mycard') { |reg| reg['URL Protocol'] = path.ljust path.bytesize } Win32::Registry::HKEY_CLASSES_ROOT.create('mycard') { |reg| reg['URL Protocol'] = path }
Win32::Registry::HKEY_CLASSES_ROOT.create('mycard\shell\open\command') { |reg| reg[nil] = command.ljust command.bytesize } Win32::Registry::HKEY_CLASSES_ROOT.create('mycard\shell\open\command') { |reg| reg[nil] = command }
Win32::Registry::HKEY_CLASSES_ROOT.create('mycard\DefaultIcon') { |reg| reg[nil] = icon.ljust icon.bytesize } Win32::Registry::HKEY_CLASSES_ROOT.create('mycard\DefaultIcon') { |reg| reg[nil] = icon }
Win32::Registry::HKEY_CLASSES_ROOT.create('.ydk') { |reg| reg[nil] = 'mycard' } Win32::Registry::HKEY_CLASSES_ROOT.create('.ydk') { |reg| reg[nil] = 'mycard' }
Win32::Registry::HKEY_CLASSES_ROOT.create('.yrp') { |reg| reg[nil] = 'mycard' } Win32::Registry::HKEY_CLASSES_ROOT.create('.yrp') { |reg| reg[nil] = 'mycard' }
#Win32::Registry::HKEY_CLASSES_ROOT.create('.deck') { |reg| reg[nil] = 'mycard' } #Win32::Registry::HKEY_CLASSES_ROOT.create('.deck') { |reg| reg[nil] = 'mycard' }
true true
rescue Win32::Registry::Error #Access Denied, need elevation rescue Win32::Registry::Error #Access Denied, need elevation
if ENV["OCRA_EXECUTABLE"] if defined?(ExerbRuntime)
System.elevate ExerbRuntime.filepath, ['register']
elsif ENV["OCRA_EXECUTABLE"]
System.elevate ENV["OCRA_EXECUTABLE"], ['register'] System.elevate ENV["OCRA_EXECUTABLE"], ['register']
else else
System.elevate Gem.ruby, [$0, 'register'] System.elevate Gem.ruby, [$0, 'register']
...@@ -152,13 +173,11 @@ begin ...@@ -152,13 +173,11 @@ begin
def load_system_conf def load_system_conf
system_conf = {} system_conf = {}
conf_path = File.join(File.dirname(Config['ygopro']['path']), 'system.conf') conf_path = File.join(File.dirname(Config['ygopro']['path']), 'system.conf')
IO.readlines(conf_path, encoding: Encoding::UTF_8).each do |line|
IO.readlines(conf_path, 'r:UTF-8').each do |line|
next if line[0, 1] == '#' next if line[0, 1] == '#'
field, contents = line.chomp.split(' = ', 2) field, contents = line.chomp.split(' = ', 2)
system_conf[field] = contents system_conf[field] = contents
end if File.file? conf_path end if File.file? conf_path
system_conf system_conf
end end
...@@ -182,21 +201,31 @@ begin ...@@ -182,21 +201,31 @@ begin
end end
module System module System
BUTTONS_OK = 0
def message_box(txt, title=nil, buttons=BUTTONS_OK)
require 'dl'
user32 = DL.dlopen('user32')
msgbox = user32['MessageBoxA', 'ILSSI']
r, rs = msgbox.call(0, txt, title, buttons)
return r
end
module_function module_function
def open(path=Config['url'], *args) def web(path=Config['url'], *args)
require 'win32ole' require 'win32ole'
$shell ||= WIN32OLE.new('Shell.Application') $shell ||= WIN32OLE.new('Shell.Application')
$shell.ShellExecute(path, *args) $shell.ShellExecute(path, *args)
end end
def elevate(path, args, pwd = Dir.pwd) def elevate(path, args, pwd = Dir.pwd)
open path, args.join(' '), Dir.pwd, 'runas' web path, args.join(' '), Dir.pwd, 'runas'
end end
end end
def run_ygopro(parameter) def run_ygopro(parameter)
System.open(Config['ygopro']['path'], parameter, File.dirname(Config['ygopro']['path'])) spawn File.basename(Config['ygopro']['path']), parameter, chdir: File.dirname(Config['ygopro']['path'])
end end
def join(room) def join(room)
...@@ -225,113 +254,112 @@ begin ...@@ -225,113 +254,112 @@ begin
def replay(replay) def replay(replay)
require 'fileutils' require 'fileutils'
moved_replay_directory = File.expand_path(File.dirname(Config['ygopro']['path'])) == Dir.pwd ? 'replay_moved' : 'replay' moved_replay_directory = File.expand_path(File.dirname(Config['ygopro']['path'])) == Dir.pwd ? 'replay_moved' : 'replay'
files = Dir.glob(File.join(File.dirname(Config['ygopro']['path']), 'replay', '*.yrp')) - File.join(File.dirname(Config['ygopro']['path']), 'replay', replay+'.yrp') files = Dir.glob(File.join(File.dirname(Config['ygopro']['path']), 'replay', '*.yrp'))
files.delete File.join(File.dirname(Config['ygopro']['path']), 'replay', replay+'.yrp')
FileUtils.mv files, moved_replay_directory FileUtils.mv files, moved_replay_directory
run_ygopro('-r') run_ygopro('-r')
end end
def local_user(system_conf=load_system_conf) def local_user(system_conf=load_system_conf)
nickname, password = system_conf['nickname'].split('$') nickname, password = system_conf['nickname'] ? system_conf['nickname'].split('$') : []
{'nickname' => nickname, 'password' => password} {'nickname' => nickname, 'password' => password}
end end
def parse(command) def parse(command)
case command case command
when 'register' when 'register'
register register
when 'registed' when 'registed'
registed? registed?
when 'mycard:///' when 'mycard:///'
#service #service
when /mycard:\/\/(.*)/ when /mycard:\/\/(.*)/
parse_uri(command) parse_uri(command)
when /.*\.(?:ydk|yrp)$/ when /.*\.(?:ydk|yrp)$/
parse_path(command) #解析函数可以分开 parse_path(command) #解析函数可以分开
end end
end end
def parse_path(path) def parse_path(path)
require 'fileutils' require 'fileutils'
case File.extname(path) case File.extname(path)
when '.ydk' when '.ydk'
deck_directory = File.join(File.dirname(Config['ygopro']['path']), 'deck') deck_directory = File.join(File.dirname(Config['ygopro']['path']), 'deck')
Dir.mkdir(deck_directory) unless File.directory?(deck_directory) if file.dirname(path) != deck_directory
FileUtils.copy(path, deck_directory) Dir.mkdir(deck_directory) unless File.directory?(deck_directory)
deck(File.basename(path, '.ydk')) FileUtils.copy(path, deck_directory)
when '.yrp' end
replay_directory = File.expand_path(File.dirname(Config['ygopro']['path'])) deck(File.basename(path, '.ydk'))
unless File.expand_path(File.dirname(path)) == replay_directory when '.yrp'
Dir.mkdir(replay_directory) unless File.directory?(replay_directory) replay_directory = File.expand_path(File.dirname(Config['ygopro']['path']))
FileUtils.copy(path, replay_directory) unless File.expand_path(File.dirname(path)) == replay_directory
end Dir.mkdir(replay_directory) unless File.directory?(replay_directory)
replay(File.basename(path, '.yrp')) FileUtils.copy(path, replay_directory)
end
replay(File.basename(path, '.yrp'))
end end
end end
def parse_uri(uri) def parse_uri(uri)
file = uri.dup.force_encoding("UTF-8")
file.force_encoding("GBK") unless file.valid_encoding?
file.encode!("UTF-8")
require 'uri' require 'uri'
if uri[0, 9] == 'mycard://' if uri[0, 9] == 'mycard://'
file = URI.unescape file[9, file.size-9] file = URI.unescape uri[9, uri.size-9]
uri = "http://" + URI.escape(file) uri = "http://" + URI.escape(file)
else else
uri = file uri = file
end end
case file case file
when /^(.*\.yrp)$/i when /^(.*\.yrp)$/i
require 'open-uri' require 'open-uri'
#fix File.basename #fix File.basename
$1 =~ /(.*)(?:\\|\/)(.*?\.yrp)/ $1 =~ /(.*)(?:\\|\/)(.*?\.yrp)/
src = open(uri, 'rb') { |src| src.read } src = open(uri, 'rb') { |src| src.read }
Dir.mkdir("replay") unless File.directory?("replay") Dir.mkdir("replay") unless File.directory?("replay")
open('replay/' + $2, 'wb') { |dest| dest.write src } open('replay/' + $2, 'wb') { |dest| dest.write src }
replay('replay/' + $2) replay('replay/' + $2)
when /^(.*\.ydk)$/i when /^(.*\.ydk)$/i
require 'open-uri' require 'open-uri'
#fix File.basename #fix File.basename
$1 =~ /(.*)(?:\\|\/)(.*?)\.ydk/ $1 =~ /(.*)(?:\\|\/)(.*?)\.ydk/
src = open(uri, 'rb') { |src| src.read } src = open(uri, 'rb') { |src| src.read }
Dir.mkdir(File.join(File.dirname(Config['ygopro']['path']), 'deck')) unless File.join(File.dirname(Config['ygopro']['path']), 'deck') Dir.mkdir(File.join(File.dirname(Config['ygopro']['path']), 'deck')) unless File.join(File.dirname(Config['ygopro']['path']), 'deck')
open(File.join(File.dirname(Config['ygopro']['path']), 'deck', $2+'.ydk'), 'wb') { |dest| dest.write src } open(File.join(File.dirname(Config['ygopro']['path']), 'deck', $2+'.ydk'), 'wb') { |dest| dest.write src }
deck($2) deck($2)
when /^(?:(.+?)(?:\:(.+?))?\@)?([\d\.]+)\:(\d+)(?:\/(.*))$/ when /^(?:(.+?)(?:\:(.+?))?\@)?([\d\.]+)\:(\d+)(?:\/(.*))$/
join({ join({
'name' => $5.to_s, 'name' => $5.to_s,
'user' => { 'user' => {
'nickname' => $1, 'nickname' => $1,
'password' => $2 'password' => $2
}, },
'server' => { 'server' => {
'ip' => $3, 'ip' => $3,
'port' => $4.to_i, 'port' => $4.to_i,
'auth' => !!$2 'auth' => !!$2
} }
}) })
end end
end end
save_config save_config
if ARGV.first if ARGV.first
puts parse(ARGV.first).to_json parse ARGV.first.dup.force_encoding(System_Encoding).encode!(Encoding::UTF_8)
else else
register if !registed? register if !registed?
System.open(Config['url']) if File.file? 'ruby\bin\rubyw.exe'
spawn 'ruby\bin\rubyw.exe', '-KU', 'lib/main.rb'
else
System.web(Config['url'])
end
#service #service
#require all, for ocra
require 'open-uri'
require 'pathname'
require 'win32api'
end end
rescue => exception rescue => exception
open('error.txt', 'w') { |f| f.write ([exception] + exception.backtrace).join("\r\n") } error = "程序出现了错误,请把你的操作及以下信息发送至zh99998@gmail.com来帮助我们完善程序
exec 'notepad error.txt' an error occurs, please send your operation and message below to zh99998@gmail.com
end
#{exception.inspect}
#{exception.backtrace.join("\n")}"
open('error.txt', 'w') { |f| f.write error }
spawn 'notepad', 'error.txt'
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment