dev: 更新代码,并准备LuaMod

This commit is contained in:
ZZY
2025-06-14 14:38:28 +08:00
parent 3fa39fc71e
commit 13450ea09a
20 changed files with 486 additions and 397 deletions

2
.gitignore vendored
View File

@ -23,7 +23,7 @@ android/
Bin/ Bin/
bin/ bin/
*.aseprite *.aseprite
*.csproj.old *.csproj.old*
*.sln *.sln
*.error *.error
*.key* *.key*

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
<Project Sdk="Godot.NET.Sdk/4.4.1"> <Project Sdk="Godot.NET.Sdk/4.4.0">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading> <EnableDynamicLoading>true</EnableDynamicLoading>
@ -7,7 +7,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Remove="Test\**\*" /> <Compile Remove="Test\**\*" />
<Protobuf Include="Protos\chess.proto" GrpcServices="Client" /> <Compile Remove="android\**\*" />
<Compile Remove="Protos\**\*" />
<!-- <Protobuf Include="Protos\chess.proto" GrpcServices="Client" /> -->
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.30.2" /> <PackageReference Include="Google.Protobuf" Version="3.30.2" />

43
Main.cs
View File

@ -1,45 +1,10 @@
using Godot; using Godot;
using System;
public partial class Main : Node2D
{
// public void ResizeChessboardToFitScreen()
// {
// var viewportSize = GetViewportRect().Size;
// var designSize = new Vector2(800, 800); // 设计时棋盘的尺寸,这里假设为正方形
// var screenAspectRatio = viewportSize.X / viewportSize.Y;
// var boardAspectRatio = designSize.X / designSize.Y;
// // 计算保持棋盘比例的缩放因子
// float scaleFactor;
// if (screenAspectRatio > boardAspectRatio)
// {
// // 屏幕更宽,按高度缩放
// scaleFactor = viewportSize.Y / designSize.Y;
// }
// else
// {
// // 屏幕更高或相等,按宽度缩放
// scaleFactor = viewportSize.X / designSize.X;
// }
// // 应用缩放
// GetNode<Node2D>("Chessboard").Scale = new Vector2(scaleFactor, scaleFactor);
// }
public partial class Main : Node2D {
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
GlobalManager global = null!; GlobalManager global = null!;
public override void _Ready() { public override void _Ready() {
// GetTree().Connect("screen_resized", ResizeChessboardToFitScreen); // GetTree().Connect("screen_resized", ResizeChessboardToFitScreen);
// Button undo = GetNode<Button>("Undo");
// undo.Connect("pressed", (event) => {
// });
// ChessBoard board = GetNodeAndResource("res://res/ChessBoard.tscn");
// ChessBoard board1 = GetChild<ChessBoard>(0);
// board.Visible = false;
// board.Can
global = GlobalManager.Instance; global = GlobalManager.Instance;
global.GlobalTheme = GetChild<Control>(GetChildCount() - 1).Theme; global.GlobalTheme = GetChild<Control>(GetChildCount() - 1).Theme;
global.GlobalThemeConfigFlush(); global.GlobalThemeConfigFlush();
@ -50,12 +15,10 @@ public partial class Main : Node2D
} }
private void GoToSignlePlayer() { private void GoToSignlePlayer() {
// global.GotoScene("res://Scenes/SinglePlayer.tscn");
global?.GotoScene("res://Scenes/ChessGame.tscn"); global?.GotoScene("res://Scenes/ChessGame.tscn");
} }
private void GoToMultiPlayer() { private void GoToMultiPlayer() {
// global.GotoScene("res://Scenes/MultiPlayer.tscn");
global?.GotoScene("res://Scenes/GameLobby.tscn"); global?.GotoScene("res://Scenes/GameLobby.tscn");
} }
@ -66,8 +29,4 @@ public partial class Main : Node2D
private void GotoMods() { private void GotoMods() {
global?.GotoScene("res://Scenes/Mods.tscn"); global?.GotoScene("res://Scenes/Mods.tscn");
} }
// private void goToMenu() {
// global.GotoScene("res://Scenes/Menu.tscn");
// }
} }

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://boa4od72355o4"] [gd_scene load_steps=3 format=3 uid="uid://boa4od72355o4"]
[ext_resource type="Script" uid="uid://kdxc7ummin4b" path="res://Main.cs" id="1_h4cv2"] [ext_resource type="Script" uid="uid://kdxc7ummin4b" path="res://Main.cs" id="1_h4cv2"]
[ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/defaultTheme.tres" id="2_afihr"] [ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/GameTheme.tres" id="2_afihr"]
[node name="Main" type="Node2D"] [node name="Main" type="Node2D"]
script = ExtResource("1_h4cv2") script = ExtResource("1_h4cv2")

View File

@ -2,86 +2,85 @@
[ext_resource type="Script" uid="uid://cqgctouhh8qok" path="res://Scripts/Controllers/ChessGame.cs" id="1_3x8ac"] [ext_resource type="Script" uid="uid://cqgctouhh8qok" path="res://Scripts/Controllers/ChessGame.cs" id="1_3x8ac"]
[ext_resource type="PackedScene" uid="uid://b1tx7v3230wab" path="res://Scenes/Entities/Chessboard.tscn" id="1_yheur"] [ext_resource type="PackedScene" uid="uid://b1tx7v3230wab" path="res://Scenes/Entities/Chessboard.tscn" id="1_yheur"]
[ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/defaultTheme.tres" id="3_rcfhx"] [ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/GameTheme.tres" id="3_rcfhx"]
[node name="ChessGame" type="Node2D"] [node name="Control" type="Control"]
script = ExtResource("1_3x8ac")
[node name="Chessboard" parent="." instance=ExtResource("1_yheur")]
position = Vector2(360, 650)
scale = Vector2(2.5, 2.5)
[node name="Control" type="Control" parent="."]
custom_minimum_size = Vector2(720, 1280)
layout_mode = 3 layout_mode = 3
anchors_preset = 0 anchors_preset = 15
offset_right = 720.0 anchor_right = 1.0
offset_bottom = 1280.0 anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme = ExtResource("3_rcfhx") theme = ExtResource("3_rcfhx")
[node name="VBoxContainer" type="VBoxContainer" parent="Control"] [node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 1 layout_mode = 1
anchors_preset = 10 anchors_preset = 10
anchor_right = 1.0 anchor_right = 1.0
offset_bottom = 133.0 offset_bottom = 133.0
grow_horizontal = 2 grow_horizontal = 2
[node name="MarginContainer" type="MarginContainer" parent="Control/VBoxContainer"] [node name="MarginContainer" type="MarginContainer" parent="VBoxContainer"]
layout_mode = 2 layout_mode = 2
[node name="HBoxContainer" type="HBoxContainer" parent="Control/VBoxContainer/MarginContainer"] [node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/MarginContainer"]
layout_mode = 2 layout_mode = 2
[node name="MarginContainer" type="MarginContainer" parent="Control/VBoxContainer/MarginContainer/HBoxContainer"] [node name="MarginContainer" type="MarginContainer" parent="VBoxContainer/MarginContainer/HBoxContainer"]
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
[node name="Home" type="Button" parent="Control/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer"] [node name="Home" type="Button" parent="VBoxContainer/MarginContainer/HBoxContainer/MarginContainer"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
text = "KEY_BackHome" text = "KEY_BackHome"
[node name="MarginContainer2" type="MarginContainer" parent="Control/VBoxContainer/MarginContainer/HBoxContainer"] [node name="MarginContainer2" type="MarginContainer" parent="VBoxContainer/MarginContainer/HBoxContainer"]
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
[node name="Undo" type="Button" parent="Control/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2"] [node name="Undo" type="Button" parent="VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_vertical = 4 size_flags_vertical = 4
text = "KEY_GameUndo" text = "KEY_GameUndo"
[node name="MarginContainer3" type="MarginContainer" parent="Control/VBoxContainer/MarginContainer/HBoxContainer"] [node name="MarginContainer3" type="MarginContainer" parent="VBoxContainer/MarginContainer/HBoxContainer"]
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
[node name="ReInit" type="Button" parent="Control/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer3"] [node name="ReInit" type="Button" parent="VBoxContainer/MarginContainer/HBoxContainer/MarginContainer3"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
text = "KEY_GameReInit" text = "KEY_GameReInit"
[node name="MarginContainer3" type="MarginContainer" parent="Control/VBoxContainer"] [node name="MarginContainer3" type="MarginContainer" parent="VBoxContainer"]
layout_mode = 2 layout_mode = 2
[node name="HFlowContainer" type="HFlowContainer" parent="Control/VBoxContainer/MarginContainer3"] [node name="HFlowContainer" type="HFlowContainer" parent="VBoxContainer/MarginContainer3"]
layout_mode = 2 layout_mode = 2
[node name="Label" type="Label" parent="Control/VBoxContainer/MarginContainer3/HFlowContainer"] [node name="Label" type="Label" parent="VBoxContainer/MarginContainer3/HFlowContainer"]
layout_mode = 2 layout_mode = 2
text = "KEY_Content_YouAre" text = "KEY_Content_YouAre"
[node name="LineEdit" type="LineEdit" parent="Control/VBoxContainer/MarginContainer3/HFlowContainer"] [node name="LineEdit_YouAre" type="LineEdit" parent="VBoxContainer/MarginContainer3/HFlowContainer"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
editable = false editable = false
[node name="Label2" type="Label" parent="Control/VBoxContainer/MarginContainer3/HFlowContainer"] [node name="Label2" type="Label" parent="VBoxContainer/MarginContainer3/HFlowContainer"]
layout_mode = 2 layout_mode = 2
text = "KEY_Content_TurnOn" text = "KEY_Content_TurnOn"
[node name="LineEdit2" type="LineEdit" parent="Control/VBoxContainer/MarginContainer3/HFlowContainer"] [node name="LineEdit_TurnSide" type="LineEdit" parent="VBoxContainer/MarginContainer3/HFlowContainer"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
editable = false editable = false
[node name="MarginContainer2" type="MarginContainer" parent="Control"] [node name="MarginContainer2" type="MarginContainer" parent="."]
layout_mode = 1 layout_mode = 1
anchors_preset = 12 anchors_preset = 12
anchor_top = 1.0 anchor_top = 1.0
@ -91,11 +90,21 @@ offset_top = -114.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 0 grow_vertical = 0
[node name="Over" type="Button" parent="Control/MarginContainer2"] [node name="Over" type="Button" parent="MarginContainer2"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
text = "KEY_OverThisTurn" text = "KEY_OverThisTurn"
[connection signal="pressed" from="Control/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer/Home" to="." method="GoHome"] [node name="ChessGame" type="Node2D" parent="."]
[connection signal="pressed" from="Control/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/Undo" to="." method="Undo"] unique_name_in_owner = true
[connection signal="pressed" from="Control/VBoxContainer/MarginContainer/HBoxContainer/MarginContainer3/ReInit" to="." method="ReInit"] script = ExtResource("1_3x8ac")
[connection signal="pressed" from="Control/MarginContainer2/Over" to="." method="BtnOver"]
[node name="Chessboard" parent="ChessGame" instance=ExtResource("1_yheur")]
unique_name_in_owner = true
position = Vector2(360, 650)
scale = Vector2(2.5, 2.5)
[connection signal="pressed" from="VBoxContainer/MarginContainer/HBoxContainer/MarginContainer/Home" to="ChessGame" method="GoHome"]
[connection signal="pressed" from="VBoxContainer/MarginContainer/HBoxContainer/MarginContainer2/Undo" to="ChessGame" method="Undo"]
[connection signal="pressed" from="VBoxContainer/MarginContainer/HBoxContainer/MarginContainer3/ReInit" to="ChessGame" method="ReInit"]
[connection signal="pressed" from="MarginContainer2/Over" to="ChessGame" method="BtnOver"]

View File

@ -15,3 +15,9 @@ text = "KEY_ServerConnectStatus"
[node name="ColorRect" type="ColorRect" parent="."] [node name="ColorRect" type="ColorRect" parent="."]
custom_minimum_size = Vector2(15, 15) custom_minimum_size = Vector2(15, 15)
layout_mode = 2 layout_mode = 2
[node name="TextureRect" type="TextureRect" parent="."]
layout_mode = 2
[node name="TextureProgressBar" type="TextureProgressBar" parent="."]
layout_mode = 2

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=3 format=3 uid="uid://00jxjgnmfbn7"] [gd_scene load_steps=3 format=3 uid="uid://00jxjgnmfbn7"]
[ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/defaultTheme.tres" id="1_ge0tq"] [ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/GameTheme.tres" id="1_ge0tq"]
[ext_resource type="Script" uid="uid://cn273e4u256r" path="res://Scripts/Controllers/GameLobby.cs" id="2_blx46"] [ext_resource type="Script" uid="uid://cn273e4u256r" path="res://Scripts/Controllers/GameLobby.cs" id="2_blx46"]
[node name="GameLobby" type="Control"] [node name="GameLobby" type="Control"]
@ -53,7 +53,8 @@ size_flags_horizontal = 3
layout_mode = 2 layout_mode = 2
size_flags_vertical = 3 size_flags_vertical = 3
[node name="ItemList" type="ItemList" parent="MarginContainer/VBoxContainer/MarginContainer3"] [node name="ItemList_Users" type="ItemList" parent="MarginContainer/VBoxContainer/MarginContainer3"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
[node name="Dialogs" type="Control" parent="."] [node name="Dialogs" type="Control" parent="."]
@ -74,4 +75,4 @@ initial_position = 2
size = Vector2i(200, 118) size = Vector2i(200, 118)
[connection signal="pressed" from="MarginContainer/VBoxContainer/MarginContainer/Button" to="." method="OnBack"] [connection signal="pressed" from="MarginContainer/VBoxContainer/MarginContainer/Button" to="." method="OnBack"]
[connection signal="item_activated" from="MarginContainer/VBoxContainer/MarginContainer3/ItemList" to="." method="OnItemSelected"] [connection signal="item_activated" from="MarginContainer/VBoxContainer/MarginContainer3/ItemList_Users" to="." method="OnItemSelected"]

View File

@ -1,11 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://c2ov83uk7r6rq"] [gd_scene load_steps=3 format=3 uid="uid://c2ov83uk7r6rq"]
[ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/defaultTheme.tres" id="1_mhtia"] [ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/GameTheme.tres" id="1_mhtia"]
[ext_resource type="Script" uid="uid://c8t8d8e7inajy" path="res://Scripts/Controllers/Mods.cs" id="1_mnyng"] [ext_resource type="Script" uid="uid://c8t8d8e7inajy" path="res://Scripts/Controllers/Mods.cs" id="1_mnyng"]
[node name="MarginContainer" type="MarginContainer"] [node name="MarginContainer" type="MarginContainer"]
offset_right = 720.0 anchors_preset = 15
offset_bottom = 1280.0 anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme = ExtResource("1_mhtia") theme = ExtResource("1_mhtia")
theme_override_constants/margin_left = 30 theme_override_constants/margin_left = 30
theme_override_constants/margin_top = 30 theme_override_constants/margin_top = 30
@ -18,6 +21,40 @@ layout_mode = 2
[node name="Button" type="Button" parent="VBoxContainer"] [node name="Button" type="Button" parent="VBoxContainer"]
layout_mode = 2 layout_mode = 2
text = "back" text = "KEY_BackHome"
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
layout_mode = 2
[node name="CheckButton" type="CheckButton" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "KEY_EnableMod"
[node name="Button2" type="Button" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "KEY_ReloadMod"
[node name="Button2" type="Button" parent="VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
text = "KEY_OpenModDir"
[node name="VScrollBar" type="VScrollBar" parent="VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 1
size_flags_vertical = 3
[node name="ItemList" type="ItemList" parent="VBoxContainer/VScrollBar"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[connection signal="pressed" from="VBoxContainer/Button" to="." method="OnBack"] [connection signal="pressed" from="VBoxContainer/Button" to="." method="OnBack"]
[connection signal="toggled" from="VBoxContainer/HBoxContainer/CheckButton" to="." method="OnEnableMods"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer/Button2" to="." method="OnReSearchMods"]
[connection signal="pressed" from="VBoxContainer/Button2" to="." method="OnOpenModFileDir"]

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=5 format=3 uid="uid://c6gxufppw1fu3"] [gd_scene load_steps=5 format=3 uid="uid://c6gxufppw1fu3"]
[ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/defaultTheme.tres" id="1_6yfoi"] [ext_resource type="Theme" uid="uid://intlbeu8h82r" path="res://Asserts/GameTheme.tres" id="1_6yfoi"]
[ext_resource type="Script" uid="uid://71ril3nh84rw" path="res://Scripts/Controllers/Setting.cs" id="1_xbvb3"] [ext_resource type="Script" uid="uid://71ril3nh84rw" path="res://Scripts/Controllers/Setting.cs" id="1_xbvb3"]
[ext_resource type="PackedScene" uid="uid://pc83bstfltn" path="res://Scenes/Entities/ServerStatus.tscn" id="3_e50hu"] [ext_resource type="PackedScene" uid="uid://pc83bstfltn" path="res://Scenes/Entities/ServerStatus.tscn" id="3_e50hu"]
@ -49,7 +49,8 @@ layout_mode = 2
layout_mode = 2 layout_mode = 2
text = "KEY_ServerIP" text = "KEY_ServerIP"
[node name="LineEdit" type="LineEdit" parent="BoxContainer/MarginContainer2/Server"] [node name="LineEdit_ServerIP" type="LineEdit" parent="BoxContainer/MarginContainer2/Server"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
@ -63,7 +64,8 @@ layout_mode = 2
layout_mode = 2 layout_mode = 2
text = "KEY_UserName" text = "KEY_UserName"
[node name="LineEdit" type="LineEdit" parent="BoxContainer/MarginContainer3/Name"] [node name="LineEdit_UserName" type="LineEdit" parent="BoxContainer/MarginContainer3/Name"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
@ -98,6 +100,7 @@ layout_mode = 2
text = "KEY_GlobalFontSize" text = "KEY_GlobalFontSize"
[node name="FontSize" type="LineEdit" parent="BoxContainer/MarginContainer5/HBoxContainer"] [node name="FontSize" type="LineEdit" parent="BoxContainer/MarginContainer5/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
editable = false editable = false
@ -105,11 +108,12 @@ editable = false
layout_mode = 2 layout_mode = 2
[node name="FontSizeBar" type="HSlider" parent="BoxContainer/MarginContainer6"] [node name="FontSizeBar" type="HSlider" parent="BoxContainer/MarginContainer6"]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
size_flags_vertical = 4 size_flags_vertical = 4
min_value = 10.0 min_value = 10.0
max_value = 50.0 max_value = 40.0
value = 10.0 value = 10.0
[node name="HBoxContainer2" type="HFlowContainer" parent="BoxContainer"] [node name="HBoxContainer2" type="HFlowContainer" parent="BoxContainer"]
@ -195,8 +199,8 @@ text = "Trace Color"
[connection signal="pressed" from="BoxContainer/MarginContainer/Back" to="." method="OnBack"] [connection signal="pressed" from="BoxContainer/MarginContainer/Back" to="." method="OnBack"]
[connection signal="pressed" from="BoxContainer/MarginContainer7/Button" to="." method="OnSave"] [connection signal="pressed" from="BoxContainer/MarginContainer7/Button" to="." method="OnSave"]
[connection signal="pressed" from="BoxContainer/MarginContainer9/Button" to="." method="OnGetSettingsFile"] [connection signal="pressed" from="BoxContainer/MarginContainer9/Button" to="." method="OnGetSettingsFile"]
[connection signal="text_changed" from="BoxContainer/MarginContainer2/Server/LineEdit" to="." method="OnServerUrlChanged"] [connection signal="text_changed" from="BoxContainer/MarginContainer2/Server/LineEdit_ServerIP" to="." method="OnServerUrlChanged"]
[connection signal="text_changed" from="BoxContainer/MarginContainer3/Name/LineEdit" to="." method="OnNameChanged"] [connection signal="text_changed" from="BoxContainer/MarginContainer3/Name/LineEdit_UserName" to="." method="OnNameChanged"]
[connection signal="value_changed" from="BoxContainer/MarginContainer6/FontSizeBar" to="." method="OnFontSizeChanged"] [connection signal="value_changed" from="BoxContainer/MarginContainer6/FontSizeBar" to="." method="OnFontSizeChanged"]
[connection signal="toggled" from="BoxContainer/HBoxContainer2/MarginContainer/Button" to="." method="ChangeLangZHCN"] [connection signal="toggled" from="BoxContainer/HBoxContainer2/MarginContainer/Button" to="." method="ChangeLangZHCN"]
[connection signal="toggled" from="BoxContainer/HBoxContainer2/MarginContainer2/Button2" to="." method="ChangeLangEN"] [connection signal="toggled" from="BoxContainer/HBoxContainer2/MarginContainer2/Button2" to="." method="ChangeLangEN"]

View File

@ -1,31 +1,22 @@
#nullable disable
using Godot; using Godot;
using Godot.Collections;
using ChineseChess; using ChineseChess;
public partial class ChessGame : Node2D { public partial class ChessGame : Node2D {
ChessBoard board;
GlobalManager global = GlobalManager.Instance; GlobalManager global = GlobalManager.Instance;
ConfirmationDialog dialog; readonly Logging.Logger logger = Logging.GetLogger("ChessGame");
ChessBoard board = null!;
ConfirmationDialog dialog = null!;
ChessCore Game = null!;
private bool isSession = false; private bool isSession = false;
ChessCore Game;
ChessCore.TurnsSideType sideSelf; ChessCore.TurnsSideType sideSelf;
ChessCore.TurnsSideType sideOpposite; ChessCore.TurnsSideType sideOpposite;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() { public override void _Ready() {
// Init.Call(); board = GetNode<ChessBoard>("%Chessboard");
board = GetNode<ChessBoard>("Chessboard"); GetNode<LineEdit>("%LineEdit_YouAre").Text = global.GlobalData["player_color"].AsString();
// ChessGameAPI api = new(); LineEdit turnSideEdit = GetNode<LineEdit>("%LineEdit_TurnSide");
// api.BindToLua(global.ModManager.lua);
// isSession = global.RPClient.IsOnline();
GetNode<LineEdit>("Control/VBoxContainer/MarginContainer3/HFlowContainer/LineEdit")
.Text = global.GlobalData["player_color"].AsString();
LineEdit turnSideEdit = GetNode<LineEdit>("Control/VBoxContainer/MarginContainer3/HFlowContainer/LineEdit2");
turnSideEdit.Text = "red"; turnSideEdit.Text = "red";
// GD.Print("ChessGame uid:", global.RPClient.GetUserId(), " color:",global.GlobalData["player_color"]);
dialog = new ConfirmationDialog { dialog = new ConfirmationDialog {
DialogAutowrap = true, DialogAutowrap = true,
@ -52,107 +43,32 @@ public partial class ChessGame : Node2D {
int posX = (int)pos.X; int posX = (int)pos.X;
int posY = (int)pos.Y; int posY = (int)pos.Y;
Vector.Vector2I vector = new(posX, posY); Vector.Vector2I vector = new(posX, posY);
if (isSession) {
Game.OnPosClicked(vector, ChessCore.PlayerSideType.Self);
// var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary {
// {"type", "mouseClicked"},
// {"X", pos.X},
// {"Y", pos.Y},
// {"id", global.RPClient.GetUserId()}
// });
} else {
Game.OnPosClicked(vector); Game.OnPosClicked(vector);
}
}; };
if (isSession) {
GD.Print("ws is connected");
// global.RPClient.OnPRCSessionExit += (cmd, code) => {
// GoHome();
// };
// global.RPClient.OnPRCSessionRecv += (msg) => {
// SessionMsgHandle(msg["msg"].AsGodotDictionary());
// };
}
} }
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) { public override void _Process(double delta) {
} }
// private void SessionMsgHandle(Dictionary msg) {
// GD.PrintErr($"session msg: {msg}");
// switch (msg["type"].AsString()) {
// case "over":
// if (global.RPClient.GetUserId() == msg["id"].AsString()) {
// break;
// }
// dialog.Title = "Opponent Finished";
// dialog.DialogText = "Turn On You\n";
// dialog.Visible = true;
// break;
// case "mouseClicked":
// if (msg["id"].ToString() == global.RPClient.GetUserId()) {
// break;
// }
// Vector2 mouseClicked = new(GD.StrToVar(msg["X"].ToString()).AsInt32(),
// GD.StrToVar(msg["Y"].ToString()).AsInt32());
// Vector.Vector2I vector = new((int)mouseClicked.X, (int)mouseClicked.Y);
// Game.OnPosClicked(vector, ChessCore.PlayerSideType.Opponent);
// break;
// case "undo":
// Game.Undo();
// break;
// case "reInit":
// Game.ReInit();
// break;
// }
// }
private void BtnOver() { private void BtnOver() {
GD.Print($"BtnOver {isSession}"); logger.Info($"BtnOver");
if (isSession == false) {
return;
}
// global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
// {"type", "over"},
// {"id", global.RPClient.GetUserId()},
// });
} }
public void GoHome() { public void GoHome() {
// if (global.RPClient.IsOnline()) { // TODO ClearServer();
// global.RPClient.ExitServer();
// }
global.GotoScene("res://Main.tscn"); global.GotoScene("res://Main.tscn");
} }
public void Undo() { public void Undo() {
GD.Print($"Undo {isSession}"); logger.Info($"Undo");
if (isSession) {
// global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
// {"type", "undo"},
// {"id", global.RPClient.GetUserId()},
// });
} else {
if (Game.board.Steps == 0) board.Clear(); if (Game.board.Steps == 0) board.Clear();
Game.Undo(); Game.Undo();
} }
}
public void ReInit() { public void ReInit() {
GD.PrintErr($"ReInit {isSession}"); // TODO using dialog to confirm
logger.Info($"ReInit");
if (isSession) {
// global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
// {"type", "reInit"},
// });
} else {
Game.ReInit(); Game.ReInit();
board.Clear();
}
} }
} }

View File

@ -1,140 +1,60 @@
#nullable disable
using Godot; using Godot;
using Godot.Collections; using Godot.Collections;
public partial class GameLobby : Control { public partial class GameLobby : Control {
GlobalManager global = GlobalManager.Instance; GlobalManager global = GlobalManager.Instance;
Timer timer = null; readonly Logging.Logger logger = Logging.GetLogger("GameLobby");
Timer timer = null!;
ItemList lists = null; ItemList lists = null!;
ConfirmationDialog dialog = null; ConfirmationDialog dialog = null!;
string URL = null!;
string URL; string userName = null!;
string userName;
bool isAcceptedPart = false;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() { public override void _Ready() {
lists = GetNode<ItemList>("MarginContainer/VBoxContainer/MarginContainer3/ItemList"); lists = GetNode<ItemList>("%ItemList_Users");
// TODO using %
dialog = GetNode<ConfirmationDialog>("Dialogs/ConfirmationDialog"); dialog = GetNode<ConfirmationDialog>("Dialogs/ConfirmationDialog");
URL = global.ConfigManager.GetValue<string>("server_url");
URL = global.ConfigManager.GetValue<string>("server_addr");
userName = global.ConfigManager.GetValue<string>("user_name"); userName = global.ConfigManager.GetValue<string>("user_name");
dialog.MinSize = new Vector2I(400, 200); dialog.MinSize = new Vector2I(400, 200);
dialog.DialogAutowrap = true; dialog.DialogAutowrap = true;
dialog.Canceled += () => { dialog.Canceled += () => {
// if (dialog.Title == "Session Created") // TODO Cancel to Accept Session Create
// global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), false);
}; };
dialog.Confirmed += () => { dialog.Confirmed += () => {
if (dialog.Title == "Session Created") { if (dialog.Title == "Session Created") {
// FIXME // TODO Create Session
isAcceptedPart = true;
// global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), true);
} }
}; };
// global.RPClient.OnOpen += (eventName, args) => { // TODO using new State Shower And Using Heartbeat to calucate the delay time
// global.RPClient.UserInit(userName, "godot chessboard", () => {
// global.RPClient.UserRename(userName);
// return global.RPClient.RegionAdd("server");
// });
// };
// global.RPClient.OnOpen += (eventName, args) => {
// global.RPClient.UserInit(userName, "none", () => {
// GD.PrintErr($"User Init Success");
// return true;
// });
// // global.RPClient.UserRename(userName);
// };
// global.RPClient.RegSessionAckCreateCallback((sessionId, res, reqUserId, reqUserName) => {
// if (reqUserId != null) {
// dialog.Title = "Session Created";
// dialog.SetMeta("reqUserName", reqUserName);
// dialog.SetMeta("reqUserId", reqUserId);
// dialog.SetMeta("sessionId", sessionId);
// // dialog.GetLabel==>Text = $"{sessdata["reqUserName"]}";
// dialog.DialogText = $"username: {reqUserName}\n" +
// $"reqUserId: {reqUserId}\n";
// dialog.Visible = true;
// } else {
// if (res) {
// // TODO FIXME
// global.sessionId = sessionId;
// if (!isAcceptedPart) {
// global.GlobalData["player_color"] = "red";
// } else {
// global.GlobalData["player_color"] = "black";
// }
// // GD.PrintErr("sessionId: ", reqUserId, "color: ", global.GlobalData["player_color"].AsString());
// global.GotoScene("res://Scenes/ChessGame.tscn");
// } else {
// dialog.Title = "Failed";
// dialog.DialogText = $"session create failed";
// dialog.Visible = true;
// }
// }
// return true;
// });
// timer = new Timer();
// AddChild(timer);
ColorRect colorRect = GetNode<ColorRect>("MarginContainer/VBoxContainer/MarginContainer2/MarginContainer2/ColorRect"); ColorRect colorRect = GetNode<ColorRect>("MarginContainer/VBoxContainer/MarginContainer2/MarginContainer2/ColorRect");
// timer.Connect("timeout", Callable.From(() => {
// if (global.RPClient.GetIsConnected()) {
// colorRect.Color = Colors.Green;
// } else {
// colorRect.Color = Colors.Red;
// }
// FlushData();
// }));
// timer.Start(1);
Connect(); Connect();
} }
public void Connect() { public void Connect() {
GD.Print("Connect"); logger.Info("Connect To Server");
// if (global.RPClient.GetIsConnected()) { // TODO connet to server
// return;
// }
// global.RPClient.ExitServer();
// global.RPClient.ConnectToUrlEx(URL);
global.SetProcess(true); global.SetProcess(true);
} }
private void OnItemSelected(int index) { private void OnItemSelected(int index) {
Dictionary item = lists.GetItemMetadata(index).AsGodotDictionary(); Dictionary item = lists.GetItemMetadata(index).AsGodotDictionary();
GD.Print($"Item {index} selected, {item}"); logger.Debug($"Item {index} selected, {item}");
string[] strings = { item["id"].ToString() }; string[] _ = [item["id"].ToString()];
// global.RPClient.SessionCreate(strings); // TODO new a Session
} }
private void FlushData() { private void FlushUserData() {
// global.RPClient.RegionInspect("server", (data) => { // TODO Flush user data or heartbeat flush
// // GD.Print(data);
// lists?.Clear();
// foreach (Dictionary<string, string> user in data) {
// string userId = user["id"].ToString();
// string userName = user["name"].ToString();
// if (userId == global.RPClient.GetUserId()) {
// lists.SetItemDisabled(
// lists.AddItem($"Name: {userName}"),
// true);
// continue;
// }
// var idx = lists.AddItem($"Name: {userName}");
// lists.SetItemMetadata(idx, user);
// lists.SetItemTooltip(idx, $"User ID: {userId}");
// }
// return true;
// });
} }
private void OnBack() { private void OnBack() {
// global.RPClient.ExitServer(); // TODO ExitServer();
global.GotoScene("res://Main.tscn"); global.GotoScene("res://Main.tscn");
} }
} }

View File

@ -3,24 +3,37 @@ using System;
public partial class Mods : Control { public partial class Mods : Control {
GlobalManager global = GlobalManager.Instance; GlobalManager global = GlobalManager.Instance;
public override void _Ready() { ItemList modsItemList = null!;
// RunDemo();
// ModManager modManager = new(); public override void _Ready() {
// modManager.RegistryFunc("OnInit", () => { GD.Print("OnInit"); }, true); // TODO
// modManager.RegistryFunc("foo", () => { GD.Print("Hello"); }); modsItemList = GetNode<ItemList>("VBoxContainer/VScrollBar/ItemList");
// LuaMod mod = new(ProjectSettings.GlobalizePath("res://Protos/test.lua")); FlushModState();
// mod.BindFunc(modManager); }
// try {
// modManager.InvokeFunc<Action>("OnInit"); private void FlushModState() {
// } modsItemList.Clear();
// catch (System.Exception e) { foreach (var mod in global.Mods.ModList) {
// GD.PrintErr("OnInit error: ", e.Message, e.StackTrace); var item = modsItemList.AddItem(mod.Key);
// } }
}
private void OnEnableMods(bool enable) {
if (enable) {
global.Mods.LoadAllMods();
}
else {
global.Mods.ClearAllMods();
}
}
private void OnReSearchMods() {
global.Mods.SearchAllMods(true);
} }
public void OnOpenModFileDir() { public void OnOpenModFileDir() {
OS.ShellOpen(global.ConfigManager.GetValue<string>("mods_fpath") ?? "user://Mods/"); string path = global.ConfigManager.GetValue<string>("mods_fpath") ?? "user://mods";
OS.ShellOpen(ProjectSettings.GlobalizePath(path));
} }
public void OnBack() { public void OnBack() {

View File

@ -4,24 +4,21 @@ using System.IO;
public partial class Setting : Control { public partial class Setting : Control {
private GlobalManager global = GlobalManager.Instance; private GlobalManager global = GlobalManager.Instance;
private IConfigManager config = GlobalManager.Instance.ConfigManager; private readonly IConfigManager config = GlobalManager.Instance.ConfigManager;
int font_size; private int font_size;
private LineEdit fontOut = null!; private LineEdit fontOut = null!;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() { public override void _Ready() {
fontOut = GetNode<LineEdit>("BoxContainer/MarginContainer5/HBoxContainer/FontSize"); fontOut = GetNode<LineEdit>("%FontSize");
font_size = config.GetValue<int>("font_size"); font_size = config.GetValue<int>("font_size");
GetNode<HSlider>("BoxContainer/MarginContainer6/FontSizeBar") GetNode<HSlider>("%FontSizeBar")
.Value = font_size; .Value = font_size;
fontOut.Text = font_size.ToString(); fontOut.Text = font_size.ToString();
FillingData(); GetNode<LineEdit>("%LineEdit_ServerIP")
} .Text = config.GetValue<string>("server_addr");
private void FillingData() { GetNode<LineEdit>("%LineEdit_UserName")
GetNode<LineEdit>("BoxContainer/MarginContainer2/Server/LineEdit")
.Text = config.GetValue<string>("server_url");
GetNode<LineEdit>("BoxContainer/MarginContainer3/Name/LineEdit")
.Text = config.GetValue<string>("user_name"); .Text = config.GetValue<string>("user_name");
} }
@ -35,17 +32,14 @@ public partial class Setting : Control {
private void OnSave() { private void OnSave() {
global.SaveConfig(); global.SaveConfig();
// OS.Alert("Saved", "Setting");
} }
void OnNameChanged(string Value) { void OnNameChanged(string Value) {
config.SetValue("user_name", Value); config.SetValue("user_name", Value);
// config.GetValue<>Flush();
} }
private void OnServerUrlChanged(string Value) { private void OnServerUrlChanged(string Value) {
config.SetValue("server_url", Value); config.SetValue("server_url", Value);
// config.GetValue<>Flush();
} }
private void OnFontSizeChanged(float Value) { private void OnFontSizeChanged(float Value) {

View File

@ -2,23 +2,26 @@ using System.Collections.Generic;
using Godot; using Godot;
public partial class GlobalManager : Node { public partial class GlobalManager : Node {
private static readonly Logging.Logger logger = Logging.GetLogger("GlobalManager");
public static GlobalManager Instance { get; private set; } = null!; public static GlobalManager Instance { get; private set; } = null!;
public static string ConfigPath { get; private set; } = "user://config.json"; public static string ConfigPath { get; private set; } = "user://config.json";
private GlobalManager() { private GlobalManager() {
ConfigManager.SetFilePath(ProjectSettings.GlobalizePath(ConfigPath)); ConfigManager.SetFilePath(ProjectSettings.GlobalizePath(ConfigPath));
ConfigManager.SetDefault(_default_config); ConfigManager.SetDefault(_default_config);
ConfigManager.LoadFromFile(); ConfigManager.LoadFromFile();
GD.Print("GameSystem Singleton Initialized");
Mods = new GlobalModManager(ConfigManager.GetValue<string>("mods_fpath"));
Mods.SearchAllMods();
} }
// TODO Will Change It public readonly GlobalModManager Mods = null!;
public readonly IConfigManager ConfigManager = new JsonConfigManager(); public readonly IConfigManager ConfigManager = new JsonConfigManager();
private static readonly Dictionary<string, object> _default_config = new() { private static readonly Dictionary<string, object> _default_config = new() {
{"font_size", 20}, {"font_size", 20},
{"server_url", "wss://game.zzyxyz.com/"}, {"server_addr", "wss://game.zzyxyz.com/"},
{"user_name", "undefined"}, {"user_name", "undefined"},
{"mods_fpath", "user://Mods/"}, {"mods_fpath", "user://mods"},
{"mods_allowed", false}, {"mods_allowed", false},
}; };
@ -39,23 +42,15 @@ public partial class GlobalManager : Node {
Instance = this; Instance = this;
} }
else { else {
GD.PushError("GlobalManager already exists"); logger.Error("GlobalManager already exists");
QueueFree(); QueueFree();
} }
if (OS.GetName() == "Android") { if (OS.GetName() == "Android") {
bool ret = OS.RequestPermissions(); bool ret = OS.RequestPermissions();
GD.Print($"RequestPermissions ret is {ret}"); logger.Warning($"RequestPermissions ret is {ret}");
} }
// OS.RequestPermissions();
// RPClient.OnRPCError += (errCode, type, cmd, errMsg) => {
// GD.PrintErr($"errCode {errCode}, type/cmd {type}/{cmd}, errMsg {errMsg}");
// };
// RPClient.OnClose += (eventName, args) => {
// SetProcess(false);
// };
Viewport root = GetTree().Root; Viewport root = GetTree().Root;
CurrentScene = root.GetChild(root.GetChildCount() - 1); CurrentScene = root.GetChild(root.GetChildCount() - 1);

175
Scripts/Logger.cs Normal file
View File

@ -0,0 +1,175 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
/// <summary>
/// 仿照 Python logging 的 日志系统
/// </summary>
public static class Logging {
// ========== 日志级别 ==========
public enum Level {
NOSET = 0,
DEBUG = 10,
INFO = 20,
WARNING = 30,
ERROR = 40,
CRITICAL = 50
}
/// <summary>
/// 日志记录器
/// </summary>
public class Logger(string name) {
public string Name { get; } = name;
public Level LogLevel { get; set; } = Level.INFO;
private readonly List<Action<Level, string, string, string>> _handlers = [];
public void AddHandler(Action<Level, string, string, string> handler) =>
_handlers.Add(handler);
// ===== 日志方法 =====
public void Debug(string fmt, params object[] args) =>
Log(Level.DEBUG, fmt, false, args);
public void Info(string fmt, params object[] args) =>
Log(Level.INFO, fmt, false, args);
public void Warning(string fmt, params object[] args) =>
Log(Level.WARNING, fmt, false, args);
public void Error(string fmt, params object[] args) =>
Log(Level.ERROR, fmt, false, args);
public void Critical(string fmt, params object[] args) =>
Log(Level.CRITICAL, fmt, true, args);
public void Exception(string fmt, Exception e, params object[] args) {
// TODO for godot not support /r/n yet
string stackTrace = e.StackTrace?.Replace("\r\n", "\n") ?? "";
string exceptionMessage = $"{fmt}\n{e.Message}\n{stackTrace}";
Log(Level.ERROR, exceptionMessage, false, args);
}
// ===== 核心Log方法 =====
private void Log(Level level, string fmt, bool isExec = false, params object[] args) {
if (level < LogLevel) return;
// 格式化消息
string message = args.Length > 0 ?
string.Format(fmt, args) : fmt;
// 添加执行堆栈信息
string stackInfo = isExec ?
$"\nExecution Stack:\n{System.Environment.StackTrace}" : "";
// 构建前缀
string prefix = $"[{DateTime.Now:HH:mm:ss}] [{level}] [{Name}]";
// 调用所有处理器
foreach (var handler in _handlers) {
handler(level, prefix, message, stackInfo);
}
}
[Conditional("DEBUG")]
public void Assert(bool condition, string fmt, params object[] args) {
if (condition) return;
string message = args.Length > 0 ?
string.Format(fmt, args) : fmt;
Error("Assertion Failed: " + message, true);
if (Debugger.IsAttached)
Debugger.Break();
System.Diagnostics.Debug.Assert(condition, message);
}
}
// ========== 全局管理器 ==========
private static readonly Dictionary<string, Logger> _loggers = [];
private static readonly Logger _root = GetLogger("root");
/// <summary>
/// 获取或创建日志记录器
/// </summary>
public static Logger GetLogger(string name = "root") {
if (!_loggers.TryGetValue(name, out var logger)) {
logger = new Logger(name);
logger.AddHandler(DefaultHandler);
_loggers[name] = logger;
}
return logger;
}
// ========== 优化的默认处理器 ==========
private static void DefaultHandler(Level level, string prefix, string message, string stackInfo) {
// 构建完整日志消息
string fullMessage = $"{prefix} {message}{stackInfo}";
switch (level) {
case Level.DEBUG:
// DEBUG级别使用GD.Print + OS条件
if (OS.IsStdOutVerbose())
GD.Print(fullMessage);
break;
case Level.INFO:
// INFO级别使用GD.Print
GD.Print(fullMessage);
break;
case Level.WARNING:
// WARNING级别使用PrintRich + 黄色
GD.PrintRich($"[color=yellow]{fullMessage}[/color]");
break;
case Level.ERROR:
// ERROR级别使用GD.PrintErr
GD.PrintRich($"[color=red][b]{fullMessage}[/b][/color]");
// GD.PrintErr(fullMessage);
break;
case Level.CRITICAL:
// CRITICAL级别使用GD.PushError
GD.PushError(fullMessage);
break;
}
}
// ========== 快捷操作方式 ==========
public static void Debug(string fmt, params object[] args) =>
_root.Debug(fmt, args);
public static void Info(string fmt, params object[] args) =>
_root.Info(fmt, args);
public static void Warning(string fmt, params object[] args) =>
_root.Warning(fmt, args);
public static void Error(string fmt, params object[] args) =>
_root.Error(fmt, args);
public static void Critical(string fmt, params object[] args) =>
_root.Critical(fmt, args);
// ===== C语言风格的宏断言快捷方式 =====
[Conditional("DEBUG")]
public static void Assert(bool condition, string fmt, params object[] args) =>
_root.Assert(condition, fmt, args);
[Conditional("DEBUG")]
public static void TestPrint() {
GD.Print("GameSystem Singleton Initialized");
GD.PrintErr("This is a print error");
GD.PushWarning("This is a warning");
GD.PushError("This is a error");
Debug("This is a debug");
Info("This is a info");
Warning("This is a warn");
Error("This is a error");
Critical("This is a critical");
Assert(true, "This is a assert");
}
}

1
Scripts/Logger.cs.uid Normal file
View File

@ -0,0 +1 @@
uid://bud682vprk1w6

View File

@ -1,67 +1,70 @@
using Godot; using Godot;
using NLua; using System.Collections.Generic;
using System; using System.Collections.ObjectModel;
using System.Text;
public class GlobalModManager public class GlobalModManager(string modPath = "user://mods") {
{ public static GlobalModManager Instance { get; } = new();
public readonly Lua lua = new(); private readonly Dictionary<string, LuaModManager> _mods = [];
private readonly string modsPath; public ReadOnlyDictionary<string, LuaModManager> Mods => new(_mods);
private readonly Dictionary<string, string> _modList = [];
public ReadOnlyDictionary<string, string> ModList => new(_modList);
private readonly string modPath = modPath;
private static readonly Logging.Logger logger = Logging.GetLogger("GlobalMod");
public GlobalModManager(string modsPath = "user://Mods/") { public void SearchAllMods(bool isFlush = false) {
this.modsPath = modsPath; if (isFlush) {
lua.State.Encoding = Encoding.UTF8; _modList.Clear();
// lua.State.SetHook(KeraLua.LuaHookMask.Count, 1000000);
InitSandBox();
// RegisterAPI();
} }
if (!DirAccess.DirExistsAbsolute(modPath))
public void LoadAllMods()
{
if (!DirAccess.DirExistsAbsolute(modsPath))
return; return;
using var dir = DirAccess.Open(modsPath); using var dir = DirAccess.Open(modPath);
if (dir == null) return; if (dir == null) return;
dir.ListDirBegin(); dir.ListDirBegin();
string fileName = dir.GetNext(); string fileName = dir.GetNext();
while (!string.IsNullOrEmpty(fileName)) while (!string.IsNullOrEmpty(fileName)) {
{ string fullPath = $"{modPath}/{fileName}";
if (fileName.EndsWith(".lua")) const string entryFileName = "main.lua";
{
LoadMod(fileName); if (dir.CurrentIsDir() && !fileName.StartsWith('.') &&
FileAccess.FileExists($"{fullPath}/{entryFileName}")) {
_modList.Add($"{fullPath}/{entryFileName}", fullPath);
} }
fileName = dir.GetNext(); fileName = dir.GetNext();
} }
} }
private void InitSandBox() { public void LoadAllMods(bool isFlush = false) {
if (isFlush) {
_mods.Clear();
} }
foreach (var mod in _modList) {
private void LoadMod(string filePath) try {
{ LoadMod(mod.Key, mod.Value);
try }
{ catch (System.Exception e) {
string luaCode = LoadFile(filePath); logger.Exception("Failed to load mod {0}", e, mod.Key);
lua.DoString(luaCode);
GD.Print($"成功加载Mod: {filePath}");
} }
catch (Exception e)
{
GD.PrintErr($"加载Mod {filePath} 失败: {e.Message}");
} }
} }
private static void SaveFile(string path, string content) public void ClearAllMods() {
{ _mods.Clear();
}
private void LoadMod(string filePath, string modPath) {
string _filePath = ProjectSettings.GlobalizePath(filePath);
string _modPath = ProjectSettings.GlobalizePath(modPath);
_mods.Add(filePath, new LuaModManager(_filePath, _modPath));
}
private static void SaveFile(string path, string content) {
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Write); using var file = FileAccess.Open(path, FileAccess.ModeFlags.Write);
file.StoreString(content); file.StoreString(content);
} }
private static string LoadFile(string path) private static string LoadFile(string path) {
{
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read); using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
string content = file.GetAsText(); string content = file.GetAsText();
return content; return content;

View File

@ -1,19 +1,19 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Godot;
using NLua; using NLua;
class LuaMod : IDisposable { public class LuaModManager : IDisposable {
private readonly Lua lua = new(); private readonly Lua lua = new();
private static readonly Logging.Logger logger = Logging.GetLogger("LuaMod");
public LuaMod(string path, string? package_path = null) { public LuaModManager(string path, string? package_path = null) {
lua.LoadCLRPackage(); lua.LoadCLRPackage();
lua.State.Encoding = System.Text.Encoding.UTF8; lua.State.Encoding = System.Text.Encoding.UTF8;
lua["package.path"] = package_path ?? Path.GetDirectoryName(path) + "/?.lua;"; lua["package.path"] = package_path ?? Path.GetDirectoryName(path) + "/?.lua;";
lua.RegisterFunction("print", new Action<object[]>(GD.Print).Method); lua.RegisterFunction("print", new Action<object[]>(Godot.GD.Print).Method);
// Action bar = () => { GD.Print("bar"); };
// lua.RegisterFunction("bar", bar.Target, bar.Method); logger.Info("Loading Lua Mod: " + path);
lua.DoFile(path); lua.DoFile(path);
} }
@ -36,5 +36,9 @@ class LuaMod : IDisposable {
lua?.Dispose(); lua?.Dispose();
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
public static void WriteComment(Delegate @delegate) {
// TODO
}
} }

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Reflection; using System.Reflection;
class ModManager { public class ModManager {
private readonly Dictionary<string, Delegate> _api = []; private readonly Dictionary<string, Delegate> _api = [];
private readonly Dictionary<string, Delegate> _hooks = []; private readonly Dictionary<string, Delegate> _hooks = [];
public delegate bool OnCallback(params object[] args); public delegate bool OnCallback(params object[] args);