public IEnumerator Load(IAssetRequest <Texture2D> assetRequest)
        {
            if (!GameDatabase.Instance.IsReady())
            {
                FARLogger.Warning("Trying to load textures before GameDatabase has been loaded");
                assetRequest.State = Progress.Error;
                assetRequest.OnError();
                yield break;
            }

            assetRequest.State = Progress.InProgress;
            try
            {
                FARLogger.DebugFormat("Getting texture {0} from GameDatabase", assetRequest.Url);
                Texture2D texture = GameDatabase.Instance.GetTexture(assetRequest.Url, false);
                assetRequest.State = Progress.Completed;
                assetRequest.OnLoad(texture);
            }
            catch (Exception e)
            {
                FARLogger.Exception(e, $"While loading texture {assetRequest.Url} from game database:");
                assetRequest.State = Progress.Error;
                assetRequest.OnError();
            }
        }
        /// <inheritdoc />
        public bool OnValueList(ListValueReflection reflection, out IList newValue)
        {
            List <string> values = Node.GetValuesList(reflection.Name);

            newValue = default;
            if (values.Count == 0)
            {
                return(false);
            }

            newValue = new List <object>();
            foreach (string str in values)
            {
                if (!Serialization.TryGetValue(str, out object v, reflection.ValueType))
                {
                    continue;
                }

                newValue.Add(v);
            }

            FARLogger.DebugFormat("Parsed {0}.{1} with {2} values", Node.name, reflection.Name, newValue.Count);

            return(true);
        }
        private static void SaveConfigs(
            string prefix,
            bool isPatch     = true,
            string extension = ".cfg",
            bool force       = false
            )
        {
            var node = new ConfigNode();
            var save = new SaveVisitor
            {
                IsPatch = isPatch,
                Node    = node
            };

            foreach (KeyValuePair <string, ReflectedConfig> pair in ConfigReflection.Instance.Configs)
            {
                if (!pair.Value.Reflection.ShouldSave)
                {
                    continue;
                }

                pair.Value.Reflection.Save(save, pair.Value.Instance);

                if (!save.Node.HasData)
                {
                    continue;
                }

                string path = PathUtil.Combine(KSPUtils.GameDataPath, $"{prefix}_{pair.Key}{extension}");

                if (!pair.Value.Reflection.AllowMultiple)
                {
                    Serialization.MakeTopNode(node, pair.Key, null, isPatch);
                }

                string nodeStr = node.ToString();

                if (lastConfigs.TryGetValue(pair.Key, out string oldStr))
                {
                    lastConfigs[pair.Key] = nodeStr;
                }
                else
                {
                    lastConfigs.Add(pair.Key, nodeStr);
                }

                // only write if requested or if the node has been modified, first time oldStr should be null so can be skipped
                if (force || (!string.IsNullOrEmpty(oldStr) && nodeStr != oldStr))
                {
                    FARLogger.DebugFormat("Saving {0} config to {1}:\n{2}\n\n{3}", pair.Key, path, oldStr, nodeStr);
                    System.IO.File.WriteAllText(path, nodeStr);
                }
                else
                {
                    FARLogger.DebugFormat("{0} does not require saving", pair.Key);
                }

                node.ClearData();
            }
        }
        public static bool TryGetValue(string str, out object value, Type valueType)
        {
            value = null;
            if (str == null)
            {
                return(false);
            }
            if (valueType.IsEnum)
            {
                if (!TryParseEnum(str, valueType, out Enum e))
                {
                    return(false);
                }
                value = e;
                return(true);
            }

            if (serializers.TryGetValue(valueType, out Serializer io))
            {
                if (!io.Parse(str, out object o))
                {
                    return(false);
                }
                value = o;
                return(true);
            }

            FARLogger.DebugFormat("Unknown value type {0}", valueType);
            return(false);
        }
        public static void AddValue(
            ConfigNode node,
            string id,
            object value,
            Type valueType,
            bool isPatch = false,
            int?index    = null
            )
        {
            if (isPatch)
            {
                id = index != null ? $"@{id},{((int)index).ToString()}" : $"%{id}";
            }

            if (valueType.IsEnum)
            {
                node.AddValue(id, value);
                return;
            }

            if (serializers.TryGetValue(valueType, out Serializer io))
            {
                io.Save(node, id, value);
                return;
            }

            FARLogger.DebugFormat("Unknown value type {0}", valueType);
            node.AddValue(id, value);
        }
Exemple #6
0
        private IEnumerator SetupType <T>(Type type, bool persistant, List <T> objects) where T : class
        {
            // skip invalid types
            if (type.IsAbstract || type.IsInterface)
            {
                yield break;
            }

            FARLogger.DebugFormat("FARAddonLoader: instantiating {0}", type);

            bool contains = true;

            // if not yet instantiated, try to find singleton instance or create a new one
            if (!instantiatedTypes.TryGetValue(type, out object o))
            {
                o = ReflectionUtils.FindInstance(type);
                if (o == null)
                {
                    o = typeof(Component).IsBaseOf(type)
                            ? ReflectionUtils.Create(type, transform, persistant)
                            : Activator.CreateInstance(type, true);
                }
                else
                {
                    FARLogger.DebugFormat("Found an instance of {0}", type);
                }
                contains = false;
            }

            // enable behaviour so that their Awake methods run
            if (o is Behaviour behaviour && behaviour != null)
            {
                behaviour.enabled = true;
            }

            // wait for the addon to finish its setup
            if (o is IWaitForAddon waitForAddon)
            {
                yield return(new WaitUntil(() => waitForAddon.Completed));
            }

            // store persistant objects
            if (persistant)
            {
                objects.Add(o as T);
                if (!contains)
                {
                    instantiatedTypes.Add(type, o);
                }
            }
            else if (o is Component component)
            {
                Destroy(component);
            }

            FARLogger.DebugFormat("FARAddonLoader: {0} finished", type);
        }
        /// <inheritdoc />
        public bool OnValue(ValueReflection reflection, out object newValue)
        {
            if (!Serialization.TryGetValue(Node, reflection.Name, out newValue, reflection.ValueType))
            {
                return(false);
            }

            FARLogger.DebugFormat("Parsed {0}.{1} = {2}", Node.name, reflection.Name, newValue);
            return(true);
        }
 protected Singleton()
 {
     if (instance == null)
     {
         FARLogger.DebugFormat("Singleton {0} is created", this);
     }
     else
     {
         FARLogger.TraceFormat("{0} is a Singleton but an instance already exists", this);
     }
 }