Browse Source

Refactor code location; add MMData to read from DB

denikson 2 years ago
parent
commit
26848ff546

MultipleMaids/ComboBox2.cs → MultipleMaids/GUI/ComboBox2.cs


MultipleMaids/MouseDrag.cs → MultipleMaids/GUI/MouseDrag.cs


MultipleMaids/MouseDrag2.cs → MultipleMaids/GUI/MouseDrag2.cs


MultipleMaids/MouseDrag3.cs → MultipleMaids/GUI/MouseDrag3.cs


MultipleMaids/MouseDrag4.cs → MultipleMaids/GUI/MouseDrag4.cs


MultipleMaids/MouseDrag5.cs → MultipleMaids/GUI/MouseDrag5.cs


MultipleMaids/MouseDrag6.cs → MultipleMaids/GUI/MouseDrag6.cs


+ 23 - 17
MultipleMaids/MultipleMaids.csproj

@@ -38,6 +38,7 @@
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <RunCodeAnalysis>false</RunCodeAnalysis>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -48,6 +49,9 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
+  <PropertyGroup>
+    <RootNamespace>COM3D2.MultipleMaids</RootNamespace>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="Assembly-CSharp">
       <HintPath>lib\Assembly-CSharp.dll</HintPath>
@@ -71,25 +75,27 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="CM3D2\MultipleMaids\Plugin\MultipleMaids.Config.cs" />
-    <Compile Include="CM3D2\MultipleMaids\Plugin\MultipleMaids.Gui.cs" />
-    <Compile Include="CM3D2\MultipleMaids\Plugin\MultipleMaids.IK.cs" />
-    <Compile Include="CM3D2\MultipleMaids\Plugin\MultipleMaids.Init.cs" />
-    <Compile Include="CM3D2\MultipleMaids\Plugin\MultipleMaids.MaidUpdate.cs" />
-    <Compile Include="CM3D2\MultipleMaids\Plugin\MultipleMaids.Update.cs" />
-    <Compile Include="ComboBox2.cs" />
-    <Compile Include="MouseDrag.cs" />
-    <Compile Include="MouseDrag2.cs" />
-    <Compile Include="MouseDrag3.cs" />
-    <Compile Include="MouseDrag4.cs" />
-    <Compile Include="MouseDrag5.cs" />
-    <Compile Include="MouseDrag6.cs" />
-    <Compile Include="IK.cs" />
-    <Compile Include="ImportCM2.cs" />
-    <Compile Include="CM3D2\MultipleMaids\Plugin\MultipleMaids.cs" />
-    <Compile Include="AssemblyInfo.cs" />
+    <Compile Include="Plugin\MultipleMaids.Config.cs" />
+    <Compile Include="Plugin\MultipleMaids.Gui.cs" />
+    <Compile Include="Plugin\MultipleMaids.IK.cs" />
+    <Compile Include="Plugin\MultipleMaids.Init.cs" />
+    <Compile Include="Plugin\MultipleMaids.MaidUpdate.cs" />
+    <Compile Include="Plugin\MultipleMaids.Update.cs" />
+    <Compile Include="GUI\ComboBox2.cs" />
+    <Compile Include="GUI\MouseDrag.cs" />
+    <Compile Include="GUI\MouseDrag2.cs" />
+    <Compile Include="GUI\MouseDrag3.cs" />
+    <Compile Include="GUI\MouseDrag4.cs" />
+    <Compile Include="GUI\MouseDrag5.cs" />
+    <Compile Include="GUI\MouseDrag6.cs" />
+    <Compile Include="Util\IK.cs" />
+    <Compile Include="Util\ImportCM2.cs" />
+    <Compile Include="Plugin\MultipleMaids.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Util\Logger.cs" />
+    <Compile Include="Util\MMData.cs" />
     <Compile Include="Util\SimpleJSON.cs" />
+    <Compile Include="Util\Voice.cs" />
   </ItemGroup>
   <ItemGroup>
     <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">

MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Config.cs → MultipleMaids/Plugin/MultipleMaids.Config.cs


MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Gui.cs → MultipleMaids/Plugin/MultipleMaids.Gui.cs


MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.IK.cs → MultipleMaids/Plugin/MultipleMaids.IK.cs


MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Init.cs → MultipleMaids/Plugin/MultipleMaids.Init.cs


MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.MaidUpdate.cs → MultipleMaids/Plugin/MultipleMaids.MaidUpdate.cs


MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.Update.cs → MultipleMaids/Plugin/MultipleMaids.Update.cs


MultipleMaids/CM3D2/MultipleMaids/Plugin/MultipleMaids.cs → MultipleMaids/Plugin/MultipleMaids.cs


MultipleMaids/AssemblyInfo.cs → MultipleMaids/Properties/AssemblyInfo.cs


MultipleMaids/IK.cs → MultipleMaids/Util/IK.cs


MultipleMaids/ImportCM2.cs → MultipleMaids/Util/ImportCM2.cs


+ 164 - 0
MultipleMaids/Util/MMData.cs

@@ -0,0 +1,164 @@
+using SimpleJSON;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace COM3D2.MultipleMaids.Util
+{
+    internal static class MMData
+    {
+        private const string CM_EXTENSIONS_DB = "cm3d2_extensions.json";
+        private const string UI_NAMES_DB = "ui_names.json";
+        private const string BGM_DB = "bgm.json";
+        private const string VOICES_DB = "personal_voices.json";
+
+        public static Dictionary<string, string> Dances { get; private set; }
+        public static Dictionary<string, string> Dogus { get; private set; }
+        public static Dictionary<string, string> Particles { get; private set; }
+        public static Dictionary<string, string> Items { get; private set; }
+        public static Dictionary<string, string> Backgrounds { get; private set; }
+        public static Dictionary<string, IVoice[]> NormalVoices { get; private set; }
+        public static Dictionary<string, IVoice[]> HVoices { get; private set; }
+
+        private static readonly Random rand = new Random();
+
+        static MMData()
+        {
+            Dances = new Dictionary<string, string>();
+            Dogus = new Dictionary<string, string>();
+            Particles = new Dictionary<string, string>();
+            Items = new Dictionary<string, string>();
+            Backgrounds = new Dictionary<string, string>();
+            NormalVoices = new Dictionary<string, IVoice[]>();
+            HVoices = new Dictionary<string, IVoice[]>();
+        }
+
+        private static void ParseUINames(string location)
+        {
+            string uiNames = Path.Combine(location, UI_NAMES_DB);
+
+            if (File.Exists(uiNames))
+            {
+                JSONNode uiNamesJson = JSON.Parse(File.ReadAllText(uiNames));
+
+                Dogus = ParseObj(uiNamesJson, "dogu");
+                Particles = ParseObj(uiNamesJson, "particles");
+                Items = ParseObj(uiNamesJson, "items");
+                Backgrounds = ParseObj(uiNamesJson, "bg");
+            }
+        }
+
+        private static void ParseExtensions(string location)
+        {
+            string extensionsPath = Path.Combine(location, CM_EXTENSIONS_DB);
+
+            if (File.Exists(extensionsPath))
+            {
+                JSONNode extensionsJson = JSON.Parse(File.ReadAllText(extensionsPath));
+
+                foreach (string key in extensionsJson.Keys)
+                {
+                    if ((key != "CM3D2_Legacy" || !GameUty.IsEnabledCompatibilityMode) && GameMain.Instance.BgMgr.CreateAssetBundle(key) == null)
+                    {
+                        continue;
+                    }
+
+                    Dogus.Merge(ParseObj(extensionsJson, "dogu"));
+                    Items.Merge(ParseObj(extensionsJson, "item"));
+                    Backgrounds.Merge(ParseObj(extensionsJson, "bg"));
+                }
+            }
+        }
+
+        private static void ParseBGM(string location)
+        {
+            string bgmPath = Path.Combine(location, BGM_DB);
+
+            if (File.Exists(bgmPath))
+            {
+                JSONNode bgmJson = JSON.Parse(File.ReadAllText(bgmPath));
+                Backgrounds = bgmJson.Linq.ToDictionary(k => k.Key, k => k.Value.ToString());
+            }
+        }
+
+        private static void ParseVoices(string location)
+        {
+            void Parse(Dictionary<string, IVoice[]> target, JSONNode node, string type)
+            {
+                JSONNode normalVoices = node[type];
+                if (!normalVoices)
+                {
+                    return;
+                }
+
+                foreach (string key in normalVoices.Keys)
+                {
+                    JSONArray obj = normalVoices[key].AsArray;
+                    IVoice[] voices = new IVoice[obj.Count];
+
+                    for (int i = 0; i < voices.Length; i++)
+                    {
+                        JSONNode voiceObj = obj[i];
+                        if (voiceObj.IsNumber)
+                        {
+                            voices[i] = new SingleVoice(voiceObj.AsInt);
+                        }
+                        else if (voiceObj.IsArray && voiceObj.Count == 2)
+                        {
+                            voices[i] = new VoiceRange(voiceObj[0].AsInt, voiceObj[1].AsInt);
+                        }
+                    }
+
+                    target[key] = voices;
+                }
+            }
+
+            string personalVoicesPath = Path.Combine(location, VOICES_DB);
+
+            if (File.Exists(VOICES_DB))
+            {
+                JSONNode voicesJson = JSON.Parse(File.ReadAllText(personalVoicesPath));
+
+                Parse(NormalVoices, voicesJson, "normal");
+                Parse(HVoices, voicesJson, "h");
+            }
+        }
+
+        public static void LoadDatabase(string location)
+        {
+            ParseUINames(location);
+            ParseExtensions(location);
+            ParseVoices(location);
+            ParseBGM(location);
+        }
+
+        public static void Merge<TKey, TVal>(this Dictionary<TKey, TVal> self, Dictionary<TKey, TVal> with)
+        {
+            if (with.Count == 0)
+            {
+                return;
+            }
+
+            foreach (KeyValuePair<TKey, TVal> keyVal in with)
+            {
+                self[keyVal.Key] = keyVal.Value;
+            }
+        }
+
+        public static Dictionary<string, string> ParseObj(JSONNode obj, string key)
+        {
+            return !obj[key] ? new Dictionary<string, string>() : obj[key].Linq.ToDictionary(k => k.Key, k => k.Value.ToString());
+        }
+
+        public static int PickRandomVoice(Dictionary<string, IVoice[]> voiceStorage, string personality)
+        {
+            if (!voiceStorage.TryGetValue(personality, out IVoice[] voices))
+            {
+                return -1;
+            }
+
+            return voices[rand.Next(voices.Length)].PickRandomVoice();
+        }
+    }
+}

+ 40 - 0
MultipleMaids/Util/Voice.cs

@@ -0,0 +1,40 @@
+namespace COM3D2.MultipleMaids.Util
+{
+    internal interface IVoice
+    {
+        int PickRandomVoice();
+    }
+
+    internal struct SingleVoice : IVoice
+    {
+        private readonly int VoiceID;
+
+        public SingleVoice(int voice)
+        {
+            VoiceID = voice;
+        }
+
+        public int PickRandomVoice()
+        {
+            return VoiceID;
+        }
+    }
+
+    internal struct VoiceRange : IVoice
+    {
+        private static readonly Random rand = new Random();
+        private readonly int VoiceStart;
+        private readonly int VoiceEnd;
+
+        public VoiceRange(int start, int end)
+        {
+            VoiceStart = start;
+            VoiceEnd = end;
+        }
+
+        public int PickRandomVoice()
+        {
+            return rand.Next(VoiceStart, VoiceEnd + 1);
+        }
+    }
+}

+ 1 - 1
database/cm3d2_extensions.json

@@ -1,5 +1,5 @@
 {
-  "": {
+  "CM3D2_Legacy": {
     "bg": {
       "Salon": "Salon",
       "Syosai": "Study",