public CreateOrRenameDialog(LuaForAGSEditorComponent component, LuaScriptFileInfo script)
 {
     this.component = component;
     this.script = script;
     this.renaming = true;
     this.folder = script.parent;
     InitializeComponent();
 }
 public CreateOrRenameDialog(LuaForAGSEditorComponent component, LuaScriptFolderInfo scriptFolder, bool creatingFolder)
 {
     this.component = component;
     this.folder = scriptFolder;
     this.renaming = false;
     this.targetingFolder = creatingFolder;
     InitializeComponent();
 }
        public LuaScriptFolderInfo(LuaForAGSEditorComponent l4a_component, LuaScriptFolderInfo parent, DirectoryInfo actualDir)
        {
            this.component = l4a_component;
            this.parent = parent;
            this.dir = actualDir;
            EnsureExistence();

            foreach (DirectoryInfo subdir in dir.GetDirectories())
            {
                // adds self to folders automatically
                new LuaScriptFolderInfo(component, this, subdir);
            }
            foreach (FileInfo file in dir.GetFiles("*.lua"))
            {
                // adds self to Scripts automatically
                new LuaScriptFileInfo(component, this, file);
            }

            InitFileSystemWatcher();
        }
 private void OnWatchedChange(WatcherChangeTypes change, string name, string oldName)
 {
     switch (change)
     {
         case WatcherChangeTypes.Created:
             string path = Path.Combine(dir.FullName, name);
             if (Directory.Exists(path))
             {
                 LuaScriptFolderInfo newFolder
                     = new LuaScriptFolderInfo(component, this, new DirectoryInfo(path));
                 RefreshIcons();
                 if (name == creating)
                 {
                     component.editor.GUIController.ProjectTree.SelectNode(component, newFolder.key);
                 }
             }
             else
             {
                 LuaScriptFileInfo newScript
                     = new LuaScriptFileInfo(component, this, new FileInfo(path));
                 RefreshIcons();
                 if (name == creating)
                 {
                     component.editor.GUIController.ProjectTree.SelectNode(component, newScript.key);
                     newScript.Open();
                 }
             }
             creating = null;
             break;
         case WatcherChangeTypes.Deleted:
         {
             LuaScriptFileInfo script;
             LuaScriptFolderInfo folder;
             if (Scripts.TryGetValue(name.ToLower(), out script))
             {
                 script.BeenDeleted();
             }
             else if (SubFolders.TryGetValue(name.ToLower(), out folder))
             {
                 folder.BeenDeleted();
             }
             break;
         }
         case WatcherChangeTypes.Changed:
             break;
         case WatcherChangeTypes.Renamed:
             if (SubFolders.ContainsKey(oldName.ToLower()))
             {
                 SubFolders[oldName.ToLower()].BeenRenamed(name);
             }
             else if (Scripts.ContainsKey(oldName.ToLower()))
             {
                 Scripts[oldName.ToLower()].BeenRenamed(name);
             }
             break;
     }
 }
 public LuaScriptFolderInfo AddSubFolder(string name)
 {
     LuaScriptFolderInfo theInfo = new LuaScriptFolderInfo(component, this, dir.CreateSubdirectory(name));
     SubFolders.Add(theInfo.dir.Name, theInfo);
     return theInfo;
 }
 void IEditorComponent.RefreshDataFromGame()
 {
     MainForm = Application.OpenForms[0];
     // Copy to new list because something else may be modifying editPanes in another thread(?)
     foreach (LuaScriptEditPane pane in new List<LuaScriptEditPane>(editPanes.Values))
     {
         editor.GUIController.RemovePaneIfExists(pane.contentDocument);
     }
     editPanes.Clear();
     if (IsPluginEnabled)
     {
         scripts.Clear();
         compileTimeScripts.Clear();
         ClearScriptFolders();
         rootScriptFolder = new LuaScriptFolderInfo(this, null, new DirectoryInfo(lscriptsdir));
         rootScriptFolder.EnsureExistence();
         RefreshProjectTree();
         if (restoredScriptsInfo != null)
         {
             rootScriptFolder.UpdateFromXml(restoredScriptsInfo);
             restoredScriptsInfo = null;
         }
         if (restoredCompileScripts != null)
         {
             foreach (string key in restoredCompileScripts)
             {
                 LuaScriptFileInfo script;
                 if (scripts.TryGetValue(key, out script))
                 {
                     compileTimeScripts.Add(script);
                 }
                 else
                 {
                     MessageBox.Show("Cannot find script " + key + " for Compile-Time Action");
                 }
             }
             restoredCompileScripts = null;
         }
         RefreshCompileTimeScripts();
     }
 }
 void IEditorComponent.CommandClick(string controlID)
 {
     if (controlID.StartsWith(DELETESCRIPT_PREFIX))
     {
         string path = controlID.Substring(DELETESCRIPT_PREFIX.Length);
         LuaScriptFileInfo script;
         if (!scripts.TryGetValue(path, out script))
         {
             MessageBox.Show("Cannot find " + path + "!");
         }
         else
         {
             if (MessageBox.Show("'" + script.file.Name + "' will be deleted permanently.",
                 "Are you sure?", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
             {
                 LuaScriptEditPane pane;
                 if (editPanes.TryGetValue(path, out pane))
                 {
                     editor.GUIController.RemovePaneIfExists(pane.contentDocument);
                 }
                 script.Delete();
                 // Cannot delete the node individually, so reload the whole lot
                 //fileInfo.parent.RefreshIcons();
                 //RefreshProjectTree();
             }
         }
         return;
     }
     if (controlID.StartsWith(INCLUDESCRIPT_PREFIX))
     {
         string key = controlID.Substring(INCLUDESCRIPT_PREFIX.Length);
         LuaScriptFileInfo script = scripts[key];
         script.IncludeAtRunTime = !script.IncludeAtRunTime;
         return;
     }
     if (controlID.StartsWith(RENAMESCRIPT_PREFIX))
     {
         string key = controlID.Substring(RENAMESCRIPT_PREFIX.Length);
         LuaScriptFileInfo selectedScript;
         if (!scripts.TryGetValue(key, out selectedScript))
         {
             MessageBox.Show("Cannot find " + key + "!");
         }
         else
         {
             new CreateOrRenameDialog(this, selectedScript).ShowDialog();
         }
         return;
     }
     if (controlID.StartsWith(DELETEFOLDER_PREFIX))
     {
         string path = controlID.Substring(DELETEFOLDER_PREFIX.Length);
         LuaScriptFolderInfo folder;
         if (!scriptFolders.TryGetValue(path, out folder))
         {
             MessageBox.Show("Cannot find " + path + "!");
         }
         else
         {
             if (MessageBox.Show("'" + folder.dir.Name + "', and all files and subfolders contained within it, will be deleted permanently.",
                 "Are you sure?", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
             {
                 folder.Delete();
             }
         }
         return;
     }
     if (controlID.StartsWith(RENAMEFOLDER_PREFIX))
     {
         string key = controlID.Substring(RENAMEFOLDER_PREFIX.Length);
         LuaScriptFolderInfo folder;
         if (!scriptFolders.TryGetValue(key, out folder))
         {
             MessageBox.Show("Cannot find " + key + "!");
         }
         else
         {
             new CreateOrRenameDialog(this, folder).ShowDialog();
         }
         return;
     }
     if (controlID.StartsWith(CTSCRIPT_MOVEDOWN_PREFIX))
     {
         int index = int.Parse(controlID.Substring(CTSCRIPT_MOVEDOWN_PREFIX.Length));
         LuaScriptFileInfo this_script = compileTimeScripts[index];
         LuaScriptFileInfo swap_with = compileTimeScripts[index+1];
         compileTimeScripts[index + 1] = this_script;
         compileTimeScripts[index] = swap_with;
         RefreshCompileTimeScripts();
         editor.GUIController.ProjectTree.SelectNode(this, COMPILETIMEENTRY_PREFIX + (index + 1));
         return;
     }
     if (controlID.StartsWith(CTSCRIPT_MOVEUP_PREFIX))
     {
         int index = int.Parse(controlID.Substring(CTSCRIPT_MOVEUP_PREFIX.Length));
         LuaScriptFileInfo this_script = compileTimeScripts[index];
         LuaScriptFileInfo swap_with = compileTimeScripts[index - 1];
         compileTimeScripts[index - 1] = this_script;
         compileTimeScripts[index] = swap_with;
         RefreshCompileTimeScripts();
         editor.GUIController.ProjectTree.SelectNode(this, COMPILETIMEENTRY_PREFIX + (index - 1));
         return;
     }
     if (controlID.StartsWith(CTSCRIPT_REMOVE_PREFIX))
     {
         if (DialogResult.Yes != MessageBox.Show("Are you sure you want to Remove this Action?",
             "Are you sure?",
             MessageBoxButtons.YesNo,
             MessageBoxIcon.Exclamation))
         {
             return;
         }
         int index = int.Parse(controlID.Substring(CTSCRIPT_REMOVE_PREFIX.Length));
         compileTimeScripts.RemoveAt(index);
         RefreshCompileTimeScripts();
         if (index < compileTimeScripts.Count)
         {
             editor.GUIController.ProjectTree.SelectNode(this, COMPILETIMEENTRY_PREFIX + index);
         }
         return;
     }
     if (controlID.StartsWith(COMPILETIME_PREFIX))
     {
         LuaScriptFileInfo script = scripts[controlID.Substring(COMPILETIME_PREFIX.Length)];
         if (compileTimeScripts.Contains(script))
         {
             if (DialogResult.Yes != MessageBox.Show(
                 "This script is already run at compile time at least once. "
                 + "Are you sure want to run it multiple times?",
                 "Script Already Runs At Compile-Time",
                 MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question))
             {
                 return;
             }
         }
         string new_entry_id = COMPILETIMEENTRY_PREFIX + compileTimeScripts.Count;
         compileTimeScripts.Add(script);
         editor.GUIController.ProjectTree.StartFromNode(this, COMPILE_TIME_SCRIPTS);
         editor.GUIController.ProjectTree.AddTreeLeaf(this, new_entry_id, compileTimeScripts.Count + ". Run: " + script.ScriptPath, SCRIPT_ICON, false);
         editor.GUIController.ProjectTree.SelectNode(this, new_entry_id);
         return;
     }
     if (controlID.StartsWith(SCRIPTS_ROOT))
     {
         if (!controlID.EndsWith("/"))
         {
             string key = controlID;
             LuaScriptFileInfo script;
             if (!scripts.TryGetValue(key, out script))
             {
                 MessageBox.Show("Cannot find " + key + "!");
             }
             else
             {
                 script.Open();
             }
         }
         else
         {
             // Double click on folder = do nothing
         }
         return;
     }
     if (controlID.StartsWith(NEWSCRIPT_PREFIX))
     {
         string path = controlID.Substring(NEWSCRIPT_PREFIX.Length);
         LuaScriptFolderInfo folder;
         if (!scriptFolders.TryGetValue(path, out folder))
         {
             MessageBox.Show("Cannot find " + path + "!");
         }
         else
         {
             new CreateOrRenameDialog(this, folder, false).ShowDialog();
         }
         return;
     }
     if (controlID.StartsWith(COMPILETIMEENTRY_PREFIX))
     {
         int index = int.Parse(controlID.Substring(COMPILETIMEENTRY_PREFIX.Length));
         LuaScriptFileInfo script = compileTimeScripts[index];
         editor.GUIController.ProjectTree.SelectNode(this, script.key);
         return;
     }
     if (controlID.StartsWith(NEWFOLDER_PREFIX))
     {
         string path = controlID.Substring(NEWFOLDER_PREFIX.Length);
         LuaScriptFolderInfo folder;
         if (!scriptFolders.TryGetValue(path, out folder))
         {
             MessageBox.Show("Cannot find " + path + "!");
         }
         else
         {
             new CreateOrRenameDialog(this, folder, true).ShowDialog();
         }
         return;
     }
     if (controlID.StartsWith(VISUAL_ROOT))
     {
         string[] split = controlID.Split(new string[] { "::" }, StringSplitOptions.None);
         VisualLuaValue val;
         if (split.Length == 2)
         {
             if (!visualValues.TryGetValue(split[0], out val))
             {
                 MessageBox.Show("Cannot find Visual Value " + split[0]);
                 return;
             }
             val.OnCommand(split[1]);
         }
         else
         {
             if (!visualValues.TryGetValue(controlID, out val))
             {
                 MessageBox.Show("Cannot find Visual Value " + controlID);
                 return;
             }
             val.OnCommand(null);
         }
     }
     switch (controlID)
     {
         case LUA_CONVERT_MENUOPTION:
             editor.GUIController.AddOrShowPane(codeConvertContentDoc);
             break;
         case CONTROL_ID_ENABLE:
             IsPluginEnabled = !IsPluginEnabled;
             if (IsPluginEnabled)
             {
                 if (!ShownEnableMessage)
                 {
                     ShownEnableMessage = true;
                     MessageBox.Show("Remember to also activate the Lua Run-Time Component in the \"Plugins\" section.",
                         "Run-Time Component Reminder", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 }
                 rootScriptFolder = new LuaScriptFolderInfo(this, null, new DirectoryInfo(lscriptsdir));
                 RefreshProjectTree();
             }
             else
             {
                 if (!ShownDisableMessage)
                 {
                     ShownDisableMessage = true;
                     MessageBox.Show("Remember to also deactivate the Lua Run-Time Component in the \"Plugins\" section.",
                         "Run-Time Component Reminder", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 }
                 scripts.Clear();
                 compileTimeScripts.Clear();
                 ClearScriptFolders();
             }
             break;
         default:
             //_editor.GUIController.ShowMessage(controlID, MessageBoxIconType.Information);
             break;
     }
 }
 internal void ClearScriptFolders()
 {
     foreach (KeyValuePair<string, LuaScriptFolderInfo> folder in scriptFolders)
     {
         if (folder.Value.watcher != null)
         {
             folder.Value.watcher.EnableRaisingEvents = false;
         }
     }
     scriptFolders.Clear();
     rootScriptFolder = null;
 }