Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9014


Ignore:
Timestamp:
12/07/12 22:47:20 (12 years ago)
Author:
ascheibe
Message:

#1952 implemented loading of HL files save with Mono on Windows

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeLoader.cs

    r8699 r9014  
    2929  internal class TypeLoader {
    3030    #region Mono Compatibility
    31     private static TypeName cachedRuntimeType;
    32     private static TypeName cachedObjectEqualityComparerType;
     31    private static TypeName cachedMonoRuntimeType;
     32    private static TypeName cachedWindowsRuntimeType;
     33    private static TypeName cachedMonoObjectEqualityComparerType;
     34    private static TypeName cachedWindowsObjectEqualityComparerType;
     35
     36    private static bool MonoInstalled {
     37      get { return Type.GetType("Mono.Runtime") != null; }
     38    }
    3339
    3440    static TypeLoader() {
    3541      // we use Int32 here because we get all the information about Mono's mscorlib and just have to change the class name
    36       cachedRuntimeType = TypeNameParser.Parse(typeof(System.Int32).AssemblyQualifiedName);
    37       cachedRuntimeType = new TypeName(cachedRuntimeType, "MonoType");
     42      cachedMonoRuntimeType = TypeNameParser.Parse(typeof(System.Int32).AssemblyQualifiedName);
     43      cachedMonoRuntimeType = new TypeName(cachedMonoRuntimeType, "MonoType");
     44
     45      cachedWindowsRuntimeType = TypeNameParser.Parse(typeof(System.Int32).AssemblyQualifiedName);
     46      cachedWindowsRuntimeType = new TypeName(cachedWindowsRuntimeType, "RuntimeType");
    3847
    3948      // we need the information about the Persistence assembly, so we use TypeName here because it is contained in this assembly
    40       cachedObjectEqualityComparerType = TypeNameParser.Parse(typeof(TypeName).AssemblyQualifiedName);
    41       cachedObjectEqualityComparerType = new TypeName(cachedObjectEqualityComparerType, "ObjectEqualityComparer", "HeuristicLab.Persistence.Mono");
     49      cachedMonoObjectEqualityComparerType = TypeNameParser.Parse(typeof(TypeName).AssemblyQualifiedName);
     50      cachedMonoObjectEqualityComparerType = new TypeName(cachedMonoObjectEqualityComparerType, "ObjectEqualityComparer", "HeuristicLab.Persistence.Mono");
     51
     52      cachedWindowsObjectEqualityComparerType = TypeNameParser.Parse(typeof(System.Int32).AssemblyQualifiedName);
     53      cachedWindowsObjectEqualityComparerType = new TypeName(cachedWindowsObjectEqualityComparerType, "ObjectEqualityComparer", "System.Collections.Generic");
    4254    }
    4355    #endregion
     
    6072      catch (PersistenceException) {
    6173        #region Mono Compatibility
    62         // if that fails, try to load Mono type
    63         TypeName monoTypeName = GetMonoType(typeName);
    64         Logger.Info(String.Format(@"Trying to load Mono type ""{0}"" instead of type ""{1}""",
    65                                   monoTypeName, typeNameString));
    66         return LoadInternal(monoTypeName);
     74        // if that fails, try to convert to the corresponding Mono or .NET type
     75        if (MonoInstalled) {
     76          TypeName monoTypeName = GetMonoType(typeName);
     77          Logger.Info(String.Format(@"Trying to load Mono type ""{0}"" instead of type ""{1}""",
     78                                    monoTypeName, typeNameString));
     79          return LoadInternal(monoTypeName);
     80        } else {
     81          TypeName dotNetTypeName = GetDotNetType(typeName);
     82          Logger.Info(String.Format(@"Trying to load .NET type ""{0}"" instead of type ""{1}""",
     83                                    dotNetTypeName, typeNameString));
     84          return LoadInternal(dotNetTypeName);
     85        }
    6786      }
    6887        #endregion
     
    134153      // map System.RuntimeType to System.MonoType
    135154      if (typeName.Namespace == "System" && typeName.ClassName == "RuntimeType") {
    136         return cachedRuntimeType;
     155        return cachedMonoRuntimeType;
    137156        // map System.Collections.Generic.ObjectEqualityComparer to HeuristicLab.Mono.ObjectEqualityComparer
    138157      } else if (typeName.Namespace == "System.Collections.Generic" && typeName.ClassName == "ObjectEqualityComparer") {
    139         TypeName newTypeName = new TypeName(cachedObjectEqualityComparerType);
     158        TypeName newTypeName = new TypeName(cachedMonoObjectEqualityComparerType);
     159        newTypeName.GenericArgs = new List<TypeName>(typeName.GenericArgs);
     160        return newTypeName;
     161      }
     162      return typeName;
     163    }
     164
     165    /// <summary>
     166    /// Returns the corresponding type for the .NET runtime
     167    /// </summary>
     168    /// <returns>
     169    /// The remapped typeName, or the original typeName if no mapping was found
     170    /// </returns>
     171    private static TypeName GetDotNetType(TypeName typeName) {
     172      // map System.MonoType to System.RuntimeType
     173      if (typeName.Namespace == "System" && typeName.ClassName == "MonoType") {
     174        return cachedWindowsRuntimeType;
     175        // maps Mono's string comparer to System.Collections.Generic.ObjectEqualityComparer
     176      } else if (typeName.Namespace == "System.Collections.Generic" && typeName.ClassName == "InternalStringComparer") {
     177        TypeName newTypeName = new TypeName(cachedWindowsObjectEqualityComparerType);
     178        var genericArgsList = new List<TypeName>();
     179        genericArgsList.Add(new TypeName(typeof(String).Namespace, "String"));
     180        newTypeName.GenericArgs = genericArgsList;
     181        return newTypeName;
     182      } else if (typeName.Namespace == "System.Collections.Generic" && typeName.ClassName == "EqualityComparer+DefaultComparer") {
     183        TypeName newTypeName = new TypeName(cachedWindowsObjectEqualityComparerType);
    140184        newTypeName.GenericArgs = new List<TypeName>(typeName.GenericArgs);
    141185        return newTypeName;
Note: See TracChangeset for help on using the changeset viewer.