mirror of
https://github.com/EpinelPS/EpinelPS.git
synced 2025-12-12 15:04:36 +01:00
refactoring
This commit is contained in:
@@ -32,6 +32,7 @@ public partial class MainView : UserControl
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (TxtIpAddress.Text == null) TxtIpAddress.Text = "";
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -25,7 +25,6 @@ namespace nksrv
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
public static readonly HttpClient AssetDownloader = new(new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.All});
|
||||
static async Task Main()
|
||||
{
|
||||
Logger.UnregisterLogger<ConsoleLogger>();
|
||||
@@ -33,11 +32,7 @@ namespace nksrv
|
||||
Logger.Info("Initializing database");
|
||||
JsonDb.Save();
|
||||
|
||||
Logger.Info("Loading static data");
|
||||
await StaticDataParser.Load();
|
||||
|
||||
Logger.Info("Parsing static data");
|
||||
await StaticDataParser.Instance.Parse();
|
||||
StaticDataParser.Instance.GetAllCostumes(); // force static data to be loaded
|
||||
|
||||
Logger.Info("Initialize handlers");
|
||||
LobbyHandler.Init();
|
||||
@@ -178,45 +173,15 @@ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
}
|
||||
private static async Task HandleAsset(IHttpContext ctx)
|
||||
{
|
||||
string targetFile = GetCachePathForPath(ctx.Request.RawUrl);
|
||||
var targetDir = Path.GetDirectoryName(targetFile);
|
||||
if (targetDir == null)
|
||||
string? targetFile = await AssetDownloadUtil.DownloadOrGetFileAsync(ctx.Request.RawUrl, ctx.CancellationToken);
|
||||
|
||||
if (targetFile == null)
|
||||
{
|
||||
Logger.Error($"ERROR: Directory name cannot be null for request " + ctx.Request.RawUrl + ", file path is " + targetFile);
|
||||
Logger.Error("Download failed: " + ctx.RequestedPath);
|
||||
ctx.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
Directory.CreateDirectory(targetDir);
|
||||
|
||||
if (!File.Exists(targetFile))
|
||||
{
|
||||
Logger.Info("Download " + targetFile);
|
||||
|
||||
// TODO: Ip might change for cloud.nikke-kr.com
|
||||
string @base = ctx.Request.RawUrl.StartsWith("/prdenv") ? "prdenv" : "media";
|
||||
if (ctx.Request.RawUrl.StartsWith("/PC"))
|
||||
@base = "PC";
|
||||
|
||||
var requestUri = new Uri("https://35.190.17.65/" + @base + ctx.RequestedPath);
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.TryAddWithoutValidation("host", "cloud.nikke-kr.com");
|
||||
using var response = await AssetDownloader.SendAsync(request);
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
if (!File.Exists(targetFile))
|
||||
{
|
||||
using var fss = new FileStream(targetFile, FileMode.CreateNew);
|
||||
await response.Content.CopyToAsync(fss, ctx.CancellationToken);
|
||||
|
||||
fss.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error("FAILED TO DOWNLOAD FILE: " + ctx.RequestedPath);
|
||||
ctx.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
using var fss = new FileStream(targetFile, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
@@ -230,7 +195,7 @@ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
ctx.Response.ContentType = "application/json";
|
||||
}
|
||||
ctx.Response.StatusCode = 200;
|
||||
//ctx.Response.ContentLength64 = fss.Length; // TODO: This causes chrome to download content very slowl
|
||||
//ctx.Response.ContentLength64 = fss.Length; // TODO: This causes chrome to download content very slowly
|
||||
|
||||
await fss.CopyToAsync(responseStream, ctx.CancellationToken);
|
||||
fss.Close();
|
||||
|
||||
@@ -28,8 +28,8 @@ message ResGetServerInfo {
|
||||
|
||||
|
||||
message NetMaintenanceWindow {
|
||||
//Timestamp from = 1;
|
||||
//Timestamp to = 2;
|
||||
google.protobuf.Timestamp from = 1;
|
||||
google.protobuf.Timestamp to = 2;
|
||||
}
|
||||
|
||||
message MaintenanceNoticeResponse {
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option csharp_namespace = "nksrv";
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "google/protobuf/Duration.proto";
|
||||
@@ -49,9 +49,31 @@ namespace nksrv.StaticInfo
|
||||
private JArray characterCostumeTable;
|
||||
private JArray characterTable;
|
||||
private JArray tutorialTable;
|
||||
|
||||
static StaticDataParser()
|
||||
{
|
||||
Logger.Info("Loading static data");
|
||||
Load().Wait();
|
||||
if (Instance == null) throw new Exception("static data load fail");
|
||||
|
||||
Logger.Info("Parsing static data");
|
||||
Instance.Parse().Wait();
|
||||
}
|
||||
public StaticDataParser(string filePath)
|
||||
{
|
||||
if (!File.Exists(filePath)) throw new ArgumentException("Static data file must exist", nameof(filePath));
|
||||
|
||||
// disable warnings
|
||||
questDataRecords = new();
|
||||
stageDataRecords = new();
|
||||
rewardDataRecords = new();
|
||||
userExpDataRecords = new();
|
||||
chapterCampaignData = new();
|
||||
characterCostumeTable = new();
|
||||
characterTable = new();
|
||||
ZipStream = new();
|
||||
tutorialTable = new();
|
||||
|
||||
DecryptStaticDataAndLoadZip(filePath);
|
||||
if (MainZip == null) throw new Exception("failed to read zip file");
|
||||
}
|
||||
@@ -173,34 +195,8 @@ namespace nksrv.StaticInfo
|
||||
}
|
||||
public static async Task Load()
|
||||
{
|
||||
string targetFile = Program.GetCachePathForPath(StaticDataUrl.Replace("https://cloud.nikke-kr.com", ""));
|
||||
var targetDir = Path.GetDirectoryName(targetFile);
|
||||
if (targetDir == null) throw new Exception("directory name is null for path " + targetDir);
|
||||
|
||||
Directory.CreateDirectory(targetDir);
|
||||
|
||||
if (!File.Exists(targetFile))
|
||||
{
|
||||
// TODO: IP might change
|
||||
var requestUri = new Uri("https://35.190.17.65/" + StaticDataUrl.Replace("https://cloud.nikke-kr.com", ""));
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.TryAddWithoutValidation("host", "cloud.nikke-kr.com");
|
||||
|
||||
Logger.Info("Downloading static game data from server. Please wait.");
|
||||
using var response = await Program.AssetDownloader.SendAsync(request);
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
using var fss = new FileStream(targetFile, FileMode.CreateNew);
|
||||
await response.Content.CopyToAsync(fss);
|
||||
|
||||
fss.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Failed to download static game data");
|
||||
}
|
||||
}
|
||||
|
||||
var targetFile = await AssetDownloadUtil.DownloadOrGetFileAsync(StaticDataUrl, CancellationToken.None);
|
||||
if (targetFile == null) throw new Exception("static data download fail");
|
||||
|
||||
Instance = new(targetFile);
|
||||
}
|
||||
|
||||
58
nksrv/Utils/AssetDownloadUtil.cs
Normal file
58
nksrv/Utils/AssetDownloadUtil.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Swan.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace nksrv.Utils
|
||||
{
|
||||
public class AssetDownloadUtil
|
||||
{
|
||||
public static readonly HttpClient AssetDownloader = new(new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.All });
|
||||
public static async Task<string?> DownloadOrGetFileAsync(string url, CancellationToken cancellationToken)
|
||||
{
|
||||
var rawUrl = url.Replace("https://cloud.nikke-kr.com", "");
|
||||
string targetFile = Program.GetCachePathForPath(rawUrl);
|
||||
var targetDir = Path.GetDirectoryName(targetFile);
|
||||
if (targetDir == null)
|
||||
{
|
||||
Logger.Error($"ERROR: Directory name cannot be null for request " + url + ", file path is " + targetFile);
|
||||
return null;
|
||||
}
|
||||
Directory.CreateDirectory(targetDir);
|
||||
|
||||
if (!File.Exists(targetFile))
|
||||
{
|
||||
Logger.Info("Download " + targetFile);
|
||||
|
||||
// TODO: Ip might change for cloud.nikke-kr.com
|
||||
string @base = rawUrl.StartsWith("/prdenv") ? "prdenv" : "media";
|
||||
if (rawUrl.StartsWith("/PC"))
|
||||
@base = "PC";
|
||||
|
||||
var requestUri = new Uri("https://35.190.17.65/" + @base + rawUrl);
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
||||
request.Headers.TryAddWithoutValidation("host", "cloud.nikke-kr.com");
|
||||
using var response = await AssetDownloader.SendAsync(request);
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
if (!File.Exists(targetFile))
|
||||
{
|
||||
using var fss = new FileStream(targetFile, FileMode.CreateNew);
|
||||
await response.Content.CopyToAsync(fss, cancellationToken);
|
||||
|
||||
fss.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return targetFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user