Exemplo n.º 1
0
        private void GetLibraryCode(
            TemplateReader templateReader,
            string baseDir,
            LibraryForExport library,
            List <LangCSharp.DllFile> dllsOut,
            HashSet <string> dotNetRefs,
            Dictionary <string, FileOutput> filesOut)
        {
            string        libraryName  = library.Name;
            TemplateSet   libTemplates = templateReader.GetLibraryTemplates(library);
            List <string> libraryLines = new List <string>();

            string libraryDir = baseDir + "Libraries/" + libraryName;

            foreach (string structKey in libTemplates.GetPaths("gen/structs/"))
            {
                string structFileName = structKey.Substring(structKey.LastIndexOf('/') + 1);
                string structName     = System.IO.Path.GetFileNameWithoutExtension(structFileName);
                filesOut[libraryDir + "/" + structName + ".cs"] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = libTemplates.GetText(structKey),
                };
            }

            foreach (string helperFile in libTemplates.GetPaths("source/"))
            {
                filesOut[libraryDir + "/" + helperFile.Substring("source/".Length)] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = libTemplates.GetText(helperFile),
                };
            }

            filesOut[libraryDir + "/LibraryWrapper.cs"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = libTemplates.GetText("gen/LibraryWrapper.cs"),
            };

            foreach (ExportEntity dllFile in library.ExportEntities["DOTNET_DLL"])
            {
                dllsOut.Add(new LangCSharp.DllFile(dllFile));
            }

            foreach (ExportEntity dotNetRef in library.ExportEntities["DOTNET_REF"])
            {
                dotNetRefs.Add(dotNetRef.StringValue);
            }
        }
Exemplo n.º 2
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options)
        {
            List <string> jsExtraHead = new List <string>()
            {
                options.GetStringOrEmpty(ExportOptionKey.JS_HEAD_EXTRAS)
            };

            if (options.GetBool(ExportOptionKey.JS_FULL_PAGE))
            {
                jsExtraHead.Add(
                    "<script type=\"text/javascript\">"
                    + "C$common$globalOptions['fullscreen'] = true;"
                    + "</script>");
            }
            options.SetOption(ExportOptionKey.JS_HEAD_EXTRAS, string.Join("\n", jsExtraHead));

            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);

            TemplateReader templateReader = new TemplateReader(new PkgAwareFileUtil(), this);
            TemplateSet    vmTemplates    = templateReader.GetVmTemplates();

            output["vm.js"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = vmTemplates.GetText("vm.js"),
            };

            List <LibraryForExport> librariesWithCode = new List <LibraryForExport>();

            foreach (LibraryForExport library in libraries.Where(lib => lib.HasNativeCode))
            {
                string      libraryName  = library.Name;
                TemplateSet libTemplates = templateReader.GetLibraryTemplates(library);

                List <string> libraryLines = new List <string>();
                libraryLines.Add(libTemplates.GetText("gen/lib_" + libraryName.ToLowerInvariant() + ".js"));
                libraryLines.Add("");
                libraryLines.Add("C$common$scrapeLibFuncNames('" + libraryName.ToLowerInvariant() + "');");
                libraryLines.Add("");

                // add helper functions after the scrape.

                foreach (string jsHelperFile in libTemplates.GetPaths("source/", ".js"))
                {
                    libraryLines.Add(libTemplates.GetText(jsHelperFile));
                    libraryLines.Add("");
                }

                output["libs/lib_" + libraryName.ToLowerInvariant() + ".js"] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = string.Join(this.NL, libraryLines),
                };
                librariesWithCode.Add(library);
            }

            Dictionary <string, string> htmlReplacements = new Dictionary <string, string>(replacements);

            replacements["JS_LIB_INCLUSIONS"] = GenerateJsLibInclusionHtml(output.Keys);

            this.CopyResourceAsText(output, "index.html", "Resources/HostHtml.txt", replacements);
            this.CopyResourceAsText(output, "test_server.py", "Resources/TestServerPy.txt", replacements);

            this.CopyResourceAsText(output, "common.js", "Resources/Common.txt", replacements);

            System.Text.StringBuilder resourcesJs = new System.Text.StringBuilder();

            foreach (FileOutput textResource in resourceDatabase.TextResources)
            {
                resourcesJs.Append("C$common$addTextRes(");
                resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(textResource.CanonicalFileName));
                resourcesJs.Append(", ");
                resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(textResource.TextContent));
                resourcesJs.Append(");\n");
            }

            foreach (FileOutput fontResource in resourceDatabase.FontResources)
            {
                resourcesJs.Append("C$common$addBinaryRes(");
                resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(fontResource.CanonicalFileName));
                resourcesJs.Append(", '");
                resourcesJs.Append(Base64.ToBase64(fontResource.GetFinalBinaryContent()));
                resourcesJs.Append("');\n");
            }

            FileOutput imageSheetManifest = resourceDatabase.ImageSheetManifestFile;

            resourcesJs.Append("C$common$addTextRes('image_sheets.txt', ");
            resourcesJs.Append(imageSheetManifest == null ? "''" : StringTokenUtil.ConvertStringValueToCode(imageSheetManifest.TextContent));
            resourcesJs.Append(");\n");

            resourcesJs.Append("C$common$resourceManifest = ");
            resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(resourceDatabase.ResourceManifestFile.TextContent));
            resourcesJs.Append(";\n");

            string filePrefix = options.GetStringOrNull(ExportOptionKey.JS_FILE_PREFIX);

            if (filePrefix != null)
            {
                resourcesJs.Append("C$common$jsFilePrefix = ");
                resourcesJs.Append(StringTokenUtil.ConvertStringValueToCode(filePrefix));
                resourcesJs.Append(";\n");
            }

            output["resources.js"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = resourcesJs.ToString(),
            };

            output["bytecode.js"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = "C$bytecode = " + StringTokenUtil.ConvertStringValueToCode(resourceDatabase.ByteCodeFile.TextContent) + ";",
            };

            foreach (string imageResourceFile in resourceDatabase.ImageSheetFiles.Keys)
            {
                FileOutput file = resourceDatabase.ImageSheetFiles[imageResourceFile];
                output["resources/images/" + imageResourceFile] = file;
            }

            foreach (FileOutput audioResourceFile in resourceDatabase.AudioResources)
            {
                output["resources/audio/" + audioResourceFile.CanonicalFileName] = audioResourceFile;
            }

            // TODO: minify JavaScript across all of output dictionary
        }
Exemplo n.º 3
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options)
        {
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);

            string srcPath        = "src";
            string srcPackagePath = srcPath + "/" + replacements["JAVA_PACKAGE"].Replace('.', '/') + "/";

            List <string> imports = new List <string>()
            {
                "import java.util.ArrayList;",
                "import java.util.HashMap;",
                "import org.crayonlang.interpreter.Interpreter;",
                "import org.crayonlang.interpreter.TranslationHelper;",
                "import org.crayonlang.interpreter.ResourceReader;",
                "import org.crayonlang.interpreter.structs.*;",
            };

            imports.Sort();
            TemplateReader templateReader = new TemplateReader(new PkgAwareFileUtil(), this);

            foreach (LibraryForExport library in libraries.Where(t => t.HasNativeCode))
            {
                TemplateSet libraryTemplates = templateReader.GetLibraryTemplates(library);

                string libraryPath = srcPath + "/org/crayonlang/libraries/" + library.Name.ToLowerInvariant();

                foreach (string templatePath in libraryTemplates.GetPaths("gen/").Concat(libraryTemplates.GetPaths("source/")))
                {
                    string code     = libraryTemplates.GetText(templatePath);
                    string realPath = templatePath.Substring(templatePath.IndexOf('/') + 1);
                    output[libraryPath + "/" + realPath] = new FileOutput()
                    {
                        Type             = FileOutputType.Text,
                        TextContent      = code,
                        TrimBomIfPresent = realPath.EndsWith(".java"),
                    };
                }
            }

            TemplateSet vmTemplates = templateReader.GetVmTemplates();

            output["src/org/crayonlang/interpreter/vm/CrayonWrapper.java"] = new FileOutput()
            {
                Type             = FileOutputType.Text,
                TextContent      = vmTemplates.GetText("CrayonWrapper.java"),
                TrimBomIfPresent = true,
            };

            foreach (string structKey in vmTemplates.GetPaths("structs/"))
            {
                string structName = System.IO.Path.GetFileNameWithoutExtension(structKey);
                string structCode = vmTemplates.GetText(structKey);

                output["src/org/crayonlang/interpreter/structs/" + structName + ".java"] = new FileOutput()
                {
                    Type             = FileOutputType.Text,
                    TextContent      = structCode,
                    TrimBomIfPresent = true,
                };
            }

            // common Java helper files
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/LibraryInstance.java", "Resources/LibraryInstance.java", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/TranslationHelper.java", "Resources/TranslationHelper.java", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/PlatformTranslationHelper.java", "Resources/PlatformTranslationHelper.java", replacements);

            // java-app specific files
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/LibraryLoader.java", "Resources/LibraryLoader.java", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/ResourceReader.java", "Resources/ResourceReader.java", replacements);

            this.CopyResourceAsText(output, srcPackagePath + "/Main.java", "Resources/Main.java", replacements);
            this.CopyResourceAsText(output, "build.xml", "Resources/Build.xml", replacements);

            output["resources/manifest.txt"] = resourceDatabase.ResourceManifestFile;
            output["resources/bytecode.txt"] = resourceDatabase.ByteCodeFile;
            if (resourceDatabase.ImageSheetManifestFile != null)
            {
                output["resources/imagesheetmanifest.txt"] = resourceDatabase.ImageSheetManifestFile;
            }

            foreach (string imageSheetFileName in resourceDatabase.ImageSheetFiles.Keys)
            {
                FileOutput imageSheetFile = resourceDatabase.ImageSheetFiles[imageSheetFileName];
                output["resources/images/" + imageSheetFileName] = imageSheetFile;
            }

            foreach (FileOutput audioResource in resourceDatabase.AudioResources)
            {
                output["resources/audio/" + audioResource.CanonicalFileName] = audioResource;
            }

            foreach (FileOutput textResource in resourceDatabase.TextResources)
            {
                output["resources/text/" + textResource.CanonicalFileName] = textResource;
            }

            foreach (FileOutput fontResource in resourceDatabase.FontResources)
            {
                output["resources/ttf/" + fontResource.CanonicalFileName] = fontResource;
            }

            IEnumerable <FileOutput> javaFiles = output.Keys
                                                 .Where(filename => filename.ToLowerInvariant().EndsWith(".java"))
                                                 .Select(filename => output[filename]);

            foreach (FileOutput file in javaFiles)
            {
                file.TrimBomIfPresent = true;
            }
        }