public ModuleFormatter(Project project, ModuleSpace module, string genDir, string srcDir) { this.project = project; this.module = module; this.genDir = genDir; this.srcDir = srcDir; }
public void Play(string moddl, Output <float> output) { var ast = new Parser().Parse(moddl); var mmls = new Dictionary <string, StringBuilder>(); foreach (var stmt in ast.Statements) { if (stmt is DirectiveStatement dir) { this.ProcessDirectiveStatement(dir); } else if (stmt is MmlStatement mml) { foreach (var track in mml.Tracks) { if (!mmls.ContainsKey(track)) { mmls.Add(track, new StringBuilder()); } mmls[track].AppendLine(mml.Mml); } } } var nodes = mmls .Where(kv => this.muteSpecifiedTracks != this.mutedOrUnmutedTracks.Contains(kv.Key)) .Select(kv => this.MmlToNode(kv.Key, kv.Value.ToString())); this.ShowGuiIfNeeded(output); var master = nodes.Aggregate(Const(0f), (acc, node) => (Node <float>)(acc + node)); var masterVol = 0.25f; ModuleSpace.Play <float>((master * masterVol).AsFloat(), output); }
public override Type Compile(ModuleSpace space, string key, string value) { if (key != null && key.Length > 0) { throw new Exception(Name + " type does not need a key. " + key); } return(new TypeDynamic(space, value)); }
public override Type Compile(ModuleSpace space, String key, String value) { if (key != null && key.Length > 0) { throw new Exception(Name + " type does not need a key. " + key); } if (value != null && value.Length > 0) { throw new Exception(Name + " type does not need a value. " + value); } return(this); }
// value=BeanName[:SpecialTypeId],BeanName2[:SpecialTypeId2] // 如果指定特别的TypeId,必须全部都指定。虽然部分指定也可以处理,感觉这样不大好。 private TypeDynamic(ModuleSpace space, string value) { foreach (var beanWithSpecialTypeId in value.Split(',')) { if (beanWithSpecialTypeId.Length == 0) // empty { continue; } var beanWithSpecialTypeIdArray = beanWithSpecialTypeId.Split(':'); if (beanWithSpecialTypeIdArray.Length == 0) { continue; } Type type = Type.Compile(space, beanWithSpecialTypeIdArray[0], null, null); if (false == type.IsNormalBean) { throw new Exception("dynamic only support normal bean"); } Bean bean = type as Bean; long specialTypeId = bean.TypeId; // default if (beanWithSpecialTypeIdArray.Length > 1) { SpecialCount++; specialTypeId = long.Parse(beanWithSpecialTypeIdArray[1]); if (specialTypeId <= 0) { throw new Exception("SpecialTypeId <= 0 is reserved"); } } RealBeans.Add(specialTypeId, bean); } if (SpecialCount == 0) // 没有配置特别的TypeId,全部使用Bean本身的TypeId。 { return; } if (RealBeans.Count == 0) // 动态类型没有配置任何具体的Bean。允许。 { return; } if (SpecialCount != RealBeans.Count) { throw new Exception("dynamic setup special TypeId,But Not All."); } }
protected void _compile(ModuleSpace space, String key, String value) { if (key != null && key.Length > 0) { throw new Exception(Name + " type does not need a key. " + key); } ValueType = Type.Compile(space, value, null, null); if (ValueType is TypeBinary) { throw new Exception(Name + " Error : value type is binary."); } if (ValueType is TypeDynamic) { throw new Exception(Name + " Error : value type is dynamic."); } }
// /////////////////////////////////////////// public BeanKey(ModuleSpace space, XmlElement self) { Space = space; _name = self.GetAttribute("name").Trim(); Program.CheckReserveName(_name); Type.Add(space, this); space.Add(this); string attr = self.GetAttribute("TypeId"); TypeId = attr.Length > 0 ? int.Parse(attr) : Zeze.Transaction.Bean.Hash64(space.Path(".", _name)); if (false == Program.BeanTypeIdDuplicateChecker.Add(TypeId)) { throw new Exception("duplicate Bean.TypeId, please choice one."); } parse(self); }
public static Type Compile(ModuleSpace space, String name, String key, String value) { Type type = null; if (Types.TryGetValue(name, out type)) { return(type.Compile(space, key, value)); } if (false == Program.IsFullName(name)) { name = space.Path(".", name); if (Types.TryGetValue(name, out type)) { return(type.Compile(space, key, value)); } } throw new Exception("type NOT FOUND! '" + name + "'" + key + "." + value); }
public void Make() { string projectBasedir = Project.Gendir; string projectDir = System.IO.Path.Combine(projectBasedir, Project.Name); string genDir = System.IO.Path.Combine(projectDir, "LuaGen"); string srcDir = System.IO.Path.Combine(projectDir, "LuaSrc"); if (System.IO.Directory.Exists(genDir)) { System.IO.Directory.Delete(genDir, true); } HashSet <ModuleSpace> allRefModules = new HashSet <ModuleSpace>(); foreach (Module mod in Project.AllModules) { allRefModules.Add(mod); } System.IO.Directory.CreateDirectory(genDir); string metaFileName = System.IO.Path.Combine(genDir, "ZezeMeta.lua"); using System.IO.StreamWriter swMeta = new System.IO.StreamWriter(metaFileName, false, Encoding.UTF8); swMeta.WriteLine("-- auto-generated"); swMeta.WriteLine("local meta = {}"); swMeta.WriteLine("meta.beans = {}"); foreach (Types.BeanKey beanKey in Project.AllBeanKeys) { allRefModules.Add(beanKey.Space); BeanFormatter.MakeMeta(beanKey.Space.PathPinyin("_", beanKey.NamePinyin), beanKey.TypeId, beanKey.Variables, swMeta); } foreach (Types.Bean bean in Project.AllBeans) { allRefModules.Add(bean.Space); BeanFormatter.MakeMeta(bean.Space.PathPinyin("_", bean.NamePinyin), bean.TypeId, bean.Variables, swMeta); } swMeta.WriteLine(); swMeta.WriteLine("meta.protocols = {}"); foreach (Protocol protocol in Project.AllProtocols) { allRefModules.Add(protocol.Space); if (protocol is Rpc rpc) { swMeta.WriteLine($"meta.protocols[{protocol.TypeId}] = {{ {GetBeanTypeId(rpc.ArgumentType)}, {GetBeanTypeId(rpc.ResultType)} }}"); continue; } swMeta.WriteLine($"meta.protocols[{protocol.TypeId}] = {{ {GetBeanTypeId(protocol.ArgumentType)} }}"); } swMeta.WriteLine(); swMeta.WriteLine("return meta"); swMeta.Close(); /* * foreach (Service ma in Project.Services.Values) * { * new ServiceFormatter(ma, genDir, srcDir).Make(); * } */ SortedDictionary <int, List <ModuleSpace> > sortDepth = new SortedDictionary <int, List <ModuleSpace> >(); ModuleSpace depth0 = null; foreach (ModuleSpace mod in allRefModules) { int depth = mod.PathDepth(); if (false == sortDepth.TryGetValue(depth, out var mods)) { sortDepth.Add(depth, mods = new List <ModuleSpace>()); } mods.Add(mod); if (depth == 0) { depth0 = mod; // 记住 solution 最后生成。 continue; } new ModuleFormatter(Project, mod, genDir, srcDir).Make(); } { ModuleSpace solution = Project.Solution; using System.IO.StreamWriter sw = new System.IO.StreamWriter(System.IO.Path.Combine(genDir, solution.Name + ".lua"), false, Encoding.UTF8); if (null != depth0) // 引用了solution内定义的bean,先调用ModuleFormatter生成 { new ModuleFormatter(Project, solution, genDir, srcDir).MakeGen(sw); } else { sw.WriteLine("-- auto-generated"); sw.WriteLine(); sw.WriteLine("local " + solution.Name + " = {}"); //sw.WriteLine("" + module.Name + ".ModuleId = " + module.Id); sw.WriteLine(); } sw.WriteLine(""); foreach (var es in sortDepth) { if (es.Key == 0) { continue; // solution 已经生成了。 } foreach (var e in es.Value) { sw.WriteLine($"{e.Path(".", null)} = require '{e.Path(".", null)}'"); } } sw.WriteLine(""); sw.WriteLine($"return {solution.Name}"); } string dispatcherFileName = System.IO.Path.Combine(srcDir, "Zeze.lua"); if (false == System.IO.File.Exists(dispatcherFileName)) { using System.IO.StreamWriter swDispatcher = new System.IO.StreamWriter(dispatcherFileName, false, Encoding.UTF8); swDispatcher.WriteLine(""); swDispatcher.WriteLine("local Zeze = { }"); swDispatcher.WriteLine("Zeze.ProtocolHandles = { }"); swDispatcher.WriteLine("Zeze.RpcContext = { }"); swDispatcher.WriteLine("Zeze.RpcSidSeed = 1"); swDispatcher.WriteLine(""); swDispatcher.WriteLine("function ZezeDispatchRequest(p)"); swDispatcher.WriteLine(" local handle = Zeze.ProtocolHandles[p.TypeId]"); swDispatcher.WriteLine(" if (nil == handle) then"); swDispatcher.WriteLine(" return 0"); swDispatcher.WriteLine(" end"); swDispatcher.WriteLine(" handle(p)"); swDispatcher.WriteLine(" return 1-- 1 if found.not result of handle"); swDispatcher.WriteLine("end"); swDispatcher.WriteLine(""); swDispatcher.WriteLine("function ZezeDispatchProtocol(p)"); swDispatcher.WriteLine(" if (p.IsRpc) then"); swDispatcher.WriteLine(" if (p.IsRequest) then"); swDispatcher.WriteLine(" return ZezeDispatchRequest(p)"); swDispatcher.WriteLine(" end"); swDispatcher.WriteLine(" local ctx = Zeze.RpcContext.remove(p.Sid)"); swDispatcher.WriteLine(" if (nil == ctx) then"); swDispatcher.WriteLine(" return 1 -- success"); swDispatcher.WriteLine(" end"); swDispatcher.WriteLine(" ctx.IsRequest = false"); swDispatcher.WriteLine(" if (p.IsTimeout ~= true) then"); swDispatcher.WriteLine(" ctx.Result = p.Result"); swDispatcher.WriteLine(" ctx.ResultCode = p.ResultCode"); swDispatcher.WriteLine(" ctx.SessionId = p.SessionId"); swDispatcher.WriteLine(" ctx.Service = p.Service"); swDispatcher.WriteLine(" end"); swDispatcher.WriteLine(" ctx.HandleResult(ctx)"); swDispatcher.WriteLine(" return 1 -- 1 if found.not result of handle"); swDispatcher.WriteLine(" end"); swDispatcher.WriteLine(" return ZezeDispatchRequest(p)"); swDispatcher.WriteLine("end"); swDispatcher.WriteLine(""); swDispatcher.WriteLine("function ZezeSocketClose(service, sessionId)"); swDispatcher.WriteLine(" print('ZezeSocketClose')"); swDispatcher.WriteLine("end"); swDispatcher.WriteLine(""); swDispatcher.WriteLine("function ZezeSendRpc(service, session, r, functionHandleResult)"); swDispatcher.WriteLine(" r.IsRequest = true"); swDispatcher.WriteLine(" r.Service = service"); swDispatcher.WriteLine(" r.SessionId = session"); swDispatcher.WriteLine(" r.HandleResult = functionHandleResult"); swDispatcher.WriteLine(" r.Sid = Zeze.RpcSidSeed"); swDispatcher.WriteLine(" Zeze.RpcSidSeed = Zeze.RpcSidSeed + 1"); swDispatcher.WriteLine(" Zeze.RpcContext[r.Sid] = r"); swDispatcher.WriteLine(" ZezeSendProtocol(service, session, r)"); swDispatcher.WriteLine("end"); swDispatcher.WriteLine(""); swDispatcher.WriteLine("function ZezeSendRpcResult(r)"); swDispatcher.WriteLine(" r.IsRequest = false"); swDispatcher.WriteLine(" -- r.Sid same as request"); swDispatcher.WriteLine(" ZezeSendProtocol(r.Service, r.SessionId, r)"); swDispatcher.WriteLine("end"); swDispatcher.WriteLine(""); swDispatcher.WriteLine("function ZezeHandshakeDone(service, sessionId)"); swDispatcher.WriteLine(" Zeze.CurrentService = service"); swDispatcher.WriteLine(" Zeze.CurrentSessionId = sessionId"); swDispatcher.WriteLine(" -- connection ready. write you code here."); swDispatcher.WriteLine("end"); swDispatcher.WriteLine(""); swDispatcher.WriteLine("return Zeze"); } }
public Tick(float tempo, int ticksPerBeat) { this.Tempo = tempo; this.ticksPerBeat = ticksPerBeat; ModuleSpace.AddTick(this); }
// /////////////////////////////////////////// public Bean(ModuleSpace space, XmlElement self) { Space = space; _name = self.GetAttribute("name").Trim(); Program.CheckReserveName(_name); Type.Add(space, this); space.Add(this); // previous sibling comment Comment = self.GetAttribute("comment"); string attr = self.GetAttribute("TypeId"); TypeId = attr.Length > 0 ? int.Parse(attr) : Zeze.Transaction.Bean.Hash64(space.Path(".", _name)); if (false == Program.BeanTypeIdDuplicateChecker.Add(TypeId)) { throw new Exception("duplicate Bean.TypeId, please choice one."); } if (Comment.Length == 0) { for (XmlNode c = self.PreviousSibling; null != c; c = c.PreviousSibling) { if (XmlNodeType.Element == c.NodeType) { break; } if (XmlNodeType.Comment == c.NodeType) { Comment = c.InnerText.Trim(); break; } } } XmlNodeList childNodes = self.ChildNodes; foreach (XmlNode node in childNodes) { if (XmlNodeType.Element != node.NodeType) { continue; } XmlElement e = (XmlElement)node; String nodename = e.Name; switch (e.Name) { case "variable": Add(new Variable(this, e)); break; case "enum": Add(new Enum(e)); break; default: throw new Exception("node=" + nodename); } } }
public override Type Compile(ModuleSpace space, String key, String value) { return(new TypeMap(space, key, value)); }
public abstract Type Compile(ModuleSpace space, String key, String value);
private static void NoiseSample() { var noise = Noise(); using (ModuleSpace.Play((noise * 0.125f).AsFloat())) Console.ReadKey(); }