Advertisement
prairial

MHFKeyconfigReader(C#)

May 2nd, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.86 KB | None | 0 0
  1.  
  2. using Microsoft.VisualBasic;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Diagnostics;
  8.  
  9.  
  10. using System.Xml;
  11.  
  12. public class Form1
  13. {
  14.     private System.Collections.Generic.Dictionary<long, string> keyHash;
  15.     const string nullKey = "割り当てなし";
  16.  
  17.  
  18.  
  19.  
  20.     private void Button1_Click(System.Object sender, System.EventArgs e)
  21.     {
  22.         OpenFileDialog ofd = new OpenFileDialog();
  23.         ofd.FileName = "keyconfig.cfg";
  24.         //はじめに表示されるフォルダを指定する
  25.         ofd.InitialDirectory = "C:\\Program Files\\CAPCOM\\Monster Hunter Frontier Online\\";
  26.  
  27.         ofd.Filter = "CFGファイル(*.cfg)|*.cfg";
  28.         ofd.FilterIndex = 1;
  29.         //タイトルを設定する
  30.         ofd.Title = "開くファイルを選択してください";
  31.         //ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする
  32.         ofd.RestoreDirectory = true;
  33.  
  34.         //ダイアログを表示する
  35.         if (ofd.ShowDialog() == DialogResult.OK) {
  36.             TextBox1.Text = ofd.FileName;
  37.         }
  38.     }
  39.  
  40.     private void Button2_Click(System.Object sender, System.EventArgs e)
  41.     {
  42.         if (!System.IO.File.Exists(TextBox2.Text.Trim)) {
  43.             MessageBox.Show("指定したファイルが存在しません");
  44.             return;
  45.         }
  46.  
  47.         readXML(TextBox2.Text.Trim);
  48.  
  49.     }
  50.  
  51.     private void TextBox1_TextChanged(System.Object sender, System.EventArgs e)
  52.     {
  53.         if (string.IsNullOrEmpty(TextBox1.Text.Trim)) {
  54.             Button4.Enabled = false;
  55.         } else {
  56.             Button4.Enabled = true;
  57.         }
  58.     }
  59.  
  60.     private void readXML(string fileName)
  61.     {
  62.         XmlDocument xDocument = new XmlDocument();
  63.         XmlElement keyconfig = null;
  64.         //Dim version As XmlElement
  65.         XmlElement controller = null;
  66.         //Dim keyboard As XmlElement
  67.         //Dim mouse As XmlElement
  68.  
  69.         XmlNodeList categoryList = null;
  70.         XmlNodeList actionList = null;
  71.         XmlNodeList keydataList = null;
  72.         string csvFileName = null;
  73.  
  74.         SaveFileDialog sfd = new SaveFileDialog();
  75.  
  76.         //はじめのファイル名を指定する
  77.         sfd.FileName = "MyKeyconfig.csv";
  78.  
  79.         //[ファイルの種類]に表示される選択肢を指定する
  80.         sfd.Filter = "CSVファイル(*.csv)|*.csv";
  81.         //[ファイルの種類]ではじめに
  82.         //「すべてのファイル」が選択されているようにする
  83.         sfd.FilterIndex = 1;
  84.         //タイトルを設定する
  85.         sfd.Title = "保存先のファイルを選択してください";
  86.         //ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする
  87.         sfd.RestoreDirectory = true;
  88.  
  89.         //ダイアログを表示する
  90.         if (sfd.ShowDialog() == DialogResult.OK) {
  91.  
  92.             csvFileName = sfd.FileName;
  93.         } else {
  94.             return;
  95.         }
  96.  
  97.         keyHash = new System.Collections.Generic.Dictionary<long, string>();
  98.  
  99.  
  100.  
  101.  
  102.         xDocument.Load(fileName);
  103.         //XMLファイルをロード
  104.  
  105.         //ルート要素のkeyconfigを取得
  106.         keyconfig = xDocument.DocumentElement;
  107.  
  108.         //controller要素を取得
  109.         controller = (XmlElement)keyconfig.GetElementsByTagName("controller")[0];
  110.  
  111.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_DLU").Value), "十字↑");
  112.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_DLD").Value), "十字↓");
  113.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_DLL").Value), "十字←");
  114.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_DLR").Value), "十字→");
  115.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_DRD").Value), "×");
  116.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_DRR").Value), "○");
  117.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_R2").Value), "R2");
  118.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_L2").Value), "L2");
  119.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_DRL").Value), "□");
  120.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_DRU").Value), "△");
  121.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_R1").Value), "R1");
  122.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_L1").Value), "L1");
  123.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_L3").Value), "左スティック押し込み");
  124.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_R3").Value), "左スティック押し込み");
  125.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_LC").Value), "SELECT");
  126.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_RC").Value), "START");
  127.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_ALU").Value), "左スティック↑");
  128.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_ALD").Value), "左スティック↓");
  129.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_ALL").Value), "左スティック←");
  130.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_ALR").Value), "左スティック→");
  131.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_ARU").Value), "右スティック↑");
  132.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_ARD").Value), "右スティック↓");
  133.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_ARL").Value), "右スティック←");
  134.         keyHash.Add(convStrToInt(controller.SelectSingleNode("@ASSIGN_ARR").Value), "右スティック→");
  135.  
  136.  
  137.         categoryList = keyconfig.GetElementsByTagName("category");
  138.  
  139.         System.IO.StreamWriter Writer = new System.IO.StreamWriter(csvFileName, false, System.Text.Encoding.GetEncoding("shift_jis"));
  140.  
  141.         //ヘッダー行出力
  142.         Writer.WriteLine("カテゴリ,アクション,キー操作");
  143.  
  144.  
  145.         foreach (XmlElement xCategory in categoryList) {
  146.  
  147.             actionList = xCategory.GetElementsByTagName("action");
  148.  
  149.             foreach (XmlElement xAction in actionList) {
  150.  
  151.                 keydataList = xAction.GetElementsByTagName("keydata");
  152.  
  153.                 if (keydataList.Count > 0) {
  154.                     dynamic xKeydata = keydataList.Item(0);
  155.                     Writer.WriteLine(makeLine(xCategory, xAction, (XmlElement)xKeydata));
  156.                 }
  157.  
  158.  
  159.             }
  160.  
  161.         }
  162.  
  163.         Writer.Close();
  164.  
  165.         MessageBox.Show(csvFileName + "を作成しました。");
  166.     }
  167.  
  168.     private string makeLine(XmlElement xCategory, XmlElement xAction, XmlElement xKeydata)
  169.     {
  170.         string str = null;
  171.         string keyStr = null;
  172.         string subKeyStr = null;
  173.  
  174.         str = xCategory.SelectSingleNode("@name").Value;
  175.         str += "," + xAction.SelectSingleNode("@name").Value;
  176.  
  177.         keyStr = SplitToKeys(convStrToInt(xKeydata.SelectSingleNode("@key").Value));
  178.         subKeyStr = SplitToKeys(convStrToInt(xKeydata.SelectSingleNode("@subkey").Value));
  179.  
  180.         if (subKeyStr == nullKey) {
  181.             str += "," + keyStr;
  182.         } else {
  183.             str += ",(" + subKeyStr + ")" + keyStr;
  184.         }
  185.  
  186.         return str;
  187.     }
  188.  
  189.     private string SplitToKeys(long keyLong)
  190.     {
  191.         string str = "";
  192.         if (keyLong == 0) {
  193.             return nullKey;
  194.  
  195.         }
  196.  
  197.         for (int i = 1; i <= 32; i++) {
  198.             long targetLong = Convert.ToInt64(Math.Pow(2, (i - 1)));
  199.             if (Convert.ToBoolean(keyLong & targetLong)) {
  200.                 str += keyHash[targetLong] + "+";
  201.             }
  202.         }
  203.  
  204.         if (!string.IsNullOrEmpty(str)) {
  205.             str = str.Substring(0, str.Length - 1);
  206.         }
  207.  
  208.         return str;
  209.  
  210.  
  211.     }
  212.  
  213.  
  214.     private void btnTest_Click(System.Object sender, System.EventArgs e)
  215.     {
  216.         //テストボタン
  217.         MessageBox.Show(SplitToKeys(16777216));
  218.  
  219.  
  220.     }
  221.  
  222.     private long convStrToInt(string str)
  223.     {
  224.         long l = 0;
  225.         l = Convert.ToInt64(str.Replace("0x", ""), 16);
  226.         return Convert.ToInt64(str.Replace("0x", ""), 16);
  227.     }
  228.  
  229.     private void Button3_Click_1(System.Object sender, System.EventArgs e)
  230.     {
  231.         OpenFileDialog ofd = new OpenFileDialog();
  232.         ofd.FileName = "keyconfig.xml";
  233.         //はじめに表示されるフォルダを指定する
  234.         ofd.InitialDirectory = "C:\\Program Files\\CAPCOM\\Monster Hunter Frontier Online\\";
  235.  
  236.         ofd.Filter = "XMLファイル(*.xml)|*.xml";
  237.         ofd.FilterIndex = 1;
  238.         //タイトルを設定する
  239.         ofd.Title = "開くファイルを選択してください";
  240.         //ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする
  241.         ofd.RestoreDirectory = true;
  242.  
  243.         //ダイアログを表示する
  244.         if (ofd.ShowDialog() == DialogResult.OK) {
  245.             TextBox2.Text = ofd.FileName;
  246.         }
  247.     }
  248.  
  249.     private void makeCorrectXMLFile(string oldFileName, string newFileName)
  250.     {
  251.         System.IO.StreamReader Reader = new System.IO.StreamReader(oldFileName, System.Text.Encoding.GetEncoding("Shift-JIS"));
  252.         System.IO.StreamWriter Writer = new System.IO.StreamWriter(newFileName, false, System.Text.Encoding.GetEncoding("shift_jis"));
  253.  
  254.         //<keydata key="0x01000000" subkey="0x00000000">
  255.         //を
  256.         //<keydata key="0x01000000" subkey="0x00000000"/>
  257.         //形式に置換する
  258.  
  259.         while (!(Reader.EndOfStream)) {
  260.             string txt = Reader.ReadLine();
  261.  
  262.             if (txt.Contains("keydata")) {
  263.                 txt = txt.Replace(">", "/>");
  264.  
  265.             }
  266.  
  267.             Writer.WriteLine(txt);
  268.  
  269.         }
  270.         Writer.Close();
  271.         Reader.Close();
  272.     }
  273.  
  274.     //妥当なXMLファイルを作成する
  275.     private void Button4_Click(System.Object sender, System.EventArgs e)
  276.     {
  277.         string oldFileName = TextBox1.Text.Trim;
  278.         if (!System.IO.File.Exists(oldFileName)) {
  279.             MessageBox.Show("指定したファイルが存在しません");
  280.             return;
  281.         }
  282.  
  283.         System.IO.FileInfo cFileInfo = new System.IO.FileInfo(oldFileName);
  284.         System.IO.DirectoryInfo hDirInfo = cFileInfo.Directory;
  285.         dynamic newFileName = hDirInfo.FullName + "\\keyconfig.xml";
  286.  
  287.         if (System.IO.File.Exists(newFileName)) {
  288.             dynamic result = MessageBox.Show(newFileName + "が既に存在します。上書きしますか?", "確認", MessageBoxButtons.OKCancel);
  289.             if (result == DialogResult.Cancel) {
  290.                 return;
  291.             }
  292.         }
  293.  
  294.         makeCorrectXMLFile(oldFileName, newFileName);
  295.  
  296.         MessageBox.Show(newFileName + "を作成しました。");
  297.         TextBox2.Text = newFileName;
  298.  
  299.     }
  300.  
  301.     private void TextBox2_TextChanged(System.Object sender, System.EventArgs e)
  302.     {
  303.         if (string.IsNullOrEmpty(TextBox2.Text.Trim)) {
  304.             Button2.Enabled = false;
  305.         } else {
  306.             Button2.Enabled = true;
  307.         }
  308.     }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement