Beispiel #1
0
        //Convert a script to C#
        public List <string> Write(Script iscript, string inamespace, ref List <string> oglobal_names, ref List <string> oglobals, ref List <string> olocal_names, ref List <string> olocals, ref List <string> iStandardReferences, ref List <List <string> > iStandardCSReferences)
        {
            List <string> tout = new List <string>();

            //Header
            tout.Add("/**Header Start");
            for (int i = 0; i < iscript.header.Count; i++)
            {
                tout.Add(iscript.header[i]);
            }
            tout.Add("Header End**/");
            tout.Add("");

            //References
            tout.Add("");
            tout.Add("/*References Start*/");

            //Add standard references
            for (int i = 0; i < standard_references.Count; i++)
            {
                tout.Add("using " + standard_references[i] + ";");
            }

            //Process rest
            for (int i = 0; i < iscript.includes.Count; i++)
            {
                ProcessReference(iscript.includes[i], ref iStandardReferences, ref iStandardCSReferences);
            }

            for (int i = 0; i < project_references.Count; i++)
            {
                tout.Add("using " + project_references[i] + ";");
            }

            tout.Add("/*References End*/");
            tout.Add("");

            //Types
            tout.Add("/*Types Start*/");
            for (int i = 0; i < iscript.classes[0].types.Count; i++)
            {
                tout.Add(Utilities.Beautify_Delphi2CS(Utilities.Delphi2CSRules(TypeToString(iscript.classes[0].types[i])).Replace(";;", ";")).Replace("==", "=").Trim());
            }

            for (int i = 0; i < iscript.classes[1].types.Count; i++)
            {
                tout.Add(Utilities.Beautify_Delphi2CS(Utilities.Delphi2CSRules(TypeToString(iscript.classes[1].types[i])).Replace(";;", ";")).Replace("==", "=").Trim());
            }

            tout.Add("/*Types End*/");
            tout.Add("");

            //Namespace
            tout.Add("namespace " + inamespace);
            tout.Add("{");

            //Global Constants, Variables and Enums
            ProcessGlobals(iscript.classes[0], ref oglobals, ref oglobal_names);

            //Local Constants, Variables and Enums
            ProcessGlobals(iscript.classes[1], ref olocals, ref olocal_names);

            List <string> telement_names = new List <string>();
            string        classtype      = "";

            classtype = "class";
            tout.Add("");
            tout.Add("//Class GlobalVars");
            int tGlobalVarInsert = tout.Count;

            List <string> tGlobalVarClass = new List <string>();

            tGlobalVarClass.Add(Indent(4) + "public " + classtype + " GlobalVars");
            tGlobalVarClass.Add(Indent(4) + "{");

            tGlobalVarClass.AddRange(WriteClassBody(iscript.classes[0], ref telement_names));
            tGlobalVarClass.AddRange(WriteClassBody(iscript.classes[1], ref telement_names));

            tGlobalVarClass.Add(Indent(4) + "}");

            //Classes
            for (int i = 2; i < iscript.classes.Count; i++)
            {
                bool tHasBaseclass = true;
                if ((iscript.classes[i].baseclass == null) || (iscript.classes[i].baseclass == "") || (iscript.classes[i].baseclass == "null"))
                {
                    tHasBaseclass = false;
                }

                classtype = "";
                if ((iscript.classes[i].type == "c"))
                {
                    classtype = "class";
                }
                else if ((iscript.classes[i].type == "r"))
                {
                    classtype = "struct";
                }
                else
                {
                    classtype = "interface";
                }

                //Handle constructor

                tout.Add("");
                tout.Add("//Class " + iscript.classes[i].name);

                //Classes / Interfaces
                //Inheritance
                if (!tHasBaseclass)
                {
                    tout.Add(Indent(4) + "public " + classtype + " " + iscript.classes[i].name);
                }
                else
                {
                    tout.Add(Indent(4) + "public " + classtype + " " + iscript.classes[i].name + " : " + iscript.classes[i].baseclass);
                }

                tout.Add(Indent(4) + "{");

                telement_names = new List <string>();
                tout.AddRange(WriteClassBody(iscript.classes[i], ref telement_names));

                tout.Add(Indent(4) + "}");
            }

            tout.Add("}");

            List <string> temptytypeslist = new List <string>();

            //Replace the local Globals used in the text
            Translate.GlobalsRename(inamespace + ".GlobalVars.", ref tout, olocal_names, temptytypeslist);
            tout.InsertRange(tGlobalVarInsert, tGlobalVarClass);
            return(tout);
        }
Beispiel #2
0
        private void AnalyzeFolder(string iPath, string iOutPath, string iPatchPath, string iOverridePath, string iILPath, ref List <ReferenceStruct> oReferences, ref List <Translate> oDelphi, ref List <string> iStandardReferences, ref List <string> iDelphiStandardReferences, ref List <List <string> > iStandardCSReferences, s iform)
        {
            string FolderPath = iPath;
            string tdirectory = "";

            List <string> globals = new List <string>(), locals = new List <string>();
            List <string> global_names = new List <string>(), local_names = new List <string>();

            //Get files in folder
            string[] files = Directory.GetFiles(iPath);

            string[] patchfiles = new string[0];

            if (iPatchPath != "")
            {
                patchfiles = Directory.GetFiles(iPatchPath);
            }

            string[] overridefiles = new string[0];
            if (iOverridePath != "")
            {
                overridefiles = Directory.GetFiles(iOverridePath);
            }

            for (int i = 0; i < patchfiles.GetLength(0); i++)
            {
                patchfiles[i] = patchfiles[i].Split('.')[0];
            }

            for (int i = 0; i < patchfiles.GetLength(0); i++)
            {
                overridefiles[i] = overridefiles[i].Split('.')[0];
            }

            string[] directories = new string[0];

            if (iPath != "")
            {
                directories = Directory.GetDirectories(iPath);
            }

            string[] patchdirectories = new string[0];

            if (iPatchPath != "")
            {
                patchdirectories = Directory.GetDirectories(iPatchPath);
            }

            string[] overridedirectories = new string[0];

            if (iOverridePath != "")
            {
                overridedirectories = Directory.GetDirectories(iOverridePath);
            }

            string tstring      = "";
            bool   pasFileFound = false;

            string[] tfilename_elements = iPath.Split('\\');
            //string tfilename = tfilename_elements[tfilename_elements.GetLength(0) - 1];
            tdirectory = tfilename_elements[tfilename_elements.GetLength(0) - 1];//.Replace(" ", "_");

            //Filter for files to convert
            for (int i = 0; i < files.GetLength(0); i++)
            {
                tstring = files[i];
                string[] tstrarray      = tstring.Split('.');
                string   tfilename      = tstrarray[0];
                string   tfileextension = tstrarray[1];

                tstrarray = tfilename.Split("\\".ToCharArray());
                tfilename = tstrarray[tstrarray.Length - 1];

                string tpatchfile = "", toverridefile = "";

                for (int j = 0; j < patchfiles.Length; j++)
                {
                    patchfiles[j] = patchfiles[j].Replace(".txt", "");
                    tstrarray     = patchfiles[j].Split("\\".ToCharArray());
                    patchfiles[j] = tstrarray[tstrarray.Length - 1];

                    if (patchfiles[j] == tfilename)
                    {
                        tpatchfile = patchfiles[j];
                    }
                }

                for (int j = 0; j < overridefiles.Length; j++)
                {
                    overridefiles[j] = overridefiles[j].Replace(".cs", "");
                    tstrarray        = overridefiles[j].Split("\\".ToCharArray());
                    overridefiles[j] = tstrarray[tstrarray.Length - 1];

                    if (overridefiles[j] == tfilename)
                    {
                        toverridefile = overridefiles[j];
                    }
                }

                switch (tfileextension)
                {
                //Parse VCL file (The dialog is manually recreated as WinForm, so nothing is done here)
                case "dfm": break;

                //Parse unit
                case "pas": pasFileFound = true;
                    Translate ttranslate;

                    //if (iform.writeIL)

                    ttranslate = new Translate(tstring, tdirectory, iILPath, iOutPath, iPatchPath, tpatchfile, iOverridePath, toverridefile, Log, iStandardReferences, iDelphiStandardReferences, standardCSReferences, Log, ref iform);

                    if (toverridefile != "")
                    {
                        Directory.CreateDirectory(iOutPath);
                        File.Copy(iOverridePath + "\\" + toverridefile + ".cs", iOutPath + "\\" + tfilename + ".cs", true);
                    }

                    oDelphi.Add(ttranslate);
                    break;

                //Parse Project files
                case "dpk":     //Translate.DPK2Vcproj(tstring);
                    break;

                case "dproj":   if (iform.genProj)     //Translate.Dproj2Vcproj(tstring);
                    {
                        ReadProj(tstring, iOutPath, iform.csproj);
                    }
                    break;

                default: break;
                }
            }

            if (pasFileFound)
            {
                //Create Global and Local classes file
                //List<string> globalsFile = new List<string>();
                //string tnamespace = tdirectory.Replace(" ", "_");
                //globalsFile.Add("using " + "System" + ";");
                //globalsFile.Add("using " + "System.Windows" + ";");
                //globalsFile.Add("using " + "System.String" + ";");
                //globalsFile.Add("using " + "System.Collections.Generic" + ";");
                //globalsFile.Add("");

                //globalsFile.Add("namespace " + tnamespace);
                //globalsFile.Add("{");
                //globalsFile.Add(Indent(4) + "public class " + tnamespace + "_Globals");
                //globalsFile.Add(Indent(4) + "{");

                //globalsFile.AddRange(globals);
                //globalsFile.Add(Indent(4) + "}");

                //globalsFile.Add(Indent(4) + "public class " + tnamespace + "_Locals");
                //globalsFile.Add(Indent(4) + "{");
                //globalsFile.AddRange(locals);
                //globalsFile.Add(Indent(4) + "}");
                //globalsFile.Add("}");

                ////Create directory
                //if (!System.IO.Directory.Exists(iOutPath))
                //    System.IO.Directory.CreateDirectory(iOutPath);

                //File.WriteAllLines(iOutPath + "\\" + "NamespaceGlobals.cs", globalsFile, Encoding.UTF8);

                ////Save Global and Local element names
                //ReferenceStruct treference = new ReferenceStruct();
                //treference.name = tdirectory;
                //treference.globals = new List<string>();
                //treference.locals = new List<string>();
                //treference.globals = global_names;
                //treference.locals = local_names;
                //oReferences.Add(treference);
            }

            for (int i = 0; i < directories.GetLength(0); i++)
            {
                string[] tpath_elements = directories[i].Split('\\');

                string[] tpatchpath_elements = new string[0];
                string   tcurr_patch_path    = "";

                if (patchdirectories.Length > 0)
                {
                    tpatchpath_elements = patchdirectories[i].Split('\\');
                    tcurr_patch_path    = iPatchPath + "\\" + tpatchpath_elements[tpatchpath_elements.GetLength(0) - 1];
                }

                string[] toverridepath_elements = new string[0];
                string   tcurr_override_path    = "";

                if (overridedirectories.Length > 0)
                {
                    toverridepath_elements = overridedirectories[i].Split('\\');
                    tcurr_override_path    = iOverridePath + "\\" + toverridepath_elements[toverridepath_elements.GetLength(0) - 1];
                }

                AnalyzeFolder(directories[i], iOutPath + "\\" + tpath_elements[tpath_elements.GetLength(0) - 1], tcurr_patch_path, tcurr_override_path, iILPath + "\\" + tpath_elements[tpath_elements.GetLength(0) - 1], ref oReferences, ref oDelphi, ref iStandardReferences, ref iDelphiStandardReferences, ref standardCSReferences, iform);
            }
        }