设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 33025|回复: 82
打印 上一主题 下一主题

[转载] RGSS 连接到MySQL数据库(网游爱好者的福音)

  [复制链接]

Lv1.梦旅人

梦石
0
星屑
200
在线时间
10 小时
注册时间
2009-7-25
帖子
656
跳转到指定楼层
1
发表于 2010-9-17 11:11:46 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

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

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

x
本帖最后由 传说VS天涯 于 2010-11-1 15:53 编辑

RGSS 连接到MySQL数据库,,有图有真相在查找MySql资料时无意看到,,转过来. 翻译~



Mysql support for Rgss
Version: 1.0
By: Berka
有任何问题直接回复~3Q
RGSSMySQL.rar (121.55 KB, 下载次数: 2791)


MySQL 下载地址:
Mysql
SQLyog

  1. #======================================================================
  2. #                                                                   Net::Mysql
  3. #      29-05-2010                            www.rpgmakervx-fr.com                                  Rgss1&2  v.1
  4. #                                                                    par berka                 
  5. #--------------------------------------------------------------------------------------------------------------
  6. # 此脚本是免费使用。可以随意转载,注明出处!!
  7. #--------------------------------------------------------------------------------------------------------------
  8. # 警告 : 如果您的游戏破解和解密, 你的MySQL将会被登录!
  9. # 请勿使用包含个人信息数据库。(最好对连接字符串进行加密,举例MD5,)
  10. # 你的MySQL主机应该接受外部连接,,
  11. # 它检查远程SSH访问你的数据库。
  12. #--------------------------------------------------------------------------------------------------------------
  13. # 这个脚本可以直接连接到MySQL数据库,,需要 "libmySQL.dll" 文件的支持!!
  14. #--------------------------------------------------------------------------------------------------------------
  15. # 注意: 如果你对游戏加密了,依然可以访问到MySql数据库。
  16. # 他是直接通过远程SSH访问你的数据库= =,,这点大家注意
  17. #--------------------------------------------------------------------------------------------------------------------------
  18. # 必须存在"libmySQL.dll"否则无法连接到MySql数据库。
  19. #--------------------------------------------------------------------------------------------------------------------------
  20. #  — md5() support
  21. #  — Mysql 方法(函数):
  22. #   - Net::Mysql.new([host,user,pass,base,port]) : return : mysql 连接句柄
  23. #   - @mysql.close : return : bool
  24. #   - @mysql.list_tables([filter]) : return : ["table1", "table2"]
  25. #   - @mysql.select_db(base_name) : return : true if the db exists or false
  26. #   - @mysql.query("query",ret=false) : return : if ret = true : rows else result handle
  27. #   - @mysql.get_fields([handle result]) : return : ["field1", "field2"]
  28. #   - @mysql.get_rows([handle result]) : return : [["l1 row1", "l1 row2"], ["l2 row1", "l2 row2"]]
  29. #   - @mysql.fetch_assoc : return : {"field" => ["row1", "row2"] }
  30. #   - @mysql.num_rows([handle result]) : return : integer
  31. #  — Html 方法(函数):
  32. #   - "string".to_ruby : return : true, false, nil, Integer, Float, self, etc.
  33. #   - "<berka>".htmlspecialchars : return : "&lr;berka&gt;"
  34. #   - "<berka>".urlencode : return : "%3Cberka%3E"
  35. #   - "%3Cberka%3E".urldecode : return : "<berka>"
  36. #--------------------------------------------------------------------------------------------------------------------------
  37. # SQL 查询示例:
  38. #  —  "SELECT * FROM table"
  39. #  —  "INSERT INTO table (fields) VALUES (values)"
  40. #  —  "INSERT INTO table SET field = value WHERE field = value"
  41. #  —  "UPDATE table SET field = value WHERE field = value"
  42. #--------------------------------------------------------------------------------------------------------------------------
  43. # 示例 :
  44. # @mysql = Net::Mysql.new
  45. # @mysql.query("SELECT * FROM `members`)
  46. # res = @mysql.fetch_assoc
  47. # => {:id=>["1","2"], :nom=>["berka","rgss"], :age=>["19",""]}
  48. #======================================================================

  49. module Berka
  50.   module Mysql
  51.     #mysql 服务器(local(本地) : 127.0.0.1)
  52.     Host   = "127.0.0.1"
  53.         #mysql 用户名
  54.     User   = ""
  55.         #mysql 密码
  56.     Pass  = ""   
  57.         #数据库名字(base name)
  58.     Base  = "rgss"
  59.         #服务器端口(默认:3306)
  60.     Port    = 3306                                  # server port (default: 3306)
  61.    
  62.     Err_Con = "Mysql:\n无法连接到数据库"
  63.     Err_Req = "Mysql:\n无法发送查询"
  64.   end
  65.   
  66.   module Html
  67.     Spec_Char=["$","&","+",",","/",";",":","=","@","?"," ","<",">","#","%","{","}","|","\\","^","~","[","]","`"]
  68.   end
  69. end

  70. class Numeric
  71.   def copymem(len)
  72.     # 移动存储转换到C结构Ruby对象
  73.     Win32API.new("kernel32", "RtlMoveMemory", "ppl", "").call(buf="\0"*len,self,len);buf
  74.   end
  75. end

  76. class String
  77.   
  78.   def to_ruby
  79.     # 检测如果该字符串是一个MD5 hash
  80.     return self if self=~/^[a-f0-9]{32}$/
  81.     #转换成一个Ruby controls字符串
  82.     eval(self)rescue self
  83.   end
  84.   
  85.   def htmlspecialchars
  86.     # converts special chars to html compatibles chars (ASCII)
  87.     {"&"=>"&amp;",'"'=>"&quot;","'"=>"&rsquo;","<"=>"&lr;",">"=>"&gt;"}.each_pair{|k,v|self.gsub!(k,v)}
  88.     self
  89.   end
  90.    
  91.   def urlencode
  92.     # 特殊字符的URL转换
  93.     o="";self.scan(/./).each{|c|c="%"+c.unpack('H*')[0]if Berka::Html::Spec_Char.include?(c);o<<c};o
  94.   end
  95.    
  96.   def urldecode
  97.     # converts encoded special char of url to normal chars
  98.     self.gsub!(/\%(\w\w)/){|c|c.gsub!("%","").hex.chr}
  99.   end
  100. end
  101.   
  102. module Net
  103.   class Mysql
  104.     MI=Win32API.new("libmysql.dll","mysql_init","l","l")
  105.     MC=Win32API.new("libmysql.dll","mysql_close","l","l")
  106.     MQ=Win32API.new("libmysql.dll","mysql_query","lp","l")
  107.     MLT=Win32API.new("libmysql.dll","mysql_list_tables","lp","l")
  108.     MFL=Win32API.new("libmysql.dll","mysql_fetch_lengths","p","l")
  109.     MFR=Win32API.new("libmysql.dll","mysql_fetch_row","p","l")
  110.     MNF=Win32API.new("libmysql.dll","mysql_num_fields","p","l")
  111.     MFC=Win32API.new("libmysql.dll","mysql_field_count","p","l")
  112.     MSR=Win32API.new("libmysql.dll","mysql_store_result","l","l")
  113.     MRC=Win32API.new("libmysql.dll","mysql_real_connect","lpppplpl","l")
  114.     MNR=Win32API.new("libmysql.dll","mysql_num_rows","p","l")
  115.     MFFD=Win32API.new("libmysql.dll","mysql_fetch_field_direct","pi","l")
  116.     MFRE=Win32API.new("libmysql.dll","mysql_free_result","p","l")
  117.     MSDB=Win32API.new("libmysql.dll","mysql_select_db","p","l")
  118.    
  119.     attr_reader :handle
  120.    
  121.     def initialize(h=Berka::Mysql::Host,u=Berka::Mysql::User,p=Berka::Mysql::Pass,b=Berka::Mysql::Base,po=Berka::Mysql::Port)
  122.       # @handle : 处理MySQL的初始化
  123.       @handle=MI.call(0)
  124.       # 建立MySQL连接
  125.       (print(Berka::Mysql::Err_Con))if MRC.call(@handle,h,u,p,b,po,nil,0)==0
  126.       # returns: handle
  127.       @handle
  128.     end
  129.    
  130.     def close
  131.       # 关闭当前连接
  132.       MC.call(@handle)
  133.     end
  134.    
  135.     def select_db(base)
  136.       # 选择一个数据库
  137.       MSDB.call(base)==true
  138.     end
  139.    
  140.     def list_tables(m="")
  141.       # lists tables request -> fetch the result -> to ruby string
  142.       l=MFR.call(MLT.call(@my,m)).copymem(1024)
  143.       # splits the string to array -> list of tables
  144.       l.scan(/\t(\w+)\0/).flatten
  145.     end
  146.    
  147.     def query(req,ret=false)
  148.       # sends the query (msg error)
  149.       (return print(Berka::Mysql::Err_Req+req))if !MQ.call(@handle,req)
  150.       # previous results are released
  151.       MFRE.call(@result)if @result
  152.       # gets the results from the query -> c struct handle
  153.       @result=MSR.call(@handle)
  154.       ret ? get_rows(@result) : @result
  155.     end
  156.    
  157.     # Proc: gets the name of the field (cstruct) -> to ruby string of handles -> to ruby string
  158.     # returns the fieldname or nil if the field is not found.
  159.     ReadField=Proc.new{|r,i,of|MFFD.call(r,i).copymem(1024).unpack("iissi")[0].copymem(of).delete!("\0")}
  160.    
  161.     def get_fields(res=nil)
  162.       # if a result handle is provided
  163.       r=res.nil? ? @result : res
  164.       # gets the number of fields, offset: 8bytes-2 (cf. loop)
  165.       nf,ch,of=MFC.call(@handle),[],6
  166.       # each field: if the fieldname is not found: increase the offset of bytes.
  167.       nf.times{|i|a=ReadField.call(r,i,of+=2)until a
  168.         # add to the fields array
  169.         ch<<a
  170.         # reinitialize the offset for the next iteration
  171.         of=6}
  172.       # returns an array of fields
  173.       ch
  174.     end
  175.    
  176.     def get_rows(res=nil)
  177.       # if a result handle is provided
  178.       r=res.nil? ? @result : res
  179.       # nr: number of rows, nf: number of fields
  180.       nr,nf,en=MNR.call(r),MNF.call(r),[]
  181.       # each row:
  182.       nr.times{|i|
  183.        # gets each row: c struct -> to ruby string -> to array (handles)
  184.        c=MFR.call(r).copymem(4).unpack("i")[0]
  185.        # gets each field length: c struct -> to ruby string -> to array (handles)
  186.        tf=MFL.call(r).copymem(4*nf).unpack("i*")
  187.        # size of field: offset of each field
  188.        sf=tf.inject(0){|n,i|n+i}
  189.        # handle of row -> to string (offset) -> to array
  190.        en<<c.copymem(sf+nf).split("\0")
  191.        }
  192.        # returns each row as an array
  193.       en
  194.     end
  195.    
  196.     def num_rows(res=nil)
  197.       # if a result handle is provided
  198.       r=res.nil? ? @result : res
  199.       # returns: number of rows
  200.       MNR.call(r)
  201.     end
  202.    
  203.     def fetch_assoc(to_ruby=false)
  204.       # gets rows and fields
  205.       h,f,r={},get_fields,get_rows
  206.       # each field: read the rows and store them to an hash : h[:field]=[rows]
  207.       # rows are converted to ruby objects if to_ruby == true
  208.       f.each_with_index{|fi,i|t=[];r.each{|l|t<<l[i]};h[fi.to_sym]=(to_ruby ? t.map!{|o|o.to_ruby if o} : t)}
  209.       h
  210.     end
  211.   end
  212. end
复制代码

评分

参与人数 3星屑 +59 收起 理由
kvkv97 + 5 精品文章
gqhondafit + 14 塞糖
无双sxa + 40 感谢分享。

查看全部评分

Lv1.梦旅人

尽头

梦石
0
星屑
119
在线时间
278 小时
注册时间
2010-6-20
帖子
1280
2
发表于 2010-9-17 12:17:14 | 只看该作者
很好很强大.......RM中能用帐号登录了..
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
110
在线时间
153 小时
注册时间
2008-5-25
帖子
585
3
发表于 2010-9-17 12:23:56 | 只看该作者
kankan
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
1040
在线时间
1564 小时
注册时间
2008-7-30
帖子
4418

贵宾

4
发表于 2010-9-17 12:50:38 | 只看该作者
恩,现在正在研究这方面的饿内容,是个很好的参考。

See FScript Here:https://github.com/DeathKing/fscript
潜心编写URG3中。
所有对URG3的疑问和勘误或者建议,请移步至发布页面。
欢迎萌妹纸催更
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
200
在线时间
10 小时
注册时间
2009-7-25
帖子
656
5
 楼主| 发表于 2010-9-17 13:03:39 | 只看该作者
:lol 很不错的东西,我自己也在完善ing,做一C/S程序来连起来= =。。主要是对C++这东西无能无力。
回复 支持 反对

使用道具 举报

Lv4.逐梦者

梦石
1
星屑
8908
在线时间
4368 小时
注册时间
2005-10-22
帖子
6744

开拓者贵宾

6
发表于 2010-9-17 19:13:52 | 只看该作者
很好很强大.......RM中能用帐号登录了..
bbaugle 发表于 2010-9-17 12:17


账号登陆的话只要调用IE的API就够实现了

点评

你忘记隐身了  发表于 2011-7-24 16:07
回复 支持 反对

使用道具 举报

头像被屏蔽

Lv1.梦旅人 (禁止发言)

梦石
0
星屑
50
在线时间
93 小时
注册时间
2009-10-16
帖子
235
7
发表于 2010-9-18 03:00:40 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
60
在线时间
61 小时
注册时间
2006-9-15
帖子
946
8
发表于 2010-9-18 08:47:19 | 只看该作者
回复能看到隐藏的东西
回复 支持 反对

使用道具 举报

Lv4.逐梦者

梦石
0
星屑
8570
在线时间
708 小时
注册时间
2010-7-22
帖子
647
9
发表于 2010-9-20 10:28:01 | 只看该作者
隐藏的是什么东东捏?
回复 支持 反对

使用道具 举报

头像被屏蔽

Lv1.梦旅人 (禁止发言)

梦石
0
星屑
50
在线时间
3 小时
注册时间
2010-8-20
帖子
51
10
发表于 2010-9-20 10:58:46 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽
回复 支持 反对

使用道具 举报

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

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-20 17:01

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表