Skip to content

Commit

Permalink
!XI (Integrate) ArmaLads_LIVE -> ArmaLads_FileXT @ 4240331
Browse files Browse the repository at this point in the history
!FT (Save/Load) Added FileXT support for parameters with auto-migration. Simplified FileXT/profilenamespace save and load functions
  • Loading branch information
uniflare committed May 6, 2022
2 parents cad9486 + 4240331 commit 21a06cb
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 69 deletions.
47 changes: 22 additions & 25 deletions Missionframework/functions/fn_doSave.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@

if (!isServer) exitWith {false};

// Write data to FileXT storage
// Param 0: (string) Filename
// Write data to the FileXT storage, or failing that, the server profileNamespace
// Param 0: (string) File/Variable name
// Param 1: (string) Save data
fnc_saveFileXT = {
private _file = _this select 0;
private _data = _this select 1;
[_file] call filext_fnc_open;
[_file, "Data", _data] call filext_fnc_set;
[_file] call filext_fnc_write;
[_file] call filext_fnc_close;
};

// Write data in the server profileNamespace
// Param 0: (string) Variable name
// Param 1: (string) Save data
fnc_saveProfileNamespace = {
private _variable = _this select 0;
private _data = _this select 1;
profileNamespace setVariable [_variable, _data];
saveProfileNamespace;
fnc_saveData = {
params [
["_name", "", [""]],
["_data", nil, []]
];

// Check if FileXT is available
if (isClass(configFile >> "CfgPatches" >> "filext")) then {
[format ["Saving '%1' to FileXT.", _name], "SAVE"] call KPLIB_fnc_log;
_file = format ["%1.savedata", _name];
[_file] call filext_fnc_open;
[_file, "Data", _data] call filext_fnc_set;
[_file] call filext_fnc_write;
[_file] call filext_fnc_close;
} else {
[format ["Fallback - Saving '%1' to Profile Namespace.", _name], "SAVE"] call KPLIB_fnc_log;
profileNamespace setVariable [_name, _data];
saveProfileNamespace;
};
};

if (!KPLIB_init) exitWith {
Expand All @@ -53,12 +55,7 @@ kp_liberation_saving = true;

private _saveData = [] call KPLIB_fnc_getSaveData;

// Check if FileXT is available
if (isClass(configFile >> "CfgPatches" >> "filext")) then {
[GRLIB_save_key + ".savedata", str _saveData] call fnc_saveFileXT;
} else {
[GRLIB_save_key, str _saveData] call fnc_saveProfileNamespace;
};
[GRLIB_save_key, str _saveData] call fnc_saveData;

kp_liberation_saving = false;

Expand Down
77 changes: 66 additions & 11 deletions Missionframework/functions/fn_getSaveableParam.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
File: fn_getSaveableParam.sqf
Author: KP Liberation Dev Team - https://github.com/KillahPotatoes
Date: 2018-01-27
Last Update: 2020-04-17
Last Update: 2022-05-06
License: MIT License - http://www.opensource.org/licenses/MIT
Description:
Saves/loads/fetches mission parameter from profileNamespace depending on "_action" argument.
Saves/loads/fetches mission parameter from FileXT or profileNamespace depending on "_action" argument.
If no action provided value from "KP_load_params" variable is used.
On SP enviroment saving/loading is disabled.
Expand All @@ -28,17 +28,72 @@ params [
private _saveKey = "KP_LIBERATION_" + (toUpper worldName) + "_SAVE_PARAMS";
private _value = nil;

// Read data from the FileXT storage, or failing that, the server profileNamespace
// Return : (string) Save data
// Param 0: (string) File/Variable name
fnc_loadData = {
params [
["_name", "", [""]]
];
private _data = nil;

// Check if FileXT is available
if (isClass(configFile >> "CfgPatches" >> "filext")) then {
[format ["Loading '%1' from FileXT.", _name], "LOAD"] call KPLIB_fnc_log;
_file = format ["%1.savedata", _name];
[_file] call filext_fnc_open;
[_file] call filext_fnc_read;
_data = [_file, "Data"] call filext_fnc_get;
[_file] call filext_fnc_close;
};

// Fallback to namespace if necessary
if (isNil "_data") then {
[format ["Fallback - Loading '%1' from Profile Namespace.", _name], "LOAD"] call KPLIB_fnc_log;
_data = profileNamespace getVariable _name;
};

if (!isNil "_data") then {
_data;
};
};

// Write data to the FileXT storage, or failing that, the server profileNamespace
// Param 0: (string) File/Variable name
// Param 1: (string) Save data
fnc_saveData = {
params [
["_name", "", [""]],
["_data", nil, []]
];

// Check if FileXT is available
if (isClass(configFile >> "CfgPatches" >> "filext")) then {
[format ["Saving '%1' to FileXT.", _name], "SAVE"] call KPLIB_fnc_log;
_file = format ["%1.savedata", _name];
[_file] call filext_fnc_open;
[_file, "Data", str _data] call filext_fnc_set;
[_file] call filext_fnc_write;
[_file] call filext_fnc_close;
} else {
[format ["Fallback - Saving '%1' to Profile Namespace.", _name], "SAVE"] call KPLIB_fnc_log;
profileNamespace setVariable [_name, _data];
saveProfileNamespace;
};
};

// Use lobby value if no action specified
if(isNil "_action") then {_action = KP_load_params;};

// We propably shoud not load parameters on SP environment
if(!isMultiplayer) then {_action = 2};

switch (_action) do {
// Save to profileNamespace
// Save parameters
case 0: {
_value = [_paramName, _defaultValue] call bis_fnc_getParamValue;
private _savedParams = profileNamespace getVariable _saveKey;
private _savedParams = [_saveKey] call fnc_loadData;
_savedParams = parseSimpleArray _savedParams;

if(isNil "_savedParams") then {
if (KP_liberation_savegame_debug > 0) then {["Param save data is corrupted, creating new.", "PARAM"] call KPLIB_fnc_log;};
Expand All @@ -57,18 +112,18 @@ switch (_action) do {
_singleParam set [1, _value];
};
};

// Save params to profile namespace
profileNamespace setVariable [_saveKey, _savedParams];
saveProfileNamespace;

[_saveKey, str _savedParams] call fnc_saveData;
};
// Load from profileNamespace
// Load parameters
case 1: {
private _savedParams = profileNamespace getVariable _saveKey;
private _savedParams = [_saveKey] call fnc_loadData;
_savedParams = parseSimpleArray _savedParams;

if(isNil "_savedParams") then {
if (KP_liberation_savegame_debug > 0) then {["Param save data is corrupted, can't load!", "PARAM"] call KPLIB_fnc_log;};
// Fix param save data
profileNamespace setVariable [_saveKey, []];
[_saveKey, ""] call fnc_saveData;
if (KP_liberation_savegame_debug > 0) then {[format ["No saved value for param: %1, fetching value.", _paramName], "PARAM"] call KPLIB_fnc_log;};
_value = [_paramName, _defaultValue] call bis_fnc_getParamValue;
} else {
Expand Down
54 changes: 21 additions & 33 deletions Missionframework/scripts/server/game/save_manager.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -161,49 +161,37 @@ stats_vehicles_recycled = 0;
_x setVariable ["KP_liberation_edenObject", true];
} forEach (allMissionObjects "");

// Read data from FileXT storage
// Return: (string) Save data
// Param 0: (string) Filename
fnc_loadFileXT = {
private _file = _this select 0;
// Read data from the FileXT storage, or failing that, the server profileNamespace
// Return : (string) Save data
// Param 0: (string) File/Variable name
fnc_loadData = {
params [
["_name", "", [""]]
];
private _data = nil;

[format ["Loading from FileXT."], "SAVE"] call KPLIB_fnc_log;

[_file] call filext_fnc_open;
[_file] call filext_fnc_read;
_data = [_file, "Data"] call filext_fnc_get;
[_file] call filext_fnc_close;
if (!isNil "_data") then {
_data;
// Check if FileXT is available
if (isClass(configFile >> "CfgPatches" >> "filext")) then {
[format ["Loading '%1' from FileXT.", _name], "LOAD"] call KPLIB_fnc_log;
_file = format ["%1.savedata", _name];
[_file] call filext_fnc_open;
[_file] call filext_fnc_read;
_data = [_file, "Data"] call filext_fnc_get;
[_file] call filext_fnc_close;
};
};

// Write data in the server profileNamespace
// Return: (string) Save data
// Param 0: (string) Variable name
fnc_loadProfileNamespace = {
private _variable = _this select 0;
private _data = nil;

[format ["Fallback - Loading from Profile Namespace."], "SAVE"] call KPLIB_fnc_log;
// Fallback to namespace if necessary
if (isNil "_data") then {
[format ["Fallback - Loading '%1' from Profile Namespace.", _name], "LOAD"] call KPLIB_fnc_log;
_data = profileNamespace getVariable _name;
};

_data = profileNamespace getVariable GRLIB_save_key;
if (!isNil "_data") then {
_data;
};
};

// Check if FileXT is available
private _saveData = nil;
if (isClass(configFile >> "CfgPatches" >> "filext")) then {
_saveData = [GRLIB_save_key + ".savedata"] call fnc_loadFileXT;
};

// Fallback/Default to profileNamespace if _saveData is nil
if (isNil "_saveData") then {
_saveData = [GRLIB_save_key] call fnc_loadProfileNamespace;
};
_saveData = [GRLIB_save_key] call fnc_loadData;

// Load save data, when retrieved
if (!isNil "_saveData") then {
Expand Down

0 comments on commit 21a06cb

Please sign in to comment.