ReboundEmulator/Controllers/ConfigAndAssetsController.cs

116 lines
4.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using NotPogo.Model;
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 GamePage(string? name,
string game,
string appletClass,
string site,
string locale,
string hostedGameClass)
{
var gamePage = new GamePage
{
GameName = name ?? game,
InternalName = game,
JavaAppletClass = appletClass,
JavaAppletParameters = new()
{
{ "name", game },
{ "hosted.game.class", hostedGameClass },
{ "standalone", "false" },
{ "type", game },
{ "config", $"client-props.jsp?game={game}" },
{ "locale", locale },
{ "site", site },
{ "rkey", "" },
{ "unid", "" },
{ "sourceid", "" },
{ "launchPage", "" },
{ "lkey", "" },
{ "cnid", "" },
{ "isSingleApplet", "true" },
{ "browserInfo", "" }
}
};
return View(gamePage);
}
[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}");
_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"
};
}
}