mirror of
https://github.com/infinitefusion/infinitefusion-e18.git
synced 2025-12-06 06:01:46 +00:00
Tidied up, removed unused code
This commit is contained in:
@@ -48,41 +48,6 @@ def pbEachCombination(array,num)
|
||||
end while _pbNextComb(currentComb,array.length)
|
||||
end
|
||||
|
||||
def pbGetCDID()
|
||||
sendString = proc { |x|
|
||||
mciSendString = Win32API.new('winmm','mciSendString','%w(p,p,l,l)','l')
|
||||
next "" if !mciSendString
|
||||
buffer = "\0"*2000
|
||||
x = mciSendString.call(x,buffer,2000,0)
|
||||
next (x==0) ? buffer.gsub(/\0/,"") : ""
|
||||
}
|
||||
sendString.call("open cdaudio shareable")
|
||||
ret = ""
|
||||
if sendString.call("status cdaudio media present")=="true"
|
||||
ret = sendString.call("info cdaudio identity")
|
||||
if ret==""
|
||||
ret = sendString.call("info cdaudio info identity")
|
||||
end
|
||||
end
|
||||
sendString.call("close cdaudio")
|
||||
return ret
|
||||
end
|
||||
|
||||
# Gets the path of the user's "My Documents" folder.
|
||||
def pbGetMyDocumentsFolder()
|
||||
csidl_personal = 0x0005
|
||||
shGetSpecialFolderLocation = Win32API.new("shell32.dll","SHGetSpecialFolderLocation","llp","i")
|
||||
shGetPathFromIDList = Win32API.new("shell32.dll","SHGetPathFromIDList","lp","i")
|
||||
return "." if !shGetSpecialFolderLocation || !shGetPathFromIDList
|
||||
idl = [0].pack("V")
|
||||
ret = shGetSpecialFolderLocation.call(0,csidl_personal,idl)
|
||||
return "." if ret!=0
|
||||
path = "\0"*512
|
||||
ret = shGetPathFromIDList.call(idl.unpack("V")[0],path)
|
||||
return "." if ret==0
|
||||
return path.gsub(/\0/,"")
|
||||
end
|
||||
|
||||
# Returns a country ID
|
||||
# http://msdn.microsoft.com/en-us/library/dd374073%28VS.85%29.aspx?
|
||||
def pbGetCountry()
|
||||
@@ -181,359 +146,6 @@ end
|
||||
|
||||
|
||||
|
||||
#===============================================================================
|
||||
# Linear congruential random number generator
|
||||
#===============================================================================
|
||||
class LinearCongRandom
|
||||
def initialize(mul, add, seed=nil)
|
||||
@s1 = mul
|
||||
@s2 = add
|
||||
@seed = seed
|
||||
@seed = (Time.now.to_i&0xffffffff) if !@seed
|
||||
@seed = (@seed+0xFFFFFFFF)+1 if @seed<0
|
||||
end
|
||||
|
||||
def self.dsSeed
|
||||
t = Time.now
|
||||
seed = (((t.mon*t.mday+t.min+t.sec)&0xFF)<<24) | (t.hour << 16) | (t.year-2000)
|
||||
seed = (seed+0xFFFFFFFF)+1 if seed<0
|
||||
return seed
|
||||
end
|
||||
|
||||
def self.pokemonRNG
|
||||
self.new(0x41c64e6d,0x6073,self.dsSeed)
|
||||
end
|
||||
|
||||
def self.pokemonRNGInverse
|
||||
self.new(0xeeb9eb65,0xa3561a1,self.dsSeed)
|
||||
end
|
||||
|
||||
def self.pokemonARNG
|
||||
self.new(0x6C078965,0x01,self.dsSeed)
|
||||
end
|
||||
|
||||
def getNext16 # calculates @seed * @s1 + @s2
|
||||
@seed = ((((@seed&0x0000ffff)*(@s1&0x0000ffff))&0x0000ffff) |
|
||||
(((((((@seed&0x0000ffff)*(@s1&0x0000ffff))&0xffff0000)>>16) +
|
||||
((((@seed&0xffff0000)>>16)*(@s1&0x0000ffff))&0x0000ffff) +
|
||||
(((@seed&0x0000ffff)*((@s1&0xffff0000)>>16))&0x0000ffff)) &
|
||||
0x0000ffff)<<16)) + @s2
|
||||
r = (@seed>>16)
|
||||
r = (r+0xFFFFFFFF)+1 if r<0
|
||||
return r
|
||||
end
|
||||
|
||||
def getNext
|
||||
r = (getNext16()<<16) | (getNext16())
|
||||
r = (r+0xFFFFFFFF)+1 if r<0
|
||||
return r
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
#===============================================================================
|
||||
# Json-related utilities
|
||||
#===============================================================================
|
||||
# Returns true if the given string represents a valid object in JavaScript
|
||||
# Object Notation, and false otherwise.
|
||||
def pbIsJsonString(str)
|
||||
return false if !str || str[/^[\s]*$/]
|
||||
d = /(?:^|:|,)(?: ?\[)+/
|
||||
charEscapes = /\\[\"\\\/nrtubf]/ #"
|
||||
stringLiterals = /"[^"\\\n\r\x00-\x1f\x7f-\x9f]*"/ #"
|
||||
whiteSpace = /[\s]+/
|
||||
str = str.gsub(charEscapes,"@").gsub(stringLiterals,"true").gsub(whiteSpace," ")
|
||||
# prevent cases like "truetrue" or "true true" or "true[true]" or "5-2" or "5true"
|
||||
otherLiterals = /(true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)(?! ?[0-9a-z\-\[\{\"])/ #"
|
||||
str = str.gsub(otherLiterals,"]").gsub(d,"") #"
|
||||
return str[/^[\],:{} ]*$/] ? true : false
|
||||
end
|
||||
|
||||
# Returns a Ruby object that corresponds to the given string, which is encoded in
|
||||
# JavaScript Object Notation (JSON). Returns nil if the string is not valid JSON.
|
||||
def pbParseJson(str)
|
||||
return nil if !pbIsJsonString(str)
|
||||
stringRE = /(\"(\\[\"\'\\rntbf]|\\u[0-9A-Fa-f]{4,4}|[^\\\"])*\")/ #"
|
||||
strings = []
|
||||
str = str.gsub(stringRE) {
|
||||
sl = strings.length
|
||||
ss = $1
|
||||
if ss.include?("\\u")
|
||||
ss.gsub!(/\\u([0-9A-Fa-f]{4,4})/) {
|
||||
codepoint = $1.to_i(16)
|
||||
if codepoint<=0x7F
|
||||
next sprintf("\\x%02X",codepoint)
|
||||
elsif codepoint<=0x7FF
|
||||
next sprintf("%s%s",
|
||||
(0xC0|((codepoint>>6)&0x1F)).chr,
|
||||
(0x80|(codepoint&0x3F)).chr)
|
||||
else
|
||||
next sprintf("%s%s%s",
|
||||
(0xE0|((codepoint>>12)&0x0F)).chr,
|
||||
(0x80|((codepoint>>6)&0x3F)).chr,
|
||||
(0x80|(codepoint&0x3F)).chr)
|
||||
end
|
||||
}
|
||||
end
|
||||
strings.push(eval(ss))
|
||||
next sprintf("strings[%d]",sl)
|
||||
}
|
||||
str = str.gsub(/\:/,"=>")
|
||||
str = str.gsub(/null/,"nil")
|
||||
return eval("("+str+")")
|
||||
end
|
||||
|
||||
|
||||
|
||||
#===============================================================================
|
||||
# XML-related utilities
|
||||
#===============================================================================
|
||||
# Represents XML content.
|
||||
class MiniXmlContent
|
||||
attr_reader :value
|
||||
|
||||
def initialize(value)
|
||||
@value = value
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
# Represents an XML element.
|
||||
class MiniXmlElement
|
||||
attr_accessor :name,:attributes,:children
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@attributes = {}
|
||||
@children = []
|
||||
end
|
||||
|
||||
# Gets the value of the attribute with the given name, or nil if it doesn't
|
||||
# exist.
|
||||
def a(name)
|
||||
self.attributes[name]
|
||||
end
|
||||
|
||||
# Gets the entire text of this element.
|
||||
def value
|
||||
ret = ""
|
||||
for c in @children
|
||||
ret += c.value
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
# Gets the first child of this element with the given name, or nil if it
|
||||
# doesn't exist.
|
||||
def e(name)
|
||||
for c in @children
|
||||
return c if c.is_a?(MiniXmlElement) && c.name==name
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
def eachElementNamed(name)
|
||||
for c in @children
|
||||
yield c if c.is_a?(MiniXmlElement) && c.name==name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
# A small class for reading simple XML documents. Such documents must
|
||||
# meet the following restrictions:
|
||||
# They may contain comments and processing instructions, but they are
|
||||
# ignored.
|
||||
# They can't contain any entity references other than 'gt', 'lt',
|
||||
# 'amp', 'apos', or 'quot'.
|
||||
# They can't contain a DOCTYPE declaration or DTDs.
|
||||
class MiniXmlReader
|
||||
def initialize(data)
|
||||
@root = nil
|
||||
@elements = []
|
||||
@done = false
|
||||
@data = data
|
||||
@content = ""
|
||||
end
|
||||
|
||||
def createUtf8(codepoint) #:nodoc:
|
||||
raise ArgumentError.new("Illegal character") if codepoint<9 ||
|
||||
codepoint==11 || codepoint==12 || (codepoint>=14 && codepoint<32) ||
|
||||
codepoint==0xFFFE || codepoint==0xFFFF || (codepoint>=0xD800 && codepoint<0xE000)
|
||||
return codepoint.chr if codepoint<=0x7F
|
||||
if codepoint<=0x7FF
|
||||
str = (0xC0|((codepoint>>6)&0x1F)).chr
|
||||
str += (0x80|(codepoint &0x3F)).chr
|
||||
return str
|
||||
elsif codepoint<=0xFFFF
|
||||
str = (0xE0|((codepoint>>12)&0x0F)).chr
|
||||
str += (0x80|((codepoint>>6)&0x3F)).chr
|
||||
str += (0x80|(codepoint &0x3F)).chr
|
||||
return str
|
||||
elsif codepoint<=0x10FFFF
|
||||
str = (0xF0|((codepoint>>18)&0x07)).chr
|
||||
str += (0x80|((codepoint>>12)&0x3F)).chr
|
||||
str += (0x80|((codepoint>>6)&0x3F)).chr
|
||||
str += (0x80|(codepoint &0x3F)).chr
|
||||
return str
|
||||
else
|
||||
raise ArgumentError.new("Illegal character")
|
||||
end
|
||||
end
|
||||
|
||||
def unescape(attr) #:nodoc:
|
||||
attr = attr.gsub(/\r(\n|$|(?=[^\n]))/,"\n")
|
||||
raise ArgumentError.new("Attribute value contains '<'") if attr.include?("<")
|
||||
attr = attr.gsub(/&(lt|gt|apos|quot|amp|\#([0-9]+)|\#x([0-9a-fA-F]+));|([\n\r\t])/) {
|
||||
next " " if $4=="\n"||$4=="\r"||$4=="\t"
|
||||
next "<" if $1=="lt"
|
||||
next ">" if $1=="gt"
|
||||
next "'" if $1=="apos"
|
||||
next "\"" if $1=="quot"
|
||||
next "&" if $1=="amp"
|
||||
next createUtf8($2.to_i) if $2
|
||||
next createUtf8($3.to_i(16)) if $3
|
||||
}
|
||||
return attr
|
||||
end
|
||||
|
||||
def readAttributes(attribs) #:nodoc:
|
||||
ret = {}
|
||||
while attribs.length>0
|
||||
if attribs[/(\s+([\w\-]+)\s*\=\s*\"([^\"]*)\")/]
|
||||
attribs = attribs[$1.length,attribs.length]
|
||||
name = $2; value = $3
|
||||
raise ArgumentError.new("Attribute already exists") if ret[name]!=nil
|
||||
ret[name] = unescape(value)
|
||||
elsif attribs[/(\s+([\w\-]+)\s*\=\s*\'([^\']*)\')/]
|
||||
attribs = attribs[$1.length,attribs.length]
|
||||
name = $2; value = $3
|
||||
raise ArgumentError.new("Attribute already exists") if ret[name]!=nil
|
||||
ret[name] = unescape(value)
|
||||
else
|
||||
raise ArgumentError.new("Can't parse attributes")
|
||||
end
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
# Reads the entire contents of an XML document. Returns the root element of
|
||||
# the document or raises an ArgumentError if an error occurs.
|
||||
def read
|
||||
if @data[/\A((\xef\xbb\xbf)?<\?xml\s+version\s*=\s*(\"1\.[0-9]\"|\'1\.[0-9]\')(\s+encoding\s*=\s*(\"[^\"]*\"|\'[^\']*\'))?(\s+standalone\s*=\s*(\"(yes|no)\"|\'(yes|no)\'))?\s*\?>)/]
|
||||
# Ignore XML declaration
|
||||
@data = @data[$1.length,@data.length]
|
||||
end
|
||||
while readOneElement(); end
|
||||
return @root
|
||||
end
|
||||
|
||||
def readOneElement #:nodoc:
|
||||
if @data[/\A\s*\z/]
|
||||
@data = ""
|
||||
if !@root
|
||||
raise ArgumentError.new("Not an XML document.")
|
||||
elsif !@done
|
||||
raise ArgumentError.new("Unexpected end of document.")
|
||||
end
|
||||
return false
|
||||
end
|
||||
if @data[/\A(\s*<([\w\-]+)((?:\s+[\w\-]+\s*\=\s*(?:\"[^\"]*\"|\'[^\']*\'))*)\s*(\/>|>))/]
|
||||
@data = @data[$1.length,@data.length]
|
||||
elementName = $2
|
||||
attributes = $3
|
||||
endtag = $4
|
||||
raise ArgumentError.new("Element tag at end of document") if @done
|
||||
if @content.length>0 && @elements.length>0
|
||||
@elements[@elements.length-1].children.push(MiniXmlContent.new(@content))
|
||||
@content = ""
|
||||
end
|
||||
element = MiniXmlElement.new(elementName)
|
||||
element.attributes = readAttributes(attributes)
|
||||
if !@root
|
||||
@root = element
|
||||
else
|
||||
@elements[@elements.length-1].children.push(element)
|
||||
end
|
||||
if endtag==">"
|
||||
@elements.push(element)
|
||||
else
|
||||
@done = true if @elements.length==0
|
||||
end
|
||||
elsif @data[/\A(<!--([\s\S]*?)-->)/]
|
||||
# ignore comments
|
||||
raise ArgumentError.new("Incorrect comment") if $2.include?("--")
|
||||
@data = @data[$1.length,@data.length]
|
||||
elsif @data[/\A(<\?([\w\-]+)\s+[\s\S]*?\?>)/]
|
||||
# ignore processing instructions
|
||||
@data = @data[$1.length,@data.length]
|
||||
if $2.downcase=="xml"
|
||||
raise ArgumentError.new("'xml' processing instruction not allowed")
|
||||
end
|
||||
elsif @data[/\A(<\?([\w\-]+)\?>)/]
|
||||
# ignore processing instructions
|
||||
@data = @data[$1.length,@data.length]
|
||||
if $2.downcase=="xml"
|
||||
raise ArgumentError.new("'xml' processing instruction not allowed")
|
||||
end
|
||||
elsif @data[/\A(\s*<\/([\w\-]+)>)/]
|
||||
@data = @data[$1.length,@data.length]
|
||||
elementName = $2
|
||||
raise ArgumentError.new("End tag at end of document") if @done
|
||||
if @elements.length==0
|
||||
raise ArgumentError.new("Unexpected end tag")
|
||||
elsif @elements[@elements.length-1].name!=elementName
|
||||
raise ArgumentError.new("Incorrect end tag")
|
||||
else
|
||||
if @content.length>0
|
||||
@elements[@elements.length-1].children.push(MiniXmlContent.new(@content))
|
||||
@content = ""
|
||||
end
|
||||
@elements.pop()
|
||||
@done = true if @elements.length==0
|
||||
end
|
||||
else
|
||||
if @elements.length>0
|
||||
# Parse content
|
||||
if @data[/\A([^<&]+)/]
|
||||
content = $1
|
||||
@data = @data[content.length,@data.length]
|
||||
raise ArgumentError.new("Incorrect content") if content.include?("]]>")
|
||||
content.gsub!(/\r(\n|\z|(?=[^\n]))/,"\n")
|
||||
@content += content
|
||||
elsif @data[/\A(<\!\[CDATA\[([\s\S]*?)\]\]>)/]
|
||||
content = $2
|
||||
@data = @data[$1.length,@data.length]
|
||||
content.gsub!(/\r(\n|\z|(?=[^\n]))/,"\n")
|
||||
@content += content
|
||||
elsif @data[/\A(&(lt|gt|apos|quot|amp|\#([0-9]+)|\#x([0-9a-fA-F]+));)/]
|
||||
@data = @data[$1.length,@data.length]
|
||||
content = ""
|
||||
if $2=="lt"; content = "<"
|
||||
elsif $2=="gt"; content = ">"
|
||||
elsif $2=="apos"; content = "'"
|
||||
elsif $2=="quot"; content = "\""
|
||||
elsif $2=="amp"; content = "&"
|
||||
elsif $3; content = createUtf8($2.to_i)
|
||||
elsif $4; content = createUtf8($3.to_i(16))
|
||||
end
|
||||
@content += content
|
||||
elsif !@data[/\A</]
|
||||
raise ArgumentError.new("Can't read XML content")
|
||||
end
|
||||
else
|
||||
raise ArgumentError.new("Can't parse XML")
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
#===============================================================================
|
||||
# Event utilities
|
||||
#===============================================================================
|
||||
|
||||
Reference in New Issue
Block a user