Skip to content

Adding Internet stuff. WebSocket is now apart of CNE. #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
02acada
Added WebSocketUtil (and intenet folder)
ItsLJcool Jan 4, 2025
b8d3132
WebSocketUtil document
ItsLJcool Jan 4, 2025
4a8af03
Added the WebSockets and made `internet` lib
ItsLJcool Jan 4, 2025
ddf1a48
added "hx.ws" as an import (wont work neo but ok)
ItsLJcool Jan 4, 2025
294fe6b
fixed indentation
ItsLJcool Jan 4, 2025
f1e8d97
Fixed some issues
ItsLJcool Jan 4, 2025
62f1569
Added `WebSocketPacket` to be used with `WebSocketUtil`.
ItsLJcool Jan 5, 2025
50aeecd
clarification
ItsLJcool Jan 5, 2025
23b4f59
allow scripting the entire raw websocket api
NexIsDumb Jan 5, 2025
4714cbf
reformatted the `WebSocketPacket` to use `Serializer` instead of a lo…
ItsLJcool Jan 5, 2025
218fd23
Merge branch 'internet-utils' of https://github.com/ItsLJcool/Codenam…
ItsLJcool Jan 5, 2025
8c8f532
moving to .system.net instead of .internet
NexIsDumb Jan 5, 2025
bc44bbe
"The => should only be in the not empty part" - Neo 2025
ItsLJcool Jan 5, 2025
13d5ab7
Added Discord Meta Data (Username, Global name and Premium Type only)
ItsLJcool Jan 6, 2025
a6fad0a
updated `WebSocketPacket` to actually you know do its job and not cra…
ItsLJcool Jan 6, 2025
6237a6d
erm i forgor about a trace line
ItsLJcool Jan 6, 2025
254e59e
No longer sends Discord Metadata if your not connected.
ItsLJcool Jan 7, 2025
05acc9c
Merge branch 'CodenameCrew:main' into internet-utils
ItsLJcool Jan 7, 2025
d52a350
Added `ServerPacketData`. Messages recieved by the WebSocket will eit…
ItsLJcool Jan 8, 2025
a5bb1fc
`closeOnError` is default since you can handle your errors by default.
ItsLJcool Jan 10, 2025
4315e38
Merge branch 'CodenameCrew:main' into internet-utils
ItsLJcool Jan 11, 2025
a82d824
Added Link in comments to Server Template
ItsLJcool Jan 11, 2025
a80578b
Connecting to WebSocket is now Async. Can be disabled.
ItsLJcool Jan 11, 2025
3434bcd
forgor about stuff, also added Threaded Packet Sending (disabled by d…
ItsLJcool Jan 11, 2025
7c480d2
oop
ItsLJcool Jan 11, 2025
13afcd1
I forgot the `onmessage` is being sent from a thread, so updated the …
ItsLJcool Jan 11, 2025
ab0e1c9
MORE!!!
ItsLJcool Jan 11, 2025
eecf1f3
testing packet storing
ItsLJcool Jan 11, 2025
ce99c1c
might fix compiling issue?
ItsLJcool Jan 12, 2025
bd9e2ea
Merge remote-tracking branch 'upstream/main' into internet-utils
ItsLJcool Mar 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<git name="flxanimate" url="https://github.com/CodenameCrew/cne-flxanimate" />
<git name="hxdiscord_rpc" url="https://github.com/CodenameCrew/cne-hxdiscord_rpc" />
<lib name="hxvlc" version="1.9.3" skipDeps="true" />
<lib name="hxWebSockets" version="1.4.0" />

<!-- Documentation and other features -->
<git name="away3d" url="https://github.com/CodenameCrew/away3d" />
Expand Down
2 changes: 2 additions & 0 deletions project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@

<haxelib name="flixel" />
<haxelib name="flixel-addons" />

<haxelib name="hxWebSockets" />

<haxelib name="hxvlc" if="VIDEO_CUTSCENES" />
<haxelib name="away3d" if="THREE_D_SUPPORT" />
Expand Down
4 changes: 4 additions & 0 deletions source/funkin/backend/system/MainState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import funkin.menus.TitleState;
import funkin.menus.BetaWarningState;
import funkin.backend.chart.EventsData;
import flixel.FlxState;
import funkin.backend.system.net.WebSocketPacket;

/**
* Simple state used for loading the game
Expand All @@ -21,6 +22,9 @@ class MainState extends FlxState {
Main.loadGameSettings();
initiated = true;

// Resetting to default because I doubt any modding the engine will reset it lmao
WebSocketPacket.packetTypes = WebSocketPacket.default_packetTypes;

#if sys
CoolUtil.deleteFolder('./.temp/'); // delete temp folder
#end
Expand Down
5 changes: 3 additions & 2 deletions source/funkin/backend/system/macros/Macros.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Macros {
#if VIDEO_CUTSCENES "hxvlc.flixel", "hxvlc.openfl", #end
#if NAPE_ENABLED "nape", "flixel.addons.nape", #end
// BASE HAXE
"DateTools", "EReg", "Lambda", "StringBuf", "haxe.crypto", "haxe.display", "haxe.exceptions", "haxe.extern", "scripting"
"DateTools", "EReg", "Lambda", "StringBuf", "haxe.crypto", "haxe.display", "haxe.exceptions", "haxe.extern", "scripting",
])
Compiler.include(inc);
Compiler.include(inc);

var isHl = Context.defined("hl");

Expand All @@ -40,6 +40,7 @@ class Macros {
}
}

Compiler.include("hx.ws", ["hx.ws.cs", "hx.ws.java", "hx.ws.nodejs"]);
Compiler.include("funkin", [#if !UPDATE_CHECKING 'funkin.backend.system.updating' #end]);
}

Expand Down
150 changes: 150 additions & 0 deletions source/funkin/backend/system/net/WebSocketPacket.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package funkin.backend.system.net;

import funkin.backend.assets.ModsFolder;
import funkin.backend.system.macros.GitCommitMacro;
import funkin.backend.utils.DiscordUtil;

import haxe.Serializer;

import Date;
import StringBuf;
import String;
import Type;

/**
* A data object that can be customized for `WebSocketUtil` to send data to the server.
* You will need to handle the custom packet on your server yourself.
*
* GENERAL WEBSOCKET WARNING: Sending data to the server on `update` SHOULD NEVER BE DONE!!
* It will be slow and generally not a good idea. It might overload the server and cause unforseen issues.
*
* Why use a packet class instead of sending your own data? Well this Serializes the data and handles it for you, so all you do is just send the class in the `WebSocketUtil.send` and thats it.
**/
class WebSocketPacket {

/**
* Packet Types that can be gathered from the server. Used by `WebSocketUtil`
* If your server doesn't use the Template ItsLJcool made, then you add your own here.
**/
public static var packetTypes:Map<String, Dynamic> = [
"haxe" => {params: "!HXP", none: "!HXp"},
"javascript" => {params: "!JSP", none: "!JSp"},
"js" => {params: "!JSP", none: "!JSp"},
];

@:dox(hide)
public static var default_packetTypes(default, never):Map<String, Dynamic> = [
"haxe" => {params: "!HXP", none: "!HXp"},
"javascript" => {params: "!JSP", none: "!JSp"},
"js" => {params: "!JSP", none: "!JSp"},
];

/**
* Just normal data that is being held for the packet to get stringified.
**/
private var packetData(default, set):Dynamic = {};
private function set_packetData(value:Dynamic):Dynamic {
if (value == null) return {};
if (value is String) value = haxe.Json.parse(value);
return this.packetData = value;
}

/**
* The name of the event the server handles.
* If null it won't be added in the packet.
**/
public var packetEventName(default, set):String;
private function set_packetEventName(value:Null<String>):String {
if (value == null) return this.packetEventName = "";
return this.packetEventName = value;
}

@:dox(hide) private var add_meta_data:Bool = true;

/**
* @param packetName The name of the event the server handles.
* @param packetData The data that is being sent to the server. Can also be a stringified JSON.
* @param add_meta_data If true, adds metadata to the packet. This is useful for data like the time it was sent,
**/
public function new(packetName:Null<String>, ?packetData:Null<Dynamic>, ?_add_meta_data:Bool = true) {
this.packetEventName = packetName;
this.add_meta_data = _add_meta_data;

this.packetData = packetData;

if (this.add_meta_data) {
try {
if (ModsFolder.currentModFolder != null) this.packetData.__mod = ModsFolder.currentModFolder;
this.packetData.__commitHash = GitCommitMacro.commitHash; // for checking outdated action builds on the server. its gonna be peak trust.
// if Discord isn't active, dont send the metadata
if (DiscordUtil.ready) this.packetData.__discord = { username: DiscordUtil.user.username, globalName: DiscordUtil.user.globalName, premiumType: DiscordUtil.user.premiumType };
} catch (e:Dynamic) {
trace("Error adding metadata to packet: " + e);
}
}

}

/**
* Checks if the packet has the field.
* @param field The field to check for
* @return If the packet has the field.
**/
public function exists(field:String):Bool {
return Reflect.hasField(this.packetData, field);
}

/**
* Gets the packet field.
* @param field The field to get the value.
* @return the value of the field.
**/
public function get(field:String):Dynamic {
return Reflect.field(this.packetData, field);
}

/**
* Sets a value to the packet.
* @param field The field to get the value.
* @param value The value to set.
* @return the packet data as a JSON structure.
**/
public function set(field:String, value:Dynamic) {
Reflect.setField(this.packetData, field, value);
return this.packetData;
}

/**
* Converts the packet to a string. Uses `Serializer` to convert the packet to a data string
* @return The packet as a string.
**/
public function toString():String {
var buffer = new StringBuf();

// if no name is associated with packet, just serialize the data
if (this.packetEventName.trim() != "") {
buffer.add('!HXP');
buffer.add(this.packetEventName);
buffer.add('=>');
} else {
buffer.add('!HXp');
}

if (add_meta_data) this.packetData.__timestamp = Date.now();

var cerial = new Serializer();
if (this.packetData != {}) cerial.serialize(this.packetData);
return '${buffer.toString()}${cerial.toString()}';
}

public static function isServerPacket(data:Dynamic):Bool {
if ((data is ServerPacketData)) return true;
return false;
}
}

@:structInit
class ServerPacketData {
public var name:String;
public var data:Dynamic;
}
Loading