コード例 #1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value != null)
            {
                bool           isControls = value is MapControls;
                PropertyInfo[] properties = value.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                StringBuilder  sb         = new StringBuilder();

                foreach (PropertyInfo property in properties)
                {
                    ConfigOptionAttribute attr = ClientConfig.GetClientConfigAttribute(property);

                    if (attr != null && property.PropertyType == typeof(bool))
                    {
                        object prValue      = property.GetValue(value, null);
                        object defaultValue = ReflectionUtils.GetDefaultValue(property);
                        if ((bool)prValue)
                        {
                            if (!isControls)
                            {
                                if (!(bool)defaultValue)
                                {
                                    sb.Append(string.Concat("'enable", property.Name, "',"));
                                }
                            }
                            else
                            {
                                sb.Append(string.Concat("'", property.Name, "',"));
                            }
                        }
                        else
                        {
                            if (!isControls)
                            {
                                if ((bool)defaultValue)
                                {
                                    sb.Append(string.Concat("'disable", property.Name, "',"));
                                }
                            }
                        }
                    }
                }

                if (sb.Length > 0)
                {
                    sb.Remove(sb.Length - 1, 1);
                    writer.WriteStartArray();
                    writer.WriteRaw(sb.ToString());
                    writer.WriteEndArray();
                    return;
                }
            }
            writer.WriteStartArray();
            writer.WriteEndArray();
        }
        public bool BuildAndUpdateConfigProperties()
        {
            // get all the public properties from the class
            ServerConfig serverConfig = new ServerConfig();

            FieldInfo[] fieldInfos = serverConfig.GetType().GetFields();

            string scriptTop =
                $"//gv{Program.ForGameVersion}\r\n" +
                $"//ev{Program.VersionString}\r\n" +
                "// This file was generated with ServerConfigConverter class\r\n" +
                "// To allow a PropertyGrid to use IRs fields as properties.\r\n" +
                "using System;\r\n" +
                "using System.ComponentModel;\r\n" +
                "using System.Drawing;\r\n" +
                "using System.Drawing.Design;\r\n" +
                "using Game.Configuration;\r\n" +
                "namespace IRSE.Modules.GameConfig\r\n" +
                "{\r\n" +
                "    public class ServerConfigProperties\r\n" +
                "    {\r\n" +
                "        private static ServerConfigProperties m_instance; public static ServerConfigProperties Instance => m_instance == null ? m_instance = new ServerConfigProperties() : m_instance;\r\n" +
                "        public ServerConfigProperties(){}\r\n" +
                "        #region Manual Properties\r\n" +
                "        [Category(\"Welcome message\")]\r\n" +
                "        [Description(\"The color of the title of the welcome popup people will see when they connect to the server\")]\r\n" +
                "        [Editor(typeof(ColorEditor), typeof(UITypeEditor))]\r\n" +
                "        public Color ServerWelcomePopupMessageTitleColor\r\n" +
                "        {\r\n" +
                "            get\r\n" +
                "            {\r\n" +
                "                uint[] color = ServerConfig.Singleton.ServerWelcomePopupMessageTitleColor;\r\n" +
                "                return Color.FromArgb(0, (int)color[0], (int)color[1], (int)color[2]);\r\n" +
                "            }\r\n" +
                "            set\r\n" +
                "            {\r\n" +
                "                uint[] color = new uint[] { value.R, value.G, value.B };\r\n" +
                "                ServerConfig.Singleton.ServerWelcomePopupMessageTitleColor = color;\r\n" +
                "            }\r\n" +
                "        }\r\n" +
                "        [Category(\"Miscellaneous\")]\r\n" +
                "        [Description(\"Sets the global trade stats of resources.\")]\r\n" +
                "        public static System.Collections.Generic.Dictionary<Game.ClientServer.Classes.Economics.ResourceTypes, Game.ClientServer.Classes.Economics.ResourcePriceStats> OverwriteResourcePriceStats\r\n" +
                "        {\r\n" +
                "            get { return ServerConfig.Singleton.OverwriteResourcePriceStats; }\r\n" +
                "            set { ServerConfig.Singleton.OverwriteResourcePriceStats = value; }\r\n" +
                "        }\r\n" +
                "        [Category(\"Miscellaneous\")]\r\n" +
                "        [Description(\"Sets the global trade stats of tools.\")]\r\n" +
                "        public static System.Collections.Generic.Dictionary<Game.ClientServer.Classes.Economics.ToolTypes, Game.ClientServer.Classes.Economics.ToolPriceStats> OverwriteToolPriceStats\r\n" +
                "        { get { return ServerConfig.Singleton.OverwriteToolPriceStats; } set { ServerConfig.Singleton.OverwriteToolPriceStats = value; } }\r\n" +
                "        [Category(\"Miscellaneous\")]\r\n" +
                "        [Description(\"Sets custom settings for specific systems.\")]\r\n" +
                "        public static System.Collections.Generic.Dictionary<System.String, Game.Universe.SystemSettings> SpecificSystemSettings\r\n" +
                "        {\r\n" +
                "            get { return ServerConfig.Singleton.SpecificSystemSettings; }\r\n" +
                "            set { ServerConfig.Singleton.SpecificSystemSettings = value; }\r\n" +
                "        }\r\n" +
                "        [Category(\"Starter Ships\")]\r\n" +
                "        [Description(\"Specifics of starter ships.\")]\r\n" +
                "        public static System.Collections.Generic.Dictionary<Game.ClientServer.Classes.FactionTypes, System.Collections.Generic.List<Game.ClientServer.Classes.FactionStarterShipDetails>> FactionStarterShips\r\n" +
                "        {\r\n" +
                "            get { return ServerConfig.Singleton.FactionStarterShips; }\r\n" +
                "            set { ServerConfig.Singleton.FactionStarterShips = value; }\r\n" +
                "        }\r\n" +
                "        #endregion\r\n" +
                "        //---<STARTGEN>---";

            string code = "";

            foreach (FieldInfo field in fieldInfos)
            {
                if (field == null)
                {
                    continue;
                }

                ConfigOptionAttribute attribute = field.GetCustomAttribute <ConfigOptionAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                string name        = field.Name;
                string category    = attribute.GetCategoryName();
                string description = Game.Configuration.Localization.Singleton.GetString("ServerConfigOptions", attribute.Description);
                string type        = field.FieldType.ToString();

                dynamic value = field.GetValue(serverConfig);

                string commentOut = "";
                if (type.Contains("Dictionary") || type.Contains("List") || type.Contains("UInt32[]"))
                {
                    Console.WriteLine($"Manual Edit Item. Name: {name}, Commenting Out Lines");
                    commentOut = @"//";
                }

                code +=
                    "\n" +
                    $"      {commentOut}[DisplayName(\"{AddSpacesToSentence(name, true)}\")]\n" +
                    $"      {commentOut}[Category(\"{category}\")]\n" +
                    $"      {commentOut}[Description(\"{description}\")]\n" +
                    $"      {commentOut}public {type} {name} \n" +
                    $"      {commentOut}{{get{{return ServerConfig.Singleton.{name};}} set {{ServerConfig.Singleton.{name} = value;}}}}\n" +
                    "\n";
            }

            string scriptBottom =
                "      //---<ENDGEN>---\n" +
                "  }\n" +
                "}\n" +
                "\n";

            File.WriteAllText(@"F:\Wrex\Desktop\ServerConfigProperties.cs", scriptTop + code + scriptBottom);

            return(false);
        }