Project1
标题:
很诡异的语法错误,无法解决。
[打印本页]
作者:
px.凤翔九天
时间:
2010-12-21 23:38
标题:
很诡异的语法错误,无法解决。
本帖最后由 px.凤翔九天 于 2010-12-22 23:08 编辑
表示这个问题可能有一定难度,成功解答者表示可能会给v。
问题:诡异的语法错误:在我自己写了一点代码后发现出现了诡异的语法错误...而且因为中途没有测试,所以出错后再想找问题所在就找不到了.....估计不是鼠标脚本的问题,也不太可能是转码工具的问题....我自己写的脚本中提到的文件夹均以创建(验证完毕)表示只要不用我写的。其他的都能和谐共存,用我的结果连写个条件分歧都报错......
错误就是我的脚本的31行...完全不知道怎么改...
试着注释掉,结果依然显示31行报错(注释都有错....)
索性删掉,结果其他地方又报错...
于是我带着报错就删的心态处理.....结果...都删掉了...
极其郁闷.....
先发一下使用到的各种诡异代码(喜欢的自己拿去用):
easyconv:
#==============================================================================
# 仭 EasyConv module - Ver 0.90
#--
# Moonlight INN
# http://cgi.members.interq.or.jp/aquarius/rasetsu/
# RaTTiE
#
[email protected]
#----------------------
# EasyConv::s2u(text) : S-JIS -> UTF-8
# EasyConv::u2s(text) : UTF-8 -> S-JIS
#==============================================================================
module EasyConv
# API梡掕悢掕媊
CP_ACP = 0
CP_UTF8 = 65001
#------------------------------------
# 仠 S-JIS -> UTF-8
#------------------------------------
def s2u(text)
# API掕媊
m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
# S-JIS -> Unicode
len = m2w.call(CP_ACP, 0, text, -1, nil, 0);
buf = "\0" * (len*2)
m2w.call(CP_ACP, 0, text, -1, buf, buf.size/2);
# Unicode -> UTF-8
len = w2m.call(CP_UTF8, 0, buf, -1, nil, 0, nil, nil);
ret = "\0" * len
w2m.call(CP_UTF8, 0, buf, -1, ret, ret.size, nil, nil);
return ret
end
# module_function偲偟偰岞奂
module_function :s2u
#------------------------------------
# 仠 UTF-8 -> S-JIS
#------------------------------------
def u2s(text)
# API掕媊
m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
# UTF-8 -> Unicode
len = m2w.call(CP_UTF8, 0, text, -1, nil, 0);
buf = "\0" * (len*2)
m2w.call(CP_UTF8, 0, text, -1, buf, buf.size/2);
# Unicode -> S-JIS
len = w2m.call(CP_ACP, 0, buf, -1, nil, 0, nil, nil);
ret = "\0" * len
w2m.call(CP_ACP, 0, buf, -1, ret, ret.size, nil, nil);
return ret
end
# module_function偲偟偰岞奂
module_function :u2s
end
复制代码
另外一个不详的转码工具:
#==============================================================================
# ■ String
#------------------------------------------------------------------------------
# 字符串类。可处理任意长度的字节串。(追加编码转换的定义)
#==============================================================================
class String
#--------------------------------------------------------------------------
# ● 用来编码 Ruby 字符串、解码 unicode 的 两个 Windows API 函数
#--------------------------------------------------------------------------
@@MultiByteToWideChar = Win32API.new("kernel32", "MultiByteToWideChar", ['I', 'L', 'P', 'I', 'P', 'I'], 'I')
@@WideCharToMultiByte = Win32API.new("kernel32", "WideCharToMultiByte", ['I', 'L', 'P', 'I', 'P', 'I', 'P', 'P'], 'I')
#--------------------------------------------------------------------------
# ● 返回将 Ruby UTF-8 字符串对象(本身)编码为 unicode 后的字符串
#--------------------------------------------------------------------------
def to_unicode
len = @@MultiByteToWideChar.call(65001, 0, self, -1, 0, 0) << 1
buf =" " * len
# 65001: UTF-8 字符集编码(代码页)
@@MultiByteToWideChar.call(65001, 0, self, -1, buf, len)
return buf
end
#--------------------------------------------------------------------------
# ● 返回将编码为 unicode 的字符串对象(本身)解码为 UTF-8 后的字符串
#--------------------------------------------------------------------------
def to_UTF8
len = @@WideCharToMultiByte.call(65001, 0, self, -1, 0, 0, 0, 0)
buf =" " * len
@@WideCharToMultiByte.call(65001, 0, self, -1, buf, len, 0, 0)
# 去掉 '\0' 字符串结束符(因为转换之后仅仅用于 Ruby 字符串)
buf.slice!(-1, 1)
return buf
end
end
FindFirstFile = Win32API.new("kernel32", "FindFirstFileW", "PP", "L")
FindNextFile = Win32API.new("kernel32", "FindNextFileW", "LP", "I")
filenames = []
findFileData = " " * 592
hFindFile = FindFirstFile.call(
"*".to_unicode, findFileData)
while FindNextFile.call(hFindFile, findFileData) != 0
filenames.push(findFileData[44, 45 + 260 * 2].to_UTF8)
end
复制代码
为判断文件名为中文的定义方法:filetest.exists?
PathFileExists = Win32API.new("shlwapi", "PathFileExistsW", "P", "I")
MultiByteToWideChar = Win32API.new("kernel32", "MultiByteToWideChar", "ILPIPI", "I")
def FileTest.exists?(filename)
len = MultiByteToWideChar.call(65001, 0, filename, -1, 0, 0) << 1
buf = " " * len
MultiByteToWideChar.call(65001, 0, filename, -1, buf, len)
return PathFileExists.call(buf) == 0 ? false : true
end
删除文件夹的脚本:
[code]module Kernel
def deleteDir(dirname)
Dir.foreach(dirname) { |filename|
next if(filename == ".." || filename == ".")
path = dirname + File::SEPARATOR + filename
if(File.ftype(path) == "directory")
deleteDir(path)
else
File.delete(path)
end
}
Dir.delete(dirname)
end
end
复制代码
复制文件夹的写法(里面是例子:复制data文件夹到桌面)
class String
@@MultiByteToWideChar = Win32API.new("kernel32", "MultiByteToWideChar", ['I', 'L', 'P', 'I', 'P', 'I'], 'I')
def to_unicode
len = @@MultiByteToWideChar.call(65001, 0, self, -1, 0, 0) << 1
buf =" " * len
@@MultiByteToWideChar.call(65001, 0, self, -1, buf, len)
return buf
end
end
sh_file_operation = Win32API.new('shell32', 'SHFileOperationW', 'P', 'i')
src = "./Data"
xx="桌面"
dest = "C:/Documents and Settings/Administrator/#{xx}"
op_struct = [0, 2, src.to_unicode, dest.to_unicode, 4 | 0x10 | 0x0200, 0, 0, 0].pack('iIppSiii')
sh_file_operation.call(op_struct)
复制代码
鼠标脚本:
#===========================================================
#===================
# 本脚本来自http://rpg.blue/web/,使用和转载请保留此信息
#==============================================================================
#=================以下两个用来调整战斗时的手感问题,可以自己试试。
$敌人选框扩大 = 20
$角色选框扩大 = 30
#==============================================================================
# API调用
#==============================================================================
$ShowCursor = Win32API.new("user32", "ShowCursor", 'i', 'l')
$GetCursorPos = Win32API.new("user32", "GetCursorPos", 'p', 'i')
$ScreenToClient = Win32API.new("user32", "ScreenToClient", 'ip', 'i')
$GetActiveWindow = Win32API.new("user32", "GetActiveWindow", nil, 'l')
$Window_HWND = $GetActiveWindow.call
$GetKeyState = Win32API.new("user32", "GetKeyState", 'i', 'i')
#$FindWindow = Win32API.new("user32", "FindWindow", 'pp', 'i')
#$HookStart = Win32API.new("mouse_hook.dll", "HookStart", 'i', nil)
#$HookEnd = Win32API.new("mouse_hook.dll", "HookEnd", nil, nil)
#$GetMouseStatus = Win32API.new("mouse_hook.dll", "GetMouseStatus", 'i', 'i')
#$Window_HWND = $FindWindow.call(nil, 'mousetry')
module Mouse
LEFT = 0x01
RIGHT = 0x02
def self.init(sprite = nil)
# $HookStart.call($Window_HWND)
$ShowCursor.call(0)
@show_cursor = false
@mouse_sprite = Sprite.new
@mouse_sprite.z = 99999
@mouse_sprite.bitmap = Bitmap.new('Graphics/Icons/cursor1.png')
#@mouse_sprite.bitmap.fill_rect(Rect.new(0, 0, 32, 32), Color.new(0, 0, 0))
@left_press = false
@right_press = false
@left_trigger = false
@right_trigger = false
@left_repeat = false
@right_repeat = false
@click_lock = false
update
end
def self.exit
@mouse_sprite.bitmap.dispose
@mouse_sprite.dispose
@show_cursor = true
# $HookEnd.call
$ShowCursor.call(1)
end
def self.mouse_debug
return @mouse_debug.bitmap
end
def self.update
left_down = $GetKeyState.call(0x01)
right_down = $GetKeyState.call(0x02)
@click_lock = false
mouse_x, mouse_y = self.get_mouse_pos
if @mouse_sprite != nil
@mouse_sprite.x = mouse_x
@mouse_sprite.y = mouse_y
end
if left_down[7] == 1
@left_repeat = (not @left_repeat)
@left_trigger = (not @left_press)
@left_press = true
else
@left_press = false
@left_trigger = false
@left_repeat = false
end
if right_down[7] == 1
@right_repeat = (not @right_repeat)
@right_trigger = (not @right_press)
@right_press = true
else
@right_press = false
@right_trigger = false
@right_repeat = false
end
end
def self.get_mouse_pos
point_var = [0, 0].pack('ll')
if $GetCursorPos.call(point_var) != 0
if $ScreenToClient.call($Window_HWND, point_var) != 0
x, y = point_var.unpack('ll')
if (x < 0) or (x > 10000) then x = 0 end
if (y < 0) or (y > 10000) then y = 0 end
if x > 640 then x = 640 end
if y > 480 then y = 480 end
return x, y
else
return 0, 0
end
else
return 0, 0
end
end
def self.press?(mouse_code)
if mouse_code == LEFT
if @click_lock
return false
else
return @left_press
end
elsif mouse_code == RIGHT
return @right_press
else
return false
end
end
def self.trigger?(mouse_code)
if mouse_code == LEFT
if @click_lock
return false
else
return @left_trigger
end
elsif mouse_code == RIGHT
return @right_trigger
else
return false
end
end
def self.repeat?(mouse_code)
if mouse_code == LEFT
if @click_lock
return false
else
return @left_repeat
end
elsif mouse_code == RIGHT
return @right_repeat
else
return false
end
end
def self.click_lock?
return @click_lock
end
def self.click_lock
@click_lock = true
end
def self.click_unlock
@click_lock = false
end
end
module Input
if @self_update == nil
@self_update = method('update')
@self_press = method('press?')
@self_trigger = method('trigger?')
@self_repeat = method('repeat?')
end
def self.update
@self_update.call
Mouse.update
end
def self.press?(key_code)
if @self_press.call(key_code)
return true
end
if key_code == C
return Mouse.press?(Mouse::LEFT)
elsif key_code == B
return Mouse.press?(Mouse::RIGHT)
else
return @self_press.call(key_code)
end
end
def self.trigger?(key_code)
if @self_trigger.call(key_code)
return true
end
if key_code == C
return Mouse.trigger?(Mouse::LEFT)
elsif key_code == B
return Mouse.trigger?(Mouse::RIGHT)
else
return @self_trigger.call(key_code)
end
end
def self.repeat?(key_code)
if @self_repeat.call(key_code)
return true
end
if key_code == C
return Mouse.repeat?(Mouse::LEFT)
elsif key_code == B
return Mouse.repeat?(Mouse::RIGHT)
else
return @self_repeat.call(key_code)
end
end
end
class Window_Selectable
if @self_alias == nil
alias self_update update
@self_alias = true
end
def update
#self.cursor_rect.empty
self_update
if self.active and @item_max > 0
index_var = @index
tp_index = @index
mouse_x, mouse_y = Mouse.get_mouse_pos
mouse_not_in_rect = true
for i in 0...@item_max
@index = i
update_cursor_rect
top_x = self.cursor_rect.x + self.x + 16
top_y = self.cursor_rect.y + self.y + 16
bottom_x = top_x + self.cursor_rect.width
bottom_y = top_y + self.cursor_rect.height
if (mouse_x > top_x) and (mouse_y > top_y) and
(mouse_x < bottom_x) and (mouse_y < bottom_y)
mouse_not_in_rect = false
if tp_index != @index
tp_index = @index
$game_system.se_play($data_system.cursor_se)
end
break
end
end
if mouse_not_in_rect
@index = index_var
update_cursor_rect
Mouse.click_lock
else
Mouse.click_unlock
end
end
end
end
class Window_NameInput
if @self_alias == nil
alias self_update update
@self_alias = true
end
def update
#self.cursor_rect.empty
self_update
if self.active
index_var = @index
mouse_x, mouse_y = Mouse.get_mouse_pos
mouse_not_in_rect = true
for i in (0...CHARACTER_TABLE.size).to_a.push(180)
@index = i
update_cursor_rect
top_x = self.cursor_rect.x + self.x + 16
top_y = self.cursor_rect.y + self.y + 16
bottom_x = top_x + self.cursor_rect.width
bottom_y = top_y + self.cursor_rect.height
#
if (mouse_x > top_x) and (mouse_y > top_y) and
(mouse_x < bottom_x) and (mouse_y < bottom_y)
mouse_not_in_rect = false
break
end
end
if mouse_not_in_rect
@index = index_var
update_cursor_rect
Mouse.click_lock
else
Mouse.click_unlock
end
end
end
end
class Window_InputNumber
if @self_alias == nil
alias self_update update
@self_alias = true
end
def update
#self.cursor_rect.empty
self_update
mouse_x, mouse_y = Mouse.get_mouse_pos
if self.active and @digits_max > 0
index_var = @index
mouse_not_in_rect = true
for i in 0...@digits_max
@index = i
update_cursor_rect
top_x = self.cursor_rect.x + self.x + 16
bottom_x = top_x + self.cursor_rect.width
#
if (mouse_x > top_x) and (mouse_x < bottom_x)
mouse_not_in_rect = false
break
end
end
if mouse_not_in_rect
@index = index_var
update_cursor_rect
Mouse.click_lock
else
Mouse.click_unlock
end
end
if @last_mouse_y == nil
@last_mouse_y = mouse_y
end
check_pos = (@last_mouse_y - mouse_y).abs
if check_pos > 10
$game_system.se_play($data_system.cursor_se)
place = 10 ** (@digits_max - 1 - @index)
n = @number / place % 10
@number -= n * place
n = (n + 1) % 10 if mouse_y < @last_mouse_y
n = (n + 9) % 10 if mouse_y > @last_mouse_y
@number += n * place
refresh
@last_mouse_y = mouse_y
end
end
end
class Scene_File
if @self_alias == nil
alias self_update update
@self_alias = true
end
def update
mouse_x, mouse_y = Mouse.get_mouse_pos
Mouse.click_lock
idx = 0
for i in @savefile_windows
top_x = i.x + 16
top_y = i.y + 16
bottom_x = top_x + i.width
bottom_y = top_y + i.height
if (mouse_x > top_x) and (mouse_y > top_y) and
(mouse_x < bottom_x) and (mouse_y < bottom_y)
i.selected = true
if @file_index != idx
@file_index = idx
$game_system.se_play($data_system.cursor_se)
end
Mouse.click_unlock
else
i.selected = false
end
idx += 1
end
self_update
end
end
class Arrow_Enemy
if @self_alias == nil
alias self_update update
@self_alias = true
end
def update
mouse_x, mouse_y = Mouse.get_mouse_pos
idx = 0
for i in $game_troop.enemies do
if i.exist?
top_x = i.screen_x - self.ox
top_y = i.screen_y - self.oy
bottom_x = top_x + self.src_rect.width
bottom_y = top_y + self.src_rect.height
if (mouse_x > top_x - $敌人选框扩大) and (mouse_y > top_y - $敌人选框扩大) and
(mouse_x < bottom_x + $敌人选框扩大) and (mouse_y < bottom_y + $敌人选框扩大)
if @index != idx
$game_system.se_play($data_system.cursor_se)
@index = idx
end
end
end
idx += 1
end
self_update
end
end
class Arrow_Actor
if @self_alias == nil
alias self_update update
@self_alias = true
end
def update
mouse_x, mouse_y = Mouse.get_mouse_pos
idx = 0
for i in $game_party.actors do
if i.exist?
top_x = i.screen_x - self.ox
top_y = i.screen_y - self.oy
bottom_x = top_x + self.src_rect.width
bottom_y = top_y + self.src_rect.height
if (mouse_x > top_x - $角色选框扩大) and (mouse_y > top_y - $角色选框扩大) and
(mouse_x < bottom_x + $角色选框扩大) and (mouse_y < bottom_y + $角色选框扩大)
if @index != idx
$game_system.se_play($data_system.cursor_se)
@index = idx
end
end
end
idx += 1
end
self_update
end
end
class Game_Player
if @self_alias == nil
alias self_update update
@self_alias = true
end
def update
mouse_x, mouse_y = Mouse.get_mouse_pos
if @last_move_x == nil
@last_move_x = false
end
if Mouse.press?(Mouse::LEFT)
last_moving = moving?
last_direction = @direction
unless moving? or $game_system.map_interpreter.running? or
@move_route_forcing or $game_temp.message_window_showing
last_x = @x
if @last_move_x
@last_move_x = false
elsif mouse_x > screen_x + 16
move_right
elsif mouse_x < screen_x - 16
move_left
end
last_y = @y
if last_x != @x
@last_move_x = true
elsif mouse_y > screen_y
move_down
elsif mouse_y < screen_y - 32
move_up
end
if last_y != @y
@last_move_x = false
elsif not @last_move_x
case last_direction
when 2
turn_down
when 4
turn_left
when 6
turn_right
when 8
turn_up
end
end
end
end
self_update
end
end
Mouse.init
END { Mouse.exit }
#==============================================================================
# 本脚本来自http://rpg.blue/web/,使用和转载请保留此信息
#==============================================================================
复制代码
完美输入法(多次测试不是这个的问题,外加脚本太长,就不复制过来了。)
最后:
我的问题写法:
def traverse(path)
if FileTest.directory?(path)
dir=Dir.open(path)
while name = dir.read
next if name=="."
next if name==".."
traverse(path + "/" + name)
end
dir.close
else
process_file(path)
end
end
def process_file(path)
if File.extname(path)==".txt"
f = File.open(path, "rb")
vs = f.readlines
f.close
yearcycle=2000
monthcycle=1
loop do
if vs.include?("#{yearcycle}年#{monthcycle}月")==true
path_to_add=nil
path_from=File.dirname(path)
path_from_final=File.basename(path_from)
if FileTest.exists?("C:/Documents and Settings/Administrator/#{EasyConv::u2s(bingzhiname)}/#{yearcycle}-#{monthcycle}") == false
fullpath="C:/Documents and Settings/Administrator/#{EasyConv::u2s(bingzhiname)}/#{yearcycle}.#{monthcycle}"
system("md #{fullpath})
end
if FileTest.exists?("C:/Documents and Settings/Administrator/桌面/文件夹/#{yearcycle}.#{monthcycle}/#{path_from_final}") == true
path_to_add="重名文件夹"
end
src = "#{path_from}"
dest = "C:/Documents and Settings/Administrator/桌面/文件夹/#{yearcycle}-#{monthcycle}/#{path_to_add}"
op_struct = [0, 2, src.to_unicode, dest.to_unicode, 4 | 0x10 | 0x0200, 0, 0, 0].pack('iIppSiii')
sh_file_operation.call(op_struct)
break
else
if monthcycle!=12
monthcycle+=1
else
yearcycle+=1
monthcycle=1
end
end
end
else
end
end
复制代码
作者:
tamashii
时间:
2010-12-22 00:31
基本都是和字符有关系的,估计是乱码造成的
作者:
苏小脉
时间:
2010-12-22 02:26
29行:
system("md #{fullpath})
复制代码
看见错误冇?
作者:
px.凤翔九天
时间:
2010-12-22 22:50
回复
苏小脉
的帖子
话说不是这个问题...出错的行不是这个....话说这个用法貌似诡异,实际是deathking给我的方法
原地址:
http://rpg.blue/thread-161374-1-1.html
里面还有deathking给出的解说。貌似没问题。
应该是其他问题....希望你继续看....貌似不是冲突问题。因为只要把我那个脚本去掉,一切功能相当完善。祈求继续研究....貌似逻辑没错啊...报错说的是syntax error(语法错误。。)但是完全没找到语法错误。(觉得错的话请先看一下上面我给出的复制 删除目录的脚本)
我写的这个功能是这样的。找到photo文件夹中的所有文件(包括子目录中的文件),如果文件为txt(后缀判定),那么打开txt,判断里面的信息(格式应为“2009-5”这类,前面为年,后面为月)然后在桌面创建文件夹,在其中按txt中的月份创建对应名的文件夹,并把含有那个对应txt的文件夹复制到创建的文件夹中....貌似我说的不是很明白...你看看能不能改良到不报错版或换一种写法来搞定相同的问题。谢谢。
作者:
DeathKing
时间:
2010-12-22 23:00
本帖最后由 DeathKing 于 2010-12-22 23:00 编辑
试试先修正29行的错误再运行一下。
作者:
px.凤翔九天
时间:
2010-12-22 23:06
本帖最后由 px.凤翔九天 于 2010-12-22 23:07 编辑
回复
DeathKing
的帖子
呃呃呃,居然神奇的好使了!!!难道这么纠结的代码居然死在了我的一个小小的疏忽....郁闷....细节决定成败啊...谢谢了。果然好使了。(但是明明报错是不是这行啊....)
(话说你们眼睛都好尖....我怎么就是挑不到啊...好不容易记住数end了...这次搞的忘了数",一定改....)
作者:
DeathKing
时间:
2010-12-22 23:08
回复
px.凤翔九天
的帖子
控件的语法高亮很好用……
仔细看看,缺那么一个单引号,解释器的分析和编辑器的着色方案就不一样了。
作者:
苏小脉
时间:
2010-12-22 23:16
回复
px.凤翔九天
的帖子
不用看你具体的代码,看到 29 行就足够了。29 行有一个双引号,这对于 Ruby 解析器来说是一个字符串字元的开始,直到下一个双引号出现而结束。由于 29 行的双引号只有一个(实际上是你少打了一个),所以 31 行的第一个双引号就成了字符串字元的结尾,而后面的 C:/Documents and Settings/ 自然就被当做字符串以外的字元来处理了。Ruby 解析器在接收一个字符串字元后,下一个可接收的字元有不少,如空格,换行,调用 String 的方法等,但绝对没有直接跟着一个常量 C 的语法,所以这里就有了语法错误。
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1