Example #1
0
        /// <summary>
        /// Separates the retrived JSON information into body and meta
        /// </summary>
        protected YleJsonParser(string json, bool caseSensitive)
        {
            var hashtable = SimpleJsonImporter.Import(json, caseSensitive);

            body       = (ArrayList)hashtable["data"];
            dataLength = body.Count;
        }
        private bool LoadContentDirectory(Mod mod, string contentDirectoryPath)
        {
            // Check if there is a `content` directory first, but don't require one for the mod to be valid
            if (!Directory.Exists(contentDirectoryPath))
            {
                Logging.Warn("No content directory found; content files must be placed in a 'content' subdirectory");
                return(true);
            }

            // Search the directory for content files
            foreach (var contentFileName in Directory.GetFiles(contentDirectoryPath, "*.json"))
            {
                var contentFileData = SimpleJsonImporter.Import(File.ReadAllText(contentFileName));
                if (contentFileData == null)
                {
                    Logging.Error("Invalid content file JSON '" + Path.GetFileName(contentFileName) + "'");
                    return(false);
                }

                foreach (DictionaryEntry contentEntry in contentFileData)
                {
                    var category = contentEntry.Key as string;
                    if (!(contentEntry.Value is ArrayList items))
                    {
                        Logging.Warn($"Unexpected type for items in category '{category}', should be array");
                        continue;
                    }
                    if (!_entityCategories.Contains(category))
                    {
                        Logging.Warn($"Invalid content category '{category}', ignoring");
                        continue;
                    }
                    mod.AddContent(category, items);
                }
            }

            // Search all subdirectories for more content files
            return(Directory.GetDirectories(contentDirectoryPath).All(
                       contentSubDirectoryPath => LoadContentDirectory(mod, contentSubDirectoryPath)));
        }
Example #3
0
    /* ------------------------------------------------------------------------------------- */
    /* ----------------------- Using the SimpleJson functions ------------------------------- */

    private Hashtable ImportJsonString(string aString)
    {
        AddToLog("Importing Json string");
        return(SimpleJsonImporter.Import(aString, caseInsensitive));
    }
        public void LoadAll()
        {
            Logging.Info("Loading all mods");
            Mods.Clear();

            // Check if the mods folder exists
            if (!Directory.Exists(_modsFolder))
            {
                Directory.CreateDirectory(_modsFolder);
                Logging.Warn("Mods folder not found, creating");
            }

            // Load the mod data from the file system
            foreach (var modFolder in Directory.GetDirectories(_modsFolder))
            {
                var modId = Path.GetFileName(modFolder);
                if (modId == null)
                {
                    Logging.Info("Unexpected null directory name for mod");
                    continue;
                }
                Logging.Info("Loading mod " + modId);

                // Find the mod's manifest and load its data
                var manifestPath = Path.Combine(modFolder, ModManifestFileName);
                if (!File.Exists(manifestPath))
                {
                    Logging.Error("Mod manifest not found, skipping mod");
                    continue;
                }
                var manifestData = SimpleJsonImporter.Import(File.ReadAllText(manifestPath));
                if (manifestData == null)
                {
                    Logging.Error("Invalid mod manifest JSON, skipping mod");
                    continue;
                }

                // Initialize the mod with its manifest information
                var mod    = new Mod(modId);
                var errors = mod.FromManifest(manifestData);
                if (errors.Count > 0)
                {
                    foreach (var error in errors)
                    {
                        Logging.Error(error);
                    }
                    Logging.Error("Encountered errors in manifest, skipping mod");
                    continue;
                }

                // Collect the mod's content files
                // If an error occurs in the process, discard the mod
                if (!LoadContentDirectory(mod, Path.Combine(modFolder, "content")))
                {
                    Logging.Error("Encountered errors in content, skipping mod");
                    continue;
                }

                // Collect the mod's images
                // If an error occurs in the process, discard the mod
                if (!LoadAllImagesDirectory(mod, Path.Combine(modFolder, "images")))
                {
                    Logging.Error("Encountered errors in images, skipping mod");
                    continue;
                }

                // Add the mod to the collection
                Logging.Info($"Loaded mod '{modId}'");
                Mods.Add(modId, mod);
            }
            Logging.Info("Loaded all mods");

            // Check the dependencies to see if there are any missing or invalid ones
            foreach (var mod in Mods)
            {
                foreach (var dependency in mod.Value.Dependencies)
                {
                    if (!Mods.ContainsKey(dependency.ModId))
                    {
                        Logging.Warn($"Dependency '{dependency.ModId}' for '{mod.Key}' not found ");
                    }
                    else
                    {
                        var  availableVersion = Mods[dependency.ModId].Version;
                        bool isVersionValid;
                        switch (dependency.VersionOperator)
                        {
                        case DependencyOperator.LessThan:
                            isVersionValid = availableVersion < dependency.Version;
                            break;

                        case DependencyOperator.LessThanOrEqual:
                            isVersionValid = availableVersion <= dependency.Version;
                            break;

                        case DependencyOperator.GreaterThan:
                            isVersionValid = availableVersion > dependency.Version;
                            break;

                        case DependencyOperator.GreaterThanOrEqual:
                            isVersionValid = availableVersion >= dependency.Version;
                            break;

                        case DependencyOperator.Equal:
                            isVersionValid = availableVersion == dependency.Version;
                            break;

                        default:
                            isVersionValid = true;
                            break;
                        }

                        if (!isVersionValid)
                        {
                            Logging.Warn($"Dependency '{dependency.ModId}' for '{mod.Key}' has incompatible version");
                        }
                    }
                }
            }
        }