LSLIPathHelper and updates

This commit is contained in:
User 2017-10-26 14:50:23 +02:00
parent 8d6b35dade
commit 086f7d2887
8 changed files with 537 additions and 209 deletions

View file

@ -45,6 +45,7 @@ using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using LSLEditor.Docking; using LSLEditor.Docking;
using LSLEditor.Helpers; using LSLEditor.Helpers;
using System.Collections.Generic;
namespace LSLEditor namespace LSLEditor
{ {
@ -52,7 +53,9 @@ namespace LSLEditor
{ {
public RuntimeConsole runtime; public RuntimeConsole runtime;
private string m_FullPathName; public List<string> verboseQueue = new List<string>();
private string m_FullPathName;
private Guid m_Guid; private Guid m_Guid;
// private bool sOutline = true; // private bool sOutline = true;
public LSLEditorForm parent; public LSLEditorForm parent;
@ -180,13 +183,29 @@ namespace LSLEditor
void TextBox_OnDirtyChanged(object sender, EventArgs e) void TextBox_OnDirtyChanged(object sender, EventArgs e)
{ {
this.Text = this.ScriptName; if(this.Text == null || this.ScriptName == null)
if (this.numberedTextBoxUC1.TextBox.Dirty) { {
this.Text = this.Text.Trim() + "* "; this.Text = this.ScriptName;
} else { }
this.Text = this.Text.Trim() + " "; if (LSLIPathHelper.GetExpandedTabName(this.ScriptName) == this.Text)
} {
TabPage tabPage = this.Tag as TabPage; if (this.numberedTextBoxUC1.TextBox.Dirty)
{
this.Text = this.Text.Trim() + "* ";
}
}
else
{
this.Text = this.ScriptName;
if (this.numberedTextBoxUC1.TextBox.Dirty) {
this.Text = this.Text.Trim() + "* ";
} else {
this.Text = this.Text.Trim() + " ";
}
}
TabPage tabPage = this.Tag as TabPage;
if (tabPage != null) { if (tabPage != null) {
tabPage.Text = this.Text; tabPage.Text = this.Text;
} }
@ -307,30 +326,68 @@ namespace LSLEditor
public void SaveCurrentFile(string strPath) public void SaveCurrentFile(string strPath)
{ {
this.FullPathName = strPath; // Check if this is an expanded.lsl
Encoding encodeAs = this.encodedAs; if (!LSLIPathHelper.IsExpandedLSL(strPath))
if (this.IsScript && encodeAs == null) { {
switch (Properties.Settings.Default.OutputFormat) { this.FullPathName = strPath;
case "UTF8": Encoding encodeAs = this.encodedAs;
encodeAs = Encoding.UTF8; if (this.IsScript && encodeAs == null)
break; {
case "Unicode": switch (Properties.Settings.Default.OutputFormat)
encodeAs = Encoding.Unicode; {
break; case "UTF8":
case "BigEndianUnicode": encodeAs = Encoding.UTF8;
encodeAs = Encoding.BigEndianUnicode; break;
break; case "Unicode":
default: encodeAs = Encoding.Unicode;
encodeAs = Encoding.Default; break;
break; case "BigEndianUnicode":
} encodeAs = Encoding.BigEndianUnicode;
} else if (encodeAs == null) { break;
encodeAs = Encoding.UTF8; default:
} encodeAs = Encoding.Default;
break;
}
}
else if (encodeAs == null)
{
encodeAs = Encoding.UTF8;
}
this.numberedTextBoxUC1.TextBox.SaveCurrentFile(strPath, encodeAs); this.numberedTextBoxUC1.TextBox.SaveCurrentFile(strPath, encodeAs);
this.encodedAs = encodeAs; this.encodedAs = encodeAs;
}
} else if (LSLIPathHelper.IsExpandedLSL(strPath))
{
string LSLIfilePath = LSLIPathHelper.CreateCollapsedPathAndScriptName(strPath);
// Check if an LSLI version of this script exists
if (File.Exists(LSLIfilePath))
{
// Save the LSLI file as well
File.WriteAllText(LSLIfilePath, LSLIConverter.CollapseToLSLI(this.numberedTextBoxUC1.TextBox.Text));
EditForm form = null;
// If it's currently open, then refresh it
for (int i = 0; i < Application.OpenForms.Count; i++)
{
Form openForm = Application.OpenForms[i];
string filename = LSLIPathHelper.TrimStarsAndWhiteSpace(openForm.Text);
if (filename == Path.GetFileName(LSLIfilePath))
{
form = (EditForm)openForm;
}
}
if (form != null && form.Enabled)
{
parent.OpenFile(LSLIfilePath, Guid.NewGuid(), true);
form.Close();
}
}
this.numberedTextBoxUC1.TextBox.Dirty = false;
this.Text = LSLIPathHelper.GetExpandedTabName(strPath);
}
}
public void SaveCurrentFile() public void SaveCurrentFile()
{ {
@ -397,12 +454,26 @@ namespace LSLEditor
// for disposing // for disposing
this.components.Add(runtime); this.components.Add(runtime);
foreach (string message in verboseQueue)
{
runtime.VerboseConsole(message);
if (message.StartsWith("Error: "))
{
StopCompiler();
this.tabControl1.SelectedIndex = 0;
verboseQueue = new List<string>();
return false;
}
}
if (!runtime.Compile(this)) { if (!runtime.Compile(this)) {
this.tabControl1.SelectedIndex = 0; this.tabControl1.SelectedIndex = 0;
return false; return false;
} }
TabPage tabPage = new TabPage("Debug");
TabPage tabPage = new TabPage("Debug");
tabPage.Controls.Add(runtime); tabPage.Controls.Add(runtime);
this.tabControl1.TabPages.Add(tabPage); this.tabControl1.TabPages.Add(tabPage);
this.tabControl1.SelectedIndex = 1; this.tabControl1.SelectedIndex = 1;
@ -422,7 +493,7 @@ namespace LSLEditor
string lsl = SourceCode; string lsl = SourceCode;
// If it is LSLI, it needs to import scripts first, before it recognizes imported functions // If it is LSLI, it needs to import scripts first, before it recognizes imported functions
if (LSLIConverter.IsLSLI(this.FullPathName)) if (LSLIPathHelper.IsLSLI(this.FullPathName))
{ {
LSLIConverter converter = new LSLIConverter(); LSLIConverter converter = new LSLIConverter();
lsl = converter.ExpandToLSL(this); lsl = converter.ExpandToLSL(this);

View file

@ -42,6 +42,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace LSLEditor.Helpers namespace LSLEditor.Helpers
{ {
@ -50,120 +51,24 @@ namespace LSLEditor.Helpers
private EditForm editForm; private EditForm editForm;
private const string BEGIN = "//@BEGIN"; private const string BEGIN = "//@BEGIN";
private const string END = "//@END"; private const string END = "//@END";
private static List<string> validExtensions = new List<string>() { "lsl", "lsli", ".lsl", ".lsli" }; private static List<string> validExtensions = new List<string>() { "lsl", "lsli", ".lsl", ".lsli" }; // TODO: Optimize dit...
public const string EXPANDED_SUBEXT = ".expanded"; public const string EXPANDED_SUBEXT = ".expanded";
public const string LSL_EXT = ".lsl"; public const string LSL_EXT = ".lsl";
public const string LSLI_EXT = ".lsli"; public const string LSLI_EXT = ".lsli";
// NEW INCLUDE REGEX MATCHES ONLY FIRST OCCURENCE OF LINE ( OLD: ([\n]|^)+//@include\\(\".*?\"\\)(\\s\\w)? ) private const string INCLUDE_REGEX = "(\n|^)//@include\\(\".*?\"\\)";
// LAST NEW: ([\n]|^)+//@include\\(\".*?\"\\)(\\s)? private const string BEGIN_REGEX = "(\n|^)" + BEGIN; //"(\n|^)//@BEGIN"
private const string INCLUDE_REGEX = "(\n|^)//@include\\(\".*?\"\\)"; // EVEN MORE SIMPLIFIED private const string END_REGEX = "(\n|^)" + END;
private const string BEGIN_REGEX = "(\n|^)//@BEGIN"; // OLD: (\n|^)+//@BEGIN(\\s)*(\r|$)
private const string END_REGEX = "(\n|^)//@END"; //OLD: ([\n]|^)+//@END(\\s)*(\r|$)
private List<string> implementedIncludes = new List<string>(); private List<string> implementedIncludes = new List<string>();
private int includeDepth = 0;
public LSLIConverter() public LSLIConverter()
{ {
} }
/// <summary>
/// Checks if a filename is LSLI
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool IsLSLI(string fileName)
{
return GetExtension(fileName) == LSLI_EXT;
}
/// <summary>
/// Creates a new path and name from the original path and name based on the editForm.
/// E.g. turns path/to/file.lsli into path/to/file.expanded.lsl
/// </summary>
/// <returns></returns>
public string CreateExpandedPathAndScriptName()
{
return RemoveExtension(editForm.FullPathName) + EXPANDED_SUBEXT + LSL_EXT;
}
/// <summary>
/// Creates an expanded scriptname out of the current scriptname.
/// </summary>
/// <returns></returns>
public string CreateExpandedScriptName(string filename = null)
{
string nameExpanded = "";
if (filename != null)
{
nameExpanded = RemoveExtension(filename) + EXPANDED_SUBEXT + LSL_EXT;
} else
{
nameExpanded = RemoveExtension(editForm.Text) + EXPANDED_SUBEXT + LSL_EXT;
}
nameExpanded = nameExpanded.IndexOf('*') > -1 ? nameExpanded.Remove(nameExpanded.IndexOf('*'), 1) : nameExpanded;
return nameExpanded;
}
/// <summary>
/// Creates a new path and name from the original path and name based on the editForm.
/// E.g. turns path/to/file.expanded.lsl into path/to/file.lsli
/// </summary>
/// <returns></returns>
public string CreateCollapsedPathAndScriptName()
{
return RemoveExtension(editForm.FullPathName) + LSLI_EXT;
}
/// <summary>
/// Creates a LSLI scriptname out of the current scriptname.
/// </summary>
/// <returns></returns>
public string CreateCollapsedScriptName()
{
string nameExpanded = RemoveExtension(editForm.Text) + LSLI_EXT;
nameExpanded = nameExpanded.IndexOf('*') > -1 ? nameExpanded.Remove(nameExpanded.IndexOf('*'), 1) : nameExpanded;
return nameExpanded;
}
/// <summary>
/// Gets the extension from a string, includes '.'
/// Only returns the last extension
/// Returns empty string if the extension cannot be found
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private static string GetExtension(string filename)
{
int lastIndexOfDirectorySeperator = -1;
// If '.' after last index of \\ or /
if (filename.Contains('/') || filename.Contains('\\'))
{
lastIndexOfDirectorySeperator = filename.LastIndexOf('/') > filename.LastIndexOf('\\') ? filename.LastIndexOf('/') : filename.LastIndexOf('\\');
}
if(lastIndexOfDirectorySeperator != -1 && filename.Contains('.') && lastIndexOfDirectorySeperator < filename.LastIndexOf('.'))
{
return filename.Substring(filename.LastIndexOf('.')).TrimEnd(' ');
}
return "";
}
/// <summary>
/// Removes the extension and possible expanded subextension from a filename
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private static string RemoveExtension(string filename)
{
filename = filename.Contains(EXPANDED_SUBEXT) ? filename.Replace(EXPANDED_SUBEXT, "") : filename;
return filename.Remove(filename.LastIndexOf(GetExtension(filename)));
}
/// <summary> /// <summary>
/// Searches for a file with one of the validExtensions based on a name or path. /// Searches for a file with one of the validExtensions based on a name or path.
/// </summary> /// </summary>
@ -178,7 +83,7 @@ namespace LSLEditor.Helpers
return file; return file;
} }
if (GetExtension(file) == "") if (Path.GetExtension(file) == "")
{ {
List<string> extensions = validExtensions.Where(e => e[0] == '.').ToList(); List<string> extensions = validExtensions.Where(e => e[0] == '.').ToList();
string pFile = ""; string pFile = "";
@ -227,6 +132,21 @@ namespace LSLEditor.Helpers
return relativePath; return relativePath;
} }
/// <summary>
/// This is a hack to get the correct line, since problems arose in WriteAfterLine when inserting index-based
/// </summary>
/// <param name="lineBefore"></param>
/// <returns></returns>
private int GetCorrectIndexOfLine(string lineBefore, string context) // TODO
{
//int correctIndex = -1;
//if(lineBefore.Trim('\n') == BEGIN)
//{
// correctIndex = context.Where(l => l).LastIndexOf(lineBefore);
//}
return context.LastIndexOf(lineBefore);
}
/// <summary> /// <summary>
/// Creates a new line in the context after another line /// Creates a new line in the context after another line
/// </summary> /// </summary>
@ -234,10 +154,11 @@ namespace LSLEditor.Helpers
/// <param name="newLine"></param> /// <param name="newLine"></param>
/// <param name="lineBefore"></param> /// <param name="lineBefore"></param>
/// <returns>Context with the new line</returns> /// <returns>Context with the new line</returns>
private StringBuilder WriteAfterLine(StringBuilder context, string newLine, string lineBefore) private StringBuilder WriteAfterLine(StringBuilder context, string newLine, string lineBefore) // TODO: HIJ MOET KIJKEN NAAR DE INDEX VAN LINEBEFORE, NIET ZELF DE INDEX OPZOEKEN
{ {
string ctx = context.ToString(); string ctx = context.ToString();
int lastIndexOfLineBefore = ctx.LastIndexOf(lineBefore); //int lastIndexOfLineBefore = ctx.LastIndexOf(lineBefore);
int lastIndexOfLineBefore = GetCorrectIndexOfLine(lineBefore, ctx);
int includeIndex = lastIndexOfLineBefore + lineBefore.Length; int includeIndex = lastIndexOfLineBefore + lineBefore.Length;
string hasSeperator = lineBefore.Substring(lineBefore.Length - 1, 1); string hasSeperator = lineBefore.Substring(lineBefore.Length - 1, 1);
@ -253,36 +174,75 @@ namespace LSLEditor.Helpers
} }
context.Insert(includeIndex, newLine); context.Insert(includeIndex, newLine);
string test = context.ToString(); string test = context.ToString(); // Debug only
return context; return context;
} }
/// <summary>
/// Creates a new line in the context after a specified index
/// </summary>
/// <param name="context"></param>
/// <param name="newLine"></param>
/// <param name="indexOfNewLine"></param>
/// <returns>Context with the new line</returns>
//private StringBuilder WriteAfterLine(StringBuilder context, string newLine, int indexOfNewLine) // DEZE IS BETER, MAAR IN PRAKTIJK WERKT NIET...
//{
// string ctx = context.ToString();
// int includeIndex = indexOfNewLine;
// string hasSeperator = context.ToString().Substring(indexOfNewLine - 1, 1);
// if (hasSeperator != "\n")
// {
// newLine = "\n" + newLine;
// }
// hasSeperator = newLine.Substring(newLine.Length - 1, 1);
// if (hasSeperator != "\n")
// {
// newLine += "\n";
// }
// context.Insert(includeIndex, newLine);
// string test = context.ToString();
// return context;
//}
/// <summary> /// <summary>
/// Imports scripts from //@include statements /// Imports scripts from //@include statements
/// </summary> /// </summary>
/// <param name="strC">Sourcecode</param> /// <param name="strC">Sourcecode</param>
/// <param name="pathOfScript">Path of the source code of the script</param> /// <param name="pathOfScript">Path of the source code of the script</param>
/// <returns>Sourcecode with imported scripts</returns> /// <returns>Sourcecode with imported scripts</returns>
private string ImportScripts(string strC, string pathOfScript) private string ImportScripts(string strC, string pathOfScript) // TODO: Lange functie, kan ik deze opsplitten?
{ {
if(GetExtension(pathOfScript).ToLower() != LSLI_EXT) if(!LSLIPathHelper.IsLSLI(pathOfScript))
{ {
// If it's not LSLI extension it can't import a script // If it's not LSLI extension it can't import a script
return strC; return strC;
} }
StringBuilder sb = new StringBuilder(strC); StringBuilder sb = new StringBuilder(strC);
MatchCollection mIncludes = Regex.Matches(strC, INCLUDE_REGEX); // Find includes MatchCollection mIncludes = Regex.Matches(strC, INCLUDE_REGEX); // Find includes
foreach (Match m in mIncludes) foreach (Match m in mIncludes)
{ {
if (this.includeDepth == 0)
{
this.implementedIncludes = new List<string>();
}
includeDepth++;
string contentAfterMatchValue = strC.Substring(m.Index + m.Value.Length); string contentAfterMatchValue = strC.Substring(m.Index + m.Value.Length);
int indexOfNewLine = contentAfterMatchValue.IndexOf('\n') + m.Index + m.Value.Length + 1; // Index of the first occurence of \n after this match int indexOfNewLine = contentAfterMatchValue.IndexOf('\n') + m.Index + m.Value.Length + 1; // Index of the first occurence of \n after this match
string line = strC.Substring(m.Index, indexOfNewLine - m.Index); // Get full line string line = strC.Substring(m.Index, indexOfNewLine - m.Index); // Get full line
string pathOfInclude = Regex.Match(line, "\".*?\"").Value.Trim('"'); int lineNumber = strC.Take(indexOfNewLine - 1).Count(c => c == '\n') + 1;
string ext = GetExtension(pathOfInclude).ToLower();
if ((validExtensions.Contains(ext) || ext == "") && !this.CreateExpandedScriptName(pathOfScript).Contains(pathOfInclude) string pathOfIncludeOriginal = Regex.Match(line, "\".*?\"").Value.Trim('"');
string pathOfInclude = pathOfIncludeOriginal;
string ext = Path.GetExtension(pathOfInclude).ToLower();
if ((validExtensions.Contains(ext) || ext == "") && !LSLIPathHelper.CreateExpandedScriptName(pathOfScript).Contains(pathOfInclude)
&& !pathOfScript.Contains(pathOfInclude)) && !pathOfScript.Contains(pathOfInclude))
{ {
// If path is relative // If path is relative
@ -292,6 +252,7 @@ namespace LSLEditor.Helpers
} }
pathOfInclude = SearchFile(pathOfInclude); pathOfInclude = SearchFile(pathOfInclude);
if (pathOfInclude != "" && !this.implementedIncludes.Contains(Path.GetFullPath(pathOfInclude))) if (pathOfInclude != "" && !this.implementedIncludes.Contains(Path.GetFullPath(pathOfInclude)))
{ {
sb = this.WriteAfterLine(sb, BEGIN, line); sb = this.WriteAfterLine(sb, BEGIN, line);
@ -302,6 +263,7 @@ namespace LSLEditor.Helpers
{ {
this.implementedIncludes.Add(Path.GetFullPath(pathOfInclude)); this.implementedIncludes.Add(Path.GetFullPath(pathOfInclude));
string scriptRaw = sr.ReadToEnd(); string scriptRaw = sr.ReadToEnd();
scriptRaw = scriptRaw.Replace("\n", "\n\t");
// If there are includes in the included script // If there are includes in the included script
if (Regex.IsMatch(scriptRaw, INCLUDE_REGEX)) if (Regex.IsMatch(scriptRaw, INCLUDE_REGEX))
@ -318,6 +280,34 @@ namespace LSLEditor.Helpers
this.WriteAfterLine(sb, END, script); this.WriteAfterLine(sb, END, script);
} }
else if (pathOfInclude != "" && this.implementedIncludes.Contains(Path.GetFullPath(pathOfInclude)))
{
string message = "Error: Recursive include loop detected: \"" + Path.GetFullPath(pathOfInclude) +
"\". In script \""
+ Path.GetFileName(pathOfScript) + "\". Line " + lineNumber + ".";
if (!editForm.verboseQueue.Contains(message))
{
MessageBox.Show(message, "Oops...", MessageBoxButtons.OK, MessageBoxIcon.Error);
editForm.verboseQueue.Add(message);
}
} else
{
string correctPath = Path.GetFullPath(GetRelativePath(pathOfScript, Environment.CurrentDirectory) + pathOfIncludeOriginal);
string message = "Error: Unable to find file \"" + correctPath +
"\". In script \"" + Path.GetFileName(pathOfScript) + "\". Line " + lineNumber + ".";
if (!editForm.verboseQueue.Contains(message))
{
MessageBox.Show(message, "Oops...", MessageBoxButtons.OK, MessageBoxIcon.Error);
editForm.verboseQueue.Add(message);
}
}
}
includeDepth--;
if(this.implementedIncludes.Count > 0)
{
this.implementedIncludes.Remove(this.implementedIncludes.Last());
} }
} }
@ -329,16 +319,15 @@ namespace LSLEditor.Helpers
/// </summary> /// </summary>
/// <param name="strC">Sourcecode</param> /// <param name="strC">Sourcecode</param>
/// <returns>Sourcecode without imported scripts</returns> /// <returns>Sourcecode without imported scripts</returns>
private string RemoveScripts(string strC) // TODO: DIT VALT MISS NOG TE OPTIMALISEREN MET STRINGBUILDER IPV STRING private static string RemoveScripts(string strC)
{ {
//StringBuilder sb = new StringBuilder(strC); StringBuilder sb = new StringBuilder(strC);
string result = strC;
int indexOfFirstBeginStatement = -1; int indexOfFirstBeginStatement = -1;
uint depth = 0; uint depth = 0;
int readIndex = 0; int readIndex = 0;
using(StringReader sr = new StringReader(strC)) using (StringReader sr = new StringReader(strC))
{ {
int amountOfLines = strC.Split('\n').Length; int amountOfLines = strC.Split('\n').Length;
for (int i = 1; i < amountOfLines; i++) for (int i = 1; i < amountOfLines; i++)
@ -347,7 +336,7 @@ namespace LSLEditor.Helpers
if (Regex.IsMatch(line, BEGIN_REGEX)) if (Regex.IsMatch(line, BEGIN_REGEX))
{ {
if(depth == 0) if (depth == 0)
{ {
indexOfFirstBeginStatement = readIndex; indexOfFirstBeginStatement = readIndex;
} }
@ -362,7 +351,7 @@ namespace LSLEditor.Helpers
if (depth == 0) if (depth == 0)
{ {
result = result.Remove(indexOfFirstBeginStatement, (readIndex - indexOfFirstBeginStatement)); sb.Remove(indexOfFirstBeginStatement, (readIndex - indexOfFirstBeginStatement));
readIndex -= readIndex - indexOfFirstBeginStatement; readIndex -= readIndex - indexOfFirstBeginStatement;
indexOfFirstBeginStatement = -1; indexOfFirstBeginStatement = -1;
} }
@ -370,7 +359,7 @@ namespace LSLEditor.Helpers
} }
} }
return result; return sb.ToString();
} }
/// <summary> /// <summary>
@ -378,11 +367,37 @@ namespace LSLEditor.Helpers
/// </summary> /// </summary>
/// <param name="editform"></param> /// <param name="editform"></param>
/// <returns>LSLI</returns> /// <returns>LSLI</returns>
public string CollapseToLSLI(EditForm editform) public static string CollapseToLSLI(string source)
{
string sourceCode = RemoveScripts(source);
return sourceCode;
}
/// <summary>
/// Call this to collapse LSL to LSLI
/// </summary>
/// <param name="editform"></param>
/// <returns>LSLI</returns>
public string CollapseToLSLIFromEditform(EditForm editform)
{ {
this.editForm = editform; this.editForm = editform;
string sourceCode = RemoveScripts(editForm.SourceCode); return CollapseToLSLI(editform.SourceCode);
return sourceCode; }
/// <summary>
/// Call this to collapse LSL to LSLI
/// </summary>
/// <param name="editform"></param>
/// <returns>LSLI</returns>
public static string CollapseToLSLIFromPath(string path)
{
string sourceCode = "";
using(StreamReader sr = new StreamReader(path))
{
sourceCode = sr.ReadToEnd();
}
return CollapseToLSLI(sourceCode);
} }
/// <summary> /// <summary>

View file

@ -0,0 +1,192 @@
// <copyright file="gpl-2.0.txt">
// ORIGINAL CODE BASE IS Copyright (C) 2006-2010 by Alphons van der Heijden.
// The code was donated on 2010-04-28 by Alphons van der Heijden to Brandon 'Dimentox Travanti' Husbands &
// Malcolm J. Kudra, who in turn License under the GPLv2 in agreement with Alphons van der Heijden's wishes.
//
// The community would like to thank Alphons for all of his hard work, blood sweat and tears. Without his work
// the community would be stuck with crappy editors.
//
// The source code in this file ("Source Code") is provided by The LSLEditor Group to you under the terms of the GNU
// General Public License, version 2.0 ("GPL"), unless you have obtained a separate licensing agreement ("Other
// License"), formally executed by you and The LSLEditor Group.
// Terms of the GPL can be found in the gplv2.txt document.
//
// GPLv2 Header
// ************
// LSLEditor, a External editor for the LSL Language.
// Copyright (C) 2010 The LSLEditor Group.
//
// This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
// Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// ********************************************************************************************************************
// The above copyright notice and this permission notice shall be included in copies or substantial portions of the
// Software.
// ********************************************************************************************************************
// </copyright>
//
// <summary>
// This class is used to help with paths and LSLI files.
// </summary>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace LSLEditor.Helpers
{
static class LSLIPathHelper
{
/// <summary>
/// Checks if a filename is LSLI
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static bool IsLSLI(string filename)
{
filename = TrimStarsAndWhiteSpace(filename);
return Path.GetExtension(filename).ToLower() == LSLIConverter.LSLI_EXT;
}
/// <summary>
/// Checks if a filename is an expanded LSL file
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static bool IsExpandedLSL(string filename)
{
filename = TrimStarsAndWhiteSpace(filename);
return filename.EndsWith(LSLIConverter.EXPANDED_SUBEXT + LSLIConverter.LSL_EXT);
}
/// <summary>
/// Creates a LSLI scriptname from a filename.
/// </summary>
/// <returns></returns>
public static string CreateCollapsedScriptName(string filename)
{
string nameCollapsed = RemoveDotInFrontOfFilename(Path.GetFileNameWithoutExtension(RemoveExpandedSubExtension(filename)) + LSLIConverter.LSLI_EXT);
return nameCollapsed;
}
private static string RemoveExtension(string filename)
{
//filename = filename.Contains(LSLIConverter.EXPANDED_SUBEXT) ? filename.Replace(LSLIConverter.EXPANDED_SUBEXT, "") : filename; // Is nu een aparte functie voor
filename = TrimStarsAndWhiteSpace(filename.Remove(filename.LastIndexOf(Path.GetExtension(filename))));
return filename;
}
public static string RemoveExpandedSubExtension(string filename)
{
if (filename.Contains(LSLIConverter.EXPANDED_SUBEXT))
{
return filename.Replace(LSLIConverter.EXPANDED_SUBEXT, "");
}
return filename;
}
/// <summary>
/// Creates a new path and name from the given filename.
/// E.g. turns path/to/file.expanded.lsl into path/to/file.lsli
/// </summary>
/// <returns></returns>
public static string CreateCollapsedPathAndScriptName(string filename)
{
return RemoveDotInFrontOfFilename(RemoveExtension(RemoveExpandedSubExtension(filename)) + LSLIConverter.LSLI_EXT);
}
/// <summary>
/// Creates a new path and name from the original path and name based on the editForm.
/// E.g. turns path/to/file.lsli into path/to/.file.expanded.lsl
/// </summary>
/// <returns></returns>
public static string CreateExpandedPathAndScriptName(string path)
{
return PutDotInFrontOfFilename(RemoveExtension(path) + LSLIConverter.EXPANDED_SUBEXT + LSLIConverter.LSL_EXT);
}
/// <summary>
/// Creates an expanded scriptname out of the given filename.
/// </summary>
/// <returns></returns>
public static string CreateExpandedScriptName(string filename)
{
string nameExpanded = "";
if (filename != null)
{
nameExpanded = Path.GetFileNameWithoutExtension(filename) + LSLIConverter.EXPANDED_SUBEXT + LSLIConverter.LSL_EXT;
}
return PutDotInFrontOfFilename(TrimStarsAndWhiteSpace(nameExpanded));
}
private static string PutDotInFrontOfFilename(string filename)
{
int afterLastIndexOfSeperator = (filename.LastIndexOf('\\') > filename.LastIndexOf('/') ? filename.LastIndexOf('\\') : filename.LastIndexOf('/')) + 1;
if (filename.Substring(afterLastIndexOfSeperator, 1) == ".")
{
return filename;
}
filename = filename.Insert(afterLastIndexOfSeperator, ".");
return filename;
}
private static string RemoveDotInFrontOfFilename(string filename)
{
int afterLastIndexOfSeperator = (filename.LastIndexOf('\\') > filename.LastIndexOf('/') ? filename.LastIndexOf('\\') : filename.LastIndexOf('/')) + 1;
if (filename.Substring(afterLastIndexOfSeperator, 1) != ".")
{
return filename;
}
filename = filename.Remove(afterLastIndexOfSeperator, 1);
return filename;
}
/// <summary>
/// "Hides" the file in the folder
/// </summary>
/// <param name="path"></param>
public static void HideFile(string path)
{
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
}
/// <summary>
/// First checks if the file exists, then deletes it
/// </summary>
/// <param name="path"></param>
public static void DeleteFile(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
public static string TrimStarsAndWhiteSpace(string str)
{
return str.Trim(' ').TrimEnd('*');
}
public static string GetExpandedTabName(string path)
{
if (path == null) return "";
return RemoveDotInFrontOfFilename(Path.GetFileNameWithoutExtension(RemoveExpandedSubExtension(path)) + LSLIConverter.LSLI_EXT + " (Expanded LSL)");
}
}
}

View file

@ -146,6 +146,7 @@ namespace LSLEditor
this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog(); this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
this.openSolutionFilesDialog = new System.Windows.Forms.OpenFileDialog(); this.openSolutionFilesDialog = new System.Windows.Forms.OpenFileDialog();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.viewLSLIToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.dockPanel = new LSLEditor.Docking.DockPanel(); this.dockPanel = new LSLEditor.Docking.DockPanel();
this.menuStrip1.SuspendLayout(); this.menuStrip1.SuspendLayout();
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
@ -468,7 +469,8 @@ namespace LSLEditor
this.advancedToolStripMenuItem, this.advancedToolStripMenuItem,
this.toolStripSeparator10, this.toolStripSeparator10,
this.CollapseToLSLIToolStripMenuItem, this.CollapseToLSLIToolStripMenuItem,
this.expandToLSLToolStripMenuItem}); this.expandToLSLToolStripMenuItem,
this.viewLSLIToolStripMenuItem});
this.editStripMenuItem.Name = "editStripMenuItem"; this.editStripMenuItem.Name = "editStripMenuItem";
this.editStripMenuItem.Size = new System.Drawing.Size(39, 20); this.editStripMenuItem.Size = new System.Drawing.Size(39, 20);
this.editStripMenuItem.Text = "Edit"; this.editStripMenuItem.Text = "Edit";
@ -939,6 +941,14 @@ namespace LSLEditor
// //
this.openSolutionFilesDialog.FileName = "openFileDialog2"; this.openSolutionFilesDialog.FileName = "openFileDialog2";
// //
// viewLSLIToolStripMenuItem
//
this.viewLSLIToolStripMenuItem.Name = "viewLSLIToolStripMenuItem";
this.viewLSLIToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F12;
this.viewLSLIToolStripMenuItem.Size = new System.Drawing.Size(225, 22);
this.viewLSLIToolStripMenuItem.Text = "View LSLI";
this.viewLSLIToolStripMenuItem.Click += new System.EventHandler(this.viewLSLIToolStripMenuItem_Click);
//
// dockPanel // dockPanel
// //
this.dockPanel.ActiveAutoHideContent = null; this.dockPanel.ActiveAutoHideContent = null;
@ -1126,5 +1136,6 @@ namespace LSLEditor
private System.Windows.Forms.ToolStripSeparator toolStripSeparator10; private System.Windows.Forms.ToolStripSeparator toolStripSeparator10;
private System.Windows.Forms.ToolStripMenuItem CollapseToLSLIToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem CollapseToLSLIToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem expandToLSLToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem expandToLSLToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem viewLSLIToolStripMenuItem;
} }
} }

View file

@ -82,8 +82,7 @@ namespace LSLEditor
private Browser browser; private Browser browser;
private SimulatorConsole SimulatorConsole; private SimulatorConsole SimulatorConsole;
public bool CancelClosing = false; public bool CancelClosing = false;
public Solution.SolutionExplorer m_SolutionExplorer; public Solution.SolutionExplorer m_SolutionExplorer;
@ -417,6 +416,11 @@ namespace LSLEditor
UpdateRecentFileList(strPath); UpdateRecentFileList(strPath);
if(Helpers.LSLIPathHelper.IsExpandedLSL(editForm.Text))
{
editForm.Text = Helpers.LSLIPathHelper.GetExpandedTabName(editForm.Text);
}
return editForm; return editForm;
} }
@ -1146,6 +1150,7 @@ namespace LSLEditor
this.SimulatorConsole = new SimulatorConsole(this.SolutionExplorer, this.Children); this.SimulatorConsole = new SimulatorConsole(this.SolutionExplorer, this.Children);
this.SimulatorConsole.Show(dockPanel); this.SimulatorConsole.Show(dockPanel);
//TODO: Show Simulator Console somewhere //TODO: Show Simulator Console somewhere
//this.panel1.Controls.Clear(); //this.panel1.Controls.Clear();
//this.panel1.Controls.Add(this.SimulatorConsole); //this.panel1.Controls.Add(this.SimulatorConsole);
@ -1800,35 +1805,54 @@ namespace LSLEditor
browser.ShowWebBrowser("LSLEditor QA", Properties.Settings.Default.qasite); browser.ShowWebBrowser("LSLEditor QA", Properties.Settings.Default.qasite);
} }
/// <summary>
/// Gets a form based on it's form.Text property.
/// </summary>
/// <param name="formName"></param>
/// <returns>Returns null if not found</returns>
private Form GetForm(string formName)
{
EditForm desirableForm = null;
for (int i = 0; i < Children.Length; i++) //Application.OpenForms
{
Form form = Children[i];
if (form.Text.TrimEnd(' ') == formName)
{
desirableForm = (EditForm)form;
}
}
return desirableForm;
}
private void expandToLSLToolStripMenuItem_Click(object sender, EventArgs e) private void expandToLSLToolStripMenuItem_Click(object sender, EventArgs e)
{ {
EditForm editForm = this.ActiveMdiForm as EditForm; EditForm editForm = this.ActiveMdiForm as EditForm;
if (editForm != null && editForm.FullPathName.IndexOf(Helpers.LSLIConverter.LSLI_EXT) > -1)
if (editForm != null && Helpers.LSLIPathHelper.IsLSLI(editForm.Text))
{ {
Helpers.LSLIConverter converter = new Helpers.LSLIConverter(); Helpers.LSLIConverter converter = new Helpers.LSLIConverter();
string lsl = converter.ExpandToLSL(editForm); string lsl = converter.ExpandToLSL(editForm);
string file = converter.CreateExpandedPathAndScriptName(); string file = Helpers.LSLIPathHelper.CreateExpandedPathAndScriptName(editForm.FullPathName);
editForm.Close();
Helpers.LSLIPathHelper.DeleteFile(file);
using (StreamWriter sw = new StreamWriter(file)) using (StreamWriter sw = new StreamWriter(file))
{ {
sw.Write(lsl); sw.Write(lsl);
} }
EditForm expandedForm = null; Helpers.LSLIPathHelper.HideFile(file);
for (int i = 0; i < Application.OpenForms.Count; i++)
{ EditForm expandedForm = (EditForm)GetForm(Helpers.LSLIPathHelper.CreateExpandedScriptName(Path.GetFileName(file)));
Form form = Application.OpenForms[i];
if (form.Text.TrimEnd(' ') == converter.CreateExpandedScriptName())
{
expandedForm = (EditForm)form;
}
}
if (expandedForm != null) if (expandedForm != null)
{ {
expandedForm.Close(); expandedForm.Close();
} }
editForm.Close();
OpenFile(file); OpenFile(file);
} }
} }
@ -1836,36 +1860,64 @@ namespace LSLEditor
private void CollapseToLSLIToolStripMenuItem_Click(object sender, EventArgs e) private void CollapseToLSLIToolStripMenuItem_Click(object sender, EventArgs e)
{ {
EditForm editForm = this.ActiveMdiForm as EditForm; EditForm editForm = this.ActiveMdiForm as EditForm;
// NOTE: This checks only if there's an LSL file, not if there's a extended subextension if (editForm != null && Helpers.LSLIPathHelper.IsExpandedLSL(editForm.ScriptName))
if (editForm != null && editForm.FullPathName.IndexOf(Helpers.LSLIConverter.LSL_EXT) > -1)
{ {
Helpers.LSLIConverter converter = new Helpers.LSLIConverter(); Helpers.LSLIConverter converter = new Helpers.LSLIConverter();
string lsli = converter.CollapseToLSLI(editForm);
string file = converter.CreateCollapsedPathAndScriptName(); Helpers.LSLIPathHelper.DeleteFile(editForm.FullPathName);
string lsli = converter.CollapseToLSLIFromEditform(editForm);
string file = Helpers.LSLIPathHelper.CreateCollapsedPathAndScriptName(editForm.FullPathName);
editForm.Close();
using (StreamWriter sw = new StreamWriter(file)) using (StreamWriter sw = new StreamWriter(file))
{ {
sw.Write(lsli); sw.Write(lsli);
} }
EditForm collapsedForm = null; Form collapsedForm = GetForm(Helpers.LSLIPathHelper.CreateCollapsedScriptName(Path.GetFileName(file)));
for (int i = 0; i < Application.OpenForms.Count; i++)
{
Form form = Application.OpenForms[i];
if (form.Text.TrimEnd(' ') == converter.CreateCollapsedScriptName())
{
collapsedForm = (EditForm)form;
}
}
if (collapsedForm != null) if (collapsedForm != null)
{ {
collapsedForm.Close(); collapsedForm.Close();
} }
editForm.Close();
OpenFile(file); OpenFile(file);
} }
} }
private void viewLSLIToolStripMenuItem_Click(object sender, EventArgs e)
{
EditForm editForm = this.ActiveMdiForm as EditForm;
if(editForm != null)
{
if (Helpers.LSLIPathHelper.IsLSLI(editForm.Text))
{
return;
}
string pathOfLSLI = Helpers.LSLIPathHelper.CreateCollapsedPathAndScriptName(editForm.FullPathName);
if (File.Exists(pathOfLSLI)) {
Form LSLIform = GetForm(Path.GetFileName(pathOfLSLI));
if (LSLIform != null)
{
LSLIform.Close();
}
OpenFile(pathOfLSLI);
GetForm(Path.GetFileName(pathOfLSLI)).Enabled = false;
}
else
{
MessageBox.Show("Error: No related LSLI file found. \n \"" + pathOfLSLI + "\"", "Oops...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
} }
} }

View file

@ -70,7 +70,7 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// //
[assembly: AssemblyVersion("2.55.0.378")] [assembly: AssemblyVersion("2.55.0.549")]
// //
// In order to sign your assembly you must specify a key to use. Refer to the // In order to sign your assembly you must specify a key to use. Refer to the
@ -100,4 +100,4 @@ using System.Runtime.InteropServices;
[assembly: AssemblyDelaySign(false)] [assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyName("")] //[assembly: AssemblyKeyName("")]
[assembly: ComVisibleAttribute(false)] [assembly: ComVisibleAttribute(false)]
[assembly: AssemblyFileVersionAttribute("2.55.0.378")] [assembly: AssemblyFileVersionAttribute("2.55.0.549")]

View file

@ -115,31 +115,17 @@ namespace LSLEditor
{ {
LSLIConverter lsliConverter = new LSLIConverter(); LSLIConverter lsliConverter = new LSLIConverter();
lsl = lsliConverter.ExpandToLSL(editForm); lsl = lsliConverter.ExpandToLSL(editForm);
string nameExpanded = lsliConverter.CreateExpandedScriptName(); string nameExpanded = LSLIPathHelper.CreateExpandedScriptName(editForm.FullPathName);
string path = lsliConverter.CreateExpandedPathAndScriptName(); string path = LSLIPathHelper.CreateExpandedPathAndScriptName(editForm.FullPathName);
LSLIPathHelper.DeleteFile(path);
using (StreamWriter sw = new StreamWriter(path)) using (StreamWriter sw = new StreamWriter(path))
{ {
sw.Write(lsl); sw.Write(lsl);
} }
// NOTE: DE EXPANDED LSL WORDT NU IN DE ACHTERGROND GERUNT EN NIET MEER LATEN ZIEN OP 2 TABS LSLIPathHelper.HideFile(path);
//EditForm expandedForm = null;
//for (int i = 0; i < Application.OpenForms.Count; i++)
//{
// Form form = Application.OpenForms[i];
// if(form.Text.TrimEnd(' ') == nameExpanded)
// {
// expandedForm = (EditForm)form;
// }
//}
//// Open the expanded file if not already open
//if(expandedForm == null)
//{
// mainForm.OpenFile(path, Guid.NewGuid(), true); // TODO: MOET AUTOMATISCH GAAN RUNNEN
//}
} }
CSharpCode = MakeSharp(editForm.ConfLSL, lsl); CSharpCode = MakeSharp(editForm.ConfLSL, lsl);

View file

@ -336,6 +336,7 @@
<Compile Include="Helpers\OutlineHelper.cs"> <Compile Include="Helpers\OutlineHelper.cs">
<ExcludeFromStyleCop>true</ExcludeFromStyleCop> <ExcludeFromStyleCop>true</ExcludeFromStyleCop>
</Compile> </Compile>
<Compile Include="Helpers\LSLIPathHelper.cs" />
<Compile Include="Helpers\SendMyKeys.cs"> <Compile Include="Helpers\SendMyKeys.cs">
<ExcludeFromStyleCop>true</ExcludeFromStyleCop> <ExcludeFromStyleCop>true</ExcludeFromStyleCop>
</Compile> </Compile>