Skip to content

Commit

Permalink
Merge pull request #1 from stoozey/buffer-support
Browse files Browse the repository at this point in the history
Buffer support
  • Loading branch information
stoozey authored Jan 27, 2023
2 parents aaf62e1 + ceb7fca commit aaa61b5
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Sphinx by JuJu Adams
Sphinx by Juju Adams
https://github.com/JujuAdams/Sphinx
Version 1.0.0 - 2021-06-08
Expand Down
4 changes: 2 additions & 2 deletions scripts/__scr_ssave_macros/__scr_ssave_macros.gml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ enum SSAVE_TYPE
BOOLEAN,
STRUCT,
ARRAY,
//BUFFER, // Should be supported by v1.2.0
BUFFER,
}

enum SSAVE_PROTECTION
{
NONE, // Save data is stored in plaintext json - good if you don't care about tampering
ENCODE, // Save data is stored in base64 encoded json - good if you want *most* players to not know how to tamper
ENCRYPT, // Save data is encrypted with a key - good if you want *most* players to be unable to tamper. this is NOT secure enough for sensitive data
ENCRYPT, // Save data is encrypted with a key - good if you want *most* players to be unable to tamper. This is NOT secure enough for sensitive data
}

#macro __SSAVE_FILE_EXTENSION "ssave"
Expand Down
28 changes: 23 additions & 5 deletions scripts/__scr_ssave_value/__scr_ssave_value.gml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,31 @@ function __ssave_class_value(_name, _type, _defaultValue) constructor
case SSAVE_TYPE.BOOLEAN:
_value = (_value >= 1);
break;

case SSAVE_TYPE.BUFFER:
if ((__value != undefined) && (buffer_exists(__value)))
buffer_delete(__value);

if (SSAVE_COPY_BUFFER_ON_SET)
{
var _bufferSize = buffer_get_size(_value);
var _buffer = buffer_create(_bufferSize, buffer_fixed, 1);
buffer_copy(_value, 0, _bufferSize, _buffer, 0);
buffer_seek(_value, buffer_seek_start, 0);
_value = _buffer;
}

break;
}

__value = _value;
}

static get_type = function()
{
return __type;
}

static __is_type = function(_value)
{
switch (__type)
Expand All @@ -41,14 +61,12 @@ function __ssave_class_value(_name, _type, _defaultValue) constructor

case SSAVE_TYPE.STRUCT:
return is_struct(_value);

case SSAVE_TYPE.BUFFER:
return buffer_exists(_value);
}
}

static __type_caster_default = function(_value)
{
return _value;
}

__name = _name;
__type = _type;
__defaultValue = _defaultValue;
Expand Down
66 changes: 56 additions & 10 deletions scripts/scr_ssave/scr_ssave.gml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function SSave(_name = "data", _protection = SSAVE_PROTECTION_DEFAULT) construct

static __save_to_file = function(_filename)
{
var _success, _buffer, _data;
var _success, _buffer = undefined, _data = undefined;
try
{
var _save = __generate_output_struct();
Expand Down Expand Up @@ -114,10 +114,10 @@ function SSave(_name = "data", _protection = SSAVE_PROTECTION_DEFAULT) construct
}
finally
{
if (buffer_exists(_buffer))
if ((_buffer != undefined) && (buffer_exists(_buffer)))
buffer_delete(_buffer);

if (buffer_exists(_data))
if ((_data != undefined) && (buffer_exists(_data)))
buffer_delete(_data);
}

Expand All @@ -128,7 +128,7 @@ function SSave(_name = "data", _protection = SSAVE_PROTECTION_DEFAULT) construct
{
if (!file_exists(_filename)) return false;

var _success, _buffer, _data;
var _success, _buffer = undefined, _data = undefined;
try
{
_buffer = buffer_load(_filename);
Expand All @@ -139,6 +139,9 @@ function SSave(_name = "data", _protection = SSAVE_PROTECTION_DEFAULT) construct
switch (_header.get_version())
{
default:
case "1.2.0":
case "1.1.1":
case "1.0.1":
case "1.0.0":
{
var _bufferPos = buffer_tell(_buffer);
Expand Down Expand Up @@ -168,14 +171,16 @@ function SSave(_name = "data", _protection = SSAVE_PROTECTION_DEFAULT) construct
}

var _save = json_parse(_json);
__decode_output_struct(_save);

var _varNames = variable_struct_get_names(_save);
var i = 0;
repeat (array_length(_varNames))
{
var _varName = _varNames[i++];
var _value = __values[$ _varName];
var _jsonValue = _save[$ _varName];
_value.set(_jsonValue);
var _valueData = __values[$ _varName];
var _value = _save[$ _varName];
_valueData.set(_value);
}

__ssave_print("loaded file : ", _filename);
Expand All @@ -190,10 +195,10 @@ function SSave(_name = "data", _protection = SSAVE_PROTECTION_DEFAULT) construct
}
finally
{
if (buffer_exists(_buffer))
if ((_buffer != undefined) && (buffer_exists(_buffer)))
buffer_delete(_buffer);

if (buffer_exists(_data))
if ((_data != undefined) && (buffer_exists(_data)))
buffer_delete(_data);
}

Expand All @@ -217,13 +222,54 @@ function SSave(_name = "data", _protection = SSAVE_PROTECTION_DEFAULT) construct
var i = 0;
repeat (array_length(_names))
{
var _value;
var _name = _names[i++];
_save[$ _name] = get(_name);
var _valueData = __get_value_data(_name);
switch (_valueData.get_type())
{
default:
_value = _valueData.get();
break;

case SSAVE_TYPE.BUFFER:
var _buffer = _valueData.get();
var _bufferSize = buffer_get_size(_buffer);
_value = buffer_base64_encode(_buffer, 0, _bufferSize);
break;
}

_save[$ _name] = _value;
}

return _save;
}

static __decode_output_struct = function(_save)
{
var i = 0;
var _names = variable_struct_get_names(_save);
repeat (array_length(_names))
{
var _override = undefined;
var _name = _names[i++];
var _valueData = __get_value_data(_name);
var _value = _save[$ _name];
switch (_valueData.get_type())
{
default:
break;

case SSAVE_TYPE.BUFFER:
_override = buffer_base64_decode(_value);
__ssave_print("buffer size is ", buffer_get_size(_override));
break;
}

if (_override != undefined)
_save[$ _name] = _override;
}
}

static __throw_name_doesnt_exist = function(_name)
{
throw ("SSave value name \"" + _name + "\" doesn't exist--did you get the name wrong?");
Expand Down
4 changes: 3 additions & 1 deletion scripts/scr_ssave_config/scr_ssave_config.gml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#macro SSAVE_DIRECTORY "" // The directory that saves are saved to.

#macro SSAVE_PROTECTION_DEFAULT SSAVE_PROTECTION.NONE // How the save data is protected in the output file
#macro SSAVE_ENCRYPTION_KEY 69420133769696969694204872936 // When using SSAVE_PROTECTION.ENCRYPT, this is the key used to encrypt it
#macro SSAVE_ENCRYPTION_KEY 69420133769696969694204872936 // When using SSAVE_PROTECTION.ENCRYPT, this is the key used to encrypt it

#macro SSAVE_COPY_BUFFER_ON_SET true // When using SSave.set() with a buffer, the supplied buffer is copied (so you can delete your original)
5 changes: 5 additions & 0 deletions scripts/scr_ssave_demo/scr_ssave_demo.gml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ function SaveFile() : SSave("save") constructor
add_value("name", SSAVE_TYPE.STRING, "stoozey_");
add_value("inventory", SSAVE_TYPE.ARRAY, [ "sword", "helmet" ]);
add_value("awesome", SSAVE_TYPE.BOOLEAN, false);

var _exampleBuffer = buffer_create(512, buffer_fixed, 1);
buffer_write(_exampleBuffer, buffer_u16, 21);
buffer_write(_exampleBuffer, buffer_string, "nope");
add_value("example_buffer", SSAVE_TYPE.BUFFER, _exampleBuffer);
}

0 comments on commit aaa61b5

Please sign in to comment.