示例#1
0
 public abstract void ExportStandaloneVm(
     Dictionary <string, FileOutput> output,
     IList <Pastel.Nodes.VariableDeclaration> globals,
     IList <Pastel.Nodes.StructDefinition> structDefinitions,
     IList <Pastel.Nodes.FunctionDefinition> functionDefinitions,
     IList <LibraryForExport> everyLibrary,
     ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform);
示例#2
0
 public abstract Dictionary <string, FileOutput> ExportProject(
     IList <Pastel.Nodes.VariableDeclaration> globals,
     IList <Pastel.Nodes.StructDefinition> structDefinitions,
     IList <Pastel.Nodes.FunctionDefinition> functionDefinitions,
     IList <LibraryForExport> libraries,
     ResourceDatabase resourceDatabase,
     Options options,
     ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform);
示例#3
0
 public override Dictionary <string, FileOutput> ExportStandaloneVm(
     IList <VariableDeclaration> globals,
     IList <StructDefinition> structDefinitions,
     IList <FunctionDefinition> functionDefinitions,
     IList <LibraryForExport> everyLibrary,
     ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
 {
     throw new NotImplementedException();
 }
示例#4
0
 public override Dictionary <string, FileOutput> ExportProject(
     IList <VariableDeclaration> globals,
     IList <StructDefinition> structDefinitions,
     IList <FunctionDefinition> functionDefinitions,
     IList <LibraryForExport> libraries,
     ResourceDatabase resourceDatabase,
     Options options,
     ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
 {
     throw new InvalidOperationException("This platform does not support direct export.");
 }
示例#5
0
        // Returns true if any export is necessary i.e. bytecode-only libraries will return false.
        private bool GetLibraryCode(string baseDir, LibraryForExport library, List <LangCSharp.DllFile> dllsOut, Dictionary <string, FileOutput> filesOut,
                                    ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            string libraryName = library.Name;

            this.Translator.CurrentLibraryFunctionTranslator = libraryNativeInvocationTranslatorProviderForPlatform.GetTranslator(libraryName);
            List <string> libraryLines = new List <string>();

            if (library.ManifestFunction != null)
            {
                string libraryDir = baseDir + "Libraries/" + libraryName;
                libraryLines.Add(this.GenerateCodeForFunction(this.Translator, library.ManifestFunction));
                foreach (FunctionDefinition funcDef in library.Functions)
                {
                    libraryLines.Add(this.GenerateCodeForFunction(this.Translator, funcDef));
                }

                filesOut[libraryDir + "/LibraryWrapper.cs"] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = string.Join(this.NL,
                                              "using System;",
                                              "using System.Collections.Generic;",
                                              "using System.Linq;",
                                              "using Interpreter;",
                                              "using Interpreter.Structs;",
                                              "using Interpreter.Vm;",
                                              "",
                                              "namespace Interpreter.Libraries." + libraryName,
                                              "{",
                                              "    public static class LibraryWrapper",
                                              "    {",
                                              this.IndentCodeWithSpaces(string.Join(this.NL, libraryLines), 8),
                                              "    }",
                                              "}",
                                              ""),
                };

                foreach (ExportEntity codeFile in library.ExportEntities["COPY_CODE"])
                {
                    string targetPath = codeFile.Values["target"].Replace("%LIBRARY_PATH%", libraryDir);
                    filesOut[targetPath] = codeFile.FileOutput;
                }

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

                return(true);
            }

            return(false);
        }
示例#6
0
 public override Dictionary <string, FileOutput> ExportProject(
     IList <VariableDeclaration> globals,
     IList <StructDefinition> structDefinitions,
     IList <FunctionDefinition> functionDefinitions,
     IList <LibraryForExport> libraries,
     ResourceDatabase resourceDatabase,
     Options options,
     ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
 {
     return(this.ExportProjectImpl(globals, structDefinitions, functionDefinitions, libraries, resourceDatabase, options, libraryNativeInvocationTranslatorProviderForPlatform, this.Translator));
 }
示例#7
0
        public override Dictionary <string, FileOutput> ExportProject(
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            if (!options.GetBool(ExportOptionKey.HAS_ICON))
            {
                throw new InvalidOperationException("Cannot generate a Chrome Web App without an icon resource.");
            }

            string iconFilePath = options.GetString(ExportOptionKey.ICON_PATH);

            if (!FileUtil.FileExists(iconFilePath))
            {
                throw new InvalidOperationException("Icon resource path points to non-existent file.");
            }
            SystemBitmap iconFile  = new SystemBitmap(iconFilePath);
            SystemBitmap smallIcon = iconFile.CloneToNewSize(16, 16);
            SystemBitmap largeIcon = iconFile.CloneToNewSize(128, 128);

            JavaScriptApp.PlatformImpl      jsBasicPlatform = (JavaScriptApp.PlatformImpl) this.PlatformProvider.GetPlatform("javascript-app");
            Dictionary <string, FileOutput> files           = jsBasicPlatform.ExportProjectImpl(globals, structDefinitions, functionDefinitions, libraries, resourceDatabase, options, libraryNativeInvocationTranslatorProviderForPlatform, this.Translator);
            Dictionary <string, string>     replacements    = this.GenerateReplacementDictionary(options, resourceDatabase);

            replacements["JS_LIB_INCLUSIONS"] = jsBasicPlatform.GenerateJsLibInclusionHtml(files.Keys);
            this.CopyResourceAsText(files, "background.js", "Resources/BackgroundJs.txt", replacements);
            this.CopyResourceAsText(files, "index.html", "Resources/IndexHtml.txt", replacements); // overwrites GameHostHtml.txt from javascript-app
            this.CopyResourceAsText(files, "chrome_web_app.js", "Resources/ChromeWebAppJs.txt", replacements);
            this.CopyResourceAsText(files, "manifest.json", "Resources/ManifestJson.txt", Util.MakeReplacementStringsJsonSafe(replacements));
            files["icon-16.png"] = new FileOutput()
            {
                Type   = FileOutputType.Image,
                Bitmap = smallIcon,
            };
            files["icon-128.png"] = new FileOutput()
            {
                Type   = FileOutputType.Image,
                Bitmap = largeIcon,
            };

            return(new Dictionary <string, FileOutput>()
            {
                { replacements["PROJECT_ID"] + ".zip", ZipCreator.Create(files, false) }
            });
        }
示例#8
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);

            this.OutputAndroidBoilerplate(output, replacements, options);

            string srcPath = "app/src/main/java";

            string[] imports = new string[]
            {
                "import org.crayonlang.interpreter.AndroidTranslationHelper;",
            };

            LangJava.PlatformImpl.ExportJavaLibraries(this, srcPath, libraries, output, libraryNativeInvocationTranslatorProviderForPlatform, imports);

            foreach (StructDefinition structDef in structDefinitions)
            {
                output["app/src/main/java/org/crayonlang/interpreter/structs/" + structDef.NameToken.Value + ".java"] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = this.GenerateCodeForStruct(structDef),
                };
            }

            StringBuilder sb = new StringBuilder();

            sb.Append(string.Join(this.NL, new string[] {
                "package org.crayonlang.interpreter;",
                "",
                "import java.util.ArrayList;",
                "import java.util.HashMap;",
                "import org.crayonlang.interpreter.structs.*;",
                "",
                "public final class Interpreter {",
                "  private Interpreter() {}",
                "",
            }));

            foreach (FunctionDefinition fnDef in functionDefinitions)
            {
                this.Translator.TabDepth = 1;
                sb.Append(this.GenerateCodeForFunction(this.Translator, fnDef));
                sb.Append(this.NL);
            }
            this.Translator.TabDepth = 0;
            sb.Append("}");
            sb.Append(this.NL);

            output["app/src/main/java/org/crayonlang/interpreter/Interpreter.java"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = sb.ToString(),
            };

            output["app/src/main/java/org/crayonlang/interpreter/VmGlobal.java"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = this.GenerateCodeForGlobalsDefinitions(this.Translator, globals),
            };

            // common Java helper files
            this.CopyResourceAsText(output, "app/src/main/java/org/crayonlang/interpreter/TranslationHelper.java", "Resources/TranslationHelper.txt", replacements);
            this.CopyResourceAsText(output, "app/src/main/java/org/crayonlang/interpreter/FastList.java", "Resources/FastList.txt", replacements);
            this.CopyResourceAsText(output, "app/src/main/java/org/crayonlang/interpreter/AndroidTranslationHelper.java", "Resources/app/src/main/java/org/crayonlang/interpreter/AndroidTranslationHelper.txt", replacements);
            this.CopyResourceAsText(output, "app/src/main/java/org/crayonlang/interpreter/LibraryFunctionPointer.java", "Resources/LibraryFunctionPointer.txt", replacements);
            this.CopyResourceAsText(output, "app/src/main/java/org/crayonlang/interpreter/LibraryInstance.java", "Resources/LibraryInstance.txt", replacements);
            this.CopyResourceAsText(output, "app/src/main/java/org/crayonlang/interpreter/LibraryLoader.java", "Resources/LibraryLoader.txt", replacements);

            output["app/src/main/assets/bytecode.txt"]         = resourceDatabase.ByteCodeFile;
            output["app/src/main/assets/resourcemanifest.txt"] = resourceDatabase.ResourceManifestFile;

            output["app/src/main/assets/imagesheetmanifest.txt"] =
                resourceDatabase.ImageSheetManifestFile != null
                    ? resourceDatabase.ImageSheetManifestFile
                    : new FileOutput()
            {
                Type = FileOutputType.Text, TextContent = ""
            };

            foreach (string imageResourceFile in resourceDatabase.ImageSheetFiles.Keys)
            {
                FileOutput file = resourceDatabase.ImageSheetFiles[imageResourceFile];
                output["app/src/main/assets/images/" + imageResourceFile] = file;
            }
        }
示例#9
0
        public override void ExportStandaloneVm(
            Dictionary <string, FileOutput> output,
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> everyLibrary,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            Dictionary <string, string> libraryProjectNameToGuid = new Dictionary <string, string>();

            string runtimeProjectGuid  = IdGenerator.GenerateCSharpGuid("runtime", "runtime-project");
            string runtimeAssemblyGuid = IdGenerator.GenerateCSharpGuid("runtime", "runtime-assembly");

            Dictionary <string, string> replacements = new Dictionary <string, string>()
            {
                { "PROJECT_ID", "CrayonRuntime" },
                { "PROJECT_GUID", runtimeProjectGuid },
                { "INTERPRETER_PROJECT_GUID", runtimeProjectGuid },
                { "ASSEMBLY_GUID", runtimeAssemblyGuid },
                { "PROJECT_TITLE", "Crayon Runtime" },
                { "COPYRIGHT", "©" },
                { "CURRENT_YEAR", DateTime.Now.Year.ToString() },
                { "DLL_REFERENCES", "" },
                { "CSHARP_APP_ICON", "<ApplicationIcon>icon.ico</ApplicationIcon>" },
                { "EMBEDDED_RESOURCES", "<EmbeddedResource Include=\"icon.ico\" />" },
                { "CSHARP_CONTENT_ICON", "" },
                { "DLLS_COPIED", "" },
            };
            string baseDir = "CrayonRuntime/";

            string dllReferencesOriginal = replacements["DLL_REFERENCES"];
            string dllsCopiedOriginal    = replacements["DLLS_COPIED"];
            string embeddedResources     = replacements["EMBEDDED_RESOURCES"];

            replacements["EMBEDDED_RESOURCES"] = "";
            foreach (LibraryForExport library in everyLibrary)
            {
                string libBaseDir = "Libs/" + library.Name + "/";
                List <LangCSharp.DllFile> dlls = new List <LangCSharp.DllFile>();
                if (this.GetLibraryCode(libBaseDir, library, dlls, output, libraryNativeInvocationTranslatorProviderForPlatform))
                {
                    string name        = library.Name;
                    string projectGuid = IdGenerator.GenerateCSharpGuid(library.Name + "|" + library.Version, "library-project");
                    replacements["PROJECT_GUID"]  = projectGuid;
                    replacements["ASSEMBLY_GUID"] = IdGenerator.GenerateCSharpGuid(library.Name + "|" + library.Version, "library-assembly");
                    replacements["PROJECT_TITLE"] = library.Name;
                    replacements["LIBRARY_NAME"]  = library.Name;
                    LangCSharp.DllReferenceHelper.AddDllReferencesToProjectBasedReplacements(replacements, dlls, library.LibProjectNamesAndGuids);

                    libraryProjectNameToGuid[name] = projectGuid;

                    replacements["DOT_NET_LIBS"] = Util.JoinLines(
                        library.DotNetLibs.Select(
                            dotNetLib =>
                            "    <Reference Include=\"" + dotNetLib + "\" />")
                        .ToArray());

                    this.CopyResourceAsText(output, libBaseDir + library.Name + ".sln", "ResourcesLib/Solution.txt", replacements);
                    this.CopyResourceAsText(output, libBaseDir + library.Name + ".csproj", "ResourcesLib/ProjectFile.txt", replacements);
                    this.CopyResourceAsText(output, libBaseDir + "Properties/AssemblyInfo.cs", "ResourcesLib/AssemblyInfo.txt", replacements);

                    foreach (LangCSharp.DllFile dll in dlls)
                    {
                        output[libBaseDir + dll.HintPath] = dll.FileOutput;
                    }
                }
            }
            replacements["DLL_REFERENCES"]     = dllReferencesOriginal;
            replacements["DLLS_COPIED"]        = dllsCopiedOriginal;
            replacements["EMBEDDED_RESOURCES"] = embeddedResources;
            replacements["PROJECT_GUID"]       = runtimeProjectGuid;
            replacements["ASSEMBLY_GUID"]      = runtimeAssemblyGuid;

            this.CopyTemplatedFiles(baseDir, output, replacements, true);
            this.ExportInterpreter(baseDir, output, globals, structDefinitions, functionDefinitions);
            this.ExportProjectFiles(baseDir, output, replacements, libraryProjectNameToGuid, true);
            this.CopyResourceAsBinary(output, baseDir + "icon.ico", "ResourcesVm/icon.ico");

            TODO.MoveCbxParserIntoTranslatedPastelCode();
            this.CopyResourceAsText(output, baseDir + "CbxDecoder.cs", "ResourcesVm/CbxDecoder.txt", replacements);
        }
示例#10
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);
            StringBuilder cCode = new StringBuilder();

            cCode.Append("#include <stdio.h>\n");
            cCode.Append("#include <stdlib.h>\n");
            cCode.Append("#include <string.h>\n");
            cCode.Append(this.NL);

            cCode.Append(this.LoadTextResource("Resources/List.txt", replacements));
            cCode.Append(this.LoadTextResource("Resources/String.txt", replacements));
            cCode.Append(this.LoadTextResource("Resources/Dictionary.txt", replacements));
            cCode.Append(this.LoadTextResource("Resources/TranslationHelper.txt", replacements));

            // This needs to be done in LangC
            foreach (StructDefinition structDef in structDefinitions)
            {
                string name = structDef.NameToken.Value;
                cCode.Append("typedef struct " + name + " " + name + ";\n");
            }
            cCode.Append(this.NL);

            foreach (StructDefinition structDef in structDefinitions)
            {
                cCode.Append(this.GenerateCodeForStruct(structDef));
            }

            foreach (FunctionDefinition fd in functionDefinitions)
            {
                string functionDeclaration = this.GenerateCodeForFunctionDeclaration(this.Translator, fd);
                cCode.Append(functionDeclaration);
                cCode.Append(this.NL);
            }

            this.CTranslator.StringTableBuilder = new LangC.StringTableBuilder("VM");

            StringBuilder functionCodeBuilder = new StringBuilder();

            foreach (FunctionDefinition fd in functionDefinitions)
            {
                string functionCode = this.GenerateCodeForFunction(this.Translator, fd);
                functionCodeBuilder.Append(functionCode);
                functionCodeBuilder.Append(this.NL);
                functionCodeBuilder.Append(this.NL);
            }

            LangC.PlatformImpl.BuildStringTable(cCode, this.CTranslator.StringTableBuilder, this.NL);

            cCode.Append(functionCodeBuilder);

            cCode.Append(this.LoadTextResource("Resources/main.txt", replacements));

            output["main.c"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = cCode.ToString(),
            };
        }
示例#11
0
        public static void ExportJavaLibraries(
            AbstractPlatform platform,
            string srcPath,
            IList <LibraryForExport> libraries,
            Dictionary <string, FileOutput> output,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform,
            string[] extraImports)
        {
            List <string> defaultImports = new List <string>()
            {
                "import java.util.ArrayList;",
                "import java.util.HashMap;",
                "import org.crayonlang.interpreter.FastList;",
                "import org.crayonlang.interpreter.Interpreter;",
                "import org.crayonlang.interpreter.LibraryFunctionPointer;",
                "import org.crayonlang.interpreter.TranslationHelper;",
                "import org.crayonlang.interpreter.VmGlobal;",
                "import org.crayonlang.interpreter.structs.*;",
            };

            defaultImports.AddRange(extraImports);
            defaultImports.Sort();

            foreach (LibraryForExport library in libraries)
            {
                if (library.ManifestFunction != null)
                {
                    platform.Translator.CurrentLibraryFunctionTranslator = libraryNativeInvocationTranslatorProviderForPlatform.GetTranslator(library.Name);

                    List <string> libraryCode = new List <string>()
                    {
                        "package org.crayonlang.libraries." + library.Name.ToLower() + ";",
                        "",
                    };
                    libraryCode.AddRange(defaultImports);
                    libraryCode.AddRange(new string[]
                    {
                        "",
                        "public final class LibraryWrapper {",
                        "  private LibraryWrapper() {}",
                        "",
                    });

                    platform.Translator.TabDepth = 1;
                    libraryCode.Add(platform.GenerateCodeForFunction(platform.Translator, library.ManifestFunction));
                    string reflectionCalledPrefix = "lib_" + library.Name.ToLower() + "_function_";
                    foreach (FunctionDefinition fnDef in library.Functions)
                    {
                        string name = fnDef.NameToken.Value;
                        bool   isFunctionPointerObject = name.StartsWith(reflectionCalledPrefix);

                        string functionCode = platform.GenerateCodeForFunction(platform.Translator, fnDef);
                        if (isFunctionPointerObject)
                        {
                            functionCode = functionCode.Replace("public static Value v_" + name + "(Value[] ", "public Value invoke(Value[] ");
                            functionCode = "  " + functionCode.Replace("\n", "\n  ").TrimEnd();
                            functionCode = "  public static class FP_" + name + " extends LibraryFunctionPointer {\n" + functionCode + "\n  }\n";

                            libraryCode.Add(functionCode);
                        }
                        else
                        {
                            libraryCode.Add(functionCode);
                        }
                    }
                    platform.Translator.TabDepth = 0;
                    libraryCode.Add("}");
                    libraryCode.Add("");

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

                    output[libraryPath + "/LibraryWrapper.java"] = new FileOutput()
                    {
                        Type        = FileOutputType.Text,
                        TextContent = string.Join(platform.NL, libraryCode),
                    };

                    foreach (StructDefinition structDef in library.Structs)
                    {
                        string structCode = platform.GenerateCodeForStruct(structDef);

                        // This is kind of a hack.
                        // TODO: better.
                        structCode = structCode.Replace(
                            "package org.crayonlang.interpreter.structs;",
                            "package org.crayonlang.libraries." + library.Name.ToLower() + ";");

                        output[libraryPath + "/" + structDef.NameToken.Value + ".java"] = new FileOutput()
                        {
                            Type        = FileOutputType.Text,
                            TextContent = structCode,
                        };
                    }

                    foreach (ExportEntity supFile in library.ExportEntities["COPY_CODE"])
                    {
                        string path = supFile.Values["target"].Replace("%LIBRARY_PATH%", libraryPath);
                        output[path] = supFile.FileOutput;
                    }
                }
            }
        }
示例#12
0
        public override Dictionary <string, FileOutput> ExportProject(
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            Dictionary <string, string>     replacements = this.GenerateReplacementDictionary(options, resourceDatabase);
            Dictionary <string, FileOutput> output       = new Dictionary <string, FileOutput>();
            string projectId = options.GetString(ExportOptionKey.PROJECT_ID);
            string baseDir   = projectId + "/";

            this.CopyTemplatedFiles(baseDir, output, replacements, false);

            List <LangCSharp.DllFile> dlls = new List <LangCSharp.DllFile>();

            foreach (LibraryForExport library in libraries)
            {
                this.GetLibraryCode(baseDir, library, dlls, output, libraryNativeInvocationTranslatorProviderForPlatform);
            }

            LangCSharp.DllReferenceHelper.AddDllReferencesToProjectBasedReplacements(replacements, dlls, new Dictionary <string, string>());

            this.ExportInterpreter(baseDir, output, globals, structDefinitions, functionDefinitions);

            output[baseDir + "Resources/ByteCode.txt"]         = resourceDatabase.ByteCodeFile;
            output[baseDir + "Resources/ResourceManifest.txt"] = resourceDatabase.ResourceManifestFile;
            if (resourceDatabase.ImageSheetManifestFile != null)
            {
                output[baseDir + "Resources/ImageSheetManifest.txt"] = resourceDatabase.ImageSheetManifestFile;
            }

            foreach (FileOutput imageFile in resourceDatabase.ImageResources.Where(img => img.CanonicalFileName != null))
            {
                output[baseDir + "Resources/" + imageFile.CanonicalFileName] = imageFile;
            }

            foreach (string imageSheetFileName in resourceDatabase.ImageSheetFiles.Keys)
            {
                output[baseDir + "Resources/" + imageSheetFileName] = resourceDatabase.ImageSheetFiles[imageSheetFileName];
            }

            foreach (FileOutput textFile in resourceDatabase.TextResources.Where(img => img.CanonicalFileName != null))
            {
                output[baseDir + "Resources/" + textFile.CanonicalFileName] = textFile;
            }

            foreach (FileOutput audioFile in resourceDatabase.AudioResources.Where(file => file.CanonicalFileName != null))
            {
                output[baseDir + "Resources/" + audioFile.CanonicalFileName] = audioFile;
            }

            foreach (FileOutput fontFile in resourceDatabase.FontResources.Where(file => file.CanonicalFileName != null))
            {
                output[baseDir + "Resources/" + fontFile.CanonicalFileName] = fontFile;
            }

            foreach (LangCSharp.DllFile dll in dlls)
            {
                output[baseDir + dll.HintPath] = dll.FileOutput;
            }

            if (options.GetBool(ExportOptionKey.HAS_ICON))
            {
                string iconPath = options.GetString(ExportOptionKey.ICON_PATH);
                output[baseDir + "icon.ico"] = new FileOutput()
                {
                    Type   = FileOutputType.Image,
                    Bitmap = new SystemBitmap(iconPath),
                };
            }

            this.ExportProjectFiles(baseDir, output, replacements, new Dictionary <string, string>(), false);

            return(output);
        }
示例#13
0
        public Dictionary <string, FileOutput> ExportProjectImpl(
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform,
            AbstractTranslator translatorOverride)
        {
            Dictionary <string, FileOutput> output       = new Dictionary <string, FileOutput>();
            Dictionary <string, string>     replacements = this.GenerateReplacementDictionary(options, resourceDatabase);

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

            coreVmCode.Add(this.GenerateCodeForGlobalsDefinitions(translatorOverride, globals));

            foreach (FunctionDefinition funcDef in functionDefinitions)
            {
                coreVmCode.Add(this.GenerateCodeForFunction(translatorOverride, funcDef));
            }

            string coreVm = string.Join("\r\n", coreVmCode);

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

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

            foreach (LibraryForExport library in libraries)
            {
                if (library.ManifestFunction != null)
                {
                    List <string> libraryLines = new List <string>();

                    translatorOverride.CurrentLibraryFunctionTranslator =
                        libraryNativeInvocationTranslatorProviderForPlatform.GetTranslator(library.Name);

                    library.ManifestFunction.NameToken = Pastel.Token.CreateDummyToken("lib_" + library.Name.ToLower() + "_manifest");
                    libraryLines.Add(this.GenerateCodeForFunction(translatorOverride, library.ManifestFunction));
                    foreach (FunctionDefinition fnDef in library.Functions)
                    {
                        libraryLines.Add(this.GenerateCodeForFunction(translatorOverride, fnDef));
                    }
                    libraryLines.Add("C$common$scrapeLibFuncNames('" + library.Name.ToLower() + "');");
                    libraryLines.Add("");

                    // add helper functions after the scrape.

                    foreach (ExportEntity embedCode in library.ExportEntities["EMBED_CODE"])
                    {
                        libraryLines.Add(embedCode.StringValue);
                    }

                    output["libs/lib_" + library.Name.ToLower() + ".js"] = new FileOutput()
                    {
                        Type        = FileOutputType.Text,
                        TextContent = string.Join("\n", libraryLines),
                    };
                    librariesWithCode.Add(library);
                }
            }

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

            replacements["JS_LIB_INCLUSIONS"] = this.GenerateJsLibInclusionHtml(output.Keys);
            this.CopyResourceAsText(output, "index.html", "Resources/GameHostHtml.txt", replacements);

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

            TODO.JavaScriptDeGamification();
            output["lib_supplemental.js"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = string.Join("\n\n", new string[] {
                    this.LoadTextResource("Resources/ImageResource.txt", replacements),
                    this.LoadTextResource("Resources/Game.txt", replacements),
                    this.LoadTextResource("Resources/Drawing.txt", replacements),
                    this.LoadTextResource("Resources/Gamepad.txt", replacements),
                    this.LoadTextResource("Resources/Input.txt", replacements),
                    this.LoadTextResource("Resources/Sound.txt", replacements),
                }),
            };

            StringBuilder resourcesJs = new StringBuilder();

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

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

            FileOutput imageSheetManifest = resourceDatabase.ImageSheetManifestFile;

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

            resourcesJs.Append("C$common$resourceManifest = ");
            resourcesJs.Append(Util.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(Util.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 = " + Util.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

            return(output);
        }
示例#14
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);

            this.OutputAndroidBoilerplate(output, replacements, options);

            options.SetOption(ExportOptionKey.JS_FILE_PREFIX, null);
            options.SetOption(ExportOptionKey.JS_FULL_PAGE, false); // TODO: figure out if logic should be implemented in the web view for this.
            options.SetOption(ExportOptionKey.JS_HEAD_EXTRAS, string.Join(
                                  "\n",
                                  "<script type=\"text/javascript\" src=\"android.js\"></script>",
                                  "<style type=\"text/css\">",
                                  "  body { margin:0px; background-color:#000; }",
                                  "  #crayon_host {",
                                  "    background-color:#000;",
                                  "    text-align:left;",
                                  "    width:100%;",
                                  "    height:100%;",
                                  "  }",
                                  "</style>"
                                  ));

            Dictionary <string, FileOutput> files        = new Dictionary <string, FileOutput>();
            Dictionary <string, FileOutput> basicProject = new Dictionary <string, FileOutput>();

            this.ParentPlatform.ExportProject(
                basicProject,
                globals,
                structDefinitions,
                functionDefinitions,
                libraries,
                resourceDatabase,
                options,
                libraryNativeInvocationTranslatorProviderForPlatform);

            // TODO: not good. The library inclusions should automatically be populated in LangJavaScript platforms.
            // This is also done identically in the ChromeApp PlatformImpl.
            replacements["JS_LIB_INCLUSIONS"] = JavaScriptApp.PlatformImpl.GenerateJsLibInclusionHtml(basicProject.Keys);

            foreach (string filePath in basicProject.Keys)
            {
                FileOutput file = basicProject[filePath];
                if (filePath.EndsWith("index.html"))
                {
                    file.TextContent = file.TextContent.Replace(
                        "<script type=\"text / javascript\" src=\"",
                        "<script type=\"text / javascript\" src=\"file:///android_asset/");
                }
                files["app/src/main/assets/" + filePath] = file;
            }

            // TODO: use orientations
            OrientationParser orientations = new OrientationParser(options);

            foreach (string filename in files.Keys)
            {
                output[this.ApplyReplacements(filename, replacements)] = files[filename];
            }
        }
示例#15
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            options.SetOption(ExportOptionKey.JS_FILE_PREFIX, null);
            options.SetOption(ExportOptionKey.JS_FULL_PAGE, false); // iOS export has its own enforced fullscreen logic
            options.SetOption(ExportOptionKey.JS_HEAD_EXTRAS, string.Join(
                                  "\n",
                                  "<script type=\"text/javascript\" src=\"ios.js\"></script>",
                                  "<style type=\"text/css\">",
                                  "  body { margin:0px; background-color:#000; }",
                                  "  #crayon_host {",
                                  "    background-color:#000;",
                                  "    text-align:left;",
                                  "    width:100%;",
                                  "    height:100%;",
                                  "  }",
                                  "</style>"
                                  ));
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);

            Dictionary <string, FileOutput> files        = new Dictionary <string, FileOutput>();
            Dictionary <string, FileOutput> basicProject = new Dictionary <string, FileOutput>();

            this.ParentPlatform.ExportProject(
                basicProject,
                globals,
                structDefinitions,
                functionDefinitions,
                libraries,
                resourceDatabase,
                options,
                libraryNativeInvocationTranslatorProviderForPlatform);

            // TODO: not good. The library inclusions should automatically be populated in LangJavaScript platforms.
            // This is also done identically in the ChromeApp PlatformImpl.
            replacements["JS_LIB_INCLUSIONS"] = JavaScriptApp.PlatformImpl.GenerateJsLibInclusionHtml(basicProject.Keys);

            foreach (string filePath in basicProject.Keys)
            {
                files["%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/jsres/" + filePath] = basicProject[filePath];
            }

            // TODO: use this in the pbxproj file.
            string uuidSeed = options.GetStringOrNull(ExportOptionKey.GUID_SEED);


            OrientationParser orientations         = new OrientationParser(options);
            bool       useLandscapeForLaunchscreen = orientations.SupportsLandscapeLeft || orientations.SupportsLandscapeRight;
            FileOutput launchScreen;

            if (options.GetBool(ExportOptionKey.HAS_LAUNCHSCREEN))
            {
                launchScreen = new FileOutput()
                {
                    Type   = FileOutputType.Image,
                    Bitmap = new SystemBitmap(options.GetString(ExportOptionKey.LAUNCHSCREEN_PATH)),
                };
            }
            else
            {
                launchScreen = new FileOutput()
                {
                    Type   = FileOutputType.Image,
                    Bitmap = new SystemBitmap(typeof(JavaScriptAppIosTranslator).Assembly, "SwiftResources/" +
                                              (useLandscapeForLaunchscreen ? "launchhorizontal.png" : "launchvertical.png")),
                };
            }
            files["%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Assets.xcassets/Launchscreen.imageset/launchscreen.png"] = launchScreen;
            replacements["LAUNCH_SCREEN_WIDTH"]  = launchScreen.Bitmap.Width.ToString();
            replacements["LAUNCH_SCREEN_HEIGHT"] = launchScreen.Bitmap.Height.ToString();

            IconSetGenerator icons = new IconSetGenerator();

            if (options.GetBool(ExportOptionKey.HAS_ICON))
            {
                string       iconPath = options.GetString(ExportOptionKey.ICON_PATH);
                SystemBitmap icon     = new SystemBitmap(iconPath);
                icons.AddInputImage(icon);
            }

            Dictionary <int, SystemBitmap> iconImagesBySize = icons
                                                              .AddOutputSize(20 * 1)
                                                              .AddOutputSize(20 * 2)
                                                              .AddOutputSize(20 * 3)
                                                              .AddOutputSize(29 * 1)
                                                              .AddOutputSize(29 * 2)
                                                              .AddOutputSize(29 * 3)
                                                              .AddOutputSize(40 * 1)
                                                              .AddOutputSize(40 * 2)
                                                              .AddOutputSize(40 * 3)
                                                              .AddOutputSize(60 * 2)
                                                              .AddOutputSize(60 * 3)
                                                              .AddOutputSize(76 * 1)
                                                              .AddOutputSize(76 * 2)
                                                              .AddOutputSize(167) // 83.5 * 2
                                                              .GenerateWithDefaultFallback();

            foreach (int size in iconImagesBySize.Keys)
            {
                files["%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Assets.xcassets/AppIcon.appiconset/icon" + size + ".png"] = new FileOutput()
                {
                    Type   = FileOutputType.Image,
                    Bitmap = iconImagesBySize[size],
                };
            }

            foreach (string pair in new string[] {
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%.xcodeproj/project.pbxproj|SwiftResources/PbxProj.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/AppDelegate.swift|SwiftResources/AppDelegateSwift.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Assets.xcassets/AppIcon.appiconset/Contents.json|SwiftResources/IconSetContentJson.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Assets.xcassets/Launchscreen.imageset/Contents.json|SwiftResources/ImageSetContentJson.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Base.lproj/LaunchScreen.storyboard|SwiftResources/LaunchScreenStoryboard.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Base.lproj/Main.storyboard|SwiftResources/MainStoryboard.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Info.plist|SwiftResources/InfoPlist.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/ViewController.swift|SwiftResources/ViewControllerSwift.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/jsres/ios.js|SwiftResources/iOSjs.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/jsres/index.html|SwiftResources/HostHtml.txt",
            })
            {
                string[] parts = pair.Split('|');
                files[parts[0]] = new FileOutput()
                {
                    TrimBomIfPresent = true,
                    Type             = FileOutputType.Text,
                    TextContent      = this.LoadTextResource(parts[1], replacements),
                };
            }

            foreach (string filename in files.Keys)
            {
                output[this.ApplyReplacements(filename, replacements)] = files[filename];
            }
        }
示例#16
0
        public override Dictionary <string, FileOutput> ExportProject(
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            Dictionary <string, string>     replacements = this.GenerateReplacementDictionary(options, resourceDatabase);
            Dictionary <string, FileOutput> output       = new Dictionary <string, FileOutput>();

            TODO.OverrideJavaPackagesFromBuildFile();

            string package    = options.GetString(ExportOptionKey.PROJECT_ID).ToLower();
            string srcPath    = "src";
            string sourcePath = srcPath + "/" + package + "/";

            string[] imports = new string[]
            {
                "import org.crayonlang.interpreter.ResourceReader;",
                "import org.crayonlang.interpreter.AwtTranslationHelper;",
            };

            LangJava.PlatformImpl.ExportJavaLibraries(this, srcPath, libraries, output, libraryNativeInvocationTranslatorProviderForPlatform, imports);

            foreach (StructDefinition structDef in structDefinitions)
            {
                output["src/org/crayonlang/interpreter/structs/" + structDef.NameToken.Value + ".java"] = new FileOutput()
                {
                    Type        = FileOutputType.Text,
                    TextContent = this.GenerateCodeForStruct(structDef),
                };
            }

            StringBuilder sb = new StringBuilder();

            sb.Append(string.Join(this.NL, new string[] {
                "package org.crayonlang.interpreter;",
                "",
                "import java.util.ArrayList;",
                "import java.util.HashMap;",
                "import org.crayonlang.interpreter.structs.*;",
                "",
                "public final class Interpreter {",
                "  private Interpreter() {}",
                "",
            }));

            foreach (FunctionDefinition fnDef in functionDefinitions)
            {
                this.Translator.TabDepth = 1;
                sb.Append(this.GenerateCodeForFunction(this.Translator, fnDef));
                sb.Append(this.NL);
            }
            this.Translator.TabDepth = 0;
            sb.Append("}");
            sb.Append(this.NL);

            output["src/org/crayonlang/interpreter/Interpreter.java"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = sb.ToString(),
            };

            output["src/org/crayonlang/interpreter/VmGlobal.java"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = this.GenerateCodeForGlobalsDefinitions(this.Translator, globals),
            };

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

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

            // need to move these to library supplemental files
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/GameWindow.java", "Resources/GameWindow.txt", replacements);
            this.CopyResourceAsText(output, "src/org/crayonlang/interpreter/RenderEngine.java", "Resources/RenderEngine.txt", replacements);

            this.CopyResourceAsText(output, "src/" + package + "/Main.java", "Resources/Main.txt", replacements);
            this.CopyResourceAsText(output, "build.xml", "Resources/BuildXml.txt", 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.ToLower().EndsWith(".java"))
                                                 .Select(filename => output[filename]);

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

            return(output);
        }
示例#17
0
        public static void ExportJavaLibraries(
            AbstractPlatform platform,
            string srcPath,
            IList <LibraryForExport> libraries,
            Dictionary <string, FileOutput> output,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform,
            string[] extraImports)
        {
            List <string> defaultImports = 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.VmGlobal;",
                "import org.crayonlang.interpreter.structs.*;",
            };

            defaultImports.AddRange(extraImports);
            defaultImports.Sort();

            foreach (LibraryForExport library in libraries)
            {
                if (library.ManifestFunction != null)
                {
                    platform.Translator.CurrentLibraryFunctionTranslator = libraryNativeInvocationTranslatorProviderForPlatform.GetTranslator(library.Name);

                    List <string> libraryCode = new List <string>()
                    {
                        "package org.crayonlang.libraries." + library.Name.ToLower() + ";",
                        "",
                    };
                    libraryCode.AddRange(defaultImports);
                    libraryCode.AddRange(new string[]
                    {
                        "",
                        "public final class LibraryWrapper {",
                        "  private LibraryWrapper() {}",
                        "",
                    });

                    platform.Translator.TabDepth = 1;
                    libraryCode.Add(platform.GenerateCodeForFunction(platform.Translator, library.ManifestFunction));
                    foreach (FunctionDefinition fnDef in library.Functions)
                    {
                        libraryCode.Add(platform.GenerateCodeForFunction(platform.Translator, fnDef));
                    }
                    platform.Translator.TabDepth = 0;
                    libraryCode.Add("}");
                    libraryCode.Add("");

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

                    output[libraryPath + "/LibraryWrapper.java"] = new FileOutput()
                    {
                        Type        = FileOutputType.Text,
                        TextContent = string.Join(platform.NL, libraryCode),
                    };

                    foreach (ExportEntity supFile in library.ExportEntities["COPY_CODE"])
                    {
                        string path = supFile.Values["target"].Replace("%LIBRARY_PATH%", libraryPath);
                        output[path] = supFile.FileOutput;
                    }
                }
            }
        }
示例#18
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);

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

            TODO.PythonAppDeGamification();

            foreach (string simpleCodeConcat in new string[] {
                "header.txt",
                "image_helper.txt",
                "game.txt",
                "gfx_renderer.txt",
                "gamepad_helper.txt",
            })
            {
                runPy.Add(this.LoadTextResource("Resources/" + simpleCodeConcat, replacements));
                runPy.Add("");
            }

            runPy.Add(this.GenerateCodeForGlobalsDefinitions(this.Translator, globals));
            runPy.Add("");
            runPy.Add(this.LoadTextResource("Resources/LibraryRegistry.txt", replacements));
            runPy.Add("");
            runPy.Add(this.LoadTextResource("Resources/TranslationHelper.txt", replacements));
            runPy.Add("");
            runPy.Add(this.LoadTextResource("Resources/ResourceReader.txt", replacements));
            runPy.Add("");

            foreach (FunctionDefinition funcDef in functionDefinitions)
            {
                runPy.Add(this.GenerateCodeForFunction(this.Translator, funcDef));
            }

            output["code/vm.py"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = string.Join("\n", runPy),
            };

            foreach (LibraryForExport library in libraries)
            {
                this.Translator.CurrentLibraryFunctionTranslator = libraryNativeInvocationTranslatorProviderForPlatform.GetTranslator(library.Name);
                string        libraryName  = library.Name;
                List <string> libraryLines = new List <string>();
                if (library.ManifestFunction != null)
                {
                    libraryLines.Add("import math");
                    libraryLines.Add("import os");
                    libraryLines.Add("import random");
                    libraryLines.Add("import sys");
                    libraryLines.Add("import time");
                    libraryLines.Add("import inspect");
                    libraryLines.Add("");
                    libraryLines.Add("from code.vm import *");
                    libraryLines.Add("");

                    libraryLines.Add(this.GenerateCodeForFunction(this.Translator, library.ManifestFunction));
                    foreach (FunctionDefinition funcDef in library.Functions)
                    {
                        libraryLines.Add(this.GenerateCodeForFunction(this.Translator, funcDef));
                    }

                    libraryLines.Add("");
                    libraryLines.Add("_moduleInfo = ('" + libraryName + "', dict(inspect.getmembers(sys.modules[__name__])))");
                    libraryLines.Add("");

                    foreach (ExportEntity codeToAppendToLibrary in library.ExportEntities["EMBED_CODE"])
                    {
                        libraryLines.Add(codeToAppendToLibrary.StringValue);
                    }

                    output["code/lib_" + libraryName.ToLower() + ".py"] = new FileOutput()
                    {
                        Type        = FileOutputType.Text,
                        TextContent = string.Join(this.NL, libraryLines),
                    };
                }
            }

            output["main.py"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = this.LoadTextResource("Resources/main.txt", replacements),
            };

            output["code/__init__.py"] = new FileOutput()
            {
                Type        = FileOutputType.Text,
                TextContent = "",
            };

            output["res/bytecode.txt"]          = resourceDatabase.ByteCodeFile;
            output["res/resource_manifest.txt"] = resourceDatabase.ResourceManifestFile;
            if (resourceDatabase.ImageSheetManifestFile != null)
            {
                output["res/image_sheet_manifest.txt"] = resourceDatabase.ImageSheetManifestFile;
            }

            foreach (FileOutput image in resourceDatabase.ImageResources)
            {
                output["res/images/" + image.CanonicalFileName] = image;
            }

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

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

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

            foreach (FileOutput fontResource in resourceDatabase.FontResources)
            {
                output["res/ttf/" + fontResource.CanonicalFileName] = fontResource;
            }
        }
示例#19
0
        public override void ExportProject(
            Dictionary <string, FileOutput> output,
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            Dictionary <string, string> replacements = this.GenerateReplacementDictionary(options, resourceDatabase);
            string projectId = options.GetString(ExportOptionKey.PROJECT_ID);
            string baseDir   = projectId + "/";

            this.CopyTemplatedFiles(baseDir, output, replacements, false);

            List <LangCSharp.DllFile> dlls = new List <LangCSharp.DllFile>();

            HashSet <string> dotNetLibs = new HashSet <string>();

            foreach (LibraryForExport library in libraries)
            {
                foreach (string dotNetLib in library.DotNetLibs)
                {
                    dotNetLibs.Add(dotNetLib);
                }
                this.GetLibraryCode(baseDir, library, dlls, output, libraryNativeInvocationTranslatorProviderForPlatform);
            }

            LangCSharp.DllReferenceHelper.AddDllReferencesToProjectBasedReplacements(replacements, dlls, new Dictionary <string, string>());

            replacements["DLL_REFERENCES"] += Util.JoinLines(
                dotNetLibs
                .OrderBy(v => v.ToLower())
                .Select(
                    dotNetLib =>
                    "    <Reference Include=\"" + dotNetLib + "\" />")
                .ToArray());

            this.ExportInterpreter(baseDir, output, globals, structDefinitions, functionDefinitions);

            output[baseDir + "Resources/ByteCode.txt"]         = resourceDatabase.ByteCodeFile;
            output[baseDir + "Resources/ResourceManifest.txt"] = resourceDatabase.ResourceManifestFile;
            if (resourceDatabase.ImageSheetManifestFile != null)
            {
                output[baseDir + "Resources/ImageSheetManifest.txt"] = resourceDatabase.ImageSheetManifestFile;
            }

            foreach (FileOutput imageFile in resourceDatabase.ImageResources.Where(img => img.CanonicalFileName != null))
            {
                output[baseDir + "Resources/" + imageFile.CanonicalFileName] = imageFile;
            }

            foreach (string imageSheetFileName in resourceDatabase.ImageSheetFiles.Keys)
            {
                output[baseDir + "Resources/" + imageSheetFileName] = resourceDatabase.ImageSheetFiles[imageSheetFileName];
            }

            foreach (FileOutput textFile in resourceDatabase.TextResources.Where(img => img.CanonicalFileName != null))
            {
                output[baseDir + "Resources/" + textFile.CanonicalFileName] = textFile;
            }

            foreach (FileOutput audioFile in resourceDatabase.AudioResources.Where(file => file.CanonicalFileName != null))
            {
                output[baseDir + "Resources/" + audioFile.CanonicalFileName] = audioFile;
            }

            foreach (FileOutput fontFile in resourceDatabase.FontResources.Where(file => file.CanonicalFileName != null))
            {
                output[baseDir + "Resources/" + fontFile.CanonicalFileName] = fontFile;
            }

            foreach (LangCSharp.DllFile dll in dlls)
            {
                output[baseDir + dll.HintPath] = dll.FileOutput;
            }

            if (options.GetBool(ExportOptionKey.HAS_ICON))
            {
                string        iconPath = options.GetString(ExportOptionKey.ICON_PATH);
                IconGenerator iconGen  = new IconGenerator();
                foreach (string path in iconPath.Split(','))
                {
                    iconGen.AddImage(new SystemBitmap(path.Trim()));
                }

                output[baseDir + "icon.ico"] = new FileOutput()
                {
                    Type          = FileOutputType.Binary,
                    BinaryContent = iconGen.GenerateIconFile(),
                };
            }

            this.ExportProjectFiles(baseDir, output, replacements, new Dictionary <string, string>(), false);
        }
示例#20
0
 public override void ExportProject(Dictionary <string, FileOutput> output, IList <VariableDeclaration> globals, IList <StructDefinition> structDefinitions, IList <FunctionDefinition> functionDefinitions, IList <LibraryForExport> libraries, ResourceDatabase resourceDatabase, Options options, ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
 {
     throw new NotImplementedException();
 }
示例#21
0
        public override Dictionary <string, FileOutput> ExportProject(
            IList <VariableDeclaration> globals,
            IList <StructDefinition> structDefinitions,
            IList <FunctionDefinition> functionDefinitions,
            IList <LibraryForExport> libraries,
            ResourceDatabase resourceDatabase,
            Options options,
            ILibraryNativeInvocationTranslatorProvider libraryNativeInvocationTranslatorProviderForPlatform)
        {
            options.SetOption(ExportOptionKey.JS_FILE_PREFIX, null);
            options.SetOption(ExportOptionKey.JS_HEAD_EXTRAS, string.Join(
                                  "\n",
                                  "<script type=\"text/javascript\" src=\"ios.js\"></script>",
                                  "<style type=\"text/css\">",
                                  "  body { margin:0px; background-color:#000; }",
                                  "  #crayon_host {",
                                  "    background-color:#000;",
                                  "    text-align:left;",
                                  "    width:100%;",
                                  "    height:100%;",
                                  "  }",
                                  "</style>"
                                  ));
            Dictionary <string, string>     replacements = this.GenerateReplacementDictionary(options, resourceDatabase);
            Dictionary <string, FileOutput> files        = new Dictionary <string, FileOutput>();
            Dictionary <string, FileOutput> basicProject = this.ParentPlatform.ExportProject(
                globals,
                structDefinitions,
                functionDefinitions,
                libraries,
                resourceDatabase,
                options,
                libraryNativeInvocationTranslatorProviderForPlatform);

            foreach (string filePath in basicProject.Keys)
            {
                files["%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/jsres/" + filePath] = basicProject[filePath];
            }

            // TODO: use this in the pbxproj file.
            string uuidSeed = options.GetStringOrNull(ExportOptionKey.GUID_SEED);

            foreach (string pair in new string[] {
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%.xcodeproj/project.pbxproj|SwiftResources/PbxProj.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/AppDelegate.swift|SwiftResources/AppDelegateSwift.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Assets.xcassets/AppIcon.appiconset/Contents.json|SwiftResources/IconSetContentJson.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Base.lproj/LaunchScreen.storyboard|SwiftResources/LaunchScreenStoryboard.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Base.lproj/Main.storyboard|SwiftResources/MainStoryboard.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/Info.plist|SwiftResources/InfoPlist.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/ViewController.swift|SwiftResources/ViewControllerSwift.txt",
                "%%%PROJECT_ID%%%/%%%PROJECT_ID%%%/jsres/ios.js|SwiftResources/iOSjs.txt",
            })
            {
                string[] parts = pair.Split('|');
                files[parts[0]] = new FileOutput()
                {
                    TrimBomIfPresent = true,
                    Type             = FileOutputType.Text,
                    TextContent      = this.LoadTextResource(parts[1], replacements),
                };
            }

            Dictionary <string, FileOutput> filesFixed = new Dictionary <string, FileOutput>();

            foreach (string filename in files.Keys)
            {
                filesFixed[this.ApplyReplacements(filename, replacements)] = files[filename];
            }

            return(filesFixed);
        }