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); } } } }
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(); }