public static void GenMatcher(FileGenerator file) { file.AddLine("public sealed partial class GameMatcher"); using (new FileGenerator.Scop(file)) { //AllOf file.AddLine("public static ECSCore.IAllOfMatcher<GameEntity> AllOf(params int[] indices)"); using (new FileGenerator.Scop(file)) { file.AddLine("return ECSCore.Matcher<GameEntity>.AllOf(indices);"); } file.AddLine("public static ECSCore.IAllOfMatcher<GameEntity> AllOf(params ECSCore.IMatcher<GameEntity>[] matchers)"); using (new FileGenerator.Scop(file)) { file.AddLine("return ECSCore.Matcher<GameEntity>.AllOf(matchers);"); } //AnyOf file.AddLine("public static ECSCore.IAnyOfMatcher<GameEntity> AnyOf(params int[] indices)"); using (new FileGenerator.Scop(file)) { file.AddLine("return ECSCore.Matcher<GameEntity>.AnyOf(indices);"); } file.AddLine("public static ECSCore.IAnyOfMatcher<GameEntity> AnyOf(params ECSCore.IMatcher<GameEntity>[] matchers)"); using (new FileGenerator.Scop(file)) { file.AddLine("return ECSCore.Matcher<GameEntity>.AnyOf(matchers);"); } } }
private static void GenEntityIndex(FileGenerator file, List <ComonentInfo> list, bool isView) { foreach (var component in list) { if (component.IsUnique) { continue; } foreach (var field in component.Fields) { if (field.IndexType == ComonentInfo.EntityIndexType.None) { continue; } if (field.IndexType == ComonentInfo.EntityIndexType.Index) { file.AddFormat("AddEntityIndex(new ECSCore.EntityIndex<GameEntity, {0}>(", field.TypeName); } else { file.AddFormat("AddEntityIndex(new ECSCore.PrimaryEntityIndex<GameEntity, {0}>(", field.TypeName); } file.AddFormat(" {0}_{1},", component.ShowName, field.Name); file.AddFormat(" GetGroup({0}Matcher.{1}),", isView ? "View" : "Game", component.ShowName); file.AddFormat(" (e, c) => (({0})c).{1}));", component.FullName, field.Name); } } }
private static void GenFlagComponent(ComonentInfo info, FileGenerator file, bool isView) { file.AddFormat("static readonly {0} {1} = new {0}();", info.FullName, LowerFirstCase(info.FullName)); string lookupName = string.Format("{0}ComponentsLookup.{1}", isView ? "View" : "Game", info.ShowName); file.AddFormat("public bool is{0}", info.ShowName); using (new FileGenerator.Scop(file)) { file.AddFormat("get {0} return HasComponent({2}); {1}", "{", "}", lookupName); file.AddLine("set"); using (new FileGenerator.Scop(file)) { file.AddFormat("if (value != is{0})", info.ShowName); using (new FileGenerator.Scop(file)) { file.AddFormat("var index = {0};", lookupName); file.AddLine("if (value)"); using (new FileGenerator.Scop(file)) { file.AddLine("var componentPool = GetComponentPool(index);"); file.AddLine("var component = componentPool.Count > 0 ? componentPool.Pop() : blockMoveComponent;"); file.AddLine("AddComponent(index, component);"); } file.AddLine("else"); using (new FileGenerator.Scop(file)) { file.AddLine("RemoveComponent(index);"); } } } } }
public static void GenAssignment(ComonentInfo info, FileGenerator file) { foreach (var field in info.Fields) { file.AddFormat("component.{0} = new{0};", field.Name); } }
private FileGenerator CreateFile(string name, string path) { if (!genFiles.TryGetValue(path, out var files)) { files = new List <FileGenerator>(); genFiles.Add(path, files); } FileGenerator file = new FileGenerator(name + ".cs"); files.Add(file); return(file); }
private static void GenNormalComponent(ComonentInfo info, FileGenerator file, bool isView) { string lookupName = string.Format("{0}ComponentsLookup.{1}", isView ? "View" : "Game", info.ShowName); file.AddFormat("{2} {3} {0} get {0} return ({2})GetComponent({4}); {1} {1}" , "{", "}" , info.FullName, LowerFirstCase(info.ShowName) , lookupName); file.AddFormat("public bool has{2} {0} get {0} return HasComponent({3});{1} {1}" , "{", "}" , info.ShowName, lookupName); file.AddLine(); //AddComponent file.AddFormat("public void Add{0}({1})", info.ShowName, GenParamList(info)); using (new FileGenerator.Scop(file)) { file.AddFormat("var index = {0};", lookupName); file.AddFormat("var component = CreateComponent<{0}>(index);", info.FullName); GenAssignment(info, file); file.AddLine("AddComponent(index, component);"); } file.AddLine(); //ReplaceComponent file.AddFormat("public void Replace{0}({1})", info.ShowName, GenParamList(info)); using (new FileGenerator.Scop(file)) { file.AddFormat("var index = {0};", lookupName); file.AddFormat("{0} component = null;", info.FullName); file.AddLine("if (HasComponent(index))"); using (new FileGenerator.Scop(file)) { file.AddFormat("component = ({0})GetComponent(index);", info.FullName); } file.AddLine("else"); using (new FileGenerator.Scop(file)) { file.AddFormat("component = CreateComponent<{0}>(index);", info.FullName); } GenAssignment(info, file); file.AddLine("ReplaceComponent(index, component);"); } file.AddLine(); //RemoveComponent file.AddFormat("public void Remove{0}()", info.ShowName); using (new FileGenerator.Scop(file)) { file.AddFormat("RemoveComponent({0});", lookupName); } }
public static void Gen(List <ComonentInfo> list, FileGenerator file, bool isView) { if (isView) { file.AddLine("public partial class ViewContext"); } else { file.AddLine("public partial class GameContext"); } using (new FileGenerator.Scop(file)) { foreach (var info in list) { if (!info.IsUnique) { continue; } if (info.Fields.Count > 0) { string memberName = ComponentGenerator.LowerFirstCase(info.ShowName); //member file.AddFormat("public {2} {3} {0}get; private set;{1}", "{", "}", info.FullName, memberName); //set file.AddFormat("public void Set{0}({1})", info.ShowName, ComponentGenerator.GenParamList(info)); using (new FileGenerator.Scop(file)) { file.AddFormat("if({0} == null)", memberName); using (new FileGenerator.Scop(file)) { file.AddFormat("{0} = new {1}();", memberName, info.FullName); } file.AddFormat("var component = {0};", memberName); ComponentGenerator.GenAssignment(info, file); } //remove file.AddFormat("public void Remove{0}()", info.ShowName); using (new FileGenerator.Scop(file)) { file.AddFormat("{0} = null;", memberName); } } else { file.AddFormat("public bool is{2} {0}get; set;{1}", "{", "}", info.ShowName); } } } }
public static void GenGameContext(FileGenerator file, ComponentList components) { file.AddLine("public partial class GameEntity : ECSCore.Entity"); file.BeginScop(); file.EndScop(); file.AddLine(); file.AddLine("public partial class GameContext : ECSCore.Context<GameEntity>"); using (new FileGenerator.Scop(file)) { GenEntityIndexKey(file, components.GameComponents); file.AddLine("public GameContext(int totalComponents, string[] names, System.Type[] types, string name)"); file.AddLine(" : base(totalComponents, 0, new ECSCore.ContextInfo(name, names, types),"); file.AddLine(" (entity) =>\n#if (!UNITY_EDITOR)"); file.AddLine(" new UnsafeAERC(),\n#else"); file.AddLine(" new ECSCore.SafeAERC(entity),\n#endif"); file.AddLine(" () => new GameEntity())"); using (new FileGenerator.Scop(file)) { file.AddLine("InitializeEntityIndices();"); } file.AddLine(); file.AddLine("public GameContext(int totalComponents, string[] names, System.Type[] types)"); file.AddLine(" : this(totalComponents, names, types, \"Game\")"); file.BeginScop(); file.EndScop(); file.AddLine(); file.AddLine("public GameContext()"); file.AddLine(" : this(GameComponentsLookup.TotalComponents, GameComponentsLookup.componentNames, GameComponentsLookup.componentTypes, \"Game\")"); file.BeginScop(); file.EndScop(); file.AddLine(); file.AddLine("protected virtual void InitializeEntityIndices()"); using (new FileGenerator.Scop(file)) { GenEntityIndex(file, components.GameComponents, false); } file.AddLine(); GenGetEntityIndex(file, components.GameComponents, false); } GenMatcher(file); }
private static void GenEntityIndexKey(FileGenerator file, List <ComonentInfo> list) { foreach (var component in list) { if (component.IsUnique) { continue; } foreach (var field in component.Fields) { if (field.IndexType == ComonentInfo.EntityIndexType.None) { continue; } file.AddFormat("public const string {0}_{1} = \"{0}.{1}\";", component.ShowName, field.Name); } } }
public static void GenMatcher(ComonentInfo info, FileGenerator file, bool isView) { string lookupName = string.Format("{0}ComponentsLookup.{1}", isView ? "View" : "Game", info.ShowName); if (isView) { file.AddLine("public sealed partial class ViewMatcher"); } else { file.AddLine("public sealed partial class GameMatcher"); } using (new FileGenerator.Scop(file)) { file.AddFormat("static ECSCore.IMatcher<GameEntity> _matcher{0};", info.ShowName); file.AddLine(); file.AddFormat("public static ECSCore.IMatcher<GameEntity> {0}", info.ShowName); using (new FileGenerator.Scop(file)) { file.AddLine("get"); using (new FileGenerator.Scop(file)) { file.AddFormat("if (_matcher{0} == null)", info.ShowName); using (new FileGenerator.Scop(file)) { file.AddFormat("var matcher = (ECSCore.Matcher<GameEntity>)ECSCore.Matcher<GameEntity>.AllOf({0});", lookupName); if (isView) { file.AddLine("matcher.componentNames = ViewComponentsLookup.componentNames;"); } else { file.AddLine("matcher.componentNames = GameComponentsLookup.componentNames;"); } file.AddFormat("_matcher{0} = matcher;", info.ShowName); } file.AddFormat("return _matcher{0};", info.ShowName); } } } }
public static void GenViewContext(FileGenerator file, ComponentList components) { file.AddLine("public partial class ViewContext : GameContext"); using (new FileGenerator.Scop(file)) { GenEntityIndexKey(file, components.ViewComponents); file.AddLine("public ViewContext()"); file.AddLine(" : base(ViewComponentsLookup.TotalComponents, ViewComponentsLookup.componentNames, ViewComponentsLookup.componentTypes, \"View\")"); file.BeginScop(); file.EndScop(); file.AddLine(); file.AddLine("protected override void InitializeEntityIndices()"); using (new FileGenerator.Scop(file)) { file.AddLine("base.InitializeEntityIndices();"); GenEntityIndex(file, components.ViewComponents, true); } file.AddLine(); GenGetEntityIndex(file, components.ViewComponents, true); } }
public static void Gen(ComonentInfo info, FileGenerator file, bool isView) { if (info.IsUnique) { return; } file.AddLine("public partial class GameEntity"); using (new FileGenerator.Scop(file)) { if (info.Fields.Count > 0) { GenNormalComponent(info, file, isView); } else { GenFlagComponent(info, file, isView); } } GenMatcher(info, file, isView); }
private static void GenGetEntityIndex(FileGenerator file, List <ComonentInfo> list, bool isView) { foreach (var component in list) { if (component.IsUnique) { continue; } foreach (var field in component.Fields) { if (field.IndexType == ComonentInfo.EntityIndexType.None) { continue; } if (field.IndexType == ComonentInfo.EntityIndexType.Index) { string paramName = ComponentGenerator.LowerFirstCase(field.Name); file.AddFormat("public System.Collections.Generic.HashSet<GameEntity> GetEntitiesWith{0}{1}({2} {3})", component.ShowName, field.Name, field.TypeName, paramName); using (new FileGenerator.Scop(file)) { file.AddFormat("return ((ECSCore.EntityIndex<GameEntity, {0}>)GetEntityIndex({1}_{2})).GetEntities({3});", field.TypeName, component.ShowName, field.Name, paramName); } } else { string paramName = ComponentGenerator.LowerFirstCase(field.Name); file.AddFormat("public GameEntity GetEntityWith{0}{1}({2} {3})", component.ShowName, field.Name, field.TypeName, paramName); using (new FileGenerator.Scop(file)) { file.AddFormat("return ((ECSCore.PrimaryEntityIndex<GameEntity, {0}>)GetEntityIndex({1}_{2})).GetEntity({3});", field.TypeName, component.ShowName, field.Name, paramName); } } } } }
public void Gen() { { FileGenerator contentFile = CreateFile("GameContext", gamePath); ContextGenerator.GenGameContext(contentFile, componentList); FileGenerator lookupfile = CreateFile("GameComponentsLookup", gamePath); LookupGenerator.Gen(componentList, lookupfile, false); } { FileGenerator contentFile = CreateFile("ViewContext", viewPath); ContextGenerator.GenViewContext(contentFile, componentList); FileGenerator lookupfile = CreateFile("ViewComponentsLookup", viewPath); LookupGenerator.Gen(componentList, lookupfile, true); } { if (componentList.GameComponents.Exists(obj => obj.IsUnique)) { var file = CreateFile("GameUniqueComponent", gamePath); UniqueGenerator.Gen(componentList.GameComponents, file, false); } if (componentList.ViewComponents.Exists(obj => obj.IsUnique)) { var file = CreateFile("ViewUniqueComponent", viewPath); UniqueGenerator.Gen(componentList.ViewComponents, file, true); } } foreach (var info in componentList.GameComponents) { if (!info.IsUnique) { var file = CreateFile(string.Format("{0}{1}", "Components/Game", info.ShowName), gamePath); ComponentGenerator.Gen(info, file, false); } } foreach (var info in componentList.ViewComponents) { if (!info.IsUnique) { var file = CreateFile(string.Format("{0}{1}", "Components/View", info.ShowName), viewPath); ComponentGenerator.Gen(info, file, true); } } //write file var utf8WithoutBom = new System.Text.UTF8Encoding(false); foreach (var kv in genFiles) { foreach (var file in kv.Value) { string path = kv.Key + file.Name; string dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string content = file.ToString(); if (File.Exists(path) && content == File.ReadAllText(path, utf8WithoutBom)) { continue; } File.WriteAllText(path, content, utf8WithoutBom); } } DeleteUnUsedFile(); }
void IDisposable.Dispose() { fileGenerator.EndScop(WithSemicolons); fileGenerator = null; }
public Scop(FileGenerator generator, bool semicolons = false) { fileGenerator = generator; WithSemicolons = semicolons; fileGenerator.BeginScop(); }