Example #1
0
        public NativeTypeMappingData(Action <string> logger, TypeMapGenerator.ModuleData[] modules, int javaNameWidth)
        {
            Modules = modules ?? throw new ArgumentNullException(nameof(modules));

            MapModuleCount = (uint)modules.Length;
            JavaNameWidth  = (uint)javaNameWidth;

            AssemblyNames = new Dictionary <string, string> (StringComparer.Ordinal);

            var tempJavaTypes        = new Dictionary <string, TypeMapGenerator.TypeMapEntry> (StringComparer.Ordinal);
            int managedStringCounter = 0;
            var moduleComparer       = new TypeMapGenerator.ModuleUUIDArrayComparer();

            foreach (TypeMapGenerator.ModuleData data in modules)
            {
                data.AssemblyNameLabel = $"map_aname.{managedStringCounter++}";
                AssemblyNames.Add(data.AssemblyNameLabel, data.AssemblyName);

                int moduleIndex = Array.BinarySearch(modules, data, moduleComparer);
                if (moduleIndex < 0)
                {
                    throw new InvalidOperationException($"Unable to map module with MVID {data.Mvid} to array index");
                }

                foreach (TypeMapGenerator.TypeMapEntry entry in data.Types)
                {
                    entry.ModuleIndex = moduleIndex;
                    if (tempJavaTypes.ContainsKey(entry.JavaName))
                    {
                        continue;
                    }
                    tempJavaTypes.Add(entry.JavaName, entry);
                }
            }

            var javaNames = tempJavaTypes.Keys.ToArray();

            Array.Sort(javaNames, StringComparer.Ordinal);

            var javaTypes = new TypeMapGenerator.TypeMapEntry[javaNames.Length];

            for (int i = 0; i < javaNames.Length; i++)
            {
                javaTypes[i] = tempJavaTypes[javaNames[i]];
            }

            JavaTypes     = javaTypes;
            JavaTypeNames = javaNames;
            JavaTypeCount = (uint)JavaTypes.Length;
        }
        uint WriteJavaMapEntry(StreamWriter output, TypeMapGenerator.TypeMapEntry entry, int entryIndex)
        {
            uint size = 0;

            WriteCommentLine(output, $"#{entryIndex}");
            WriteCommentLine(output, "module_index");
            size += WriteData(output, entry.ModuleIndex);

            WriteCommentLine(output, "type_token_id");
            size += WriteData(output, entry.Token);

            WriteCommentLine(output, "java_name");
            size += WriteAsciiData(output, entry.JavaName, mappingData.JavaNameWidth);

            output.WriteLine();

            return(size);
        }