/// <summary>
        /// シェル情報一括読み込み
        /// </summary>
        protected virtual void LoadShells()
        {
            var appDirPath = Util.GetAppDirPath();

            // 既存情報クリア
            lstShell.Clear();
            imgListFace.Images.Clear();

            // FMO情報更新
            UpdateFMOInfo();

            // コマンドライン引数を解釈
            var args = Environment.GetCommandLineArgs().ToList();

            args.RemoveAt(0);                              // 先頭はexe名のため削除

            var caller = args.First();                     // 呼び出し元ゴースト (id or "unspecified")

            args.RemoveAt(0);                              // 先頭削除
            var ghostDirPath = args.First().TrimEnd('\\'); // ゴーストフォルダパス

            SakuraFMOData target  = null;
            var           matched = Regex.Match(caller, @"^id:(.+?)\z");

            if (matched.Success)
            {
                // idが指定された場合、そのidと一致するゴースト情報を探す
                var id = matched.Groups[1].Value.Trim();
                target = FMOGhostList.FirstOrDefault(g => g.Id == id);
            }

            if (caller == "unspecified")
            {
                // "unspecified" が指定された場合、現在起動しているゴーストのうち、ランダムに1体を呼び出し元とする (デバッグ用)
                var rand = new Random();
                target = FMOGhostList[rand.Next(0, FMOGhostList.Count - 1)];
            }

            if (target == null)
            {
                throw new Exception(string.Format("指定されたゴーストが見つかりませんでした。 ({0})", caller));
            }

            // 呼び出し元の情報をプロパティにセット
            CallerId         = target.Id;
            CallerSakuraName = target.Name;
            CallerKeroName   = target.KeroName;
            CallerHWnd       = (IntPtr)target.HWnd;

            // チェックボックスの位置移動
            ChkCloseAfterChange.Left = BtnChange.Left + 1;

            // ゴースト情報読み込み
            var ghost = ExplorerGhost.Load(ghostDirPath);

            // シェル情報読み込み
            ShellManager = ShellManager.Load(ghostDirPath, ghost.SakuraDefaultSurfaceId, ghost.KeroDefaultSurfaceId);

            // シェルが1件も取得できなかった場合はエラー表示
            if (!ShellManager.ListItems.Any())
            {
                MessageBox.Show("有効なシェルフォルダが1件も見つかりませんでした。"
                                , "エラー"
                                , MessageBoxButtons.OK
                                , MessageBoxIcon.Error);
                Application.Exit();
                return;
            }

            // 最終起動時の記録があり、かつ最終起動時とバージョンが異なる場合は、キャッシュをすべて破棄
            if (CurrentProfile.LastBootVersion != null && Util.GetVersion() != CurrentProfile.LastBootVersion)
            {
                Directory.Delete(Util.GetCacheBaseDirPath(), recursive: true);
            }

            // 最終起動情報をセットして、Profileを保存
            CurrentProfile.LastBootVersion = Util.GetVersion();
            Util.SaveProfile(CurrentProfile);

            // ゴーストの顔画像を変換・取得
            var faceImages = ShellManager.GetFaceImages(imgListFace.ImageSize);

            // リストビュー構築処理
            foreach (var shellItem in ShellManager.ListItems)
            {
                // 顔画像を正常に読み込めていれば、イメージリストに追加
                if (faceImages.ContainsKey(shellItem.DirPath))
                {
                    imgListFace.Images.Add(shellItem.DirPath, faceImages[shellItem.DirPath]);
                }

                // リスト項目追加
                var item = lstShell.Items.Add(key: shellItem.DirPath, text: shellItem.Name ?? "", imageKey: shellItem.DirPath);
                item.Tag = shellItem.DirPath;
            }

            // 読み込み中表示を消す
            lblLoading.Hide();

            // ゴースト情報リストの更新に伴う画面表示更新
            UpdateUIOnFMOChanged();

            // ボタン等表示状態を更新
            UpdateUIState();

            // 現在の使用シェルを選択
            var currentShellFolderName = Path.GetFileName(ghost.CurrentShellRelDirPath);

            foreach (ListViewItem item in lstShell.Items)
            {
                var shellFolderName = Path.GetFileName((string)item.Tag);
                if (shellFolderName == currentShellFolderName)
                {
                    item.Focused  = true;
                    item.Selected = true;
                    break;
                }
            }

            // 選択対象のシェルを判別できなかった場合は、1件目を選択
            if (SelectedShellListItem == null)
            {
                lstShell.Items[0].Focused  = true;
                lstShell.Items[0].Selected = true;
            }

            // スクロール
            var selectedIndex = lstShell.SelectedItems[0].Index;

            lstShell.EnsureVisible(selectedIndex);
        }
Exemple #2
0
        /// <summary>
        /// 起動後の初期設定 (設定読み込み、ゴースト情報読み込みなど)
        /// </summary>
        protected virtual void Setup()
        {
            // 既存情報クリア
            lstGhost.Clear();
            imgListFace.Images.Clear();

            // FMO情報更新
            UpdateFMOInfo();

            // この時点でSSPのパスが特定できない場合は、SSPが起動していないためエラー

            // コマンドライン引数を解釈
            var args = Environment.GetCommandLineArgs().ToList();

            args.RemoveAt(0);                   // 先頭はexe名のため削除

            var caller = args.FirstOrDefault(); // 呼び出し元ゴースト (id or "unspecified") 省略された場合はunspecified扱い

            if (args.Any())
            {
                args.RemoveAt(0); // 先頭削除
            }

            var specifiedSSPDirPath = args.FirstOrDefault(); // SSPが存在するフォルダパス

            if (!string.IsNullOrWhiteSpace(specifiedSSPDirPath))
            {
                if (!Directory.Exists(specifiedSSPDirPath))
                {
                    MessageBox.Show("起動パラメータで指定されたSSPフォルダが見つかりませんでした。\n" + specifiedSSPDirPath
                                    , "エラー"
                                    , MessageBoxButtons.OK
                                    , MessageBoxIcon.Error);
                    Application.Exit();
                    return;
                }

                SSPDirPath = specifiedSSPDirPath;
            }
            if (args.Any())
            {
                args.RemoveAt(0); // 先頭削除
            }

            // この時点でSSPパスが特定できていない場合はエラー
            if (SSPDirPath == null)
            {
                MessageBox.Show("SSPが見つかりませんでした。\nゴーストエクスプローラ通は、SSPが起動している状態で実行するか、もしくはWindowsのスタートメニューから実行する必要があります。"
                                , "エラー"
                                , MessageBoxButtons.OK
                                , MessageBoxIcon.Error);
                Application.Exit();
                return;
            }

            SakuraFMOData target = null;

            if (!string.IsNullOrWhiteSpace(caller))
            {
                var matched = Regex.Match(caller, @"^id:(.+?)\z");
                if (matched.Success)
                {
                    // idが指定された場合、そのidと一致するゴースト情報を探す
                    var id = matched.Groups[1].Value.Trim();
                    target = FMOGhostList.FirstOrDefault(g => g.Id == id);
                }
            }

            if (caller == "unspecified" || string.IsNullOrWhiteSpace(caller))
            {
                // "unspecified" が指定された場合、現在起動しているゴーストのうち、ランダムに1体を呼び出し元とする
                if (FMOGhostList.Any())
                {
                    var rand = new Random();
                    target = FMOGhostList[rand.Next(0, FMOGhostList.Count - 1)];
                }
            }

            // SSPのapp.datからゴーストフォルダの設定を取得
            var ghostDirPaths = new List <string>();
            var appDataPath   = Path.Combine(SSPDirPath, @"data\profile\app.dat");

            if (File.Exists(appDataPath))
            {
                // descript.txtと同じフォーマット
                var appData = DescriptText.Load(appDataPath);

                // path.ghost.Num を取得
                var ghostNumStr = appData.Get("path.ghost.Num");
                int ghostNum;
                if (ghostNumStr != null && int.TryParse(ghostNumStr, out ghostNum))
                {
                    // ゴーストパスを1つずつ取得して追加
                    for (var i = 0; i < ghostNum; i++)
                    {
                        var dirPath = appData.Get(string.Format("path.ghost.{0}", i));
                        if (dirPath != null)
                        {
                            // 最初が "." から始まっている場合は相対パスとみなして、絶対パスに変換
                            if (dirPath.StartsWith("."))
                            {
                                dirPath = Path.GetFullPath(Path.Combine(SSPDirPath, dirPath));
                            }

                            // 存在するなら追加
                            if (Directory.Exists(dirPath))
                            {
                                ghostDirPaths.Add(dirPath);
                            }
                        }
                    }
                }
            }

            // ゴーストフォルダ選択パスの設定
            foreach (var dirPath in ghostDirPaths)
            {
                var trimmedPath = dirPath.TrimEnd('\\');
                var label       = string.Format("{0} [{1}]", Path.GetFileName(trimmedPath), trimmedPath);
                cmbGhostDir.Items.Add(new DropDownItem()
                {
                    Value = trimmedPath, Label = label
                });
            }

            // パスが複数ない場合は、コンボボックス非表示
            if (ghostDirPaths.Count == 1)
            {
                lstGhost.Height    += lstGhost.Top;
                lstGhost.Top        = 0;
                cmbGhostDir.Visible = false;
            }

            // 呼び出し元の情報をプロパティにセット
            if (target != null)
            {
                CallerId         = target.Id;
                CallerSakuraName = target.Name;
                CallerKeroName   = target.KeroName;
                CallerHWnd       = (IntPtr)target.HWnd;
            }

            // ボタンラベル変更
            BtnChange.Text = string.Format("{0}から切り替え", CallerSakuraName);

            // チェックボックスの位置移動
            ChkCloseAfterChange.Left = BtnChange.Left + 1;

            // イメージリストに不在アイコンを追加
            AbsenceImageKeys.Clear();
            foreach (var path in Directory.GetFiles(Path.Combine(Util.GetAppDirPath(), @"res\absence_icon"), "*.png"))
            {
                var fileName = Path.GetFileName(path);
                imgListFace.Images.Add(fileName, new Bitmap(path));
                AbsenceImageKeys.Add(fileName);
            }

            // 最終起動時の記録があり、かつ最終起動時とバージョンが異なる場合は、キャッシュをすべて破棄
            if (CurrentProfile.LastBootVersion != null && Util.GetVersion() != CurrentProfile.LastBootVersion)
            {
                Directory.Delete(Util.GetCacheDirPath(), recursive: true);
            }

            // 最終起動情報をセットして、Profileを保存
            CurrentProfile.LastBootVersion = Util.GetVersion();
            Util.SaveProfile(CurrentProfile);

            // ゴーストフォルダ選択ドロップダウンの項目を選択
            // 前回の選択フォルダと一致するものがあればそれを選択
            // なければ先頭項目を選択
            {
                var lastUseIndex = -1;
                for (var i = 0; i < cmbGhostDir.Items.Count; i++)
                {
                    var item = (DropDownItem)cmbGhostDir.Items[i];
                    if (item.Value == CurrentProfile.LastUsePath)
                    {
                        lastUseIndex = i;
                        break;
                    }
                }

                if (lastUseIndex >= 0)
                {
                    cmbGhostDir.SelectedIndex = lastUseIndex;
                }
                else
                {
                    cmbGhostDir.SelectedIndex = 0;
                }
            }

            // ソート選択ドロップダウンの項目を選択
            // 前回の選択フォルダと一致するものがあればそれを選択
            // なければ先頭項目を選択
            {
                var lastSortIndex = -1;
                for (var i = 0; i < cmbSort.Items.Count; i++)
                {
                    var item = (DropDownItem)cmbSort.Items[i];
                    if (item.Value == CurrentProfile.LastSortType)
                    {
                        lastSortIndex = i;
                        break;
                    }
                }

                if (lastSortIndex >= 0)
                {
                    cmbSort.SelectedIndex = lastSortIndex;
                }
                else
                {
                    cmbSort.SelectedIndex = 0;
                }
            }

            // ゴースト情報の読み込みと一覧表示更新
            UpdateGhostList();
        }