Beispiel #1
0
        static void ReadHumanCheck()
        {
            var humanCheckFilePath = files.First(x => x.Contains("HumanCheck.as"));
            var humanCheckFileData = File.ReadAllLines(humanCheckFilePath);

            HumanCheckClass = new HumanCheckClass("HumanCheck", null, humanCheckFilePath, humanCheckFileData);
            var hcString = string.Join("\n", humanCheckFileData);

            ReplaceInArray(ref HumanCheckClass.FileData, "§*§", "*");  //we fix the imports

            var classNameIndex = Array.IndexOf(HumanCheckClass.FileData,
                                               HumanCheckClass.FileData.First(x => x.Contains("public class HumanCheck")));
            int beginningBracketIndex = 0;
            int endingBracketIndex    = 0;

            FindBracketsPosition(ref beginningBracketIndex, ref endingBracketIndex, HumanCheckClass.FileData, classNameIndex + 1);

            var realClass = HumanCheckClass.FileData.ToList().GetRange(0, endingBracketIndex + 2);
            //we inject the new imports of the clean hurlant library
            var tempList = realClass.Take(2).ToList();

            tempList.AddRange(ImportsToInject.Select(import => $"   {import}"));
            realClass.RemoveRange(0, 2);
            tempList.AddRange(realClass);
            realClass = tempList;
            //
            int varCounter  = 0;
            int funcCounter = 0;

            for (int i = 0; i < realClass.Count; i++)
            {
                var line = realClass[i];
                var m    = HumanCheckVariableRegex.Match(line);
                if (m.Success)
                {
                    Console.WriteLine($"[HumanCheck] found variable {m.Groups["name"].Value} with type {m.Groups["variableType"].Value} at line {i}");
                    HumanCheckClass.Variables.Add(new AsVariable(m.Groups["type"].Value == "const", m.Groups["name"].Value, $"var_{varCounter++}", m.Groups["variableType"].Value, null));
                }
                var m2 = HumanCheckFunctionNameRegex.Match(line);
                if (m2.Success)
                {
                    Console.WriteLine($"[HumanCheck] found function {m2.Groups["name"].Value} with return type {m2.Groups["returnType"].Value} at line {i}");
                    HumanCheckClass.Functions.Add(new AsFunction(m2.Groups["accesibility"].Value == "private", m2.Groups["name"].Value, $"func_{funcCounter++}", m2.Groups["returnType"].Value, m2.Groups["parameters"].Value, i + 1));
                    //int startingFuncBracketIndex=0;
                    //int endingFuncBracketIndex=0;
                    //FindBracketsPosition(ref startingFuncBracketIndex, ref endingFuncBracketIndex, realClass.ToArray(), i);
                    //var code = realClass.GetRange(i,  endingFuncBracketIndex - i + 1);
                }
            }
            //we replace the variables & functions names
            foreach (var @class in WeirdClasses)
            {
                ReplaceInArray(ref realClass, @class.OldName, @class.NewName);

                foreach (var variable in @class.Variables)
                {
                    ReplaceInArray(ref realClass, @class.NewName + "." + variable.OldName,
                                   @class.NewName + "." + variable.NewName);
                }
                foreach (var function in @class.Functions)
                {
                    ReplaceInArray(ref realClass, @class.NewName + "." + function.OldName,
                                   @class.NewName + "." + function.NewName);
                }
            }

            HumanCheckClass.Variables.ForEach(x => ReplaceInArray(ref realClass, x.OldName, x.NewName));
            HumanCheckClass.Functions.ForEach(x => ReplaceInArray(ref realClass, x.OldName, x.NewName));
            // we get each function's code with the updated names
            foreach (var function in HumanCheckClass.Functions)
            {
                int startingFuncBracketIndex = 0;
                int endingFuncBracketIndex   = 0;
                FindBracketsPosition(ref startingFuncBracketIndex, ref endingFuncBracketIndex, realClass.ToArray(), function.BeginningBracketIndex);
                var code = realClass.GetRange(function.BeginningBracketIndex - 1, (endingFuncBracketIndex - function.BeginningBracketIndex) + 2).ToArray();
                function.Code = code;
            }
            //
            HumanCheckClass.FileData = realClass.ToArray();
            CreateDotNetClassFromAS();
            HumanCheckClass.FunctionsClass = CompilationHelper.CreateFunctionsClass(HumanCheckClass.DotNetCode);
        }