aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2016-05-02 15:48:37 +0200
committerpiernov <piernov@piernov.org>2016-05-02 15:48:37 +0200
commit888afbab390f227bb89c98847b516897886df3d3 (patch)
tree8d15be1ff728aa21d4065bd8a608822ca0e0e230
parent76df4eb85cadf9cf548b4aa1cc95970e1d53f48d (diff)
downloadcandybox-888afbab390f227bb89c98847b516897886df3d3.tar.gz
candybox-888afbab390f227bb89c98847b516897886df3d3.tar.bz2
candybox-888afbab390f227bb89c98847b516897886df3d3.tar.xz
candybox-888afbab390f227bb89c98847b516897886df3d3.zip
Fix game save XML generation + add ability to send a previously generated game save
-rw-r--r--inc/savegame.inc44
1 files changed, 27 insertions, 17 deletions
diff --git a/inc/savegame.inc b/inc/savegame.inc
index 4646601..79b1dae 100644
--- a/inc/savegame.inc
+++ b/inc/savegame.inc
@@ -4,23 +4,22 @@ require_once("inc/messages.inc");
define("SAVEDIR", "data/save");
-function genXML($v, $k, $xml) {
- if(is_object($v))
- {
- if(is_callable(array($v, "addToXML")))
- $v->addToXML($xml->addChild($k));
+function genXML($table, $xml) {
+ foreach($table as $k => $v) {
+ if(is_object($v)) { // Object: either Item or Inventory
+ if(is_callable(array($v, "addToXML"))) // Check if the object has a addToXML method
+ $v->addToXML($xml->addChild($k));
+ } elseif(is_array($v)) // Nested array
+ genXML($v, $xml->addChild($k));
+ else // Single value (numeric or string)
+ $xml->addChild($k, $v);
}
- else
- $xml->addChild($k, $v);
}
function genSave() {
- header("Content-Type: application/xml");
$save = new SimpleXMLElement("<save/>");
-
- array_walk_recursive($_SESSION, "genXML", $save);
+ genXML($_SESSION, $save);
return $save;
- echo $save->asXML();
}
function genFilename() {
@@ -34,11 +33,22 @@ function saveGame() {
}
function downSave() {
- $save = genSave();
- header("Content-Type: application/xml");
- header("Content-Disposition: attachment; filename=".genFilename());
- header("Pragma: no-cache");
- echo $save->asXML();
+ $save = "";
+ $filename = "";
+
+ if(empty($_GET["filename"])) {
+ $filename = genFilename();
+ $save = genSave()->asXML(); // Send current game save file if no file specified
+ } else {
+ $filename = $_GET["filename"];
+ $save = file_get_contents(SAVEDIR . "/" . $filename);
+ if(empty($save)) return; // Probably file not found
+ }
+
+ header("Content-Type: application/xml"); // Force browser to intepret the file as XML
+ header("Content-Disposition: attachment; filename=".$filename); // Force download with specific filename
+ header("Cache-Control: no-cache"); // Avoid storing save file in proxy/browser cache
+ echo $save;
}
function listSaves() {
@@ -56,7 +66,7 @@ function parseSave($xml, &$table) { // Passing $table by reference
foreach($v as $item) Inventory::addItem(Item::fromXML($item));
} else { // If nested array
$table[$k] = array();
- parseXML($v, $table[$k]);
+ parseSave($v, $table[$k]);
} // Other types unsupported (unused)
}
}