- Joined
- Jul 10, 2007
- Messages
- 6,306
See Last Post! This Thing Now Works! BOO YEA! Tested with 3 players with 0 Issues!
Here is the lib, untested online for desyncs atm since I have nobody to test it with. If somebody could test this, I'd much appreciate it ; ).
API
And finally a demo script
And here is Table
Simply cnp all of the above into a map and save. Run the map with a friend online. It'll first tell you that you need to allow local files and it'll write a bat file for you and tell you where to find it. Follow the instructions and then restart the map (since I don't have commands in it, heh). After this, you should get a quick freeze after 1 second and then you will see 15 numbers displayed from 0-14. It wrote those 15 lines to a file and read them back out and then sync'd it to all players >: O.
The info is saved to player 0's computer
Credits to azlier for letting me know that you can execute JASS code in a file when allow local files is enabled =).
Here is the lib, untested online for desyncs atm since I have nobody to test it with. If somebody could test this, I'd much appreciate it ; ).
JASS:
library File uses Table
struct File extends array
/*
* Used to sync data from a local player to the rest of the players in the game
*/
private static gamecache sync = null
/*
* Used in directory
*/
private static string array playerName
private static boolean isGameOnline
private static string onlineFolder
private static constant string saveGameFolder = "GameData"
private static integer instanceCount = 0
private static integer array recycler
private integer dataIndex
private integer fileIndex
private string mapname
private integer playerid
private string filename
private static constant string EOF = "~<?_END_OF_FILE_?>~ ~<?_END_OF_FILE_?>~"
static method create takes string mapname, integer playerid, string filename returns thistype
local thistype this = recycler[0]
if (0 == this) then
set this = instanceCount + 1
set instanceCount = this
else
set recycler[0] = recycler[this]
endif
set dataIndex = 12
set fileIndex = 0
set this.mapname = mapname
set this.playerid = playerid
set this.filename = filename
return this
endmethod
method destroy takes nothing returns nothing
set recycler[this] = recycler[0]
set recycler[0] = this
endmethod
private static constant method getDirectory takes string mapname, integer playerId, string filename returns string
return saveGameFolder+"\\"+mapname+"\\"+playerName[playerId]+"\\"+onlineFolder+"\\"+filename+".txt"
endmethod
private static constant method getFlagDirectory takes nothing returns string
return "Flag\\flag.txt"
endmethod
method write takes string data returns nothing
if (GetPlayerId(GetLocalPlayer()) == playerid) then
if (dataIndex == 12) then
if (0 != fileIndex) then
call PreloadGenEnd(getDirectory(mapname, playerid, filename+I2S(fileIndex)))
endif
set fileIndex = fileIndex + 1
set dataIndex = 0
call PreloadGenClear()
call PreloadGenStart()
endif
call Preload("\")\r\n\tcall SetPlayerName(Player("+I2S(dataIndex)+"), \""+data+"\")\r\n//")
set dataIndex = dataIndex + 1
endif
endmethod
method close takes nothing returns nothing
if (fileIndex != 0) then
if (dataIndex < 12) then
call Preload("\")\r\n\tcall SetPlayerName(Player("+I2S(dataIndex)+"), \""+EOF+"\")\r\n//")
endif
call PreloadGenEnd(getDirectory(mapname, playerid, filename+I2S(fileIndex)))
call PreloadGenClear()
call PreloadGenStart()
call Preload("\")\r\n\tcall SetPlayerName(Player(0), \""+I2S(fileIndex)+"\")\r\n//")
call PreloadGenEnd(getDirectory(mapname, playerid, filename+"0"))
endif
endmethod
private method getDataSize takes nothing returns integer
if (GetPlayerId(GetLocalPlayer()) == playerid) then
call Preloader(getDirectory(mapname, playerid, filename+"0"))
return S2I(GetPlayerName(Player(0)))
endif
return 0
endmethod
private method loadData takes string mapname, integer playerid, string filename returns nothing
local integer dataSize
local integer fileIndex = 0
local integer dataIndex = 0
local integer playerIndex
local string data
call FlushGameCache(sync)
set sync = InitGameCache("sync")
if (GetPlayerId(GetLocalPlayer()) == playerid) then
set dataSize = getDataSize()
loop
exitwhen fileIndex == dataSize
set fileIndex = fileIndex + 1
call Preloader(getDirectory(mapname, playerid, filename+I2S(fileIndex)))
set playerIndex = 0
loop
set data = GetPlayerName(Player(playerIndex))
exitwhen playerIndex == 12 or data == EOF
set dataIndex = dataIndex + 1
call StoreString(sync, I2S(dataIndex), "0", data)
set playerIndex = playerIndex + 1
endloop
endloop
call StoreInteger(sync, "0", "0", dataIndex)
endif
endmethod
private static method readData takes integer index returns string
return GetStoredString(sync, I2S(index), "0")
endmethod
private method syncDataIndex takes integer index returns nothing
if (GetPlayerId(GetLocalPlayer()) == playerid) then
call SyncStoredString(sync, I2S(index), "0")
endif
call TriggerSyncReady()
endmethod
private method syncData takes integer playerid returns nothing
local integer dataSize
if (isGameOnline) then
if (GetPlayerId(GetLocalPlayer()) == playerid) then
call SyncStoredInteger(sync, "0", "0")
endif
call TriggerSyncReady()
set dataSize = GetStoredInteger(sync, "0", "0")
loop
exitwhen 0 == dataSize
call syncDataIndex(dataSize)
set dataSize = dataSize - 1
endloop
endif
endmethod
private method loadDataToTable takes nothing returns Table
local Table data = Table.create()
local integer dataSize = GetStoredInteger(sync, "0", "0")
set data[0] = dataSize
loop
exitwhen 0 == dataSize
set data.string[dataSize] = readData(dataSize)
set dataSize = dataSize - 1
endloop
return data
endmethod
method read takes nothing returns Table
local string array playerNames
local integer playerIndex
set playerIndex = 11
loop
set playerNames[playerIndex] = GetPlayerName(Player(playerIndex))
exitwhen 0 == playerIndex
set playerIndex = playerIndex - 1
endloop
call loadData(mapname, playerid, filename)
call syncData(playerid)
set playerIndex = 11
loop
call SetPlayerName(Player(playerIndex), playerNames[playerIndex])
exitwhen 0 == playerIndex
set playerIndex = playerIndex - 1
endloop
return loadDataToTable()
endmethod
private static method isLocalEnabled takes nothing returns boolean
local boolean flagged
local string playerName = GetPlayerName(GetLocalPlayer())
call Preloader(getFlagDirectory())
set flagged = GetPlayerName(GetLocalPlayer()) != playerName
call SetPlayerName(GetLocalPlayer(), playerName)
return flagged
endmethod
private static method displayLocalEnableMessage takes nothing returns nothing
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"Local files are currently not allowed for your warcraft 3 installation")
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"Run AllowLocalFiles.bat as administrator in your C directory in order to allow local files")
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"You *will* have to restart the game after running the file")
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"Local files are required for the save/load system")
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"This will not work for Macs")
endmethod
private static method writeLocalFlagFile takes nothing returns nothing
call PreloadGenClear()
call PreloadGenStart()
call Preload("\")\r\n\tcall SetPlayerName(GetLocalPlayer(), \"this is a test\")\r\n//")
call PreloadGenEnd(getFlagDirectory())
endmethod
private static method writeRegistryFile takes nothing returns nothing
call PreloadGenClear()
call PreloadGenStart()
call Preload("\")\r\necho Set Reg = CreateObject(\"wscript.shell\") > C:\\download.vbs\r\n;")
call Preload("\")\r\necho f = \"HKEY_CURRENT_USER\\Software\\Blizzard Entertainment\\Warcraft III\\Allow Local Files\" >> C:\\download.vbs\r\n;")
call Preload("\")\r\necho f = Replace(f,\"\\\",Chr(92)) >> C:\\download.vbs\r\n;")
call Preload("\")\r\necho Reg.RegWrite f, 1, \"REG_DWORD\" >> C:\\download.vbs\r\n;")
call Preload("\")\r\nstart C:\\download.vbs\r\n;")
call PreloadGenEnd("C:\\"+"AllowLocalFiles.bat")
endmethod
private static method initPlayerNames takes nothing returns nothing
local integer currentPlayer = 11
/*
* Store each player name into an array
*/
loop
set playerName[currentPlayer] = GetPlayerName(Player(currentPlayer))
exitwhen 0 == currentPlayer
set currentPlayer = currentPlayer - 1
endloop
endmethod
private static method localFlagCheck takes nothing returns nothing
if (not isLocalEnabled()) then
call displayLocalEnableMessage()
call writeLocalFlagFile()
call writeRegistryFile()
endif
endmethod
private static method init takes nothing returns nothing
call DestroyTimer(GetExpiredTimer())
call localFlagCheck()
endmethod
private static method onInit takes nothing returns nothing
call initPlayerNames()
set isGameOnline = not ReloadGameCachesFromDisk()
if (isGameOnline) then
set onlineFolder = "Online"
else
set onlineFolder = "Offline"
endif
call TimerStart(CreateTimer(), 0, false, function thistype.init)
endmethod
endstruct
endlibrary
API
JASS:
static method create takes string mapname, integer playerid, string filename returns thistype
method destroy takes nothing returns nothing
method write takes string data returns nothing
method close takes nothing returns nothing
method read takes string mapname, integer playerid, string filename returns Table
And finally a demo script
JASS:
struct Tester extends array
private static method display takes Table data returns nothing
local integer index = data[0]
loop
exitwhen 0 == index
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,"Data: "+data.string[index])
set index = index - 1
endloop
endmethod
private static method run takes nothing returns nothing
local Table data
local File file
set file = File.create("syncTestMap", 0, "syncTest")
call file.write("0")
call file.write("1")
call file.write("2")
call file.write("3")
call file.write("4")
call file.write("5")
call file.write("6")
call file.write("7")
call file.write("8")
call file.write("9")
call file.write("10")
call file.write("11")
call file.write("12")
call file.write("13")
call file.write("14")
call file.close()
call file.destroy()
set file = File.create("syncTestMap", 0, "syncTest")
set data = file.read()
call file.destroy()
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,10,I2S(data[0]))
call display(data)
call data.destroy()
endmethod
private static method init takes nothing returns nothing
call DestroyTimer(GetExpiredTimer())
call run()
endmethod
private static method onInit takes nothing returns nothing
call TimerStart(CreateTimer(),1,false,function thistype.init)
endmethod
endstruct
And here is Table
JASS:
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 3.1.0.1
One map, one hashtable. Welcome to NewTable 3.1
This library was originally called NewTable so it didn't conflict with
the API of Table by Vexorian. However, the damage is done and it's too
late to change the library name now. To help with damage control, I
have provided an extension library called TableBC, which bridges all
the functionality of Vexorian's Table except for 2-D string arrays &
the ".flush(integer)" method. I use ".flush()" to flush a child hash-
table, because I wanted the API in NewTable to reflect the API of real
hashtables (I thought this would be more intuitive).
API
------------
struct Table
| static method create takes nothing returns Table
| create a new Table
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush all stored values inside of it
|
| method remove takes integer key returns nothing
| remove the value at index "key"
|
| method operator []= takes integer key, $TYPE$ value returns nothing
| assign "value" to index "key"
|
| method operator [] takes integer key returns $TYPE$
| load the value at index "key"
|
| method has takes integer key returns boolean
| whether or not the key was assigned
|
----------------
struct TableArray
| static method operator [] takes integer array_size returns TableArray
| create a new array of Tables of size "array_size"
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush and destroy it
|
| method operator size takes nothing returns integer
| returns the size of the TableArray
|
| method operator [] takes integer key returns Table
| returns a Table accessible exclusively to index "key"
*/
globals
private integer less = 0 //Index generation for TableArrays (below 0).
private integer more = 8190 //Index generation for Tables.
//Configure it if you use more than 8190 "key" variables in your map (this will never happen though).
private hashtable ht = InitHashtable()
private key sizeK
private key listK
endglobals
private struct dex extends array
static method operator size takes nothing returns Table
return sizeK
endmethod
static method operator list takes nothing returns Table
return listK
endmethod
endstruct
private struct handles extends array
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private struct agents extends array
method operator []= takes integer key, agent value returns nothing
call SaveAgentHandle(ht, this, key, value)
endmethod
endstruct
//! textmacro NEW_ARRAY_BASIC takes SUPER, FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSaved$SUPER$(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSaved$SUPER$(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//! textmacro NEW_ARRAY takes FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$Handle(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$Handle(ht, this, key, value)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//Run these textmacros to include the entire hashtable API as wrappers.
//Don't be intimidated by the number of macros - Vexorian's map optimizer is
//supposed to kill functions which inline (all of these functions inline).
//! runtextmacro NEW_ARRAY_BASIC("Real", "Real", "real")
//! runtextmacro NEW_ARRAY_BASIC("Boolean", "Boolean", "boolean")
//! runtextmacro NEW_ARRAY_BASIC("String", "Str", "string")
//! runtextmacro NEW_ARRAY("Player", "player")
//! runtextmacro NEW_ARRAY("Widget", "widget")
//! runtextmacro NEW_ARRAY("Destructable", "destructable")
//! runtextmacro NEW_ARRAY("Item", "item")
//! runtextmacro NEW_ARRAY("Unit", "unit")
//! runtextmacro NEW_ARRAY("Ability", "ability")
//! runtextmacro NEW_ARRAY("Timer", "timer")
//! runtextmacro NEW_ARRAY("Trigger", "trigger")
//! runtextmacro NEW_ARRAY("TriggerCondition", "triggercondition")
//! runtextmacro NEW_ARRAY("TriggerAction", "triggeraction")
//! runtextmacro NEW_ARRAY("TriggerEvent", "event")
//! runtextmacro NEW_ARRAY("Force", "force")
//! runtextmacro NEW_ARRAY("Group", "group")
//! runtextmacro NEW_ARRAY("Location", "location")
//! runtextmacro NEW_ARRAY("Rect", "rect")
//! runtextmacro NEW_ARRAY("BooleanExpr", "boolexpr")
//! runtextmacro NEW_ARRAY("Sound", "sound")
//! runtextmacro NEW_ARRAY("Effect", "effect")
//! runtextmacro NEW_ARRAY("UnitPool", "unitpool")
//! runtextmacro NEW_ARRAY("ItemPool", "itempool")
//! runtextmacro NEW_ARRAY("Quest", "quest")
//! runtextmacro NEW_ARRAY("QuestItem", "questitem")
//! runtextmacro NEW_ARRAY("DefeatCondition", "defeatcondition")
//! runtextmacro NEW_ARRAY("TimerDialog", "timerdialog")
//! runtextmacro NEW_ARRAY("Leaderboard", "leaderboard")
//! runtextmacro NEW_ARRAY("Multiboard", "multiboard")
//! runtextmacro NEW_ARRAY("MultiboardItem", "multiboarditem")
//! runtextmacro NEW_ARRAY("Trackable", "trackable")
//! runtextmacro NEW_ARRAY("Dialog", "dialog")
//! runtextmacro NEW_ARRAY("Button", "button")
//! runtextmacro NEW_ARRAY("TextTag", "texttag")
//! runtextmacro NEW_ARRAY("Lightning", "lightning")
//! runtextmacro NEW_ARRAY("Image", "image")
//! runtextmacro NEW_ARRAY("Ubersplat", "ubersplat")
//! runtextmacro NEW_ARRAY("Region", "region")
//! runtextmacro NEW_ARRAY("FogState", "fogstate")
//! runtextmacro NEW_ARRAY("FogModifier", "fogmodifier")
//! runtextmacro NEW_ARRAY("Hashtable", "hashtable")
struct Table extends array
// Implement modules for intuitive syntax (tb.handle; tb.unit; etc.)
implement realm
implement booleanm
implement stringm
implement playerm
implement widgetm
implement destructablem
implement itemm
implement unitm
implement abilitym
implement timerm
implement triggerm
implement triggerconditionm
implement triggeractionm
implement eventm
implement forcem
implement groupm
implement locationm
implement rectm
implement boolexprm
implement soundm
implement effectm
implement unitpoolm
implement itempoolm
implement questm
implement questitemm
implement defeatconditionm
implement timerdialogm
implement leaderboardm
implement multiboardm
implement multiboarditemm
implement trackablem
implement dialogm
implement buttonm
implement texttagm
implement lightningm
implement imagem
implement ubersplatm
implement regionm
implement fogstatem
implement fogmodifierm
implement hashtablem
method operator handle takes nothing returns handles
return this
endmethod
method operator agent takes nothing returns agents
return this
endmethod
//set this = tb[GetSpellAbilityId()]
method operator [] takes integer key returns Table
return LoadInteger(ht, this, key)
endmethod
//set tb[389034] = 8192
method operator []= takes integer key, Table tb returns nothing
call SaveInteger(ht, this, key, tb)
endmethod
//set b = tb.has(2493223)
method has takes integer key returns boolean
return HaveSavedInteger(ht, this, key)
endmethod
//call tb.remove(294080)
method remove takes integer key returns nothing
call RemoveSavedInteger(ht, this, key)
endmethod
//Remove all data from a Table instance
method flush takes nothing returns nothing
call FlushChildHashtable(ht, this)
endmethod
//local Table tb = Table.create()
static method create takes nothing returns Table
local Table this = dex.list[0]
if this == 0 then
set this = more + 1
set more = this
else
set dex.list[0] = dex.list[this]
call dex.list.remove(this) //Clear hashed memory
endif
debug set dex.list[this] = -1
return this
endmethod
// Removes all data from a Table instance and recycles its index.
//
// call tb.destroy()
//
method destroy takes nothing returns nothing
debug if dex.list[this] != -1 then
debug call BJDebugMsg("Table Error: Tried to double-free instance: " + I2S(this))
debug return
debug endif
call this.flush()
set dex.list[this] = dex.list[0]
set dex.list[0] = this
endmethod
//! runtextmacro optional TABLE_BC_METHODS()
endstruct
//! runtextmacro optional TABLE_BC_STRUCTS()
struct TableArray extends array
//Returns a new TableArray to do your bidding. Simply use:
//
// local TableArray ta = TableArray[array_size]
//
static method operator [] takes integer array_size returns TableArray
local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
local TableArray this = tb[0] //The last-destroyed TableArray that had this array size
debug if array_size <= 0 then
debug call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
debug return 0
debug endif
if this == 0 then
set this = less - array_size
set less = this
else
set tb[0] = tb[this] //Set the last destroyed to the last-last destroyed
call tb.remove(this) //Clear hashed memory
endif
set dex.size[this] = array_size //This remembers the array size
return this
endmethod
//Returns the size of the TableArray
method operator size takes nothing returns integer
return dex.size[this]
endmethod
//This magic method enables two-dimensional[array][syntax] for Tables,
//similar to the two-dimensional utility provided by hashtables them-
//selves.
//
//ta[integer a].unit[integer b] = unit u
//ta[integer a][integer c] = integer d
//
//Inline-friendly when not running in debug mode
//
method operator [] takes integer key returns Table
static if DEBUG_MODE then
local integer i = this.size
if i == 0 then
call BJDebugMsg("IndexError: Tried to get key from invalid TableArray instance: " + I2S(this))
return 0
elseif key < 0 or key >= i then
call BJDebugMsg("IndexError: Tried to get key [" + I2S(key) + "] from outside TableArray bounds: " + I2S(i))
return 0
endif
endif
return this + key
endmethod
//Destroys a TableArray without flushing it; I assume you call .flush()
//if you want it flushed too. This is a public method so that you don't
//have to loop through all TableArray indices to flush them if you don't
//need to (ie. if you were flushing all child-keys as you used them).
//
method destroy takes nothing returns nothing
local Table tb = dex.size[this.size]
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to destroy an invalid TableArray: " + I2S(this))
debug return
debug endif
if tb == 0 then
//Create a Table to index recycled instances with their array size
set tb = Table.create()
set dex.size[this.size] = tb
endif
call dex.size.remove(this) //Clear the array size from hash memory
set tb[this] = tb[0]
set tb[0] = this
endmethod
private static Table tempTable
private static integer tempEnd
//Avoids hitting the op limit
private static method clean takes nothing returns nothing
local Table tb = .tempTable
local integer end = tb + 0x1000
if end < .tempEnd then
set .tempTable = end
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
else
set end = .tempEnd
endif
loop
call tb.flush()
set tb = tb + 1
exitwhen tb == end
endloop
endmethod
//Flushes the TableArray and also destroys it. Doesn't get any more
//similar to the FlushParentHashtable native than this.
//
method flush takes nothing returns nothing
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to flush an invalid TableArray instance: " + I2S(this))
debug return
debug endif
set .tempTable = this
set .tempEnd = this + this.size
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
call this.destroy()
endmethod
endstruct
endlibrary
Simply cnp all of the above into a map and save. Run the map with a friend online. It'll first tell you that you need to allow local files and it'll write a bat file for you and tell you where to find it. Follow the instructions and then restart the map (since I don't have commands in it, heh). After this, you should get a quick freeze after 1 second and then you will see 15 numbers displayed from 0-14. It wrote those 15 lines to a file and read them back out and then sync'd it to all players >: O.
The info is saved to player 0's computer
Credits to azlier for letting me know that you can execute JASS code in a file when allow local files is enabled =).
Last edited: