diff --git a/Services/Admin/Pages/Assets/Index.cshtml b/Services/Admin/Pages/Assets/Index.cshtml index 30fdfe4..d2e7179 100644 --- a/Services/Admin/Pages/Assets/Index.cshtml +++ b/Services/Admin/Pages/Assets/Index.cshtml @@ -10,183 +10,289 @@

CDN Assets

-

Browse and manage client assets

+

Browse CDN and manage tracked assets

- - - - - Add Asset +
+ + +
+ + Browse CDN + + + Tracked Assets (@Model.TrackedCount)
- - -
-
-
- - + + @if (Model.Tab == "browse") + { + +
+
+ +
-
- - -
- - Clear - -
- - -
-
-

@Model.TotalCount

-

Total Assets

+ +

+ CDN Path: @Model.CdnBaseUrl/downloads/@Model.Folder/ +

+ +
-

@Model.TextureCount

-

Textures

+
+ @Html.AntiForgeryToken() +
+ + +
+
+ + +
+ +
-
-

@Model.SceneCount

-

Scenes

-
-
-

@Model.ScriptCount

-

Scripts

-
-
- - -
- @foreach (var asset in Model.Assets) + + + @if (Model.Folder?.StartsWith("textures/") == true) { -
- -
- @if (asset.AssetType == ClientAssetType.Texture) - { - @asset.FilePath - } - else - { -
- @switch (asset.AssetType) - { - case ClientAssetType.Scene: - - - - break; - case ClientAssetType.Script: - - - - break; - case ClientAssetType.Audio: - - - - break; - default: - - - - break; - } -

@asset.AssetType

-
- } +
+
+

Preview - @Model.Folder

+

Click an image to copy its path, or use the register button to track it.

+
+
+
+ @foreach (var file in Model.PreviewFiles) + { + var isTracked = Model.TrackedPaths.Contains(file); +
+
+ @System.IO.Path.GetFileName(file) +
+

@System.IO.Path.GetFileName(file)

+ + +
+ + @if (!isTracked) + { +
+ @Html.AntiForgeryToken() + + + +
+ } + else + { + + + + + + } +
+
+ } +
- -
- - @asset.AssetType - -
-
- - -
-

- @System.IO.Path.GetFileName(asset.FilePath) -

-

- @asset.FilePath -

-
- v@asset.Version - @FormatFileSize(asset.FileSize) -
-
- @if (asset.IsRequired) - { - Required - } - @if (!asset.IsEnabled) - { - Disabled - } -
-
- - -
- - - - - - + @if (!Model.PreviewFiles.Any()) + { +

+ Enter file names manually or check that assets exist at this CDN path. +

+ }
} - @if (!Model.Assets.Any()) - { -
- - - -

No assets found.

-
- } -
- - - @if (Model.TotalPages > 1) - { -
-

- Showing @((Model.CurrentPage - 1) * Model.PageSize + 1)-@Math.Min(Model.CurrentPage * Model.PageSize, Model.TotalCount) of @Model.TotalCount -

-
- @if (Model.CurrentPage > 1) - { - ← Prev - } - @Model.CurrentPage / @Model.TotalPages - @if (Model.CurrentPage < Model.TotalPages) - { - Next → - } -
+ +
+

Quick Test - Enter filenames (comma separated)

+
+ + + + +
} + else + { + +
+
+ +
+ + +
+
+ + +
+ + Clear +
+
+ + +
+
+

@Model.TrackedCount

+

Total Tracked

+
+
+

@Model.TextureCount

+

Textures

+
+
+

@Model.SceneCount

+

Scenes

+
+
+

@Model.ScriptCount

+

Scripts

+
+
+ + +
+ @foreach (var asset in Model.Assets) + { +
+
+ @if (asset.AssetType == ClientAssetType.Texture) + { + @asset.FilePath + } + else + { +
+ @switch (asset.AssetType) + { + case ClientAssetType.Scene: + + + + break; + case ClientAssetType.Script: + + + + break; + default: + + + + break; + } +

@asset.AssetType

+
+ } +
+ @asset.AssetType +
+
+
+

@System.IO.Path.GetFileName(asset.FilePath)

+

@asset.FilePath

+
+ v@asset.Version + @FormatFileSize(asset.FileSize) +
+
+
+ +
+ @Html.AntiForgeryToken() + + +
+
+
+ } + + @if (!Model.Assets.Any()) + { +
+

No tracked assets found.

+
+ } +
+ + + @if (Model.TotalPages > 1) + { +
+

+ Page @Model.CurrentPage of @Model.TotalPages +

+
+ @if (Model.CurrentPage > 1) + { + ← Prev + } + @if (Model.CurrentPage < Model.TotalPages) + { + Next → + } +
+
+ } + }
+@if (!string.IsNullOrEmpty(Model.Message)) +{ +
+ @Model.Message +
+} + @functions { string FormatFileSize(long bytes) { @@ -198,10 +304,14 @@ @section Scripts { diff --git a/Services/Admin/Pages/Assets/Index.cshtml.cs b/Services/Admin/Pages/Assets/Index.cshtml.cs index 394f975..343fa38 100644 --- a/Services/Admin/Pages/Assets/Index.cshtml.cs +++ b/Services/Admin/Pages/Assets/Index.cshtml.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Server.Data; using Server.Data.Entities; +using System.Security.Cryptography; namespace VoidScavengers.Services.Admin.Pages.Assets; @@ -14,6 +15,21 @@ public class IndexModel : AdminPageModel _db = db; } + // Tab state + [FromQuery(Name = "tab")] + public string Tab { get; set; } = "browse"; + + // Browse tab + [FromQuery(Name = "folder")] + public string Folder { get; set; } = "textures/items"; + + [FromQuery(Name = "files")] + public string? FileList { get; set; } + + public List PreviewFiles { get; set; } = new(); + public HashSet TrackedPaths { get; set; } = new(); + + // Tracked tab public List Assets { get; set; } = new(); [FromQuery(Name = "search")] @@ -26,18 +42,22 @@ public class IndexModel : AdminPageModel public int CurrentPage { get; set; } = 1; public int PageSize { get; set; } = 24; - public int TotalCount { get; set; } - public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize); + public int TotalPages { get; set; } + // Stats + public int TrackedCount { get; set; } public int TextureCount { get; set; } public int SceneCount { get; set; } public int ScriptCount { get; set; } public string CdnBaseUrl { get; set; } = "https://assets.aeternalisgames.com"; + + public string? Message { get; set; } + public bool IsError { get; set; } public string GetPageUrl(int page) { - var parts = new List { $"page={page}" }; + var parts = new List { $"tab=tracked", $"page={page}" }; if (!string.IsNullOrEmpty(Search)) parts.Add($"search={Uri.EscapeDataString(Search)}"); if (TypeFilter.HasValue) parts.Add($"type={TypeFilter}"); return "/admin/assets?" + string.Join("&", parts); @@ -45,30 +65,129 @@ public class IndexModel : AdminPageModel public async Task OnGetAsync() { - if (CurrentPage < 1) CurrentPage = 1; - var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL"); if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl; // Stats + TrackedCount = await _db.ClientAssets.CountAsync(); TextureCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Texture); SceneCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Scene); ScriptCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Script); + + // Get tracked paths for comparison + TrackedPaths = (await _db.ClientAssets.Select(a => a.FilePath).ToListAsync()).ToHashSet(); - var query = _db.ClientAssets.AsQueryable(); + if (Tab == "browse") + { + // Generate preview files based on folder and file list + if (!string.IsNullOrEmpty(FileList)) + { + var files = FileList.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + PreviewFiles = files.Select(f => $"{Folder}/{f}".Replace("//", "/")).ToList(); + } + else + { + // Default preview files - try common naming patterns + // For items, try fb001.png through fb999.png pattern + if (Folder == "textures/items") + { + // Just show a few placeholder suggestions + PreviewFiles = Enumerable.Range(1, 50) + .Select(i => $"{Folder}/fb{i:D3}.png") + .ToList(); + } + else if (Folder == "textures/mobs") + { + // Try common mob names + var mobNames = new[] { "skeleton", "zombie", "slime", "goblin", "orc", "spider", "bat", "wolf", "bear", "demon" }; + PreviewFiles = mobNames.Select(m => $"{Folder}/{m}.png").ToList(); + } + } + } + else + { + // Tracked assets tab + if (CurrentPage < 1) CurrentPage = 1; - if (!string.IsNullOrWhiteSpace(Search)) - query = query.Where(a => a.FilePath.Contains(Search)); + var query = _db.ClientAssets.AsQueryable(); - if (TypeFilter.HasValue) - query = query.Where(a => a.AssetType == TypeFilter.Value); + if (!string.IsNullOrWhiteSpace(Search)) + query = query.Where(a => a.FilePath.Contains(Search)); - TotalCount = await query.CountAsync(); + if (TypeFilter.HasValue) + query = query.Where(a => a.AssetType == TypeFilter.Value); - Assets = await query - .OrderByDescending(a => a.UpdatedAt) - .Skip((CurrentPage - 1) * PageSize) - .Take(PageSize) - .ToListAsync(); + var total = await query.CountAsync(); + TotalPages = (int)Math.Ceiling(total / (double)PageSize); + + Assets = await query + .OrderByDescending(a => a.UpdatedAt) + .Skip((CurrentPage - 1) * PageSize) + .Take(PageSize) + .ToListAsync(); + } + } + + public async Task OnPostRegisterAssetAsync(string filePath, ClientAssetType assetType) + { + if (string.IsNullOrWhiteSpace(filePath)) + { + Message = "File path is required."; + IsError = true; + return RedirectToPage(new { tab = Tab, folder = Folder }); + } + + // Normalize path + filePath = filePath.Trim().Replace("\\", "/"); + if (filePath.StartsWith("/")) filePath = filePath.Substring(1); + + // Check if already tracked + var existing = await _db.ClientAssets.FirstOrDefaultAsync(a => a.FilePath == filePath); + if (existing != null) + { + Message = $"Asset '{filePath}' is already tracked."; + IsError = true; + return RedirectToPage(new { tab = Tab, folder = Folder }); + } + + // Create new asset tracking entry + var asset = new ClientAsset + { + FilePath = filePath, + AssetType = assetType, + FileHash = GenerateHash(filePath), // Placeholder hash based on path + FileSize = 0, // Unknown without actually fetching the file + Version = 1, + IsRequired = false, + IsEnabled = true, + CreatedAt = DateTime.UtcNow, + UpdatedAt = DateTime.UtcNow + }; + + _db.ClientAssets.Add(asset); + await _db.SaveChangesAsync(); + + Message = $"Asset '{filePath}' registered successfully."; + return RedirectToPage(new { tab = Tab, folder = Folder }); + } + + public async Task OnPostDeleteAssetAsync(int assetId) + { + var asset = await _db.ClientAssets.FindAsync(assetId); + if (asset != null) + { + _db.ClientAssets.Remove(asset); + await _db.SaveChangesAsync(); + Message = $"Asset '{asset.FilePath}' untracked."; + } + return RedirectToPage(new { tab = "tracked" }); + } + + private string GenerateHash(string input) + { + using var md5 = MD5.Create(); + var bytes = System.Text.Encoding.UTF8.GetBytes(input + DateTime.UtcNow.Ticks); + var hash = md5.ComputeHash(bytes); + return Convert.ToHexString(hash).ToLower(); } } diff --git a/Services/Admin/Pages/Items/Edit.cshtml b/Services/Admin/Pages/Items/Edit.cshtml index 89b43ee..68640f1 100644 --- a/Services/Admin/Pages/Items/Edit.cshtml +++ b/Services/Admin/Pages/Items/Edit.cshtml @@ -281,7 +281,7 @@ @section Scripts {