////////////////////////////////////////////////////////////////////////// private void AddID(List <StringID> IdList, StringItem StrItem) { if (StrItem.ID == string.Empty) { return; } string StrID; int MaxNum; GetIDBase(StrItem.ID, out StrID, out MaxNum); if (StrID == string.Empty) { return; } StringID ID = new StringID(StrID, MaxNum); int Index = IdList.BinarySearch(ID); if (Index < 0) { IdList.Add(ID); IdList.Sort(); } else { IdList[Index].MaxNum = Math.Max(IdList[Index].MaxNum, MaxNum); } }
////////////////////////////////////////////////////////////////////////// private ListViewItem AddStringToList(StringItem StrItem, bool RespectOrder) { ListViewItem Item = new ListViewItem(StrItem.ID); Item.SubItems.Add(StrItem.Value); Item.Tag = StrItem; bool Found = false; if (RespectOrder) { foreach (ListViewItem It in ListStrings.Items) { StringItem Str = It.Tag as StringItem; if (Str != null && Str.Order > StrItem.Order) { ListStrings.Items.Insert(It.Index, Item); Found = true; break; } } } if (!Found) { ListStrings.Items.Add(Item); } return(Item); }
////////////////////////////////////////////////////////////////////////// private void OnIgnoreMenu(object sender, CancelEventArgs e) { IgnoreMenu.Items.Clear(); StringItem StrItem = null; if (ListIgnored.SelectedItems.Count > 0) { StrItem = ListIgnored.SelectedItems[0].Tag as StringItem; } if (StrItem != null) { IgnoreMenu.Items.Add("Unignore item", null, new EventHandler(OnUnignoreItem)); IgnoreMenu.Items.Add("-"); if (Mgr.IgnoreList.Contains(StrItem.Value)) { IgnoreMenu.Items.Add("Remove from ignore list", null, new EventHandler(OnRemoveFromIgnoreList)); } else { IgnoreMenu.Items.Add("Add to ignore list", null, new EventHandler(OnAddToIgnoreList)); } e.Cancel = false; } else { e.Cancel = true; } }
////////////////////////////////////////////////////////////////////////// private void OnUnignoreItem(object sender, EventArgs e) { StringItem StrItem = null; if (ListIgnored.SelectedItems.Count > 0) { ListViewItem Item = ListIgnored.SelectedItems[0]; StrItem = Item.Tag as StringItem; if (StrItem != null) { StrItem.Ignored = false; Mgr.ProjectStrings.Add(StrItem); Mgr.IgnoredStrings.Remove(StrItem); int OrigSelIndex = Item.Index; ListIgnored.Items.Remove(Item); int SelIndex = Math.Min(ListIgnored.Items.Count - 1, OrigSelIndex); if (SelIndex >= 0) { ListIgnored.Items[SelIndex].Selected = true; ListIgnored.EnsureVisible(SelIndex); } ListViewItem ListItem = AddStringToList(StrItem, true); SelIndex = ListItem.Index; if (SelIndex >= 0) { ListStrings.Items[SelIndex].Selected = true; ListStrings.EnsureVisible(SelIndex); } } } }
////////////////////////////////////////////////////////////////////////// private void IgnoreSingleItem(ListViewItem Item, bool AddToIgnoreList) { StringItem StrItem = Item.Tag as StringItem; if (StrItem == null) { return; } Mgr.ProjectStrings.Remove(StrItem); Mgr.IgnoredStrings.Add(StrItem); if (AddToIgnoreList) { if (!Mgr.IgnoreList.Contains(StrItem.Value)) { Mgr.IgnoreList.Add(StrItem.Value); } } StrItem.IgnoreReason = AddToIgnoreList ? IgnoreReason.InIgnoreList : IgnoreReason.SelectedByUser; StrItem.Ignored = true; ListStrings.Items.Remove(Item); AddStringToIgnoreList(StrItem); }
////////////////////////////////////////////////////////////////////////// private ListViewItem AddStringToIgnoreList(StringItem StrItem) { ListViewItem Item = new ListViewItem(StrItem.Value); Item.SubItems.Add(IgnoreReasonToString(StrItem.IgnoreReason)); Item.Tag = StrItem; ListIgnored.Items.Add(Item); return(Item); }
////////////////////////////////////////////////////////////////////////// private void OnSelectWithoutID(object sender, EventArgs e) { UpdateStatusLocked = true; ListStrings.BeginUpdate(); foreach (ListViewItem Item in ListStrings.Items) { StringItem StrItem = Item.Tag as StringItem; Item.Checked = StrItem != null && StrItem.ID == string.Empty; } ListStrings.EndUpdate(); UpdateStatusLocked = false; UpdateStringStatus(); }
////////////////////////////////////////////////////////////////////////// private void RefreshStringIDs() { ListStrings.BeginUpdate(); foreach (ListViewItem Item in ListStrings.Items) { StringItem StrItem = Item.Tag as StringItem; if (StrItem != null) { Item.Text = StrItem.ID; } } ListStrings.EndUpdate(); }
////////////////////////////////////////////////////////////////////////// public bool ScanProject() { // load project strings AddLog("Scanning project...", false); ProjectStrings = new List <StringItem>(); StringExtractor se = new StringExtractor(this); bool Ret = se.ExtractProjectStrings(this.ProjectFile, "*.script;*.inc", "*.scene;*.entity;*.actor;*.window;*.items;*.game"); if (!Ret) { return(false); } int Order = 0; foreach (StringLocation Str in se.Strings) { Order++; StringItem Item = new StringItem(); ExtractStringParts(Str.Value, out Item.ID, out Item.Value); Item.Line = Str.Line; Item.Filename = Str.Filename; Item.OriginalString = Str.Value; Item.IsScriptItem = (Str.Type == StringLocation.StringType.ScriptFile); Item.Order = Order; Item.Value = Item.Value.Replace("~n", "|"); Item.Value = Item.Value.Replace("~\"", "\""); ProjectStrings.Add(Item); } AddLog(ProjectStrings.Count.ToString() + " strings found"); // load string table string AddLog("Reading string table...", false); LoadStringTable(StringTableFile); AddLog(TableStrings.Count.ToString() + " strings found"); if (StringTableUsingUtf8) { AddLog("String table is using UTF-8 format"); } // perform initial string classification ClassifyStrings(); return(true); }
////////////////////////////////////////////////////////////////////////// private void OnStringSelected(object sender, EventArgs e) { ListView List = sender as ListView; if (List == null || List.SelectedItems.Count != 1) { return; } StringItem Item = List.SelectedItems[0].Tag as StringItem; if (Item != null) { DisplayContext(Item); } // handle multi selection if (List == ListStrings) { if (PrevSelectedIndex >= 0 && (ModifierKeys & Keys.Shift) == Keys.Shift) { int Start, End; if (PrevSelectedIndex < List.SelectedIndices[0]) { Start = PrevSelectedIndex; End = List.SelectedIndices[0]; } else { Start = List.SelectedIndices[0]; End = PrevSelectedIndex; } bool Select = (ModifierKeys & Keys.Control) != Keys.Control; UpdateStatusLocked = true; ListStrings.BeginUpdate(); for (int i = Start; i <= End; i++) { List.Items[i].Checked = Select; } ListStrings.EndUpdate(); UpdateStatusLocked = false; UpdateStringStatus(); } PrevSelectedIndex = List.SelectedIndices[0]; } }
////////////////////////////////////////////////////////////////////////// private bool LoadStringTable(string Filename) { TableStrings = new List <StringItem>(); StringTableUsingUtf8 = true; try { using (FileStream Stream = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { StreamReader Reader = new StreamReader(Stream, Encoding.Default, true); string Line; int LineNum = 0; while ((Line = Reader.ReadLine()) != null) { LineNum++; if (Line.StartsWith(";")) { continue; } string[] LineSplit = Line.Split(new char[] { '\t' }); if (LineSplit.Length != 2) { continue; } StringItem Item = new StringItem(); Item.ID = LineSplit[0]; Item.Value = LineSplit[1]; Item.Filename = Filename; Item.Line = LineNum; TableStrings.Add(Item); } StringTableUsingUtf8 = (Reader.CurrentEncoding == Encoding.UTF8); return(true); } } catch { return(false); } }
////////////////////////////////////////////////////////////////////////// private void DisplayContext(StringItem Item) { LblContext.Text = ""; TxtContext.Text = ""; if (Item == null) { return; } LblContext.Text = "Line " + Item.Line.ToString() + ", " + Item.Filename; if (Item.IsScriptItem) { try { string FileText = ""; using (StreamReader sr = new StreamReader(Item.Filename, Encoding.Default, true)) { string Line; while ((Line = sr.ReadLine()) != null) { FileText += Line + "\r\n"; } } TxtContext.Text = FileText; int SelStart = TxtContext.GetFirstCharIndexFromLine(Item.Line); if (SelStart >= 0) { TxtContext.Select(SelStart, 0); TxtContext.ScrollToCaret(); SelStart = TxtContext.GetFirstCharIndexFromLine(Item.Line - 1); if (SelStart >= 0) { TxtContext.Select(SelStart, TxtContext.Lines[Item.Line - 1].Length); } } } catch (Exception e) { TxtContext.Text = "Error loading file\n" + e.Message; } } }
////////////////////////////////////////////////////////////////////////// private void OnRemoveFromIgnoreList(object sender, EventArgs e) { StringItem StrItem = null; if (ListIgnored.SelectedItems.Count > 0) { ListViewItem Item = ListIgnored.SelectedItems[0]; StrItem = Item.Tag as StringItem; if (StrItem != null) { if (Mgr.IgnoreList.Contains(StrItem.Value)) { Mgr.IgnoreList.Remove(StrItem.Value); } StrItem.IgnoreReason = IgnoreReason.SelectedByUser; Item.SubItems[1].Text = IgnoreReasonToString(StrItem.IgnoreReason); } } }
////////////////////////////////////////////////////////////////////////// private bool IsWellKnownPattern(StringItem Item) { string ScriptLine = GetFileLine(Item.Filename, Item.Line); ScriptLine = ScriptLine.ToUpper(); ScriptLine = ScriptLine.Replace(" ", ""); ScriptLine = ScriptLine.Replace("\t", ""); foreach (string ScriptPattern in ScriptPatterns) { string Pattern = ScriptPattern.Replace("%string%", Item.OriginalString); Pattern = Pattern.ToUpper(); if (ScriptLine.IndexOf(Pattern) >= 0) { return(true); } } return(false); }
////////////////////////////////////////////////////////////////////////// public static int CompareByLine(StringItem p1, StringItem p2) { if (p1 == null) { if (p2 == null) { return(0); } else { return(-1); } } else { if (p2 == null) { return(1); } else { if (p1.Line < p2.Line) { return(-1); } else if (p1.Line > p2.Line) { return(1); } else { return(0); } } } }
////////////////////////////////////////////////////////////////////////// private ListViewItem AddStringToList(StringItem StrItem) { return(AddStringToList(StrItem, false)); }
////////////////////////////////////////////////////////////////////////// private void ClassifyStrings() { AddLog("Classifying strings...", false); IgnoredStrings = new List <StringItem>(); int CurrentIndex = 0; foreach (StringItem Item in ProjectStrings) { CurrentIndex++; ReportProgress(CurrentIndex, ProjectStrings.Count, Item.Value); Item.Ignored = false; // remove strings already present in the string table if (Item.ID != string.Empty && IsIDInTable(Item.ID)) { Item.Ignored = true; Item.IgnoreReason = IgnoreReason.AlreadyInTable; continue; } // keep strings with ID if (Item.ID != string.Empty) { continue; } // keep non-script strings if (!Item.IsScriptItem) { continue; } // remove strings in ignore list if (IsStringIgnored(Item.Value)) { Item.Ignored = true; Item.IgnoreReason = IgnoreReason.InIgnoreList; continue; } // remove filenames WmeProject Project = new WmeProject(this.ProjectFile); if (Project.ContainsFile(Item.Value)) { Item.Ignored = true; Item.IgnoreReason = IgnoreReason.IsFilename; continue; } // check one-word terms against well known code patterns if (Item.Value.IndexOf(' ') < 0 && IsWellKnownPattern(Item)) { Item.Ignored = true; Item.IgnoreReason = IgnoreReason.KnownCodePattern; continue; } } for (int i = 0; i < ProjectStrings.Count; i++) { StringItem Item = ProjectStrings[i]; if (Item.Ignored) { IgnoredStrings.Add(Item); ProjectStrings.RemoveAt(i); i--; } } ReportProgress(0, 0, ""); AddLog(IgnoredStrings.Count.ToString() + " strings automatically ignored"); }