예제 #1
0
        public static void ScanFile(string Filepath)
        {
            mForm.AddLog("scaning " + Filepath);

            List <string>     Lines        = new List <string>(File.ReadAllLines(Filepath));
            List <ParserLine> npcLines     = new List <ParserLine>();
            ScriptParserList  scriptParser = new ScriptParserList();
            bool   mMultiComment           = false;
            Match  m;
            string npcName  = "";
            int    codeOpen = 0;

            for (int i = 0; i < Lines.Count; i++)
            {
                string line = Lines[i].Trim();

                #region Comment removal
                // empty or comment
                if (line.Length == 0 || line.StartsWith("//") == true)
                {
                    continue;
                }

                // multiline continue
                if (mMultiComment == true)
                {
                    if (line.Contains("*/") == false)
                    {
                        continue;
                    }

                    // */ found, end it
                    mMultiComment = false;
                    if ((line = line.Substring(line.IndexOf("*/"))).Length == 0)
                    {
                        continue;
                    }
                }

                // start of multiline
                if (line.StartsWith("/*") == true)
                {
                    // only this line?
                    if (line.EndsWith("*/") == true)
                    {
                        continue;
                    }

                    // rly multiline
                    if (line.Contains("*/") == false)
                    {
                        mMultiComment = true;
                        continue;
                    }

                    // line contains */, get the substr
                    if ((line = line.Substring(line.IndexOf("*/") + 2)).Length == 0)
                    {
                        continue;
                    }
                }

                #endregion

                // okay, need to check for a new Script with Strings
                if (codeOpen == 0 && (m = Regex.Match(line, "^[^\t]+\t[^\t]+\t([^\t]+)\t[^{]*{", RegexOptions.IgnoreCase)).Success == true)
                {
                    // start of a new Script
                    codeOpen++;
                    npcName = m.Groups[1].Captures[0].Value;
                    if (npcName.IndexOf("::") != -1)
                    {
                        npcName = npcName.Substring(npcName.IndexOf("::") + 2);
                    }
                    npcLines.Clear();
                    continue;
                }
                else if (codeOpen > 0)
                {
                    // we are in a Script block
                    codeOpen += line.CountChar('{');
                    codeOpen -= line.CountChar('}');
                    if (codeOpen > 0)
                    {
                        // still in context, add to npcLines
                        npcLines.Add(new ParserLine(i, line));
                        continue;
                    }

                    // last bracket is closed, NPC is complete!
                    if (npcLines.Count == 0)
                    {
                        continue;                         // huh, no valid line found? o.o
                    }
                    // start Parser with the Script Lines
                    ScriptParser parser = new ScriptParser();
                    parser.Lines      = npcLines.ToArray();
                    parser.Scriptpath = Filepath;
                    parser.NpcName    = npcName;
                    parser.Parse();
                    if (parser.Messages.Count == 0)
                    {
                        continue;                         // no Messages to work with, next Script!
                    }
                    scriptParser.Add(parser);             // stor parser to replace Lines after finishing
                    FillDB(parser);
                }
                // else: no Script block, no valid Script head, nothing todo here
            }

            // nothing to replace
            //if( scriptParser.Count == 0 )
            return;

            foreach (ScriptParser parser in scriptParser)
            {
                foreach (ScriptMessage msg in parser.Messages.Values)
                {
                    Lines[msg.Index]  = Lines[msg.Index].Replace("\"" + msg.Message + "\"", "GetLanguageString( " + msg.Constant + " )");
                    Lines[msg.Index] += " // " + msg.Message;
                }
                foreach (KeyValuePair <int, ScriptMessage> pair in parser.Duplicates)
                {
                    ScriptMessage msg = pair.Value;
                    Lines[pair.Key]  = Lines[pair.Key].Replace("\"" + msg.Message + "\"", "GetLanguageString( " + msg.Constant + " )");
                    Lines[pair.Key] += " // " + msg.Message;
                }
            }

            // add Constants to end of Script
            List <string> consts = scriptParser.GetConstants();
            Lines.AddRange(new string[] { "", "", "" });               // 3 lines
            Lines.Add("/* auto generated by GodLesZ' Script Parser 1.0.2");
            Lines.Add("\t#Language constants used#");
            for (int i = 0; i < consts.Count; i++)
            {
                Lines.Add("\t\t" + consts[i]);
            }
            Lines.Add("*/");

            // write new File
            File.Delete(Filepath);
            File.WriteAllLines(Filepath, Lines.ToArray());
        }
예제 #2
0
        public void Parse()
        {
            for (int i = 0; i < mLines.Length; i++)
            {
                string Line = mLines[i].Line.Trim().Replace("\\\"", "'");                     // mes "123 \"bla\" blubb";
                if (Line.Length == 0)
                {
                    continue;
                }

                string dup        = Line.ToLower().Trim();
                string npcMessage = "";
                string constName  = "";
                Match  m;
                if (dup.Contains("mes") == true)
                {
                    m = Regex.Match(Line, ScriptParser.REGEX_MESSAGE + ";", RegexOptions.IgnoreCase);
                }
                else if (dup.Contains("mapannounce") == true)
                {
                    m = Regex.Match(Line, ScriptParser.REGEX_MAPANNOUNCE + ";", RegexOptions.IgnoreCase);
                }
                else if (dup.Contains("announce") == true)
                {
                    m = Regex.Match(Line, ScriptParser.REGEX_ANNOUNCE + ";", RegexOptions.IgnoreCase);
                }
                else
                {
                    continue;
                }

                if (m.Success == false)
                {
                    continue;
                }
                if (m.Groups.Count < 2)
                {
                    continue;
                }
                if (m.Groups[1].Captures.Count == 0)
                {
                    continue;
                }

                npcMessage = m.Groups[1].Captures[0].Value;
                if (npcMessage.Trim().Length == 0)                  // empty mes, no need to Translate
                {
                    continue;
                }
                constName = "";
                if (BuildNameKey(npcMessage, out constName) == false)
                {
                    continue;
                }

                ScriptMessage msg = new ScriptMessage(mLines[i].Index, npcMessage, constName);
                if (Messages.ContainsKey(msg.Constant) == true)
                {
                    mDuplicates.Add(mLines[i].Index, Messages[msg.Constant]);
                    continue;
                }
                Messages.Add(constName, msg);
            }
        }
예제 #3
0
		public void Parse() {

			for( int i = 0; i < mLines.Length; i++ ) {
				string Line = mLines[ i ].Line.Trim().Replace( "\\\"", "'" ); // mes "123 \"bla\" blubb";
				if( Line.Length == 0 )
					continue;

				string dup = Line.ToLower().Trim();
				string npcMessage = "";
				string constName = "";
				Match m;
				if( dup.Contains( "mes" ) == true )
					m = Regex.Match( Line, ScriptParser.REGEX_MESSAGE + ";", RegexOptions.IgnoreCase );
				else if( dup.Contains( "mapannounce" ) == true )
					m = Regex.Match( Line, ScriptParser.REGEX_MAPANNOUNCE + ";", RegexOptions.IgnoreCase );
				else if( dup.Contains( "announce" ) == true )
					m = Regex.Match( Line, ScriptParser.REGEX_ANNOUNCE + ";", RegexOptions.IgnoreCase );
				else
					continue;

				if( m.Success == false )
					continue;
				if( m.Groups.Count < 2 )
					continue;
				if( m.Groups[ 1 ].Captures.Count == 0 )
					continue;

				npcMessage = m.Groups[ 1 ].Captures[ 0 ].Value;
				if( npcMessage.Trim().Length == 0 ) // empty mes, no need to Translate 
					continue;
				constName = "";
				if( BuildNameKey( npcMessage, out constName ) == false )
					continue;

				ScriptMessage msg = new ScriptMessage( mLines[ i ].Index, npcMessage, constName );
				if( Messages.ContainsKey( msg.Constant ) == true ) {
					mDuplicates.Add( mLines[ i ].Index, Messages[ msg.Constant ] );
					continue;
				}
				Messages.Add( constName, msg );
			}

		}