refactoring

This commit is contained in:
Mikhail
2024-07-11 15:26:43 -04:00
parent 5e4817c222
commit 3ddbe47b7b
6 changed files with 92 additions and 78 deletions

View File

@@ -32,6 +32,7 @@ public partial class MainView : UserControl
return;
}
}
if (TxtIpAddress.Text == null) TxtIpAddress.Text = "";
try
{

View File

@@ -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();

View File

@@ -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 {

View File

@@ -1,6 +0,0 @@
syntax = "proto3";
option csharp_namespace = "nksrv";
import "google/protobuf/timestamp.proto";
import "google/protobuf/Duration.proto";

View File

@@ -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);
}

View 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;
}
}
}