public override void OnStartup()
        {
            string dataPath = FolderPath;

            _dataPath = dataPath;

            GetOrCreateZippedModsFolder();
            GetOrCreateModsFolder();
            GetOrCreateSpecialModsDataFolder();

            string modsFolder = GetOrCreateModsFolder();

            string[] modFolders = Directory.GetDirectories(modsFolder);
            foreach (string folder in modFolders)
            {
                string dataFilePath = folder + "/" + ModsManager.MOD_INFO_FILE_NAME;
                if (!File.Exists(dataFilePath))
                {
                    continue;
                }

                ModInfo loadedModInfo;
                string  json = File.ReadAllText(dataFilePath);
                try
                {
                    loadedModInfo = JsonConvert.DeserializeObject <ModInfo>(json);
                }
                catch (JsonException e)
                {
                    OutputConsole.WriteLine("Failed to read mod data file for mod: " + folder);
                    continue;
                }
                if (!loadedModInfo.AreAllEssentialFieldsAssigned(out string msg))
                {
                    OutputConsole.WriteLine("Missing fields for mod: " + folder + ", " + msg);
                    continue;
                }
                loadedModInfo.FixFieldValues();

                _loadedMods.Add(loadedModInfo.UniqueID, loadedModInfo);
                _loadedModJsons.Add(loadedModInfo.UniqueID, json);
            }

            string specialModDataFolder = GetOrCreateSpecialModsDataFolder();

            string[] specialModDataFiles = Directory.GetFiles(specialModDataFolder);
            foreach (string specialModDatFile in specialModDataFiles)
            {
                string         json           = File.ReadAllText(specialModDatFile);
                SpecialModData specialModData = SpecialModData.FromJson(json);
                _loadedSpecialModData.Add(specialModData.ModId, specialModData);
            }

            foreach (KeyValuePair <string, ModInfo> loadedMod in _loadedMods)
            {
                ZipMod(loadedMod.Key);
            }
        }
        public void CreateAndAddSpecialModDataFormMod(string modID, string ownerID)
        {
            ModInfo        modInfo        = GetModInfoFromId(modID);
            SpecialModData specialModData = SpecialModData.CreateNewSpecialModData(modInfo, ownerID);

            _loadedSpecialModData.Add(specialModData.ModId, specialModData);

            SaveSpecialModData(specialModData);
        }
        public string GetSpecialModInfoJsonFromId(string id)
        {
            SpecialModData modData = GetSpecialModInfoFromId(id);

            if (modData == null)
            {
                return(null);
            }

            return(modData.ToJson());
        }
Beispiel #4
0
        public static SpecialModData CreateNewSpecialModData(ModInfo mod, string ownerID)
        {
            SpecialModData specialModData = new SpecialModData();

            specialModData.Likes       = 0;
            specialModData.Downloads   = 0;
            specialModData.ModId       = mod.UniqueID;
            specialModData.OwnerID     = ownerID;
            specialModData.PostedDate  = (ulong)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            specialModData.UpdatedDate = specialModData.PostedDate;

            specialModData.Verified = false;

            specialModData.Comments = new List <Comment>();
            specialModData.Reports  = new List <ModReport>();

            return(specialModData);
        }
        public void SaveSpecialModData(SpecialModData data)
        {
            string path = GetSpecialModDataJsonPath(data.ModId);

            File.WriteAllText(path, data.ToJson());
        }
        public bool TryUploadModFromZip(byte[] zipData, string sessionID, out ModInfo modInfo, out string error)
        {
            ModInfo loadedModInfo;

            try
            {
                string tempPath = Path.GetTempPath();
                string guid     = Guid.NewGuid().ToString();

                string tempZipPath = tempPath + guid + ".zip";
                string tempModPath = tempPath + guid + "/";
                Directory.CreateDirectory(tempModPath);

                File.WriteAllBytes(tempZipPath, zipData);
                ZipFile.ExtractToDirectory(tempZipPath, tempModPath);


                string[] directories = Directory.GetDirectories(tempModPath);
                if (Directory.GetFiles(tempModPath).Length == 0 && directories.Length == 1) // if there is a single folder and no files then the mod is probably in that folder
                {
                    tempModPath = directories[0];
                }

                string modInfoPath = tempModPath + ModsManager.MOD_INFO_FILE_NAME;

                if (!File.Exists(modInfoPath))
                {
                    error   = "Could not find the " + ModsManager.MOD_INFO_FILE_NAME + " file :/";
                    modInfo = null;
                    return(false);
                }

                string json = File.ReadAllText(modInfoPath);
                try
                {
                    loadedModInfo = JsonConvert.DeserializeObject <ModInfo>(json);
                }
                catch (JsonException e)
                {
                    error   = "Error deserializing" + ModsManager.MOD_INFO_FILE_NAME + " file";
                    modInfo = null;
                    return(false);
                }
                if (!loadedModInfo.AreAllEssentialFieldsAssigned(out string msg))
                {
                    error   = msg;
                    modInfo = null;
                    return(false);
                }
                loadedModInfo.FixFieldValues();

                string playerId = SessionsManager.Instance.GetPlayerIdFromSession(sessionID);

                SpecialModData oldModWithSameID = GetSpecialModInfoFromId(loadedModInfo.UniqueID);
                if (oldModWithSameID != null)
                {
                    if (oldModWithSameID.OwnerID != playerId)
                    {
                        error   = "You cannot override a mod that you do not own";
                        modInfo = null;
                        return(false);
                    }
                }

                string newFolderPath = GetModFolderPath(loadedModInfo.UniqueID);
                if (Directory.Exists(newFolderPath))
                {
                    Utils.RecursivelyDeleteFolder(newFolderPath);
                }

                Directory.CreateDirectory(newFolderPath);

                Utils.CopyFilesRecursively(new DirectoryInfo(tempModPath), new DirectoryInfo(newFolderPath));

                File.Delete(tempZipPath);
                Utils.RecursivelyDeleteFolder(tempModPath);
            }
            catch (Exception e)
            {
                modInfo = null;
                error   = "something went wrong while handaling the zip file";
                OutputConsole.WriteLine(e.ToString());
                return(false);
            }

            if (_loadedMods.ContainsKey(loadedModInfo.UniqueID))
            {
                _loadedMods.Remove(loadedModInfo.UniqueID);
            }
            if (_loadedModJsons.ContainsKey(loadedModInfo.UniqueID))
            {
                _loadedModJsons.Remove(loadedModInfo.UniqueID);
            }

            _loadedMods.Add(loadedModInfo.UniqueID, loadedModInfo);
            _loadedModJsons.Add(loadedModInfo.UniqueID, JsonConvert.SerializeObject(loadedModInfo));

            ZipMod(loadedModInfo.UniqueID);

            if (_loadedSpecialModData.ContainsKey(loadedModInfo.UniqueID))
            {
                _loadedSpecialModData[loadedModInfo.UniqueID].UpdatedDate = (ulong)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            }
            else
            {
                CreateAndAddSpecialModDataFormMod(loadedModInfo.UniqueID, SessionsManager.Instance.GetPlayerIdFromSession(sessionID));
            }

            modInfo = loadedModInfo;
            error   = null;
            return(true);
        }