Thread: [Essentials Script] My Script Library
View Single Post
  #1    
Old October 3rd, 2012 (10:13 AM). Edited October 3rd, 2012 by ☆Rei☆.
☆Rei☆'s Avatar
☆Rei☆ ☆Rei☆ is offline
 
Join Date: Aug 2009
Location: Nearby my feet.
Age: 21
Gender: Female
Nature: Calm
Posts: 1,747
Intro:

As it is soon my birthday, I shall share some scripts (and programs) I have made for anyone to use, I will slowly upload them as I get the chance.


Current Scripts/Programs:

World Map Generator (program): Generates realistic looking world maps in seconds!
Class To String (rgss script): Convert any class to a string (also supports compression)
LoadFileToString (rgss script): Ummm this can load .png files (actually any file type) into a string in rmxp.
DLC script (rgss script): only 4 lines of code, this can download any file to a proper file (as long as you have the exact link of it and it's not too big)
Json reader (rgss script): Creates and parses Json data

Scripts:

Code:
################################################################################
# Class To String and String To Class
# By Hansiec
# Safely Converts a class to a compressed string and vise-versa
# If it failed it returns "Failed to convert" and also popups up a message.
# Uses:
# Fast compression (and storage) of classes (no need to input any data)
# Fast decompression of classes (never fails to load properly!)
# This could be used in some systems which requires to send sensitive data (or
# otherwise difficult to handle data) Example: Pokemon, Trainers, Ect.
################################################################################

def class_to_string(class_var) 
  save_data(class_var,"Temp_File")
  data = ""
  file = File.open("Temp_File", 'rb')
  data += file.read() while !file.eof?
  file.close
  File.delete("Temp_File")
  return compress(data)
rescue
  print "Failed to convert #{class_var}"
  return "Failed to convert #{class_var}"
end

def string_to_class(string)
  file = File.open("Temp_File", 'wb')
  file.write(decompress(string))
  file.close
  ret = load_data("Temp_File")
  File.delete("Temp_File")
  return ret
rescue
  print "Failed to convert #{string}"
  return "Failed to convert #{string}"
end


def compress(string)
  z = Zlib::Deflate.new(2)
  dst = z.deflate(string, Zlib::FINISH)
  z.close
  dst
end

def decompress(string)
  zstream = Zlib::Inflate.new
  buf = zstream.inflate(string)
  zstream.finish
  zstream.close
  buf
end

################################################################################
# LoadFileToString
# By Hansiec
# Uses:
# Can load and save files from/to your computer and also allows you to compress
# or decompress
################################################################################

def loadFile(file,compress_d=0)
  return if file == ""
  data = ""
  file = File.open(file, 'rb')
  data += file.read() while !file.eof?
  file.close
  data = compress(data) if compress_d == 1
  data = decompress(data) if compress_d == 2
  return data
end

def saveFile(file,data,compress_d=0)
  return if file == "" || data == ""
  data = compress(data) if compress_d == 1
  data = decompress(data) if compress_d == 2
  file = File.open(file, 'wb')
  file.write(data)
  file.close
end

def compress(string)
  z = Zlib::Deflate.new(2)
  dst = z.deflate(string, Zlib::FINISH)
  z.close
  dst
end

def decompress(string)
  zstream = Zlib::Inflate.new
  buf = zstream.inflate(string)
  zstream.finish
  zstream.close
  buf
end

################################################################################
# Downloaded Content Script
# By Hansiec
# Uses:
# Not really any since this is actually a 4 lined script which almost anyone can
# make. You cannot yet download HUGE files as of the hangup will uccor.
################################################################################


def pbDownloadContent(url, filename="")
  filename = url.split("/")[url.split("/").length-1] if filename == ""
  pbDownloadData(url,filename)
end

################################################################################
# Json Reader/Writer
# By Hansiec
# Uses:
# Converts data to the Json format and loads data from the Json format
# Input any normal data type (String,Integer,Array,Hash, or Boolean) and my
# System converts it into Json!
################################################################################


def toJson(data,name="")
  ret = ""
  ret = name+"=" if name != ""
  if data.is_a?(String)
    ret+="\""+data+"\""
  elsif data.is_a?(Numeric)
    ret+=data.to_s
  elsif data.is_a?(Array)
    ret+="["
    data2=""
    for i in 0..data.length-1
      if data2 == ""
        data2=toJson(data[i])
      else
        data2+=","+toJson(data[i])
      end
    end
    ret+=data2+"]"
  elsif data.is_a?(Hash)
    ret+="{"
    data2=""
    for i in data.keys
      if data2 == ""
        data2='"'+i+'"'+"=>"+toJson(data[i])
      else
        data2+=","+'"'+i+'"'+"=>"+toJson(data[i])
      end
    end
    ret+=data2+"}"
  elsif data == false
    ret+="false"
  elsif data == true
    ret+="true"
  elsif data == nil
    ret+="nil"
  else
    return 'Cannot convert!'
  end
  return ret
end

def fromJson(data)
  return eval(data)
end

Programs:

World Map Generator: https://rapidshare.com/files/4008392752/World%20Map%20Generator.zip

More scripts and programs to come soon!

Credits:
Hansiec -- Creator of all scripts
Game Maker -- Program used to create the World Map Generator
Reply With Quote