Exemple #1
0
        public static string Gen(string name, string upLevelContext)
        {
            CSharpWriter writer = new CSharpWriter(true);

            writer.Write($"public interface I{name}Component : LiteECS.IComponent");
            writer.EmptyScop();

            writer.Write($"public class {name}Entity : LiteECS.EntityT<I{name}Component>");
            using (new CSharpWriter.Scop(writer))
            {
                writer.Write($"public {name}Entity(LiteECS.Context context, int id) : base(context, id)");
                writer.EmptyScop(false);
            }

            writer.Write($"public static partial class {name}Components");
            using (new CSharpWriter.Scop(writer))
            {
                writer.Write($"public static System.Action<{name}Context> OnContextCreat;").NewLine();
                writer.Write($"public static int ComponentCount {{ get; private set; }}").NewLine();
            }

            writer.Write($"public class {name}Context : LiteECS.ContextT<{name}Entity>");
            using (new CSharpWriter.Scop(writer))
            {
                if (!string.IsNullOrEmpty(upLevelContext))
                {
                    writer.Write($"public {upLevelContext}Context {upLevelContext}{{ get; private set; }}").NewLine();
                }
                //Ctor
                writer.Write($"protected {name}Context(int componentTypeCount) : base(componentTypeCount, CreatFunc)");
                writer.EmptyScop();

                //CreatFunc
                writer.Write($"private static {name}Entity CreatFunc(LiteECS.Context context, int id)");
                using (new CSharpWriter.Scop(writer))
                {
                    writer.Write($"return new {name}Entity(context, id);");
                }

                //Creat
                writer.Write($"public static {name}Context Creat()");
                using (new CSharpWriter.Scop(writer))
                {
                    writer.Write($"var contxt = new {name}Context({name}Components.ComponentCount);").NewLine();
                    writer.Write($"{name}Components.OnContextCreat(contxt);").NewLine();
                    writer.Write("return contxt;");
                }
                if (!string.IsNullOrEmpty(upLevelContext))
                {
                    writer.Write($"public void Set{upLevelContext}( {upLevelContext}Context {upLevelContext.ToLower()} )");
                    using (new CSharpWriter.Scop(writer))
                    {
                        writer.Write($"{upLevelContext} = {upLevelContext.ToLower()};");
                    }
                }
            }


            return(writer.ToString());
        }
Exemple #2
0
 public void Gen(GeneratorFolder folder)
 {
     folder.AddFile($"{context.Name}Components", GenComponentsFile());
     //自动System生成
     foreach (var type in componentTypes)
     {
         var cleanup = type.GetCustomAttribute <LiteECS.CleanupAttribute>();
         if (cleanup == null)
         {
             continue;
         }
         CSharpWriter writer    = new CSharpWriter();
         string       className = $"{type.Name}CleanupSystem";
         writer.Write($"using TComponent = {type.FullName};").NewLine();
         writer.Write($"public class {className} : LiteECS.ICleanupSystem");
         using (new CSharpWriter.Scop(writer))
         {
             if (cleanup.Mode == LiteECS.CleanupMode.DestroyEntity)
             {
                 GenDestroySystem(type, writer, className);
             }
             else
             {
                 GenRemoveSystem(type, writer, className);
             }
         }
         folder.AddFile(className, writer.ToString());
     }
 }
Exemple #3
0
        public static string Gen(string className, string context, ECSSystemGenerateType type, string componentName = null)
        {
            CSharpWriter writer = new CSharpWriter(true);

            if (type < ECSSystemGenerateType.GroupExecute)
            {
                writer.Write($"public class {className} : LiteECS.I{type}System");
                using (new CSharpWriter.Scop(writer))
                {
                    writer.Write($"{context}Context context;").NewLine();
                    writer.Write($"public {className}({context}Context context)");
                    using (new CSharpWriter.Scop(writer))
                    {
                        writer.Write("this.context = context;");
                    }
                    writer.Write($"public void On{type}()");
                    writer.EmptyScop();
                }
            }
            else if (type == ECSSystemGenerateType.GroupExecute)
            {
                GenGroupExecuteSystem(writer, className, context, componentName);
            }
            else if (type == ECSSystemGenerateType.ReactiveExecute)
            {
                GenReactiveExecuteSystem(writer, className, context, componentName);
            }
            return(writer.ToString());
        }
Exemple #4
0
        private string GenComponentsFile()
        {
            CSharpWriter writer = new CSharpWriter();

            writer.Write($"public static partial class {context.Name}Components");
            using (new CSharpWriter.Scop(writer))
            {
                writer.Write($"static {context.Name}Components()");
                using (new CSharpWriter.Scop(writer))
                {
                    writer.Write($"OnContextCreat = DoContentInit;").NewLine();
                    writer.Write($"ComponentCount = {componentTypes.Count};").NewLine();
                    writer.Write("InitComponentsIdentity();");
                }
                writer.Write($"static void InitComponentsIdentity()");
                using (new CSharpWriter.Scop(writer))
                {
                    for (int i = 0; i < componentTypes.Count; ++i)
                    {
                        var type = componentTypes[i];
                        if (typeof(LiteECS.IUnique).IsAssignableFrom(type))
                        {
                            writer.Write($"LiteECS.ComponentIdentity<{type.FullName}>.Unique = true;").NewLine();
                        }
                        writer.Write($"LiteECS.ComponentIdentity<{type.FullName}>.Id = {i};");
                        if (i < componentTypes.Count - 1)
                        {
                            writer.NewLine();
                        }
                    }
                }
                writer.Write($"static void DoContentInit({context.Name}Context context)");
                using (new CSharpWriter.Scop(writer))
                {
                    for (int i = 0; i < componentTypes.Count; ++i)
                    {
                        var type = componentTypes[i];

                        if (typeof(LiteECS.IUnique).IsAssignableFrom(type))
                        {
                            writer.Write($"context.InitUniqueComponentCollector<{type.FullName}>();");
                        }
                        else
                        {
                            writer.Write($"context.InitComponentCollector<{type.FullName}>();");
                        }
                        if (i < componentTypes.Count - 1)
                        {
                            writer.NewLine();
                        }
                    }
                }
            }
            return(writer.ToString());
        }
Exemple #5
0
        private static int Main(string[] args)
        {
            if (args.Length == 0 || args[0] == "-h" || args[0] == "--help")
            {
                Console.WriteLine("usage:  dotnet XcbSharpGen.dll [output] [protocol]*");
                return(0);
            }

            if (args.Length <= 0)
            {
                Console.WriteLine("Need at least an output file and 1 header file.");
                Console.WriteLine("usage:  dotnet XcbSharpGen.dll [output] [protocol]*");
                return(0);
            }

            var headerFiles = args.Skip(1).Select(Path.GetFullPath);
            var w           = new CSharpWriter();

            w.Line("using System;");
            w.Line("using System.Runtime.InteropServices;");
            w.Line("using SMarshal = System.Runtime.InteropServices.Marshal;");
            w.Line();
            w.Line("namespace OpenWindow.Backends.Wayland");
            w.OpenBlock();

            w.Line("// This file was generated from xml XCB header specifications");
            w.Line("// by XcbSharpGen. https://github.com/Jjagg/OpenWindow/tree/master/generators/XcbSharpGen");
            w.Line();

            var importedHeaders = new List <string>();

            foreach (var path in headerFiles)
            {
                Console.WriteLine($"Using protocol at '{path}'.");
                if (!File.Exists(path))
                {
                    Console.WriteLine("Protocol file not found. Exiting...");
                    return(1);
                }

                var doc = XDocument.Load(path);
                ParseHeader(doc, w, importedHeaders);
            }

            var fp = args[0];

            Directory.CreateDirectory(Path.GetDirectoryName(fp));
            File.WriteAllText(fp, w.ToString());
            Console.WriteLine($"Wrote output to '{fp}'.");
            return(0);
        }
Exemple #6
0
        public static void GenerateExterns(IEnumerable <Type> types, string className, string filePath)
        {
            CSharpWriter writer = new CSharpWriter();

            writer.Write($"public static {className}");
            using (new CSharpWriter.Scop(writer))
            {
                foreach (var type in types)
                {
                    GenerateContextExtern(type, writer);
                }
            }
            FileUtil.WriteFileWithCreateFolder(filePath, writer.ToString());
        }
Exemple #7
0
        private static int Main(string[] args)
        {
            if (args.Length < 2 || (args.Length > 0 && (args[0] == "-h" || args[0] == "--help")))
            {
                Console.WriteLine("usage:  dotnet WaylandSharpGen.dll [protocol] [output]");
                return(0);
            }

            if (args.Length == 0)
            {
                Console.WriteLine("No protocol path specified, using default path.");
            }

            var path = args[0];

            Console.WriteLine($"Using protocol at '{path}'.");
            if (!File.Exists(path))
            {
                Console.WriteLine("Protocol file not found. Exiting...");
                return(1);
            }

            var fp = args[1];

            var doc = XDocument.Load(path);
            var w   = new CSharpWriter();

            w.Line("// This file was generated from an xml Wayland protocol specification");
            w.Line("// by WaylandSharpGen. https://github.com/Jjagg/OpenWindow/tree/master/generators/WaylandSharpGen");
            w.Line();
            w.Line("#pragma warning disable CS0649");
            w.Line();
            WriteProtocol(doc, w);

            var dirName = Path.GetDirectoryName(fp);

            if (!string.IsNullOrWhiteSpace(dirName))
            {
                Directory.CreateDirectory(dirName);
            }
            File.WriteAllText(fp, w.ToString());
            Console.WriteLine($"Wrote output to '{fp}'.");
            return(0);
        }
Exemple #8
0
        private void OnWizardCreate()
        {
            var cfg = ECSConfig.Instance.Contexts.Find(it => it.Name == contextNames[selectContextIdx]);

            if (cfg == null)
            {
                EditorUtility.DisplayDialog("错误", "选择的Context不存在,可能被移动位置或者删除\n,请重新创建", "ok");
                return;
            }
            int    typeIdx   = (int)createType;
            string dirPath   = Path.Combine(cfg.DirectoryPath, catalogs[typeIdx][selectCatalogIdx[typeIdx]]);
            string className = nameInput;

            if (createType == CreateType.Component)
            {
                if (!nameInput.StartsWith(cfg.Name))
                {
                    className = $"{cfg.Name}{nameInput}";
                }
            }
            else if (createType == CreateType.System)
            {
                if (!nameInput.StartsWith(cfg.Name))
                {
                    className = $"{cfg.Name}{nameInput}System";
                }
                else
                {
                    className = $"{nameInput}System";
                }
            }
            System.Reflection.Assembly assembly = ECSContextUtils.GetECSAssembly(cfg);
            if (assembly != null)
            {
                if (assembly.GetType(className) != null)
                {
                    EditorUtility.DisplayDialog("错误", $"创建失败,类型 {className} 已存在", "ok");
                    return;
                }
            }
            string filePath = Path.Combine(dirPath, $"{className}.cs");

            if (File.Exists(filePath))
            {
                EditorUtility.DisplayDialog("错误", "创建失败,目标文件已存在", "ok");
                return;
            }
            string content = null;

            if (createType == CreateType.Component)
            {
                CSharpWriter writer = new CSharpWriter(true);
                writer.Write($"public partial class {className} : I{cfg.Name}Component");
                writer.EmptyScop();
                content = writer.ToString();
            }
            else if (createType == CreateType.System)
            {
                string component = null;
                if (systemType >= ECSSystemGenerateType.GroupExecute && componentTypes != null)
                {
                    component = componentTypes[componentSelectIdx];
                }
                content = SystemGenertor.Gen(className, cfg.Name, systemType, component);
            }
            File.WriteAllText(filePath, content, System.Text.Encoding.UTF8);
            AssetDatabase.ImportAsset(filePath);
            if (OpenAfterCreate)
            {
                var obj = AssetDatabase.LoadMainAssetAtPath(filePath);
                if (obj)
                {
                    AssetDatabase.OpenAsset(obj);
                }
            }
        }