diff --git a/Fushigi.Logger/Logger.cs b/Fushigi.Logger/Logger.cs index 0a230bf8..a30d8f20 100644 --- a/Fushigi.Logger/Logger.cs +++ b/Fushigi.Logger/Logger.cs @@ -1,4 +1,6 @@ -namespace Fushigi.Logger +using System; + +namespace Fushigi.Logger { public static class Logger { @@ -41,10 +43,7 @@ public static void HandleUnhandledException(object sender, UnhandledExceptionEve if (e.ExceptionObject is not Exception exception) return; Console.ForegroundColor = ConsoleColor.Red; - if (exception.StackTrace == null) - Log(exception.Message); - else - Log($"[ERROR] {exception.GetType().Name}: {exception.Message}\n{exception.StackTrace}"); + LogError(exception); Console.ForegroundColor = ConsoleColor.White; Environment.Exit(1); @@ -77,5 +76,13 @@ public static void LogError(object from, object msg) Log($"[ERROR] [{from}]: {msg}"); Console.ForegroundColor = ConsoleColor.White; } + + public static void LogError(Exception e) + { + if (e.StackTrace == null) + Log(e.Message); + else + Log($"[ERROR] {e.GetType().Name}: {e.Message}\n{e.StackTrace}"); + } } } diff --git a/Fushigi/gl/Bfres/Shaders/BfshaShaderRender.cs b/Fushigi/gl/Bfres/Shaders/BfshaShaderRender.cs index 946355a3..8213aa7c 100644 --- a/Fushigi/gl/Bfres/Shaders/BfshaShaderRender.cs +++ b/Fushigi/gl/Bfres/Shaders/BfshaShaderRender.cs @@ -30,10 +30,10 @@ public class BfshaShaderRender private Material Material; public BfshaFile ShaderFile; - public BfshaFile.ShaderModel ShaderModel; + public ShaderModel ShaderModel; //Resources - private BfshaFile.ShaderProgram ShaderProgram; + private ShaderProgram ShaderProgram; private UniformBlock ConstantVSBlock; private UniformBlock ConstantFSBlock; @@ -52,8 +52,11 @@ public virtual void Init(GL gl, BfresRender.BfresModel modelRender, BfresRender. ShaderFile = TryGetShader(material.ShaderAssign.ShaderArchiveName, material.ShaderAssign.ShadingModelName); //Search for shader model - if (ShaderFile != null && ShaderFile.ShaderModels.ContainsKey(material.ShaderAssign.ShadingModelName)) - ShaderModel = ShaderFile.ShaderModels[material.ShaderAssign.ShadingModelName]; + if (ShaderFile != null && + ShaderFile.ShaderModels.TryGetValue(material.ShaderAssign.ShadingModelName, out ShaderModel? value)) + { + ShaderModel = value; + } if (ShaderModel == null) return; @@ -65,7 +68,7 @@ public virtual void Init(GL gl, BfresRender.BfresModel modelRender, BfresRender. if (SupportBlock == null) { SupportBlock = new UniformBlock(gl); - this.LoadSupportingBlock(); + LoadSupportingBlock(); } GLUtil.Label(gl, ObjectIdentifier.Buffer, MaterialBlock.ID, "Material Block"); @@ -94,7 +97,7 @@ public virtual void Init(GL gl, BfresRender.BfresModel modelRender, BfresRender. public virtual void ReloadMaterialBlock() { - SetMaterialBlock(this.MaterialBlock, this.Material); + SetMaterialBlock(MaterialBlock, Material); } @@ -129,36 +132,25 @@ public virtual void SetSkeletonBlock(UniformBlock block, BfresRender.BfresModel /// /// Finds the shader to use via material options and mesh skin count. /// - public virtual BfshaFile.ShaderProgram ReloadProgram(byte skinCount) - { - return null; - } + public virtual ShaderProgram ReloadProgram(byte skinCount) => null; /// /// Looks for a texture to use that is not present in the bfres. /// - public virtual GLTexture GetExternalTexture(GL gl, string sampler) - { - return null; - } + public virtual GLTexture GetExternalTexture(GL gl, string sampler) => null; /// /// Looks for the uniform block resource to bind. /// - public virtual UniformBlock GetUniformBlock(string name) - { - return null; - } + public virtual UniformBlock GetUniformBlock(string name) => null; /// /// Compiles the given bfsha program to "ShaderInfo" with glsl code. /// - public void CompileShader(GL gl, BfshaFile.ShaderProgram program) + public void CompileShader(GL gl, ShaderProgram program) { - var variation = ShaderModel.GetShaderVariation(program); - if (variation == null) - throw new Exception($"Failed to load shader variation!"); - + var variation = ShaderModel.GetShaderVariation(program) + ?? throw new Exception($"Failed to load shader variation!"); ShaderInfo = TegraShaderDecoder.LoadShaderProgram(gl, variation); if (ShaderInfo.VertexConstants != null) @@ -182,7 +174,7 @@ public virtual void Render(GL gl, BfresRender renderer, BfresRender.BfresModel m SkeletonBlock = model.SkeletonBuffer; - this.SetShapeBlock(ShapeBlock, transform); + SetShapeBlock(ShapeBlock, transform); SetSkeletonBlock(SkeletonBlock, model, transform); if (ShaderInfo == null) @@ -203,12 +195,10 @@ private void BindUniformBlocks(GL gl) int ubo_bind = 1; //Constants - if (this.ConstantVSBlock != null) - ConstantVSBlock.Render(this.ShaderInfo.Shader.ID, "_vp_c1", ubo_bind++); - if (this.ConstantFSBlock != null) - ConstantFSBlock.Render(this.ShaderInfo.Shader.ID, "_fp_c1", ubo_bind++); + ConstantVSBlock?.Render(ShaderInfo.Shader.ID, "_vp_c1", ubo_bind++); + ConstantFSBlock?.Render(ShaderInfo.Shader.ID, "_fp_c1", ubo_bind++); - SupportBlock.Render(this.ShaderInfo.Shader.ID, "_support_buffer", ubo_bind++); + SupportBlock.Render(ShaderInfo.Shader.ID, "_support_buffer", ubo_bind++); ubo_bind = 5; @@ -217,8 +207,7 @@ private void BindUniformBlocks(GL gl) { string name = ShaderModel.UniformBlocks.GetKey(i); //Get the shader location info - var locationInfo = this.ShaderProgram.UniformBlockIndices[i]; - int fragLocation = locationInfo.FragmentLocation; + var locationInfo = ShaderProgram.UniformBlockIndices[i]; int vertLocation = locationInfo.VertexLocation; //Block unused for this program so skip it @@ -233,16 +222,15 @@ private void BindUniformBlocks(GL gl) //Bind uniform data to the vertex and/or pixel location and prepare a binding ID //Prepare a unique binding per stage - BindUniformBlock(shaderBlock, this.ShaderInfo.Shader.ID, vertLocation, -1, ubo_bind++); + BindUniformBlock(shaderBlock, ShaderInfo.Shader.ID, vertLocation, -1, ubo_bind++); } for (int i = 0; i < ShaderModel.UniformBlocks.Count; i++) { string name = ShaderModel.UniformBlocks.GetKey(i); //Get the shader location info - var locationInfo = this.ShaderProgram.UniformBlockIndices[i]; + var locationInfo = ShaderProgram.UniformBlockIndices[i]; int fragLocation = locationInfo.FragmentLocation; - int vertLocation = locationInfo.VertexLocation; //Block unused for this program so skip it if (fragLocation == -1) @@ -256,16 +244,13 @@ private void BindUniformBlocks(GL gl) //Bind uniform data to the vertex and/or pixel location and prepare a binding ID //Prepare a unique binding per stage - BindUniformBlock(shaderBlock, this.ShaderInfo.Shader.ID, -1, fragLocation, ubo_bind++); + BindUniformBlock(shaderBlock, ShaderInfo.Shader.ID, -1, fragLocation, ubo_bind++); } } //Bind sampler data private void BindSamplers(GL gl, BfresRender renderer) { - int id = 1; - - BindBindlessTextures(gl, ShaderInfo.Shader); for (int i = 0; i < ShaderModel.Samplers.Count; i++) @@ -281,7 +266,7 @@ private void BindSamplers(GL gl, BfresRender renderer) for (int i = 0; i < ShaderModel.Samplers.Count; i++) { - var locationInfo = this.ShaderProgram.SamplerIndices[i]; + var locationInfo = ShaderProgram.SamplerIndices[i]; //Currently only using the vertex and fragment stages if (locationInfo.VertexLocation == -1 && locationInfo.FragmentLocation == -1) continue; @@ -292,14 +277,14 @@ private void BindSamplers(GL gl, BfresRender renderer) // Console.WriteLine($"sampler {sampler} loc {locationInfo.FragmentLocation} slot {i} uniform {ConvertSamplerID(locationInfo.FragmentLocation)}"); //Sampler assign has a key list of fragment shader samplers, value list of bfres material samplers - if (this.Material.ShaderAssign.SamplerAssign.ContainsKey(sampler)) + if (Material.ShaderAssign.SamplerAssign.ContainsKey(sampler)) { //Get the resource sampler //Important to note, fragment samplers are unique while material samplers can be the same //So we need to lookup which material sampler the current fragment sampler uses. - string resSampler = this.Material.ShaderAssign.SamplerAssign[sampler].String; + string resSampler = Material.ShaderAssign.SamplerAssign[sampler].String; //Find a texture using the sampler - textureIndex = this.Material.Samplers.Keys.ToList().FindIndex(x => x == resSampler); + textureIndex = Material.Samplers.Keys.ToList().FindIndex(x => x == resSampler); } var slot = i + 1; @@ -315,7 +300,7 @@ private void BindSamplers(GL gl, BfresRender renderer) } else //Bind texture inside the bfres material { - var texMap = this.Material.Textures[textureIndex]; + var texMap = Material.Textures[textureIndex]; tex = TryGetTexture(gl, renderer, texMap); } @@ -330,7 +315,7 @@ private void BindSamplers(GL gl, BfresRender renderer) if (textureIndex != -1) { - var samplerConfig = this.Material.Samplers[textureIndex]; + var samplerConfig = Material.Samplers[textureIndex]; SetupSampler(gl, tex, samplerConfig); } diff --git a/Fushigi/gl/Bfres/Shaders/WonderGameShader.cs b/Fushigi/gl/Bfres/Shaders/WonderGameShader.cs index ab2223ed..43d9a20b 100644 --- a/Fushigi/gl/Bfres/Shaders/WonderGameShader.cs +++ b/Fushigi/gl/Bfres/Shaders/WonderGameShader.cs @@ -1,16 +1,8 @@ using Fushigi.Bfres; using Silk.NET.OpenGL; -using System; -using System.Collections.Generic; -using System.Linq; using System.Numerics; -using System.Text; -using System.Threading.Tasks; using Fushigi.util; using Fushigi.env; -using System.Runtime.Intrinsics.Arm; -using static Fushigi.gl.Bfres.GsysEnvironment; -using Silk.NET.Core.Native; namespace Fushigi.gl.Bfres { @@ -83,7 +75,7 @@ public void Update(UniformBlock block) } } - class ShapeBlock + new class ShapeBlock { public Matrix4x4 ShapeMatrix = Matrix4x4.Identity; diff --git a/Fushigi/gl/Bfres/TileBfresRenderer.cs b/Fushigi/gl/Bfres/TileBfresRenderer.cs index 96ae9621..f9bca67c 100644 --- a/Fushigi/gl/Bfres/TileBfresRenderer.cs +++ b/Fushigi/gl/Bfres/TileBfresRenderer.cs @@ -5,17 +5,12 @@ using Silk.NET.OpenGL; using System.Diagnostics; using System.Numerics; -using System.Reflection; using static Fushigi.course.CourseUnit; namespace Fushigi.gl.Bfres { public class TileBfresRender { - private TileParamBlock TileParams; - - private BfresRender BfresRender; - private Matrix4x4 Transform = Matrix4x4.Identity; private SkinDivision mSkinDivision; public record struct MaterialNames(string? Edge, string? Wall, string? Ground); diff --git a/Fushigi/rstb/RSTB.cs b/Fushigi/rstb/RSTB.cs index d3317095..a8b28569 100644 --- a/Fushigi/rstb/RSTB.cs +++ b/Fushigi/rstb/RSTB.cs @@ -94,6 +94,7 @@ public void Save() } catch (IOException e) { + Logger.Logger.LogError(e); //Likely due to the course being open in the game, caught to prevent crash //TODO: notify the user } diff --git a/Fushigi/ui/EditContextBase.cs b/Fushigi/ui/EditContextBase.cs index 4586e1db..39894da5 100644 --- a/Fushigi/ui/EditContextBase.cs +++ b/Fushigi/ui/EditContextBase.cs @@ -11,7 +11,8 @@ internal class EditContextBase { private class BatchAction(EditContextBase context) : ICommittable { - public string Name { get; private set; } + public string? Name { get; private set; } + public void Commit(string name) { Name = name; diff --git a/Fushigi/ui/MainWindow.cs b/Fushigi/ui/MainWindow.cs index 79a37f7a..85a14b13 100644 --- a/Fushigi/ui/MainWindow.cs +++ b/Fushigi/ui/MainWindow.cs @@ -52,39 +52,36 @@ public MainWindow() iconConfig->GlyphOffset = new Vector2(0); float size = 16; + mDefaultFont = io.Fonts.AddFontFromFileTTF( + Path.Combine("res", "Font.ttf"), + size, nativeConfig, io.Fonts.GetGlyphRangesDefault()); - { - mDefaultFont = io.Fonts.AddFontFromFileTTF( - Path.Combine("res", "Font.ttf"), - size, nativeConfig, io.Fonts.GetGlyphRangesDefault()); - - io.Fonts.AddFontFromFileTTF( - Path.Combine("res", "NotoSansCJKjp-Medium.otf"), - size, nativeConfigJP, io.Fonts.GetGlyphRangesJapanese()); + io.Fonts.AddFontFromFileTTF( + Path.Combine("res", "NotoSansCJKjp-Medium.otf"), + size, nativeConfigJP, io.Fonts.GetGlyphRangesJapanese()); - //other fonts go here and follow the same schema - GCHandle rangeHandle = GCHandle.Alloc(new ushort[] { IconUtil.MIN_GLYPH_RANGE, IconUtil.MAX_GLYPH_RANGE, 0 }, GCHandleType.Pinned); - try - { - io.Fonts.AddFontFromFileTTF( - Path.Combine("res", "la-regular-400.ttf"), - size, iconConfig, rangeHandle.AddrOfPinnedObject()); + //other fonts go here and follow the same schema + GCHandle rangeHandle = GCHandle.Alloc(new ushort[] { IconUtil.MIN_GLYPH_RANGE, IconUtil.MAX_GLYPH_RANGE, 0 }, GCHandleType.Pinned); + try + { + io.Fonts.AddFontFromFileTTF( + Path.Combine("res", "la-regular-400.ttf"), + size, iconConfig, rangeHandle.AddrOfPinnedObject()); - io.Fonts.AddFontFromFileTTF( - Path.Combine("res", "la-solid-900.ttf"), - size, iconConfig, rangeHandle.AddrOfPinnedObject()); + io.Fonts.AddFontFromFileTTF( + Path.Combine("res", "la-solid-900.ttf"), + size, iconConfig, rangeHandle.AddrOfPinnedObject()); - io.Fonts.AddFontFromFileTTF( - Path.Combine("res", "la-brands-400.ttf"), - size, iconConfig, rangeHandle.AddrOfPinnedObject()); + io.Fonts.AddFontFromFileTTF( + Path.Combine("res", "la-brands-400.ttf"), + size, iconConfig, rangeHandle.AddrOfPinnedObject()); - io.Fonts.Build(); - } - finally - { - if (rangeHandle.IsAllocated) - rangeHandle.Free(); - } + io.Fonts.Build(); + } + finally + { + if (rangeHandle.IsAllocated) + rangeHandle.Free(); } } }); @@ -105,9 +102,7 @@ public async Task TryCloseCourse() return true; } else - { return false; - } } return true; @@ -132,7 +127,6 @@ public void Close() mSkipCloseTest = true; mWindow.Close(); } - }).ConfigureAwait(false); //fire and forget } @@ -175,7 +169,6 @@ await ProgressBarDialog.ShowDialogForAsyncAction(this, await LoadParamDBWithProgressBar(this); await Task.Delay(500); - } string? latestCourse = UserSettings.GetLatestCourse(); diff --git a/Fushigi/ui/Transform.cs b/Fushigi/ui/Transform.cs index f0807d3d..02ee7699 100644 --- a/Fushigi/ui/Transform.cs +++ b/Fushigi/ui/Transform.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Numerics; -using System.Text; -using System.Threading.Tasks; +using System.Numerics; namespace Fushigi.ui { diff --git a/Fushigi/util/DictionaryExtensions.cs b/Fushigi/util/DictionaryExtensions.cs index e580a5aa..0b257b00 100644 --- a/Fushigi/util/DictionaryExtensions.cs +++ b/Fushigi/util/DictionaryExtensions.cs @@ -1,17 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Fushigi.util +namespace Fushigi.util { internal static class DictionaryExtensions { public static TValue GetOrCreate(this Dictionary dict, TKey key) where TValue : new() { - if (dict.TryGetValue(key, out TValue value)) + if (dict.TryGetValue(key, out TValue? value)) return value; return dict[key] = new TValue();