From 24883547ac57dadf3d70f72b68bb8639d45512c4 Mon Sep 17 00:00:00 2001 From: Lucia Ceionia Date: Sat, 2 Nov 2024 18:49:03 -0500 Subject: [PATCH] refactor; 0.2 features --- Godot/mods/Lucy.LucysTools/lucys_menu.gd | 16 +- Godot/mods/Lucy.LucysTools/lucys_menu.tscn | 54 +- Godot/mods/Lucy.LucysTools/main.gd | 34 +- LucysTools/Config.cs | 7 - LucysTools/Mod.cs | 1443 +++++++++++--------- 5 files changed, 865 insertions(+), 689 deletions(-) delete mode 100644 LucysTools/Config.cs diff --git a/Godot/mods/Lucy.LucysTools/lucys_menu.gd b/Godot/mods/Lucy.LucysTools/lucys_menu.gd index 9a2e2cf..5d82f15 100644 --- a/Godot/mods/Lucy.LucysTools/lucys_menu.gd +++ b/Godot/mods/Lucy.LucysTools/lucys_menu.gd @@ -12,9 +12,20 @@ func setup(manager): get_node("%lucy_bpackets").value = manager.bulk_packets get_node("%lucy_binterval").value = manager.bulk_interval get_node("%lucy_finterval").value = manager.full_interval + + update() + +func update(): + get_node("%lucy_srv_allow_bbcode").text = "Yes" if MANAGER.srv_allow_bbcode else "No" func _ready(): print("[LUCY] Menu Ready") + + get_node("%lucy_bbcode").disabled = MANAGER.host_required and not Network.GAME_MASTER + get_node("%lucy_raincloud").disabled = (MANAGER.host_required and not Network.GAME_MASTER) or not MANAGER.ingame + get_node("%lucy_meteor").disabled = (MANAGER.host_required and not Network.GAME_MASTER) or not MANAGER.ingame + get_node("%lucy_freezerain").disabled = (MANAGER.host_required and not Network.GAME_MASTER) or not MANAGER.ingame + get_node("%lucy_clearrain").disabled = (MANAGER.host_required and not Network.GAME_MASTER) or not MANAGER.ingame func _input(event): if event is InputEventKey and event.scancode == KEY_F5 && event.pressed: @@ -83,4 +94,7 @@ func _on_lucy_clearrain_pressed(): for cloud in get_tree().get_nodes_in_group("raincloud"): cloud._deinstantiate(true) - +func _on_lucy_clearchat_pressed(): + Network.GAMECHAT = "" + Network.LOCAL_GAMECHAT = "" + Network.emit_signal("_chat_update") diff --git a/Godot/mods/Lucy.LucysTools/lucys_menu.tscn b/Godot/mods/Lucy.LucysTools/lucys_menu.tscn index 8417bfe..056b316 100644 --- a/Godot/mods/Lucy.LucysTools/lucys_menu.tscn +++ b/Godot/mods/Lucy.LucysTools/lucys_menu.tscn @@ -58,10 +58,28 @@ margin_left = 509.0 margin_right = 585.0 margin_bottom = 40.0 -[node name="HFlowContainer4" type="HFlowContainer" parent="PanelContainer/VBoxContainer"] +[node name="HFlowContainer3" type="HFlowContainer" parent="PanelContainer/VBoxContainer"] margin_top = 70.0 margin_right = 786.0 -margin_bottom = 94.0 +margin_bottom = 84.0 +rect_pivot_offset = Vector2( -141, -49 ) + +[node name="Label" type="Label" parent="PanelContainer/VBoxContainer/HFlowContainer3"] +margin_right = 196.0 +margin_bottom = 14.0 +text = "Server Allows BBCode in Chat: " + +[node name="lucy_srv_allow_bbcode" type="Label" parent="PanelContainer/VBoxContainer/HFlowContainer3"] +unique_name_in_owner = true +margin_left = 200.0 +margin_right = 218.0 +margin_bottom = 14.0 +text = "No" + +[node name="HFlowContainer4" type="HFlowContainer" parent="PanelContainer/VBoxContainer"] +margin_top = 88.0 +margin_right = 786.0 +margin_bottom = 112.0 rect_pivot_offset = Vector2( -141, -49 ) [node name="Label" type="Label" parent="PanelContainer/VBoxContainer/HFlowContainer4"] @@ -79,9 +97,9 @@ expand_to_text_length = true placeholder_text = "Name" [node name="HFlowContainer5" type="HFlowContainer" parent="PanelContainer/VBoxContainer"] -margin_top = 98.0 +margin_top = 116.0 margin_right = 786.0 -margin_bottom = 122.0 +margin_bottom = 140.0 rect_pivot_offset = Vector2( -141, -49 ) [node name="Label" type="Label" parent="PanelContainer/VBoxContainer/HFlowContainer5"] @@ -99,42 +117,53 @@ expand_to_text_length = true placeholder_text = "Message" [node name="HFlowContainer2" type="HFlowContainer" parent="PanelContainer/VBoxContainer"] -margin_top = 126.0 +margin_top = 144.0 margin_right = 786.0 -margin_bottom = 146.0 +margin_bottom = 164.0 [node name="lucy_raincloud" type="Button" parent="PanelContainer/VBoxContainer/HFlowContainer2"] +unique_name_in_owner = true margin_right = 118.0 margin_bottom = 20.0 text = "Spawn Raincloud" [node name="lucy_meteor" type="Button" parent="PanelContainer/VBoxContainer/HFlowContainer2"] +unique_name_in_owner = true margin_left = 122.0 margin_right = 224.0 margin_bottom = 20.0 text = "Spawn Meteor" [node name="lucy_freezerain" type="Button" parent="PanelContainer/VBoxContainer/HFlowContainer2"] +unique_name_in_owner = true margin_left = 228.0 margin_right = 314.0 margin_bottom = 20.0 text = "Freeze Rain" [node name="lucy_clearrain" type="Button" parent="PanelContainer/VBoxContainer/HFlowContainer2"] +unique_name_in_owner = true margin_left = 318.0 margin_right = 393.0 margin_bottom = 20.0 text = "Clear Rain" +[node name="lucy_clearchat" type="Button" parent="PanelContainer/VBoxContainer/HFlowContainer2"] +margin_left = 397.0 +margin_right = 473.0 +margin_bottom = 20.0 +hint_tooltip = "Clears game chat (for you only)" +text = "Clear Chat" + [node name="HSeparator2" type="HSeparator" parent="PanelContainer/VBoxContainer"] -margin_top = 150.0 +margin_top = 168.0 margin_right = 786.0 -margin_bottom = 154.0 +margin_bottom = 172.0 [node name="HSplitContainer" type="HSplitContainer" parent="PanelContainer/VBoxContainer"] -margin_top = 158.0 +margin_top = 176.0 margin_right = 786.0 -margin_bottom = 182.0 +margin_bottom = 200.0 split_offset = 100 [node name="HFlowContainer3" type="HFlowContainer" parent="PanelContainer/VBoxContainer/HSplitContainer"] @@ -179,9 +208,9 @@ rounded = true allow_greater = true [node name="HSplitContainer2" type="HSplitContainer" parent="PanelContainer/VBoxContainer"] -margin_top = 186.0 +margin_top = 204.0 margin_right = 786.0 -margin_bottom = 210.0 +margin_bottom = 228.0 split_offset = 100 [node name="HFlowContainer3" type="HFlowContainer" parent="PanelContainer/VBoxContainer/HSplitContainer2"] @@ -233,6 +262,7 @@ allow_greater = true [connection signal="pressed" from="PanelContainer/VBoxContainer/HFlowContainer2/lucy_meteor" to="." method="_on_lucy_meteor_pressed"] [connection signal="pressed" from="PanelContainer/VBoxContainer/HFlowContainer2/lucy_freezerain" to="." method="_on_lucy_freezerain_pressed"] [connection signal="pressed" from="PanelContainer/VBoxContainer/HFlowContainer2/lucy_clearrain" to="." method="_on_lucy_clearrain_pressed"] +[connection signal="pressed" from="PanelContainer/VBoxContainer/HFlowContainer2/lucy_clearchat" to="." method="_on_lucy_clearchat_pressed"] [connection signal="value_changed" from="PanelContainer/VBoxContainer/HSplitContainer/HFlowContainer3/lucy_fpackets" to="." method="_on_lucy_fpackets_value_changed"] [connection signal="value_changed" from="PanelContainer/VBoxContainer/HSplitContainer/HFlowContainer/lucy_bpackets" to="." method="_on_lucy_bpackets_value_changed"] [connection signal="value_changed" from="PanelContainer/VBoxContainer/HSplitContainer2/HFlowContainer3/lucy_binterval" to="." method="_on_lucy_binterval_value_changed"] diff --git a/Godot/mods/Lucy.LucysTools/main.gd b/Godot/mods/Lucy.LucysTools/main.gd index 147d137..4182ed9 100644 --- a/Godot/mods/Lucy.LucysTools/main.gd +++ b/Godot/mods/Lucy.LucysTools/main.gd @@ -2,6 +2,8 @@ extends Node const LUCYS_MENU_SCENE = preload("res://mods/Lucy.LucysTools/lucys_menu.tscn") +var host_required = true + var lucys_menu = null onready var root = get_tree().root @@ -14,6 +16,8 @@ var bulk_packets = 200 setget set_bulk_packets var bulk_interval = 1 setget set_bulk_interval var full_interval = 5 setget set_full_interval +var srv_allow_bbcode = false setget set_srv_bbcode + # Patched Network vars # var LUCY_PACKETS_READ = 0 # var LUCY_BULK_FULL_TIMER = 0 @@ -31,7 +35,12 @@ func set_punchback(punchback): do_punchback = punchback func set_bbcode(bbcode): allow_bbcode = bbcode - Network.LUCY_CHAT_BBCODE = bbcode + if Network.GAME_MASTER or not host_required: self.srv_allow_bbcode = bbcode +func set_srv_bbcode(bbcode): + if Network.GAME_MASTER and not Network.PLAYING_OFFLINE: send_server_sync_actor() + srv_allow_bbcode = bbcode + Network.LUCY_CHAT_BBCODE = bbcode if host_required else allow_bbcode + if lucys_menu != null: lucys_menu.update() func set_server_name(name): custom_server_name = name Network.LUCY_SRV_NAME = name @@ -58,6 +67,21 @@ func _ready(): root.connect("child_entered_tree", self, "_on_enter") Network.connect("_new_player_join", self, "new_player") PlayerData.connect("_punched", self, "punched") + Network.connect("_instance_actor", self, "_instance_actor") + + +func send_server_sync_actor(to = "peers"): + if not Network.GAME_MASTER: return + var dict = {"actor_type": "lucy_fake_actor", "at": Vector3.ZERO, "zone": "", "actor_id": 0, "creator_id": Network.STEAM_ID, "data": { + "allow_bbcode": allow_bbcode + }} + Network._send_P2P_Packet({"type": "instance_actor", "params": dict}, to, 2) + +func _instance_actor(dict, sender_id): + if dict["actor_type"] != "lucy_fake_actor": return + if sender_id != Network.KNOWN_GAME_MASTER or Network.GAME_MASTER: return + var data = dict["data"] + self.srv_allow_bbcode = data["allow_bbcode"] func get_player() -> Actor: for p in get_tree().get_nodes_in_group("player"): @@ -84,6 +108,7 @@ func new_player(id): if server_join_message.empty() or not Network.GAME_MASTER: return print("[LUCY] sending join message") Network._send_message(server_join_message) + send_server_sync_actor(str(id)) func _on_enter(node: Node): if node.name == "main_menu": @@ -95,6 +120,9 @@ func _on_enter(node: Node): lucys_menu = LUCYS_MENU_SCENE.instance() node.add_child(lucys_menu) ingame = true + # retrigger setter + self.srv_allow_bbcode = false + self.allow_bbcode = allow_bbcode lucys_menu.setup(self) func load_settings(): @@ -113,6 +141,7 @@ func load_settings(): self.bulk_packets = result.bulk_packets self.bulk_interval = result.bulk_interval self.full_interval = result.full_interval + self.host_required = result.host_required func save_settings(): print("[LUCY] Saving settings") @@ -124,7 +153,8 @@ func save_settings(): "frame_packets": frame_packets, "bulk_packets": bulk_packets, "bulk_interval": bulk_interval, - "full_interval": full_interval + "full_interval": full_interval, + "host_required": host_required } var file = File.new() if file.open(OS.get_executable_path().get_base_dir().plus_file("GDWeave/configs/LucysTools.json"),File.WRITE) == OK: diff --git a/LucysTools/Config.cs b/LucysTools/Config.cs deleted file mode 100644 index 80f8e14..0000000 --- a/LucysTools/Config.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Text.Json.Serialization; - -namespace LucysTools; - -public class Config { - [JsonInclude] public bool SomeSetting = true; -} diff --git a/LucysTools/Mod.cs b/LucysTools/Mod.cs index 8b46c2d..17d8a3a 100644 --- a/LucysTools/Mod.cs +++ b/LucysTools/Mod.cs @@ -8,144 +8,181 @@ namespace LucysTools; public class Mod : IMod { public static IModInterface ModInterface; - public Config Config; public Mod(IModInterface modInterface) { modInterface.Logger.Information("Lucy was here :3"); ModInterface = modInterface; - modInterface.RegisterScriptMod(new LucysNetFixes()); modInterface.RegisterScriptMod(new LucysChatChanges()); + modInterface.RegisterScriptMod(new LucysNetFixes()); } - public void Dispose() { - // Cleanup anything you do here - } + public void Dispose(){} +} + +public record CodeChange { + public required String name; + public required Func[] multitoken_prefix; + public required Token[] code_to_add; } public class LucysChatChanges : IScriptMod { bool IScriptMod.ShouldRun(string path) => path == "res://Scenes/HUD/playerhud.gdc"; - Func[] start_sendmsg = { - t => t.Type == TokenType.PrFunction, - t => t is IdentifierToken {Name: "_send_message"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.ParenthesisClose, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline, - }; - Func[] sendmsg_openbracket = { - t => t is IdentifierToken {Name: "color"}, - t => t.Type == TokenType.Period, - t => t is IdentifierToken {Name: "to_html"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t.Type == TokenType.ParenthesisClose, - t => t.Type == TokenType.Newline, - t => t.Type == TokenType.Newline, - t => t.Type == TokenType.Newline, - }; - Func[] sendmsg_closebracket = { - t => t is IdentifierToken {Name: "text"}, - t => t.Type == TokenType.OpAssign, - t => t is IdentifierToken {Name: "text"}, - t => t.Type == TokenType.Period, - t => t is IdentifierToken {Name: "replace"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t is ConstantToken {Value:StringVariant{Value: "["}}, - t => t.Type == TokenType.Comma, - t => t.Type == TokenType.Constant, - t => t.Type == TokenType.ParenthesisClose, - t => t.Type == TokenType.Newline, - }; - Func[] sendmsg_breakdownbracket = { - t => t.Type == TokenType.CfElif, - t => t is IdentifierToken {Name: "line"}, - t => t.Type == TokenType.Period, - t => t is IdentifierToken {Name: "begins_with"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t is ConstantToken {Value:StringVariant{Value: "["}}, - t => t.Type == TokenType.ParenthesisClose, + CodeChange[] changes = { + new CodeChange { + name = "_send_message literal input", + // func _send_message(text): + // END + multitoken_prefix = new Func[] { + t => t.Type == TokenType.PrFunction, + t => t is IdentifierToken {Name: "_send_message"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.ParenthesisClose, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline, + }, + // if text.begins_with('%') and Network.LUCY_CHAT_BBCODE): + // text = text.trim_prefix('%') + // Network._send_message(text, chat_local) + // return + // END + code_to_add = new Token[] { + new Token(TokenType.CfIf), + new IdentifierToken("text"), + new Token(TokenType.Period), + new IdentifierToken("begins_with"), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("%")), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.OpAnd), + new IdentifierToken("Network"), + new Token(TokenType.Period), + new IdentifierToken("LUCY_CHAT_BBCODE"), + new Token(TokenType.Colon), + new Token(TokenType.Newline,2), + new IdentifierToken("text"), + new Token(TokenType.OpAssign), + new IdentifierToken("text"), + new Token(TokenType.Period), + new IdentifierToken("trim_prefix"), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("%")), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline,2), + new IdentifierToken("Network"), + new Token(TokenType.Period), + new IdentifierToken("_send_message"), + new Token(TokenType.ParenthesisOpen), + new IdentifierToken("text"), + new Token(TokenType.Comma), + new IdentifierToken("chat_local"), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline,2), + new Token(TokenType.CfReturn), + new Token(TokenType.Newline,1), + } + }, + + new CodeChange { + name = "[ filter", + // color.to_html() + // + // + // END + multitoken_prefix = new Func[] { + t => t is IdentifierToken {Name: "color"}, + t => t.Type == TokenType.Period, + t => t is IdentifierToken {Name: "to_html"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.ParenthesisClose, + t => t.Type == TokenType.Newline, + t => t.Type == TokenType.Newline, + t => t.Type == TokenType.Newline, + }, + // if not Network.LUCY_CHAT_BBCODE: END + code_to_add = new Token[] { + new Token(TokenType.CfIf), + new Token(TokenType.OpNot), + new IdentifierToken("Network"), + new Token(TokenType.Period), + new IdentifierToken("LUCY_CHAT_BBCODE"), + new Token(TokenType.Colon), + } + }, + + new CodeChange { + name = "] filter", + // text = text.replace('[','') + // END + multitoken_prefix = new Func[] { + t => t is IdentifierToken {Name: "text"}, + t => t.Type == TokenType.OpAssign, + t => t is IdentifierToken {Name: "text"}, + t => t.Type == TokenType.Period, + t => t is IdentifierToken {Name: "replace"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t is ConstantToken {Value:StringVariant{Value: "["}}, + t => t.Type == TokenType.Comma, + t => t.Type == TokenType.Constant, + t => t.Type == TokenType.ParenthesisClose, + t => t.Type == TokenType.Newline, + }, + // if not Network.LUCY_CHAT_BBCODE: END + code_to_add = new Token[] { + new Token(TokenType.CfIf), + new Token(TokenType.OpNot), + new IdentifierToken("Network"), + new Token(TokenType.Period), + new IdentifierToken("LUCY_CHAT_BBCODE"), + new Token(TokenType.Colon), + } + }, + + new CodeChange { + name = "breakdown [ filter", + // elif line.begins_with('[') END + multitoken_prefix = new Func[] { + t => t.Type == TokenType.CfElif, + t => t is IdentifierToken {Name: "line"}, + t => t.Type == TokenType.Period, + t => t is IdentifierToken {Name: "begins_with"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t is ConstantToken {Value:StringVariant{Value: "["}}, + t => t.Type == TokenType.ParenthesisClose, + }, + // and false END + code_to_add = new Token[] { + new Token(TokenType.OpAnd), + new ConstantToken(new BoolVariant(false)), + } + }, }; IEnumerable IScriptMod.Modify(string path, IEnumerable tokens) { - var start_sendmsg_waiter = new MultiTokenWaiter(start_sendmsg); - var sendmsg_openbracket_waiter = new MultiTokenWaiter(sendmsg_openbracket); - var sendmsg_closebracket_waiter = new MultiTokenWaiter(sendmsg_closebracket); - var sendmsg_breakdownbracket_waiter = new MultiTokenWaiter(sendmsg_breakdownbracket); + var pending_changes = changes + .Select(c => (c, new MultiTokenWaiter(c.multitoken_prefix))) + .ToList(); + // I'm sure there's a better way to do this + // with list comprehension stuff, but my + // C# is too rusty foreach (var token in tokens) { - if (start_sendmsg_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Chat mod 0..."); - yield return token; + var had_change = false; + foreach ((var change, var waiter) in pending_changes) { + if (waiter.Check(token)) { + Mod.ModInterface.Logger.Information($"Adding Lucy Chat mod {change.name}"); - // if text.beings_with('%'): - // text = text.trim_prefix('%') - // Network._send_message(text,chat_local) - // return - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("text"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("begins_with"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("%")); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.OpAnd); - yield return new IdentifierToken("Network"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("LUCY_CHAT_BBCODE"); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.Newline,2); - yield return new IdentifierToken("text"); - yield return new Token(TokenType.OpAssign); - yield return new IdentifierToken("text"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("trim_prefix"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("%")); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline,2); - yield return new IdentifierToken("Network"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("_send_message"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new IdentifierToken("text"); - yield return new Token(TokenType.Comma); - yield return new IdentifierToken("chat_local"); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline,2); - yield return new Token(TokenType.CfReturn); - yield return new Token(TokenType.Newline,1); - } else if (sendmsg_openbracket_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Chat mod 1..."); - yield return token; + yield return token; + foreach (var t in change.code_to_add) yield return t; - yield return new Token(TokenType.CfIf); - yield return new Token(TokenType.OpNot); - yield return new IdentifierToken("Network"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("LUCY_CHAT_BBCODE"); - yield return new Token(TokenType.Colon); - } else if (sendmsg_closebracket_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Chat mod 2..."); - yield return token; - - yield return new Token(TokenType.CfIf); - yield return new Token(TokenType.OpNot); - yield return new IdentifierToken("Network"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("LUCY_CHAT_BBCODE"); - yield return new Token(TokenType.Colon); - } else if (sendmsg_breakdownbracket_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Chat mod 3..."); - yield return token; - - yield return new Token(TokenType.OpAnd); - yield return new ConstantToken(new BoolVariant(false)); - } else { - yield return token; + had_change = true; + break; + } } + if (!had_change) yield return token; } } } @@ -153,576 +190,648 @@ public class LucysChatChanges : IScriptMod public class LucysNetFixes : IScriptMod { bool IScriptMod.ShouldRun(string path) => path == "res://Scenes/Singletons/SteamNetwork.gdc"; - Func[] original_kick = { - //t => t.Type == TokenType.Constant && t.AssociatedData == 158, // kick - t => t is ConstantToken {Value:StringVariant{Value: "kick"}}, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline - }; + CodeChange[] changes = { + new CodeChange { + name = "send_message channel 2", + // MESSAGE_ZONE, "zone_owner": PlayerData.player_saved_zone_owner}, "peers", 2) + multitoken_prefix = new Func[] { + t => t is IdentifierToken {Name: "MESSAGE_ZONE"}, + t => t.Type == TokenType.Comma, + t => t is ConstantToken {Value:StringVariant{Value: "zone_owner"}}, + t => t.Type == TokenType.Colon, + t => t is IdentifierToken {Name: "PlayerData"}, + t => t.Type == TokenType.Period, + t => t is IdentifierToken {Name: "player_saved_zone_owner"}, + t => t.Type == TokenType.CurlyBracketClose, + t => t.Type == TokenType.Comma, + t => t is ConstantToken {Value:StringVariant{Value: "peers"}}, + t => t.Type == TokenType.Comma, + t => t is ConstantToken {Value:IntVariant{Value: 2}}, + }, + // , 2 END + code_to_add = new Token[] { + new Token(TokenType.Comma), + new ConstantToken(new IntVariant(2)), + } + }, - Func[] original_ban = { - //t => t.Type == TokenType.Constant && t.AssociatedData == 160, // ban - t => t is ConstantToken {Value:StringVariant{Value: "ban"}}, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline - }; + new CodeChange { + name = "instance_actor", + // "instance_actor": + // emit_signal("_instance_actor", DATA["params"] END + multitoken_prefix = new Func[] { + t => t is ConstantToken {Value:StringVariant{Value: "instance_actor"}}, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline, + t => t is IdentifierToken {Name: "emit_signal"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.Constant, + t => t.Type == TokenType.Comma, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.BracketOpen, + t => t.Type == TokenType.Constant, + t => t.Type == TokenType.BracketClose, + }, + // , packet_sender END + code_to_add = new Token[] { + new Token(TokenType.Comma), + new IdentifierToken("packet_sender"), + } + }, - Func[] original_punch = { - t => t is ConstantToken {Value:StringVariant{Value: "player_punch"}}, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline - }; + new CodeChange { + name = "kick", + // "kick": + // END + multitoken_prefix = new Func[] { + t => t is ConstantToken {Value:StringVariant{Value: "kick"}}, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline + }, + // print("[KICK]") + // if GAME_MASTER: return + // if packet_sender != KNOWN_GAME_MASTER: return + // END + code_to_add = new Token[] { + new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("[KICK]")), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 4), - Func[] original_msg = { - //t => t.Type == TokenType.Constant && t.AssociatedData == 139, // message - t => t is ConstantToken {Value:StringVariant{Value: "message"}}, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline - }; + new Token(TokenType.CfIf), + new IdentifierToken("GAME_MASTER"), + new Token(TokenType.Colon), + new Token(TokenType.CfReturn), + new Token(TokenType.Newline, 4), - Func[] original_read_all = { - t => t.Type == TokenType.PrFunction, - //t => t.Type == TokenType.Identifier && t.AssociatedData == 69, // _read_all_P2P_packets - t => t is IdentifierToken {Name: "_read_all_P2P_packets"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.OpAssign, - t => t.Type == TokenType.Constant, - }; + new Token(TokenType.CfIf), + new IdentifierToken("packet_sender"), + new Token(TokenType.OpNotEqual), + new IdentifierToken("KNOWN_GAME_MASTER"), + new Token(TokenType.Colon), + new Token(TokenType.CfReturn), + new Token(TokenType.Newline, 4), + } + }, - Func[] original_process = { - t => t.Type == TokenType.PrFunction, - //t => t.Type == TokenType.Identifier && t.AssociatedData == 63, // _process - t => t is IdentifierToken {Name: "_process"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.ParenthesisClose, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline, - t => t.Type == TokenType.CfIf, - t => t.Type == TokenType.OpNot, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.CfReturn, - t => t.Type == TokenType.Newline, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.Period, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.ParenthesisOpen, - t => t.Type == TokenType.ParenthesisClose, - t => t.Type == TokenType.Newline, - t => t.Type == TokenType.CfIf, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.OpGreater, - t => t.Type == TokenType.Constant, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline, - }; + new CodeChange { + name = "ban", + // "ban": + // END + multitoken_prefix = new Func[] { + t => t is ConstantToken {Value:StringVariant{Value: "ban"}}, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline + }, + // print("[BAN]") + // if GAME_MASTER: return + // if packet_sender != KNOWN_GAME_MASTER: return + // END + code_to_add = new Token[] { + new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("[BAN]")), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 4), - Func[] original_physics_process = { - t => t.Type == TokenType.PrFunction, - //t => t.Type == TokenType.Identifier && t.AssociatedData == 68, // _physics_process - t => t is IdentifierToken {Name: "_physics_process"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.ParenthesisClose, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline, - t => t.Type == TokenType.CfIf, - t => t.Type == TokenType.OpNot, - t => t.Type == TokenType.Identifier, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.CfReturn, - t => t.Type == TokenType.Newline, - }; + new Token(TokenType.CfIf), + new IdentifierToken("GAME_MASTER"), + new Token(TokenType.Colon), + new Token(TokenType.CfReturn), + new Token(TokenType.Newline, 4), - Func[] original_globals = { - t => t.Type == TokenType.PrVar, - //t => t.Type == TokenType.Identifier && t.AssociatedData == 31, // REPLICATIONS_RECIEVED - t => t is IdentifierToken {Name: "REPLICATIONS_RECIEVED"}, - t => t.Type == TokenType.OpAssign, - t => t.Type == TokenType.BracketOpen, - t => t.Type == TokenType.BracketClose, - t => t.Type == TokenType.Newline, - }; + new Token(TokenType.CfIf), + new IdentifierToken("packet_sender"), + new Token(TokenType.OpNotEqual), + new IdentifierToken("KNOWN_GAME_MASTER"), + new Token(TokenType.Colon), + new Token(TokenType.CfReturn), + new Token(TokenType.Newline, 4), + } + }, - Func[] original_p2p_test = { - //t => t.Type == TokenType.Identifier && t.AssociatedData == 71, // PACKET - t => t is IdentifierToken {Name: "PACKET"}, - t => t.Type == TokenType.Period, - //t => t.Type == TokenType.Identifier && t.AssociatedData == 241, // empty - t => t is IdentifierToken {Name: "empty"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t.Type == TokenType.ParenthesisClose, - t => t.Type == TokenType.Colon, - t => t.Type == TokenType.Newline, - t => t.Type == TokenType.BuiltInFunc, - t => t.Type == TokenType.ParenthesisOpen, - t => t.Type == TokenType.Constant, - t => t.Type == TokenType.ParenthesisClose, - t => t.Type == TokenType.Newline, - }; + new CodeChange { + name = "punch", + // "player_punch": + // END + multitoken_prefix = new Func[] { + t => t is ConstantToken {Value:StringVariant{Value: "player_punch"}}, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline + }, + // if not DATA.has("nya"): LUCY_PUNCHED_ME = packet_sender + // END + code_to_add = new Token[] { + new Token(TokenType.CfIf), + new Token(TokenType.OpNot), + new IdentifierToken("DATA"), + new Token(TokenType.Period), + new IdentifierToken("has"), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("nya")), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Colon), - Func[] original_setlobbyname = { - t => t is IdentifierToken {Name: "setLobbyData"}, - t => t.Type == TokenType.ParenthesisOpen, - t => t is IdentifierToken {Name: "lobby_id"}, - t => t.Type == TokenType.Comma, - t => t.Type == TokenType.Constant, - t => t.Type == TokenType.Comma, - t => t.Type == TokenType.BuiltInFunc, - t => t.Type == TokenType.ParenthesisOpen, - t => t is IdentifierToken {Name: "STEAM_USERNAME"}, - t => t.Type == TokenType.ParenthesisClose, - }; + new IdentifierToken("LUCY_PUNCHED_ME"), + new Token(TokenType.OpAssign), + new IdentifierToken("packet_sender"), + new Token(TokenType.Newline,4), + } + }, - IEnumerable IScriptMod.Modify(string path, IEnumerable tokens) { - var kick_waiter = new MultiTokenWaiter(original_kick); - var ban_waiter = new MultiTokenWaiter(original_ban); - var msg_waiter = new MultiTokenWaiter(original_msg); - var read_all_waiter = new MultiTokenWaiter(original_read_all); - var process_waiter = new MultiTokenWaiter(original_process); - var physics_process_waiter = new MultiTokenWaiter(original_physics_process); - var globals_waiter = new MultiTokenWaiter(original_globals); - var p2p_test_waiter = new MultiTokenWaiter(original_p2p_test); - var setlobbyname_waiter = new MultiTokenWaiter(original_setlobbyname); - var punch_waiter = new MultiTokenWaiter(original_punch); + new CodeChange { + name = "message", + // "message": + multitoken_prefix = new Func[] { + t => t is ConstantToken {Value:StringVariant{Value: "message"}}, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline + }, + // print("[msg ", _get_username_from_id(packet_sender), "] ", DATA.message) + code_to_add = new Token[] { + new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("[msg ")), + new Token(TokenType.Comma), + new IdentifierToken("_get_username_from_id"), + new Token(TokenType.ParenthesisOpen), + new IdentifierToken("packet_sender"), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Comma), + new ConstantToken(new StringVariant("] ")), + new Token(TokenType.Comma), + new IdentifierToken("DATA"), + new Token(TokenType.Period), + new IdentifierToken("message"), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 4), + } + }, - foreach (var token in tokens) { - if (globals_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network globals..."); - yield return token; + new CodeChange { + name = "_read_all_P2P_packets", + // func _read_all_P2P_packets(channel = 0 END + multitoken_prefix = new Func[] { + t => t.Type == TokenType.PrFunction, + t => t is IdentifierToken {Name: "_read_all_P2P_packets"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.OpAssign, + t => t.Type == TokenType.Constant, + }, + // , limit = 64): + // var read_count = 0 + // while Steam.getAvailableP2PPacketSize(channel) > 0 and read_count < limit: + // _read_P2P_Packet(channel) + // read_count += 1 + // LUCY_PACKETS_READ += read_count + // func _old_read_all_P2P_packets(channel = 0 END + code_to_add = new Token[] { + new Token(TokenType.Comma), + new IdentifierToken("limit"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(64)), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Colon), + new Token(TokenType.Newline, 1), - // var LUCY_PACKETS_READ = 0 - // var LUCY_BULK_FULL_TIMER = 0 - // var LUCY_FRAME_PACKETS = 32 - // var LUCY_BULK_PACKETS = 128 - // var LUCY_BULK_INTERVAL = 0.8 - // var LUCY_BULK_FULL_INTERVAL = 6.4 - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_PACKETS_READ"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(0)); - yield return new Token(TokenType.Newline, 0); + new Token(TokenType.PrVar), + new IdentifierToken("read_count"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(0)), + new Token(TokenType.Newline, 1), - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_BULK_FULL_TIMER"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(0)); - yield return new Token(TokenType.Newline, 0); + new Token(TokenType.CfWhile), + new IdentifierToken("Steam"), + new Token(TokenType.Period), + new IdentifierToken("getAvailableP2PPacketSize"), + new Token(TokenType.ParenthesisOpen), + new IdentifierToken("channel"), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.OpGreater), + new ConstantToken(new IntVariant(0)), + new Token(TokenType.OpAnd), + new IdentifierToken("read_count"), + new Token(TokenType.OpLess), + new IdentifierToken("limit"), + new Token(TokenType.Colon), + new Token(TokenType.Newline, 2), - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_FRAME_PACKETS"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(32)); - yield return new Token(TokenType.Newline, 0); + new IdentifierToken("_read_P2P_Packet"), + new Token(TokenType.ParenthesisOpen), + new IdentifierToken("channel"), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 2), - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_BULK_PACKETS"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(128)); - yield return new Token(TokenType.Newline, 0); + new IdentifierToken("read_count"), + new Token(TokenType.OpAssignAdd), + new ConstantToken(new IntVariant(1)), + new Token(TokenType.Newline, 1), - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_BULK_INTERVAL"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new RealVariant(0.8)); - yield return new Token(TokenType.Newline, 0); - - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_BULK_FULL_INTERVAL"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new RealVariant(6.4)); - yield return new Token(TokenType.Newline, 0); - - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_CHAT_BBCODE"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new BoolVariant(false)); - yield return new Token(TokenType.Newline, 0); - - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_SRV_NAME"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new StringVariant("")); - yield return new Token(TokenType.Newline, 0); - - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("LUCY_PUNCHED_ME"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(0)); - yield return new Token(TokenType.Newline, 0); - } else if (p2p_test_waiter.Check(token)) { - yield return token; - - // check if packet had sender? - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("packet_sender"); - yield return new Token(TokenType.OpAssign); - yield return new IdentifierToken("PACKET"); - yield return new Token(TokenType.BracketOpen); - yield return new ConstantToken(new StringVariant("steam_id_remote")); - yield return new Token(TokenType.BracketClose); - yield return new Token(TokenType.Newline, 2); - } else if (kick_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network kick fixes..."); - yield return token; - - // print("[KICK]") - yield return new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("[KICK]")); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 4); - - // if GAME_MASTER: return - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("GAME_MASTER"); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.CfReturn); - yield return new Token(TokenType.Newline, 4); - - // if packet_sender != KNOWN_GAME_MASTER: return - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("packet_sender"); - yield return new Token(TokenType.OpNotEqual); - yield return new IdentifierToken("KNOWN_GAME_MASTER"); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.CfReturn); - yield return new Token(TokenType.Newline, 4); - } else if (ban_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network ban fixes..."); - yield return token; - - // print("[BAN]") - yield return new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("[BAN]")); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 4); - - // if GAME_MASTER: return - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("GAME_MASTER"); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.CfReturn); - yield return new Token(TokenType.Newline, 4); - - // if packet_sender != KNOWN_GAME_MASTER: return - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("packet_sender"); - yield return new Token(TokenType.OpNotEqual); - yield return new IdentifierToken("KNOWN_GAME_MASTER"); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.CfReturn); - yield return new Token(TokenType.Newline, 4); - } else if (msg_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network msg fixes..."); - yield return token; - - // print("[msg ", _get_username_from_id(packet_sender), "] ", DATA.message) - yield return new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("[msg ")); - yield return new Token(TokenType.Comma); - yield return new IdentifierToken("_get_username_from_id"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new IdentifierToken("packet_sender"); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Comma); - yield return new ConstantToken(new StringVariant("] ")); - yield return new Token(TokenType.Comma); - yield return new IdentifierToken("DATA"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("message"); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 4); - } else if (read_all_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network _read_all_P2P_packets fixes..."); - // func _read_all_P2P_packets(channel = 0 - yield return token; - - // Our new function - // - // func _read_all_P2P_packets(channel = 0, limit = 64): - // var read_count = 0 - // while Steam.getAvailableP2PPacketSize(channel) > 0 and read_count < limit: - // _read_P2P_Packet(channel) - // read_count += 1 - // LUCY_PACKETS_READ += read_count - // return - // ...old code - yield return new Token(TokenType.Comma); - yield return new IdentifierToken("limit"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(64)); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.Newline, 1); - - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("read_count"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(0)); - yield return new Token(TokenType.Newline, 1); - - yield return new Token(TokenType.CfWhile); - yield return new IdentifierToken("Steam"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("getAvailableP2PPacketSize"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new IdentifierToken("channel"); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.OpGreater); - yield return new ConstantToken(new IntVariant(0)); - yield return new Token(TokenType.OpAnd); - yield return new IdentifierToken("read_count"); - yield return new Token(TokenType.OpLess); - yield return new IdentifierToken("limit"); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.Newline, 2); - - yield return new IdentifierToken("_read_P2P_Packet"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new IdentifierToken("channel"); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 2); - - yield return new IdentifierToken("read_count"); - yield return new Token(TokenType.OpAssignAdd); - yield return new ConstantToken(new IntVariant(1)); - yield return new Token(TokenType.Newline, 1); - - yield return new IdentifierToken("LUCY_PACKETS_READ"); - yield return new Token(TokenType.OpAssignAdd); - yield return new IdentifierToken("read_count"); - yield return new Token(TokenType.Newline, 0); + new IdentifierToken("LUCY_PACKETS_READ"), + new Token(TokenType.OpAssignAdd), + new IdentifierToken("read_count"), + new Token(TokenType.Newline, 0), // Give old function new signature, it'll come after in token stream - yield return new Token(TokenType.PrFunction); - yield return new IdentifierToken("_old_read_all_P2P_packets"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new IdentifierToken("channel"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(0)); - } else if (process_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network _process fixes..."); - // func _process(delta): - // if not STEAM_ENABLED: return - // Steam.run_callbacks() - // if STEAM_LOBBY_ID > 0: - // - yield return token; - - // better code - // if STEAM_LOBBY_ID > 0: - // for i in 3: _read_all_P2P_packets(i,LUCY_FRAME_PACKETS) - // return - // if false: - yield return new Token(TokenType.CfFor); - yield return new IdentifierToken("i"); - yield return new Token(TokenType.OpIn); - yield return new ConstantToken(new IntVariant(3)); - yield return new Token(TokenType.Colon); - yield return new IdentifierToken("_read_all_P2P_packets"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new IdentifierToken("i"); - yield return new Token(TokenType.Comma); - yield return new IdentifierToken("LUCY_FRAME_PACKETS"); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 1); - - yield return new Token(TokenType.CfReturn); - yield return new Token(TokenType.Newline, 1); - yield return new Token(TokenType.CfIf); - yield return new ConstantToken(new BoolVariant(false)); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.Newline, 2); - } else if (physics_process_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network _physics_process fixes..."); - // func _physics_process(delta): - // if not STEAM_ENABLED: return - // - yield return token; - - // better code - // var do_print = false - // BULK_PACKET_READ_TIMER -= delta - // if BULK_PACKET_READ_TIMER <= 0: - // print("Bulk Reading Packets.") - // for i in 3: _read_all_P2P_packets(i,LUCY_BULK_PACKETS) - // BULK_PACKET_READ_TIMER = LUCY_BULK_INTERVAL - // do_print = true - // LUCY_BULK_FULL_TIMER -= delta - // if LUCY_BULK_FULL_TIMER <= 0: - // print("Reading all packets.") - // for i in 3: _read_all_P2P_packets(i,1000000) - // LUCY_BULK_FULL_TIMER = LUCY_BULK_FULL_INTERVAL - // do_print = true - // if do_print: - // print("PACKETS ", LUCY_PACKETS_READ) - // LUCY_PACKETS_READ = 0 - // return - - // var do_print = false - // BULK_PACKET_READ_TIMER -= delta - // if BULK_PACKET_READ_TIMER <= 0: - // print("Bulk Reading Packets.") - // for i in 3: _read_all_P2P_packets(i,LUCY_BULK_PACKETS) - // BULK_PACKET_READ_TIMER = LUCY_BULK_INTERVAL - // do_print = true - yield return new Token(TokenType.PrVar); - yield return new IdentifierToken("do_print"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new BoolVariant(false)); - yield return new Token(TokenType.Newline, 1); - - yield return new IdentifierToken("BULK_PACKET_READ_TIMER"); - yield return new Token(TokenType.OpAssignSub); - yield return new IdentifierToken("delta"); - yield return new Token(TokenType.Newline, 1); - - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("BULK_PACKET_READ_TIMER"); - yield return new Token(TokenType.OpLessEqual); - yield return new ConstantToken(new IntVariant(0)); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.Newline, 2); - - yield return new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("Bulk Reading Packets.")); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 2); - - yield return new Token(TokenType.CfFor); - yield return new IdentifierToken("i"); - yield return new Token(TokenType.OpIn); - yield return new ConstantToken(new IntVariant(3)); - yield return new Token(TokenType.Colon); - yield return new IdentifierToken("_read_all_P2P_packets"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new IdentifierToken("i"); - yield return new Token(TokenType.Comma); - yield return new IdentifierToken("LUCY_BULK_PACKETS"); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 2); - - yield return new IdentifierToken("BULK_PACKET_READ_TIMER"); - yield return new Token(TokenType.OpAssign); - yield return new IdentifierToken("LUCY_BULK_INTERVAL"); - yield return new Token(TokenType.Newline, 2); - - yield return new IdentifierToken("do_print"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new BoolVariant(true)); - yield return new Token(TokenType.Newline, 1); - - // LUCY_BULK_FULL_TIMER -= delta - // if LUCY_BULK_FULL_TIMER <= 0: - // print("Reading all packets.") - // for i in 3: _read_all_P2P_packets(i,1000000) - // LUCY_BULK_FULL_TIMER = LUCY_BULK_FULL_INTERVAL - // do_print = true - yield return new IdentifierToken("LUCY_BULK_FULL_TIMER"); - yield return new Token(TokenType.OpAssignSub); - yield return new IdentifierToken("delta"); - yield return new Token(TokenType.Newline, 1); - - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("LUCY_BULK_FULL_TIMER"); - yield return new Token(TokenType.OpLessEqual); - yield return new ConstantToken(new IntVariant(0)); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.Newline, 2); - - yield return new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("Reading all packets.")); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 2); - - yield return new Token(TokenType.CfFor); - yield return new IdentifierToken("i"); - yield return new Token(TokenType.OpIn); - yield return new ConstantToken(new IntVariant(3)); - yield return new Token(TokenType.Colon); - yield return new IdentifierToken("_read_all_P2P_packets"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new IdentifierToken("i"); - yield return new Token(TokenType.Comma); - yield return new ConstantToken(new IntVariant(1000000)); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 2); - - yield return new IdentifierToken("LUCY_BULK_FULL_TIMER"); - yield return new Token(TokenType.OpAssign); - yield return new IdentifierToken("LUCY_BULK_FULL_INTERVAL"); - yield return new Token(TokenType.Newline, 2); - - yield return new IdentifierToken("do_print"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new BoolVariant(true)); - yield return new Token(TokenType.Newline, 1); - - // if do_print: - // print("PACKETS ", LUCY_PACKETS_READ) - // LUCY_PACKETS_READ = 0 - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("do_print"); - yield return new Token(TokenType.Colon); - yield return new Token(TokenType.Newline, 2); - - yield return new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("PACKETS ")); - yield return new Token(TokenType.Comma); - yield return new IdentifierToken("LUCY_PACKETS_READ"); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Newline, 2); - - yield return new IdentifierToken("LUCY_PACKETS_READ"); - yield return new Token(TokenType.OpAssign); - yield return new ConstantToken(new IntVariant(0)); - yield return new Token(TokenType.Newline, 1); - - // return - yield return new Token(TokenType.CfReturn); - yield return new Token(TokenType.Newline, 1); - } else if (punch_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network punch mod..."); - yield return token; - - yield return new Token(TokenType.CfIf); - yield return new Token(TokenType.OpNot); - yield return new IdentifierToken("DATA"); - yield return new Token(TokenType.Period); - yield return new IdentifierToken("has"); - yield return new Token(TokenType.ParenthesisOpen); - yield return new ConstantToken(new StringVariant("nya")); - yield return new Token(TokenType.ParenthesisClose); - yield return new Token(TokenType.Colon); - - yield return new IdentifierToken("LUCY_PUNCHED_ME"); - yield return new Token(TokenType.OpAssign); - yield return new IdentifierToken("packet_sender"); - yield return new Token(TokenType.Newline,4); - } else if (setlobbyname_waiter.Check(token)) { - Mod.ModInterface.Logger.Information("Adding Lucy Network lobby name mod..."); - // Steam.setLobbyData(lobby_id, "name", str(STEAM_USERNAME) - yield return token; - - // Steam.setLobbyData(lobby_id, "name", str(STEAM_USERNAME) if LUCY_SRV_NAME == "" else LUCY_SRV_NAME - yield return new Token(TokenType.CfIf); - yield return new IdentifierToken("LUCY_SRV_NAME"); - yield return new Token(TokenType.OpEqual); - yield return new ConstantToken(new StringVariant("")); - yield return new Token(TokenType.CfElse); - yield return new IdentifierToken("LUCY_SRV_NAME"); - } else { - yield return token; + new Token(TokenType.PrFunction), + new IdentifierToken("_old_read_all_P2P_packets"), + new Token(TokenType.ParenthesisOpen), + new IdentifierToken("channel"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(0)), } + }, + + new CodeChange { + name = "_process", + // func _process(delta): + // if not STEAM_ENABLED: return + // Steam.run_callbacks() + // if STEAM_LOBBY_ID > 0: + // END + multitoken_prefix = new Func[] { + t => t.Type == TokenType.PrFunction, + t => t is IdentifierToken {Name: "_process"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.ParenthesisClose, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline, + t => t.Type == TokenType.CfIf, + t => t.Type == TokenType.OpNot, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.CfReturn, + t => t.Type == TokenType.Newline, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.Period, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.ParenthesisClose, + t => t.Type == TokenType.Newline, + t => t.Type == TokenType.CfIf, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.OpGreater, + t => t.Type == TokenType.Constant, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline, + }, + // for i in 3: _read_all_P2P_packets(i,LUCY_FRAME_PACKETS) + // return + // if false: + code_to_add = new Token[] { + new Token(TokenType.CfFor), + new IdentifierToken("i"), + new Token(TokenType.OpIn), + new ConstantToken(new IntVariant(3)), + new Token(TokenType.Colon), + new IdentifierToken("_read_all_P2P_packets"), + new Token(TokenType.ParenthesisOpen), + new IdentifierToken("i"), + new Token(TokenType.Comma), + new IdentifierToken("LUCY_FRAME_PACKETS"), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 1), + + new Token(TokenType.CfReturn), + new Token(TokenType.Newline, 1), + new Token(TokenType.CfIf), + new ConstantToken(new BoolVariant(false)), + new Token(TokenType.Colon), + new Token(TokenType.Newline, 2), + } + }, + + new CodeChange { + name = "_physics_process", + // func _physics_process(delta): + // if not STEAM_ENABLED: return + // END + multitoken_prefix = new Func[] { + t => t.Type == TokenType.PrFunction, + t => t is IdentifierToken {Name: "_physics_process"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.ParenthesisClose, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline, + t => t.Type == TokenType.CfIf, + t => t.Type == TokenType.OpNot, + t => t.Type == TokenType.Identifier, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.CfReturn, + t => t.Type == TokenType.Newline, + }, + // var do_print = false + // BULK_PACKET_READ_TIMER -= delta + // if BULK_PACKET_READ_TIMER <= 0: + // print("Bulk Reading Packets.") + // for i in 3: _read_all_P2P_packets(i,LUCY_BULK_PACKETS) + // BULK_PACKET_READ_TIMER = LUCY_BULK_INTERVAL + // do_print = true + // LUCY_BULK_FULL_TIMER -= delta + // if LUCY_BULK_FULL_TIMER <= 0: + // print("Reading all packets.") + // for i in 3: _read_all_P2P_packets(i,1000000) + // LUCY_BULK_FULL_TIMER = LUCY_BULK_FULL_INTERVAL + // do_print = true + // if do_print: + // print("PACKETS ", LUCY_PACKETS_READ) + // LUCY_PACKETS_READ = 0 + // return + code_to_add = new Token[] { + // var do_print = false + // BULK_PACKET_READ_TIMER -= delta + // if BULK_PACKET_READ_TIMER <= 0: + // print("Bulk Reading Packets.") + // for i in 3: _read_all_P2P_packets(i,LUCY_BULK_PACKETS) + // BULK_PACKET_READ_TIMER = LUCY_BULK_INTERVAL + // do_print = true + new Token(TokenType.PrVar), + new IdentifierToken("do_print"), + new Token(TokenType.OpAssign), + new ConstantToken(new BoolVariant(false)), + new Token(TokenType.Newline, 1), + + new IdentifierToken("BULK_PACKET_READ_TIMER"), + new Token(TokenType.OpAssignSub), + new IdentifierToken("delta"), + new Token(TokenType.Newline, 1), + + new Token(TokenType.CfIf), + new IdentifierToken("BULK_PACKET_READ_TIMER"), + new Token(TokenType.OpLessEqual), + new ConstantToken(new IntVariant(0)), + new Token(TokenType.Colon), + new Token(TokenType.Newline, 2), + + new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("Bulk Reading Packets.")), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 2), + + new Token(TokenType.CfFor), + new IdentifierToken("i"), + new Token(TokenType.OpIn), + new ConstantToken(new IntVariant(3)), + new Token(TokenType.Colon), + new IdentifierToken("_read_all_P2P_packets"), + new Token(TokenType.ParenthesisOpen), + new IdentifierToken("i"), + new Token(TokenType.Comma), + new IdentifierToken("LUCY_BULK_PACKETS"), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 2), + + new IdentifierToken("BULK_PACKET_READ_TIMER"), + new Token(TokenType.OpAssign), + new IdentifierToken("LUCY_BULK_INTERVAL"), + new Token(TokenType.Newline, 2), + + new IdentifierToken("do_print"), + new Token(TokenType.OpAssign), + new ConstantToken(new BoolVariant(true)), + new Token(TokenType.Newline, 1), + + // LUCY_BULK_FULL_TIMER -= delta + // if LUCY_BULK_FULL_TIMER <= 0: + // print("Reading all packets.") + // for i in 3: _read_all_P2P_packets(i,1000000) + // LUCY_BULK_FULL_TIMER = LUCY_BULK_FULL_INTERVAL + // do_print = true + new IdentifierToken("LUCY_BULK_FULL_TIMER"), + new Token(TokenType.OpAssignSub), + new IdentifierToken("delta"), + new Token(TokenType.Newline, 1), + + new Token(TokenType.CfIf), + new IdentifierToken("LUCY_BULK_FULL_TIMER"), + new Token(TokenType.OpLessEqual), + new ConstantToken(new IntVariant(0)), + new Token(TokenType.Colon), + new Token(TokenType.Newline, 2), + + new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("Reading all packets.")), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 2), + + new Token(TokenType.CfFor), + new IdentifierToken("i"), + new Token(TokenType.OpIn), + new ConstantToken(new IntVariant(3)), + new Token(TokenType.Colon), + new IdentifierToken("_read_all_P2P_packets"), + new Token(TokenType.ParenthesisOpen), + new IdentifierToken("i"), + new Token(TokenType.Comma), + new ConstantToken(new IntVariant(1000000)), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 2), + + new IdentifierToken("LUCY_BULK_FULL_TIMER"), + new Token(TokenType.OpAssign), + new IdentifierToken("LUCY_BULK_FULL_INTERVAL"), + new Token(TokenType.Newline, 2), + + new IdentifierToken("do_print"), + new Token(TokenType.OpAssign), + new ConstantToken(new BoolVariant(true)), + new Token(TokenType.Newline, 1), + + // if do_print: + // print("PACKETS ", LUCY_PACKETS_READ) + // LUCY_PACKETS_READ = 0 + new Token(TokenType.CfIf), + new IdentifierToken("do_print"), + new Token(TokenType.Colon), + new Token(TokenType.Newline, 2), + + new Token(TokenType.BuiltInFunc, (uint)BuiltinFunction.TextPrint), + new Token(TokenType.ParenthesisOpen), + new ConstantToken(new StringVariant("PACKETS ")), + new Token(TokenType.Comma), + new IdentifierToken("LUCY_PACKETS_READ"), + new Token(TokenType.ParenthesisClose), + new Token(TokenType.Newline, 2), + + new IdentifierToken("LUCY_PACKETS_READ"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(0)), + new Token(TokenType.Newline, 1), + + // return + new Token(TokenType.CfReturn), + new Token(TokenType.Newline, 1), + } + }, + + new CodeChange { + name = "new globals", + // var REPLICATIONS_RECIEVED = [] + // END + multitoken_prefix = new Func[] { + t => t.Type == TokenType.PrVar, + t => t is IdentifierToken {Name: "REPLICATIONS_RECIEVED"}, + t => t.Type == TokenType.OpAssign, + t => t.Type == TokenType.BracketOpen, + t => t.Type == TokenType.BracketClose, + t => t.Type == TokenType.Newline, + }, + // var LUCY_PACKETS_READ = 0 + // var LUCY_BULK_FULL_TIMER = 0 + // var LUCY_FRAME_PACKETS = 32 + // var LUCY_BULK_PACKETS = 128 + // var LUCY_BULK_INTERVAL = 0.8 + // var LUCY_BULK_FULL_INTERVAL = 6.4 + // var LUCY_CHAT_BBCODE = false + // var LUCY_SRV_NAME = "" + // var LUCY_PUNCHED_ME = 0 + // END + code_to_add = new Token[] { + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_PACKETS_READ"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(0)), + new Token(TokenType.Newline, 0), + + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_BULK_FULL_TIMER"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(0)), + new Token(TokenType.Newline, 0), + + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_FRAME_PACKETS"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(32)), + new Token(TokenType.Newline, 0), + + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_BULK_PACKETS"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(128)), + new Token(TokenType.Newline, 0), + + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_BULK_INTERVAL"), + new Token(TokenType.OpAssign), + new ConstantToken(new RealVariant(0.8)), + new Token(TokenType.Newline, 0), + + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_BULK_FULL_INTERVAL"), + new Token(TokenType.OpAssign), + new ConstantToken(new RealVariant(6.4)), + new Token(TokenType.Newline, 0), + + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_CHAT_BBCODE"), + new Token(TokenType.OpAssign), + new ConstantToken(new BoolVariant(false)), + new Token(TokenType.Newline, 0), + + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_SRV_NAME"), + new Token(TokenType.OpAssign), + new ConstantToken(new StringVariant("")), + new Token(TokenType.Newline, 0), + + new Token(TokenType.PrVar), + new IdentifierToken("LUCY_PUNCHED_ME"), + new Token(TokenType.OpAssign), + new ConstantToken(new IntVariant(0)), + new Token(TokenType.Newline, 0), + } + }, + + new CodeChange { + name = "packet sender", + // if PACKET.empty(): + // print("Error! Empty Packet!") + // END + multitoken_prefix = new Func[] { + t => t is IdentifierToken {Name: "PACKET"}, + t => t.Type == TokenType.Period, + t => t is IdentifierToken {Name: "empty"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.ParenthesisClose, + t => t.Type == TokenType.Colon, + t => t.Type == TokenType.Newline, + t => t.Type == TokenType.BuiltInFunc, + t => t.Type == TokenType.ParenthesisOpen, + t => t.Type == TokenType.Constant, + t => t.Type == TokenType.ParenthesisClose, + t => t.Type == TokenType.Newline, + }, + // var packet_sender = PACKET['steam_id_remote'] + // END + code_to_add = new Token[] { + new Token(TokenType.PrVar), + new IdentifierToken("packet_sender"), + new Token(TokenType.OpAssign), + new IdentifierToken("PACKET"), + new Token(TokenType.BracketOpen), + new ConstantToken(new StringVariant("steam_id_remote")), + new Token(TokenType.BracketClose), + new Token(TokenType.Newline, 2), + } + }, + + new CodeChange { + name = "set lobby name", + // Steam.setLobbyData(lobby_id, "name", str(STEAM_USERNAME) END + multitoken_prefix = new Func[] { + t => t is IdentifierToken {Name: "setLobbyData"}, + t => t.Type == TokenType.ParenthesisOpen, + t => t is IdentifierToken {Name: "lobby_id"}, + t => t.Type == TokenType.Comma, + t => t.Type == TokenType.Constant, + t => t.Type == TokenType.Comma, + t => t.Type == TokenType.BuiltInFunc, + t => t.Type == TokenType.ParenthesisOpen, + t => t is IdentifierToken {Name: "STEAM_USERNAME"}, + t => t.Type == TokenType.ParenthesisClose, + }, + // if LUCY_SRV_NAME == "" else LUCY_SRV_NAME END + code_to_add = new Token[] { + new Token(TokenType.CfIf), + new IdentifierToken("LUCY_SRV_NAME"), + new Token(TokenType.OpEqual), + new ConstantToken(new StringVariant("")), + new Token(TokenType.CfElse), + new IdentifierToken("LUCY_SRV_NAME"), + } + }, + }; + + IEnumerable IScriptMod.Modify(string path, IEnumerable tokens) + { + var pending_changes = changes + .Select(c => (c, new MultiTokenWaiter(c.multitoken_prefix))) + .ToList(); + + // I'm sure there's a better way to do this + // with list comprehension stuff, but my + // C# is too rusty + foreach (var token in tokens) { + var had_change = false; + foreach ((var change, var waiter) in pending_changes) { + if (waiter.Check(token)) { + Mod.ModInterface.Logger.Information($"Adding Lucy Network mod {change.name}"); + + yield return token; + foreach (var t in change.code_to_add) yield return t; + + had_change = true; + break; + } + } + if (!had_change) yield return token; } } - }