예제 #1
0
파일: Program.cs 프로젝트: Toocanzs/OWLib
        public static void AddNewClasses(Dictionary <uint, STUInstanceJSON> instanceJson)
        {
            const string dumpNamespace = "Dump";
            const string enumNamespace = "Enums";

            CreateDirectory(Path.Combine(OutputDirectory, dumpNamespace));
            CreateDirectory(Path.Combine(OutputDirectory, dumpNamespace, "Enums"));
            foreach (KeyValuePair <uint, STUInstanceJSON> json in Version2Comparer.InstanceJSON)
            {
                if (InvalidTypes.Contains(json.Value.Name))
                {
                    continue;
                }
                if (ISTU.InstanceTypes.ContainsKey(json.Value.Hash))
                {
                    continue;
                }
                InstanceData instanceData = Version2Comparer.GetData(json.Key);
                ClassBuilder builder      = new ClassBuilder(instanceData);
                string       @class       = builder.Build(InstanceNames, EnumNames, FieldNames, $"STULib.Types.{dumpNamespace}", true, false);
                string       className    = $"STU_{instanceData.Checksum:X8}";
                if (InstanceNames.ContainsKey(instanceData.Checksum))
                {
                    className = InstanceNames[instanceData.Checksum];
                }

                WriteStringToFile(@class, Path.Combine(OutputDirectory, dumpNamespace, $"{className}.cs"));

                foreach (FieldData field in instanceData.Fields)
                {
                    if (!field.IsEnum && !field.IsEnumArray)
                    {
                        continue;
                    }
                    if (ISTU.EnumTypes.ContainsKey(field.EnumChecksum))
                    {
                        continue;
                    }
                    EnumBuilder enumBuilder = new EnumBuilder(new STUEnumData {
                        Type     = STUHashTool.Program.GetSizeType(field.Size),
                        Checksum = field.EnumChecksum
                    });
                    string @enum    = enumBuilder.Build(new Dictionary <uint, string>(), $"STULib.Types.{dumpNamespace}.{enumNamespace}", true);
                    string enumName = $"STUEnum_{field.EnumChecksum:X8}";
                    if (EnumNames.ContainsKey(field.EnumChecksum))
                    {
                        enumName = EnumNames[field.EnumChecksum];
                    }
                    WriteStringToFile(@enum, Path.Combine(OutputDirectory, dumpNamespace, enumNamespace, $"{enumName}.cs"));
                }
            }
        }
예제 #2
0
        public static void CompileSTUs()
        {
            StringBuilder  sb            = new StringBuilder();
            HashSet <uint> doneEnums     = new HashSet <uint>();
            HashSet <uint> doneInstances = new HashSet <uint>();

            foreach (KeyValuePair <uint, STUInstanceJSON> json in Version2Comparer.InstanceJSON)
            {
                if (InvalidTypes.Contains(json.Value.Name))
                {
                    continue;
                }
                InstanceData instanceData = Version2Comparer.GetData(json.Key);
                if (instanceData == null)
                {
                    continue;
                }
                if (doneInstances.Contains(instanceData.Checksum))
                {
                    continue;
                }
                doneInstances.Add(instanceData.Checksum);
                ClassBuilder builder = new ClassBuilder(instanceData);
                string       @class  = builder.Build(InstanceNames, EnumNames, FieldNames, "STUExcavator.Types", false, true);
                sb.AppendLine(@class);

                foreach (FieldData field in instanceData.Fields)
                {
                    if (!field.IsEnum && !field.IsEnumArray)
                    {
                        continue;
                    }
                    if (doneEnums.Contains(field.EnumChecksum))
                    {
                        continue;
                    }
                    doneEnums.Add(field.EnumChecksum);
                    EnumBuilder enumBuilder = new EnumBuilder(new STUEnumData {
                        Type     = STUHashTool.Program.GetSizeType(field.Size),
                        Checksum = field.EnumChecksum
                    });
                    sb.AppendLine(enumBuilder.Build(EnumNames, "STUExcavator.Types.Enums", true));
                }
            }

            CSharpCodeProvider provider   = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();

            parameters.ReferencedAssemblies.Add("STULib.dll");
            parameters.ReferencedAssemblies.Add("OWLib.dll");
            parameters.GenerateInMemory = true;
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, sb.ToString());

            if (results.Errors.HasErrors)
            {
                StringBuilder sb2 = new StringBuilder();

                foreach (CompilerError error in results.Errors)
                {
                    sb2.AppendLine($"Error ({error.ErrorNumber}): {error.ErrorText}");
                }

                throw new InvalidOperationException(sb2.ToString());
            }

            Assembly assembly = results.CompiledAssembly;

            foreach (KeyValuePair <uint, STUInstanceJSON> json in Version2Comparer.InstanceJSON)
            {
                if (InvalidTypes.Contains(json.Value.Name))
                {
                    continue;
                }
                Type compiledInst = assembly.GetType($"STUExcavator.Types.{json.Value.Name}");
                if (compiledInst == null)
                {
                    continue;
                }
                ISTU.InstanceTypes[json.Value.Hash] = compiledInst;
            }
        }