Esempio n. 1
0
		public Parser(AbstractPlatform platform, BuildContext buildContext, SystemLibraryManager sysLibMan)
		{
			this.NullablePlatform = platform;
			this.IsTranslateMode = platform != null;
			this.CurrentClass = null;
			this.CurrentSystemLibrary = null;
			this.BuildContext = buildContext;
			this.VariableIds = new VariableIdAllocator();
			this.SystemLibraryManager = sysLibMan ?? new SystemLibraryManager();
			this.CurrentNamespace = "";
			this.NamespacePrefixLookupForCurrentFile = new List<string>();
		}
Esempio n. 2
0
		private static AbstractPlatform GetPlatformInstance(BuildContext buildContext)
		{
			switch (buildContext.Platform.ToLowerInvariant())
			{
				case "android": return new Crayon.Translator.Java.JavaAndroidPlatform();
				case "cwin": return new Crayon.Translator.COpenGL.COpenGLPlatform();
				case "csopengl": return new Crayon.Translator.CSharp.CSharpOpenTkPlatform();
				case "java": return new Crayon.Translator.Java.JavaAwtPlatform();
				case "js": return new Crayon.Translator.JavaScript.JavaScriptPlatform(buildContext.Minified, buildContext.JsFilePrefix);
				case "python": return new Crayon.Translator.Python.PythonPlatform();
				default:
					throw new InvalidOperationException("Unrecognized platform. See usage.");
			}
		}
Esempio n. 3
0
		private ByteBuffer GenerateByteCode(BuildContext buildContext, string inputFolder, List<string> spriteSheetOpsStringArgs, List<int[]> spriteSheetOpsIntArgs)
		{
			Parser userCodeParser = new Parser(null, buildContext, null);
			ParseTree.Executable[] userCode = userCodeParser.ParseAllTheThings(inputFolder);
			
			foreach (Executable ex in userCode)
			{
				ex.GenerateGlobalNameIdManifest(userCodeParser.VariableIds);
			}

			ByteCodeCompiler bcc = new ByteCodeCompiler();
			ByteBuffer buffer = bcc.GenerateByteCode(userCodeParser, userCode, spriteSheetOpsStringArgs, spriteSheetOpsIntArgs);

			this.LibraryBigSwitchStatement = userCodeParser.SystemLibraryManager.GetLibrarySwitchStatement(this.LanguageId, this.PlatformId);
			this.InterpreterCompiler = new InterpreterCompiler(this, userCodeParser.SystemLibraryManager);
			return buffer;
		}
Esempio n. 4
0
		public abstract Dictionary<string, FileOutput> Package(
			BuildContext buildContext,
			string projectId, 
			Dictionary<string, Executable[]> finalCode, 
			List<string> filesToCopyOver, 
			ICollection<StructDefinition> structDefinitions,
			string fileCopySourceRoot,
			SpriteSheetBuilder spriteSheet);
Esempio n. 5
0
		private void GenerateFiles(BuildContext buildContext, Dictionary<string, FileOutput> files, string rootDirectory, string inputDirectory)
		{
			foreach (string path in files.Keys)
			{
				FileOutput file = files[path];
				string fullOutputPath = System.IO.Path.Combine(rootDirectory, path).Replace('/', '\\').Replace("%PROJECT_ID%", buildContext.ProjectID);
				Util.EnsureFolderExists(fullOutputPath);
				switch (file.Type)
				{
					case FileOutputType.Text:
						System.IO.File.WriteAllText(fullOutputPath, file.TextContent);
						break;

					case FileOutputType.Binary:
						System.IO.File.WriteAllBytes(fullOutputPath, file.BinaryContent);
						break;

					case FileOutputType.Copy:
						string absolutePath = System.IO.Path.Combine(inputDirectory, file.RelativeInputPath);
						try
						{
							System.IO.File.Copy(absolutePath, fullOutputPath, true);
						}
						catch (System.IO.IOException ioe)
						{
							if (ioe.Message.Contains("it is being used by another process"))
							{
								throw new InvalidOperationException("The file '" + file.RelativeInputPath + "' appears to be in use. Please stop playing your game and try again.");
							}
							else
							{
								throw new InvalidOperationException("The file '" + file.RelativeInputPath + "' could not be copied to the output directory.");
							}
						}
						break;

					case FileOutputType.Image:
						file.Bitmap.Save(fullOutputPath);
						break;

					default:
						throw new NotImplementedException();
				}
			}
		}
Esempio n. 6
0
		public void Compile(
			BuildContext buildContext, 
			string inputFolder, 
			string baseOutputFolder, 
			string nullableReadableByteCodeOutputPath)
		{
			Parser.IsTranslateMode_STATIC_HACK = false;

			this.VerifyProjectId(buildContext.ProjectID);

			inputFolder = inputFolder.Replace('/', '\\');
			if (inputFolder.EndsWith("\\")) inputFolder = inputFolder.Substring(0, inputFolder.Length - 1);

			List<string> filesToCopyOver = new List<string>();
			this.GetRelativePaths(inputFolder, null, filesToCopyOver);

			SpriteSheetBuilder spriteSheetBuilder = new SpriteSheetBuilder(buildContext);
			if (buildContext.SpriteSheetIds != null)
			{
				foreach (string spriteSheetId in buildContext.SpriteSheetIds)
				{
					foreach (string fileMatcher in buildContext.SpriteSheetPrefixesById[spriteSheetId])
					{
						spriteSheetBuilder.AddPrefix(spriteSheetId, fileMatcher);
					}
				}
			}
			List<string> spriteSheetOpsStringArgs = new List<string>();
			List<int[]> spriteSheetOpsIntArgs = new List<int[]>();
			Dictionary<string, FileOutput> spriteSheetFiles = new Dictionary<string,FileOutput>();
			HashSet<string> filesAccountedForInSpriteSheet = new HashSet<string>();
			spriteSheetBuilder.Generate(this.GeneratedFilesFolder, filesToCopyOver, spriteSheetOpsStringArgs, spriteSheetOpsIntArgs, spriteSheetFiles, filesAccountedForInSpriteSheet);

			ByteBuffer byteCodeBuffer = GenerateByteCode(buildContext, inputFolder, spriteSheetOpsStringArgs, spriteSheetOpsIntArgs);

			if (nullableReadableByteCodeOutputPath != null)
			{
				this.GenerateReadableByteCode(nullableReadableByteCodeOutputPath, byteCodeBuffer);
			}

			string byteCode = ByteCodeEncoder.Encode(byteCodeBuffer);

			this.Context.ByteCodeString = byteCode;
			Parser.IsTranslateMode_STATIC_HACK = true;
			Dictionary<string, Executable[]> executablesByFile = this.InterpreterCompiler.Compile();
			Parser.IsTranslateMode_STATIC_HACK = false;

			StructDefinition[] structs = this.InterpreterCompiler.GetStructDefinitions();

			HashSet<string> filesToCopyOverTemporary = new HashSet<string>(filesToCopyOver);
			foreach (string fileInSpriteSheet in filesAccountedForInSpriteSheet)
			{
				filesToCopyOverTemporary.Remove(fileInSpriteSheet.Replace('/', '\\'));
			}
			filesToCopyOver.Clear();
			filesToCopyOver.AddRange(filesToCopyOverTemporary);

			Dictionary<string, FileOutput> files = this.Package(
				buildContext,
				buildContext.ProjectID,
				executablesByFile,
				filesToCopyOver,
				structs,
				inputFolder,
				spriteSheetBuilder);

			foreach (string file in spriteSheetFiles.Keys)
			{
				if (files.ContainsKey(file))
				{
					throw new InvalidOperationException("Autogenerated sprite sheet files are overwriting existing files.");
				}

				files[file] = spriteSheetFiles[file];
			}

			string outputFolder = baseOutputFolder;

			Util.EnsureFolderExists(outputFolder);
			
			this.DeleteExistingContents(outputFolder);

			this.GenerateFiles(buildContext, files, outputFolder, inputFolder);
		}
Esempio n. 7
0
		public SpriteSheetBuilder(BuildContext buildContext)
		{
			this.buildContext = buildContext;
		}