示例#1
0
        public static string GetText(this CodeClass2 cls, vsCMPart part = vsCMPart.vsCMPartWholeWithAttributes)
        {
            try {
                var startPoint = cls.GetStartPoint(part);
                if (startPoint == null)
                {
                    return("");
                }

                var endPoint = cls.GetEndPoint(part);

                return(endPoint == null ? "" : startPoint.CreateEditPoint().GetText(endPoint));
            }
            catch (NotImplementedException e) {
                //catch random errors when trying to get start / end point
                Console.WriteLine(e.ToString());
                return("");
            }
        }
示例#2
0
        public void WritePropertyNames(CodeClass2 cls)
        {
            var props  = cls.GetProperties().Select(p => p.ToPropertyInfo()).ToArray();
            var output = new System.IO.StringWriter();

            GenEnum(output, props, true);

            var writer = Manager.CreateWriter();

            writer.Class       = cls;
            writer.SegmentType = SegmentTypes.Region;
            writer.SearchStart = cls.StartPoint;
            writer.SearchEnd   = cls.EndPoint;
            writer.OptionTag   = new ManagerType.OptionTag()
            {
                Version = Version, RegenMode = RegenModes.Always, Trigger = new TriggerInfo(TriggerTypes.CodeSnippet)
            };
            writer.Content     = output.ToString();
            writer.InsertStart = cls.GetStartPoint(vsCMPart.vsCMPartBody);
            writer.InsertOrReplace();
        }
        private void WriteGetbytypeMethod(CodeClass2 cls)
        {
            var sets = GetExistingSets(cls);

            var code = General.GetTemplateOutput(output => GenGetbytype(output, sets));
            var insertPoint = cls.GetStartPoint(vsCMPart.vsCMPartBody);
            var manager = new Manager<DbSet, GeneratorOptionAttribute>(TagFormat.Json);
            var writer = manager.CreateWriter(cls);
            writer.OptionTag.Trigger.Type = TriggerTypes.CodeSnippet;
            writer.InsertStart = insertPoint;
            writer.TargetRange = new TaggedRange() {StartPoint = cls.StartPoint, EndPoint = cls.EndPoint};
            writer.Content = code;
            writer.TagNote = "GetDbsetByType";
            writer.InsertOrReplace(true);
        }
 private void WriteDbset(CodeClass2 targetContext, IEnumerable<DbsetCandidate> selections)
 {
     var code =General.GetTemplateOutput(output => GenDbset(output,selections));
         var insertPoint =targetContext.GetStartPoint(vsCMPart.vsCMPartBody);
     insertPoint.InsertAndFormat(code);
 }
        public static void Excute(CodeClass2 classElement, Dictionary<string, CodeClass2> dic, TyrantVSPackage package)
        {
            if (classElement == null)
                return;

            //var classCode = classElement.StartPoint.CreateEditPoint().GetText(classElement.GetEndPoint());
            var validBases = new List<CodeElement>();
            foreach (CodeElement baseClass in classElement.Bases)
            {
                if (!baseClass.FullName.StartsWith("Tyrant.GameCore.Message<") && baseClass.FullName != typeof(object).FullName)
                    validBases.Add(baseClass);
            }
            var validInterfaces = new List<CodeElement>();
            foreach (CodeElement implementedInterface in classElement.ImplementedInterfaces)
            {
                if (implementedInterface.FullName != "Tyrant.GameCore.ISerializable")
                {
                    validInterfaces.Add(implementedInterface);
                    break;
                }
            }

            var baseCollection = classElement.Collection;
            string name = classElement.Name;
            classElement.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint().ReplaceText(classElement.GetStartPoint(vsCMPart.vsCMPartBody), $"public partial class {name}{Environment.NewLine}{{{Environment.NewLine}", (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
            classElement = baseCollection.Item(name) as CodeClass2;
            classElement.AddImplementedInterface("Tyrant.GameCore.ISerializable");
            foreach (CodeElement baseClass in validBases)
            {
                try
                {
                    classElement.AddBase(baseClass, -1);
                }
                catch { }
            }
            foreach (CodeElement implementedInterface in validInterfaces)
            {
                try
                {
                    classElement.AddImplementedInterface(implementedInterface, -1);
                }
                catch { }
            }

            CommunicationCodeHelper.RemoveMembers(classElement);

            CodeFunction2 serializeMethod = null, deserializeMethod = null;
            try
            {
                classElement.AddFunction("Tyrant.GameCore.ISerializable.Serialize", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessDefault);
            }
            catch { }
            serializeMethod = classElement.Members.Item("Tyrant.GameCore.ISerializable.Serialize") as CodeFunction2;
            try
            {
                serializeMethod.AddParameter("dest", typeof(ProtoBuf.ProtoWriter).FullName);
            }
            catch
            {
                serializeMethod = classElement.Members.Item("Tyrant.GameCore.ISerializable.Serialize") as CodeFunction2;
            }
            try
            {
                classElement.AddFunction("Tyrant.GameCore.ISerializable.Deserialize", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessDefault);
            }
            catch { }
            deserializeMethod = classElement.Members.Item("Tyrant.GameCore.ISerializable.Deserialize") as CodeFunction2;
            try
            {
                deserializeMethod.AddParameter("source", typeof(ProtoBuf.ProtoReader).FullName);
            }
            catch
            {
                deserializeMethod = classElement.Members.Item("Tyrant.GameCore.ISerializable.Deserialize") as CodeFunction2;
            }
            CommunicationCodeHelper.GenerateSerializaCode(classElement, serializeMethod, deserializeMethod, dic, package/*, "dest", "source"*/);
        }
        public static void Excute(CodeClass2 classElement, Dictionary<string, CodeClass2> dic, TyrantVSPackage package)
        {
            if (classElement == null)
                return;

            string callBackGenericParamaName = null;
            foreach (CodeElement baseClass in classElement.Bases)
            {
                string baseFullName = baseClass.FullName;
                if (baseFullName.StartsWith("Tyrant.GameCore.Message<"))
                {
                    Stack<int> ind = new Stack<int>();
                    int thirdGenericParama = -1, find = 0;
                    int first = baseFullName.IndexOf('<');
                    for (int i = first + 1; i < baseFullName.Length; ++i)
                    {
                        char c = baseFullName[i];
                        if (c == '<')
                            ind.Push(i);
                        else if (c == ',')
                        {
                            if (ind.Count <= 0)
                            {
                                ++find;
                                if (find == 2)
                                    thirdGenericParama = i + 1;
                            }
                        }
                        else if (c == '>')
                        {
                            if (ind.Count > 0)
                                ind.Pop();
                            else
                            {
                                if (thirdGenericParama > 0 && thirdGenericParama < baseFullName.Length)
                                    callBackGenericParamaName = baseFullName.Substring(thirdGenericParama, i - 1 - thirdGenericParama).Trim();
                                break;
                            }
                        }
                    }
                }
            }

            var validInterfaces = new List<CodeElement>();
            foreach (CodeElement implementedInterface in classElement.ImplementedInterfaces)
            {
                if (implementedInterface.FullName != "Tyrant.GameCore.ISerializable")
                {
                    validInterfaces.Add(implementedInterface);
                    break;
                }
            }

            var name = classElement.Name;
            var fullName = classElement.FullName;
            var collection = classElement.Collection;
            if (!string.IsNullOrEmpty(callBackGenericParamaName))
            {
                int first = callBackGenericParamaName.IndexOf('<');
                if (first >= 0)
                    callBackGenericParamaName = callBackGenericParamaName.Substring(0, first);
                callBackGenericParamaName = $", {callBackGenericParamaName}<TUser>";
            }
            else
                callBackGenericParamaName = "";
            classElement.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint().ReplaceText(classElement.GetStartPoint(vsCMPart.vsCMPartBody), $"public partial class {name}<TUser> : Tyrant.GameCore.Message<TUser, {name}<TUser>{callBackGenericParamaName}> where TUser : Tyrant.GameCore.IUser{Environment.NewLine}{{{Environment.NewLine}", (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
            classElement = collection.Item(name) as CodeClass2;

            foreach (CodeElement implementedInterface in validInterfaces)
            {
                try
                {
                    classElement.AddImplementedInterface(implementedInterface, -1);
                }
                catch { }
            }

            CommunicationCodeHelper.RemoveMembers(classElement);

            CodeFunction2 serializeMethod = null, deserializeMethod = null;
            try
            {
                classElement.AddFunction("Serialize", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessProtected);
            }
            catch { }
            serializeMethod = classElement.Members.Item("Serialize") as CodeFunction2;
            if (serializeMethod == null)
            {
                return;
            }
            serializeMethod.OverrideKind = vsCMOverrideKind.vsCMOverrideKindOverride;
            try
            {
                serializeMethod.AddParameter("dest", typeof(ProtoBuf.ProtoWriter).FullName);
            }
            catch
            {
                serializeMethod = classElement.Members.Item("Serialize") as CodeFunction2;
            }
            try
            {
                classElement.AddFunction("Deserialize", vsCMFunction.vsCMFunctionFunction, vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessProtected);
            }
            catch { }
            deserializeMethod = classElement.Members.Item("Deserialize") as CodeFunction2;
            if (deserializeMethod == null)
            {
                return;
            }
            deserializeMethod.OverrideKind = vsCMOverrideKind.vsCMOverrideKindOverride;
            try
            {
                deserializeMethod.AddParameter("source", typeof(ProtoBuf.ProtoReader).FullName);
            }
            catch
            {
                deserializeMethod = classElement.Members.Item("Deserialize") as CodeFunction2;
            }
            CommunicationCodeHelper.GenerateSerializaCode(classElement, serializeMethod, deserializeMethod, dic, package/*, "dest", "source"*/);
        }
        public void GenerateClass()
        {
            if (classFile == null)
            {
                return;
            }

            ImportModelSourceToContentProject();

            StringBuilder sb       = new StringBuilder();
            string        variable = sb.
                                     Append("Camera camera;\r\n").
                                     Append("Terrain terrain;\r\n").
                                     Append("Texture2D heightMap;\r\n").
                                     Append("World world;\r\n").
                                     Append("Grid grid;\r\n").
                                     ToString();

            sb.Clear();
            Vector3 cameraPos = mapModel.MainCamera.Position;
            Vector3 cameraRot = mapModel.MainCamera.EulerRotation;

            sb.
            Append("CollisionSystem collisionSystem = new CollisionSystemPersistentSAP();\r\n").
            Append("world = new World(collisionSystem);\r\n").
            Append("world.AllowDeactivation = true;\r\n").
            Append("world.Gravity = new JVector(0, ").Append(mapModel.PhysicsWorld.Gravity).Append("f, 0);\r\n").
            Append("world.ContactSettings.MaterialCoefficientMixing = ContactSettings.MaterialCoefficientMixingType.").Append(mapModel.PhysicsWorld.MaterialCoefficientMixing.ToString()).Append(";\r\n").
            Append("collisionSystem.CollisionDetected += new CollisionDetectedHandler(collisionSystem_CollisionDetected);\r\n\r\n").
            Append("camera = new Camera(this);\r\n").
            Append("camera.Name = \"camera\";\r\n").
            Append("camera.Position = new Vector3(").Append(cameraPos.X).Append("f, ").Append(cameraPos.Y).Append("f, ").Append(cameraPos.Z).Append("f);\r\n").
            Append("camera.EulerRotation = new Vector3(").Append(cameraRot.X).Append("f, ").Append(cameraRot.Y).Append("f, ").Append(cameraRot.Z).Append("f);\r\n");
            foreach (EditorModel.PropertyModel.Script script in mapModel.MainCamera.Scripts)
            {
                sb.Append("camera.AddScript(new ").Append(System.IO.Path.GetFileNameWithoutExtension(script.Name)).Append("());\r\n");
            }
            sb.Append("Components.Add(camera);\r\n\r\n");
            string constructor = sb.ToString();

            sb.Clear();
            sb.
            Append("heightMap = Content.Load<Texture2D>(\"" + heightMapAsset + "\");\r\n").
            Append("terrain = new Terrain(GraphicsDevice, camera, heightMap, this, world);\r\n").
            Append("terrain.Texture = Content.Load<Texture2D>(\"" + textureAsset + "\");\r\n").
            Append("Components.Add(terrain);\r\n\r\n").
            Append("grid = new Grid(this, terrain, 8, camera, GraphicsDevice, new BasicEffect(GraphicsDevice), world);\r\n").
            Append("grid.RoadModel = Content.Load<Model>(\"jalan_raya\");\r\n").
            Append("grid.RoadModel_belok = Content.Load<Model>(\"jalan_raya_belok\");\r\n").
            Append("grid.GridMap = Content.Load<Texture2D>(\"gridmap_Game2\");\r\n").
            Append("grid.ImportGridMap();\r\n\r\n");
            string loadContent = sb.ToString();

            sb.Clear();
            string update = sb.
                            Append("float step = (float)gameTime.ElapsedGameTime.TotalSeconds;\r\n").
                            Append("if (step > 1.0f / 100.0f) step = 1.0f / 100.0f;\r\n").
                            Append("world.Step(step, true);\r\n").
                            ToString();

            sb.Clear();
            string draw = sb.
                          ToString();

            sb.Clear();
            foreach (CodeLines codeLines in codeLinesList)
            {
                variable    += codeLines.Code[CodeLines.CodePosition.Variable] + ((codeLines.Code[CodeLines.CodePosition.Variable] != "") ? "\r\n" : "");
                constructor += codeLines.Code[CodeLines.CodePosition.Constructor] + ((codeLines.Code[CodeLines.CodePosition.Constructor] != "") ? "\r\n" : "");
                loadContent += codeLines.Code[CodeLines.CodePosition.LoadContent] + ((codeLines.Code[CodeLines.CodePosition.LoadContent] != "") ? "\r\n" : "");
                draw        += codeLines.Code[CodeLines.CodePosition.Draw] + ((codeLines.Code[CodeLines.CodePosition.Draw] != "") ? "\r\n" : "");
            }

            EditPoint editPoint    = null;
            EditPoint movePoint    = null;
            string    startPattern = "#region XnaLevelEditor";
            string    endPattern   = "#endregion";

            if (codeClass != null)
            {
                editPoint = codeClass.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                editPoint.FindPattern(startPattern, (int)vsFindOptions.vsFindOptionsNone, ref editPoint);
                movePoint = editPoint.CreateEditPoint();
                movePoint.FindPattern(endPattern, (int)vsFindOptions.vsFindOptionsNone);
                editPoint.ReplaceText(movePoint, "\r\n" + variable, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
                movePoint.SmartFormat(movePoint);
            }

            if (constructorFunction != null)
            {
                editPoint = constructorFunction.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                editPoint.FindPattern(startPattern, (int)vsFindOptions.vsFindOptionsNone, ref editPoint);
                movePoint = editPoint.CreateEditPoint();
                movePoint.FindPattern(endPattern, (int)vsFindOptions.vsFindOptionsNone);
                editPoint.ReplaceText(movePoint, "\r\n" + constructor, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
                movePoint.SmartFormat(movePoint);
            }

            if (loadContentFunction != null)
            {
                editPoint = loadContentFunction.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                editPoint.FindPattern(startPattern, (int)vsFindOptions.vsFindOptionsNone, ref editPoint);
                movePoint = editPoint.CreateEditPoint();
                movePoint.FindPattern(endPattern, (int)vsFindOptions.vsFindOptionsNone);
                editPoint.ReplaceText(movePoint, "\r\n" + loadContent, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
                movePoint.SmartFormat(movePoint);
            }

            if (updateFunction != null)
            {
                editPoint = updateFunction.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                editPoint.FindPattern(startPattern, (int)vsFindOptions.vsFindOptionsNone, ref editPoint);
                movePoint = editPoint.CreateEditPoint();
                movePoint.FindPattern(endPattern, (int)vsFindOptions.vsFindOptionsNone);
                editPoint.ReplaceText(movePoint, "\r\n" + update, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
                movePoint.SmartFormat(movePoint);
            }

            if (drawFunction != null)
            {
                editPoint = drawFunction.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
                editPoint.FindPattern(startPattern, (int)vsFindOptions.vsFindOptionsNone, ref editPoint);
                movePoint = editPoint.CreateEditPoint();
                movePoint.FindPattern(endPattern, (int)vsFindOptions.vsFindOptionsNone);
                editPoint.ReplaceText(movePoint, "\r\n" + draw, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
                movePoint.SmartFormat(movePoint);
            }
        }
        public void WritePropertyNames(CodeClass2 cls)
        {
            var props = cls.GetProperties().Select(p => p.ToPropertyInfo()).ToArray();
            var output = new System.IO.StringWriter();
            GenEnum(output, props, true);

            var writer = Manager.CreateWriter() ;
            writer.Class = cls;
            writer.SegmentType = SegmentTypes.Region;
            writer.SearchStart = cls.StartPoint;
            writer.SearchEnd = cls.EndPoint;
            writer.OptionTag = new ManagerType.OptionTag() { Version = Version, RegenMode = RegenModes.Always, Trigger = new TriggerInfo( TriggerTypes.CodeSnippet)};
            writer.Content = output.ToString();
            writer.InsertStart =cls.GetStartPoint(vsCMPart.vsCMPartBody);
            writer.InsertOrReplace();
        }