示例#1
0
        /// <summary>Reads values of the annotated persistent fields from a config file.</summary>
        /// <param name="filePath">
        /// A relative or an absolute path to the file. It's resolved via
        /// <see cref="KspPaths.MakeAbsPathForGameData"/>.
        /// </param>
        /// <param name="type">A type to load fields for.</param>
        /// <param name="instance">
        /// An instance of type <paramref name="type"/>. If it's <c>null</c> then
        /// only static fields will be loaded.
        /// </param>
        /// <param name="nodePath">
        /// An optional path in the file. All type's field will be read relative to this part.
        /// </param>
        /// <param name="group">A group tag (see <see cref="BasePersistentFieldAttribute"/>).</param>
        /// <seealso cref="PersistentFieldAttribute"/>
        public static void ReadFieldsFromFile(string filePath, Type type, object instance,
                                              string nodePath = null,
                                              string group    = StdPersistentGroups.Default)
        {
            DebugEx.Fine("Loading persistent fields: file={0}, group=\"{1}\"",
                         KspPaths.MakeRelativePathToGameData(filePath), group ?? "<ALL>");
            var node = ConfigNode.Load(KspPaths.MakeAbsPathForGameData(filePath));

            if (node != null && nodePath.Length > 0)
            {
                node = node.GetNode(nodePath);
            }
            if (node != null)
            {
                ReadFieldsFromNode(node, type, instance, group: group);
            }
        }
示例#2
0
        void Awake()
        {
            if (!KASAPI.isLoaded)
            {
                KASAPI.JointUtils       = new KASImpl.JointUtilsImpl();
                KASAPI.AttachNodesUtils = new KASImpl.AttachNodesUtilsImpl();
                KASAPI.LinkUtils        = new KASImpl.LinkUtilsImpl();
                KASAPI.PhysicsUtils     = new KASImpl.PhysicsUtilsImpl();
                KASAPI.CommonConfig     = new KASImpl.CommonConfigImpl();
                KASAPI.KasEvents        = new KASImpl.KasEventsImpl();
                KASAPI.isLoaded         = true;

                var assembly = GetType().Assembly;
                DebugEx.Info("Loading KAS API v2 from: {0} (v{1})",
                             KspPaths.MakeRelativePathToGameData(assembly.Location),
                             assembly.GetName().Version);
            }
        }
示例#3
0
        void Awake()
        {
            if (loaded)
            {
                gameObject.DestroyGameObject();
                return; // Only let the loader to work once per version.
            }
            loaded = true;

            var assembly = GetType().Assembly;

            assemblyVersionStr = $"{KspPaths.MakeRelativePathToGameData(assembly.Location)} (v{assembly.GetName().Version})";
            DebugEx.Info("Loading KSPDevUtils: {0}", assemblyVersionStr);

            // Install the localization callbacks. The object must not be destroyed.
            DontDestroyOnLoad(gameObject);
            gameObject.AddComponent <LocalizationLoader>();
            gameObject.AddComponent <UISoundPlayer>();
        }
示例#4
0
        /// <summary>Writes values of the annotated persistent fields into a file.</summary>
        /// <remarks>
        /// All persitent values are <b>added</b> into the file provided. I.e. if node had already had a
        /// value being persited then it either overwritten (ordinary fields) or extended (collection
        /// fields).
        /// </remarks>
        /// <param name="filePath">
        /// A relative or an absolute path to the file. It's resolved via
        /// <see cref="KspPaths.MakeAbsPathForGameData"/>.
        /// </param>
        /// <param name="rootNodePath">
        /// A path to the node in the file where the data should be written. If the node already exsists
        /// it will be deleted.
        /// </param>
        /// <param name="type">A type to write fields for.</param>
        /// <param name="instance">
        /// An instance of type <paramref name="type"/>. If it's <c>null</c> then only static fields will
        /// be written.
        /// </param>
        /// <param name="mergeMode">
        /// If <c>true</c> and the file already exists then only will be created.
        /// </param>
        /// <param name="group">A group tag (see <see cref="BasePersistentFieldAttribute"/>).</param>
        /// <seealso cref="PersistentFieldAttribute"/>
        public static void WriteFieldsIntoFile(string filePath,
                                               Type type, object instance,
                                               string rootNodePath = null, bool mergeMode = true,
                                               string group        = StdPersistentGroups.Default)
        {
            DebugEx.Fine("Writing persistent fields: file={0}, group=\"{1}\", isMerging={2}, root={3}",
                         KspPaths.MakeRelativePathToGameData(filePath),
                         group ?? "<ALL>", mergeMode, rootNodePath ?? "/");
            var node = mergeMode
        ? ConfigNode.Load(filePath) ?? new ConfigNode()  // Make empty node if file doesn't exist.
        : new ConfigNode();
            var tagetNode = node;

            if (rootNodePath != null)
            {
                tagetNode = GetNodeByPath(node, rootNodePath, createIfMissing: true);
                tagetNode.ClearData(); // In case of it's an existing node.
            }
            WriteFieldsIntoNode(tagetNode, type, instance, group: group);
            node.Save(KspPaths.MakeAbsPathForGameData(filePath));
        }
示例#5
0
        /// <summary>Finds all the entities for the prefix, and populates the list.</summary>
        /// <param name="prefix">The prefix to find URL by.</param>
        void GuiActionUpdateTargets(string prefix)
        {
            targets.Clear();
            if (prefix.Length < 3)
            {
                targets.Add(new StubRecord()
                {
                    stubText = TypePrefixToStartTxt,
                });
                return;
            }

            // Find part configs for the prefix.
            targets.AddRange(PartLoader.LoadedPartsList
                             .Where(x => x.partUrl.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                             .OrderBy(x => x.partUrl)
                             .GroupBy(x => {
                var pos = x.partUrl.LastIndexOf("/Parts", StringComparison.OrdinalIgnoreCase);
                return(pos != -1 ? x.partUrl.Substring(0, pos + 6) : x.partUrl.Split('/')[0]);
            })
                             .Select(group => new PartsRecord()
            {
                urlPrefix = group.Key,
                parts     = group.ToList(),
            })
                             .Cast <ScannedRecord>());

            // Find assemblies for the prefix.
            // Utility assemblies of the same version are loaded only once, but they are referred for every
            // URL at which the assembly was found.
            targets.AddRange(AssemblyLoader.loadedAssemblies
                             .Where(x =>
                                    x.url.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) &&
                                    KspPaths.MakeRelativePathToGameData(x.assembly.Location)
                                    .StartsWith(prefix, StringComparison.OrdinalIgnoreCase) &&
                                    (allowNoModulesAssemblies || x.types.Count > 0))
                             .Select(assembly => new AssemblyRecord()
            {
                assembly = assembly.assembly,
                types    = assembly.types.SelectMany(x => x.Value).ToList(),
                url      = assembly.url,
            })
                             .Cast <ScannedRecord>());

            // Find localization files for the prefix.
            targets.AddRange(GameDatabase.Instance.GetConfigs("Localization")
                             .Where(x => x.url.StartsWith(lookupPrefix, StringComparison.OrdinalIgnoreCase) &&
                                    x.config.GetNodes(Localizer.CurrentLanguage).Any())
                             .Select(url => new ConfigRecord()
            {
                url      = url.url,
                filePath = url.parent.fullPath,
                lang     = Localizer.CurrentLanguage,
                node     = url.config.GetNodes(Localizer.CurrentLanguage).FirstOrDefault(),
            })
                             .Cast <ScannedRecord>());

            if (targets.Count == 0)
            {
                targets.Add(new StubRecord()
                {
                    stubText = NothingFoundForPrefixTxt,
                });
            }
        }
示例#6
0
 /// <inheritdoc/>
 public override string ToString()
 {
     return(string.Format(
                "{0}, lang={1} ({2} strings)",
                KspPaths.MakeRelativePathToGameData(url), lang, node.GetValues().Length));
 }
示例#7
0
 /// <inheritdoc/>
 public override string ToString()
 {
     return(string.Format("{0}, v{1} ({2} modules)",
                          KspPaths.MakeRelativePathToGameData(assembly.Location),
                          assembly.GetName().Version, types.Count));
 }
 /// <inheritdoc/>
 public override string ToString() {
   return ConfigRecordTxt.Format(
       KspPaths.MakeRelativePathToGameData(url), lang, node.GetValues().Length);
 }
 /// <inheritdoc/>
 public override string ToString() {
   return AssemblyRecordTxt.Format(
       KspPaths.MakeRelativePathToGameData(assembly.Location),
       assembly.GetName().Version.ToString(), types.Count);
 }