22 File: fn_getSaveableParam.sqf
33 Author: KP Liberation Dev Team - https://github.com/KillahPotatoes
44 Date: 2018-01-27
5- Last Update: 2020-08-06
5+ Last Update: 2023-03-02
66 License: MIT License - http://www.opensource.org/licenses/MIT
77
88 Description:
9- Saves/loads/fetches mission parameter from profileNamespace depending on "_action" argument.
9+ Saves/loads/fetches mission parameter from FileXT or profileNamespace depending on "_action" argument.
1010 If no action provided value from "KP_load_params" variable is used.
1111 On SP enviroment saving/loading is disabled.
1212
@@ -27,17 +27,72 @@ params [
2727
2828private _value = nil ;
2929
30+ // Read data from the FileXT storage, or failing that, the server profileNamespace
31+ // Return : (string) Save data
32+ // Param 0: (string) File/Variable name
33+ fnc_loadData = {
34+ params [
35+ [" _name" , " " , [" " ]]
36+ ];
37+ private _data = nil ;
38+
39+ // Check if FileXT is available
40+ if (isClass(configFile >> " CfgPatches" >> " filext" )) then {
41+ [format [" Loading '%1' from FileXT." , _name ], " LOAD" ] call KPLIB_fnc_log ;
42+ _file = format [" %1.savedata" , _name ];
43+ [_file ] call filext_fnc_open ;
44+ [_file ] call filext_fnc_read ;
45+ _data = [_file , " Data" ] call filext_fnc_get ;
46+ [_file ] call filext_fnc_close ;
47+ };
48+
49+ // Fallback to namespace if necessary
50+ if (isNil " _data" ) then {
51+ [format [" Fallback - Loading '%1' from Profile Namespace." , _name ], " LOAD" ] call KPLIB_fnc_log ;
52+ _data = profileNamespace getVariable _name ;
53+ };
54+
55+ if (! isNil " _data" ) then {
56+ _data ;
57+ };
58+ };
59+
60+ // Write data to the FileXT storage, or failing that, the server profileNamespace
61+ // Param 0: (string) File/Variable name
62+ // Param 1: (string) Save data
63+ fnc_saveData = {
64+ params [
65+ [" _name" , " " , [" " ]],
66+ [" _data" , nil , []]
67+ ];
68+
69+ // Check if FileXT is available
70+ if (isClass(configFile >> " CfgPatches" >> " filext" )) then {
71+ [format [" Saving '%1' to FileXT." , _name ], " SAVE" ] call KPLIB_fnc_log ;
72+ _file = format [" %1.savedata" , _name ];
73+ [_file ] call filext_fnc_open ;
74+ [_file , " Data" , str _data ] call filext_fnc_set ;
75+ [_file ] call filext_fnc_write ;
76+ [_file ] call filext_fnc_close ;
77+ } else {
78+ [format [" Fallback - Saving '%1' to Profile Namespace." , _name ], " SAVE" ] call KPLIB_fnc_log ;
79+ profileNamespace setVariable [_name , _data ];
80+ saveProfileNamespace;
81+ };
82+ };
83+
3084// Use lobby value if no action specified
3185if (isNil " _action" ) then {_action = KP_load_params;};
3286
3387// We propably shoud not load parameters on SP environment
3488if (! isMultiplayer) then {_action = 2 };
3589
3690switch (_action ) do {
37- // Save to profileNamespace
91+ // Save parameters
3892 case 0 : {
3993 _value = [_paramName , _defaultValue ] call bis_fnc_getParamValue ;
40- private _savedParams = profileNamespace getVariable KPLIB_save_paramKey;
94+ private _savedParams = [KPLIB_save_paramKey] call fnc_loadData;
95+ _savedParams = parseSimpleArray _savedParams ;
4196
4297 if (isNil " _savedParams" ) then {
4398 if (KPLIB_savegame_debug > 0 ) then {[" Param save data is corrupted, creating new." , " PARAM" ] call KPLIB_fnc_log ;};
@@ -57,17 +112,16 @@ switch (_action) do {
57112 };
58113 };
59114
60- // Save params to profile namespace
61- profileNamespace setVariable [KPLIB_save_paramKey, _savedParams ];
62- saveProfileNamespace;
115+ [KPLIB_save_paramKey, str _savedParams ] call fnc_saveData;
63116 };
64- // Load from profileNamespace
117+ // Load parameters
65118 case 1 : {
66- private _savedParams = profileNamespace getVariable KPLIB_save_paramKey;
119+ private _savedParams = [KPLIB_save_paramKey] call fnc_loadData;
120+ _savedParams = parseSimpleArray _savedParams ;
67121 if (isNil " _savedParams" ) then {
68122 if (KPLIB_savegame_debug > 0 ) then {[" Param save data is corrupted, can't load!" , " PARAM" ] call KPLIB_fnc_log ;};
69123 // Fix param save data
70- profileNamespace setVariable [KPLIB_save_paramKey, []] ;
124+ [KPLIB_save_paramKey, " " ] call fnc_saveData ;
71125 if (KPLIB_savegame_debug > 0 ) then {[format [" No saved value for param: %1, fetching value." , _paramName ], " PARAM" ] call KPLIB_fnc_log ;};
72126 _value = [_paramName , _defaultValue ] call bis_fnc_getParamValue ;
73127 } else {
0 commit comments