/// <summary> /// プロジェクトの初期化を行う。 /// </summary> public void Initialize( WarlockConsole console = null, bool advancedMode = false) { if (console != null) console.WriteLine("========== プロジェクト初期化開始 =========="); ProjectType = WarlockProjectType.Other; ScriptFolderPath = "Script"; if (!Directory.Exists(this.ScriptFolderFullPath)) { Directory.CreateDirectory(this.ScriptFolderFullPath); } Func<String, String> findRpgProject = null; findRpgProject = (path) => { if (File.Exists(Path.Combine(ProjectPath, path, "Game.rxproj"))) { ProjectType = WarlockProjectType.Xp; if (console != null) console.WriteLine("プロジェクトタイプ検出 - RPGXP"); return path; } else if (File.Exists(Path.Combine(ProjectPath, path, "Game.rvproj"))) { ProjectType = WarlockProjectType.Vx; if (console != null) console.WriteLine("プロジェクトタイプ検出 - RPGVX"); return path; } else if (File.Exists(Path.Combine(ProjectPath, path, "Game.rvproj2"))) { ProjectType = WarlockProjectType.VxAce; if (console != null) console.WriteLine("プロジェクトタイプ検出 - RPGVXAce"); return path; } if (path == "") { foreach (var dirs in Directory.GetDirectories(ProjectPath)) { var result = findRpgProject(dirs.Substring(ProjectPath.Length + 1)); if (result != "") return result; } } return ""; }; var rpgProjectPath = findRpgProject(""); DataFolderPath = Path.Combine(rpgProjectPath, "Data"); if (!Directory.Exists(this.DataFolderFullPath)) { Directory.CreateDirectory(this.DataFolderFullPath); } var iniFilePath = Path.Combine(ProjectPath, rpgProjectPath, "Game.ini"); if (File.Exists(iniFilePath)) { try { if (console != null) console.WriteLine("Game.iniを検出"); var ini = new IniFileAccessor(iniFilePath); ScriptFileName = Path.Combine(rpgProjectPath, ini.GetProfile("Game", "Scripts")); Name = ini.GetProfile("Game", "Title"); if (advancedMode) { if (console != null) console.WriteLine("ScriptFileName: {0}", ScriptFileName); } if (console != null) console.WriteLine("Name: {0}", Name); } catch { // I/Oエラー等は無視 if (console != null) console.WriteLine("Game.iniの読み取りに失敗しました。"); } } else { if (console != null) console.WriteLine("Game.iniが見つかりませんでした。"); } if (!String.IsNullOrEmpty(ScriptFileName) && !File.Exists(ScriptFilePath)) { if (advancedMode) { if (console != null) console.WriteLine("{0}が見つかりませんでした。", ScriptFileName); } ScriptFileName = null; } if (String.IsNullOrEmpty(ScriptFileName)) { ScriptFileName = ""; } if (console != null) console.WriteLine("========== プロジェクト初期化完了 =========="); }
/// <summary> /// スクリプトのエクスポートを行う。 /// </summary> /// <param name="console">コンソールのインスタンス。</param> public void ExportScript(WarlockConsole console = null) { // スクリプトのロード if (console != null) console.WriteLine("========== エクスポート開始 =========="); // 出力フォルダがなければ作成 if (!Directory.Exists(DataFolderFullPath)) { Directory.CreateDirectory(DataFolderFullPath); } var scriptDatas = new List<ScriptData>(); foreach (var definition in GetAllScripts()) { var scriptData = new ScriptData(); if (definition is ClassDefinition) { var castedDefinition = (ClassDefinition)definition; scriptData.Guid = castedDefinition.Guid; scriptData.DisplayName = castedDefinition.FullName; if (console != null) console.WriteLine("{0}({1}) -> {2}", castedDefinition.FullName, castedDefinition.Guid ,scriptData.DisplayName); scriptData.SourceCode = castedDefinition.ToScript(); } else if (definition is ScriptDefinition) { var castedDefinition = (ScriptDefinition)definition; scriptData.Guid = castedDefinition.Guid; scriptData.DisplayName = castedDefinition.DisplayName; if (console != null) console.WriteLine("{0} -> {1}", castedDefinition.FileName, scriptData.DisplayName); if (!castedDefinition.IsFolder && File.Exists(GetScriptFullPath(castedDefinition.FileName))) { using (var sr = new StreamReader(GetScriptFullPath(castedDefinition.FileName))) { scriptData.SourceCode = sr.ReadToEnd(); } } else { scriptData.SourceCode = ""; } } else { throw new NotImplementedException(); } scriptDatas.Add(scriptData); } if (console != null) console.WriteLine("スクリプトをセーブ中 - {0}", ScriptFilePath); File.Copy( ScriptFilePath, String.Format("{0}.{1:yyyyMMddHHmmss}.bak", ScriptFilePath, DateTime.Now), overwrite: true ); WarlockDatabase.SaveScript(ScriptFilePath, scriptDatas); if (console != null) console.WriteLine("スクリプトの再インポート実行"); ImportScript(); if (console != null) console.WriteLine("========== エクスポート完了 =========="); }
/// <summary> /// スクリプトのインポートを行う。 /// </summary> /// <param name="console">コンソールのインスタンス。</param> public void ImportScript(WarlockConsole console = null) { // スクリプトのロード if (console != null) console.WriteLine("========== インポート開始 =========="); if (console != null) console.WriteLine("スクリプトをロード中 - {0}", ScriptFilePath); var scriptDatas = WarlockDatabase.LoadScript(ScriptFilePath); // 出力フォルダがなければ作成 if (!Directory.Exists(ScriptFolderFullPath)) { Directory.CreateDirectory(ScriptFolderFullPath); } // 現在のスクリプト定義をクリア var oldDefinitions = ClearScriptDefinitions(); int nonameIndex = 0; var fileNameCheck = new Regex("^[A-Za-z0-9_]+$"); if (console != null) console.WriteLine("========== スクリプトを出力します =========="); var scriptDefinitionMap = new Dictionary<int, ScriptDefinition>(); int fileCount = 0; foreach (ScriptData scriptData in scriptDatas) { var definition = new ScriptDefinition() { Guid = scriptData.Guid, DisplayName = scriptData.DisplayName }; // 展開前の定義情報にある同一GUIDのデータを取得 ScriptDefinition oldDefinition = null; if (oldDefinitions.ContainsKey(definition.Guid)) { oldDefinition = oldDefinitions[definition.Guid]; } string sourceCode = scriptData.SourceCode; if (!String.IsNullOrEmpty(sourceCode)) { // ソースコードが存在するならファイルを展開 if (oldDefinition != null && !oldDefinition.IsFolder) { // 旧定義が存在するなら旧定義を引き継ぐ definition.FileName = oldDefinition.FileName; definition.ExecuteTarget = oldDefinition.ExecuteTarget; } else { definition.FileName = String.Format("{0}.rb", definition.DisplayName); try { // すでにファイルがある場合は別名を付ける if (File.Exists(GetScriptFullPath(definition.FileName))) { int i = 1; do { definition.FileName = String.Format("{0}_{1}.rb", definition.DisplayName, ++i); } while (File.Exists(GetScriptFullPath(definition.FileName))); } } catch { // 表現不可ならファイル名クリア definition.FileName = ""; } if (String.IsNullOrEmpty(definition.DisplayName) || !fileNameCheck.IsMatch(definition.DisplayName) || String.IsNullOrEmpty(definition.FileName)) { do { definition.FileName = String.Format("UnnamedScript_{0}.rb", ++nonameIndex); } while (File.Exists(GetScriptFullPath(definition.FileName))); } } string outputPath = GetScriptFullPath(definition.FileName); if (console != null) console.WriteLine("{0} -> {1}", definition.DisplayName, outputPath); using (var sw = new StreamWriter(outputPath, append: false, encoding: new UTF8Encoding())) { sw.Write(sourceCode); } fileCount++; } scriptDefinitionMap.Add(definition.Guid, definition); } // 展開前データのツリー構造を復元 foreach (var definition in scriptDefinitionMap.Values) { if (oldDefinitions.ContainsKey(definition.Guid)) { var oldDefinition = oldDefinitions[definition.Guid]; var oldParentDefinition = ( from d in oldDefinitions.Values where d.Children.Contains(oldDefinition) select d ).FirstOrDefault(); if (oldParentDefinition != null) { if (scriptDefinitionMap.ContainsKey(oldParentDefinition.Guid)) { var parentDefinition = scriptDefinitionMap[oldParentDefinition.Guid]; parentDefinition.Children.Add(definition); continue; } } } ScriptDefinitions.Add(definition); } // フォルダ扱いになったスクリプト定義とリンクしたクラス定義が存在する場合、 // クラス定義のGUIDを再発行 { var query1 = from sd in GetScriptDefinitionList() where sd.IsFolder select sd; foreach (var sd in query1) { var scriptDefinition = sd; var query2 = from cd in ClassDefinitions where cd.Guid == scriptDefinition.Guid select cd; foreach (var cd in query2) { var classDefinition = cd; int oldGuid = classDefinition.Guid; classDefinition.Guid = NewScriptGuid(); if (console != null) { console.WriteLine("スクリプトフォルダとクラス定義のGUIDが重複したため、クラス定義のGUIDを変更します"); console.WriteLine( "{0} {1} -> {2}", classDefinition.FullName, oldGuid, classDefinition.Guid ); } } } } if (console != null) console.WriteLine("========== インポート完了: {0} Files ==========", fileCount); }
/// <summary> /// アプリケーションを起動する。 /// </summary> public void Run() { MainForm = new MainForm(); MainForm.Command += CommandHandler; MainForm.Shown += MainForm_Shown; ShowSplashScreen(); if (!Settings.Default.Upgraded) { Settings.Default.Upgrade(); Settings.Default.Upgraded = true; Settings.Default.Save(); } Message = new MessageProvider(); Message.Load(Resources.MessageResource); Console = new WarlockConsole(); Console.Command += CommandHandler; using (ScriptWatcher = new FileSystemWatcher()) { ScriptWatcher.SynchronizingObject = MainForm; ScriptWatcher.Filter = "*.rb"; ScriptWatcher.NotifyFilter = NotifyFilters.LastWrite; ScriptWatcher.Changed += ScriptWatcher_Changed; ScriptWatcher.Deleted += ScriptWatcher_Changed; Application.Run(MainForm); ScriptWatcher.Deleted -= ScriptWatcher_Changed; ScriptWatcher.Changed -= ScriptWatcher_Changed; } Settings.Default.Save(); }