aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEladNLG <e1lad8955@gmail.com>2023-07-22 23:04:33 +0300
committerGitHub <noreply@github.com>2023-07-22 21:04:33 +0100
commit2cce2943b50952ff7eea36bba529b6ce62ec5ba1 (patch)
tree8186ed70a03b7954192b419af7d5b565ca385052
parentb951cae3d2bcf581bf9b6d8fece0d6b9bc66c67b (diff)
downloadNorthstarMods-2cce2943b50952ff7eea36bba529b6ce62ec5ba1.tar.gz
NorthstarMods-2cce2943b50952ff7eea36bba529b6ce62ec5ba1.zip
Safe IO script part (#595)
* Safe IO script part * Fix compile-check * Apply suggestions from code review * Fix compile-check... again... * Apply suggestions from code review :( * Apply suggestions from code review * stuff :) * :D * Add optional failure callback * fix :) * he forgot forgor * Fix memory leak :) * gah * oops :) * Use failed callback if the json file is invalid --------- Co-authored-by: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> Co-authored-by: uniboi <64006268+uniboi@users.noreply.github.com>
-rw-r--r--.github/nativefuncs.json144
-rw-r--r--Northstar.Custom/mod.json4
-rw-r--r--Northstar.Custom/mod/scripts/vscripts/sh_northstar_safe_io.gnut83
3 files changed, 231 insertions, 0 deletions
diff --git a/.github/nativefuncs.json b/.github/nativefuncs.json
index 751b48934..6f46095fd 100644
--- a/.github/nativefuncs.json
+++ b/.github/nativefuncs.json
@@ -179,6 +179,54 @@
"helpText":"Whether or not HTTP requests can be made to a private network address. You can enable this by starting the game with -allowlocalhttp.",
"returnTypeString":"bool",
"argTypes":""
+ },
+ {
+ "name":"NS_InternalLoadFile",
+ "helpText":"Loads a file asynchronously.",
+ "returnTypeString":"int",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSSaveFile",
+ "helpText":"Saves a file.",
+ "returnTypeString":"void",
+ "argTypes":"string file, string data"
+ },
+ {
+ "name":"NSSaveJSONFile",
+ "helpText":"Converts a squirrel table to a json string, then saves it to a file.",
+ "returnTypeString":"void",
+ "argTypes":"string file, table data"
+ },
+ {
+ "name":"NSDoesFileExist",
+ "helpText":"Checks whether or not a file exists.",
+ "returnTypeString":"bool",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSDeleteFile",
+ "helpText":"Deletes a file.",
+ "returnTypeString":"bool",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NS_InternalGetAllFiles",
+ "helpText":"Returns an array of all files in a mod's save folder.",
+ "returnTypeString":"array<string>",
+ "argTypes":"string path"
+ },
+ {
+ "name":"NSGetFileSize",
+ "helpText":"Returns the size of a file, in KB, rounded down.",
+ "returnTypeString":"int",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSIsFolder",
+ "helpText":"Returns whether or not a given path leads to a folder.",
+ "returnTypeString":"bool",
+ "argTypes":"string path"
}
],
"CLIENT":[
@@ -327,6 +375,54 @@
"argTypes":""
},
{
+ "name":"NS_InternalLoadFile",
+ "helpText":"Loads a file asynchronously.",
+ "returnTypeString":"int",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSSaveFile",
+ "helpText":"Saves a file.",
+ "returnTypeString":"void",
+ "argTypes":"string file, string data"
+ },
+ {
+ "name":"NSSaveJSONFile",
+ "helpText":"Converts a squirrel table to a json string, then saves it to a file.",
+ "returnTypeString":"void",
+ "argTypes":"string file, table data"
+ },
+ {
+ "name":"NSDoesFileExist",
+ "helpText":"Checks whether or not a file exists.",
+ "returnTypeString":"bool",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSDeleteFile",
+ "helpText":"Deletes a file.",
+ "returnTypeString":"bool",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NS_InternalGetAllFiles",
+ "helpText":"Returns an array of all files in a mod's save folder.",
+ "returnTypeString":"array<string>",
+ "argTypes":"string path"
+ },
+ {
+ "name":"NSGetFileSize",
+ "helpText":"Returns the size of a file, in KB, rounded down.",
+ "returnTypeString":"int",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSIsFolder",
+ "helpText":"Returns whether or not a given path leads to a folder.",
+ "returnTypeString":"bool",
+ "argTypes":"string path"
+ },
+ {
"name":"NSPushGameStateData",
"helpText":"",
"returnTypeString":"void",
@@ -581,6 +677,54 @@
"argTypes":""
},
{
+ "name":"NS_InternalLoadFile",
+ "helpText":"Loads a file asynchronously.",
+ "returnTypeString":"int",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSSaveFile",
+ "helpText":"Saves a file.",
+ "returnTypeString":"void",
+ "argTypes":"string file, string data"
+ },
+ {
+ "name":"NSSaveJSONFile",
+ "helpText":"Converts a squirrel table to a json string, then saves it to a file.",
+ "returnTypeString":"void",
+ "argTypes":"string file, table data"
+ },
+ {
+ "name":"NSDoesFileExist",
+ "helpText":"Checks whether or not a file exists.",
+ "returnTypeString":"bool",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSDeleteFile",
+ "helpText":"Deletes a file.",
+ "returnTypeString":"bool",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NS_InternalGetAllFiles",
+ "helpText":"Returns an array of all files in a mod's save folder.",
+ "returnTypeString":"array<string>",
+ "argTypes":"string path"
+ },
+ {
+ "name":"NSGetFileSize",
+ "helpText":"Returns the size of a file, in KB, rounded down.",
+ "returnTypeString":"int",
+ "argTypes":"string file"
+ },
+ {
+ "name":"NSIsFolder",
+ "helpText":"Returns whether or not a given path leads to a folder.",
+ "returnTypeString":"bool",
+ "argTypes":"string path"
+ },
+ {
"name":"NSPushUIPresence",
"helpText":"",
"returnTypeString":"void",
diff --git a/Northstar.Custom/mod.json b/Northstar.Custom/mod.json
index 5fcdb0fd1..22f9c45fd 100644
--- a/Northstar.Custom/mod.json
+++ b/Northstar.Custom/mod.json
@@ -430,6 +430,10 @@
{
"Path": "sh_northstar_http_requests.gnut",
"RunOn": "CLIENT || SERVER || UI"
+ },
+ {
+ "Path": "sh_northstar_safe_io.gnut",
+ "RunOn": "CLIENT || SERVER || UI"
}
],
diff --git a/Northstar.Custom/mod/scripts/vscripts/sh_northstar_safe_io.gnut b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_safe_io.gnut
new file mode 100644
index 000000000..f7b31cc21
--- /dev/null
+++ b/Northstar.Custom/mod/scripts/vscripts/sh_northstar_safe_io.gnut
@@ -0,0 +1,83 @@
+globalize_all_functions
+
+table< int, void functionref( string ) > pendingCallbacks
+table< int, void functionref( table ) > pendingJSONCallbacks
+table< int, void functionref() > failedCallbacks
+
+
+void function NSLoadFile( string file, void functionref( string ) onSuccess, void functionref() onFailure = null )
+{
+ int handle = NS_InternalLoadFile( file )
+
+ pendingCallbacks[handle] <- onSuccess
+ if (onFailure != null)
+ failedCallbacks[handle] <- onFailure
+}
+
+void function NSLoadJSONFile( string file, void functionref( table ) onSuccess, void functionref() onFailure = null )
+{
+ int handle = NS_InternalLoadFile( file )
+
+ pendingJSONCallbacks[handle] <- onSuccess
+ if (onFailure != null)
+ failedCallbacks[handle] <- onFailure
+}
+
+void function NSHandleLoadResult( int handle, bool success, string result )
+{
+ bool hasFailedCallback = handle in failedCallbacks
+ bool isJSONRequest = handle in pendingJSONCallbacks
+ bool isValid = isJSONRequest || handle in pendingCallbacks
+
+ if (!isValid)
+ throw "Invalid IO callback handle"
+
+ if (success)
+ {
+ if (isJSONRequest)
+ {
+ try
+ {
+ table result = DecodeJSON(result, true)
+ pendingJSONCallbacks[handle](result)
+ }
+ catch (ex)
+ {
+ print(ex)
+ // parsing failed, setting 'success' to false, since we
+ // consider this a failure.
+ success = false
+ }
+ }
+ else
+ {
+ pendingCallbacks[handle](result)
+ }
+ }
+ // don't use 'else', json might fail parsing and set 'success' to false.
+ if (!success)
+ {
+ if (hasFailedCallback)
+ failedCallbacks[handle]()
+ else
+ {
+ if (isJSONRequest)
+ pendingJSONCallbacks[handle]({})
+ else
+ pendingCallbacks[handle]("")
+ }
+ }
+
+ if (isJSONRequest)
+ delete pendingJSONCallbacks[handle]
+ else
+ delete pendingCallbacks[handle]
+
+ if (hasFailedCallback)
+ delete failedCallbacks[handle]
+}
+
+array<string> function NSGetAllFiles( string path = "" )
+{
+ return NS_InternalGetAllFiles(path)
+}