Advertisement
Guest User

CM3D2.YASD.Plugin

a guest
Sep 4th, 2015
13,935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 27.15 KB | None | 0 0
  1. /*
  2.  * CM3D2.YASD.Plugin (Yet Another Soft Demosaic Plugin)
  3.  *
  4.  * モザイクのON/OFFを切り替えたりボディのテクスチャを変更できるUnityInjectorのプラグインです。
  5.  *
  6.  * モザイクはF4でメイド|メイドとおもちゃ|全部|消さないを切り替えます。
  7.  * iniファイルを書き換えるとキーや切り替えの有効無効、モザイクを消す対象を変更できます。
  8.  * 切り替えを無効にしてもiniで指定されたモザイクは消えます。
  9.  * 切り替え機能がいらなければOFFにすると無駄な処理が減る分ちょっとだけ軽くなるかも。
  10.  *
  11.  * テクスチャ変更はメイドのボディ限定でTranslation Loaderと同じような処理をします。
  12.  * Translation Loaderと違いReiPatcherでパッチを当てる必要がないのが強みです。
  13.  * メイドのボディ以外のテクスチャも変更したい場合は素直にTranslation Loaderを使ってください。
  14.  * プラグインを導入して一度ゲームを起動するとUnityInjector\Configに
  15.  * YASD.iniファイルとYASDフォルダが作成されるのでYASDフォルダの中にテクスチャファイルを
  16.  * コピーしてください。
  17.  * テクスチャはpng形式のみに対応しています。
  18.  * メイドが表示されているときにF3を押すとメイドに反映されます。
  19.  * F3を押さずに自動で変更することもできますがデフォルトで無効になっています。
  20.  * 自動で変更する場合はiniファイルの[ChangeTexture]セクションの[AutoChange]を
  21.  * Trueに書き換えてください。
  22.  *
  23.  * 自動変更モードの仕様
  24.  * テクスチャはゲーム起動時にリストが作成されてその中の物のみが反映されます。
  25.  * テクスチャ変更時に何らかの理由で読み込みに失敗したファイルはリストから消去されます。
  26.  * また、一度変更したテクスチャはファイルを入れ替えても即時反映されません。
  27.  * ゲーム内のエディットで肌を変更後元の肌戻すと最新のファイルを読み込みます。
  28.  * また、自動変更中にF3を押すとリストを更新し最新のファイルを強制的に適用します。
  29.  * 要はゲーム中にテクスチャを入れ替えたらF3を押してください。
  30.  *
  31.  *
  32.  * SofrDemosaic.iniの内容
  33.  * [DeMosaic]セクション
  34.  * ChangeMode={True, False}
  35.  *      キーボードでの切り替えの有効無効を設定します。
  36.  *      True=有効 False=無効
  37.  *
  38.  * Target={None, Maid, MaidAndItem, All}
  39.  *     モザイクを消す対象を指定。
  40.  *     キーボード切り替えが有効な場合は押す度に切り替わります。
  41.  *     無効の場合はiniで指定した値で固定されます。
  42.  *          None=消さない
  43.  *          Maid=女性のみ
  44.  *          MaidAndItem=女性とおもちゃ
  45.  *          All=全て(女性、おもちゃ、男性)
  46.  *
  47.  * HotKey
  48.  *      モザイクモード切替キーの設定。デフォルトはF4。
  49.  *      指定するコードは下記URL参照。
  50.  *      http://docs.unity3d.com/jp/current/ScriptReference/KeyCode.html
  51.  *
  52.  *
  53.  * [ChangeTexture]セクション
  54.  * AutoChange={True, False}
  55.  *      テクスチャ自動変更機能の有効無効を設定します。
  56.  *      True=有効 False=無効
  57.  *
  58.  * HotKey
  59.  *      テクスチャリスト再読み込みキーの設定。デフォルトはF3
  60.  *      自動更新が無効の場合はテクスチャリスト読み込みとテクスチャの変更を行います。
  61.  *      指定するコードはDeMosaicセクションのURL参照。
  62.  *
  63.  *
  64.  * コンパイル時の参照設定
  65.  * /r:UnityEngine.dll /r:UnityInjector.dll /r:Assembly-CSharp.dll /r:Assembly-CSharp-firstpass.dll /r:ExINI.dll
  66.  *
  67. */
  68.  
  69. using ExIni;
  70. using System;
  71. using System.Collections;
  72. using System.Collections.Generic;
  73. using System.Diagnostics;
  74. using System.IO;
  75. using System.Linq;
  76. using System.Text.RegularExpressions;
  77. using UnityEngine;
  78. using UnityInjector;
  79. using UnityInjector.Attributes;
  80.  
  81. namespace CM3D2.YetAnotherSoftDemosaic.Plugin
  82. {
  83.     [PluginFilter("CM3D2x64"),
  84.     PluginFilter("CM3D2x86"),
  85.     PluginFilter("CM3D2VRx64"),
  86.     PluginName("Yet Another Soft DeMosaic"),
  87.     PluginVersion("2.0.0.0")]
  88.     public class YASD : PluginBase
  89.     {
  90.         private Coroutine deMosaicCoroutine;
  91.         private Coroutine[] changeTextureCoroutine = new Coroutine[2];
  92.         private bool runningCoroutine = false;
  93.  
  94.         private bool allowChangeMosaic = true;
  95.         private KeyCode deMosaicHotKey = KeyCode.F4;
  96.  
  97.         private bool allowAutoChangeTexture = false;
  98.         private KeyCode loadTextureKey = KeyCode.F3;
  99.  
  100.         //1fで1秒
  101.         private const float WAIT_SECOND_DEMOSAIC = 0.5f;
  102.  
  103.         private const float WAIT_SECOND_CHANGE_TEXTURE = 1f;
  104.  
  105.         /// <summary>
  106.         /// CM3D2のシーンリスト
  107.         /// </summary>
  108.         private enum Scene
  109.         {
  110.             /// <summary>メイド選択(夜伽、品評会の前など)</summary>
  111.             SceneCharacterSelect = 1,
  112.  
  113.             /// <summary>品評会</summary>
  114.             SceneCompetitiveShow = 2,
  115.  
  116.             /// <summary>昼夜メニュー、仕事結果</summary>
  117.             SceneDaily = 3,
  118.  
  119.             /// <summary>ダンス1</summary>
  120.             SceneDance_DDFL = 4,
  121.  
  122.             /// <summary>メイドエディット</summary>
  123.             SceneEdit = 5,
  124.  
  125.             /// <summary>メーカーロゴ</summary>
  126.             SceneLogo = 6,
  127.  
  128.             /// <summary>メイド管理</summary>
  129.             SceneMaidManagement = 7,
  130.  
  131.             /// <summary>ショップ</summary>
  132.             SceneShop = 8,
  133.  
  134.             /// <summary>タイトル画面</summary>
  135.             SceneTitle = 9,
  136.  
  137.             /// <summary>トロフィー閲覧</summary>
  138.             SceneTrophy = 10,
  139.  
  140.             /// <summary>???</summary>
  141.             SceneTryInfo = 11,
  142.  
  143.             /// <summary>主人公エディット</summary>
  144.             SceneUserEdit = 12,
  145.  
  146.             /// <summary>起動時警告画面</summary>
  147.             SceneWarning = 13,
  148.  
  149.             /// <summary>夜伽</summary>
  150.             SceneYotogi = 14,
  151.  
  152.             /// <summary>ADVパート(kgスクリプト処理)</summary>
  153.             SceneADV = 15,
  154.  
  155.             /// <summary>日付画面</summary>
  156.             SceneStartDaily = 16,
  157.  
  158.             /// <summary>タイトルに戻る</summary>
  159.             SceneToTitle = 17,
  160.  
  161.             /// <summary>MVP</summary>
  162.             SceneSingleEffect = 18,
  163.  
  164.             /// <summary>スタッフロール</summary>
  165.             SceneStaffRoll = 19,
  166.  
  167.             /// <summary>ダンス2</summary>
  168.             SceneDance_ETYL = 20
  169.         }
  170.  
  171.         /// <summary>
  172.         /// 3Dキャラが描画されるレベル
  173.         /// </summary>
  174.         private enum EnableLevel
  175.         {
  176.             /// <summary>品評会</summary>
  177.             SceneCompetitiveShow = 2,
  178.  
  179.             /// <summary>ダンス1</summary>
  180.             SceneDance_DDFL = 4,
  181.  
  182.             /// <summary>メイドエディット</summary>
  183.             SceneEdit = 5,
  184.  
  185.             /// <summary>ショップ</summary>
  186.             SceneShop = 8,
  187.  
  188.             /// <summary>夜伽</summary>
  189.             SceneYotogi = 14,
  190.  
  191.             /// <summary>ADVパート(kgスクリプト処理)</summary>
  192.             SceneADV = 15,
  193.  
  194.             /// <summary>MVP</summary>
  195.             SceneSingleEffect = 18,
  196.  
  197.             /// <summary>ダンス2</summary>
  198.             SceneDance_ETYL = 20
  199.         }
  200.  
  201.         /// <summary>
  202.         /// 男キャラやおもちゃが出現するレベル
  203.         /// </summary>
  204.         private enum ShowMenOrToysLevel
  205.         {
  206.             /// <summary>夜伽</summary>
  207.             SceneYotogi = 14,
  208.  
  209.             /// <summary>ADVパート(kgスクリプト処理)</summary>
  210.             SceneADV = 15
  211.         }
  212.  
  213.         /// <summary>
  214.         /// メイドが出現するがキャラ変更や着替えを行わないレベル
  215.         /// </summary>
  216.         private enum NotChangeMaidsLevel
  217.         {
  218.             /// <summary>品評会</summary>
  219.             SceneCompetitiveShow = 2,
  220.  
  221.             /// <summary>ダンス1</summary>
  222.             SceneDance_DDFL = 4,
  223.  
  224.             /// <summary>MVP</summary>
  225.             SceneSingleEffect = 18,
  226.  
  227.             /// <summary>ダンス2</summary>
  228.             SceneDance_ETYL = 20
  229.         }
  230.  
  231.         private ChangeBodyTexture changeBodyTexture = new ChangeBodyTexture();
  232.         private DeMosaic deMosaic = new DeMosaic();
  233.  
  234.         private void LoadConfig()
  235.         {
  236.             IniKey changeMosaic = Preferences["DeMosaic"]["ChangeMode"];
  237.             IniKey target = Preferences["DeMosaic"]["Target"];
  238.             IniKey changeMosaicKey = Preferences["DeMosaic"]["HotKey"];
  239.             IniKey changeTex = Preferences["ChangeTexture"]["AutoChange"];
  240.             IniKey changeTexKey = Preferences["ChangeTexture"]["HotKey"];
  241.  
  242.             if (changeMosaic == null || string.IsNullOrEmpty(changeMosaic.Value))
  243.             {
  244.                 changeMosaic.Value = allowChangeMosaic.ToString();
  245.                 SaveConfig();
  246.             }
  247.             else
  248.             {
  249.                 if (!bool.TryParse(changeMosaic.Value, out allowChangeMosaic))
  250.                 {
  251.                     changeMosaic.Value = allowChangeMosaic.ToString();
  252.                     SaveConfig();
  253.                 }
  254.             }
  255.  
  256.             if (target == null || string.IsNullOrEmpty(target.Value))
  257.             {
  258.                 target.Value = deMosaic.Target.ToString();
  259.                 SaveConfig();
  260.             }
  261.             else
  262.             {
  263.                 if (Enum.IsDefined(typeof(DeMosaic.Switch), target.Value))
  264.                 {
  265.                     deMosaic.Target = (DeMosaic.Switch)Enum.Parse(typeof(DeMosaic.Switch), target.Value, true);
  266.                 }
  267.                 else
  268.                 {
  269.                     target.Value = deMosaic.Target.ToString();
  270.                     SaveConfig();
  271.                 }
  272.             }
  273.  
  274.             if (changeMosaicKey == null || string.IsNullOrEmpty(changeMosaicKey.Value))
  275.             {
  276.                 changeMosaicKey.Value = deMosaicHotKey.ToString();
  277.                 SaveConfig();
  278.             }
  279.             else
  280.             {
  281.                 if (Enum.IsDefined(typeof(KeyCode), changeMosaicKey.Value))
  282.                 {
  283.                     deMosaicHotKey = (KeyCode)Enum.Parse(typeof(KeyCode), changeMosaicKey.Value, true);
  284.                 }
  285.                 else
  286.                 {
  287.                     changeMosaicKey.Value = deMosaicHotKey.ToString();
  288.                     SaveConfig();
  289.                 }
  290.             }
  291.  
  292.             if (changeTex == null || string.IsNullOrEmpty(changeTex.Value))
  293.             {
  294.                 changeTex.Value = allowAutoChangeTexture.ToString();
  295.                 SaveConfig();
  296.             }
  297.             else
  298.             {
  299.                 if (!bool.TryParse(changeTex.Value, out allowAutoChangeTexture))
  300.                 {
  301.                     changeTex.Value = allowAutoChangeTexture.ToString();
  302.                     SaveConfig();
  303.                 }
  304.             }
  305.  
  306.             if (changeTexKey == null || string.IsNullOrEmpty(changeTexKey.Value))
  307.             {
  308.                 changeTexKey.Value = loadTextureKey.ToString();
  309.                 SaveConfig();
  310.             }
  311.             else
  312.             {
  313.                 if (Enum.IsDefined(typeof(KeyCode), changeTexKey.Value))
  314.                 {
  315.                     loadTextureKey = (KeyCode)Enum.Parse(typeof(KeyCode), changeTexKey.Value, true);
  316.                 }
  317.                 else
  318.                 {
  319.                     changeTexKey.Value = loadTextureKey.ToString();
  320.                     SaveConfig();
  321.                 }
  322.             }
  323.         }
  324.  
  325.         private void Awake()
  326.         {
  327.             GameObject.DontDestroyOnLoad(this);
  328.             System.Diagnostics.Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
  329.  
  330.             LoadConfig();
  331.  
  332.             StartCoroutine(GetKeyInput());
  333.         }
  334.  
  335.         private void OnLevelWasLoaded(int level)
  336.         {
  337.             if (runningCoroutine)
  338.             {
  339.                 System.Diagnostics.Debug.WriteLine("Demosaic Coroutine stop.");
  340.                 StopCoroutine(deMosaicCoroutine);
  341.                 StopCoroutine(changeTextureCoroutine[0]);
  342.                 if (allowAutoChangeTexture)
  343.                 {
  344.                     StopCoroutine(changeTextureCoroutine[1]);
  345.                 }
  346.                 runningCoroutine = false;
  347.             }
  348.             if (Enum.IsDefined(typeof(EnableLevel), level))
  349.             {
  350.                 System.Diagnostics.Debug.WriteLine("Demosaic Coroutine start.");
  351.                 deMosaicCoroutine = StartCoroutine(DeMosaicCoroutine(level));
  352.                 changeTextureCoroutine[0] = StartCoroutine(GetKeyInputForTextureChanger());
  353.                 if (allowAutoChangeTexture)
  354.                 {
  355.                     changeTextureCoroutine[1] = StartCoroutine(AutoTextureChangerCoroutine());
  356.                 }
  357.                 runningCoroutine = true;
  358.             }
  359.         }
  360.  
  361.         private IEnumerator AutoTextureChangerCoroutine()
  362.         {
  363.             while (true)
  364.             {
  365.                 yield return new WaitForSeconds(WAIT_SECOND_CHANGE_TEXTURE);
  366.                 changeBodyTexture.ChangeTexture(false);
  367.             }
  368.         }
  369.  
  370.         private IEnumerator GetKeyInputForTextureChanger()
  371.         {
  372.             while (true)
  373.             {
  374.                 if (Input.GetKeyDown(loadTextureKey))
  375.                 {
  376.                     ChangeBodyTexture.LoadTextureList(ChangeBodyTexture.textureList);
  377.                     changeBodyTexture.ChangeTexture(true);
  378.                 }
  379.                 yield return null;
  380.             }
  381.         }
  382.  
  383.         private IEnumerator GetKeyInput()
  384.         {
  385.             while (true)
  386.             {
  387.                 if (Input.GetKeyDown(deMosaicHotKey) && allowChangeMosaic)
  388.                 {
  389.                     deMosaic.Target++;
  390.                     Console.WriteLine("YADSM Target:{0}", deMosaic.Target.ToString());
  391.                 }
  392.                 yield return null;
  393.             }
  394.         }
  395.  
  396.         private IEnumerator DeMosaicCoroutine(int level)
  397.         {
  398.             while (true)
  399.             {
  400.                 bool result = deMosaic.DeMosaicMaids();
  401.  
  402.                 if (result && !allowChangeMosaic && Enum.IsDefined(typeof(NotChangeMaidsLevel), level))
  403.                 {
  404.                     System.Diagnostics.Debug.WriteLine("DeMosaic Coroutine Break.");
  405.                     runningCoroutine = false;
  406.                     yield break;
  407.                 }
  408.                 yield return null;
  409.  
  410.                 if (Enum.IsDefined(typeof(ShowMenOrToysLevel), level))
  411.                 {
  412.                     deMosaic.DeMosaicToys();
  413.                     yield return null;
  414.  
  415.                     deMosaic.DeMosaicMen();
  416.                 }
  417.                 //System.Diagnostics.Debug.WriteLine("DeMosaic Coroutine Continue.");
  418.                 yield return new WaitForSeconds(WAIT_SECOND_DEMOSAIC);
  419.             }
  420.         }
  421.  
  422.         public void OnApplicationQuit()
  423.         {
  424.             Preferences["DeMosaic"]["Target"].Value = deMosaic.Target.ToString();
  425.             SaveConfig();
  426.         }
  427.     }
  428.  
  429.     public class DeMosaic
  430.     {
  431.         public enum Switch
  432.         {
  433.             None,
  434.             Maid,
  435.             MaidAndItem,
  436.             All,
  437.             Max
  438.         }
  439.  
  440.         private Switch _target = Switch.All;
  441.  
  442.         public Switch Target
  443.         {
  444.             get { return _target; }
  445.             set { _target = value == Switch.Max ? Switch.None : value; }
  446.         }
  447.  
  448.         private Shader shaderTransparent = Shader.Find("CM3D2/Toony_Lighted_Trans");
  449.         private Shader shaderMosaic = Shader.Find("CM3D2/Mosaic");
  450.  
  451.         private string getHiraerchy(Transform t)
  452.         {
  453.             string hiraerchy = "/" + t.name;
  454.             while (t.parent)
  455.             {
  456.                 t = t.parent;
  457.                 hiraerchy = "/" + t.name + hiraerchy;
  458.             }
  459.  
  460.             return hiraerchy;
  461.         }
  462.  
  463.         private Renderer[] GetMosaicRenderer(string type)
  464.         {
  465.             GameObject alloffset = GameObject.Find("__GameMain__/Character/Active/AllOffset");
  466.             if (!alloffset)
  467.             {
  468.                 return null;
  469.             }
  470.             Renderer[] mosaic = alloffset.GetComponentsInChildren<Renderer>(true)
  471.                 .Where(_ => _.materials.Any(m => m.name.Contains("moza")))
  472.                 .ToArray();
  473.  
  474.             string s;
  475.  
  476.             if (type.Equals("toy"))
  477.             {
  478.                 return mosaic
  479.                     .Where(_ => _.materials.Any(m => m.name != "moza"))
  480.                     .ToArray();
  481.             }
  482.             else if (type.Equals("man"))
  483.             {
  484.                 s = "Man[";
  485.             }
  486.             else if (type.Equals("maid"))
  487.             {
  488.                 s = "Maid[";
  489.             }
  490.             else
  491.             {
  492.                 Console.WriteLine("YASD.GetMosaicRenderer: Argument error.");
  493.                 return null;
  494.             }
  495.  
  496.             return mosaic
  497.                 .Where(_ => _.name.Equals("moza"))
  498.                 .Where(_ => getHiraerchy(_.transform).Contains(s))
  499.                 .ToArray();
  500.         }
  501.  
  502.         private Renderer[] GetMaidsMosaicRenderer()
  503.         {
  504.             return GetMosaicRenderer("maid");
  505.         }
  506.  
  507.         private Renderer[] GetMenMosaicRenderer()
  508.         {
  509.             return GetMosaicRenderer("man");
  510.         }
  511.  
  512.         private Renderer[] GetToysMosaicRenderer()
  513.         {
  514.             return GetMosaicRenderer("toy");
  515.         }
  516.  
  517.         public bool DeMosaicMaids()
  518.         {
  519.             bool result = false;
  520.             Renderer[] maidsMosaicList = GetMaidsMosaicRenderer();
  521.  
  522.             if (maidsMosaicList != null)
  523.             {
  524.                 result = HumanMosaicSwitcher(maidsMosaicList, (Target == DeMosaic.Switch.None));
  525.             }
  526.             return result;
  527.         }
  528.  
  529.         public void DeMosaicMen()
  530.         {
  531.             Renderer[] menMosaicList = GetMenMosaicRenderer();
  532.  
  533.             if (menMosaicList != null)
  534.             {
  535.                 HumanMosaicSwitcher(menMosaicList, (Target != DeMosaic.Switch.All));
  536.             }
  537.         }
  538.  
  539.         private bool HumanMosaicSwitcher(Renderer[] mosaic, bool enableMosaic)
  540.         {
  541.             bool result = false;
  542.             if (mosaic == null)
  543.             {
  544.                 return result;
  545.             }
  546.             mosaic.Where(_ => _.enabled == !enableMosaic).ForEach(_ =>
  547.             {
  548.                 _.enabled = enableMosaic;
  549.                 System.Diagnostics.Debug.WriteLine(string.Format("{0}'s Mosaic Enable:{1}", _.name, enableMosaic));
  550.                 result = true;
  551.             });
  552.  
  553.             return result;
  554.         }
  555.  
  556.         public void DeMosaicToys()
  557.         {
  558.             Renderer[] rs = GetToysMosaicRenderer();
  559.             if (rs != null)
  560.             {
  561.                 foreach (Renderer r in rs)
  562.                 {
  563.                     ToyMaterialChanger(r);
  564.                 }
  565.             }
  566.         }
  567.  
  568.         private void ToyMaterialChanger(Renderer mosaic)
  569.         {
  570.             if (mosaic.materials.Length == 1)
  571.             {
  572.                 return;
  573.             }
  574.             bool mosaicDisable = Target >= DeMosaic.Switch.MaidAndItem;
  575.             mosaic.materials
  576.                 .Where(_ => mosaicDisable ? _.shader.Equals(shaderMosaic) : _.shader.Equals(shaderTransparent))
  577.                 .ForEach(_ =>
  578.                 {
  579.                     _.shader = mosaicDisable ? shaderTransparent : shaderMosaic;
  580.                     _.color += mosaicDisable ? new Color(0f, 0f, 0f, -1f) : new Color(0f, 0f, 0f, 1f);
  581.                 });
  582.         }
  583.     }
  584.  
  585.     public class ChangeBodyTexture
  586.     {
  587.         public static List<string> textureList;
  588.             public static readonly string path = Path.Combine(
  589.                 UnityInjector.Extensions.UserDataPath,
  590.                 "YASD"
  591.                 );
  592.  
  593.         public ChangeBodyTexture()
  594.         {
  595.             if (!Directory.Exists(path))
  596.             {
  597.                 Directory.CreateDirectory(path);
  598.             }
  599.  
  600.             textureList = new List<string>();
  601.             LoadTextureList(textureList);
  602. #if DEBUG
  603.             foreach (var item in textureList)
  604.             {
  605.                 System.Diagnostics.Debug.WriteLine(item);
  606.             }
  607. #endif
  608.         }
  609.  
  610.         public class TextureSet
  611.         {
  612.  
  613.             private const string diffuseType = "_MainTex";
  614.             private const string toonType = "_ToonRamp";
  615.             private const string shadowType = "_ShadowTex";
  616.             private const string shadowRateToonType = "_ShadowRateToon";
  617.  
  618.             public Tex MainTex;
  619.             public Tex Toon;
  620.             public Tex Shadow;
  621.             public Tex ShadowRateToon;
  622.  
  623.             public TextureSet(Material material, bool force)
  624.             {
  625.                 MainTex = new Tex(diffuseType, material);
  626.                 if (!MainTex.IsReplaced || force)
  627.                 {
  628.                     Toon = new Tex(toonType, material);
  629.                     Shadow = new Tex(shadowType, material);
  630.                     ShadowRateToon = new Tex(shadowRateToonType, material);
  631.                 }
  632.             }
  633.  
  634.             public class Tex
  635.             {
  636.                 private readonly Regex rx = new Regex(@"(?<name>[^:]+)\.tex");
  637.  
  638.                 private Material material;
  639.  
  640.                 private string _type;
  641.  
  642.                 public string Type
  643.                 {
  644.                     get { return _type; }
  645.                 }
  646.  
  647.                 private Texture _texture;
  648.  
  649.                 public Texture Texture
  650.                 {
  651.                     get { return _texture; }
  652.                 }
  653.  
  654.                 private string _name;
  655.  
  656.                 public string Name
  657.                 {
  658.                     get
  659.                     {
  660.                         return _name;
  661.                     }
  662.                 }
  663.  
  664.                 public string FullPath
  665.                 {
  666.                     get { return Path.Combine(path, Name); }
  667.                 }
  668.  
  669.                 public bool IsReplaced
  670.                 {
  671.                     get { return Path.GetExtension(Texture.name).Equals(".png", System.StringComparison.OrdinalIgnoreCase); }
  672.                 }
  673.  
  674.                 public Tex(String textureType, Material mate)
  675.                 {
  676.                     this.material = mate;
  677.                     _type = textureType;
  678.                     _texture = material.GetTexture(Type);
  679.                     Match m = rx.Match(this._texture.name);
  680.                     if (m.Success)
  681.                     {
  682.                         _name = m.Groups["name"].Value + ".png";
  683.                     }
  684.                     else
  685.                     {
  686.                         _name = Texture.name;
  687.                     }
  688.                 }
  689.  
  690.                 public void ReplaceTex()
  691.                 {
  692.                     System.Diagnostics.Debug.WriteLine(string.Format("YASD.ReplaceTex: Texture Name {0}", Texture.name));
  693.                     if (!textureList.Any(_ => _.Equals(Name, StringComparison.OrdinalIgnoreCase)))
  694.                     {
  695.                         System.Diagnostics.Debug.WriteLine("YASD.ReplaceTex: FileName not Found in FileList");
  696.                         return;
  697.                     }
  698.                     if (!Texture)
  699.                     {
  700.                         System.Diagnostics.Debug.WriteLine("YASD.ReplaceTex: Texture is NULL.");
  701.  
  702.                         return;
  703.                     }
  704.                     if (string.IsNullOrEmpty(Name))
  705.                     {
  706.                         System.Diagnostics.Debug.WriteLine("YASD.ReplaceTex: Texture's Name is NULL or Empty.");
  707.                         return;
  708.                     }
  709.  
  710.                     try
  711.                     {
  712.                         if (!File.Exists(FullPath))
  713.                         {
  714.                             textureList.Remove(Name);
  715.                             return;
  716.                         }
  717.                         System.Diagnostics.Debug.WriteLine(string.Format("YASD.ReplaceTex: {0} found.", Name));
  718.  
  719.                         using (BinaryReader br = new BinaryReader(File.OpenRead(FullPath)))
  720.                         {
  721.                             byte[] b = br.ReadBytes((int)br.BaseStream.Length);
  722.  
  723.                             Texture2D t = new Texture2D(1, 1, TextureFormat.RGBA32, false);
  724.  
  725.                             if (t.LoadImage(b))
  726.                             {
  727.                                 t.name = Name;
  728.                                 material.SetTexture(Type, t);
  729.                                 System.Diagnostics.Debug.WriteLine(string.Format("YASD.ReplaceTex: {0} Replaced.", Name));
  730.                             }
  731.                         }
  732.                     }
  733.                     catch (Exception ex)
  734.                     {
  735.                         Console.WriteLine("YASD.ReplaceTex: Texture Replace Fail!{0} ({1})", Name, ex);
  736.                         textureList.Remove(Name);
  737.                     }
  738.                 }
  739.             }
  740.  
  741.             public void ReplaceTexture(bool force)
  742.             {
  743.                 if (!MainTex.IsReplaced || force)
  744.                 {
  745.                     MainTex.ReplaceTex();
  746.                     Toon.ReplaceTex();
  747.                     Shadow.ReplaceTex();
  748.                     ShadowRateToon.ReplaceTex();
  749.                 }
  750.             }
  751.  
  752.         }
  753.             public static void LoadTextureList(List<string> list)
  754.             {
  755.                 list.Clear();
  756.                 list.AddRange(Directory.GetFiles(path, "*.png").Select(_ => Path.GetFileName(_)));
  757.  
  758.                 Console.WriteLine("YASD: {0} Texture(s) found.", list.Count);
  759.             }
  760.  
  761.         public void ChangeTexture(bool force)
  762.         {
  763.             GameObject maid = GameMain.Instance.CharacterMgr.GetMaid(0).gameObject;
  764.             GameObject body = maid.transform.Find("Offset/_BO_body001/_SM_body001/body").gameObject;
  765.             Material[] materials = body.renderer.materials;
  766.  
  767.             ////GetMaterialsの方がゲーム内で使われている手法に近くて汎用性が高いが1.5倍ぐらい遅い
  768.             //Maid md = GameMain.Instance.CharacterMgr.GetMaid(0);
  769.             //Material[] materials = GetMaterials(md, "body");
  770.  
  771.             foreach (var material in materials)
  772.             {
  773.                 TextureSet tex = new TextureSet(material, force);
  774.  
  775.                 if (Directory.Exists(path))
  776.                 {
  777.                     tex.ReplaceTexture(force);
  778.                 }
  779.             }
  780.         }
  781.  
  782.         public Material[] GetMaterials(Maid maid, string slotname)
  783.         {
  784.             TBody tbody = maid.body0;
  785.             TBodySkin skin = tbody.goSlot[(int)TBody.hashSlotName[slotname]];
  786.             GameObject go = skin.obj;
  787.  
  788.             Renderer[] renderers = go.transform.GetComponentsInChildren<Renderer>(true);
  789.  
  790.             return renderers.Where(_ => _ != null || _.sharedMaterials != null).SelectMany(_ => _.materials).ToArray();
  791.         }
  792.     }
  793. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement