|
昨天开始就想做个SL的系统.. 直接读取字符串的..
结果遇到好几个问题``
1. string是不是不能SyncStoredString + TriggerSyncReady啊.?
同样的代码.. integer就可以成功, string函数运行都中断了..
不知道怎么直接SyncStoredString ..只好用string转integer同步数据,然后再转回string的...
2.第2个问题就很诡异了,关于缓存同步的运作机理的``
因为不太了解缓存同步数据的运作机理..写出来的代码必须运行2次才会同步到所有玩家(为啥?).
也就是 - - 我多开来测试的时候, 有时候(小几率)有的玩家的数据总是2次也同步不了..(难道是我单机多开的原因么)
以下是内容...
[jass]
// nothing SaveString(string filename,string value) 所有玩家存储一个字符串到本地文件
// string LoadString(string filename) 所有玩家读取本地文件(读取的内容各玩家可能不同)
// nothing LoadStringSync(string filename) 所有玩家读取本地文件,同步数据后储存到字符串数组sl_string中
globals
string array sl_string
string sl_folder="save\\"
string sl_stdchars="!@#$%&'()*+,- /0123456789:;<=>.@ABCDEFGHIJKLMNOPQRSTUVWXYZ[.]^_`abcdefghijklmnopqrstuvwxyz{|}~"
endglobals
function ConvertIntToString takes integer i returns string
local string s = ""
local integer c
loop
set c=i-i/94*94
set i=i/94
set s=SubString(sl_stdchars,c,c+1)+s
exitwhen i<=0
endloop
return s
endfunction
function ConvertStringToInt takes string s returns integer
local string temp_char
local integer length=StringLength(s)
local integer result
local integer c
local integer pc
local integer times=0
local integer base
set result=0
loop
exitwhen length<=0
set temp_char=SubString(s,length-1,length)
set c=0
loop
exitwhen c>93
if temp_char==SubString(sl_stdchars,c,c+1) then
set pc=times
set base=1
loop
exitwhen pc<=0
set base=base*94
set pc=pc-1
endloop
set result=result+c*base
endif
set c=c+1
endloop
set length=length-1
set times=times+1
endloop
return result
endfunction
function SaveString takes string filename,string value returns nothing
call PreloadGenClear()
call PreloadGenStart()
call Preload("\" )
call SetPlayerName(Player(0),\""+value+"\")
//")
call PreloadGenEnd(sl_folder+filename)
endfunction
function LoadString takes string filename returns string
local string name
local string value
local player p=Player(0)
set name=GetPlayerName(p)
call Preloader(sl_folder+filename)
set value=GetPlayerName(p)
call SetPlayerName(p,name)
if name==value then
return ""
endif
return value //返回空字符串""表示存档文件不存在
endfunction
function LoadStringSync takes string filename returns nothing
local gamecache cache=InitGameCache("SaveLoad.w3v")
local string s=LoadString(filename)
local integer length=StringLength(s)
local integer count=length/4
local string missionKey=I2S(GetPlayerId(GetLocalPlayer()))
local integer c
local integer count_start
if count*4!=length then
set count=count+1
endif
//-- 储存
call StoreInteger(cache,missionKey,"count",count)
set c=0
loop
exitwhen c>=count
call StoreInteger(cache,missionKey,I2S(c),ConvertStringToInt(SubString(s,c*4,c*4+4)))
set c=c+1
endloop
//-- 同步 (为啥要至少2次??)
set count_start=0 //
loop
exitwhen count_start>=2
call TriggerSyncStart()
call SyncStoredInteger(cache,missionKey,"count")
set c=0
loop
exitwhen c>=count
call SyncStoredInteger(cache,missionKey,I2S(c))
set c=c+1
endloop
call TriggerSyncReady()
set count_start=count_start+1
endloop
//-- 读取
set c=0
loop
exitwhen c>11
if HaveStoredInteger(cache,I2S(c),"count") then
set count=GetStoredInteger(cache,I2S(c),"count")
set count_start=0
set sl_string[c]=""
loop
exitwhen count_start>=count
set sl_string[c]=sl_string[c]+ConvertIntToString(GetStoredInteger(cache,I2S(c),I2S(count_start)))
set count_start=count_start+1
endloop
endif
set c=c+1
endloop
call FlushGameCache(cache)
set cache=null
endfunction
[/jass] |
|