aboutsummaryrefslogtreecommitdiff
path: root/utils/networkUtils.js
diff options
context:
space:
mode:
Diffstat (limited to 'utils/networkUtils.js')
-rw-r--r--utils/networkUtils.js157
1 files changed, 157 insertions, 0 deletions
diff --git a/utils/networkUtils.js b/utils/networkUtils.js
new file mode 100644
index 0000000..51090b2
--- /dev/null
+++ b/utils/networkUtils.js
@@ -0,0 +1,157 @@
+if(!global.networkUtilsThingSoopy){
+
+ let jURL = Java.type("java.net.URL")
+ let jStandardCharsets = Java.type("java.nio.charset.StandardCharsets")
+ let jCollectors = Java.type("java.util.stream.Collectors")
+ let jBufferedReader = Java.type("java.io.BufferedReader")
+ let jInputStreamReader = Java.type("java.io.InputStreamReader")
+
+ function getUrlContent(theUrl, {userAgent="Mozilla/5.0", includeConnection=false}={}){
+
+ if(global.soopyv2loggerthing){
+ global.soopyv2loggerthing.logMessage("Loading API: " + theUrl, 4)
+ }
+
+ let conn = new jURL(theUrl).openConnection()
+ conn.setRequestProperty("User-Agent", userAgent)
+
+ let stringData
+
+ if(conn.getResponseCode() < 400){
+ stringData = new jBufferedReader(
+ new jInputStreamReader(conn.getInputStream(), jStandardCharsets.UTF_8))
+ .lines()
+ .collect(jCollectors.joining("\n"));
+
+ conn.getInputStream().close()
+ }else{
+ stringData = new jBufferedReader(
+ new jInputStreamReader(conn.getErrorStream(), jStandardCharsets.UTF_8))
+ .lines()
+ .collect(jCollectors.joining("\n"));
+
+ conn.getErrorStream().close()
+ }
+
+ if(includeConnection){
+ return {stringData, connection: conn}
+ }
+
+ return stringData
+ }
+
+ function fetch(url, options={userAgent: "Mozilla/5.0"}){
+ let loadedConnection = undefined
+ let loadedString = undefined
+ let loadedJSON = undefined
+
+ let ret = {
+ sync(){
+ if(loadedString === undefined){
+ options.includeConnection = true
+
+ let data = getUrlContent(url, options)
+ loadedString = data.stringData
+ loadedConnection = data.connection
+ }
+
+ return
+ },
+ async(callback){
+ if(!callback){
+ callback = ()=>{}
+ }
+
+ if(loadedString === undefined){
+ options.includeConnection = true
+
+ pendingRequests.push({
+ callback: (data)=>{
+ loadedString = data.stringData
+ loadedConnection = data.connection
+ callback()
+ },
+ url: url,
+ options: options
+ })
+ }else{
+ callback()
+ }
+ },
+ text: (callback)=>{
+ if(!callback){
+ ret.sync()
+
+ return this.loadedString
+ }
+
+ ret.async(()=>{
+ callback(loadedString)
+ })
+ },
+ json: (callback)=>{
+ if(!callback){
+ if(loadedJSON === undefined){
+ loadedJSON = JSON.parse(ret.text())
+ }
+
+ return loadedJSON
+ }
+
+ ret.text(data=>{
+ callback(JSON.parse(data))
+ })
+ },
+ responseCode: (callback)=>{
+ if(!callback){
+ if(loadedConnection === undefined){
+ ret.text()
+ }
+
+ return loadedConnection.getResponseCode()
+ }
+
+ ret.text(data=>{
+ callback(loadedConnection.getResponseCode())
+ })
+ }
+ }
+ return ret
+ }
+
+ let pendingRequests = []
+ let pendingResolves = []
+ let running = true
+
+ new Thread(()=>{
+ while(running){
+ while(pendingRequests.length > 0){
+ let req = pendingRequests.shift()
+
+ let data = getUrlContent(req.url, req.options)
+
+ pendingResolves.push([req.callback, data])
+ }
+ Thread.sleep(100)
+ }
+ }).start()
+
+ register("gameUnload", ()=>{
+ running = false
+ })
+
+ register("tick", ()=>{
+ while(pendingResolves.length > 0){
+ let [callback, data] = pendingResolves.shift()
+
+ callback(data)
+ }
+ })
+
+ global.networkUtilsThingSoopy = {
+ getUrlContent: getUrlContent,
+ fetch: fetch
+ }
+}
+
+module.exports = global.networkUtilsThingSoopy