「C#/RFS/コード断片」の編集履歴(バックアップ)一覧に戻る

C#/RFS/コード断片 - (2009/03/03 (火) 00:59:21) のソース

codehighlightを使ってみたかっただけという説もある。
だいぶ前に書いたものだからほぼ他人状態w これから直すところ。



*Util
読んで字のごとく。

#codehighlight(css){{
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class Util
    {
        public const string DEFAULT_NICKNAME = "(no nickname)";

        public const string NICKNAME_FILE = "\\_nickname.txt";

        public const string MBAC_ROOT = "..";

        public const string REPLAY_FOLDER = MBAC_ROOT + "\\Replay";

        public static string AvailableFolderName()
        {
            return AvailableFolderName("Replay_1");
        }

        public static string AvailableFolderName(string name)
        {
            if (!Directory.Exists(name))
            {
                return name;
            }

            int i;
            for (i = 1; Directory.Exists(name + "_" + i); i++);
            return name + "_" + i;
        }

        public static void WriteNickname(string folder, string nickname){
            string filename = folder + NICKNAME_FILE;
            StreamWriter w = new StreamWriter(
                filename, false, System.Text.Encoding.Default
            );
            w.WriteLine(nickname);
            w.Flush();
            w.Close();
        }

        public static string ReadNickname(string folder)
        {
            string filename = folder + NICKNAME_FILE;

            try
            {
                if (!File.Exists(filename))
                {
                    WriteNickname(folder, DEFAULT_NICKNAME);
                }
                else {
                    StreamReader r = new StreamReader(
                        filename, System.Text.Encoding.Default
                    );
                    string s = r.ReadLine();
                    r.Close();
                    return s;
                }
            }
            catch (IOException)
            {
                return "(???)";
            }
        }
    }
}
}}


*EntryManager
リプレイフォルダたちを管理するクラス。リプレイフォルダのことをEntryって呼んでた・・・らしいw

#codehighlight(css){{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class EntryManager
    {
        private string[] nicknames = {};

        private string[] paths = {};

        public string[] Nicknames
        {
            get { return nicknames; }
        }

        public string[] Paths
        {
            get { return paths; }
        }

        public void Update()
        {
            paths = Directory.GetDirectories(".", "Replay_*");

            List<string> a = new List<string>();
            foreach (string path in paths)
            {
                string suffix = path.Substring(path.IndexOf('_') + 1);
                a.Add(suffix + ". " + Util.ReadNickname(path));
            }

            nicknames = a.ToArray();
        }

        public void CreateFolder()
        {
            try
            {
                string path = Util.AvailableFolderName();
                Directory.CreateDirectory(path);
            }
            catch (IOException)
            {
                MessageBox.Show("フォルダの作成に失敗しました", "ERROR");
            }
        }

        public void ChangeFolder(int index)
        {
            try
            {
                string path = Util.AvailableFolderName();
                Directory.Move(Util.REPLAY_FOLDER, path);
                Directory.Move(paths[index], Util.REPLAY_FOLDER);
            }
            catch (IOException)
            {
                MessageBox.Show("Replayフォルダのリネームに失敗しました", "ERROR");
            }
        }

        public void MoveReplay(string repname, int index)
        {
            string dest = paths[index] + "\\" + repname;

            if (Directory.Exists(dest))
            {
                MessageBox.Show("リプレイ名が重複してしまいます!", "移動失敗");
            }
            else
            {
                try
                {
                    Directory.Move(Util.REPLAY_FOLDER + "\\" + repname, dest);
                }
                catch (IOException)
                {
                    MessageBox.Show("リプレイのリネームに失敗しました", "ERROR");
                }
            }
        }

        public void DissoluteFolder(int index)
        {
            string target = paths[index];
            string[] srcs = Directory.GetDirectories(target);

            try
            {
                foreach (string src in srcs)
                {
                    int i = src.IndexOf(target) + target.Length + 1;
                    string path0 = Util.REPAY_FOLDER + "\\" + src.Substring(i);
                    string dest = Util.AvailableFolderName(path0);
                    Directory.Move(src, dest);
                }
            }
            catch (IOException)
            {
                MessageBox.Show("リプレイのリネームに失敗しました", "ERROR");
                return;
            }

            if (Directory.GetFiles(target).Length <= 1)
            {
                Directory.Delete(target, true);
            }
            else
            {
                MessageBox.Show("他のファイルがあるので" + target + "の削除は行いません",
                    "他のファイルが残っています");
            }
        }
    }
}
}}

まずメソッドやプロパティを並べてみる:
-Update()
-CreateFolder()
-ChangeFolder(int index)
-MoveReplay(string repname, int index)
-DissoluteFolder(int index)
-string[] Paths
-string[] Nicknames


*ReplayFolderManager
カレントリプレイフォルダを管理するクラス。

#codehighlight(css){{
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class ReplayFolderManager
    {
        private string[] replays = {};

        private string nickname;

        public string[] Replays
        {
            get { return replays; }
        }

        public string Nickname
        {
            get { return nickname; }
            set
            {
                try
                {
                    Util.WriteNickname(Util.REPLAY_FOLDER, value);
                    nickname = value;
                }
                catch (IOException)
                {
                    MessageBox.Show("ニックネームの設定に失敗しました", "ERROR");
                }
            }
        }

        public void Update()
        {
            string[] dir = Directory.GetDirectories(Util.REPLAY_FOLDER, "*");
            List<string> a = new List<string>();

            foreach (string d in dir)
            {
                if (File.Exists(d + "\\" + "_Header.ini"))
                {
                    a.Add(d.Substring(d.IndexOf('\\') + 1));
                }
            }

            replays = a.ToArray();
            nickname = Util.ReadNickname(Util.REPLAY_FOLDER);
        }

        public void RenameReplay(string oldname, string newname)
        {
            string dest = Util.REPLAY_FOLDER + "\\" + newname;

            try
            {
                if (Directory.Exists(dest))
                {
                    MessageBox.Show("リプレイ名が重複してしまいます!", "移動失敗");
                }
                else
                {
                    Directory.Move(Util.REPLAY_FOLDER + "\\" + oldname, dest);
                }
            }
            catch (IOException)
            {
                MessageBox.Show("リプレイのリネームに失敗しました", "ERROR");
            }
        }

        public void RemoveReplay(string repname)
        {
            DialogResult result = MessageBox.Show(
                "本当に '" + repname + "' を削除しますか?",
                "削除の確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if(result == DialogResult.Yes)
            {
                Directory.Delete(Util.REPLAY_FOLDER + "\\" + repname, true);
            }
        }
    }
}
}}

-Update()
-RenameReplay(string oldname, string newname)
-RemoveReplay(string repname)
-string[] Replays
-string Nickname





----