Created
August 17, 2020 16:19
-
-
Save IntelOrca/30d96975146b618b4c74865153142ccf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path="C:\Users\Ted\Documents\GitHub\openrct2\distribution\openrct2.d.ts" /> | |
var doDownloadHtml = function(url, callback) { | |
var socket = network.createSocket(); | |
var allData = ""; | |
socket.on('close', function() { | |
console.log('socket closed'); | |
callback(allData); | |
}); | |
socket.on('data', function(data) { | |
allData += data; | |
var bodyIndex = allData.indexOf("\r\n\r\n"); | |
if (bodyIndex != -1) { | |
console.log("bodyIndex: " + bodyIndex) | |
var contentLengthIndex = allData.indexOf('Content-Length: '); | |
if (contentLengthIndex != -1) { | |
console.log("contentLengthIndex: " + contentLengthIndex) | |
var contentLengthEndIndex = allData.indexOf('\r\n', contentLengthIndex); | |
console.log("contentLengthEndIndex: " + contentLengthEndIndex) | |
contentLength = parseInt(allData.substring(contentLengthIndex + 16, contentLengthEndIndex)); | |
console.log("contentLength: " + contentLength) | |
if (allData.length - bodyIndex >= contentLength) { | |
socket.destroy(); | |
} | |
} | |
} | |
}); | |
var protocolIndex = url.indexOf("http://"); | |
if (protocolIndex != -1) { | |
var host = url.substring(protocolIndex + 7); | |
var path = '/'; | |
var pathIndex = host.indexOf('/'); | |
if (pathIndex != -1) { | |
path = host.substring(pathIndex); | |
host = host.substring(0, pathIndex); | |
} | |
socket.connect(80, host, function() { | |
console.log('socket connected'); | |
socket.write('GET ' + path + ' HTTP/1.1\r\nHost: ' + host + '\r\n\r\n'); | |
}); | |
} | |
} | |
var doHttpServer = function () { | |
var server = network.createServer(); | |
server.on('connection', function (conn) { | |
// console.log("Incomming connection!"); | |
var allData = ''; | |
conn.on('data', function(data) { | |
allData += data; | |
if (allData.endsWith('\r\n\r\n')) | |
{ | |
var lines = allData.split('\n'); | |
if (lines.length > 0) | |
{ | |
var firstLine = lines[0]; | |
var parts = firstLine.split(' '); | |
if (parts.length > 2) | |
{ | |
console.log(parts[0] + ' ' + parts[1]); | |
} | |
} | |
var html = '<!DOCTYPE html><head><title>OpenRCT2</title></head><body><p>Current cash: ' + park.cash + '</p></body></html>'; | |
conn.write( | |
'HTTP/1.1 200 OK\r\n' + | |
'Server: OpenRCT2 plugin' + | |
'Content-Length: ' + html.length + | |
'Content-Type: text/html\r\n\r\n' + | |
html); | |
conn.end(); | |
} | |
}); | |
conn.on('close', function(hadError) { | |
// console.log('Connection closed.'); | |
}); | |
}); | |
server.listen(80); | |
console.log("listening on port 80..."); | |
console.log(server.listening); | |
}; | |
var main = function () { | |
doHttpServer(); | |
// doDownloadHtml("http://intelorca.co.uk", function(html) { | |
// console.log(html); | |
// }); | |
} | |
registerPlugin({ | |
name: 'Test', | |
version: '1.0', | |
authors: ['Ted'], | |
type: 'remote', | |
licence: 'MIT', | |
main: main | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment