Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CM3D2.AddModsSlider.Plugin.0.0.2.3 : フリーコメント欄の各modパラメータをスライドバーで操作する UnityInjector 用プラグイン
- // メイドエディット画面中にF5で表示トグル。
- // コメント欄で操作する数値をスライドバーで調整する事が可能。
- // \CM3D2\UnityInjector\Config\ModsParam.xml (サンプル: http://pastebin.com/wiCL6ETM )で各modパラメータを定義。このファイル必須。
- // フリーコメント欄にそのmodの有効な書式が在れば、該当するスライドバーが表示される。
- // 更新履歴
- // 0.0.2.3 フォントサイズがウィンドウ幅と比例する様に。
- // スクロールバーが滑らかに。
- // ModsParam.xmlの<mod>属性 forced="..." が正常に機能していなかったのを修正。
- // リフレクションがフレーム毎に実行されてたのを修正。(軽量化)
- // 0.0.2.2 ModsParam.xmlの書式を変更。
- // ModsParam.xmlで<mod>の属性に forced="true" 指定で、フリーコメント欄にかかわらずそのmodのスライダーが表示される様に。
- // <value>の属性でtype="scale"指定の時、最小値をスライダーに反映してなかったのを修正。
- // 0.0.1.1 各mod定義をxmlファイルにして読み込む様に。
- // スライダーバーとフリーコメント欄が連動する様に。(改造スレその2>>192)
- // EYEBALL,TEST_EYE_ANGの縦横が逆だったのを修正。
- // 0.0.0.0 初版
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Xml;
- using UnityEngine;
- using UnityInjector.Attributes;
- namespace CM3D2.AddModsSlider.Plugin
- {
- [PluginFilter("CM3D2x64"),
- PluginFilter("CM3D2x86"),
- PluginFilter("CM3D2VRx64"),
- PluginName("CM3D2 AddModsSlider"),
- PluginVersion("0.0.2.3")]
- public class AddModsSlider : UnityInjector.PluginBase
- {
- public const string Version = "0.0.2.3";
- private int sceneLevel;
- private bool visible = false;
- private bool success = true;
- private ModsParam mp;
- private UIInput uiInputFreeComment;
- private Vector2 scrollViewVector = Vector2.zero;
- public class ModsParam
- {
- public string DefMatchPattern = @"([-+]?[0-9]*\.?[0-9]+)";
- public string XmlFileName = Directory.GetCurrentDirectory() + @"\UnityInjector\Config\ModsParam.xml";
- public string[] sKey;
- public Dictionary<string, float[]> fValue = new Dictionary<string, float[]>();
- public Dictionary<string, float[]> fVmin = new Dictionary<string, float[]>();
- public Dictionary<string, float[]> fVmax = new Dictionary<string, float[]>();
- public Dictionary<string, string[]> sLabel = new Dictionary<string, string[]>();
- public Dictionary<string, bool[]> bScale = new Dictionary<string, bool[]>();
- public Dictionary<string, string[]> sMatchPattern = new Dictionary<string, string[]>();
- public Dictionary<string, string> sDescription = new Dictionary<string, string>();
- public Dictionary<string, bool> bForced = new Dictionary<string, bool>();
- public Dictionary<string, bool> bEnabled = new Dictionary<string, bool>();
- public int KeyCount { get{return this.sKey.Length; } }
- public int ValCount(string key) { return this.fValue[key].Length; }
- //--------
- public ModsParam()
- {
- this.Init();
- }
- public bool Init()
- {
- if(!loadModsParamXML(this))
- {
- Debug.LogError("AddModsSlider : loadModsParamXML() failed.");
- return false;
- }
- return true;
- }
- private bool loadModsParamXML(ModsParam mp)
- {
- string fn = mp.XmlFileName;
- bool flag = true;
- if (File.Exists(fn))
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(fn);
- XmlNode root = doc.DocumentElement;
- string format = ((XmlElement)root).GetAttribute("format");
- if (format != "1.0")
- {
- flag = loadModsParamXML00(mp, doc);
- }
- else if (root.HasChildNodes)
- {
- XmlNodeList mod_nodes = ((XmlElement)root).GetElementsByTagName("mod");
- if (mod_nodes.Count > 0)
- {
- mp.sKey = new string[mod_nodes.Count];
- }
- else
- {
- flag = false;
- }
- for (int i=0; i<mod_nodes.Count; i++)
- {
- if (!flag) break;
- // modノード
- XmlNode mod_node = mod_nodes[i];
- string key = ((XmlElement)mod_node).GetAttribute("id");
- if (key != "" )
- {
- mp.sKey[i] = key;
- }
- else
- {
- flag = false;
- break;
- }
- // mod属性
- bool f,r;
- mp.sDescription[key] = ((XmlElement)mod_node).GetAttribute("description");
- r = Boolean.TryParse(((XmlElement)mod_node).GetAttribute("forced"), out f);
- mp.bForced[key] = (r) ? f :false;
- r = Boolean.TryParse(((XmlElement)mod_node).GetAttribute("enabled"), out f);
- mp.bEnabled[key] = (r) ? f :false;
- XmlNodeList values = ((XmlElement)mod_node).GetElementsByTagName("value");
- if (values.Count > 0)
- {
- int tmp = values.Count;
- mp.fValue[key] = new float[tmp];
- mp.fVmin[key] = new float[tmp];
- mp.fVmax[key] = new float[tmp];
- mp.sLabel[key] = new string[tmp];
- mp.bScale[key] = new bool[tmp];
- mp.sMatchPattern[key] = new string[tmp];
- }
- else flag = false;
- // valueノード
- for (int j=0; j<values.Count; j++)
- {
- if (!flag) break;
- mp.fValue[key][j] = 0f;
- mp.fVmin[key][j] = 0f;
- mp.fVmax[key][j] = 10f;
- mp.sLabel[key][j] = "";
- mp.bScale[key][j] = false;
- mp.sMatchPattern[key][j] = "";
- XmlNode val_node = values[j];
- int arg_no = -1;
- // value属性
- r = Int32.TryParse(((XmlElement)val_node).GetAttribute("arg_no"), out arg_no);
- arg_no = (r) ? arg_no-1 : -1;
- if (arg_no >= 0)
- {
- r = Single.TryParse(((XmlElement)val_node).GetAttribute("min"), out mp.fVmin[key][arg_no]);
- if(!r) mp.fVmin[key][arg_no] = 0f;
- r = Single.TryParse(((XmlElement)val_node).GetAttribute("max"), out mp.fVmax[key][arg_no]);
- if(!r) mp.fVmax[key][arg_no] = 10f;
- mp.sLabel[key][arg_no] = ((XmlElement)val_node).GetAttribute("label");
- mp.sMatchPattern[key][arg_no] = ((XmlElement)val_node).GetAttribute("match_pattern");
- string sv = ((XmlElement)mod_node).GetAttribute("type");
- switch (sv)
- {
- case "num": mp.bScale[key][arg_no] = false; break;
- case "scale": mp.bScale[key][arg_no] = true; break;
- default : mp.bScale[key][arg_no] = false; break;
- }
- }
- else flag = false;
- }
- }
- }
- }
- else
- {
- Debug.LogError("AddModsSlider : \"" + fn + "\" does not exist.");
- flag = false;
- }
- return flag;
- }
- private bool loadModsParamXML00(ModsParam mp, XmlDocument doc)
- {
- bool flag = true;
- {
- XmlNode root = doc.DocumentElement;
- if (root.HasChildNodes)
- {
- XmlNodeList mods = ((XmlElement)root).GetElementsByTagName("mod");
- if (mods.Count > 0)
- {
- mp.sKey = new string[mods.Count];
- }
- else flag = false;
- for (int i=0; i<mods.Count; i++)
- {
- if (!flag) break;
- XmlNode mod_node = mods[i];
- string key = ((XmlElement)mod_node).GetAttribute("id");
- if (key != "" ) mp.sKey[i] = key;
- else
- {
- flag = false;
- break;
- }
- // mod子ノード
- XmlNodeList values = ((XmlElement)mod_node).GetElementsByTagName("value");
- if (values.Count > 0)
- {
- int tmp = values.Count;
- mp.fValue[key] = new float[tmp];
- mp.fVmin[key] = new float[tmp];
- mp.fVmax[key] = new float[tmp];
- mp.sLabel[key] = new string[tmp];
- mp.bScale[key] = new bool[tmp];
- mp.sMatchPattern[key] = new string[tmp];
- }
- else flag = false;
- // valueノード
- for (int j=0; j<values.Count; j++)
- {
- mp.fValue[key][j] = 0;
- mp.fVmin[key][j] = -10;
- mp.fVmax[key][j] = 10;
- mp.sLabel[key][j] = "";
- mp.bScale[key][j] = false;
- mp.sMatchPattern[key][j] = "";
- if (!flag) break;
- XmlNode val_node = values[j];
- int arg_no = -1;
- if(Int32.TryParse(((XmlElement)val_node).GetAttribute("arg_no"), out arg_no))
- {
- arg_no -= 1;
- }
- else
- {
- arg_no = -1;
- }
- // value子ノード
- if (arg_no >= 0)
- {
- mp.bScale[key][arg_no] = (((XmlElement)val_node).GetAttribute("type") == "scale") ? true : false;
- XmlNodeList value_params = val_node.ChildNodes;
- foreach (XmlNode param in value_params)
- {
- switch (param.LocalName)
- {
- case "value_min":
- flag &= Single.TryParse(param.InnerText, out mp.fVmin[key][arg_no]);
- break;
- case "value_max":
- flag &= Single.TryParse(param.InnerText, out mp.fVmax[key][arg_no]);
- break;
- case "label": mp.sLabel[key][arg_no] = param.InnerText; break;
- case "match_pattern": mp.sMatchPattern[key][arg_no] = param.InnerText; break;
- default : break;
- }
- }
- }
- else flag = false;
- }
- // その他mod子ノード
- XmlNodeList description = ((XmlElement)mod_node).GetElementsByTagName("description");
- mp.sDescription[key] = (description.Count > 0) ? description[0].InnerText : "";
- XmlNodeList forced = ((XmlElement)mod_node).GetElementsByTagName("forced");
- mp.bForced[key] = (forced.Count > 0) ? true : false;
- XmlNodeList enabled = ((XmlElement)mod_node).GetElementsByTagName("enabled");
- mp.bEnabled[key] = (enabled.Count > 0) ? true : false;
- }
- }
- }
- //Debug.LogError("flag = "+ flag);
- return flag;
- }
- }
- //--------
- public void Awake()
- {
- this.mp = new ModsParam();
- }
- public void OnLevelWasLoaded(int level)
- {
- sceneLevel = level;
- if (sceneLevel == 5) success = this.mp.Init();
- else visible = false;
- }
- public void Update()
- {
- if (success) {
- if (Input.GetKeyDown(KeyCode.F5)) visible = !visible;
- if (sceneLevel == 5 && !this.uiInputFreeComment)
- {
- this.uiInputFreeComment = getUIInputFreeComment();
- }
- }
- }
- public void OnGUI()
- {
- if (!success || !visible || sceneLevel != 5) return;
- Maid maid = GameMain.Instance.CharacterMgr.GetMaid(0);
- string freeComment = null;
- if (maid == null) return;
- if (maid.Param != null && maid.Param.status != null && maid.Param.status.free_comment != null)
- {
- freeComment = maid.Param.status.free_comment;
- }
- if (freeComment == null) return;
- checkModsEnabled(this.mp, freeComment);
- addModsSlider(this.mp);
- updateFreeComment(maid, this.mp);
- }
- //--------
- private void checkModsEnabled(ModsParam mp, string freeComment)
- {
- for(int i=0; i<mp.KeyCount; i++)
- {
- string key = mp.sKey[i];
- int val_num = mp.ValCount(key);
- Match match = Regex.Match(freeComment, getMatchPattern(mp, key));
- if (match.Groups.Count > val_num)
- {
- bool tpf = true;
- for(int j=0; j<val_num; j++)
- {
- mp.bEnabled[key] = true;
- tpf &= Single.TryParse(match.Groups[j + 1].Value, out mp.fValue[key][j]);
- }
- if(!tpf) mp.bEnabled[key] = false;
- }
- else mp.bEnabled[key] = false;
- if (mp.bForced[key]) mp.bEnabled[key] = true;
- }
- }
- private void addModsSlider(ModsParam mp)
- {
- int mod_num = mp.KeyCount;
- int margin = fixPx(10);
- int menuHeight = fixPx(45);
- int okButtonHeight = fixPx(90);
- int lineC = fixPx(14);
- int lineH = fixPx(22);
- int lineH2 = fixPx(24);
- int lineH3 = fixPx(30);
- int fontC = fixPx(11);
- int fontH = fixPx(14);
- int fontH2 = fixPx(16);
- int fontH3 = fixPx(20);
- int winTop = margin + menuHeight;
- int winLeft = margin + (int)((Screen.width - margin * 2) * 0.75);
- int winWidth = (int)((Screen.width - margin * 2) * 0.25);
- int winHeight = (int)(Screen.height- winTop - margin - okButtonHeight);
- int conTop = lineH3 + margin * 1;
- int conLeft = margin;
- int conWidth = winWidth - margin * 4 ;
- int conHeight = 0;
- for (int i=0; i<mod_num; i++)
- {
- string key = mp.sKey[i];
- if (!mp.bEnabled[key]) continue;
- int val_num = mp.ValCount(key);
- conHeight += lineH2;
- for (int j=0; j<val_num; j++) conHeight += lineH * 2;
- conHeight += margin * 2;
- }
- Rect outRect = new Rect(winLeft, winTop, winWidth, winHeight);
- GUI.BeginGroup(outRect);
- // 下地のボックス
- GUIStyle bStyle = "box";
- outRect.Set(0, 0, winWidth, winHeight);
- bStyle.fontSize = fontC;
- bStyle.alignment = TextAnchor.UpperRight;
- GUI.Box(outRect, AddModsSlider.Version, bStyle);
- GUIStyle lStyle = "label";
- lStyle.fontSize = fontH3;
- outRect.Set(margin, margin, conWidth, lineH3);
- GUI.Label(outRect, "Mods Slider", lStyle);
- // スクロールビュー
- outRect.Set(0, conTop, winWidth-margin/2, winHeight-conTop-margin);
- scrollViewVector = GUI.BeginScrollView(outRect, scrollViewVector, new Rect (0, 0, conWidth, conHeight));
- // 各modスライダーバー
- outRect.Set(margin, 0, conWidth, lineH2);
- for (int i=0; i<mod_num; i++)
- {
- string key = mp.sKey[i];
- if (!mp.bEnabled[key]) continue;
- int val_num = mp.ValCount(key);
- for (int j=0; j<val_num; j++)
- {
- float value = mp.fValue[key][j];
- float vmin = mp.fVmin[key][j];
- float vmax = mp.fVmax[key][j];
- string label = mp.sLabel[key][j] +" : "+ value.ToString("F");
- bool isScale = mp.bScale[key][j];
- if (j == 0)
- {
- lStyle.fontSize = fontH2;
- outRect.height = lineH2;
- GUI.Label(outRect, key, lStyle);
- outRect.y += outRect.height;
- }
- outRect.height = lineH;
- if (value < vmin) value = vmin;
- if (value > vmax) value = vmax;
- if (isScale && vmin < 1f)
- {
- if (vmin < 0f) vmin = 0f;
- if (value < 0f) value = 0f;
- float tmp = 0;
- float tmpmin = -Mathf.Abs(vmax - 1f);
- float tmpmax = Mathf.Abs(vmax - 1f);
- if (value < 1f)
- {
- tmp = Mathf.Abs((1f-value)/(1f-vmin)) * tmpmin;
- }
- else tmp = value - 1f;
- if(tmp < tmpmin) tmp = tmpmin;
- if(tmp > tmpmax) tmp = tmpmax;
- tmp = drawModValueSlider(outRect, tmp, tmpmin, tmpmax, label, fontH);
- if (tmp < 0f)
- {
- mp.fValue[key][j] = 1f - tmp/tmpmin * Mathf.Abs(1f-vmin);
- }
- else mp.fValue[key][j] = 1f + tmp;
- }
- else mp.fValue[key][j] = drawModValueSlider(outRect, value, vmin, vmax, label, fontH);
- outRect.y += outRect.height * 2;
- }
- outRect.y += margin*2;
- }
- GUI.EndScrollView();
- GUI.EndGroup();
- }
- private void updateFreeComment(Maid maid, ModsParam mp)
- {
- string freeComment = maid.Param.status.free_comment;
- for(int i=0; i<mp.KeyCount; i++)
- {
- string key = mp.sKey[i];
- int vnum = mp.ValCount(key);
- if (mp.bEnabled[key])
- {
- freeComment = Regex.Replace(freeComment, getMatchPattern(mp, key), "");
- string ws = @"#" + key;
- for(int j=0; j<vnum; j++)
- {
- ws += (j == 0) ? "=" : ",";
- ws += mp.fValue[key][j].ToString("F2");
- }
- ws += "#";
- freeComment += ws;
- }
- }
- if(this.uiInputFreeComment) this.uiInputFreeComment.value = freeComment;
- else maid.Param.SetFreeComment(freeComment);
- }
- //--------
- private int fixPx(int px)
- {
- float mag = 1f + (Screen.width/1280f - 1f) * 0.5f;
- return (int)(mag * px);
- }
- private float drawModValueSlider(Rect outRect, float value, float min, float max, string label, int fsize)
- {
- GUIStyle lStyle = "label";
- lStyle.fontSize = fsize;
- GUI.Label(outRect, label, lStyle);
- outRect.y += outRect.height;
- value = GUI.HorizontalSlider(outRect, value, min, max);
- return value;
- }
- private string getMatchPattern(ModsParam mp, string key)
- {
- string s = "#" + key;
- int val_num = mp.ValCount(key);
- for(int j=0; j<val_num; j++)
- {
- s += (j==0) ? "=" : ",";
- if(mp.sMatchPattern[key][j] == "")
- {
- s += mp.DefMatchPattern;
- }
- else
- {
- s += mp.sMatchPattern[key][j];
- }
- }
- s += "#";
- return s;
- }
- // 改造スレその2 >>192
- private UIInput getUIInputFreeComment()
- {
- BindingFlags bf = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
- ProfileMgr profileMgr = BaseMgr<ProfileMgr>.Instance;
- if (profileMgr == null) return null;
- FieldInfo field_m_profileCtrl = typeof(ProfileMgr).GetField("m_profileCtrl", bf);
- if (field_m_profileCtrl == null) return null;
- ProfileCtrl profileCtrl = (ProfileCtrl)field_m_profileCtrl.GetValue(profileMgr);
- if (profileCtrl == null) return null;
- FieldInfo field_m_inFreeComment = typeof(ProfileCtrl).GetField("m_inFreeComment", bf);
- if (field_m_inFreeComment == null) return null;
- UIInput uiInputFreeComment = (UIInput)field_m_inFreeComment.GetValue(profileCtrl);
- if (uiInputFreeComment == null) return null;
- return uiInputFreeComment;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement