public static void AddInterfaceIfNotExists(this CodeClass2 cls, string interfaceName) { try { if (!(cls.ImplementedInterfaces.OfType <CodeInterface>().Any(x => x.FullName == interfaceName))) { cls.AddImplementedInterface(interfaceName); } } catch (Exception e) { MessageBox.Show("The added interface has to exists in the project." + e); } }
//public void ShowInClassDesigner( string projectName, string typeName ) //{ // //Project prj = FindProject( projectName ); // //IVsSolution sln = ApplicationModelPackage.Singleton.GetServiceInternal( typeof( IVsSolution ) ) as IVsSolution; // //IVsHierarchy hierarchy; // //ErrorHandler.ThrowOnFailure( sln.GetProjectOfUniqueName( prj.UniqueName, out hierarchy ) ); // //IVsProject3 project2 = hierarchy as IVsProject3; // //ClassDesignerHelper.ShowClassInDiagram( ApplicationModelPackage.Singleton, // // project2, // // typeName ); //} /// <summary> /// Adds the interface if not exists. /// </summary> /// <param name="cc">The cc.</param> /// <param name="interfaceName">Name of the interface.</param> public static void AddInterfaceIfNotExists(CodeClass2 cc, string interfaceName) { foreach (CodeElement elem in cc.ImplementedInterfaces) { if (elem.Name == interfaceName) { return; } } try { cc.AddImplementedInterface(interfaceName, 0); } catch { } }
public ShellCodeInterface AddImplementedInterface(object Base, object Position) { return(new ShellCodeInterface(_codeClass.AddImplementedInterface(Base, Position) as CodeInterface2)); }
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"*/); }