Add Default Handler

This commit is contained in:
Somebody
2024-08-25 15:40:34 +08:00
parent 879730ce85
commit dd2d640bd6
11 changed files with 75 additions and 60 deletions

View File

@@ -142,12 +142,25 @@ public class CommandScene : ICommand
}
var player = arg.Target!.Player!;
if (player.SceneData?.ScenePropData.TryGetValue(floorId, out var _) == true)
if (player.SceneData?.ScenePropData.TryGetValue(floorId, out _) == true)
player.SceneData.ScenePropData[floorId] = [];
if (player.SceneData?.FloorSavedData.TryGetValue(floorId, out var _) == true)
if (player.SceneData?.FloorSavedData.TryGetValue(floorId, out _) == true)
player.SceneData.FloorSavedData[floorId] = [];
await arg.SendMsg(I18nManager.Translate("Game.Command.Scene.SceneReset", floorId.ToString()));
}
[CommandMethod("0 cur")]
public async ValueTask GetCurrentScene(CommandArg arg)
{
if (arg.Target == null)
{
await arg.SendMsg(I18nManager.Translate("Game.Command.Notice.PlayerNotFound"));
return;
}
var player = arg.Target!.Player!;
await arg.SendMsg(I18nManager.Translate("Game.Command.Scene.CurrentScene", player.Data.EntryId.ToString(), player.Data.PlaneId.ToString(), player.Data.FloorId.ToString()));
}
}

View File

@@ -78,7 +78,7 @@ public class ServerOption
public bool EnableMission { get; set; } = true; // experimental
public bool AutoLightSection { get; set; } = true;
public string Language { get; set; } = "EN";
public List<string> DefaultPermissions { get; set; } = ["*"];
public HashSet<string> DefaultPermissions { get; set; } = ["*"];
public ServerAnnounce ServerAnnounce { get; set; } = new();
public ServerProfile ServerProfile { get; set; } = new();
public bool AutoCreateUser { get; set; } = true;

View File

@@ -31,8 +31,9 @@ public static class I18nManager
{
var value = GetNestedPropertyValue(Language, key);
foreach (var arg in args) value = value.Replace("{" + args.ToList().IndexOf(arg) + "}", arg);
return value;
var index = 0;
return args.Aggregate(value, (current, arg) => current.Replace("{" + index++ + "}", arg));
}

View File

@@ -361,7 +361,7 @@ public class SceneTextCHS
"使用 unlockall 来解锁场景内所有道具即将所有能设置为open状态的道具设置为open状态此命令有较大可能会导致游戏加载卡条约90%,使用 /scene reset <floorId> 来解决问题\n" +
"使用 change 来进入指定场景要获取EntryId请访问 Resources/MapEntrance.json\n" +
"使用 reload 来重新加载当前场景,并回到初始位置\n" +
"使用 reset 来重置指定场景所有道具状态要获取当前FloorId访问数据库 Player 表";
"使用 reset 来重置指定场景所有道具状态要获取当前FloorId使用 /scene cur";
public string Usage =>
"用法:/scene prop [组ID] [道具ID] [状态]\n\n用法/scene remove [实体ID]\n\n用法/scene unlockall\n\n用法/scene change [entryId]\n\n用法/scene reload\n\n用法/scene reset <floorId>";
@@ -375,6 +375,7 @@ public class SceneTextCHS
public string SceneChanged => "已进入场景 {0}";
public string SceneReloaded => "场景已重新加载!";
public string SceneReset => "已重置场景 {0} 中所有道具状态!";
public string CurrentScene => "当前场景Entry Id: {0}, Plane Id: {1}, Floor Id: {2}";
}
/// <summary>

View File

@@ -361,7 +361,7 @@ public class SceneTextCHT
"使用 unlockall 來解鎖場景內所有道具即將所有能設置為open狀態的道具設置為open狀態此命令有較大可能會導致遊戲加載卡條約90%,使用 /scene reset <floorId> 來解決問題\n" +
"使用 change 來進入指定場景要獲取EntryId請訪問 Resources/MapEntrance.json\n" +
"使用 reload 來重新加載當前場景,並回到初始位置\n" +
"使用 reset 來重置指定景所有道具狀態,要獲取當前FloorId訪問數據庫 Player 表";
"使用 reset 來重置指定景所有道具狀態,要穫取噹前FloorId使用 /scene cur";
public string Usage =>
"用法:/scene prop [組ID] [道具ID] [狀態]\n\n用法/scene remove [實體ID]\n\n用法/scene unlockall\n\n用法/scene change [entryId]\n\n用法/scene reload\n\n用法/scene reset <floorId>";
@@ -375,6 +375,7 @@ public class SceneTextCHT
public string SceneChanged => "已進入場景 {0}";
public string SceneReloaded => "場景已重新加載!";
public string SceneReset => "已重置場景 {0} 中所有道具狀態!";
public string CurrentScene => "噹前塲景Entry Id: {0}, Plane Id: {1}, Floor Id: {2}";
}
/// <summary>

View File

@@ -371,7 +371,7 @@ public class SceneTextEN
"Use 'unlockall' to unlock all props in the scene (i.e., set all props that can be opened to the 'open' state). This command may cause the game to load to about 90%. Use '/scene reset <floorId>' to resolve this issue.\n" +
"Use 'change' to enter a specified scene. For EntryId, refer to Resources/MapEntrance.json\n" +
"Use 'reload' to reload the current scene and return to the initial position.\n" +
"Use 'reset' to reset the state of all props in the specified scene. For the current FloorId, refer to the Player table in the database.";
"Use 'reset' to reset the state of all props in the specified scene. For the current FloorId, use '/scene cur'.";
public string Usage =>
"Usage: /scene prop [groupId] [propId] [state]\n\nUsage: /scene remove [entityId]\n\nUsage: /scene unlockall\n\nUsage: /scene change [entryId]\n\nUsage: /scene reload\n\nUsage: /scene reset <floorId>";
@@ -385,6 +385,7 @@ public class SceneTextEN
public string SceneChanged => "Entered scene {0}";
public string SceneReloaded => "Scene has been reloaded!";
public string SceneReset => "The prop state in floor {0} has been reset!";
public string CurrentScene => "Current Scene Entry Id: {0}, Plane Id: {1}, Floor Id: {2}";
}
/// <summary>

View File

@@ -23,6 +23,9 @@ public static class ConfigManager
using var reader = new StreamReader(file.OpenRead());
var json = reader.ReadToEnd();
Config = JsonConvert.DeserializeObject<ConfigContainer>(json)!;
// save it again to make sure all fields are present
reader.Close();
SaveConfig();
}
public static void SaveConfig()

View File

@@ -13,7 +13,7 @@ public class DanhengConnection
public const int HANDSHAKE_SIZE = 20;
public static readonly List<int> BannedPackets = [];
private static readonly Logger Logger = new("GameServer");
public static readonly Dictionary<string, string> LogMap = [];
public static readonly Dictionary<int, string> LogMap = [];
public static readonly List<int> IgnoreLog =
[
@@ -73,27 +73,25 @@ public class DanhengConnection
if (IgnoreLog.Contains(opcode)) return;
var typ = AppDomain.CurrentDomain.GetAssemblies()
.SingleOrDefault(assembly => assembly.GetName().Name == "DanhengProto")!.GetTypes()
.First(t => t.Name == $"{LogMap[opcode.ToString()]}"); //get the type using the packet name
.First(t => t.Name == $"{LogMap[opcode]}"); //get the type using the packet name
var descriptor =
typ.GetProperty("Descriptor", BindingFlags.Public | BindingFlags.Static)?.GetValue(
null, null) as MessageDescriptor; // get the static property Descriptor
var packet = descriptor?.Parser.ParseFrom(payload);
var formatter = JsonFormatter.Default;
var asJson = formatter.Format(packet);
var output = $"{sendOrRecv}: {LogMap[opcode.ToString()]}({opcode})\r\n{asJson}";
var output = $"{sendOrRecv}: {LogMap[opcode]}({opcode})\r\n{asJson}";
#if DEBUG
Logger.Debug(output);
#endif
if (DebugFile != "" && ConfigManager.Config.ServerOption.SavePersonalDebugFile)
{
var sw = GetWriter();
sw.WriteLine($"[{DateTime.Now:HH:mm:ss}] [GameServer] [DEBUG] " + output);
sw.Flush();
}
if (DebugFile == "" || !ConfigManager.Config.ServerOption.SavePersonalDebugFile) return;
var sw = GetWriter();
sw.WriteLine($"[{DateTime.Now:HH:mm:ss}] [GameServer] [DEBUG] " + output);
sw.Flush();
}
catch
{
var output = $"{sendOrRecv}: {LogMap.GetValueOrDefault(opcode.ToString(), "UnknownPacket")}({opcode})";
var output = $"{sendOrRecv}: {LogMap.GetValueOrDefault(opcode, "UnknownPacket")}({opcode})";
#if DEBUG
Logger.Debug(output);
#endif

View File

@@ -21,10 +21,8 @@ public class SceneEntityLoader(SceneInstance scene)
if (dimInfo == null) return;
LoadGroups.AddRange(dimInfo.GroupIDList);
foreach (var group in Scene?.FloorInfo?.Groups.Values!) // Sanity check in SceneInstance
foreach (var group in from @group in Scene.FloorInfo?.Groups.Values! where @group.LoadSide != GroupLoadSideEnum.Client where !@group.GroupName.Contains("TrainVisitor") select @group)
{
if (group.LoadSide == GroupLoadSideEnum.Client) continue;
if (group.GroupName.Contains("TrainVisitor")) continue;
await LoadGroup(group);
}
@@ -35,31 +33,25 @@ public class SceneEntityLoader(SceneInstance scene)
{
var refreshed = false;
var oldGroupId = new List<int>();
foreach (var entity in Scene.Entities.Values)
if (!oldGroupId.Contains(entity.GroupID))
oldGroupId.Add(entity.GroupID);
foreach (var entity in Scene.Entities.Values.Where(entity => !oldGroupId.Contains(entity.GroupID)))
oldGroupId.Add(entity.GroupID);
var removeList = new List<IGameEntity>();
var addList = new List<IGameEntity>();
foreach (var group in Scene.FloorInfo!.Groups.Values)
foreach (var group in Scene.FloorInfo!.Groups.Values.Where(group => group.LoadSide != GroupLoadSideEnum.Client).Where(group => !group.GroupName.Contains("TrainVisitor")))
{
if (group.LoadSide == GroupLoadSideEnum.Client) continue;
if (group.GroupName.Contains("TrainVisitor")) continue;
if (oldGroupId.Contains(group.Id)) // check if it should be unloaded
{
if (group.ForceUnloadCondition.IsTrue(Scene.Player.MissionManager!.Data, false) ||
group.UnloadCondition.IsTrue(Scene.Player.MissionManager!.Data, false))
{
foreach (var entity in Scene.Entities.Values)
if (entity.GroupID == group.Id)
{
await Scene.RemoveEntity(entity, false);
removeList.Add(entity);
refreshed = true;
}
foreach (var entity in Scene.Entities.Values.Where(entity => entity.GroupID == group.Id))
{
await Scene.RemoveEntity(entity, false);
removeList.Add(entity);
refreshed = true;
}
Scene.Groups.Remove(group.Id);
}
@@ -67,13 +59,12 @@ public class SceneEntityLoader(SceneInstance scene)
Scene.Player.MissionManager!.GetMainMissionStatus(group.OwnerMainMissionID) !=
MissionPhaseEnum.Accept)
{
foreach (var entity in Scene.Entities.Values)
if (entity.GroupID == group.Id)
{
await Scene.RemoveEntity(entity, false);
removeList.Add(entity);
refreshed = true;
}
foreach (var entity in Scene.Entities.Values.Where(entity => entity.GroupID == group.Id))
{
await Scene.RemoveEntity(entity, false);
removeList.Add(entity);
refreshed = true;
}
Scene.Groups.Remove(group.Id);
}

View File

@@ -115,10 +115,6 @@ public class Connection : DanhengConnection
{
Logger.Error(e.Message, e);
}
finally
{
await ms.DisposeAsync();
}
}
private async Task<bool> HandlePacket(ushort opcode, byte[] header, byte[] payload)
@@ -150,6 +146,17 @@ public class Connection : DanhengConnection
return true;
}
// No handler found
// get the packet name
var packetName = LogMap.GetValueOrDefault(opcode);
if (packetName == null) return false;
var respName = packetName.Replace("Cs", "Sc").Replace("Req", "Rsp"); // Get the response packet name
var respOpcode = LogMap.FirstOrDefault(x => x.Value == respName).Key; // Get the response opcode
// Send Rsp
await SendPacket(respOpcode);
return false;
}
}

View File

@@ -45,11 +45,9 @@ public class EntryPoint
while (true)
{
file = new FileInfo(GetConfig().Path.LogPath + $"/{DateTime.Now:yyyy-MM-dd}-{++counter}.log");
if (!file.Exists && file.Directory != null)
{
file.Directory.Create();
break;
}
if (file is not { Exists: false, Directory: not null }) continue;
file.Directory.Create();
break;
}
Logger.SetLogFile(file);
@@ -160,13 +158,14 @@ public class EntryPoint
{
if ((con as Connection)!.Player!.RogueManager?.GetRogueInstance() != null)
{
status = PlayerStatusEnum.Rogue;
if ((con as Connection)!.Player!.ChessRogueManager?.RogueInstance?.AreaExcel.RogueVersionId ==
RogueSubModeEnum.ChessRogue)
status = PlayerStatusEnum.ChessRogueNous;
else if ((con as Connection)!.Player!.ChessRogueManager?.RogueInstance?.AreaExcel
.RogueVersionId == RogueSubModeEnum.ChessRogueNous)
status = PlayerStatusEnum.ChessRogue;
status =
(con as Connection)!.Player!.ChessRogueManager?.RogueInstance?.AreaExcel
.RogueVersionId switch
{
RogueSubModeEnum.ChessRogue => PlayerStatusEnum.ChessRogueNous,
RogueSubModeEnum.ChessRogueNous => PlayerStatusEnum.ChessRogue,
_ => PlayerStatusEnum.Rogue
};
}
else if ((con as Connection)!.Player!.ChallengeManager?.ChallengeInstance != null)
{
@@ -243,7 +242,7 @@ public class EntryPoint
{
var name = opcode.Name;
var value = (int)opcode.GetValue(null)!;
DanhengConnection.LogMap.Add(value.ToString(), name);
DanhengConnection.LogMap.Add(value, name);
}
}
}