ReboundEmulator/Controllers/ConfigAndAssetsController.cs
2026-09-14 17:02:26 -07:00

110 lines
4.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using System.IO.Compression;
using System.Net;
using System.Reflection.Metadata.Ecma335;
using System.Text;
namespace NotPogo.Controllers
{
[Route("/")]
public class ConfigAndAssetsController(ILogger<ConfigAndAssetsController> logger) : Controller
{
private readonly ILogger<ConfigAndAssetsController> _logger = logger;
public IActionResult Index() => Ok("Not Pogo Is Running!");
// might aswell provide a minimal way to play the games supported
[HttpGet]
[Route("/play-game")]
public IActionResult GetGamePage(string game)
{
var path = Path.Combine(AppContext.BaseDirectory, "StaticFiles", "GameAssets", game, "index.html");
_logger.LogInformation("Attempting To Serve Game Applet At {Path}", path);
return new ContentResult
{
Content = System.IO.File.Exists(path) ? System.IO.File.ReadAllText(path) : $"Game Not Found - {game}",
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK
};
}
[HttpGet]
[Route("/get-jar")]
public IActionResult GetAppletCode(string game)
{
var path = Path.Combine(AppContext.BaseDirectory, "StaticFiles", "JARs", game + "-en_US.jar");
if (!System.IO.File.Exists(path))
return NotFound("Applet Not Found");
else
return File(System.IO.File.ReadAllBytes(path), "application/x-java-archive", fileDownloadName: $"{game}.jar");
}
[HttpGet]
[Route("/blank.html")]
public IActionResult GetEmptyPage() => Ok();
[HttpGet]
[Route("/client-props.jsp")]
public async Task<IActionResult> GetConfigAsync(string game)
{
var path = Path.Combine(AppContext.BaseDirectory, "StaticFiles", "Configs", $"{game}.txt");
byte[] config = System.IO.File.Exists(path) ? await System.IO.File.ReadAllBytesAsync(path) : Encoding.UTF8.GetBytes($"Configration Missing For {game} :(");
// client props needs to be gzip compressed for whatever reason (probably to throw off dumpers or something idk)
byte[] compressed;
using (var ms = new MemoryStream())
{
using (var gzip = new GZipStream(ms, CompressionLevel.Fastest, leaveOpen: true))
{
await gzip.WriteAsync(config);
}
compressed = ms.ToArray();
}
return File(compressed, "text/plain");
}
[HttpGet]
[Route("/assets")]
public async Task<IActionResult> GetGameAssetAsync(string game, string assetPath)
{
var pathToAsset = Path.Combine(AppContext.BaseDirectory, "StaticFiles", "GameAssets", game, assetPath);
byte[] assetBytes = System.IO.File.Exists(pathToAsset) ?
await System.IO.File.ReadAllBytesAsync(pathToAsset) :
Encoding.UTF8.GetBytes($"Missing Asset File For {game} - {assetPath}");
//var contentType = GetContentType(pathToAsset);
//if(contentType == "text/xml")
//{
// // response needs to be gzipped
// byte[] compressed;
// using (var ms = new MemoryStream())
// {
// using (var gzip = new GZipStream(ms, CompressionLevel.Fastest, leaveOpen: true))
// {
// await gzip.WriteAsync(assetBytes);
// }
// compressed = ms.ToArray();
// }
// _logger.LogInformation("Serving Compressed Game Asset {Game}/{FileName}", game, assetPath);
// return File(compressed, GetContentType(pathToAsset));
//}
_logger.LogInformation("Serving Game Asset {Game}/{FileName}", game, assetPath);
return File(assetBytes, GetContentType(pathToAsset));
}
private string GetContentType(string assetFileName) => Path.GetExtension(assetFileName).ToLowerInvariant() switch
{
".gif" => "image/gif",
".jpg" or ".jpeg" => "image/jpeg",
".au" => "audio/basic",
".xml" => "text/xml",
".dat" => "application/octet-stream",
// ".txt" => "text/plain",
_ => "application/octet-stream"
};
}
}