-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* allot AsyncHttp to request misc urls * exclude swfs in simple mode * add CloudSaves * add SaveSlotList and ResultType * remove useless callback args * documentation and readibility * more documentation * simplify load, rename saveData to contents * fix loadAllFiles * readme
- Loading branch information
Showing
14 changed files
with
678 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.newgrounds.components; | ||
|
||
import io.newgrounds.objects.events.Result.SaveSlotResult; | ||
import io.newgrounds.objects.events.Result.LoadSlotsResult; | ||
import io.newgrounds.NGLite; | ||
|
||
/** Handles loading and saving of game states. */ | ||
class CloudSaveComponent extends Component { | ||
|
||
/** | ||
* Pass into the default value to allow null values. | ||
* | ||
* TODO: find less hacky solution | ||
**/ | ||
static var allowNull:Dynamic = {}; | ||
|
||
public function new (core:NGLite){ super(core); } | ||
|
||
/** | ||
* Deletes all data from a save slot. | ||
* | ||
* @param id The slot number | ||
**/ | ||
public function clearSlot(id:Int):Call<SaveSlotResult> { | ||
|
||
return new Call<SaveSlotResult>(_core, "CloudSave.clearSlot", true) | ||
.addComponentParameter("id", id); | ||
} | ||
|
||
/** | ||
* Returns a specific saveslot object. | ||
* | ||
* @param id The slot number | ||
**/ | ||
public function loadSlot(id:Int):Call<SaveSlotResult> { | ||
|
||
return new Call<SaveSlotResult>(_core, "CloudSave.loadSlot", true) | ||
.addComponentParameter("id", id); | ||
} | ||
|
||
/** | ||
* Returns a list of saveslot objects. | ||
* | ||
* @param id The slot number | ||
**/ | ||
public function loadSlots():Call<LoadSlotsResult> { | ||
|
||
return new Call<LoadSlotsResult>(_core, "CloudSave.loadSlots", true); | ||
} | ||
|
||
/** | ||
* Deletes all data from a save slot. | ||
* | ||
* @param data The data you want to save | ||
* @param id The slot number | ||
**/ | ||
public function setData(data:String, id:Int):Call<SaveSlotResult> { | ||
|
||
return new Call<SaveSlotResult>(_core, "CloudSave.setData", true) | ||
.addComponentParameter("data", data, allowNull) | ||
.addComponentParameter("id", id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package io.newgrounds.objects; | ||
|
||
import io.newgrounds.utils.AsyncHttp; | ||
import io.newgrounds.objects.events.Result.SaveSlotResult; | ||
import io.newgrounds.objects.events.ResultType; | ||
import io.newgrounds.objects.events.Response; | ||
|
||
/** | ||
* Contains information about a CloudSave slot. | ||
**/ | ||
typedef RawSaveSlot = { | ||
|
||
/** A date and time (in ISO 8601 format) representing when this slot was last saved. */ | ||
var datetime :Null<String>; | ||
/** The slot number. */ | ||
var id :Int; | ||
/** The size of the save data in bytes. */ | ||
var size :Int; | ||
/** A unix timestamp representing when this slot was last saved. */ | ||
var timestamp:Int; | ||
/** The URL containing the actual save data for this slot, or null if this slot has no data. */ | ||
var url :Null<String>; | ||
} | ||
|
||
/** | ||
* Contains information about a CloudSave slot. | ||
* | ||
* This is helper class that lets you call methods directly on the slot object, rather than | ||
* calling generic calls using the slot's id number. | ||
**/ | ||
class SaveSlot extends Object<RawSaveSlot> | ||
{ | ||
/** A date and time (in ISO 8601 format) representing when this slot was last saved. */ | ||
public var datetime(get, never):Null<String>; | ||
|
||
/** The slot number. */ | ||
public var id(get, never):Int; | ||
|
||
/** The size of the save data in bytes. */ | ||
public var size(get, never):Int; | ||
|
||
/** A unix timestamp representing when this slot was last saved. */ | ||
public var timestamp(get, never):Int; | ||
|
||
/** The URL containing the actual save data for this slot, or null if this slot has no data. */ | ||
public var url(get, never):Null<String>; | ||
|
||
inline function get_datetime () return _data.datetime; | ||
inline function get_id () return _data.id; | ||
inline function get_size () return _data.size; | ||
inline function get_timestamp() return _data.timestamp; | ||
inline function get_url () return _data.url; | ||
|
||
/** The contents of this slot's save file. Will be null until `load` is called. **/ | ||
public var contents(default, null):Null<String>; | ||
|
||
public function new(core:NGLite, data:RawSaveSlot = null) { | ||
|
||
super(core, data); | ||
} | ||
|
||
/** | ||
* Saves the supplied data to the cloud save slot | ||
* | ||
* @param data The data to save to the slot | ||
* @param callback Called when the data is saved with the new value. | ||
* Tells whether the server call was successful. | ||
* | ||
* @throws Exception if data is null | ||
*/ | ||
public function save(data:String, ?callback:(ResultType)->Void) { | ||
|
||
if (data == null) | ||
throw "cannot save null to a SaveSlot"; | ||
|
||
_core.calls.cloudSave.setData(data, id) | ||
.addDataHandler((response)->setContentsOnSlotFetch(response, data, callback)) | ||
.send(); | ||
} | ||
|
||
/** | ||
* Clears cloud save slot | ||
* | ||
* @param callback Called when the data is cleared. | ||
* Tells whether the server call was successful. | ||
*/ | ||
public function clear(?callback:(ResultType)->Void) { | ||
|
||
_core.calls.cloudSave.clearSlot(id) | ||
.addDataHandler((response)->setContentsOnSlotFetch(response, null, callback)) | ||
.send(); | ||
} | ||
|
||
function setContentsOnSlotFetch | ||
( response:Response<SaveSlotResult> | ||
, contents:Null<String> | ||
, ?callback:(ResultType)->Void | ||
) { | ||
|
||
// Always have a non-null callback to avoid having to null check everywhere | ||
if (callback == null) | ||
callback = (_)->{}; | ||
|
||
if (response.success && response.result.success) { | ||
|
||
this.contents = contents; | ||
parse(response.result.data.slot); | ||
} | ||
|
||
callback(Success); | ||
} | ||
|
||
/** | ||
* Loads the save slot's file contents | ||
* | ||
* @param callback Called when the save file is loaded. | ||
* Returns the contents, is successful, otherwise returns an error. | ||
*/ | ||
public function load(?callback:(SaveSlotResultType)->Void) { | ||
|
||
if (isEmpty()) | ||
throw 'Cannot load from an empty SaveSlot, id:$id'; | ||
|
||
// TODO: load data (async) | ||
AsyncHttp.send(url, null, | ||
(s)-> | ||
{ | ||
contents = s; | ||
callback(Success(contents)); | ||
onUpdate.dispatch(); | ||
}, | ||
(error)->callback(Error(error)) | ||
); | ||
} | ||
|
||
/** Whether any data has been saved to this slot. */ | ||
inline public function isEmpty():Bool return url == null; | ||
} | ||
|
||
typedef SaveSlotResultType = TypedResultType<Null<String>>; |
Oops, something went wrong.