private void HandleIncomingMessageFromJavaScript(string type, string jsonPayload) { IDictionary <string, object> wrappedData = new Wax.Util.JsonParser("{\"rawData\": " + jsonPayload + "}").ParseAsDictionary(); object rawData = wrappedData["rawData"]; switch (type) { case "bridgeReady": this.SendData(new Dictionary <string, object>() { { "buffer", initialData }, { "options", new Dictionary <string, object>() { { "keepAspectRatio", keepAspectRatio } } } }); this.initialData = null; break; case "shown": this.LoadedListener(new Dictionary <string, object>()); if (queueEventsMessages != null) { foreach (Dictionary <string, object> evMsg in queueEventsMessages) { this.EventsListener(evMsg); } queueEventsMessages = null; } break; case "events": Dictionary <string, object> eventData = new Dictionary <string, object>() { { "msgs", rawData }, }; if (queueEventsMessages != null) { queueEventsMessages.Add(eventData); } else { this.EventsListener(eventData); } break; case "eventBatch": this.BatchListener(new Dictionary <string, object>() { { "data", rawData } }); break; default: throw new NotImplementedException("No handler for message type: " + type); } }
internal override async Task <string> CreateAndShowWindowImpl( string title, byte[] nullableIcon, int width, int height, Func <string, string, bool> handleVmBoundMessage) { System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket( System.Net.Sockets.AddressFamily.Unix, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Unspecified); socket.Bind(new System.Net.Sockets.UnixDomainSocketEndPoint(this.filePath + "_us")); socket.Listen(1); System.ComponentModel.BackgroundWorker bgworker = new System.ComponentModel.BackgroundWorker(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); bgworker.DoWork += async(e, sender) => { System.Net.Sockets.Socket s = socket.Accept(); SocketReader sr = new SocketReader(s); while (true) { int length = sr.ReadLength(); string data = sr.ReadString(length); int colon = data.IndexOf(':'); string type = colon == -1 ? data : data.Substring(0, colon); string payload = colon == -1 ? "" : data.Substring(colon + 1); if (type == "READY") { StartSocketClient(); await this.SendString("SRC", JsResourceUtil.GetU3Source()); } else if (type == "VMJSON") { IDictionary <string, object> jsonPayload = new Wax.Util.JsonParser(payload.Substring(1, payload.Length - 2)).ParseAsDictionary(); handleVmBoundMessage((string)jsonPayload["type"], (string)jsonPayload["message"]); } else { throw new Exception("Unknown message type: " + type); } } }; bgworker.RunWorkerAsync(); await Task.Delay(TimeSpan.FromMilliseconds(10)); return(await RunProcess(title, width, height)); // Process ends when window is closed. }
internal static BuildRoot Parse(string file, string projectDir) { Wax.Util.JsonParser parser = new Wax.Util.JsonParser(file); IDictionary <string, object> rootDict = parser.ParseAsDictionary(); Wax.Util.JsonLookup rootData = new Wax.Util.JsonLookup(rootDict); BuildRoot rootOut = new BuildRoot(); ParseBuildItem(rootOut, rootDict, projectDir); List <Target> targets = new List <Target>(); foreach (IDictionary <string, object> targetRoot in rootData.GetAsList("targets").OfType <IDictionary <string, object> >()) { Target target = new Target(); ParseBuildItem(target, targetRoot, projectDir); targets.Add(target); } rootOut.Targets = targets.ToArray(); return(rootOut); }
private static void ParseBuildItem(BuildItem item, IDictionary <string, object> jsonData, string projectDir) { List <BuildVar> buildVars = new List <BuildVar>(); List <Wax.BuildArg> buildArgs = new List <Wax.BuildArg>(); List <Wax.ExtensionArg> extensionArgs = new List <Wax.ExtensionArg>(); List <BuildVar> envFileDefinedArgs = new List <BuildVar>(); List <IDictionary <string, object> > rawVars = new List <IDictionary <string, object> >(); foreach (string key in jsonData.Keys.OrderBy(k => k)) { object valueObj = jsonData[key]; string valueStr = ObjectToString(valueObj); if (valueObj == null) { continue; } switch (key) { case "targets": if (item is Target) { throw new System.InvalidOperationException("Cannot nest a 'targets' field within build targets."); } break; case "vars": case "extensions": object[] vars = jsonData[key] as object[]; if (vars == null) { throw new System.InvalidOperationException("'" + key + "' field in build file must be a list of objects"); } rawVars.AddRange(vars.OfType <IDictionary <string, object> >()); if (rawVars.Count != vars.Length) { throw new System.InvalidOperationException("'" + key + "' field in build file must be a list of objects"); } foreach (Wax.Util.JsonLookup itemInfo in rawVars.Select(rv => new Wax.Util.JsonLookup(rv))) { string name = itemInfo.GetAsString("name"); object value = itemInfo.Get("value"); if (key == "vars") { string envName = itemInfo.GetAsString("env"); if (envName != null) { value = value ?? Wax.Util.EnvironmentVariables.Get(envName); } if (name == null || name.Length == 0) { throw new System.InvalidOperationException("There is a build variable with no name."); } buildVars.Add(new BuildVar(name, value)); } else { string extension = itemInfo.GetAsString("extension"); string extArgValue = ObjectToString(value); extensionArgs.Add(new Wax.ExtensionArg() { Extension = extension, Name = name, Value = extArgValue }); } } break; case "name": if (valueStr != null) { if (item is BuildRoot) { throw new System.InvalidOperationException("Cannot set a name field on the root build data."); } ((Target)item).Name = valueStr; } break; case "inheritFrom": if (valueStr != null) { if (item is BuildRoot) { throw new System.InvalidOperationException("Build file root scope cannot use the 'inheritFrom' attribute. All targets inherit from root by default."); } ((Target)item).InheritFrom = valueStr; } break; default: object[] valueObjs = valueObj as object[]; if (valueObjs == null) { valueObjs = new object[] { valueObj }; } if (key == "envFile") { foreach (ProjectFilePath envFilePath in BuildContext.ToFilePaths(projectDir, valueObjs.OfType <string>().ToArray())) { if (System.IO.File.Exists(envFilePath.AbsolutePath)) { string envContents = System.IO.File.ReadAllText(envFilePath.AbsolutePath); IDictionary <string, object> envFileData = new Wax.Util.JsonParser(envContents).ParseAsDictionary(); foreach (string envKey in envFileData.Keys) { envFileDefinedArgs.Add(new BuildVar(envKey, envFileData[envKey])); } } else { throw new System.InvalidOperationException("The env file path '" + envFilePath + "' does not exist."); } } } else { buildArgs.AddRange(valueObjs .Where(v => v != null) .Select(v => ObjectToString(v)) .Where(s => { if (s == null) { throw new System.InvalidOperationException("Unexpected value in build file for field '" + key + "'."); } return(true); }) .Select(s => new Wax.BuildArg() { Name = key, Value = s })); } break; } } item.BuildArgs = buildArgs.ToArray(); item.ExtensionArgs = extensionArgs.ToArray(); // Flatten build vars and env file defined vars (with the latter taking precedence) Dictionary <string, BuildVar> flattenedVars = new Dictionary <string, BuildVar>(); foreach (BuildVar buildVar in buildVars.Concat(envFileDefinedArgs)) { flattenedVars[buildVar.ID] = buildVar; } item.BuildVars = flattenedVars.Values.ToArray(); }