Browse Source

Use C# 8.0 features and clean up

habeebweeb 4 years ago
parent
commit
dd2b301a29

+ 2 - 1
COM3D2.MeidoPhotoStudio.Plugin/COM3D2.MeidoPhotoStudio.Plugin.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.Net.Sdk">
+<Project Sdk="Microsoft.Net.Sdk">
     <PropertyGroup>
         <TargetFramework>net35</TargetFramework>
         <AssemblyName>COM3D2.MeidoPhotoStudio</AssemblyName>
@@ -6,6 +6,7 @@
         <FileVersion>0.0.0</FileVersion>
         <GenerateAssemblyInfo>true</GenerateAssemblyInfo>
         <FrameworkPathOverride Condition="'$(TargetFramework)' == 'net35'">$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
+        <LangVersion>8</LangVersion>
     </PropertyGroup>
     <ItemGroup>
         <Reference Include="Assembly-CSharp">

+ 28 - 31
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Constants.cs

@@ -327,7 +327,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         public static void InitializePoses()
         {
-            void AddDefaultPose()
+            static void AddDefaultPose()
             {
                 if (!PoseDict.ContainsKey("normal")) PoseDict["normal"] = new List<string>() { "maid_stand01" };
                 if (!PoseGroupList.Contains("normal")) PoseGroupList.Insert(0, "normal");
@@ -411,7 +411,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         public static void InitializeCustomPoses()
         {
-            void GetPoses(string directory)
+            static void GetPoses(string directory)
             {
                 IEnumerable<string> poseList = Directory.GetFiles(directory)
                     .Where(file => Path.GetExtension(file) == ".anm");
@@ -441,7 +441,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         public static void InitializeHandPresets()
         {
-            void GetPresets(string directory)
+            static void GetPresets(string directory)
             {
                 IEnumerable<string> presetList = Directory.GetFiles(directory)
                     .Where(file => Path.GetExtension(file) == ".xml");
@@ -485,7 +485,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         public static void InitializeCustomFaceBlends()
         {
-            void GetFacePresets(string directory)
+            static void GetFacePresets(string directory)
             {
                 IEnumerable<string> presetList = Directory.GetFiles(directory)
                     .Where(file => Path.GetExtension(file) == ".xml").ToList();
@@ -531,15 +531,13 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             // CM3D2 BGs
             if (GameUty.IsEnabledCompatibilityMode)
             {
-                using (CsvParser csvParser = OpenCsvParser("phot_bg_list.nei", GameUty.FileSystemOld))
+                using CsvParser csvParser = OpenCsvParser("phot_bg_list.nei", GameUty.FileSystemOld);
+                for (int cell_y = 1; cell_y < csvParser.max_cell_y; cell_y++)
                 {
-                    for (int cell_y = 1; cell_y < csvParser.max_cell_y; cell_y++)
+                    if (csvParser.IsCellToExistData(3, cell_y))
                     {
-                        if (csvParser.IsCellToExistData(3, cell_y))
-                        {
-                            string bg = csvParser.GetCellAsString(3, cell_y);
-                            BGList.Add(bg);
-                        }
+                        string bg = csvParser.GetCellAsString(3, cell_y);
+                        BGList.Add(bg);
                     }
                 }
             }
@@ -667,29 +665,28 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
             void GetDeskItems(AFileSystemBase fs)
             {
-                using (CsvParser csvParser = OpenCsvParser("desk_item_detail.nei", fs))
+                using CsvParser csvParser = OpenCsvParser("desk_item_detail.nei", fs);
+
+                for (int cell_y = 1; cell_y < csvParser.max_cell_y; cell_y++)
                 {
-                    for (int cell_y = 1; cell_y < csvParser.max_cell_y; cell_y++)
+                    if (csvParser.IsCellToExistData(0, cell_y))
                     {
-                        if (csvParser.IsCellToExistData(0, cell_y))
+                        int cell = csvParser.GetCellAsInteger(0, cell_y);
+                        if (enabledIDs.Contains(cell))
                         {
-                            int cell = csvParser.GetCellAsInteger(0, cell_y);
-                            if (enabledIDs.Contains(cell))
+                            string dogu = string.Empty;
+                            if (csvParser.IsCellToExistData(3, cell_y))
+                            {
+                                dogu = csvParser.GetCellAsString(3, cell_y);
+                            }
+                            else if (csvParser.IsCellToExistData(4, cell_y))
+                            {
+                                dogu = csvParser.GetCellAsString(4, cell_y);
+                            }
+
+                            if (!string.IsNullOrEmpty(dogu))
                             {
-                                string dogu = string.Empty;
-                                if (csvParser.IsCellToExistData(3, cell_y))
-                                {
-                                    dogu = csvParser.GetCellAsString(3, cell_y);
-                                }
-                                else if (csvParser.IsCellToExistData(4, cell_y))
-                                {
-                                    dogu = csvParser.GetCellAsString(4, cell_y);
-                                }
-
-                                if (!string.IsNullOrEmpty(dogu))
-                                {
-                                    com3d2DeskDogu.Add(dogu);
-                                }
+                                com3d2DeskDogu.Add(dogu);
                             }
                         }
                     }
@@ -1042,7 +1039,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         }
     }
 
-    public struct MpnAttachProp
+    public readonly struct MpnAttachProp
     {
         public MPN Tag { get; }
         public string MenuFile { get; }

+ 9 - 21
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/DragPoint/CustomGizmo.cs

@@ -127,24 +127,15 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         private void SetTransform()
         {
-            switch (gizmoMode)
-            {
-                case GizmoMode.Local:
-                    transform.position = target.transform.position;
-                    transform.rotation = target.transform.rotation;
-                    break;
-                case GizmoMode.World:
-                    transform.position = target.transform.position;
-                    transform.rotation = Quaternion.identity;
-                    break;
-                case GizmoMode.Global:
-                    transform.position = target.transform.position;
-                    transform.rotation = Quaternion.LookRotation(
-                        transform.position - camera.transform.position, transform.up
-                    );
-                    break;
-            }
+            transform.position = target.transform.position;
             transform.localScale = Vector3.one;
+            transform.rotation = gizmoMode switch
+            {
+                GizmoMode.Local => target.transform.rotation,
+                GizmoMode.World => Quaternion.identity,
+                GizmoMode.Global => Quaternion.LookRotation(transform.position - camera.transform.position),
+                _ => target.transform.rotation
+            };
         }
 
         private void SetGizmoType(GizmoType gizmoType)
@@ -174,9 +165,6 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             }
         }
 
-        private void OnGizmoDrag()
-        {
-            GizmoDrag?.Invoke(this, EventArgs.Empty);
-        }
+        private void OnGizmoDrag() => GizmoDrag?.Invoke(this, EventArgs.Empty);
     }
 }

+ 2 - 2
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Controls/DropDown.cs

@@ -200,7 +200,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         {
             if (!initialized) InitializeStyle();
 
-            style = style ?? DefaultDropdownStyle;
+            style ??= DefaultDropdownStyle;
 
             return style.CalcSize(new GUIContent(item));
         }
@@ -209,7 +209,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         {
             if (!initialized) InitializeStyle();
 
-            style = style ?? DefaultDropdownStyle;
+            style ??= DefaultDropdownStyle;
 
             GUIContent content = new GUIContent(list[0]);
             Vector2 calculatedSize = style.CalcSize(content);

+ 1 - 1
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Controls/Slider.cs

@@ -89,7 +89,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         }
     }
 
-    public struct SliderProp
+    public readonly struct SliderProp
     {
         public float Left { get; }
         public float Right { get; }

+ 0 - 1
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Panes/BackgroundWindowPanes/LightsPane.cs

@@ -98,7 +98,6 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
             lightHeader = Translation.Get("lightsPane", "header");
             resetLabel = Translation.Get("lightsPane", "resetLabel");
-
         }
 
         protected override void ReloadTranslation()

+ 3 - 2
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Managers/EnvironmentManager.cs

@@ -98,7 +98,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             string bgAsset = binaryReader.ReadString();
             bool isCreative = Utility.IsGuidString(bgAsset);
             System.Collections.Generic.List<string> bgList = isCreative
-                ? Constants.MyRoomCustomBGList.Select(kvp => kvp.Key).ToList()
+                ? Constants.MyRoomCustomBGList.ConvertAll(kvp => kvp.Key)
                 : Constants.BGList;
 
             int assetIndex = bgList.FindIndex(
@@ -315,12 +315,13 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         }
     }
 
-    public struct CameraInfo
+    public readonly struct CameraInfo
     {
         public Vector3 TargetPos { get; }
         public Vector3 Pos { get; }
         public Vector3 Angle { get; }
         public float Distance { get; }
+
         public CameraInfo(CameraMain camera)
         {
             TargetPos = camera.GetTargetPos();

+ 23 - 26
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Managers/SceneManager.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 using BepInEx.Configuration;
+using System.Text;
 
 namespace COM3D2.MeidoPhotoStudio.Plugin
 {
@@ -182,13 +183,12 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         public void SortScenes(SortMode sortMode)
         {
             CurrentSortMode = sortMode;
-            Comparison<Scene> comparator;
-            switch (CurrentSortMode)
+            Comparison<Scene> comparator = CurrentSortMode switch
             {
-                case SortMode.DateModified: comparator = SortByDateModified; break;
-                case SortMode.DateCreated: comparator = SortByDateCreated; break;
-                default: comparator = SortByName; break;
-            }
+                SortMode.DateModified => SortByDateModified,
+                SortMode.DateCreated => SortByDateCreated,
+                _ => SortByName,
+            };
             SceneList.Sort(comparator);
         }
 
@@ -332,32 +332,29 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
                 if (sceneData == null) return;
 
-                using (MemoryStream memoryStream = new MemoryStream(sceneData))
-                using (BinaryReader binaryReader = new BinaryReader(memoryStream, System.Text.Encoding.UTF8))
+                using BinaryReader binaryReader = new BinaryReader(new MemoryStream(sceneData), Encoding.UTF8);
+
+                try
                 {
-                    try
+                    if (binaryReader.ReadString() != "MPS_SCENE")
                     {
-                        if (binaryReader.ReadString() != "MPS_SCENE")
-                        {
-                            Utility.LogWarning($"'{filePath}' is not a {MeidoPhotoStudio.pluginName} scene");
-                            return;
-                        }
-
-                        if (binaryReader.ReadInt32() > MeidoPhotoStudio.sceneVersion)
-                        {
-                            Utility.LogWarning(
-                                $"'{filePath}' is made in a newer version of {MeidoPhotoStudio.pluginName}"
-                            );
-                            return;
-                        }
-
-                        NumberOfMaids = binaryReader.ReadInt32();
+                        Utility.LogWarning($"'{filePath}' is not a {MeidoPhotoStudio.pluginName} scene");
+                        return;
                     }
-                    catch (Exception e)
+
+                    if (binaryReader.ReadInt32() > MeidoPhotoStudio.sceneVersion)
                     {
-                        Utility.LogWarning($"Failed to deserialize scene '{filePath}' because {e.Message}");
+                        Utility.LogWarning(
+                            $"'{filePath}' is made in a newer version of {MeidoPhotoStudio.pluginName}"
+                        );
                         return;
                     }
+
+                    NumberOfMaids = binaryReader.ReadInt32();
+                }
+                catch (Exception e)
+                {
+                    Utility.LogWarning($"Failed to deserialize scene '{filePath}' because {e.Message}");
                 }
             }
 

+ 2 - 2
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Meido/IK/DragPointSpine.cs

@@ -31,7 +31,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             {
                 if (!isHead && current == DragType.RotLocalXZ) ApplyProperties(false, false, isThigh);
                 else if (!isThigh && (current == DragType.MoveY)) ApplyProperties(isHip, isHip, !isHip);
-                else if (!(isThigh || isHead) && (current == DragType.RotLocalY)) ApplyProperties(!isHip, !isHip, isHip);
+                else if (!isThigh && !isHead && (current == DragType.RotLocalY)) ApplyProperties(!isHip, !isHip, isHip);
                 else ApplyProperties(!isThigh, !isThigh, false);
             }
             else ApplyProperties(false, false, false);
@@ -42,7 +42,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             bool shift = Input.Shift;
             bool alt = Input.Alt;
 
-            if (isThigh && alt && shift)
+            if (isThigh && !Input.Control && alt && shift)
             {
                 // gizmo thigh rotation
                 CurrentDragType = DragType.RotLocalXZ;

+ 5 - 1
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Meido/Meido.cs

@@ -963,11 +963,15 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         }
     }
 
-    public struct PoseInfo
+    public readonly struct PoseInfo
     {
         public string PoseGroup { get; }
         public string Pose { get; }
         public bool CustomPose { get; }
+        private static readonly PoseInfo defaultPose =
+            new PoseInfo(Constants.PoseGroupList[0], Constants.PoseDict[Constants.PoseGroupList[0]][0]);
+        public static ref readonly PoseInfo DefaultPose => ref defaultPose;
+
         public PoseInfo(string poseGroup, string pose, bool customPose = false)
         {
             PoseGroup = poseGroup;

+ 15 - 15
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Meido/MeidoDragPointManager.cs

@@ -293,21 +293,20 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
             int mirror = mirroring ? -1 : 1;
 
-            using (MemoryStream memoryStream = new MemoryStream(fingerBinary))
-            using (BinaryReader binaryReader = new BinaryReader(memoryStream))
+            using MemoryStream memoryStream = new MemoryStream(fingerBinary);
+            using BinaryReader binaryReader = new BinaryReader(memoryStream);
+
+            for (Bone bone = start; bone <= end; bone += joints)
             {
-                for (Bone bone = start; bone <= end; bone += joints)
+                for (int i = 0; i < joints - 1; i++)
                 {
-                    for (int i = 0; i < joints - 1; i++)
-                    {
-                        BoneTransform[bone + i].localRotation = new Quaternion
-                        (
-                            binaryReader.ReadSingle() * mirror,
-                            binaryReader.ReadSingle() * mirror,
-                            binaryReader.ReadSingle(),
-                            binaryReader.ReadSingle()
-                        );
-                    }
+                    BoneTransform[bone + i].localRotation = new Quaternion
+                    (
+                        binaryReader.ReadSingle() * mirror,
+                        binaryReader.ReadSingle() * mirror,
+                        binaryReader.ReadSingle(),
+                        binaryReader.ReadSingle()
+                    );
                 }
             }
         }
@@ -670,12 +669,13 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         }
     }
 
-    public struct AttachPointInfo
+    public readonly struct AttachPointInfo
     {
         public AttachPoint AttachPoint { get; }
         public string MaidGuid { get; }
         public int MaidIndex { get; }
-        public static AttachPointInfo Empty => new AttachPointInfo(AttachPoint.None, string.Empty, -1);
+        private static readonly AttachPointInfo empty = new AttachPointInfo(AttachPoint.None, string.Empty, -1);
+        public static ref readonly AttachPointInfo Empty => ref empty;
 
         public AttachPointInfo(AttachPoint attachPoint, string maidGuid, int maidIndex)
         {

+ 12 - 16
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/MenuFileCache.cs

@@ -38,29 +38,25 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         private void Deserialize()
         {
-            using (BinaryReader binaryReader = new BinaryReader(File.OpenRead(cachePath)))
+            using BinaryReader binaryReader = new BinaryReader(File.OpenRead(cachePath));
+            if (binaryReader.ReadInt32() != cacheVersion)
             {
-                if (binaryReader.ReadInt32() != cacheVersion)
-                {
-                    Utility.LogInfo("Cache version out of date. Rebuilding");
-                    return;
-                }
-                while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
-                {
-                    ModItem item = ModItem.Deserialize(binaryReader);
-                    modItems[item.MenuFile] = item;
-                }
+                Utility.LogInfo("Cache version out of date. Rebuilding");
+                return;
+            }
+            while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length)
+            {
+                ModItem item = ModItem.Deserialize(binaryReader);
+                modItems[item.MenuFile] = item;
             }
         }
 
         public void Serialize()
         {
             if (!rebuild) return;
-            using (BinaryWriter binaryWriter = new BinaryWriter(File.OpenWrite(cachePath)))
-            {
-                binaryWriter.Write(cacheVersion);
-                foreach (ModItem item in modItems.Values) item.Serialize(binaryWriter);
-            }
+            using BinaryWriter binaryWriter = new BinaryWriter(File.OpenWrite(cachePath));
+            binaryWriter.Write(cacheVersion);
+            foreach (ModItem item in modItems.Values) item.Serialize(binaryWriter);
         }
     }
 }

+ 1 - 1
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/MenuFileUtility.cs

@@ -802,7 +802,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             public string ModelFile { get; set; }
         }
 
-        private struct MaterialChange
+        private readonly struct MaterialChange
         {
             public int MaterialIndex { get; }
             public string MaterialFile { get; }