Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions JortPob.Tests/DescribeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ float ConvertValue(float colorValue)
Color ConvertColor(Color color)
{
byte alpha = color.A;
byte red = (byte)(ConvertValue(color.R / 255f) * 255);
byte green = (byte)(ConvertValue(color.G / 255f) * 255);
byte blue = (byte)(ConvertValue(color.B / 255f) * 255);
byte red = (byte)Math.Round(ConvertValue(color.R / 255f) * 255);
byte green = (byte)Math.Round(ConvertValue(color.G / 255f) * 255);
byte blue = (byte)Math.Round(ConvertValue(color.B / 255f) * 255);
return Color.FromArgb(alpha, red, green, blue);
}

Expand All @@ -38,7 +38,7 @@ Color ConvertColor(Color color)
[Color.ForestGreen, Color.DarkSalmon, Color.Chocolate]];
Color[][] expectedOutputColor = inputColors.Select(cArr => cArr.Select(ConvertColor).ToArray()).ToArray();

var inputBitmap = new Bitmap(3, 3, PixelFormat.Format32bppArgb);
using var inputBitmap = new Bitmap(3, 3, PixelFormat.Format32bppArgb);
for (var h = 0; h < inputColors.Length; h++)
{
for (var w = 0; w < inputColors[h].Length; w++)
Expand All @@ -47,7 +47,7 @@ Color ConvertColor(Color color)
}
}

var convertedBitmap = Utility.LinearToSRGB(inputBitmap);
using var convertedBitmap = Utility.LinearToSRGB(inputBitmap);
for (var h = 0; h < inputColors.Length; h++)
{
for (var w = 0; w < inputColors[h].Length; w++)
Expand Down
14 changes: 8 additions & 6 deletions JortPob/BigTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System.Diagnostics;
using System.Numerics;

#nullable enable

namespace JortPob
{
/* BigTile is a 2x2 grid of Tiles. Sort of like an LOD type thing. */
[DebuggerDisplay("Big m{map}_{coordinate.x}_{coordinate.y}_{block} :: [{cells.Count}] Cells")]
public class BigTile : BaseTile
{
public HugeTile huge;
public HugeTile? huge { get; set; }
public List<Tile> tiles;

public BigTile(int m, int x, int y, int b) : base(m, x, y, b)
Expand Down Expand Up @@ -39,7 +41,7 @@ public bool PositionInside(Vector3 position)
public override void AddCell(Cell cell)
{
cells.Add(cell);
Tile tile = GetTile(cell.center);
var tile = GetTile(cell.center);
if (tile == null) { Lort.Log($" ## WARNING ## Cell fell outside of reality [{cell.coordinate.x}, {cell.coordinate.y}] -- {cell.name} :: B00", Lort.Type.Debug); return; }
tile.AddCell(cell);
}
Expand All @@ -50,27 +52,27 @@ public override void AddCell(Cell cell)
switch (content)
{
case AssetContent a:
ModelInfo modelInfo = cache.GetModel(a.mesh);
ModelInfo modelInfo = cache.GetModel(a.mesh) ?? throw new System.Exception($"Asset mesh is invalid '{a.mesh}'");
if (modelInfo.size * (content.scale*0.01f) > Const.CONTENT_SIZE_BIG) {
float x = (coordinate.x * 2f * Const.TILE_SIZE) + (Const.TILE_SIZE * 0.5f);
float y = (coordinate.y * 2f * Const.TILE_SIZE) + (Const.TILE_SIZE * 0.5f);
content.relative = (content.position + Const.LAYOUT_COORDINATE_OFFSET) - new Vector3(x, 0, y);
Tile t = GetTile(cell.center);
var t = GetTile(cell.center);
if (t == null) { break; } // Content fell outside of the bounds of any valid msbs. BAD!
content.load = t.coordinate;
base.AddContent(cache, cell, content);
break;
}
goto default;
default:
Tile tile = GetTile(cell.center);
var tile = GetTile(cell.center);
if (tile == null) { break; } // Content fell outside of the bounds of any valid msbs. BAD!
tile.AddContent(cache, cell, content);
break;
}
}

public Tile GetTile(Vector3 position)
public Tile? GetTile(Vector3 position)
{
foreach (Tile tile in tiles)
{
Expand Down
19 changes: 10 additions & 9 deletions JortPob/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void AddConvertedEmitter(EmitterContent emitterContent)
/* Big stupid load function */
public static Cache Load(ESM esm)
{
string manifestPath = Const.CACHE_PATH + @"cache.json";
string manifestPath = Path.Combine(Const.CACHE_PATH, "cache.json");

/* Cache Exists ? */
if (File.Exists(manifestPath))
Expand Down Expand Up @@ -324,9 +324,9 @@ void Scoop(List<Content> contents)
nu.liquids = LiquidManager.GenerateLiquids(esm, materialContext);

/* Add some pregen assets */
ModelInfo boxModelInfo = new("InteriorShadowBox", $"meshes\\interior_shadow_box.flver", 100);
ModelConverter.NIFToFLVER(materialContext, boxModelInfo, false, Utility.ResourcePath(@"mesh\\box.nif"), $"{Const.CACHE_PATH}{boxModelInfo.path}");
FLVER2 boxFlver = FLVER2.Read($"{Const.CACHE_PATH}{boxModelInfo.path}"); // we need this box to be exactly 1 unit in each direction no matter what so we just edit it real quick
ModelInfo boxModelInfo = new("InteriorShadowBox", @"meshes\interior_shadow_box.flver", 100);
ModelConverter.NIFToFLVER(materialContext, boxModelInfo, false, Utility.ResourcePath(@"mesh\box.nif"), Path.Combine(Const.CACHE_PATH, boxModelInfo.path));
FLVER2 boxFlver = FLVER2.Read(Path.Combine(Const.CACHE_PATH, boxModelInfo.path)); // we need this box to be exactly 1 unit in each direction no matter what so we just edit it real quick
foreach (FLVER.Vertex v in boxFlver.Meshes[0].Vertices)
{
float x = v.Position.X > 0f ? .5f : -.5f;
Expand All @@ -335,11 +335,12 @@ void Scoop(List<Content> contents)
v.Position = new Vector3(x, y, z);
}
BoundingBoxSolver.FLVER(boxFlver); // redo bounding box since we edited the mesh
boxFlver.Write($"{Const.CACHE_PATH}{boxModelInfo.path}");
boxFlver.Write(Path.Combine(Const.CACHE_PATH, boxModelInfo.path));
nu.assets.Add(boxModelInfo);

string defaultCollisionObjPath = "meshes\\default_collision_plane.obj";
if(!File.Exists($"{Const.CACHE_PATH}\\{defaultCollisionObjPath}")) { File.Copy(Utility.ResourcePath(@"mesh\plane.obj.file"), $"{Const.CACHE_PATH}{defaultCollisionObjPath}"); }
string defaultCollisionObjPath = @"meshes\default_collision_plane.obj";
if(!File.Exists(Path.Combine(Const.CACHE_PATH, defaultCollisionObjPath)))
File.Copy(Utility.ResourcePath(@"mesh\plane.obj.file"), Path.Combine(Const.CACHE_PATH, defaultCollisionObjPath));
nu.defaultCollision = new("DefaultCollisionPlane", defaultCollisionObjPath);

/* Write textures */
Expand Down Expand Up @@ -441,14 +442,14 @@ void Scoop(List<Content> contents)
}

/* Write new cache file */
string jsonOutput = JsonSerializer.Serialize(nu, new JsonSerializerOptions { IncludeFields = true, NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals });
string jsonOutput = JsonSerializer.Serialize(nu, new JsonSerializerOptions { IncludeFields = true, NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals });
File.WriteAllText(manifestPath, jsonOutput);
Lort.Log($"Generated new cache: {Const.CACHE_PATH}", Lort.Type.Main);
}

/* Load cache manifest */
string tempRawJson = File.ReadAllText(manifestPath);
Cache cache = JsonSerializer.Deserialize<Cache>(tempRawJson, new JsonSerializerOptions { IncludeFields = true, NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals })!;
Cache cache = JsonSerializer.Deserialize<Cache>(tempRawJson, new JsonSerializerOptions { IncludeFields = true, NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals })!;
return cache!;
}
}
Expand Down
7 changes: 7 additions & 0 deletions JortPob/Common/Bind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.IO;
using System.Linq;

#nullable enable

namespace JortPob.Common
{
public class Bind
Expand Down Expand Up @@ -84,6 +86,11 @@ public static void BindEmitters(Cache cache)
{
foreach(EmitterInfo emitterInfo in cache.emitters)
{
if (emitterInfo.model == null)
{
Lort.LogDebug($" ## WARNING ## Trying to bind EmitterInfo {emitterInfo.id} without a model!");
continue;
}
string outPath = Path.Combine(Const.OUTPUT_PATH, @$"asset\aeg\{emitterInfo.AssetPath()}.geombnd.dcx");

// Bind up emitter asset flver
Expand Down
4 changes: 3 additions & 1 deletion JortPob/Common/Const.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.IO;
using System.Numerics;

#nullable enable

namespace JortPob.Common
{
public static class Const
Expand Down Expand Up @@ -137,7 +139,7 @@ public static class Const
public static readonly bool DEBUG_SKIP_FMG_PARAM_SORTING = false;
public static readonly bool DEBUG_SKIP_ESD = false; // skip building dialog esd for npcs, can be slow
public static readonly bool DEBUG_SKIP_NICE_WATER_CIRCLIFICATION = true; // slow as shit, skipping this saves about a minute per build
public static readonly string DEBUG_EXCLUSIVE_CELL_BUILD_BY_NAME = null; // set to "null" to build entire map.
public static readonly string? DEBUG_EXCLUSIVE_CELL_BUILD_BY_NAME = null; // set to "null" to build entire map.
public static readonly int[] DEBUG_EXCLUSIVE_BUILD_BY_BOX = new int[] { -3, -10, -1, -8 }; // also set to null to build entire map. format x1, y1, x2, y2. smaller values first, 1 = 1 cell, use cell coordinates
// seyda neen area (small) = new int[] {-3, -10, -1, -8 }
// seyda neen area (large) = new int[] { -5, -15, 5, -5 }
Expand Down
40 changes: 24 additions & 16 deletions JortPob/Common/DDS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Runtime.InteropServices;
using TeximpNet.DDS;

#nullable enable

namespace JortPob.Common
{
public class DDS
Expand Down Expand Up @@ -124,7 +126,7 @@ public static byte[] MakeTextureFromPixelData(Byte4[] pixels, int width, int hei
lock(_lock) {

/* For some damn reason the System.Drawing.Common is a NuGet dll. Something something windows only something */
Bitmap img = new(width, height);
using Bitmap img = new(width, height);
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
Expand All @@ -147,22 +149,25 @@ public static byte[] MakeTextureFromPixelData(Byte4[] pixels, int width, int hei
ScratchImage sImage = TexHelper.Instance.LoadFromWICMemory(pinnedArray.AddrOfPinnedObject(), pngBytes.Length, WIC_FLAGS.DEFAULT_SRGB);

if (scaleX != null && scaleY != null)
sImage = sImage.Resize(0, scaleX.Value, scaleY.Value, filterFlags);
{
var tImage = sImage.Resize(0, scaleX.Value, scaleY.Value, filterFlags);
sImage.Dispose();
sImage = tImage;
}

sImage = sImage.Compress(format, texCompFlag, 0.5f);
sImage.OverrideFormat(format);
using var compressedSImage = sImage.Compress(format, texCompFlag, 0.5f);
sImage.Dispose();
compressedSImage.OverrideFormat(format);

/* Save the DDS to memory stream and then read the stream into a byte array. */
byte[] bytes;
using (UnmanagedMemoryStream uStream = sImage.SaveToDDSMemory(ddsFlags))
using (UnmanagedMemoryStream uStream = compressedSImage.SaveToDDSMemory(ddsFlags))
{
bytes = new byte[uStream.Length];
uStream.Read(bytes);
}

pinnedArray.Free(); //We have to manually free pinned stuff, or it will never be collected.
img.Dispose();
sImage.Dispose();
return bytes;
}
}
Expand Down Expand Up @@ -196,7 +201,7 @@ public static byte[] Scale(byte[] dds)
return scaled;
} catch (Exception ex) {
Lort.Log($"{ex.Message} {ex.StackTrace} {ex.Source}", Lort.Type.Debug);
return null;
throw;
}
}

Expand All @@ -207,12 +212,16 @@ public static Bitmap DDStoBitmap(byte[] dds, int width = 0, int height = 0)
DirectXTexNet.ScratchImage scratchImage = DirectXTexNet.TexHelper.Instance.LoadFromDDSMemory(pinnedArray.AddrOfPinnedObject(), dds.Length, DirectXTexNet.DDS_FLAGS.NONE);
if (TexHelper.Instance.IsCompressed(scratchImage.GetMetadata().Format))
{
scratchImage = scratchImage.Decompress(DirectXTexNet.DXGI_FORMAT.R8G8B8A8_UNORM);
var tempScratchImage = scratchImage.Decompress(DirectXTexNet.DXGI_FORMAT.R8G8B8A8_UNORM);
scratchImage.Dispose();
scratchImage = tempScratchImage;
}

if(width > 0 || height > 0)
{
scratchImage = scratchImage.Resize(0, width, height, TEX_FILTER_FLAGS.CUBIC);
var tempScratchImage = scratchImage.Resize(0, width, height, TEX_FILTER_FLAGS.CUBIC);
scratchImage.Dispose();
scratchImage = tempScratchImage;
}

Bitmap bitmap = new(scratchImage.GetImage(0).Width, scratchImage.GetImage(0).Height);
Expand Down Expand Up @@ -258,23 +267,22 @@ public static byte[] BitmapToDDS

/* pin the array to memory so the garbage collector can't mess with it, */
GCHandle pinnedArray = GCHandle.Alloc(pngBytes, GCHandleType.Pinned);
ScratchImage sImage = TexHelper.Instance.LoadFromWICMemory(pinnedArray.AddrOfPinnedObject(), pngBytes.Length, WIC_FLAGS.DEFAULT_SRGB);
using ScratchImage sImage = TexHelper.Instance.LoadFromWICMemory(pinnedArray.AddrOfPinnedObject(), pngBytes.Length, WIC_FLAGS.DEFAULT_SRGB);

//sImage = sImage.Compress(DXGI_FORMAT.BC2_UNORM_SRGB, texCompFlag, 0.5f);
// sImage = sImage.Decompress(DirectXTexNet.DXGI_FORMAT.R8G8B8A8_UNORM);
sImage = sImage.Compress(format, texCompFlag, 0.5f);
sImage.OverrideFormat(format);
// sImage = sImage.Decompress(DirectXTexNet.DXGI_FORMAT.R8G8B8A8_UNORM);
using var compressedImage = sImage.Compress(format, texCompFlag, 0.5f);
compressedImage.OverrideFormat(format);

/* Save the DDS to memory stream and then read the stream into a byte array. */
byte[] bytes;
using (UnmanagedMemoryStream uStream = sImage.SaveToDDSMemory(ddsFlags))
using (UnmanagedMemoryStream uStream = compressedImage.SaveToDDSMemory(ddsFlags))
{
bytes = new byte[uStream.Length];
uStream.Read(bytes);
}

pinnedArray.Free(); //We have to manually free pinned stuff, or it will never be collected.
sImage.Dispose();
return bytes;
}

Expand Down
37 changes: 25 additions & 12 deletions JortPob/Common/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,27 @@
using System.Linq;
using System.Threading;

#nullable enable

namespace JortPob.Common
{
public class Lort
{
public static ConcurrentBag<string> mainOutput { get; private set; }
public static ConcurrentBag<string> debugOutput { get; private set; }
public static string progressOutput { get; private set; }
public static ConcurrentBag<string> mainOutput { get; } = new();
public static ConcurrentBag<string> debugOutput { get; } = new();
public static string progressOutput { get; private set; } = string.Empty;
public static int total { get; private set; }
public static int current { get; private set; }
public static bool update { get; set; }
public static string logFilePath { get; private set; }
public static string logFilePath { get; private set; } = string.Empty;

public static void Initialize()
{
mainOutput = new();
debugOutput = new();
progressOutput = string.Empty;
total = 0;
current = 0;
update = false;

if (!Directory.Exists(Path.Combine(Const.OUTPUT_PATH, "logs")))
{
Directory.CreateDirectory(Path.Combine(Const.OUTPUT_PATH, "logs"));
}
Directory.CreateDirectory(Path.Combine(Const.OUTPUT_PATH, "logs"));

logFilePath = Path.Combine(Const.OUTPUT_PATH, @$"logs\jortpob-log-{DateTime.UtcNow.ToLongTimeString().Replace(":", "").Replace(" PM", "")}.txt");
File.WriteAllText(logFilePath, "");
Expand All @@ -42,7 +38,7 @@ public enum Type
Debug
}

public static void Log(string message, Lort.Type type)
public static void Log(string message, Type type)
{
switch (type)
{
Expand All @@ -55,6 +51,20 @@ public static void Log(string message, Lort.Type type)
AppendTextToLog(message, type);
}

public static void LogDebug(string message)
{
debugOutput.Add(message);
update = true;
AppendTextToLog(message, Type.Debug);
}

public static void LogMain(string message)
{
mainOutput.Add(message);
update = true;
AppendTextToLog(message, Type.Main);
}

public static void NewTask(string task, int max)
{
progressOutput = $"{task}";
Expand All @@ -71,6 +81,9 @@ public static void TaskIterate()

private static void AppendTextToLog(string message, Type type)
{
if (string.IsNullOrEmpty(logFilePath))
return;

switch (type)
{
case Type.Main:
Expand Down
Loading