diff --git a/Resources/data/.gitignore b/Resources/data/.gitignore deleted file mode 100644 index f59ec20..0000000 --- a/Resources/data/.gitignore +++ /dev/null @@ -1 +0,0 @@ -* \ No newline at end of file diff --git a/Resources/download/.gitignore b/Resources/download/.gitignore deleted file mode 100644 index f59ec20..0000000 --- a/Resources/download/.gitignore +++ /dev/null @@ -1 +0,0 @@ -* \ No newline at end of file diff --git a/Resources/url.txt b/Resources/url.txt deleted file mode 100644 index 4dd4f0d..0000000 --- a/Resources/url.txt +++ /dev/null @@ -1 +0,0 @@ -https://prod-clientpatch.bluearchiveyostar.com/r67_utfwo6vcvx7rhl017phc \ No newline at end of file diff --git a/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Clan.cs b/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Clan.cs index 8a9052f..be28d59 100644 --- a/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Clan.cs +++ b/SCHALE.GameServer/Controllers/Api/ProtocolHandlers/Clan.cs @@ -27,8 +27,8 @@ namespace SCHALE.GameServer.Controllers.Api.ProtocolHandlers { IrcConfig = new() { - HostAddress = Config.Instance.Address, - Port = Config.Instance.Port, + HostAddress = Config.Instance.IRCAddress, + Port = Config.Instance.IRCPort, Password = "" }, AccountClanDB = new() diff --git a/SCHALE.GameServer/Controllers/Data/DataController.cs b/SCHALE.GameServer/Controllers/Data/DataController.cs deleted file mode 100644 index ef0cc09..0000000 --- a/SCHALE.GameServer/Controllers/Data/DataController.cs +++ /dev/null @@ -1,81 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using SCHALE.Common.Crypto; -using SCHALE.Common.NetworkProtocol; -using System.IO; -using System.IO.Compression; -using System.Text; -using System.Text.Json; -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; -using System.Threading.Tasks; - -namespace SCHALE.GameServer.Controllers.Data -{ - [ApiController] - [Route("/data")] - public class DataController : ControllerBase - { - - private readonly string absFolder; - private readonly ILogger logger; - public DataController(ILogger _logger) - { - logger = _logger; - - var folder = Path.GetDirectoryName(AppContext.BaseDirectory); - string dataFolder; - while (true) - { - dataFolder = Path.Join(folder, "Resources/data"); - if (Directory.Exists(dataFolder)) break; - folder = Path.GetDirectoryName(folder); - if (folder == null) - throw new FileNotFoundException($"Excel folder is not found."); - } - absFolder = dataFolder; - } - - - string? AbsPath(string relPath) - { - string filePath = Path.Combine(absFolder, relPath); - if (!System.IO.File.Exists(filePath)) return null; - logger.LogDebug($"Use our own {relPath}."); - return filePath; - } - - [HttpGet("TableBundles/{fileName}")] - public IActionResult GetTableBundles(string fileName) - { - string relPath = $"TableBundles/{fileName}"; - string? filePath = AbsPath(relPath); - if (filePath == null) return CatchAll(relPath); - - if (fileName.EndsWith(".json")) - { - var jsonContent = System.IO.File.ReadAllText(filePath); - return Content(jsonContent, "application/json"); - } - if (fileName.EndsWith(".zip")) - { - var fileStream = System.IO.File.OpenRead(filePath); - return File(fileStream, "application/zip", fileName); - } - if (fileName.EndsWith(".bytes")) - { - var fileStream = System.IO.File.OpenRead(filePath); - return File(fileStream, "application/octet-stream", fileName); - } - return CatchAll(relPath); - } - - [HttpGet("{*catchAll}")] - public IActionResult CatchAll(string catchAll) - { - logger.LogDebug("Data path: {path}", catchAll); - string re = $"https://prod-clientpatch.bluearchiveyostar.com/r68_10iazxytt13razwn7x9n_3/{catchAll}"; - return Redirect(re); - // return RedirectPermanent(re); - } - } -} diff --git a/SCHALE.GameServer/GameServer.cs b/SCHALE.GameServer/GameServer.cs index 2992010..27944e7 100644 --- a/SCHALE.GameServer/GameServer.cs +++ b/SCHALE.GameServer/GameServer.cs @@ -63,32 +63,15 @@ namespace SCHALE.GameServer Log.Information("Starting..."); try { - Log.Information("Downloading Excels..."); - await ExcelTableService.Init(); + // Load Config + Config.Load(); + + // Load Excels + await ExcelTableService.LoadExcels(); // Load Commands CommandFactory.LoadCommands(); - // Load Config - Config.Load(); - - if (Config.Instance.Address == "127.0.0.1") - { - Config.Instance.Address = NetworkInterface - .GetAllNetworkInterfaces() - .Where(i => - i.NetworkInterfaceType != NetworkInterfaceType.Loopback - && i.OperationalStatus == OperationalStatus.Up - ) - .First() - .GetIPProperties() - .UnicastAddresses.Where(a => - a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork - ) - .First() - .Address.ToString(); - Config.Save(); - } var builder = WebApplication.CreateBuilder(args); diff --git a/SCHALE.GameServer/SCHALE.GameServer.csproj b/SCHALE.GameServer/SCHALE.GameServer.csproj index d759da9..d9be2df 100644 --- a/SCHALE.GameServer/SCHALE.GameServer.csproj +++ b/SCHALE.GameServer/SCHALE.GameServer.csproj @@ -20,8 +20,4 @@ - - - - diff --git a/SCHALE.GameServer/Services/ExcelTableService.cs b/SCHALE.GameServer/Services/ExcelTableService.cs index 061698e..83738a7 100644 --- a/SCHALE.GameServer/Services/ExcelTableService.cs +++ b/SCHALE.GameServer/Services/ExcelTableService.cs @@ -6,6 +6,8 @@ using System.Threading.Tasks; using Google.FlatBuffers; using Ionic.Zip; using SCHALE.Common.Crypto; +using SCHALE.GameServer.Utils; +using Serilog; namespace SCHALE.GameServer.Services { @@ -15,63 +17,34 @@ namespace SCHALE.GameServer.Services private readonly ILogger logger = _logger; private readonly Dictionary caches = []; - private static string? ResourcesFolder; - private static string? ExcelFolder; - - private static string GetUrl() + public static async Task LoadExcels() { - string urlPath; - if (ResourcesFolder == null) + var excelZipUrl = $"https://prod-clientpatch.bluearchiveyostar.com/{Config.Instance.VersionId}/TableBundles/Excel.zip"; + + var excelDir = $"{Path.GetDirectoryName(AppContext.BaseDirectory)}/Resources/excel/"; + var excelZipPath = Path.Combine(excelDir, "Excel.zip"); + + if (Directory.Exists(excelDir)) { - var folder = Path.GetDirectoryName(AppContext.BaseDirectory); - while (true) - { - urlPath = Path.Join(folder, "Resources/url.txt"); - if (File.Exists(urlPath)) - break; - folder = Path.GetDirectoryName(folder); - if (folder == null) - throw new FileNotFoundException($"Resources folder is not found."); - } - ResourcesFolder = Path.GetDirectoryName(urlPath); - } - else - { - urlPath = Path.Join(ResourcesFolder, "url.txt"); - } - string url = File.ReadAllText(urlPath); - return url + "/"; - } - - private static async Task GetZip() - { - - string url = GetUrl(); - string filePath = "TableBundles/Excel.zip"; - string zipPath = Path.Combine(ResourcesFolder!, "download", filePath); - - ExcelFolder = zipPath[..^4]; - if (File.Exists(zipPath)) + Log.Information("Excels already downloaded, skipping..."); return; - Directory.CreateDirectory(Path.GetDirectoryName(zipPath)!); - - using HttpClient client = new(); - HttpResponseMessage response = await client.GetAsync(url + filePath); - response.EnsureSuccessStatusCode(); - byte[] content = await response.Content.ReadAsByteArrayAsync(); - File.WriteAllBytes(zipPath, content); - using ZipFile zip = ZipFile.Read(zipPath); - zip.Password = Convert.ToBase64String(TableService.CreatePassword(Path.GetFileName(filePath))); - - foreach (ZipEntry e in zip) - { - e.Extract(ExcelFolder, ExtractExistingFileAction.OverwriteSilently); } - } + + Log.Information("Downloading Excels..."); + Directory.CreateDirectory(excelDir); - public static async Task Init() - { - await GetZip(); + using var client = new HttpClient(); + var zipBytes = await client.GetByteArrayAsync(excelZipUrl); + + File.WriteAllBytes(excelZipPath, zipBytes); + + using (var zip = ZipFile.Read(excelZipPath)) { + zip.Password = Convert.ToBase64String(TableService.CreatePassword(Path.GetFileName(excelZipPath))); + zip.ExtractAll(excelDir, ExtractExistingFileAction.OverwriteSilently); + } + + File.Delete(excelZipPath); + Log.Information($"Excel Version {Config.Instance.VersionId} downloaded! Notice that versions higher than r67 currently does not work"); } /// @@ -80,16 +53,14 @@ namespace SCHALE.GameServer.Services /// /// /// - public T GetTable() - where T : IFlatbufferObject + public T GetTable() where T : IFlatbufferObject { var type = typeof(T); if (caches.TryGetValue(type, out var cache)) return (T)cache; - ArgumentNullException.ThrowIfNull(ExcelFolder); - var bytesFilePath = Path.Join(ExcelFolder, $"{type.Name.ToLower()}.bytes"); + var bytesFilePath = Path.Join(Path.GetDirectoryName(AppContext.BaseDirectory), "Resources/excel/", $"{type.Name.ToLower()}.bytes"); if (!File.Exists(bytesFilePath)) { throw new FileNotFoundException($"bytes files for {type.Name} not found"); @@ -97,12 +68,7 @@ namespace SCHALE.GameServer.Services var bytes = File.ReadAllBytes(bytesFilePath); TableEncryptionService.XOR(type.Name, bytes); - var inst = type.GetMethod( - $"GetRootAs{type.Name}", - BindingFlags.Static | BindingFlags.Public, - [typeof(ByteBuffer)] - )! - .Invoke(null, [new ByteBuffer(bytes)]); + var inst = type.GetMethod($"GetRootAs{type.Name}", BindingFlags.Static | BindingFlags.Public, [typeof(ByteBuffer)])!.Invoke(null, [new ByteBuffer(bytes)]); caches.Add(type, inst!); logger.LogDebug("{Excel} loaded and cached", type.Name); diff --git a/SCHALE.GameServer/Utils/Config.cs b/SCHALE.GameServer/Utils/Config.cs index 65757fe..6b9a4f9 100644 --- a/SCHALE.GameServer/Utils/Config.cs +++ b/SCHALE.GameServer/Utils/Config.cs @@ -1,4 +1,8 @@ -using Serilog; +using SCHALE.GameServer.Controllers.Api.ProtocolHandlers; +using Serilog; +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; using System.Text.Json; namespace SCHALE.GameServer.Utils @@ -7,29 +11,49 @@ namespace SCHALE.GameServer.Utils { public static string ConfigPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json"); - public string Address { get; set; } = "127.0.0.1"; - public int Port { get; set; } = 6667; + public string IRCAddress { get; set; } = "127.0.0.1"; + public int IRCPort { get; set; } = 6667; + + public string VersionId { get; set; } = "r67_utfwo6vcvx7rhl017phc"; public static void Load() { if (!File.Exists(ConfigPath)) + { + Instance.IRCAddress = GetLocalIPv4(NetworkInterfaceType.Wireless80211) == string.Empty ? GetLocalIPv4(NetworkInterfaceType.Ethernet) : GetLocalIPv4(NetworkInterfaceType.Wireless80211); Save(); + } -#if !DOCKER_BUILD string json = File.ReadAllText(ConfigPath); Instance = JsonSerializer.Deserialize(json); -#endif - + Log.Debug($"Config loaded"); } public static void Save() { -#if !DOCKER_BUILD File.WriteAllText(ConfigPath, JsonSerializer.Serialize(Instance)); -#endif Log.Debug($"Config saved"); } + + public static string GetLocalIPv4(NetworkInterfaceType _type) + { + string output = ""; + foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) + { + if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up) + { + foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) + { + if (ip.Address.AddressFamily == AddressFamily.InterNetwork) + { + output = ip.Address.ToString(); + } + } + } + } + return output; + } } }