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

fix

parent 6a8fd953
......@@ -3,6 +3,11 @@
#################
/error.txt
/main.exe
/Gemfile.lock
/main.exa
/main.exy
/main.upx
/upx.exe
#################
## 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
# == initialize
Version = "0.1.0"
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 = {
'port' => 9998,
'ygopro' => {
'path' => ['ygocore/ygopro_vs.exe', 'ygopro_vs.exe'],
'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'],
'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', '/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']
},
"url" => 'http://my-card.in/rooms'
}
Dir.chdir File.dirname(ENV["OCRA_EXECUTABLE"] || __FILE__)
require 'json'
if File.file? 'config.json'
config = open('config.json') { |f| JSON.load(f) }
......@@ -21,6 +37,10 @@ begin
end
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']
require 'win32api'
GetOpenFileName = Win32API.new("comdlg32.dll", "GetOpenFileNameW", "p", "i")
......@@ -65,7 +85,7 @@ begin
0 # lpTemplateName L
].pack("LLLPLLLPLPLPPLS2L4")
Dir.chdir('.') {
p GetOpenFileName.call(ofn)
GetOpenFileName.call(ofn)
}
szFile.delete!("\0".encode("UTF-16LE"))
result = szFile.encode("UTF-8")
......@@ -76,8 +96,7 @@ begin
exit
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)
require 'json'
......@@ -101,7 +120,7 @@ begin
end
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\""
icon = "\"#{path}\" ,0"
[path, command, icon]
......@@ -111,15 +130,17 @@ begin
require 'win32/registry'
path, command, icon = register_paths
begin
Win32::Registry::HKEY_CLASSES_ROOT.create('mycard') { |reg| reg['URL Protocol'] = path.ljust path.bytesize }
Win32::Registry::HKEY_CLASSES_ROOT.create('mycard\shell\open\command') { |reg| reg[nil] = command.ljust command.bytesize }
Win32::Registry::HKEY_CLASSES_ROOT.create('mycard\DefaultIcon') { |reg| reg[nil] = icon.ljust icon.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 }
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('.yrp') { |reg| reg[nil] = 'mycard' }
#Win32::Registry::HKEY_CLASSES_ROOT.create('.deck') { |reg| reg[nil] = 'mycard' }
true
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']
else
System.elevate Gem.ruby, [$0, 'register']
......@@ -152,13 +173,11 @@ begin
def load_system_conf
system_conf = {}
conf_path = File.join(File.dirname(Config['ygopro']['path']), 'system.conf')
IO.readlines(conf_path, 'r:UTF-8').each do |line|
IO.readlines(conf_path, encoding: Encoding::UTF_8).each do |line|
next if line[0, 1] == '#'
field, contents = line.chomp.split(' = ', 2)
system_conf[field] = contents
end if File.file? conf_path
system_conf
end
......@@ -182,21 +201,31 @@ begin
end
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
def open(path=Config['url'], *args)
def web(path=Config['url'], *args)
require 'win32ole'
$shell ||= WIN32OLE.new('Shell.Application')
$shell.ShellExecute(path, *args)
end
def elevate(path, args, pwd = Dir.pwd)
open path, args.join(' '), Dir.pwd, 'runas'
web path, args.join(' '), Dir.pwd, 'runas'
end
end
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
def join(room)
......@@ -225,113 +254,112 @@ begin
def replay(replay)
require 'fileutils'
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
run_ygopro('-r')
end
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}
end
def parse(command)
case command
when 'register'
register
when 'registed'
registed?
when 'mycard:///'
#service
when /mycard:\/\/(.*)/
parse_uri(command)
when /.*\.(?:ydk|yrp)$/
parse_path(command) #解析函数可以分开
when 'register'
register
when 'registed'
registed?
when 'mycard:///'
#service
when /mycard:\/\/(.*)/
parse_uri(command)
when /.*\.(?:ydk|yrp)$/
parse_path(command) #解析函数可以分开
end
end
def parse_path(path)
require 'fileutils'
case File.extname(path)
when '.ydk'
deck_directory = File.join(File.dirname(Config['ygopro']['path']), 'deck')
Dir.mkdir(deck_directory) unless File.directory?(deck_directory)
FileUtils.copy(path, deck_directory)
deck(File.basename(path, '.ydk'))
when '.yrp'
replay_directory = File.expand_path(File.dirname(Config['ygopro']['path']))
unless File.expand_path(File.dirname(path)) == replay_directory
Dir.mkdir(replay_directory) unless File.directory?(replay_directory)
FileUtils.copy(path, replay_directory)
end
replay(File.basename(path, '.yrp'))
when '.ydk'
deck_directory = File.join(File.dirname(Config['ygopro']['path']), 'deck')
if file.dirname(path) != deck_directory
Dir.mkdir(deck_directory) unless File.directory?(deck_directory)
FileUtils.copy(path, deck_directory)
end
deck(File.basename(path, '.ydk'))
when '.yrp'
replay_directory = File.expand_path(File.dirname(Config['ygopro']['path']))
unless File.expand_path(File.dirname(path)) == replay_directory
Dir.mkdir(replay_directory) unless File.directory?(replay_directory)
FileUtils.copy(path, replay_directory)
end
replay(File.basename(path, '.yrp'))
end
end
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'
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)
else
uri = file
end
case file
when /^(.*\.yrp)$/i
require 'open-uri'
#fix File.basename
$1 =~ /(.*)(?:\\|\/)(.*?\.yrp)/
src = open(uri, 'rb') { |src| src.read }
Dir.mkdir("replay") unless File.directory?("replay")
open('replay/' + $2, 'wb') { |dest| dest.write src }
replay('replay/' + $2)
when /^(.*\.ydk)$/i
require 'open-uri'
#fix File.basename
$1 =~ /(.*)(?:\\|\/)(.*?)\.ydk/
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')
open(File.join(File.dirname(Config['ygopro']['path']), 'deck', $2+'.ydk'), 'wb') { |dest| dest.write src }
deck($2)
when /^(?:(.+?)(?:\:(.+?))?\@)?([\d\.]+)\:(\d+)(?:\/(.*))$/
join({
'name' => $5.to_s,
'user' => {
'nickname' => $1,
'password' => $2
},
'server' => {
'ip' => $3,
'port' => $4.to_i,
'auth' => !!$2
}
})
when /^(.*\.yrp)$/i
require 'open-uri'
#fix File.basename
$1 =~ /(.*)(?:\\|\/)(.*?\.yrp)/
src = open(uri, 'rb') { |src| src.read }
Dir.mkdir("replay") unless File.directory?("replay")
open('replay/' + $2, 'wb') { |dest| dest.write src }
replay('replay/' + $2)
when /^(.*\.ydk)$/i
require 'open-uri'
#fix File.basename
$1 =~ /(.*)(?:\\|\/)(.*?)\.ydk/
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')
open(File.join(File.dirname(Config['ygopro']['path']), 'deck', $2+'.ydk'), 'wb') { |dest| dest.write src }
deck($2)
when /^(?:(.+?)(?:\:(.+?))?\@)?([\d\.]+)\:(\d+)(?:\/(.*))$/
join({
'name' => $5.to_s,
'user' => {
'nickname' => $1,
'password' => $2
},
'server' => {
'ip' => $3,
'port' => $4.to_i,
'auth' => !!$2
}
})
end
end
save_config
if ARGV.first
puts parse(ARGV.first).to_json
parse ARGV.first.dup.force_encoding(System_Encoding).encode!(Encoding::UTF_8)
else
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
#require all, for ocra
require 'open-uri'
require 'pathname'
require 'win32api'
end
rescue => exception
open('error.txt', 'w') { |f| f.write ([exception] + exception.backtrace).join("\r\n") }
exec 'notepad error.txt'
end
error = "程序出现了错误,请把你的操作及以下信息发送至zh99998@gmail.com来帮助我们完善程序
an error occurs, please send your operation and message below to zh99998@gmail.com
#{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