public MainForm() { InitializeComponent(); _ResourceStatusListView = new ListView[] { listView_texture, listView_image, listView_animation, listView_music, listView_soundeffect, listView_particle, listView_texturedfont, listView_ttffont, listView_shader }; try { _Target = new TargetProgram(this); } catch (Exception ex) { MessageBox.Show("无法创建性能监测。\n\n错误:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } // 绑定事件 _Target.OnProcessTrace += Target_OnProcessTrace; _Target.OnProcessExit += Target_OnProcessExit; _Target.OnResourceLoaded += Target_OnResourceLoaded; _Target.OnResourceRemoved += Target_OnResourceRemoved; _Target.OnResourceCleared += Target_OnResourceCleared; }
public TargetProgram GenerateTargetCode(CobaltProgram cobaltProgram) { TargetProgram program = new TargetProgram("JavaScript"); string js = GenerateProgramCode(cobaltProgram); program.AddFile(new TextFile("index.js", js)); return(program); }
/// <summary> /// The compiler entry point. /// </summary> /// <param name="args">Command line arguments.</param> public static void Main(string[] args) { // Set up configuration from command line arguments IConfiguration configuration = new ConfigurationBuilder().AddCommandLine(args, new Dictionary <string, string>() { { "-i", ARG_INPUT_FILE }, { "-o", ARG_OUTPUT_DIR }, { "-t", ARG_TARGET_PLATFORM } }).Build(); // Check for verbose mode bool verbose = args.Contains("-v"); // Set up logging providers ILoggerFactory loggerFactory = LoggingBootstrapper.CreateLoggerFactory(verbose); // Logger for local logging ILogger logger = loggerFactory.CreateLogger <Program>(); // Display help if requested if (args.Contains("-h")) { PrintHelpAndExit(logger); } // Parse command line arguments string inputFile = configuration.GetValue <string>(ARG_INPUT_FILE); string outputDir = configuration.GetValue <string>(ARG_OUTPUT_DIR); string targetPlatform = configuration.GetValue <string>(ARG_TARGET_PLATFORM); // Validate required parameters if (string.IsNullOrWhiteSpace(inputFile) || string.IsNullOrWhiteSpace(outputDir) || string.IsNullOrWhiteSpace(targetPlatform)) { logger.LogCritical("One or more required parameter is missing!"); PrintHelpAndExit(logger); } // Display information in verbose mode logger.LogDebug("Compiler parameters:"); logger.LogDebug($" - Input file: {inputFile}"); logger.LogDebug($" - Output directory: {outputDir}"); logger.LogDebug($" - Target platform: {targetPlatform}"); // Set up compiler backend ITargetCodeGenerator backend = null; switch (targetPlatform.ToLowerInvariant()) { case "node": case "Node": backend = new NodeJavaScriptCodeGenerator(); break; default: logger.LogError($"Unknown target platform `{targetPlatform}`."); break; } if (backend != null) { try { // Set up compiler CobaltCompiler compiler = new CobaltCompiler(loggerFactory, backend); // Read source code from input file string sourceCode = File.ReadAllText(inputFile); // Compile! logger.LogInformation("Compiling program..."); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); TargetProgram targetProgram = compiler.Compile(sourceCode); stopwatch.Stop(); logger.LogInformation($"Successfully compiled program in {stopwatch.ElapsedMilliseconds} ms."); // Write output files if (!Directory.Exists(outputDir)) { Directory.CreateDirectory(outputDir); } logger.LogInformation($"Writing files to {outputDir}..."); foreach (ITargetFile file in targetProgram.GetFiles()) { string filePath = Path.Combine(outputDir, file.Name); file.Writer.WriteTargetFile(file, filePath); logger.LogDebug($" - Wrote file {file.Name}"); } logger.LogInformation("All files have been written to disk."); } catch (Exception exception) { if (verbose) { logger.LogError($"Compilation failed with {exception.Message}{Environment.NewLine}{exception.StackTrace}"); } else { logger.LogError("Compilation failed.", exception); } logger.LogInformation("Press any key to terminate..."); Console.ReadKey(); } } }
void Target_OnResourceRemoved(TargetProgram.ResourceType type, TargetProgram.ResourcePoolType pool, string name) { if ((int)type < 1 || (int)type > _ResourceStatusListView.Count()) return; string tPoolName = pool == TargetProgram.ResourcePoolType.Global ? "全局" : "关卡"; List<ListViewItem> tItemsToRemove = new List<ListViewItem>(); foreach (ListViewItem item in _ResourceStatusListView[(int)type - 1].Items) { if (item.SubItems[0].Text == tPoolName && item.SubItems[1].Text == name) tItemsToRemove.Add(item); } ListViewItem tCounter = listView_resourceCounter.Items[(int)type - 1]; double tTotalTime = 0; int tTotalCount = 0; foreach (ListViewItem item in tItemsToRemove) { tTotalTime += Convert.ToDouble(item.SubItems[3].Text); ++tTotalCount; _ResourceStatusListView[(int)type - 1].Items.Remove(item); } double tNewTime = Math.Max(0, Convert.ToDouble(tCounter.SubItems[1].Text) - tTotalTime); if (tNewTime < 0.00001) tNewTime = 0; tCounter.SubItems[1].Text = tNewTime.ToString(); tCounter.SubItems[2].Text = (Convert.ToInt32(tCounter.SubItems[2].Text) - tTotalCount).ToString(); }
void Target_OnResourceLoaded(TargetProgram.ResourceType type, TargetProgram.ResourcePoolType pool, string name, string path, float time) { if ((int)type < 1 || (int)type > _ResourceStatusListView.Count()) return; ListViewItem tNewItem = new ListViewItem(pool == TargetProgram.ResourcePoolType.Global ? "全局" : "关卡"); tNewItem.SubItems.Add(new ListViewItem.ListViewSubItem(tNewItem, name)); tNewItem.SubItems.Add(new ListViewItem.ListViewSubItem(tNewItem, path)); tNewItem.SubItems.Add(new ListViewItem.ListViewSubItem(tNewItem, time.ToString())); _ResourceStatusListView[(int)type - 1].Items.Add(tNewItem); ListViewItem tCounter = listView_resourceCounter.Items[(int)type - 1]; tCounter.SubItems[1].Text = (Convert.ToDouble(tCounter.SubItems[1].Text) + time).ToString(); tCounter.SubItems[2].Text = (Convert.ToInt32(tCounter.SubItems[2].Text) + 1).ToString(); }
void Target_OnProcessTrace(DateTime t, TargetProgram.LogType type, string message) { ListViewItem tItem = new ListViewItem(); switch (type) { case TargetProgram.LogType.Information: tItem.Text = "信息"; tItem.ImageIndex = imageList_main.Images.IndexOfKey("info"); break; case TargetProgram.LogType.Error: tItem.Text = "错误"; tItem.ImageIndex = imageList_main.Images.IndexOfKey("error"); break; case TargetProgram.LogType.Warning: tItem.Text = "警告"; tItem.ImageIndex = imageList_main.Images.IndexOfKey("warning"); break; } tItem.SubItems.Add(new ListViewItem.ListViewSubItem(tItem, t.ToString("hh:mm:ss.fff"))); tItem.SubItems.Add(new ListViewItem.ListViewSubItem(tItem, message)); listView_log.Items.Add(tItem); if (listView_log.Items.Count > 5000) listView_log.Items.RemoveAt(0); listView_log.Items[listView_log.Items.Count - 1].EnsureVisible(); }