コード例 #1
0
        public static ModConfig Load(ICoreAPI api)
        {
            ModConfig config = null;

            try {
                for (int attempts = 1; attempts < 4; attempts++)
                {
                    try {
                        config = api.LoadModConfig <ModConfig>(filename);
                    } catch (JsonReaderException e) {
                        var badLineNum = e.LineNumber;
                        api.Logger.Error($"[CompassMod Error] Unable to parse config JSON. Attempt {attempts} to salvage the file...");
                        var configFilepath     = Path.Combine(GamePaths.ModConfig, filename);
                        var badConfigFilepath  = Path.Combine(GamePaths.Logs, "ERROR_" + filename);
                        var compassLogFilepath = Path.Combine(GamePaths.Logs, "compass-mod-logs.txt");
                        if (attempts == 1)
                        {
                            if (File.Exists(badConfigFilepath))
                            {
                                File.Delete(badConfigFilepath);
                            }
                            File.Copy(configFilepath, badConfigFilepath);
                            File.WriteAllText(compassLogFilepath, e.ToString());
                        }
                        if (attempts != 3)
                        {
                            var lines = new List <string>(File.ReadAllLines(configFilepath));
                            lines.RemoveAt(badLineNum - 1);
                            File.WriteAllText(configFilepath, string.Join("\n", lines.ToArray()));
                        }
                    }
                }
                try {
                    config = api.LoadModConfig <ModConfig>(filename);
                } catch (JsonReaderException) {
                    api.Logger.Error("[CompassMod Error] Unable to salvage config.");
                }
            } catch (System.Exception e) {
                api.Logger.Error("[CompassMod Error] Something went really wrong with reading the config file.");
                File.WriteAllText(Path.Combine(GamePaths.Logs, "compass-mod-logs.txt"), e.ToString());
            }

            if (config == null)
            {
                api.Logger.Warning("[CompassMod Warning] Unable to load valid config file. Generating default config.");
                config = new ModConfig();
            }
            config.OriginCompassGears   = GameMath.Clamp(config.OriginCompassGears, 1, 8);
            config.RelativeCompassGears = GameMath.Clamp(config.RelativeCompassGears, 1, 8);
            Save(api, config);
            return(config);
        }
コード例 #2
0
ファイル: ApiExtensions.cs プロジェクト: p3t3rix/VsFoundation
        public static TModConfig LoadOrCreateConfig <TModConfig>(this ICoreAPI api, string filename, bool required) where TModConfig : ModConfigBase, new()
        {
            var modCode = filename.Split('.').First();

            try
            {
                var loadedConfig = api.LoadModConfig <TModConfig>(filename);
                if (loadedConfig != null)
                {
                    return(loadedConfig);
                }
            }
            catch (Exception e)
            {
                api.World.Logger.Error($"{modCode}: Failed loading modconfig file at 'ModConfig/{filename}', with an error of '{e}'! Stopping...");
                return(null);
            }

            var message = $"{modCode}: non-existant modconfig at 'ModConfig/{filename}', creating default" + (required ? " and disabling mod..." : "...");

            api.World.Logger.Notification(message);

            var newConfig = new TModConfig();

            api.StoreModConfig(newConfig, filename);

            return(required ? null : newConfig);
        }
コード例 #3
0
        public static ModConfig Load(ICoreAPI api)
        {
            var config = api.LoadModConfig <ModConfig>(filename);

            if (config == null)
            {
                config = new ModConfig();
                Save(api, config);
            }
            return(config);
        }
コード例 #4
0
        public override void Start(ICoreAPI api)
        {
            base.Start(api);

            api.RegisterItemClass("ProgCard", typeof(ItemProgCard));
            api.RegisterItemClass("ItemDeployBot", typeof(ItemDeployBot));

            api.RegisterEntityBehaviorClass("program", typeof(EntityBehaviorProgram));

            api.RegisterEntity("EntityReprogLocust", typeof(EntityReprogLocust));

            AiTaskRegistry.Register <AiTaskHarvest>("harvest");
            AiTaskRegistry.Register <AiTaskSeekItem>("seekitem");
            AiTaskRegistry.Register <AiTaskToChest>("tochest");
            AiTaskRegistry.Register <AiTaskFromChest>("fromchest");
            AiTaskRegistry.Register <AiTaskPlant>("plant");
            AiTaskRegistry.Register <AiTaskSqueezeHoney>("squeezehoney");
            AiTaskRegistry.Register <AiTaskMilk>("milk");
            AiTaskRegistry.Register <AiTaskToBarrel>("tobarrel");
            AiTaskRegistry.Register <AiTaskGetSeed>("getseed");
            AiTaskRegistry.Register <AiTaskSeekItemAny>("seekanyitem");
            AiTaskRegistry.Register <AiTaskGetFuel>("getfuel");
            AiTaskRegistry.Register <AiTaskRefuel>("refuel");
            AiTaskRegistry.Register <AiTaskRepair>("repair");
            AiTaskRegistry.Register <AiTaskProgSeekEntity>("progseekentity");
            AiTaskRegistry.Register <AiTaskProgMeleeAttack>("progmeleeattack");
            AiTaskRegistry.Register <AiTaskQuarry>("quarry");
            AiTaskRegistry.Register <AiTaskStayCloseToOwner>("stayclosetoowner");
            AiTaskRegistry.Register <AiTaskPathBuilder>("pathbuilder");
            AiTaskRegistry.Register <AiTaskToAltChest>("toaltchest");
            AiTaskRegistry.Register <AiTaskProgRangedAttack>("prograngedattack");
            AiTaskRegistry.Register <AiTaskForm>("form");
            AiTaskRegistry.Register <AiTaskToPoint>("topoint");
            AiTaskRegistry.Register <AiTaskGetItem>("getitem");
            AiTaskRegistry.Register <AiTaskAnyFromChest>("anyfromchest");
            AiTaskRegistry.Register <AiTaskGetTool>("gettool");

            try
            {
                TemporalHackerConfig FromDisk;
                if ((FromDisk = api.LoadModConfig <TemporalHackerConfig>("TemporalHackerConfig.json")) == null)
                {
                    api.StoreModConfig <TemporalHackerConfig>(TemporalHackerConfig.Loaded, "TemporalHackerConfig.json");
                }
                else
                {
                    TemporalHackerConfig.Loaded = FromDisk;
                }
            }
            catch
            {
                api.StoreModConfig <TemporalHackerConfig>(TemporalHackerConfig.Loaded, "TemporalHackerConfig.json");
            }
        }
コード例 #5
0
        public static T LoadOrCreateConf <T>(this ICoreAPI serverApi, string filename) where T : new()
        {
            try
            {
                var loadedConf = serverApi.LoadModConfig <T>(filename);
                if (loadedConf != null)
                {
                    return(loadedConf);
                }
            }
            catch (Exception e)
            {
                serverApi.Logger.Error($"Failed to loading config {filename} with error {e}. Initialize new...");
            }
            var newConf = new T();

            serverApi.StoreModConfig(newConf, filename);
            serverApi.Logger.Warning($"Created new {filename} config.");
            return(newConf);
        }
コード例 #6
0
        public static ModConfig Load(ICoreAPI api)
        {
            ModConfig config;

            try
            {
                config = api.LoadModConfig <ModConfig>(Filename);
            }
            catch (Exception exception)
            {
                // on load failure, return defaults but log it.
                api.Logger.Warning($"Failed to load {Filename}: {exception.Message}");
                return(new ModConfig());
            }
            if (config != null)
            {
                return(config);
            }
            config = new ModConfig();
            Save(api, config);
            return(config);
        }
コード例 #7
0
        public override void Start(ICoreAPI api)
        {
            base.Start(api);
            this.api = api;

            api.Logger.StoryEvent("[Tailor] Hello world!");

            // register network channel and message types
            api.Network.RegisterChannel("tailor")
            .RegisterMessageType(typeof(NetworkApiTailorRequest))
            .RegisterMessageType(typeof(NetworkApiTailorResponse))
            ;

            // load config file or write it with defaults
            config = api.LoadModConfig <ModConfig>("TailorConfig.json");
            if (config == null)
            {
                config = new ModConfig();
                api.StoreModConfig(config, "TailorConfig.json");
            }

            api.RegisterItemClass("boneneedle", typeof(BoneNeedleItem));
        }
コード例 #8
0
        public static TConfig LoadOrCreateConfig <TConfig>(this ICoreAPI api, string file, TConfig defaultConfig = null) where TConfig : class, new()
        {
            TConfig config = null;

            try
            {
                config = api.LoadModConfig <TConfig>(file);
            }
            catch (Exception e)
            {
                string format = "Failed loading config file ({0}), error {1}. Will initialize default config";
                Core.ModLogger.Error(string.Format(format), file, e);
            }

            if (config == null)
            {
                Core.ModLogger.Notification("Will initialize default config");
                config = defaultConfig ?? new TConfig();
            }

            api.StoreModConfig(config, file);
            return(config);
        }
コード例 #9
0
 private MeteoricExpansionConfig LoadConfig(ICoreAPI api)
 {
     return(api.LoadModConfig <MeteoricExpansionConfig>("MeteoricExpansionConfig.json"));
 }