Project1
标题:
超省位存档
[打印本页]
作者:
英顺的马甲
时间:
2011-4-25 16:07
标题:
超省位存档
本帖最后由 英顺的马甲 于 2011-4-25 16:37 编辑
首发地址:
http://www.our-story.tk/read.php?tid=64
用法与File相似
比如说将变量存进aaa.txt
#储存部分
f = FWrite.open("aaa.txt",true)
f.save(123)
f.save(456)
f.save(789)
f.close
# 读取部分
f = FWrite.open("aaa.txt",true)
p f.load,f.load,f.load # => 123,456,789
f.close
复制代码
脚本:
module FWrite
class Text
def initialize(file,b)
@file = file
@b = b
@pos_save = 0
@pos_load = 0
end
def save(obj)
defl = Zlib::Deflate.new
if @pos_save == 0
File.open(@file,"wb"){ |f|
f.write(defl.deflate(Marshal.dump([obj]),4))
}
@pos_save += 1 if @b
else
obj2 = nil
inf = Zlib::Inflate.new
File.open(@file,"rb"){|f|
obj2 = inf.inflate(f.read)
inf.finish
}
obj2 = Marshal.load(obj2)
obj2.push(obj)
File.open(@file,"wb"){ |f|
f.write(defl.deflate(Marshal.dump(obj2),4))
}
@pos_save += 1
end
end
def load
inf = Zlib::Inflate.new
if @pos_load == 0
obj = nil
File.open(@file,"rb"){|f|
obj = inf.inflate(f.read)
inf.finish
}
obj = Marshal.load(obj)
@pos_load += 1 if @b
return obj[0]
else
obj = nil
File.open(@file,"rb"){|f|
obj = inf.inflate(f.read)
inf.finish
}
obj = Marshal.load(obj)
@pos_load += 1
return obj[@pos_load - 1]
end
end
def close
@file = nil
@b = false
@pos_save = 0
@pos_load = 0
end
end
def self.open(file,b = false)
return Text.new(file,b)
end
end
复制代码
注意:
不支持FWrite.open("aaa.txt",true){|f|f.load}写法
存档脚本修改:
class Scene_Save < Scene_File
def write_save_data(file)
characters = []
for i in 0...$game_party.actors.size
actor = $game_party.actors[i]
characters.push([actor.character_name, actor.character_hue])
end
f = FRwrite.open(make_filename(@file_index),true)
f.save(characters)
f.save(Graphics.frame_count)
$game_system.save_count += 1
$game_system.magic_number = $data_system.magic_number
f.save($game_system)
f.save($game_switches)
f.save($game_variables)
f.save($game_self_switches)
f.save($game_screen)
f.save($game_actors)
f.save($game_party)
f.save($game_troop)
f.save($game_map)
f.save($game_player)
f.close
end
end
class Scene_Load < Scene_File
def read_save_data(file)
f = FRwrite.open(make_filename(@file_index),true)
Graphics.frame_count = f.load
$game_system = f.load
$game_switches = f.load
$game_variables = f.load
$game_self_switches = f.load
$game_screen = f.load
$game_actors = f.load
$game_party = f.load
$game_troop = f.load
$game_map = f.load
$game_player = f.load
f.close
if $game_system.magic_number != $data_system.magic_number
$game_map.setup($game_map.map_id)
$game_player.center($game_player.x, $game_player.y)
end
$game_party.refresh
end
end
class Window_SaveFile < Window_Base
def initialize(file_index, filename)
super(0, 64 + file_index % 4 * 104, 640, 104)
self.contents = Bitmap.new(width - 32, height - 32)
@file_index = file_index
@filename = "Save#{@file_index + 1}.rxdata"
@time_stamp = Time.at(0)
@file_exist = FileTest.exist?(@filename)
if @file_exist
file = FWrite.open(@filename, true)
@time_stamp = file.mtime
@characters = f.load(file)
@frame_count = f.load(file)
@game_system = f.load(file)
@game_switches = f.load(file)
@game_variables = f.load(file)
@total_sec = @frame_count / Graphics.frame_rate
file.close
end
refresh
@selected = false
end
end
复制代码
正努力将此脚本与
Base64字符串加密算法
结合加密,由于"/000"的关系无法结合,头疼当中。。。
作者:
summer92
时间:
2011-4-25 16:34
貌似不支持中文,出来是乱码
作者:
一箭烂YiJL
时间:
2011-4-25 17:26
看过了以后,说一下超省位的原理。
靠的是用Zlib库的压缩,而且我发现压缩程度是4级。
如果将Zlib的后面那个参数改为无的话,压缩量应该会提高。
作者:
yangff
时间:
2011-4-25 18:05
我相信StringIO可以解决LZ很多问题……这样写您不解的麻烦么= =算了当我没说……
作者:
苏小脉
时间:
2011-4-25 23:42
本帖最后由 苏小脉 于 2011-4-25 23:49 编辑
正努力将此脚本与Base64字符串加密算法结合加密
Base64 乃是一种编码格式,不是加密算法。其主要目的是把二进制数据转换为可打印的 ASCII 字符,之后就能交给只兼容文本数据的程序使用,比如 SMTP/MIME,HTTP,XML。它和 URL 编码一样,和加密是两回事。Base64 也不是压缩算法,经 Base64 编码后的字符串长度反而更大了。
作者:
summer92
时间:
2011-4-26 11:59
本帖最后由 summer92 于 2011-4-27 10:33 编辑
还得继续研究下转码
作者:
一箭烂YiJL
时间:
2011-4-26 20:35
回复
summer92
的帖子
读取不会乱码,你是用txt开启的吧。我所说用Zlib库来压缩会导致字符不同,
你可以试试:
a = Zlib::Deflate.deflate("中文字符") # 压缩
p a
a = Zlib::Inflate.inflate(a) # 解压缩
p a
复制代码
显然你会看见一堆不明物(压缩后的字符串),具体细节我不说。
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1