diff options
| author | jpbruyere <jp.bruyere@hotmail.com> | 2015-12-23 12:41:41 +0100 |
|---|---|---|
| committer | jpbruyere <jp.bruyere@hotmail.com> | 2015-12-23 12:41:41 +0100 |
| commit | 8678a1351e90c94741661a83efefdce8e7493b01 (patch) | |
| tree | 749ec237dcc7a1b93e15b0cb9256b93079c26e31 /src/Interface.cs | |
| parent | b23b66e6da5f4d355a0424997b4b40e06c078c6c (diff) | |
Group layouting debug (x positionning of children sized with fit)
Prevent multiple loading of template and default values while creating XmlSerializer(type)
Removed DynamicClass functions, not used for now.
Divers debug
Diffstat (limited to 'src/Interface.cs')
| -rw-r--r-- | src/Interface.cs | 292 |
1 files changed, 16 insertions, 276 deletions
diff --git a/src/Interface.cs b/src/Interface.cs index 1b6f2437..7836b903 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -35,12 +35,16 @@ namespace go { public static class Interface { + /// <summary> Used to prevent spurious loading of templates </summary> + internal static bool XmlSerializerInit = false; + /// <summary> keep ressource path for debug msg </summary> + internal static string CurrentGOMLPath = ""; + public static int TabSize = 4; public static string LineBreak = "\r\n"; public static bool ReplaceTabsWithSpace = false; /// <summary> Allow rendering of interface in development environment </summary> public static bool DesignerMode = false; - public static bool DontResoveGOML = false; /// <summary> Threshold to catch borders for sizing </summary> public static int BorderThreshold = 5; @@ -128,20 +132,12 @@ namespace go public static Type GetTopContainerOfGOMLStream (Stream stream) { string root = "Object"; - string ClassName = ""; stream.Seek (0, SeekOrigin.Begin); using (XmlReader reader = XmlReader.Create (stream)) { while (reader.Read ()) { // first element is the root element if (reader.NodeType == XmlNodeType.Element) { root = reader.Name; - ClassName = reader.GetAttribute ("Class"); - if (!string.IsNullOrEmpty (ClassName)) - break; - if (CurrentGOMLPath.StartsWith ("#")) - ClassName = Path.GetFileNameWithoutExtension (CurrentGOMLPath.Substring (1)); - else - ClassName = Path.GetFileNameWithoutExtension (CurrentGOMLPath); break; } } @@ -149,285 +145,23 @@ namespace go Type t = Type.GetType ("go." + root); - //t = CreateDynamicType (ClassName, t); - stream.Seek (0, SeekOrigin.Begin); return t; } - - - static AssemblyBuilder assemblyBuilder; - static ModuleBuilder moduleBuilder; - - public static void InitDynamicAssembly () - { - AssemblyName an = new AssemblyName ("DynamicGraphicObjects"); - assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.RunAndSave); - moduleBuilder = assemblyBuilder.DefineDynamicModule ("MainModule"); - } - - public static void TerminateDynamicAssembly () - { - } - - public static CodeCompileUnit CompileUnit; - public static CodeTypeDeclaration GOTypeDecl; - - public static CodeTypeDeclaration GenCodeType (string newTypeName, Type baseType) - { - CompileUnit = new CodeCompileUnit (); - CodeNamespace cns = null; - - int idxLastDot = newTypeName.LastIndexOf ('.'); - string typeName = newTypeName; - if (idxLastDot < 0) - cns = new CodeNamespace ("go"); - else { - typeName = newTypeName.Substring (idxLastDot + 1); - cns = new CodeNamespace (newTypeName.Substring (0, idxLastDot)); - } - CompileUnit.Namespaces.Add (new CodeNamespace ()); - CompileUnit.Namespaces.Add (cns); - CompileUnit.Namespaces [0].Imports.Add (new CodeNamespaceImport ("System")); - CodeTypeDeclaration GOTypeDecl = new CodeTypeDeclaration (typeName); - GOTypeDecl.IsClass = true; - GOTypeDecl.IsPartial = true; - GOTypeDecl.TypeAttributes |= TypeAttributes.Public; - GOTypeDecl.BaseTypes.Add (baseType.Name); - cns.Types.Add (GOTypeDecl); - return GOTypeDecl; - } - - static void GenNewClassFromGOML (string path) - { - string root = null; - string newClassName = ""; - - using (Stream stream = GetStreamFromPath (path)) { - using (XmlReader reader = XmlReader.Create (stream)) { - CodeTypeDeclaration GOTypeDecl = null; - CodeConstructor constructor = null; - - CodeExpression curRef = null; - Type curType = null; - Stack<CodeExpression> curRefStack = new Stack<CodeExpression> (); - Stack<Type> curTypeStack = new Stack<Type> (); - - int arrayIndex = -1; - int localVarCpt = 0; - - while (reader.Read ()) { - switch (reader.NodeType) { - case XmlNodeType.Element: - if (string.IsNullOrEmpty (root)) { - //create the new base class - // first element is the root element - root = reader.Name; - newClassName = reader.GetAttribute ("Class"); - if (string.IsNullOrEmpty (newClassName)) { - if (path.StartsWith ("#")) - newClassName = Path.GetFileNameWithoutExtension (path.Substring (1)); - else - newClassName = Path.GetFileNameWithoutExtension (path); - } - curType = Type.GetType ("go." + root); - - GOTypeDecl = GenCodeType (newClassName, curType); - // Declares a constructor. - constructor = new CodeConstructor (); - constructor.Attributes = MemberAttributes.Public; - GOTypeDecl.Members.Add (constructor); - - curRef = new CodeThisReferenceExpression (); - } else if (reader.Name == "Template") { - }else{ - Type childType = Type.GetType ("go." + reader.Name); - localVarCpt++; - string localVarName = childType.Name + localVarCpt; - constructor.Statements.Add ( - new CodeVariableDeclarationStatement ( - childType, - localVarName, - new CodeObjectCreateExpression (childType) - ) - ); - if (curType == typeof(go.Container) || curType.IsSubclassOf (typeof(go.Container))) { - constructor.Statements.Add ( - new CodeMethodInvokeExpression ( - curRef, - "SetChild", - new CodeVariableReferenceExpression (localVarName) - ) - ); - } else if (curType == typeof(go.Group) || curType.IsSubclassOf (typeof(go.Group))) { - constructor.Statements.Add ( - new CodeMethodInvokeExpression (curRef, "addChild", - new CodeVariableReferenceExpression (localVarName) - ) - ); - } - curTypeStack.Push (curType); - curRefStack.Push (curRef); - curRef = new CodeVariableReferenceExpression (localVarName); - curType = childType; - } - while (reader.MoveToNextAttribute ()) { - string attName = reader.Name; - string attValue = reader.Value; - - if (string.IsNullOrEmpty (attValue)) - continue; - - MemberInfo mi = curType.GetMember (attName).FirstOrDefault (); - if (mi == null) { - Debug.WriteLine (Interface.CurrentGOMLPath + "=>GOML: Unknown attribute in " + curType.ToString () + " : " + attName); - continue; - } - if (mi.MemberType == MemberTypes.Event) { - //TODO: handle events - continue; - } - if (mi.MemberType == MemberTypes.Property) { - PropertyInfo pi = mi as PropertyInfo; - - if (pi.GetSetMethod () == null) { - Debug.WriteLine (Interface.CurrentGOMLPath + "=>GOML: Read only property in " + curType.ToString () + " : " + attName); - continue; - } - - if (attValue.StartsWith("{")) { - if (Interface.DontResoveGOML) - continue; - //binding - if (!attValue.EndsWith("}")) - throw new Exception (string.Format("GOML:Malformed binding: {0}", attValue)); - - string strBinding = attValue.Substring (1, attValue.Length - 2); - - continue; - } - - CodeExpression val = null; - if (pi.PropertyType == typeof(string)) { - val = new CodePrimitiveExpression (attValue); - } else if (pi.PropertyType.IsPrimitive) { - MethodInfo me = pi.PropertyType.GetMethod ("Parse", new Type[] { typeof(string) }); - val = new CodePrimitiveExpression ( - me.Invoke (null, new string[] { attValue })); - } else if (pi.PropertyType.IsEnum || (pi.PropertyType == typeof(go.Color) && Char.IsLetter (attValue [0]))) { - val = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (pi.PropertyType), attValue); - }else if (pi.PropertyType == typeof(go.Color) || pi.PropertyType == typeof(go.Font)) { - val = new CodeCastExpression(pi.PropertyType, - new CodeMethodInvokeExpression ( - new CodeTypeReferenceExpression (pi.PropertyType), - "Parse", - new CodePrimitiveExpression (attValue) - ) - ); - } else { - val = new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (pi.PropertyType), - "Parse", new CodePrimitiveExpression (attValue)); - - } - constructor.Statements.Add ( - new CodeAssignStatement ( - new CodePropertyReferenceExpression (curRef, attName), - val - ) - ); - } - } - reader.MoveToElement (); - if (reader.IsEmptyElement) { - curType = curTypeStack.Pop (); - curRef = curRefStack.Pop (); - } - break; - case XmlNodeType.EndElement: - if (curTypeStack.Count < 1)//GOML last closing tag - break; - curType = curTypeStack.Pop (); - curRef = curRefStack.Pop (); - if (curType.IsSubclassOf (typeof(go.Container))) - arrayIndex = -1; - break; - } - } - } - } - - - - - GenerateCSharpCode (CompileUnit, path + ".cs"); - } - - static void GenerateCSharpCode (CodeCompileUnit codeBase, string file) - { - CodeDomProvider codeDomProvider = new CSharpCodeProvider (); - //On définit les options de génération de code - CodeGeneratorOptions options = new CodeGeneratorOptions (); - //On demande a ce que le code généré soit dans le même ordre que le code inséré - options.VerbatimOrder = false; - //options.BracingStyle = "C"; - //options.BracingStyle = "C"; - options.ElseOnClosing = true; - options.BlankLinesBetweenMembers = false; - - using (IndentedTextWriter itw = new IndentedTextWriter (new StreamWriter (file, false), "\t")) { - //On demande la génération proprement dite - codeDomProvider.GenerateCodeFromCompileUnit (codeBase, itw, options); - itw.Flush (); - } - Console.WriteLine ("C# code generated: " + file); - } - - public static Type CreateDynamicType (string newTypeName, Type baseType) + public static GraphicObject Load (string path, object hostClass = null) { - if (moduleBuilder == null) - InitDynamicAssembly (); - TypeBuilder tb = moduleBuilder.DefineType (newTypeName - , TypeAttributes.Public | - TypeAttributes.Class | - TypeAttributes.AutoClass | - TypeAttributes.AnsiClass | - TypeAttributes.BeforeFieldInit | - TypeAttributes.AutoLayout - , baseType); - - ConstructorBuilder constructor = tb.DefineDefaultConstructor (MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName); - - ConstructorInfo cn = typeof(XmlRootAttribute).GetConstructor (new Type[] { typeof(string) }); - CustomAttributeBuilder cab = new CustomAttributeBuilder (cn, new object[] { baseType.Name }); - - tb.SetCustomAttribute (cab); - - - - - Type tmp = tb.CreateType (); - //assemblyBuilder.Save ("newAssembly.dll",PortableExecutableKinds.ILOnly,ImageFileMachine.I386); - return tmp; - } - - public static string CurrentGOMLPath; - - public static GraphicObject Load (string path, object hostClass = null, bool resolveGOML = true) - { -// GenNewClassFromGOML (path); -// return null; CurrentGOMLPath = path; using (Stream stream = GetStreamFromPath (path)) { - return Load(stream, GetTopContainerOfGOMLStream(stream), hostClass, resolveGOML); + return Load(stream, GetTopContainerOfGOMLStream(stream), hostClass); } CurrentGOMLPath = ""; } - public static GraphicObject Load (Stream stream, Type type, object hostClass = null, bool resolve = true) + public static GraphicObject Load (Stream stream, Type type, object hostClass = null) { #if DEBUG_LOAD_TIME Stopwatch loadingTime = new Stopwatch (); @@ -439,17 +173,23 @@ namespace go XmlSerializerNamespaces xn = new XmlSerializerNamespaces (); xn.Add ("", ""); + + XmlSerializerInit = true; XmlSerializer xs = new XmlSerializer (type); + XmlSerializerInit = false; result = (GraphicObject)xs.Deserialize (stream); //result.DataSource = hostClass; #if DEBUG_LOAD_TIME + FileStream fs = stream as FileStream; + if (fs!=null) + CurrentGOMLPath = fs.Name; loadingTime.Stop (); - Debug.WriteLine ("GOML Loading ({2}): {0} ticks \t, {1} ms", + Debug.WriteLine ("GOML Loading ({2}->{3}): {0} ticks, {1} ms", loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, - CurrentGOMLPath); + CurrentGOMLPath, result.ToString()); #endif return result; |